-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.c
105 lines (90 loc) · 2.3 KB
/
main.c
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
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
void usage(const char *prog) {
fprintf(stderr, "Usage: %s [OPTIONS] dir1 [dir2 ...]\n", prog);
fprintf(stderr, " OPTIONS can be any of the following:\n");
fprintf(stderr, " -v, --verbose\n");
fprintf(stderr, " print a message for each created directory\n");
fprintf(stderr, " -p, --parents\n");
fprintf(stderr, " no error if existing, make parent directories as needed\n");
}
int extract(int *argc, char **argv[], const char *str) {
int i, j;
int found = 0;
for(i = 1; i < *argc; i++) {
if(strcmp(str, (*argv)[i]) == 0) {
found = 1;
/* remove from the arguments */
(*argc)--;
for(j = i; j < *argc; j++)
(*argv)[j] = (*argv)[j+1];
i = 1;
}
}
return found;
}
int mkdirp(char *path, int mode, int verbose) {
char *end = &path[strlen(path)];
char *c;
for(c = path; c < end; c++)
if(*c == '/') *c = 0;
c = path;
do {
if(*path && mkdir(path, mode)) {
if(errno != EEXIST) {
fprintf(stderr, "mkdir: '%s': %s\n", path, strerror(errno));
for(c = path; c < end; c++) {
if(*c == 0) {
while(c < end && *c == 0)
*c++ = '/';
}
}
return 1;
}
}
else if(*path && verbose)
printf("mkdir: created directory '%s'\n", path);
while(c < end && *c != 0)
c++;
if(c != end)
*c = '/';
} while(c < end);
return 0;
}
int main(int argc, char *argv[]) {
int i;
int error = 0;
int verbose = 0;
int parents = 0;
/* look for 'verbose' option */
while(extract(&argc, &argv, "-v"))
verbose = 1;
while(extract(&argc, &argv, "--verbose"))
verbose = 1;
/* look for 'verbose' option */
while(extract(&argc, &argv, "-p"))
parents = 1;
while(extract(&argc, &argv, "--parents"))
parents = 1;
if(argc == 1) {
usage(argv[0]);
return 1;
}
for(i = 1; i < argc; i++) {
if(parents) {
if(mkdirp(argv[i], 0777, verbose))
error = 1;
}
else if(mkdir(argv[i], 0777)) {
if(parents && errno == EEXIST) {
fprintf(stderr, "mkdir: '%s': %s\n", argv[i], strerror(errno));
error = 1;
}
}
else if(verbose)
printf("mkdir: created directory '%s'\n", argv[i]);
}
return error;
}