VirtualBox

source: kBuild/trunk/src/gmakenew/kmkbuiltin/mkdir.c@ 942

Last change on this file since 942 was 942, checked in by bird, 18 years ago

Always use the GNU make getopt stuff.

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