VirtualBox

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

最後變更 在這個檔案從2113是 2113,由 bird 提交於 16 年 前

kmkbuiltin: include config.h

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

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