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

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

o Ported all kmk builtins to win32.
o Fixed serveral bugs in kmk builtins.
o Probably broke both linux, bsd and OS/2. :-)

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