xref: /freebsd/bin/mkdir/mkdir.c (revision afe61c15161c324a7af299a9b8457aba5afc92db)
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
35 static 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
41 static 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 
54 int	build __P((char *));
55 void	usage __P((void));
56 
57 int
58 main(argc, argv)
59 	int argc;
60 	char *argv[];
61 {
62 	int ch, exitval, oct, omode, pflag;
63 	mode_t *set;
64 	char *ep, *mode;
65 
66 	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 			warn("%s", *argv);
108 			exitval = 1;
109 		}
110 	}
111 	exit(exitval);
112 }
113 
114 int
115 build(path)
116 	char *path;
117 {
118 	struct stat sb;
119 	mode_t numask, oumask;
120 	int first;
121 	char *p;
122 
123 	p = path;
124 	if (p[0] == '/')		/* Skip leading '/'. */
125 		++p;
126 	for (first = 1;; ++p) {
127 		if (p[0] == '\0' || p[0] == '/' && p[1] == '\0')
128 			break;
129 		if (p[0] != '/')
130 			continue;
131 		*p = '\0';
132 		if (first) {
133 			/*
134 			 * POSIX 1003.2:
135 			 * For each dir operand that does not name an existing
136 			 * directory, effects equivalent to those cased by the
137 			 * following command shall occcur:
138 			 *
139 			 * mkdir -p -m $(umask -S),u+wx $(dirname dir) &&
140 			 *    mkdir [-m mode] dir
141 			 *
142 			 * We change the user's umask and then restore it,
143 			 * instead of doing chmod's.
144 			 */
145 			oumask = umask(0);
146 			numask = oumask & ~(S_IWUSR | S_IXUSR);
147 			(void)umask(numask);
148 			first = 0;
149 		}
150 		if (stat(path, &sb)) {
151 			if (errno != ENOENT ||
152 			    mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
153 				warn("%s", path);
154 				return (1);
155 			}
156 		}
157 		*p = '/';
158 	}
159 	if (!first)
160 		(void)umask(oumask);
161 	return (0);
162 }
163 
164 void
165 usage()
166 {
167 	(void)fprintf(stderr, "usage: mkdir [-p] [-m mode] directory ...\n");
168 	exit (1);
169 }
170