VirtualBox

source: kBuild/trunk/src/kmk/kmkbuiltin/mkdir.c@ 2134

Last change on this file since 2134 was 2121, checked in by bird, 16 years ago

kmk: make sure alloca.h gets included (Solaris again).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 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#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 "config.h"
45#include <sys/types.h>
46#include <sys/stat.h>
47
48#include "err.h"
49#include <errno.h>
50#ifndef _MSC_VER
51# include <libgen.h>
52#endif
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
56#include <sysexits.h>
57#include <unistd.h>
58#ifdef HAVE_ALLOCA_H
59# include <alloca.h>
60#endif
61#include "getopt.h"
62#ifdef _MSC_VER
63# include <malloc.h>
64# include "mscfakes.h"
65#endif
66#include "kmkbuiltin.h"
67
68
69static int vflag;
70static struct option long_options[] =
71{
72 { "help", no_argument, 0, 261 },
73 { "version", no_argument, 0, 262 },
74 { 0, 0, 0, 0 },
75};
76
77
78extern void * bsd_setmode(const char *p);
79extern mode_t bsd_getmode(const void *bbox, mode_t omode);
80
81static int build(char *, mode_t);
82static int usage(FILE *);
83
84
85int
86kmk_builtin_mkdir(int argc, char *argv[], char **envp)
87{
88 int ch, exitval, success, pflag;
89 mode_t omode, *set = (mode_t *)NULL;
90 char *mode;
91
92 omode = pflag = 0;
93 mode = NULL;
94
95 /* reinitialize globals */
96 vflag = 0;
97
98 /* kmk: reset getopt and set progname */
99 g_progname = argv[0];
100 opterr = 1;
101 optarg = NULL;
102 optopt = 0;
103 optind = 0; /* init */
104 while ((ch = getopt_long(argc, argv, "m:pv", long_options, NULL)) != -1)
105 switch(ch) {
106 case 'm':
107 mode = optarg;
108 break;
109 case 'p':
110 pflag = 1;
111 break;
112 case 'v':
113 vflag = 1;
114 break;
115 case 261:
116 usage(stdout);
117 return 0;
118 case 262:
119 return kbuild_version(argv[0]);
120 case '?':
121 default:
122 return usage(stderr);
123 }
124
125 argc -= optind;
126 argv += optind;
127 if (argv[0] == NULL)
128 return usage(stderr);
129
130 if (mode == NULL) {
131 omode = S_IRWXU | S_IRWXG | S_IRWXO;
132 } else {
133 if ((set = bsd_setmode(mode)) == NULL)
134 return errx(1, "invalid file mode: %s", mode);
135 omode = bsd_getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
136 free(set);
137 }
138
139 for (exitval = 0; *argv != NULL; ++argv) {
140 success = 1;
141 if (pflag) {
142 if (build(*argv, omode))
143 success = 0;
144 } else if (mkdir(*argv, omode) < 0) {
145 if (errno == ENOTDIR || errno == ENOENT)
146 warn("%s", dirname(*argv));
147 else
148 warn("%s", *argv);
149 success = 0;
150 } else if (vflag)
151 (void)printf("%s\n", *argv);
152
153 if (!success)
154 exitval = 1;
155 /*
156 * The mkdir() and umask() calls both honor only the low
157 * nine bits, so if you try to set a mode including the
158 * sticky, setuid, setgid bits you lose them. Don't do
159 * this unless the user has specifically requested a mode,
160 * as chmod will (obviously) ignore the umask.
161 */
162 if (success && mode != NULL && chmod(*argv, omode) == -1) {
163 warn("%s", *argv);
164 exitval = 1;
165 }
166 }
167 return exitval;
168}
169
170static int
171build(char *path, mode_t omode)
172{
173 struct stat sb;
174 mode_t numask, oumask;
175 int first, last, retval;
176 char *p;
177
178 const size_t len = strlen(path);
179 p = alloca(len + 1);
180 path = memcpy(p, path, len + 1);
181
182#if defined(_MSC_VER) || defined(__EMX__)
183 /* correct slashes. */
184 p = strchr(path, '\\');
185 while (p) {
186 *p++ = '/';
187 p = strchr(p, '\\');
188 }
189#endif
190
191 p = path;
192 oumask = 0;
193 retval = 0;
194#if defined(_MSC_VER) || defined(__EMX__)
195 if ( ( (p[0] >= 'A' && p[0] <= 'Z')
196 || (p[0] >= 'a' && p[0] <= 'z'))
197 && p[1] == ':')
198 p += 2; /* skip the drive letter */
199 else if ( p[0] == '/'
200 && p[1] == '/'
201 && p[2] != '/')
202 {
203 /* UNC */
204 p += 2;
205 while (*p && *p != '/') /* server */
206 p++;
207 while (*p == '/')
208 p++;
209 while (*p && *p != '/') /* share */
210 p++;
211 }
212#endif
213 if (p[0] == '/') /* Skip leading '/'. */
214 ++p;
215 for (first = 1, last = 0; !last ; ++p) {
216 if (p[0] == '\0')
217 last = 1;
218 else if (p[0] != '/')
219 continue;
220 *p = '\0';
221 if (!last && p[1] == '\0')
222 last = 1;
223 if (first) {
224 /*
225 * POSIX 1003.2:
226 * For each dir operand that does not name an existing
227 * directory, effects equivalent to those cased by the
228 * following command shall occcur:
229 *
230 * mkdir -p -m $(umask -S),u+wx $(dirname dir) &&
231 * mkdir [-m mode] dir
232 *
233 * We change the user's umask and then restore it,
234 * instead of doing chmod's.
235 */
236 oumask = umask(0);
237 numask = oumask & ~(S_IWUSR | S_IXUSR);
238 (void)umask(numask);
239 first = 0;
240 }
241 if (last)
242 (void)umask(oumask);
243 if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
244 if (errno == EEXIST || errno == EISDIR || errno == ENOSYS /* (solaris crap) */) {
245 if (stat(path, &sb) < 0) {
246 warn("%s", path);
247 retval = 1;
248 break;
249 } else if (!S_ISDIR(sb.st_mode)) {
250 if (last)
251 errno = EEXIST;
252 else
253 errno = ENOTDIR;
254 warn("%s", path);
255 retval = 1;
256 break;
257 }
258 } else {
259 warn("%s", path);
260 retval = 1;
261 break;
262 }
263 } else if (vflag)
264 printf("%s\n", path);
265 if (!last)
266 *p = '/';
267 }
268 if (!first && !last)
269 (void)umask(oumask);
270 return (retval);
271}
272
273static int
274usage(FILE *pf)
275{
276 fprintf(pf, "usage: %s [-pv] [-m mode] directory ...\n"
277 " or: %s --help\n"
278 " or: %s --version\n",
279 g_progname, g_progname, g_progname);
280 return EX_USAGE;
281}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette