-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathmkdir.c
143 lines (130 loc) · 2.65 KB
/
mkdir.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright (c) 1983, 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* %sccs.include.redist.c%
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1983, 1992, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)mkdir.c 8.2 (Berkeley) 01/25/94";
#endif /* not lint */
#include <sys/types.h>
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int build __P((char *));
void usage __P((void));
int
main(argc, argv)
int argc;
char *argv[];
{
int ch, exitval, oct, omode, pflag;
mode_t *set;
char *ep, *mode;
pflag = 0;
mode = NULL;
while ((ch = getopt(argc, argv, "m:p")) != EOF)
switch(ch) {
case 'p':
pflag = 1;
break;
case 'm':
mode = optarg;
break;
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
if (argv[0] == NULL)
usage();
if (mode == NULL) {
omode = S_IRWXU | S_IRWXG | S_IRWXO;
oct = 1;
} else if (*mode >= '0' && *mode <= '7') {
omode = (int)strtol(mode, &ep, 8);
if (omode < 0 || *ep)
errx(1, "invalid file mode: %s", mode);
oct = 1;
} else {
if ((set = setmode(mode)) == NULL)
errx(1, "invalid file mode: %s", mode);
oct = 0;
}
for (exitval = 0; *argv != NULL; ++argv) {
if (pflag && build(*argv)) {
exitval = 1;
continue;
}
if (mkdir(*argv, oct ?
omode : getmode(set, S_IRWXU | S_IRWXG | S_IRWXO)) < 0) {
warn("%s", *argv);
exitval = 1;
}
}
exit(exitval);
}
int
build(path)
char *path;
{
struct stat sb;
mode_t numask, oumask;
int first;
char *p;
p = path;
if (p[0] == '/') /* Skip leading '/'. */
++p;
for (first = 1;; ++p) {
if (p[0] == '\0' || p[0] == '/' && p[1] == '\0')
break;
if (p[0] != '/')
continue;
*p = '\0';
if (first) {
/*
* POSIX 1003.2:
* For each dir operand that does not name an existing
* directory, effects equivalent to those cased by the
* following command shall occcur:
*
* mkdir -p -m $(umask -S),u+wx $(dirname dir) &&
* mkdir [-m mode] dir
*
* We change the user's umask and then restore it,
* instead of doing chmod's.
*/
oumask = umask(0);
numask = oumask & ~(S_IWUSR | S_IXUSR);
(void)umask(numask);
first = 0;
}
if (stat(path, &sb)) {
if (errno != ENOENT ||
mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
warn("%s", path);
return (1);
}
}
*p = '/';
}
if (!first)
(void)umask(oumask);
return (0);
}
void
usage()
{
(void)fprintf(stderr, "usage: mkdir [-p] [-m mode] directory ...\n");
exit (1);
}