xref: /freebsd/bin/mkdir/mkdir.c (revision 5b31cc94b10d4bb7109c6b27940a0fc76a44a331)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #if 0
33 #ifndef lint
34 static char const copyright[] =
35 "@(#) Copyright (c) 1983, 1992, 1993\n\
36 	The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38 
39 #endif
40 #include <sys/cdefs.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 
44 #include <err.h>
45 #include <errno.h>
46 #include <libgen.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <sysexits.h>
51 #include <unistd.h>
52 
53 static int	build(char *, mode_t);
54 static void	usage(void);
55 
56 static int	vflag;
57 
58 int
59 main(int argc, char *argv[])
60 {
61 	int ch, exitval, success, pflag;
62 	mode_t omode;
63 	void *set = NULL;
64 	char *mode;
65 
66 	omode = pflag = 0;
67 	mode = NULL;
68 	while ((ch = getopt(argc, argv, "m:pv")) != -1)
69 		switch(ch) {
70 		case 'm':
71 			mode = optarg;
72 			break;
73 		case 'p':
74 			pflag = 1;
75 			break;
76 		case 'v':
77 			vflag = 1;
78 			break;
79 		case '?':
80 		default:
81 			usage();
82 		}
83 
84 	argc -= optind;
85 	argv += optind;
86 	if (argv[0] == NULL)
87 		usage();
88 
89 	if (mode == NULL) {
90 		omode = S_IRWXU | S_IRWXG | S_IRWXO;
91 	} else {
92 		if ((set = setmode(mode)) == NULL)
93 			errx(1, "invalid file mode: %s", mode);
94 		omode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
95 		free(set);
96 	}
97 
98 	for (exitval = 0; *argv != NULL; ++argv) {
99 		if (pflag) {
100 			success = build(*argv, omode);
101 		} else if (mkdir(*argv, omode) < 0) {
102 			if (errno == ENOTDIR || errno == ENOENT)
103 				warn("%s", dirname(*argv));
104 			else
105 				warn("%s", *argv);
106 			success = 0;
107 		} else {
108 			success = 1;
109 			if (vflag)
110 				(void)printf("%s\n", *argv);
111 		}
112 		if (!success)
113 			exitval = 1;
114 		/*
115 		 * The mkdir() and umask() calls both honor only the low
116 		 * nine bits, so if you try to set a mode including the
117 		 * sticky, setuid, setgid bits you lose them.  Don't do
118 		 * this unless the user has specifically requested a mode,
119 		 * as chmod will (obviously) ignore the umask.  Do this
120 		 * on newly created directories only.
121 		 */
122 		if (success == 1 && mode != NULL && chmod(*argv, omode) == -1) {
123 			warn("%s", *argv);
124 			exitval = 1;
125 		}
126 	}
127 	exit(exitval);
128 }
129 
130 
131 /*
132  * Returns 1 if a directory has been created,
133  * 2 if it already existed, and 0 on failure.
134  */
135 static int
136 build(char *path, mode_t omode)
137 {
138 	struct stat sb;
139 	mode_t numask, oumask;
140 	int first, last, retval;
141 	char *p;
142 
143 	p = path;
144 	oumask = 0;
145 	retval = 1;
146 	if (p[0] == '/')		/* Skip leading '/'. */
147 		++p;
148 	for (first = 1, last = 0; !last ; ++p) {
149 		if (p[0] == '\0')
150 			last = 1;
151 		else if (p[0] != '/')
152 			continue;
153 		*p = '\0';
154 		if (!last && p[1] == '\0')
155 			last = 1;
156 		if (first) {
157 			/*
158 			 * POSIX 1003.2:
159 			 * For each dir operand that does not name an existing
160 			 * directory, effects equivalent to those caused by the
161 			 * following command shall occur:
162 			 *
163 			 * mkdir -p -m $(umask -S),u+wx $(dirname dir) &&
164 			 *    mkdir [-m mode] dir
165 			 *
166 			 * We change the user's umask and then restore it,
167 			 * instead of doing chmod's.
168 			 */
169 			oumask = umask(0);
170 			numask = oumask & ~(S_IWUSR | S_IXUSR);
171 			(void)umask(numask);
172 			first = 0;
173 		}
174 		if (last)
175 			(void)umask(oumask);
176 		if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
177 			if (errno == EEXIST || errno == EISDIR) {
178 				if (stat(path, &sb) < 0) {
179 					warn("%s", path);
180 					retval = 0;
181 					break;
182 				} else if (!S_ISDIR(sb.st_mode)) {
183 					if (last)
184 						errno = EEXIST;
185 					else
186 						errno = ENOTDIR;
187 					warn("%s", path);
188 					retval = 0;
189 					break;
190 				}
191 				if (last)
192 					retval = 2;
193 			} else {
194 				warn("%s", path);
195 				retval = 0;
196 				break;
197 			}
198 		} else if (vflag)
199 			printf("%s\n", path);
200 		if (!last)
201 		    *p = '/';
202 	}
203 	if (!first && !last)
204 		(void)umask(oumask);
205 	return (retval);
206 }
207 
208 static void
209 usage(void)
210 {
211 
212 	(void)fprintf(stderr,
213 	    "usage: mkdir [-pv] [-m mode] directory_name ...\n");
214 	exit (EX_USAGE);
215 }
216