source: trunk/src/kmk/kmkbuiltin/mkdir.c@ 1327

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

include getopt.h.

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