This commit is contained in:
K. Lange 2018-10-05 18:48:24 +09:00
parent 0c67e10a5d
commit 638c2dbea3

View File

@ -9,17 +9,63 @@
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
int makedir(const char * dir, int mask, int parents) {
if (!parents) return mkdir(dir,mask);
char * tmp = strdup(dir);
char * c = tmp;
while ((c = strchr(c+1,'/'))) {
*c = '\0';
if (mkdir(tmp,mask) < 0) {
if (errno == EEXIST) {
*c = '/';
continue;
} else {
return -1;
}
}
*c = '/';
continue;
}
return mkdir(tmp, mask);
}
int main(int argc, char ** argv) {
if (argc < 2) {
int retval = 0;
int parents = 0;
int opt;
while ((opt = getopt(argc, argv, "m:p")) != -1) {
switch (opt) {
case 'm':
fprintf(stderr, "%s: -m unsupported\n", argv[0]);
return 1;
case 'p':
parents = 1;
break;
}
}
if (optind == argc) {
fprintf(stderr, "%s: expected argument\n", argv[0]);
return 1;
}
mkdir(argv[1], 0x00);
return 0;
for (int i = optind; i < argc; ++i) {
if (makedir(argv[i], 0777, parents) < 0) {
if (parents && errno == EEXIST) continue;
fprintf(stderr, "%s: %s: %s\n", argv[0], argv[i], strerror(errno));
retval = 1;
}
}
return retval;
}