VirtualBox

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

Last change on this file since 3192 was 3192, checked in by bird, 6 years ago

kmkbuiltin: funnel output thru output.c (usually via err.c).

  • 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#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#ifndef __HAIKU__
57# include <sysexits.h>
58#endif
59#include <unistd.h>
60#ifdef HAVE_ALLOCA_H
61# include <alloca.h>
62#endif
63#include "getopt.h"
64#ifdef __HAIKU__
65# include "haikufakes.h"
66#endif
67#ifdef _MSC_VER
68# include <malloc.h>
69# include "mscfakes.h"
70#endif
71#include "kmkbuiltin.h"
72
73
74static struct option long_options[] =
75{
76 { "help", no_argument, 0, 261 },
77 { "version", no_argument, 0, 262 },
78 { 0, 0, 0, 0 },
79};
80
81
82extern void * bsd_setmode(const char *p);
83extern mode_t bsd_getmode(const void *bbox, mode_t omode);
84
85static int build(PKMKBUILTINCTX pCtx, char *, mode_t, int);
86static int usage(PKMKBUILTINCTX pCtx, int fIsErr);
87
88
89int
90kmk_builtin_mkdir(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
91{
92 int ch, exitval, success, pflag, vflag;
93 mode_t omode, *set = (mode_t *)NULL;
94 char *mode;
95
96 omode = pflag = vflag = 0;
97 mode = NULL;
98
99 /* kmk: reset getopt and set progname */
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(pCtx, 0);
117 return 0;
118 case 262:
119 return kbuild_version(argv[0]);
120 case '?':
121 default:
122 return usage(pCtx, 1);
123 }
124
125 argc -= optind;
126 argv += optind;
127 if (argv[0] == NULL)
128 return usage(pCtx, 1);
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(pCtx, 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(pCtx, *argv, omode, vflag))
143 success = 0;
144 } else if (mkdir(*argv, omode) < 0) {
145 if (errno == ENOTDIR || errno == ENOENT)
146 warn(pCtx, "mkdir: %s", dirname(*argv));
147 else
148 warn(pCtx, "mkdir: %s", *argv);
149 success = 0;
150 } else if (vflag)
151 kmk_builtin_ctx_printf(pCtx, 0, "%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(pCtx, "chmod: %s", *argv);
164 exitval = 1;
165 }
166 }
167 return exitval;
168}
169
170#ifdef KMK_BUILTIN_STANDALONE
171int main(int argc, char **argv, char **envp)
172{
173 KMKBUILTINCTX Ctx = { "kmk_mkdir", NULL };
174 return kmk_builtin_mkdir(argc, argv, envp, &Ctx);
175}
176#endif
177
178static int
179build(PKMKBUILTINCTX pCtx, char *path, mode_t omode, int vflag)
180{
181 struct stat sb;
182 mode_t numask, oumask;
183 int first, last, retval;
184 char *p;
185
186 const size_t len = strlen(path);
187 p = alloca(len + 1);
188 path = memcpy(p, path, len + 1);
189
190#if defined(_MSC_VER) || defined(__EMX__)
191 /* correct slashes. */
192 p = strchr(path, '\\');
193 while (p) {
194 *p++ = '/';
195 p = strchr(p, '\\');
196 }
197#endif
198
199 p = path;
200 oumask = 0;
201 retval = 0;
202#if defined(_MSC_VER) || defined(__EMX__)
203 if ( ( (p[0] >= 'A' && p[0] <= 'Z')
204 || (p[0] >= 'a' && p[0] <= 'z'))
205 && p[1] == ':')
206 p += 2; /* skip the drive letter */
207 else if ( p[0] == '/'
208 && p[1] == '/'
209 && p[2] != '/')
210 {
211 /* UNC */
212 p += 2;
213 while (*p && *p != '/') /* server */
214 p++;
215 while (*p == '/')
216 p++;
217 while (*p && *p != '/') /* share */
218 p++;
219 }
220#endif
221 if (p[0] == '/') /* Skip leading '/'. */
222 ++p;
223 for (first = 1, last = 0; !last ; ++p) {
224 if (p[0] == '\0')
225 last = 1;
226 else if (p[0] != '/')
227 continue;
228 *p = '\0';
229 if (!last && p[1] == '\0')
230 last = 1;
231 if (first) {
232 /*
233 * POSIX 1003.2:
234 * For each dir operand that does not name an existing
235 * directory, effects equivalent to those cased by the
236 * following command shall occcur:
237 *
238 * mkdir -p -m $(umask -S),u+wx $(dirname dir) &&
239 * mkdir [-m mode] dir
240 *
241 * We change the user's umask and then restore it,
242 * instead of doing chmod's.
243 */
244 oumask = umask(0);
245 numask = oumask & ~(S_IWUSR | S_IXUSR);
246 (void)umask(numask);
247 first = 0;
248 }
249 if (last)
250 (void)umask(oumask);
251 if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
252 if (errno == EEXIST || errno == EISDIR
253 || errno == ENOSYS /* (solaris crap) */
254 || errno == EACCES /* (ditto) */) {
255 if (stat(path, &sb) < 0) {
256 warn(pCtx, "stat: %s", path);
257 retval = 1;
258 break;
259 } else if (!S_ISDIR(sb.st_mode)) {
260 if (last)
261 errno = EEXIST;
262 else
263 errno = ENOTDIR;
264 warn(pCtx, "st_mode: %s", path);
265 retval = 1;
266 break;
267 }
268 } else {
269 warn(pCtx, "mkdir: %s", path);
270 retval = 1;
271 break;
272 }
273 } else if (vflag)
274 kmk_builtin_ctx_printf(pCtx, 0, "%s\n", path);
275 if (!last)
276 *p = '/';
277 }
278 if (!first && !last)
279 (void)umask(oumask);
280 return (retval);
281}
282
283static int
284usage(PKMKBUILTINCTX pCtx, int fIsErr)
285{
286 kmk_builtin_ctx_printf(pCtx, fIsErr,
287 "usage: %s [-pv] [-m mode] directory ...\n"
288 " or: %s --help\n"
289 " or: %s --version\n",
290 pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName);
291 return EX_USAGE;
292}
293
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use