1 /*
2 * Written by Robert Rother, Mariah Corporation, August 1985.
3 *
4 * If you want it, it's yours. All I ask in return is that if you
5 * figure out how to do this in a Bourne Shell script you send me
6 * a copy.
7 * sdcsvax!rmr or rmr@uscd
8 *
9 * Severely hacked over by John Gilmore to make a 4.2BSD compatible
10 * subroutine. 11Mar86; hoptoad!gnu
11 *
12 * Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
13 * subroutine didn't return EEXIST. It does now.
14 */
15
16 #include <sys/stat.h>
17 #include <errno.h>
18 /*
19 * Make a directory.
20 */
21 int
22 mkdir (dpath, dmode)
23 char *dpath;
24 int dmode;
25 {
26 int cpid, status;
27 struct stat statbuf;
28
29 if (stat (dpath, &statbuf) == 0)
30 {
31 errno = EEXIST; /* Stat worked, so it already exists */
32 return -1;
33 }
34
35 /* If stat fails for a reason other than non-existence, return error */
36 if (errno != ENOENT)
37 return -1;
38
39 switch (cpid = fork ())
40 {
41
42 case -1: /* Error in fork() */
43 return (-1); /* Errno is set already */
44
45 case 0: /* Child process */
46 /*
47 * Cheap hack to set mode of new directory. Since this
48 * child process is going away anyway, we zap its umask.
49 * FIXME, this won't suffice to set SUID, SGID, etc. on this
50 * directory. Does anybody care?
51 */
52 status = umask (0); /* Get current umask */
53 status = umask (status | (0777 & ~dmode)); /* Set for mkdir */
54 execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
55 _exit (-1); /* Can't exec /bin/mkdir */
56
57 default: /* Parent process */
58 while (cpid != wait (&status)); /* Wait for kid to finish */
59 }
60
61 if (WIFSIGNALED (status) || WEXITSTATUS (status) != 0)
62 {
63 errno = EIO; /* We don't know why, but */
64 return -1; /* /bin/mkdir failed */
65 }
66
67 return 0;
68 }
69
70 int
71 rmdir (dpath)
72 char *dpath;
73 {
74 int cpid, status;
75 struct stat statbuf;
76
77 if (stat (dpath, &statbuf) != 0)
78 {
79 /* Stat just set errno. We don't have to */
80 return -1;
81 }
82
83 switch (cpid = fork ())
84 {
85
86 case -1: /* Error in fork() */
87 return (-1); /* Errno is set already */
88
89 case 0: /* Child process */
90 execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
91 _exit (-1); /* Can't exec /bin/mkdir */
92
93 default: /* Parent process */
94 while (cpid != wait (&status)); /* Wait for kid to finish */
95 }
96
97 if (WIFSIGNALED (status) || WEXITSTATUS (status) != 0)
98 {
99 errno = EIO; /* We don't know why, but */
100 return -1; /* /bin/rmdir failed */
101 }
102
103 return 0;
104 }