VirtualBox

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

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

kmk_ln, kmk_mkdir, kmk_mv, kmk_printf: changed to use getopt_r and got rid of remaining static buffers.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use