initial import
[glibc.git] / time / emkdir.c
blob5cc62d29e2f41f66782fc9ee02905bb295e7e202
1 #ifndef lint
2 #ifndef NOID
3 static char elsieid[] = "@(#)emkdir.c 8.23";
4 #endif /* !defined NOID */
5 #endif /* !defined lint */
7 #ifndef emkdir
9 /*LINTLIBRARY*/
11 #include "private.h"
13 extern char * imalloc P((int n));
14 extern void ifree P((char * p));
16 static char *
17 quoted(name)
18 register const char * name;
20 register char * result;
21 register char * cp;
22 register int c;
24 if (name == NULL)
25 name = "";
26 result = imalloc((int) (4 * strlen(name) + 3));
27 if (result == NULL)
28 return NULL;
29 cp = result;
30 #ifdef unix
31 *cp++ = '\'';
32 while ((c = *name++) != '\0')
33 if (c == '\'') {
34 *cp++ = c;
35 *cp++ = '\\';
36 *cp++ = c;
37 *cp++ = c;
38 } else *cp++ = c;
39 *cp++ = '\'';
40 #endif /* defined unix */
41 #ifndef unix
42 while ((c = *name++) != '\0')
43 if (c == '/')
44 *cp++ = '\\';
45 else *cp++ = c;
46 #endif /* !defined unix */
47 *cp = '\0';
48 return result;
51 int
52 emkdir(name, mode)
53 const char * name;
54 const int mode;
56 register int result;
57 register const char * format;
58 register char * command;
59 register char * qname;
61 if ((qname = quoted(name)) == NULL)
62 return -1;
63 #ifdef unix
64 format = "mkdir 2>&- %s && chmod 2>&- %o %s";
65 #endif /* defined unix */
66 #ifndef unix
67 format = "mkdir %s";
68 #endif /* !defined unix */
69 command = imalloc((int) (strlen(format) + 2 * strlen(qname) + 20 + 1));
70 if (command == NULL) {
71 ifree(qname);
72 return -1;
74 (void) sprintf(command, format, qname, mode, qname);
75 ifree(qname);
76 result = system(command);
77 ifree(command);
78 return (result == 0) ? 0 : -1;
82 ** UNIX was a registered trademark of UNIX System Laboratories in 1993.
85 #endif /* !defined emkdir */