1 /* $OpenBSD: mkdir.c,v 1.17 2004/07/01 18:25:47 otto Exp $ */
2 /* $NetBSD: mkdir.c,v 1.14 1995/06/25 21:59:21 mycroft Exp $ */
3
4 /*
5 * Copyright (c) 1983, 1992, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1983, 1992, 1993\n\
35 The Regents of the University of California. All rights reserved.\n");
36 __SCCSID("@(#)mkdir.c 8.2 (Berkeley) 1/25/94");
37 __RCSID("$MirOS: src/bin/mkdir/mkdir.c,v 1.2 2007/07/05 23:09:33 tg Exp $");
38
39 #include <sys/types.h>
40 #include <sys/stat.h>
41
42 #include <err.h>
43 #include <errno.h>
44 #include <locale.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49
50 extern char *__progname;
51
52 int mkpath(char *, mode_t, mode_t);
53 void usage(void);
54
55 int
main(int argc,char * argv[])56 main(int argc, char *argv[])
57 {
58 int ch, exitval, pflag;
59 void *set;
60 mode_t mode, dir_mode;
61
62 #ifndef __MirBSD__
63 setlocale(LC_ALL, "");
64 #endif
65
66 /*
67 * The default file mode is a=rwx (0777) with selected permissions
68 * removed in accordance with the file mode creation mask. For
69 * intermediate path name components, the mode is the default modified
70 * by u+wx so that the subdirectories can always be created.
71 */
72 mode = 0777 & ~umask(0);
73 dir_mode = mode | S_IWUSR | S_IXUSR;
74
75 pflag = 0;
76 while ((ch = getopt(argc, argv, "m:p")) != -1)
77 switch(ch) {
78 case 'p':
79 pflag = 1;
80 break;
81 case 'm':
82 if ((set = setmode(optarg)) == NULL)
83 errx(1, "invalid file mode: %s", optarg);
84 mode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
85 free(set);
86 break;
87 default:
88 usage();
89 }
90 argc -= optind;
91 argv += optind;
92
93 if (*argv == NULL)
94 usage();
95
96 for (exitval = 0; *argv != NULL; ++argv) {
97 char *slash;
98
99 /* Remove trailing slashes, per POSIX. */
100 slash = strrchr(*argv, '\0');
101 while (--slash > *argv && *slash == '/')
102 *slash = '\0';
103
104 if (pflag) {
105 if (mkpath(*argv, mode, dir_mode) < 0)
106 exitval = 1;
107 } else {
108 if (mkdir(*argv, mode) < 0) {
109 warn("%s", *argv);
110 exitval = 1;
111 } else {
112 /*
113 * The mkdir() and umask() calls both honor only the low
114 * nine bits, so if you try to set a mode including the
115 * sticky, setuid, setgid bits you lose them. Don't do
116 * this unless the user has specifically requested a mode
117 * as chmod will (obviously) ignore the umask.
118 */
119 if (mode > 0777 && chmod(*argv, mode) == -1) {
120 warn("%s", *argv);
121 exitval = 1;
122 }
123 }
124 }
125 }
126 exit(exitval);
127 }
128
129 /*
130 * mkpath -- create directories.
131 * path - path
132 * mode - file mode of terminal directory
133 * dir_mode - file mode of intermediate directories
134 */
135 int
mkpath(char * path,mode_t mode,mode_t dir_mode)136 mkpath(char *path, mode_t mode, mode_t dir_mode)
137 {
138 struct stat sb;
139 char *slash;
140 int done = 0;
141
142 slash = path;
143
144 while (!done) {
145 slash += strspn(slash, "/");
146 slash += strcspn(slash, "/");
147
148 done = (*slash == '\0');
149 *slash = '\0';
150
151 if (stat(path, &sb)) {
152 if (errno != ENOENT ||
153 (mkdir(path, done ? mode : dir_mode) &&
154 errno != EEXIST)) {
155 warn("%s", path);
156 return (-1);
157 }
158 } else if (!S_ISDIR(sb.st_mode)) {
159 warnx("%s: %s", path, strerror(ENOTDIR));
160 return (-1);
161 }
162
163 *slash = '/';
164 }
165
166 return (0);
167 }
168
169 void
usage(void)170 usage(void)
171 {
172 (void)fprintf(stderr, "usage: %s [-p] [-m mode] dirname ...\n", __progname);
173 exit(1);
174 }
175