-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkdir.c
132 lines (103 loc) · 2.34 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <ctype.h>
#include <stdlib.h>
#include <errno.h>
#define sz 100
extern int errno;
int main(int argc, char *argv[])
{
errno = 0;
char *flag = argv[1], *args = argv[2];
if(strlen(args) == 0){
printf("%s", "Error: File name not given!");
return 0;
}
char **files = (char**)malloc(sz * sizeof(char*));
int f = 0;
char *temp = strtok(args, " ");
while (temp != NULL){
files[f] = temp;
f++;
temp = strtok(NULL," ");
}
if(strlen(flag) == 0){
for (int i = 0; i < f; i++){
char *args = files[i];
int check = mkdir(args, 0);
if(check == -1){
perror("Error");
}
else
chmod(args, 0777);
}
}
else if(strlen(flag) == 1){
if(flag[0] == 'v'){
for (int i = 0; i < f; i++){
char *args = files[i]; //mkdir -v dir_name
int check = mkdir(args, 0);
if(check == -1){
perror("Error");
}
else{
chmod(args, 0777);
printf("Created directory \"%s\"\n", args);
}
}
}
else if(flag[0] == 'm'){ //mkdir -m dir_name
if (f > 2){
printf("Error: Only one file for this flag!");
return 0;
}
char *args = (char*)malloc(sz * sizeof(char));
args = files[0];
int ptr = 0, ptr_mode;
char *mode = (char*)malloc(100 * sizeof(char));
for(int i = 0; args[i] != '\0' && args[i] != ' '; i++){
mode[i] = args[i];
if(isdigit(mode[i]) == 0){ //not a numeric char
printf("Invalid mode value!");
return 0;
}
ptr++;
mode[i + 1] = '\0';
}
int len = (int) strlen(mode);
mode_t sum = 0;
for(int i = 0; i < len; i++){
int p = 1;
for(int j = 0; j < len - i - 1; j++){
p *= 8;
}
sum += p*(mode[i] - '0');
}
char *filename = (char*)malloc(100 * sizeof(char));
filename = files[1];
while(args[ptr] != '\0' && args[ptr] == ' ')
ptr++;
int ptr2 = 0;
for(int i = ptr; args[i] != '\0'; i++){
filename[ptr2] = args[i];
ptr2++;
}
sum = strtol(mode, NULL, 8);
int check = mkdir(filename, 0);
if(check == -1){
perror("Error");
}
else
chmod(filename, sum);
}
else
printf("%s", "Error: Invalid flag!");
}
else
printf("%s", "Error: Invalid flag!");
return 0;
}