-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmkdir.c
52 lines (47 loc) · 955 Bytes
/
mkdir.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
/* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "util.h"
static void mkdirp(char *, int);
int
main(int argc, char *argv[])
{
bool pflag = false;
char c;
int mode = S_IRWXU | S_IRWXG | S_IRWXO;
while((c = getopt(argc, argv, "m:p")) != -1)
switch(c) {
case 'm':
mode = strtoul (optarg, 0, 8);
break;
case 'p':
pflag = true;
break;
default:
exit(EXIT_FAILURE);
}
for(; optind < argc; optind++)
if(pflag)
mkdirp(argv[optind], mode);
else if(mkdir(argv[optind], mode) == -1)
eprintf("mkdir %s:", argv[optind]);
return EXIT_SUCCESS;
}
void
mkdirp(char *path, int mode)
{
char *p = path;
do {
if((p = strchr(&p[1], '/')))
*p = '\0';
if(mkdir(path, mode) == -1 && errno != EEXIST)
eprintf("mkdir %s:", path);
if(p)
*p = '/';
} while(p);
}