-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd_mains.c
213 lines (190 loc) · 4.67 KB
/
cmd_mains.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "misc.h"
#include "cmd_mains.h"
#include "arg_parse.h"
int rm_main(char *buf)
{
char **args;
int pflag = 0, vflag = 0, rflag = 0, hflag = 0, i = 0;
args = get_args(buf, 2);
char* flags = get_flags(buf);
if (flags[0] != '\0') {
do {
switch (flags[i])
{
case 'v':
vflag = 1;
break;
case 'p':
pflag = 1;
break;
case 'r':
rflag = 1;
break;
case 'h':
hflag = 1;
break;
}
i++;
} while (flags[i] != '\0');
}
if (hflag) {
printf("rm [FLAGS] [FILES(s)]\n"
"remove the FILE(s)\n\n"
"-p\tprompt before each removal\n"
"-v\texplain what is being done\n"
"-r\trecursively delete directories\n");
return 1;
}
if (_argc == 0) {
fprintf(stderr, "error: no operand.\n"
"Type 'rm -h' for more information.\n");
return 1;
}
for (i = 0; i < _argc; ++i) {
rm(args[i], vflag, pflag, rflag);
}
free_args(args);
free_flags(flags);
return 0;
}
int ls_main(char *buf)
{
char **args;
int aflag = 0, iflag = 0, rflag = 0, hflag = 0, i = 0;
args = get_args(buf, 2);
char* flags = get_flags(buf);
if (flags[0] != '\0') {
do {
switch(flags[i])
{
case 'a':
aflag = 1;
break;
case 'i':
iflag = 1;
break;
case 'h':
hflag = 1;
break;
case 'r':
rflag = 1;
break;
}
i++;
} while (flags[i] != '\0');
}
if (hflag) {
ls_usage:
printf("ls [FLAGS] [DIRECTORY]\n"
"list the contents of DIRECTORY -- current dir is used if none given\n\n"
"a\tdisplay hidden files (files starting with '.')\n"
"i\tprint the index of each file\n"
"r\trecursively list the contents of sub-directories\n");
return 1;
}
if (_argc == 0)
ls("./", aflag, iflag, rflag);
else if (_argc > 1) {
printf("ls: too many arguments\n\n");
goto ls_usage;
} else
ls(args[0], aflag, iflag, rflag);
free_args(args);
free_flags(flags);
return 0;
}
int mkdir_main(char *buf)
{
char **args;
int vflag = 0, hflag = 0, i = 0;
args = get_args(buf, 5);
char* flags = get_flags(buf);
if (flags[0] != '\0') {
do {
switch (flags[i])
{
case 'h':
hflag = 1;
break;
case 'v':
vflag = 1;
break;
}
i++;
} while (flags[i] != '\0');
}
if (hflag) {
mkdir_usage:
printf("mkdir [FLAGS] [DIRECTORY...]\n"
"create a directory named DIRECTORY\n\n"
"v\texplain what is being done\n");
return 1;
}
if (_argc == 0) {
printf("mkdir: no arguments given\n\n");
goto mkdir_usage;
} else {
for (i = 0; i < _argc; ++i)
makedir(args[i], vflag);
}
free_args(args);
free_flags(flags);
return 0;
}
int cat_main(char *buf)
{
char **args;
int nflag = 0, hflag = 0, i = 0;
args = get_args(buf, 3);
char* flags = get_flags(buf);
if (flags[0] != '\0') {
do {
switch (flags[i])
{
case 'h':
hflag = 1;
break;
case 'n':
nflag = 1;
break;
}
i++;
} while (flags[i] != '\0');
}
if (hflag) {
cat_usage:
printf("cat [FLAGS] [FILE]\n"
"output the contents of FILE\n\n"
"n\tnumber the lines of output\n");
return 1;
}
if (_argc == 0) {
printf("cat: no arguments given\n");
goto cat_usage;
} else if (_argc > 1) {
printf("cat: too many arguments\n");
goto cat_usage;
} else
cat(args[0], nflag);
free_args(args);
free_flags(flags);
return 0;
}
int cd_main(char *buf)
{
char **args;
args = get_args(buf, 2);
if (_argc == 0) {
cd("/home");
return 1;
}
else if (_argc > 1) {
printf("cd: too many args.\n"
"usage: cd DIRECTORY\n\n");
return 1;
}
else
cd(args[0]);
free_args(args);
return 0;
}