VirtualBox

source: kBuild/trunk/src/gmake/kmkbuiltin/mkdir.c@ 295

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

fixing and adding more commands.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette