xref: /freebsd/bin/mkdir/mkdir.c (revision 0ddf9be1f0723916ebd4feb7313d64dffab0c2bb)
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.9 1997/03/28 15:24:25 imp Exp $
34  */
35 
36 #ifndef lint
37 static 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
43 static 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 
56 int	build __P((char *, mode_t));
57 void	usage __P((void));
58 
59 int
60 main(argc, argv)
61 	int argc;
62 	char *argv[];
63 {
64 	int ch, exitval, omode, pflag;
65 	mode_t *set = (mode_t *)NULL;
66 	char *mode;
67 
68 	omode = pflag = 0;
69 	mode = NULL;
70 	while ((ch = getopt(argc, argv, "m:p")) != -1)
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 	} else {
91 		if ((set = setmode(mode)) == NULL)
92 			errx(1, "invalid file mode: %s", mode);
93 		omode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
94 	}
95 
96 	for (exitval = 0; *argv != NULL; ++argv) {
97 		if (pflag) {
98 			if (build(*argv, omode))
99 				exitval = 1;
100 			continue;
101 		}
102 		if (mkdir(*argv, omode) < 0) {
103 			warn("%s", *argv);
104 			exitval = 1;
105 		}
106 	}
107 	exit(exitval);
108 }
109 
110 int
111 build(path, omode)
112 	char *path;
113 	mode_t omode;
114 {
115 	struct stat sb;
116 	mode_t numask, oumask;
117 	int first, last, retval;
118 	char *p;
119 
120 	p = path;
121 	oumask = 0;
122 	retval = 0;
123 	if (p[0] == '/')		/* Skip leading '/'. */
124 		++p;
125 	for (first = 1, last = 0; !last ; ++p) {
126 		if (p[0] == '\0')
127 			last = 1;
128 		else if (p[0] != '/')
129 			continue;
130 		*p = '\0';
131 		if (p[1] == '\0')
132 			last = 1;
133 		if (first) {
134 			/*
135 			 * POSIX 1003.2:
136 			 * For each dir operand that does not name an existing
137 			 * directory, effects equivalent to those cased by the
138 			 * following command shall occcur:
139 			 *
140 			 * mkdir -p -m $(umask -S),u+wx $(dirname dir) &&
141 			 *    mkdir [-m mode] dir
142 			 *
143 			 * We change the user's umask and then restore it,
144 			 * instead of doing chmod's.
145 			 */
146 			oumask = umask(0);
147 			numask = oumask & ~(S_IWUSR | S_IXUSR);
148 			(void)umask(numask);
149 			first = 0;
150 		}
151 		if (last)
152 			(void)umask(oumask);
153 		if (stat(path, &sb)) {
154 			if (errno != ENOENT ||
155 			    mkdir(path, last ? omode :
156 				  S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
157 				warn("%s", path);
158 				retval = 1;
159 				break;
160 			}
161 		}
162 		else if ((sb.st_mode & S_IFMT) != S_IFDIR) {
163 			if (last)
164 				errno = EEXIST;
165 			else
166 				errno = ENOTDIR;
167 			warn("%s", path);
168 			retval = 1;
169 			break;
170 		}
171 		*p = '/';
172 	}
173 	if (!first && !last)
174 		(void)umask(oumask);
175 	return (retval);
176 }
177 
178 void
179 usage()
180 {
181 	(void)fprintf(stderr, "usage: mkdir [-p] [-m mode] directory ...\n");
182 	exit (1);
183 }
184