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 /* mkdir and rmdir adapted from GNU tar. */
27 /* Make directory DPATH, with permission mode DMODE.
29 Written by Robert Rother, Mariah Corporation, August 1985
30 (sdcsvax!rmr or rmr@uscd). If you want it, it's yours.
32 Severely hacked over by John Gilmore to make a 4.2BSD compatible
33 subroutine. 11Mar86; hoptoad!gnu
35 Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
36 subroutine didn't return EEXIST. It does now. */
46 if (stat (dpath
, &statbuf
) == 0)
48 errno
= EEXIST
; /* stat worked, so it already exists. */
52 /* If stat fails for a reason other than non-existence, return error. */
59 case -1: /* Cannot fork. */
60 return -1; /* errno is set already. */
62 case 0: /* Child process. */
63 /* Cheap hack to set mode of new directory. Since this child
64 process is going away anyway, we zap its umask.
65 This won't suffice to set SUID, SGID, etc. on this
66 directory, so the parent process calls chmod afterward. */
67 status
= umask (0); /* Get current umask. */
68 umask (status
| (0777 & ~dmode
)); /* Set for mkdir. */
69 execl ("/bin/mkdir", "mkdir", dpath
, (char *) 0);
72 default: /* Parent process. */
73 while (wait (&status
) != cpid
) /* Wait for kid to finish. */
78 errno
= EIO
; /* /bin/mkdir failed. */
81 return chmod (dpath
, dmode
);
85 /* Remove directory DPATH.
86 Return 0 if successful, -1 if not. */
95 if (stat (dpath
, &statbuf
) != 0)
96 return -1; /* stat set errno. */
98 if ((statbuf
.st_mode
& S_IFMT
) != S_IFDIR
)
107 case -1: /* Cannot fork. */
108 return -1; /* errno is set already. */
110 case 0: /* Child process. */
111 execl ("/bin/rmdir", "rmdir", dpath
, (char *) 0);
114 default: /* Parent process. */
115 while (wait (&status
) != cpid
) /* Wait for kid to finish. */
120 errno
= EIO
; /* /bin/rmdir failed. */