VirtualBox

source: kBuild/trunk/src/gmake/kmkbuiltin/mkdir.c@ 803

最後變更 在這個檔案從803是 611,由 bird 提交於 18 年 前

Added cat as builtin command.

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

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