source: trunk/src/gmake/kmkbuiltin/mkdir.c@ 375

Last change on this file since 375 was 375, checked in by bird, 20 years ago

Re-initialize globals.

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