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

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

FreeBSD 5.x on amd64

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