source: trunk/src/gmake/kmkbuiltin/mkdir.c@ 369

Last change on this file since 369 was 363, checked in by bird, 20 years ago

drive letter and unc for windows and OS/2.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 KB
Line 
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
32static 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
38static char sccsid[] = "@(#)mkdir.c 8.2 (Berkeley) 1/25/94";
39#endif /* not lint */
40#endif
41#ifndef _MSC_VER
42#include <sys/cdefs.h>
43#endif
44//__FBSDID("$FreeBSD: src/bin/mkdir/mkdir.c,v 1.28 2004/04/06 20:06:48 markm Exp $");
45
46#include <sys/types.h>
47#include <sys/stat.h>
48
49//#include <err.h>
50#include <errno.h>
51#ifndef _MSC_VER
52#include <libgen.h>
53#endif
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#ifndef _MSC_VER
58#include <sysexits.h>
59#include <unistd.h>
60#endif
61
62#ifdef _MSC_VER
63#define setmode setmode_msc
64#include <stdarg.h>
65#include <io.h>
66#include <direct.h>
67#undef setmode
68#include "getopt.h"
69
70typedef int mode_t;
71#define EX_USAGE 1
72void warn(const char *fmt, ...)
73{
74 int err = errno;
75 va_list args;
76 va_start(args, fmt);
77 fprintf(stderr, "mkdir: ");
78 vfprintf(stderr, fmt, args);
79 fprintf(stderr, ": %s\n", strerror(err));
80 va_end(args);
81}
82
83int mkdir_msc(const char *path, mode_t mode)
84{
85 int rc = mkdir(path);
86 if (rc)
87 {
88 int len = strlen(path);
89 if (len > 0 && (path[len - 1] == '/' || path[len - 1] == '\\'))
90 {
91 char *str = strdup(path);
92 while (len > 0 && (str[len - 1] == '/' || str[len - 1] == '\\'))
93 str[--len] = '\0';
94 rc = mkdir(str);
95 free(str);
96 }
97 }
98 return rc;
99}
100#define mkdir(a,b) mkdir_msc(a,b)
101
102char *dirname(char *path)
103{
104 return path;
105}
106
107#define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
108#define S_IRWXG 0000070
109#define S_IRWXO 0000007
110#define S_IRWXU (_S_IREAD | _S_IWRITE | _S_IEXEC)
111#define S_IXUSR _S_IEXEC
112#define S_IWUSR _S_IWRITE
113#define S_IRUSR _S_IREAD
114
115#endif
116
117extern void * setmode(const char *p);
118extern mode_t getmode(const void *bbox, mode_t omode);
119
120static int build(char *, mode_t);
121static int usage(void);
122
123static int vflag;
124
125int
126kmk_builtin_mkdir(int argc, char *argv[])
127{
128 int ch, exitval, success, pflag;
129 mode_t omode, *set = (mode_t *)NULL;
130 char *mode;
131
132 omode = pflag = 0;
133 mode = NULL;
134 vflag = 0;
135 opterr = 1;
136 optarg = NULL;
137 optopt = 0;
138#if defined(__FreeBSD__) || defined(__EMX__)
139 optreset = 1;
140 optind = 1;
141#else
142 optind = 0; /* init */
143#endif
144 while ((ch = getopt(argc, argv, "m:pv")) != -1)
145 switch(ch) {
146 case 'm':
147 mode = optarg;
148 break;
149 case 'p':
150 pflag = 1;
151 break;
152 case 'v':
153 vflag = 1;
154 break;
155 case '?':
156 default:
157 return usage();
158 }
159
160 argc -= optind;
161 argv += optind;
162 if (argv[0] == NULL)
163 return usage();
164
165 if (mode == NULL) {
166 omode = S_IRWXU | S_IRWXG | S_IRWXO;
167 } else {
168 if ((set = setmode(mode)) == NULL) {
169 fprintf(stderr, "%s: invalid file mode: %s\n", mode, argv[0]);
170 return 1;
171 }
172 omode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
173 free(set);
174 }
175
176 for (exitval = 0; *argv != NULL; ++argv) {
177 success = 1;
178 if (pflag) {
179 if (build(*argv, omode))
180 success = 0;
181 } else if (mkdir(*argv, omode) < 0) {
182 if (errno == ENOTDIR || errno == ENOENT)
183 fprintf(stderr, "%s: %s: %s\n", argv[0], dirname(*argv), strerror(errno));
184 else
185 fprintf(stderr, "%s: %s: %s\n", argv[0], *argv, strerror(errno));
186 success = 0;
187 } else if (vflag)
188 (void)printf("%s\n", *argv);
189
190 if (!success)
191 exitval = 1;
192 /*
193 * The mkdir() and umask() calls both honor only the low
194 * nine bits, so if you try to set a mode including the
195 * sticky, setuid, setgid bits you lose them. Don't do
196 * this unless the user has specifically requested a mode,
197 * as chmod will (obviously) ignore the umask.
198 */
199 if (success && mode != NULL && chmod(*argv, omode) == -1) {
200 fprintf(stderr, "%s: %s: %s\n", argv[0], *argv, strerror(errno));
201 exitval = 1;
202 }
203 }
204 return exitval;
205}
206
207static int
208build(char *path, mode_t omode)
209{
210 struct stat sb;
211 mode_t numask, oumask;
212 int first, last, retval;
213 char *p;
214
215 p = path;
216 oumask = 0;
217 retval = 0;
218#if defined(_MSC_VER) || defined(__EMX__)
219 if ( ( (p[0] >= 'A' && p[0] <= 'Z')
220 || (p[0] >= 'a' && p[0] <= 'z'))
221 && p[1] == ':')
222 p += 2;
223 else if ( (p[0] == '/' || p[0] == '\\')
224 && (p[1] == '/' || p[1] == '\\')
225 && (p[2] != '/' && p[2] != '\\'))
226 {
227 char *p2;
228 p += 2;
229 p2 = strpbrk(p, "\\/");
230 if (p2)
231 p = p2 + 1;
232 }
233#endif
234 if (p[0] == '/') /* Skip leading '/'. */
235 ++p;
236 for (first = 1, last = 0; !last ; ++p) {
237 if (p[0] == '\0')
238 last = 1;
239 else if (p[0] != '/')
240 continue;
241 *p = '\0';
242 if (p[1] == '\0')
243 last = 1;
244 if (first) {
245 /*
246 * POSIX 1003.2:
247 * For each dir operand that does not name an existing
248 * directory, effects equivalent to those cased by the
249 * following command shall occcur:
250 *
251 * mkdir -p -m $(umask -S),u+wx $(dirname dir) &&
252 * mkdir [-m mode] dir
253 *
254 * We change the user's umask and then restore it,
255 * instead of doing chmod's.
256 */
257 oumask = umask(0);
258 numask = oumask & ~(S_IWUSR | S_IXUSR);
259 (void)umask(numask);
260 first = 0;
261 }
262 if (last)
263 (void)umask(oumask);
264 if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
265 if (errno == EEXIST || errno == EISDIR) {
266 if (stat(path, &sb) < 0) {
267 warn("%s", path);
268 retval = 1;
269 break;
270 } else if (!S_ISDIR(sb.st_mode)) {
271 if (last)
272 errno = EEXIST;
273 else
274 errno = ENOTDIR;
275 warn("%s", path);
276 retval = 1;
277 break;
278 }
279 } else {
280 warn("%s", path);
281 retval = 1;
282 break;
283 }
284 } else if (vflag)
285 printf("%s\n", path);
286 if (!last)
287 *p = '/';
288 }
289 if (!first && !last)
290 (void)umask(oumask);
291 return (retval);
292}
293
294static int
295usage(void)
296{
297
298 (void)fprintf(stderr, "usage: mkdir [-pv] [-m mode] directory ...\n");
299 return EX_USAGE;
300}
Note: See TracBrowser for help on using the repository browser.