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