-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkdir.c
92 lines (73 loc) · 1.82 KB
/
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
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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
void mkdirr(char *arr[])
{
if (arr[2] != NULL)
{
arr[2][strcspn(arr[2], "\n")] = 0;
if (!strcmp(arr[1], "-v"))
{
if (mkdir(arr[2], 0777))
{
printf("mkdir: cannot create directory ‘%s’\n", arr[2]);
}
else
{
printf("mkdir: created directory '%s'\n", arr[2]);
}
}
else if (!strcmp(arr[1], "-p"))
{
char original_directory[10000];
getcwd(original_directory, 10000);
int i = 0;
char *cmdArr[10000];
char *cmd = strtok(arr[2], "/");
while (cmd != NULL)
{
cmdArr[i] = cmd;
cmd = strtok(NULL, "/");
i++;
}
char cwd[10000];
char arrr[20000];
for (int j = 0; j < i; j++)
{
getcwd(cwd, 10000);
strcat(arrr, cwd);
strcat(arrr, "/");
strcat(arrr, cmdArr[j]);
mkdir(arrr, 0777);
chdir(arrr);
memset(arrr, 0, 20000);
}
chdir(original_directory);
}
else
{
printf("mkdir: invalid option %s\n", arr[1]);
}
}
else if (arr[1] != NULL)
{
arr[1][strcspn(arr[1], "\n")] = 0;
if (mkdir(arr[1], 0777))
{
printf("mkdir: cannot create directory ‘%s’\n", arr[1]);
}
}
else
{
printf("mkdir: missing operand\n");
}
}
int main(int argc, char *argv[])
{
char *arr[] = {argv[1], argv[2], argv[3]};
mkdirr(arr);
return 0;
}