mkdir.c revision 20418
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 *	$Id: mkdir.c,v 1.5 1994/09/24 02:56:03 davidg Exp $
34 */
35
36#ifndef lint
37static char const copyright[] =
38"@(#) Copyright (c) 1983, 1992, 1993\n\
39	The Regents of the University of California.  All rights reserved.\n";
40#endif /* not lint */
41
42#ifndef lint
43static char const sccsid[] = "@(#)mkdir.c	8.2 (Berkeley) 1/25/94";
44#endif /* not lint */
45
46#include <sys/types.h>
47#include <sys/stat.h>
48
49#include <err.h>
50#include <errno.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54#include <unistd.h>
55
56int	build __P((char *));
57void	usage __P((void));
58
59int
60main(argc, argv)
61	int argc;
62	char *argv[];
63{
64	int ch, exitval, oct, omode, pflag;
65	mode_t *set = (mode_t *)NULL;
66	char *ep, *mode;
67
68	omode = pflag = 0;
69	mode = NULL;
70	while ((ch = getopt(argc, argv, "m:p")) != EOF)
71		switch(ch) {
72		case 'p':
73			pflag = 1;
74			break;
75		case 'm':
76			mode = optarg;
77			break;
78		case '?':
79		default:
80			usage();
81		}
82
83	argc -= optind;
84	argv += optind;
85	if (argv[0] == NULL)
86		usage();
87
88	if (mode == NULL) {
89		omode = S_IRWXU | S_IRWXG | S_IRWXO;
90		oct = 1;
91	} else if (*mode >= '0' && *mode <= '7') {
92		omode = (int)strtol(mode, &ep, 8);
93		if (omode < 0 || *ep)
94			errx(1, "invalid file mode: %s", mode);
95		oct = 1;
96	} else {
97		if ((set = setmode(mode)) == NULL)
98			errx(1, "invalid file mode: %s", mode);
99		oct = 0;
100	}
101
102	for (exitval = 0; *argv != NULL; ++argv) {
103		if (pflag && build(*argv)) {
104			exitval = 1;
105			continue;
106		}
107		if (mkdir(*argv, oct ?
108		    omode : getmode(set, S_IRWXU | S_IRWXG | S_IRWXO)) < 0) {
109			if (!pflag) {
110				warn("%s", *argv);
111				exitval = 1;
112			}
113		}
114	}
115	exit(exitval);
116}
117
118int
119build(path)
120	char *path;
121{
122	struct stat sb;
123	mode_t numask, oumask;
124	int first;
125	char *p;
126
127	p = path;
128	oumask = 0;
129	if (p[0] == '/')		/* Skip leading '/'. */
130		++p;
131	for (first = 1;; ++p) {
132		if (p[0] == '\0' || (p[0] == '/' && p[1] == '\0'))
133			break;
134		if (p[0] != '/')
135			continue;
136		*p = '\0';
137		if (first) {
138			/*
139			 * POSIX 1003.2:
140			 * For each dir operand that does not name an existing
141			 * directory, effects equivalent to those cased by the
142			 * following command shall occcur:
143			 *
144			 * mkdir -p -m $(umask -S),u+wx $(dirname dir) &&
145			 *    mkdir [-m mode] dir
146			 *
147			 * We change the user's umask and then restore it,
148			 * instead of doing chmod's.
149			 */
150			oumask = umask(0);
151			numask = oumask & ~(S_IWUSR | S_IXUSR);
152			(void)umask(numask);
153			first = 0;
154		}
155		if (stat(path, &sb)) {
156			if (errno != ENOENT ||
157			    mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
158				warn("%s", path);
159				return (1);
160			}
161		}
162		*p = '/';
163	}
164	if (!first)
165		(void)umask(oumask);
166	return (0);
167}
168
169void
170usage()
171{
172	(void)fprintf(stderr, "usage: mkdir [-p] [-m mode] directory ...\n");
173	exit (1);
174}
175