mkdir.c revision 2923
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 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char copyright[] =
36"@(#) Copyright (c) 1983, 1992, 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)mkdir.c	8.2 (Berkeley) 1/25/94";
42#endif /* not lint */
43
44#include <sys/types.h>
45#include <sys/stat.h>
46
47#include <err.h>
48#include <errno.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <unistd.h>
53
54int	build __P((char *));
55void	usage __P((void));
56
57int
58main(argc, argv)
59	int argc;
60	char *argv[];
61{
62	int ch, exitval, oct, omode, pflag;
63	mode_t *set = (mode_t *)NULL;
64	char *ep, *mode;
65
66	omode = pflag = 0;
67	mode = NULL;
68	while ((ch = getopt(argc, argv, "m:p")) != EOF)
69		switch(ch) {
70		case 'p':
71			pflag = 1;
72			break;
73		case 'm':
74			mode = optarg;
75			break;
76		case '?':
77		default:
78			usage();
79		}
80
81	argc -= optind;
82	argv += optind;
83	if (argv[0] == NULL)
84		usage();
85
86	if (mode == NULL) {
87		omode = S_IRWXU | S_IRWXG | S_IRWXO;
88		oct = 1;
89	} else if (*mode >= '0' && *mode <= '7') {
90		omode = (int)strtol(mode, &ep, 8);
91		if (omode < 0 || *ep)
92			errx(1, "invalid file mode: %s", mode);
93		oct = 1;
94	} else {
95		if ((set = setmode(mode)) == NULL)
96			errx(1, "invalid file mode: %s", mode);
97		oct = 0;
98	}
99
100	for (exitval = 0; *argv != NULL; ++argv) {
101		if (pflag && build(*argv)) {
102			exitval = 1;
103			continue;
104		}
105		if (mkdir(*argv, oct ?
106		    omode : getmode(set, S_IRWXU | S_IRWXG | S_IRWXO)) < 0) {
107			if (!pflag) {
108				warn("%s", *argv);
109				exitval = 1;
110			}
111		}
112	}
113	exit(exitval);
114}
115
116int
117build(path)
118	char *path;
119{
120	struct stat sb;
121	mode_t numask, oumask;
122	int first;
123	char *p;
124
125	p = path;
126        oumask = (mode_t)NULL;
127	if (p[0] == '/')		/* Skip leading '/'. */
128		++p;
129	for (first = 1;; ++p) {
130		if (p[0] == '\0' || (p[0] == '/' && p[1] == '\0'))
131			break;
132		if (p[0] != '/')
133			continue;
134		*p = '\0';
135		if (first) {
136			/*
137			 * POSIX 1003.2:
138			 * For each dir operand that does not name an existing
139			 * directory, effects equivalent to those cased by the
140			 * following command shall occcur:
141			 *
142			 * mkdir -p -m $(umask -S),u+wx $(dirname dir) &&
143			 *    mkdir [-m mode] dir
144			 *
145			 * We change the user's umask and then restore it,
146			 * instead of doing chmod's.
147			 */
148			oumask = umask(0);
149			numask = oumask & ~(S_IWUSR | S_IXUSR);
150			(void)umask(numask);
151			first = 0;
152		}
153		if (stat(path, &sb)) {
154			if (errno != ENOENT ||
155			    mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
156				warn("%s", path);
157				return (1);
158			}
159		}
160		*p = '/';
161	}
162	if (!first)
163		(void)umask(oumask);
164	return (0);
165}
166
167void
168usage()
169{
170	(void)fprintf(stderr, "usage: mkdir [-p] [-m mode] directory ...\n");
171	exit (1);
172}
173