83 lines
1.4 KiB
C
83 lines
1.4 KiB
C
#include <sys/stat.h>
|
|
#include <libgen.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
#include "parse_mode.h"
|
|
unsigned int v_flag;
|
|
|
|
int do_mkdir(const char *path, mode_t mode) {
|
|
if (mkdir(path, mode)) {
|
|
fprintf(stderr, "mkdir: %s %s\n", path, strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
else if (v_flag)
|
|
printf("mkdir: %s created with mode: %d\n", path, mode);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int do_parents(const char *path, mode_t mode) {
|
|
if (path[0] == '.' || path[0] == '/')
|
|
return 0;
|
|
|
|
char *path2 = strdup(path);
|
|
if (!path2) {
|
|
fprintf(stderr, "mkdir: %s: %s\n", path, strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
const char *par = dirname(path2);
|
|
do_parents(par, mode);
|
|
do_mkdir(path2, mode);
|
|
|
|
free(path2);
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
unsigned int p_flag = 0;
|
|
mode_t mode = 0777;
|
|
|
|
int opt;
|
|
while ((opt = getopt(argc, argv, "pm:v")) != -1) {
|
|
switch (opt) {
|
|
case 'p':
|
|
p_flag = 1;
|
|
break;
|
|
|
|
case 'm':
|
|
mode = mu_parse_mode(optarg, 0);
|
|
break;
|
|
|
|
case 'v':
|
|
v_flag = 1;
|
|
break;
|
|
|
|
default:
|
|
printf("mkdir [dst1 dst2...]\n\t[-p Make parent dir] [-m MODE Mode]\n");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
argv += optind;
|
|
argc -= optind;
|
|
|
|
for (int i = 0; i < argc; i++) {
|
|
if (p_flag) {
|
|
if (do_parents(argv[i], mode))
|
|
return 1;
|
|
}
|
|
|
|
else {
|
|
if (do_mkdir(argv[i], mode))
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|