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
|
#include <u.h>
#include <libc.h>
char *e;
ulong mode = 0777L;
void
usage(void)
{
fprint(2, "usage: mkdir [-p] [-m mode] dir...\n");
exits("usage");
}
int
makedir(char *s)
{
int f;
if(access(s, AEXIST) == 0){
fprint(2, "mkdir: %s already exists\n", s);
e = "error";
return -1;
}
f = create(s, OREAD, DMDIR | mode);
if(f < 0){
fprint(2, "mkdir: can't create %s: %r\n", s);
e = "error";
return -1;
}
close(f);
return 0;
}
void
mkdirp(char *s)
{
char *p;
for(p=strchr(s+1, '/'); p; p=strchr(p+1, '/')){
*p = 0;
if(access(s, AEXIST) != 0 && makedir(s) < 0)
return;
*p = '/';
}
if(access(s, AEXIST) != 0)
makedir(s);
}
void
main(int argc, char *argv[])
{
int i, pflag;
char *m;
pflag = 0;
ARGBEGIN{
default:
usage();
case 'm':
m = ARGF();
if(m == nil)
usage();
mode = strtoul(m, &m, 8);
if(mode > 0777)
usage();
break;
case 'p':
pflag = 1;
break;
}ARGEND
for(i=0; i<argc; i++){
if(pflag)
mkdirp(argv[i]);
else
makedir(argv[i]);
}
exits(e);
}
|