Unified · Split
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | 20110329 |
| 2 | + utils/mkdir/mkdir.c - add -p option [mechiel, issue 250] | |
| 2 | 3 | emu/Linux/mkfile - try searching -lpthread after -lrt for sem_* [issue 247] |
| 3 | 4 | emu/port/devfs-posix.c - treat unix domain sockets as files (open/read/write/close) [npe, rsc, issue 254] |
| 4 | 5 | emu/port/devfs-posix.c - use volatile correctly; slight code tidy |
| @@ -1,14 +1,56 @@ | ||
| 1 | 1 | #include <lib9.h> |
| 2 | 2 | |
| 3 | −void | |
| 4 | −main(int argc, char **argv) | |
| 3 | +static void | |
| 4 | +usage(void) | |
| 5 | +{ | |
| 6 | + fprint(2, "usage: mkdir [-p] dir ...\n"); | |
| 7 | + exits("usage"); | |
| 8 | +} | |
| 9 | + | |
| 10 | +static int | |
| 11 | +mkdirp(char *s, int pflag) | |
| 5 | 12 | { |
| 6 | − for(argv++; *argv; argv++){ | |
| 7 | − if(access(*argv, 0) == 0){ | |
| 8 | − fprint(2, "mkdir: %s already exists\n", *argv); | |
| 13 | + char *p; | |
| 14 | + | |
| 15 | + if(!pflag) { | |
| 16 | + if(access(s, 0) == 0){ | |
| 17 | + fprint(2, "mkdir: %s already exists\n", s); | |
| 9 | 18 | exits("exists"); |
| 10 | 19 | } |
| 11 | − if(mkdir(*argv) < 0){ | |
| 20 | + return mkdir(s); | |
| 21 | + } | |
| 22 | + | |
| 23 | + /* create intermediate directories */ | |
| 24 | + p = strchr(s+1, '/'); | |
| 25 | + while(p != nil) { | |
| 26 | + *p = '\0'; | |
| 27 | + if(access(s, 0) != 0 && mkdir(s) != 0) | |
| 28 | + return -1; | |
| 29 | + *p = '/'; | |
| 30 | + p = strchr(p+1, '/'); | |
| 31 | + } | |
| 32 | + | |
| 33 | + /* create final directory */ | |
| 34 | + if(access(s, 0) == 0) | |
| 35 | + return 0; | |
| 36 | + return mkdir(s); | |
| 37 | +} | |
| 38 | + | |
| 39 | +void | |
| 40 | +main(int argc, char **argv) | |
| 41 | +{ | |
| 42 | + int pflag; | |
| 43 | + | |
| 44 | + pflag = 0; | |
| 45 | + ARGBEGIN{ | |
| 46 | + case 'p': | |
| 47 | + pflag++; | |
| 48 | + break; | |
| 49 | + default: | |
| 50 | + usage(); | |
| 51 | + }ARGEND | |
| 52 | + for(; *argv; argv++){ | |
| 53 | + if(mkdirp(*argv, pflag) < 0){ | |
| 12 | 54 | fprint(2, "mkdir: can't create %s\n", *argv); |
| 13 | 55 | perror(0); |
| 14 | 56 | exits("error"); |