1 | /*
|
---|
2 | * Copyright (c) 1983, 1992, 1993
|
---|
3 | * The Regents of the University of California. All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | * 1. Redistributions of source code must retain the above copyright
|
---|
9 | * notice, this list of conditions and the following disclaimer.
|
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer in the
|
---|
12 | * documentation and/or other materials provided with the distribution.
|
---|
13 | * 4. Neither the name of the University nor the names of its contributors
|
---|
14 | * may be used to endorse or promote products derived from this software
|
---|
15 | * without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
27 | * SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #if 0
|
---|
31 | #ifndef lint
|
---|
32 | static char const copyright[] =
|
---|
33 | "@(#) Copyright (c) 1983, 1992, 1993\n\
|
---|
34 | The Regents of the University of California. All rights reserved.\n";
|
---|
35 | #endif /* not lint */
|
---|
36 |
|
---|
37 | #ifndef lint
|
---|
38 | static char sccsid[] = "@(#)mkdir.c 8.2 (Berkeley) 1/25/94";
|
---|
39 | #endif /* not lint */
|
---|
40 | #include <sys/cdefs.h>
|
---|
41 | __FBSDID("$FreeBSD: src/bin/mkdir/mkdir.c,v 1.28 2004/04/06 20:06:48 markm Exp $");
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #include <sys/types.h>
|
---|
45 | #include <sys/stat.h>
|
---|
46 |
|
---|
47 | #include "err.h"
|
---|
48 | #include <errno.h>
|
---|
49 | #ifndef _MSC_VER
|
---|
50 | # include <libgen.h>
|
---|
51 | #endif
|
---|
52 | #include <stdio.h>
|
---|
53 | #include <stdlib.h>
|
---|
54 | #include <string.h>
|
---|
55 | #include <sysexits.h>
|
---|
56 | #include <unistd.h>
|
---|
57 | #include "getopt.h"
|
---|
58 | #ifdef _MSC_VER
|
---|
59 | # include <malloc.h>
|
---|
60 | # include "mscfakes.h"
|
---|
61 | #endif
|
---|
62 | #include "kmkbuiltin.h"
|
---|
63 |
|
---|
64 |
|
---|
65 | static int vflag;
|
---|
66 | static struct option long_options[] =
|
---|
67 | {
|
---|
68 | { "help", no_argument, 0, 261 },
|
---|
69 | { "version", no_argument, 0, 262 },
|
---|
70 | { 0, 0, 0, 0 },
|
---|
71 | };
|
---|
72 |
|
---|
73 |
|
---|
74 | extern void * bsd_setmode(const char *p);
|
---|
75 | extern mode_t bsd_getmode(const void *bbox, mode_t omode);
|
---|
76 |
|
---|
77 | static int build(char *, mode_t);
|
---|
78 | static int usage(FILE *);
|
---|
79 |
|
---|
80 |
|
---|
81 | int
|
---|
82 | kmk_builtin_mkdir(int argc, char *argv[], char **envp)
|
---|
83 | {
|
---|
84 | int ch, exitval, success, pflag;
|
---|
85 | mode_t omode, *set = (mode_t *)NULL;
|
---|
86 | char *mode;
|
---|
87 |
|
---|
88 | omode = pflag = 0;
|
---|
89 | mode = NULL;
|
---|
90 |
|
---|
91 | /* reinitialize globals */
|
---|
92 | vflag = 0;
|
---|
93 |
|
---|
94 | /* kmk: reset getopt and set progname */
|
---|
95 | g_progname = argv[0];
|
---|
96 | opterr = 1;
|
---|
97 | optarg = NULL;
|
---|
98 | optopt = 0;
|
---|
99 | optind = 0; /* init */
|
---|
100 | while ((ch = getopt_long(argc, argv, "m:pv", long_options, NULL)) != -1)
|
---|
101 | switch(ch) {
|
---|
102 | case 'm':
|
---|
103 | mode = optarg;
|
---|
104 | break;
|
---|
105 | case 'p':
|
---|
106 | pflag = 1;
|
---|
107 | break;
|
---|
108 | case 'v':
|
---|
109 | vflag = 1;
|
---|
110 | break;
|
---|
111 | case 261:
|
---|
112 | usage(stdout);
|
---|
113 | return 0;
|
---|
114 | case 262:
|
---|
115 | return kbuild_version(argv[0]);
|
---|
116 | case '?':
|
---|
117 | default:
|
---|
118 | return usage(stderr);
|
---|
119 | }
|
---|
120 |
|
---|
121 | argc -= optind;
|
---|
122 | argv += optind;
|
---|
123 | if (argv[0] == NULL)
|
---|
124 | return usage(stderr);
|
---|
125 |
|
---|
126 | if (mode == NULL) {
|
---|
127 | omode = S_IRWXU | S_IRWXG | S_IRWXO;
|
---|
128 | } else {
|
---|
129 | if ((set = bsd_setmode(mode)) == NULL)
|
---|
130 | return errx(1, "invalid file mode: %s", mode);
|
---|
131 | omode = bsd_getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
|
---|
132 | free(set);
|
---|
133 | }
|
---|
134 |
|
---|
135 | for (exitval = 0; *argv != NULL; ++argv) {
|
---|
136 | success = 1;
|
---|
137 | if (pflag) {
|
---|
138 | if (build(*argv, omode))
|
---|
139 | success = 0;
|
---|
140 | } else if (mkdir(*argv, omode) < 0) {
|
---|
141 | if (errno == ENOTDIR || errno == ENOENT)
|
---|
142 | warn("%s", dirname(*argv));
|
---|
143 | else
|
---|
144 | warn("%s", *argv);
|
---|
145 | success = 0;
|
---|
146 | } else if (vflag)
|
---|
147 | (void)printf("%s\n", *argv);
|
---|
148 |
|
---|
149 | if (!success)
|
---|
150 | exitval = 1;
|
---|
151 | /*
|
---|
152 | * The mkdir() and umask() calls both honor only the low
|
---|
153 | * nine bits, so if you try to set a mode including the
|
---|
154 | * sticky, setuid, setgid bits you lose them. Don't do
|
---|
155 | * this unless the user has specifically requested a mode,
|
---|
156 | * as chmod will (obviously) ignore the umask.
|
---|
157 | */
|
---|
158 | if (success && mode != NULL && chmod(*argv, omode) == -1) {
|
---|
159 | warn("%s", *argv);
|
---|
160 | exitval = 1;
|
---|
161 | }
|
---|
162 | }
|
---|
163 | return exitval;
|
---|
164 | }
|
---|
165 |
|
---|
166 | static int
|
---|
167 | build(char *path, mode_t omode)
|
---|
168 | {
|
---|
169 | struct stat sb;
|
---|
170 | mode_t numask, oumask;
|
---|
171 | int first, last, retval;
|
---|
172 | char *p;
|
---|
173 |
|
---|
174 | const size_t len = strlen(path);
|
---|
175 | p = alloca(len + 1);
|
---|
176 | path = memcpy(p, path, len + 1);
|
---|
177 |
|
---|
178 | #if defined(_MSC_VER) || defined(__EMX__)
|
---|
179 | /* correct slashes. */
|
---|
180 | p = strchr(path, '\\');
|
---|
181 | while (p) {
|
---|
182 | *p++ = '/';
|
---|
183 | p = strchr(p, '\\');
|
---|
184 | }
|
---|
185 | #endif
|
---|
186 |
|
---|
187 | p = path;
|
---|
188 | oumask = 0;
|
---|
189 | retval = 0;
|
---|
190 | #if defined(_MSC_VER) || defined(__EMX__)
|
---|
191 | if ( ( (p[0] >= 'A' && p[0] <= 'Z')
|
---|
192 | || (p[0] >= 'a' && p[0] <= 'z'))
|
---|
193 | && p[1] == ':')
|
---|
194 | p += 2; /* skip the drive letter */
|
---|
195 | else if ( p[0] == '/'
|
---|
196 | && p[1] == '/'
|
---|
197 | && p[2] != '/')
|
---|
198 | {
|
---|
199 | /* UNC */
|
---|
200 | p += 2;
|
---|
201 | while (*p && *p != '/') /* server */
|
---|
202 | p++;
|
---|
203 | while (*p == '/')
|
---|
204 | p++;
|
---|
205 | while (*p && *p != '/') /* share */
|
---|
206 | p++;
|
---|
207 | }
|
---|
208 | #endif
|
---|
209 | if (p[0] == '/') /* Skip leading '/'. */
|
---|
210 | ++p;
|
---|
211 | for (first = 1, last = 0; !last ; ++p) {
|
---|
212 | if (p[0] == '\0')
|
---|
213 | last = 1;
|
---|
214 | else if (p[0] != '/')
|
---|
215 | continue;
|
---|
216 | *p = '\0';
|
---|
217 | if (!last && p[1] == '\0')
|
---|
218 | last = 1;
|
---|
219 | if (first) {
|
---|
220 | /*
|
---|
221 | * POSIX 1003.2:
|
---|
222 | * For each dir operand that does not name an existing
|
---|
223 | * directory, effects equivalent to those cased by the
|
---|
224 | * following command shall occcur:
|
---|
225 | *
|
---|
226 | * mkdir -p -m $(umask -S),u+wx $(dirname dir) &&
|
---|
227 | * mkdir [-m mode] dir
|
---|
228 | *
|
---|
229 | * We change the user's umask and then restore it,
|
---|
230 | * instead of doing chmod's.
|
---|
231 | */
|
---|
232 | oumask = umask(0);
|
---|
233 | numask = oumask & ~(S_IWUSR | S_IXUSR);
|
---|
234 | (void)umask(numask);
|
---|
235 | first = 0;
|
---|
236 | }
|
---|
237 | if (last)
|
---|
238 | (void)umask(oumask);
|
---|
239 | if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
|
---|
240 | if (errno == EEXIST || errno == EISDIR || errno == ENOSYS /* (solaris crap) */) {
|
---|
241 | if (stat(path, &sb) < 0) {
|
---|
242 | warn("%s", path);
|
---|
243 | retval = 1;
|
---|
244 | break;
|
---|
245 | } else if (!S_ISDIR(sb.st_mode)) {
|
---|
246 | if (last)
|
---|
247 | errno = EEXIST;
|
---|
248 | else
|
---|
249 | errno = ENOTDIR;
|
---|
250 | warn("%s", path);
|
---|
251 | retval = 1;
|
---|
252 | break;
|
---|
253 | }
|
---|
254 | } else {
|
---|
255 | warn("%s", path);
|
---|
256 | retval = 1;
|
---|
257 | break;
|
---|
258 | }
|
---|
259 | } else if (vflag)
|
---|
260 | printf("%s\n", path);
|
---|
261 | if (!last)
|
---|
262 | *p = '/';
|
---|
263 | }
|
---|
264 | if (!first && !last)
|
---|
265 | (void)umask(oumask);
|
---|
266 | return (retval);
|
---|
267 | }
|
---|
268 |
|
---|
269 | static int
|
---|
270 | usage(FILE *pf)
|
---|
271 | {
|
---|
272 | fprintf(pf, "usage: %s [-pv] [-m mode] directory ...\n"
|
---|
273 | " or: %s --help\n"
|
---|
274 | " or: %s --version\n",
|
---|
275 | g_progname, g_progname, g_progname);
|
---|
276 | return EX_USAGE;
|
---|
277 | }
|
---|