1 |
|
|
/* $OpenBSD: mkdir.c,v 1.29 2015/10/23 01:00:16 deraadt 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/types.h> |
34 |
|
|
#include <sys/stat.h> |
35 |
|
|
|
36 |
|
|
#include <err.h> |
37 |
|
|
#include <errno.h> |
38 |
|
|
#include <locale.h> |
39 |
|
|
#include <stdio.h> |
40 |
|
|
#include <stdlib.h> |
41 |
|
|
#include <string.h> |
42 |
|
|
#include <unistd.h> |
43 |
|
|
|
44 |
|
|
extern char *__progname; |
45 |
|
|
|
46 |
|
|
int mkpath(char *, mode_t, mode_t); |
47 |
|
|
void usage(void); |
48 |
|
|
|
49 |
|
|
int |
50 |
|
|
main(int argc, char *argv[]) |
51 |
|
83 |
{ |
52 |
|
|
int ch, rv, exitval, pflag; |
53 |
|
|
void *set; |
54 |
|
|
mode_t mode, dir_mode; |
55 |
|
|
|
56 |
|
83 |
setlocale(LC_ALL, ""); |
57 |
|
|
|
58 |
|
|
/* |
59 |
|
|
* The default file mode is a=rwx (0777) with selected permissions |
60 |
|
|
* removed in accordance with the file mode creation mask. For |
61 |
|
|
* intermediate path name components, the mode is the default modified |
62 |
|
|
* by u+wx so that the subdirectories can always be created. |
63 |
|
|
*/ |
64 |
|
83 |
mode = 0777 & ~umask(0); |
65 |
|
83 |
dir_mode = mode | S_IWUSR | S_IXUSR; |
66 |
|
|
|
67 |
|
83 |
pflag = 0; |
68 |
✓✓ |
237 |
while ((ch = getopt(argc, argv, "m:p")) != -1) |
69 |
✓✓✗ |
71 |
switch(ch) { |
70 |
|
|
case 'p': |
71 |
|
70 |
pflag = 1; |
72 |
|
70 |
break; |
73 |
|
|
case 'm': |
74 |
✗✓ |
1 |
if ((set = setmode(optarg)) == NULL) |
75 |
|
|
errx(1, "invalid file mode: %s", optarg); |
76 |
|
1 |
mode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO); |
77 |
|
1 |
free(set); |
78 |
|
1 |
break; |
79 |
|
|
default: |
80 |
|
|
usage(); |
81 |
|
|
} |
82 |
|
83 |
argc -= optind; |
83 |
|
83 |
argv += optind; |
84 |
|
|
|
85 |
✓✗ |
83 |
if ((mode & (S_ISUID | S_ISGID | S_ISTXT)) == 0) { |
86 |
✗✓ |
83 |
if (pledge("stdio rpath cpath fattr wpath", NULL) == -1) |
87 |
|
|
err(1, "pledge"); |
88 |
|
|
} |
89 |
|
|
|
90 |
✗✓ |
83 |
if (*argv == NULL) |
91 |
|
|
usage(); |
92 |
|
|
|
93 |
✓✓ |
166 |
for (exitval = 0; *argv != NULL; ++argv) { |
94 |
|
|
char *slash; |
95 |
|
|
|
96 |
|
|
/* Remove trailing slashes, per POSIX. */ |
97 |
|
83 |
slash = strrchr(*argv, '\0'); |
98 |
✓✓✗✓
|
166 |
while (--slash > *argv && *slash == '/') |
99 |
|
|
*slash = '\0'; |
100 |
|
|
|
101 |
✓✓ |
83 |
if (pflag) { |
102 |
|
70 |
rv = mkpath(*argv, mode, dir_mode); |
103 |
|
|
} else { |
104 |
|
13 |
rv = mkdir(*argv, mode); |
105 |
|
|
/* |
106 |
|
|
* The mkdir() and umask() calls both honor only the |
107 |
|
|
* low nine bits, so if you try to set a mode including |
108 |
|
|
* the sticky, setuid, setgid bits you lose them. Don't |
109 |
|
|
* do this unless the user has specifically requested |
110 |
|
|
* a mode as chmod will (obviously) ignore the umask. |
111 |
|
|
*/ |
112 |
✗✓ |
13 |
if (rv == 0 && mode > 0777) |
113 |
|
|
rv = chmod(*argv, mode); |
114 |
|
|
} |
115 |
✗✓ |
83 |
if (rv < 0) { |
116 |
|
|
warn("%s", *argv); |
117 |
|
|
exitval = 1; |
118 |
|
|
} |
119 |
|
|
} |
120 |
|
83 |
exit(exitval); |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
/* |
124 |
|
|
* mkpath -- create directories. |
125 |
|
|
* path - path |
126 |
|
|
* mode - file mode of terminal directory |
127 |
|
|
* dir_mode - file mode of intermediate directories |
128 |
|
|
*/ |
129 |
|
|
int |
130 |
|
|
mkpath(char *path, mode_t mode, mode_t dir_mode) |
131 |
|
70 |
{ |
132 |
|
|
struct stat sb; |
133 |
|
|
char *slash; |
134 |
|
|
int done; |
135 |
|
|
|
136 |
|
70 |
slash = path; |
137 |
|
|
|
138 |
|
|
for (;;) { |
139 |
|
108 |
slash += strspn(slash, "/"); |
140 |
|
108 |
slash += strcspn(slash, "/"); |
141 |
|
|
|
142 |
|
108 |
done = (*slash == '\0'); |
143 |
|
108 |
*slash = '\0'; |
144 |
|
|
|
145 |
✓✓✓✓
|
108 |
if (mkdir(path, done ? mode : dir_mode) == 0) { |
146 |
✗✓✗✗
|
98 |
if (mode > 0777 && chmod(path, mode) < 0) |
147 |
|
|
return (-1); |
148 |
|
|
} else { |
149 |
|
10 |
int mkdir_errno = errno; |
150 |
|
|
|
151 |
✗✓ |
10 |
if (stat(path, &sb)) { |
152 |
|
|
/* Not there; use mkdir()s errno */ |
153 |
|
|
errno = mkdir_errno; |
154 |
|
|
return (-1); |
155 |
|
|
} |
156 |
✗✓ |
10 |
if (!S_ISDIR(sb.st_mode)) { |
157 |
|
|
/* Is there, but isn't a directory */ |
158 |
|
|
errno = ENOTDIR; |
159 |
|
|
return (-1); |
160 |
|
|
} |
161 |
|
|
} |
162 |
|
|
|
163 |
✓✓ |
108 |
if (done) |
164 |
|
70 |
break; |
165 |
|
|
|
166 |
|
38 |
*slash = '/'; |
167 |
|
38 |
} |
168 |
|
|
|
169 |
|
70 |
return (0); |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
void |
173 |
|
|
usage(void) |
174 |
|
|
{ |
175 |
|
|
(void)fprintf(stderr, "usage: %s [-p] [-m mode] directory ...\n", |
176 |
|
|
__progname); |
177 |
|
|
exit(1); |
178 |
|
|
} |