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

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

Solaris + cleanup.

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