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

Last change on this file since 362 was 361, checked in by bird, 20 years ago

Ported to MSC

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 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 (p[0] == '/') /* Skip leading '/'. */
219 ++p;
220 for (first = 1, last = 0; !last ; ++p) {
221 if (p[0] == '\0')
222 last = 1;
223 else if (p[0] != '/')
224 continue;
225 *p = '\0';
226 if (p[1] == '\0')
227 last = 1;
228 if (first) {
229 /*
230 * POSIX 1003.2:
231 * For each dir operand that does not name an existing
232 * directory, effects equivalent to those cased by the
233 * following command shall occcur:
234 *
235 * mkdir -p -m $(umask -S),u+wx $(dirname dir) &&
236 * mkdir [-m mode] dir
237 *
238 * We change the user's umask and then restore it,
239 * instead of doing chmod's.
240 */
241 oumask = umask(0);
242 numask = oumask & ~(S_IWUSR | S_IXUSR);
243 (void)umask(numask);
244 first = 0;
245 }
246 if (last)
247 (void)umask(oumask);
248 if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
249 if (errno == EEXIST || errno == EISDIR) {
250 if (stat(path, &sb) < 0) {
251 warn("%s", path);
252 retval = 1;
253 break;
254 } else if (!S_ISDIR(sb.st_mode)) {
255 if (last)
256 errno = EEXIST;
257 else
258 errno = ENOTDIR;
259 warn("%s", path);
260 retval = 1;
261 break;
262 }
263 } else {
264 warn("%s", path);
265 retval = 1;
266 break;
267 }
268 } else if (vflag)
269 printf("%s\n", path);
270 if (!last)
271 *p = '/';
272 }
273 if (!first && !last)
274 (void)umask(oumask);
275 return (retval);
276}
277
278static int
279usage(void)
280{
281
282 (void)fprintf(stderr, "usage: mkdir [-pv] [-m mode] directory ...\n");
283 return EX_USAGE;
284}
Note: See TracBrowser for help on using the repository browser.