1 | /* mkdir.c -- BSD compatible make directory function for System V
|
---|
2 | Copyright (C) 1988, 1990 Free Software Foundation, Inc.
|
---|
3 |
|
---|
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)
|
---|
7 | any later version.
|
---|
8 |
|
---|
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.
|
---|
13 |
|
---|
14 | You should have received a copy of the GNU General Public
|
---|
15 | License along with this program; if not, write to the Free
|
---|
16 | Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
17 | Boston, MA 02110-1301 USA. */
|
---|
18 |
|
---|
19 | #ifdef HAVE_CONFIG_H
|
---|
20 | #include <config.h>
|
---|
21 | #endif
|
---|
22 |
|
---|
23 | #include <sys/types.h>
|
---|
24 | #include <sys/stat.h>
|
---|
25 | #include <errno.h>
|
---|
26 |
|
---|
27 | #if !HAVE_DECL_ERRNO
|
---|
28 | extern int errno;
|
---|
29 | #endif
|
---|
30 |
|
---|
31 | #ifdef STAT_MACROS_BROKEN
|
---|
32 | #undef S_ISDIR
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | #if !defined(S_ISDIR) && defined(S_IFDIR)
|
---|
36 | #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | #include "safe-stat.h"
|
---|
40 |
|
---|
41 | /* mkdir adapted from GNU tar. */
|
---|
42 |
|
---|
43 | /* Make directory DPATH, with permission mode DMODE.
|
---|
44 |
|
---|
45 | Written by Robert Rother, Mariah Corporation, August 1985
|
---|
46 | (sdcsvax!rmr or rmr@uscd). If you want it, it's yours.
|
---|
47 |
|
---|
48 | Severely hacked over by John Gilmore to make a 4.2BSD compatible
|
---|
49 | subroutine. 11Mar86; hoptoad!gnu
|
---|
50 |
|
---|
51 | Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
|
---|
52 | subroutine didn't return EEXIST. It does now. */
|
---|
53 |
|
---|
54 | int
|
---|
55 | mkdir (dpath, dmode)
|
---|
56 | char *dpath;
|
---|
57 | int dmode;
|
---|
58 | {
|
---|
59 | int cpid, status;
|
---|
60 | struct stat statbuf;
|
---|
61 |
|
---|
62 | if (SAFE_STAT (dpath, &statbuf) == 0)
|
---|
63 | {
|
---|
64 | errno = EEXIST; /* stat worked, so it already exists. */
|
---|
65 | return -1;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /* If stat fails for a reason other than non-existence, return error. */
|
---|
69 | if (errno != ENOENT)
|
---|
70 | return -1;
|
---|
71 |
|
---|
72 | cpid = fork ();
|
---|
73 | switch (cpid)
|
---|
74 | {
|
---|
75 | case -1: /* Cannot fork. */
|
---|
76 | return -1; /* errno is already set. */
|
---|
77 |
|
---|
78 | case 0: /* Child process. */
|
---|
79 | /* Cheap hack to set mode of new directory. Since this child
|
---|
80 | process is going away anyway, we zap its umask.
|
---|
81 | This won't suffice to set SUID, SGID, etc. on this
|
---|
82 | directory, so the parent process calls chmod afterward. */
|
---|
83 | status = umask (0); /* Get current umask. */
|
---|
84 | umask (status | (0777 & ~dmode)); /* Set for mkdir. */
|
---|
85 | execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
|
---|
86 | _exit (1);
|
---|
87 |
|
---|
88 | default: /* Parent process. */
|
---|
89 | /* Wait for kid to finish. */
|
---|
90 | while (wait (&status) != cpid)
|
---|
91 | /* Do nothing. */ ;
|
---|
92 |
|
---|
93 | if (status & 0xFFFF)
|
---|
94 | {
|
---|
95 | /* /bin/mkdir failed. */
|
---|
96 | errno = EIO;
|
---|
97 | return -1;
|
---|
98 | }
|
---|
99 | return chmod (dpath, dmode);
|
---|
100 | }
|
---|
101 | }
|
---|