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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/* */
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* make directory.
* If -m is used with a valid mode, directories will be
* created in that mode. Otherwise, the default mode will
* be 777 possibly altered by the process's file mode creation
* mask.
* If -p is used, make the directory as well as
* its non-existing parent directories.
*/
#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <locale.h>
#include <stdlib.h>
#include <unistd.h>
#include <libgen.h>
#include <stdarg.h>
#include <wchar.h>
#define MSGEXISTS "\"%s\": Exists but is not a directory\n"
#define MSGUSAGE "usage: mkdir [-m mode] [-p] dirname ...\n"
#define MSGFMT1 "\"%s\": %s\n"
#define MSGFAILED "Failed to make directory \"%s\"; %s\n"
extern int optind, errno;
extern char *optarg;
static char
*simplify(char *path);
void
errmsg(int severity, int code, char *format, ...);
extern mode_t
newmode(char *ms, mode_t new_mode, mode_t umsk, char *file, char *path);
#define ALLRWX (S_IRWXU | S_IRWXG | S_IRWXO)
int
main(int argc, char *argv[])
{
int pflag, errflg, mflag;
int c, local_errno, tmp_errno;
mode_t cur_umask;
mode_t mode;
mode_t modediff;
char *d;
struct stat buf;
pflag = mflag = errflg = 0;
local_errno = 0;
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
#endif
(void) textdomain(TEXT_DOMAIN);
cur_umask = umask(0);
mode = ALLRWX;
while ((c = getopt(argc, argv, "m:p")) != EOF) {
switch (c) {
case 'm':
mflag++;
mode = newmode(optarg, ALLRWX, cur_umask, "", "");
break;
case 'p':
pflag++;
break;
case '?':
errflg++;
break;
}
}
/*
* When using default ACLs, mkdir() should be called with
* 0777 always; and umask or default ACL should do the work.
* Because of the POSIX.2 requirement that the
* intermediate mode be at least -wx------,
* we do some trickery here.
*
* If pflag is not set, we can just leave the umask as
* it the user specified it, unless it masks any of bits 0300.
*/
if (pflag) {
modediff = cur_umask & (S_IXUSR | S_IWUSR);
if (modediff)
cur_umask &= ~modediff;
}
(void) umask(cur_umask);
argc -= optind;
if (argc < 1 || errflg) {
errmsg(0, 2, gettext(MSGUSAGE));
}
argv = &argv[optind];
errno = 0;
while (argc--) {
if ((d = simplify(*argv++)) == NULL) {
exit(2);
}
/*
* When -p is set, invokes mkdirp library routine.
* Although successfully invoked, mkdirp sets errno to ENOENT
* if one of the directory in the pathname does not exist,
* thus creates a confusion on success/failure status
* possibly checked by the calling routine or shell.
* Therefore, errno is reset only when
* mkdirp has executed successfully, otherwise save
* in local_errno.
*/
if (pflag) {
/*
* POSIX.2 says that it is not an error if
* the argument names an existing directory.
* We will, however, complain if the argument
* exists but is not a directory.
*/
if (lstat(d, &buf) != -1) {
if (S_ISDIR(buf.st_mode)) {
continue;
} else {
local_errno = EEXIST;
errmsg(0, 0, gettext(MSGEXISTS), d);
continue;
}
}
errno = 0;
if (mkdirp(d, ALLRWX) < 0) {
tmp_errno = errno;
if (tmp_errno == EEXIST) {
if (lstat(d, &buf) != -1) {
if (! S_ISDIR(buf.st_mode)) {
local_errno =
tmp_errno;
errmsg(0, 0, gettext(
MSGEXISTS), d);
continue;
}
/* S_ISDIR: do nothing */
} else {
local_errno = tmp_errno;
perror("mkdir");
errmsg(0, 0,
gettext(MSGFAILED), d,
strerror(local_errno));
continue;
}
} else {
local_errno = tmp_errno;
errmsg(0, 0, gettext(MSGFMT1), d,
strerror(tmp_errno));
continue;
}
}
errno = 0;
/*
* get the file mode for the newly
* created directory and test for
* set gid bit being inherited from the parent
* directory to include it with the file
* mode creation for the last directory
* on the dir path.
*
* This is only needed if mflag was specified
* or if the umask was adjusted with -wx-----
*
* If mflag is specified, we chmod to the specified
* mode, oring in the 02000 bit.
*
* If modediff is set, those bits need to be
* removed from the last directory component,
* all other bits are kept regardless of umask
* in case a default ACL is present.
*/
if (mflag || modediff) {
mode_t tmpmode;
(void) lstat(d, &buf);
if (modediff && !mflag)
tmpmode = (buf.st_mode & 07777)
& ~modediff;
else
tmpmode = mode | (buf.st_mode & 02000);
if (chmod(d, tmpmode) < 0) {
tmp_errno = errno;
local_errno = errno;
errmsg(0, 0, gettext(MSGFMT1), d,
strerror(tmp_errno));
continue;
}
errno = 0;
}
continue;
} else {
/*
* No -p. Make only one directory
*/
errno = 0;
if (mkdir(d, mode) < 0) {
local_errno = tmp_errno = errno;
errmsg(0, 0, gettext(MSGFAILED), d,
strerror(tmp_errno));
continue;
}
if (mflag) {
mode_t tmpmode;
(void) lstat(d, &buf);
tmpmode = mode | (buf.st_mode & 02000);
if (chmod(d, tmpmode) < 0) {
tmp_errno = errno;
local_errno = errno;
errmsg(0, 0, gettext(MSGFMT1), d,
strerror(tmp_errno));
continue;
}
errno = 0;
}
}
} /* end while */
/* When pflag is set, the errno is saved in local_errno */
if (local_errno)
errno = local_errno;
return (errno ? 2: 0);
}
/*
* errmsg - This is an interface required by the code common to mkdir and
* chmod. The severity parameter is ignored here, but is meaningful
* to chmod.
*/
/* ARGSUSED */
/* PRINTFLIKE3 */
void
errmsg(int severity, int code, char *format, ...)
{
va_list ap;
va_start(ap, format);
(void) fprintf(stderr, "mkdir: ");
(void) vfprintf(stderr, format, ap);
va_end(ap);
if (code > 0) {
exit(code);
}
}
/*
* simplify - given a pathname in a writable buffer, simplify that
* path by removing meaningless occurances of path
* syntax.
*
* The change happens in place in the argument. The
* result is neceassarily no longer than the original.
*
* Return the pointer supplied by the caller on success, or
* NULL on error.
*
* The caller should handle error reporting based upon the
* returned vlaue.
*/
static char *
simplify(char *mbPath)
{
int i;
size_t mbPathlen; /* length of multi-byte path */
size_t wcPathlen; /* length of wide-character path */
wchar_t *wptr; /* scratch pointer */
wchar_t *wcPath; /* wide-character version of the path */
/*
* bail out if there is nothing there.
*/
if (!mbPath)
return (mbPath);
/*
* convert the multi-byte version of the path to a
* wide-character rendering, for doing our figuring.
*/
mbPathlen = strlen(mbPath);
if ((wcPath = calloc(sizeof (wchar_t), mbPathlen+1)) == NULL) {
perror("mkdir");
exit(2);
}
if ((wcPathlen = mbstowcs(wcPath, mbPath, mbPathlen)) == (size_t)-1) {
free(wcPath);
return (NULL);
}
/*
* remove duplicate slashes first ("//../" -> "/")
*/
for (wptr = wcPath, i = 0; i < wcPathlen; i++) {
*wptr++ = wcPath[i];
if (wcPath[i] == '/') {
i++;
while (wcPath[i] == '/') {
i++;
}
i--;
}
}
*wptr = '\0';
/*
* next skip initial occurances of "./"
*/
for (wcPathlen = wcslen(wcPath), wptr = wcPath, i = 0;
i < wcPathlen-2 && wcPath[i] == '.' && wcPath[i+1] == '/';
i += 2) {
/* empty body */
}
/*
* now make reductions of various forms.
*/
while (i < wcPathlen) {
if (i < wcPathlen-2 && wcPath[i] == '/' &&
wcPath[i+1] == '.' && wcPath[i+2] == '/') {
/* "/./" -> "/" */
i += 2;
} else {
/* Normal case: copy the character */
*wptr++ = wcPath[i++];
}
}
*wptr = '\0';
/*
* now convert back to the multi-byte format.
*/
if (wcstombs(mbPath, wcPath, mbPathlen) == (size_t)-1) {
free(wcPath);
return (NULL);
}
free(wcPath);
return (mbPath);
}
|