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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1992-2010 AT&T Intellectual Property *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.opensource.org/licenses/cpl1.0.txt *
* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* David Korn
* AT&T Bell Laboratories
*
* mkdir
*/
static const char usage[] =
"[-?\n@(#)$Id: mkdir (AT&T Research) 2009-12-03 $\n]"
USAGE_LICENSE
"[+NAME?mkdir - make directories]"
"[+DESCRIPTION?\bmkdir\b creates one or more directories. By "
"default, the mode of created directories is \ba=rwx\b minus the "
"bits set in the \bumask\b(1).]"
"[m:mode]:[mode?Set the mode of created directories to \amode\a. "
"\amode\a is symbolic or octal mode as in \bchmod\b(1). Relative "
"modes assume an initial mode of \ba=rwx\b.]"
"[p:parents?Create any missing intermediate pathname components. For "
"each dir operand that does not name an existing directory, effects "
"equivalent to those caused by the following command shall occur: "
"\vmkdir -p -m $(umask -S),u+wx $(dirname dir) && mkdir [-m mode]] "
"dir\v where the \b-m\b mode option represents that option supplied to "
"the original invocation of \bmkdir\b, if any. Each dir operand that "
"names an existing directory shall be ignored without error.]"
"[v:verbose?Print a message on the standard error for each created "
"directory.]"
"\n"
"\ndirectory ...\n"
"\n"
"[+EXIT STATUS?]{"
"[+0?All directories created successfully, or the \b-p\b option "
"was specified and all the specified directories now exist.]"
"[+>0?An error occurred.]"
"}"
"[+SEE ALSO?\bchmod\b(1), \brmdir\b(1), \bumask\b(1)]"
;
#include <cmd.h>
#include <ls.h>
#define DIRMODE (S_IRWXU|S_IRWXG|S_IRWXO)
int
b_mkdir(int argc, char** argv, void* context)
{
register char* arg;
register int n;
register mode_t mode = DIRMODE;
register mode_t mask = 0;
register int mflag = 0;
register int pflag = 0;
register int vflag = 0;
char* name;
mode_t dmode;
struct stat st;
cmdinit(argc, argv, context, ERROR_CATALOG, 0);
for (;;)
{
switch (optget(argv, usage))
{
case 0:
break;
case 'm':
mflag = 1;
mode = strperm(arg = opt_info.arg, &opt_info.arg, mode);
if (*opt_info.arg)
error(ERROR_exit(0), "%s: invalid mode", arg);
continue;
case 'p':
pflag = 1;
continue;
case 'v':
vflag = 1;
continue;
case ':':
error(2, "%s", opt_info.arg);
continue;
case '?':
error(ERROR_usage(2), "%s", opt_info.arg);
continue;
}
break;
}
argv += opt_info.index;
if (error_info.errors || !*argv)
error(ERROR_usage(2), "%s", optusage(NiL));
mask = umask(0);
if (mflag || pflag)
{
dmode = DIRMODE & ~mask;
if (!mflag)
mode = dmode;
dmode |= S_IWUSR | S_IXUSR;
}
else
{
mode &= ~mask;
umask(mask);
mask = 0;
}
while (arg = *argv++)
{
if (mkdir(arg, mode) < 0)
{
if (!pflag || !(errno == ENOENT || errno == EEXIST || errno == ENOTDIR))
{
error(ERROR_system(0), "%s:", arg);
continue;
}
if (errno == EEXIST)
continue;
/*
* -p option, preserve intermediates
* first eliminate trailing /'s
*/
n = strlen(arg);
while (n > 0 && arg[--n] == '/');
arg[n + 1] = 0;
for (name = arg, n = *arg; n;)
{
/* skip over slashes */
while (*arg == '/')
arg++;
/* skip to next component */
while ((n = *arg) && n != '/')
arg++;
*arg = 0;
if (mkdir(name, n ? dmode : mode) < 0 && errno != EEXIST && access(name, F_OK) < 0)
{
*arg = n;
error(ERROR_system(0), "%s:", name);
break;
}
if (vflag)
error(0, "%s: directory created", name);
if (!(*arg = n) && (mode & (S_ISVTX|S_ISUID|S_ISGID)))
{
if (stat(name, &st))
{
error(ERROR_system(0), "%s: cannot stat", name);
break;
}
if ((st.st_mode & (S_ISVTX|S_ISUID|S_ISGID)) != (mode & (S_ISVTX|S_ISUID|S_ISGID)) && chmod(name, mode))
{
error(ERROR_system(0), "%s: cannot change mode from %s to %s", name, fmtperm(st.st_mode & (S_ISVTX|S_ISUID|S_ISGID)), fmtperm(mode));
break;
}
}
}
}
else if (vflag)
error(0, "%s: directory created", arg);
}
if (mask)
umask(mask);
return error_info.errors != 0;
}
|