diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cmd/mkdir.C | 78 |
1 files changed, 61 insertions, 17 deletions
diff --git a/src/cmd/mkdir.C b/src/cmd/mkdir.C index 5bfa6773..11421d97 100644 --- a/src/cmd/mkdir.C +++ b/src/cmd/mkdir.C @@ -1,26 +1,70 @@ #include <u.h> #include <libc.h> +char *e; + +void +usage(void) +{ + fprint(2, "usage: mkdir [-p] 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 | 0777L); + 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, f; - char *e; - - e = nil; - for(i=1; i<argc; i++){ - if(access(argv[i], 0) == AEXIST){ - fprint(2, "mkdir: %s already exists\n", argv[i]); - e = "error"; - continue; - } - f = create(argv[i], OREAD, DMDIR | 0777L); - if(f < 0){ - fprint(2, "mkdir: can't create %s: %r\n", argv[i]); - e = "error"; - continue; - } - close(f); + int i, pflag; + + pflag = 0; + ARGBEGIN{ + default: + usage(); + case 'p': + pflag = 1; + break; + }ARGEND + + for(i=0; i<argc; i++){ + if(pflag) + mkdirp(argv[i]); + else + makedir(argv[i]); } exits(e); } |