1 /* mkrmdir.c -- BSD compatible directory functions for System V
2 Copyright (C) 1988, 1990 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 #include <sys/types.h>
25 #ifdef STAT_MACROS_BROKEN
29 #endif /* STAT_MACROS_BROKEN. */
31 #if !defined(S_ISDIR) && defined(S_IFDIR)
32 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
35 /* mkdir and rmdir adapted from GNU tar. */
37 /* Make directory DPATH, with permission mode DMODE.
39 Written by Robert Rother, Mariah Corporation, August 1985
40 (sdcsvax!rmr or rmr@uscd). If you want it, it's yours.
42 Severely hacked over by John Gilmore to make a 4.2BSD compatible
43 subroutine. 11Mar86; hoptoad!gnu
45 Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
46 subroutine didn't return EEXIST. It does now. */
56 if (stat (dpath, &statbuf) == 0)
58 errno = EEXIST; /* stat worked, so it already exists. */
62 /* If stat fails for a reason other than non-existence, return error. */
69 case -1: /* Cannot fork. */
70 return -1; /* errno is set already. */
72 case 0: /* Child process. */
73 /* Cheap hack to set mode of new directory. Since this child
74 process is going away anyway, we zap its umask.
75 This won't suffice to set SUID, SGID, etc. on this
76 directory, so the parent process calls chmod afterward. */
77 status = umask (0); /* Get current umask. */
78 umask (status | (0777 & ~dmode)); /* Set for mkdir. */
79 execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
82 default: /* Parent process. */
83 while (wait (&status) != cpid) /* Wait for kid to finish. */
88 errno = EIO; /* /bin/mkdir failed. */
91 return chmod (dpath, dmode);
95 /* Remove directory DPATH.
96 Return 0 if successful, -1 if not. */
105 if (stat (dpath, &statbuf) != 0)
106 return -1; /* stat set errno. */
108 if (!S_ISDIR (statbuf.st_mode))
117 case -1: /* Cannot fork. */
118 return -1; /* errno is set already. */
120 case 0: /* Child process. */
121 execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
124 default: /* Parent process. */
125 while (wait (&status) != cpid) /* Wait for kid to finish. */
130 errno = EIO; /* /bin/rmdir failed. */