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