1 /* mkdir.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. */
19 #if defined (CONFIG_BROKETS)
20 /* We use <config.h> instead of "config.h" so that a compilation
21 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
22 (which it would do because it found this file in $srcdir). */
29 #include <sys/types.h>
36 #ifdef STAT_MACROS_BROKEN
40 #endif /* STAT_MACROS_BROKEN. */
42 #if !defined(S_ISDIR) && defined(S_IFDIR)
43 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
46 #include "safe-stat.h"
48 /* mkdir and rmdir adapted from GNU tar. */
50 /* Make directory DPATH, with permission mode DMODE.
52 Written by Robert Rother, Mariah Corporation, August 1985
53 (sdcsvax!rmr or rmr@uscd). If you want it, it's yours.
55 Severely hacked over by John Gilmore to make a 4.2BSD compatible
56 subroutine. 11Mar86; hoptoad!gnu
58 Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
59 subroutine didn't return EEXIST. It does now. */
69 if (SAFE_STAT (dpath, &statbuf) == 0)
71 errno = EEXIST; /* stat worked, so it already exists. */
75 /* If stat fails for a reason other than non-existence, return error. */
82 case -1: /* Cannot fork. */
83 return -1; /* errno is set already. */
85 case 0: /* Child process. */
86 /* Cheap hack to set mode of new directory. Since this child
87 process is going away anyway, we zap its umask.
88 This won't suffice to set SUID, SGID, etc. on this
89 directory, so the parent process calls chmod afterward. */
90 status = umask (0); /* Get current umask. */
91 umask (status | (0777 & ~dmode)); /* Set for mkdir. */
92 execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
95 default: /* Parent process. */
96 while (wait (&status) != cpid) /* Wait for kid to finish. */
101 errno = EIO; /* /bin/mkdir failed. */
104 return chmod (dpath, dmode);
108 /* Remove directory DPATH.
109 Return 0 if successful, -1 if not. */
118 if (SAFE_STAT (dpath, &statbuf) != 0)
119 return -1; /* stat set errno. */
121 if (!S_ISDIR (statbuf.st_mode))
130 case -1: /* Cannot fork. */
131 return -1; /* errno is set already. */
133 case 0: /* Child process. */
134 execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
137 default: /* Parent process. */
138 while (wait (&status) != cpid) /* Wait for kid to finish. */
143 errno = EIO; /* /bin/rmdir failed. */