-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
299 lines (250 loc) · 6.47 KB
/
main.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <dirent.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#define CLASH_RL_BUFSIZE 1024
#define CLASH_TOKEN_BUFSIZE 64
#define CLASH_TOKEN_DELIM " \t\r\n\a"
int get_clash_builtin_cmd_count();
char *get_dir_name();
int clash_launch(char **args);
int clash_execute(char **args);
void clash_loop(void);
char *clash_read_line(void);
char **clash_split_line(char *line);
int clash_echo(char **args);
int clash_cd(char **args);
int clash_pwd(char **args);
int clash_mkdir(char **args);
int clash_touch(char **args);
int clash_ls(char **args);
int clash_exit(char **args);
char *builtin_cmd_str[] = {"echo", "cd", "pwd", "mkdir", "touch", "ls", "exit"};
int (*builtin_cmd_func[])(char **) = {&clash_echo, &clash_cd, &clash_pwd,
&clash_mkdir, &clash_touch, &clash_ls,
&clash_exit};
int main() {
clash_loop();
return 0;
}
int get_clash_builtin_cmd_count() {
return sizeof(builtin_cmd_str) / sizeof(char *);
}
char *get_dir_name() {
int bufsize = CLASH_RL_BUFSIZE;
char *dirpath = malloc(sizeof(char) * bufsize);
getcwd(dirpath, bufsize);
if (dirpath == NULL) {
perror("clash");
}
free(dirpath);
return basename(dirpath);
}
int clash_launch(char **args) {
pid_t pid;
int status;
pid = fork();
if (pid == 0) {
if (execvp(args[0], args) == -1) {
perror("clash");
}
exit(EXIT_FAILURE);
} else if (pid < 0) {
perror("clash");
} else {
do {
waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
int clash_execute(char **args) {
int i;
if (args[0] == NULL) {
return 1;
}
for (i = 0; i < get_clash_builtin_cmd_count(); i++) {
if (strcmp(args[0], builtin_cmd_str[i]) == 0) {
return (builtin_cmd_func[i])(args);
}
}
return clash_launch(args);
}
char *clash_read_line(void) {
int bufsize = CLASH_RL_BUFSIZE;
int position = 0;
char *buffer = malloc(sizeof(char) * bufsize);
int c;
if (!buffer) {
fprintf(stderr, "clash: memory allocation error\n");
exit(EXIT_FAILURE);
}
for (;;) {
c = getchar();
if (c == EOF) {
exit(EXIT_SUCCESS);
} else if (c == '\n') {
buffer[position] = '\0';
return buffer;
} else {
buffer[position] = c;
}
position++;
if (position >= bufsize) {
bufsize += CLASH_RL_BUFSIZE;
buffer = realloc(buffer, bufsize);
if (!buffer) {
fprintf(stderr, "clash: memory allocation error\n");
exit(EXIT_FAILURE);
}
}
}
}
char **clash_split_line(char *line) {
int bufsize = CLASH_TOKEN_BUFSIZE;
int position = 0;
char **tokens = malloc(sizeof(char *) * bufsize);
char *token, *token_backup;
if (!tokens) {
fprintf(stderr, "clash: memory allocation error\n");
exit(EXIT_FAILURE);
}
token = strtok(line, CLASH_TOKEN_DELIM);
for (; token != NULL;) {
tokens[position] = token;
position++;
if (position >= bufsize) {
bufsize += CLASH_TOKEN_BUFSIZE;
token_backup = token;
tokens = realloc(tokens, sizeof(char *) * bufsize);
if (!tokens) {
free(token_backup);
fprintf(stderr, "clash: memory allocation error\n");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, CLASH_TOKEN_DELIM);
}
tokens[position] = NULL;
return tokens;
}
void clash_loop(void) {
char *line;
char **args;
int status;
do {
printf("%s> ", get_dir_name());
line = clash_read_line();
args = clash_split_line(line);
status = clash_execute(args);
free(line);
free(args);
} while (status);
}
int clash_echo(char **args) {
if (args[1] == NULL) {
fprintf(stderr, "clash: argument error");
} else {
fprintf(stdout, "%s\n", args[1]);
}
return 1;
}
int clash_cd(char **args) {
if (args[1] == NULL) {
fprintf(stderr, "clash: argument error");
} else {
if (chdir(args[1]) != 0) {
perror("clash");
}
}
return 1;
}
int clash_pwd(char **args) {
int bufsize = CLASH_RL_BUFSIZE;
char *dirname = malloc(sizeof(char) * bufsize);
getcwd(dirname, bufsize);
if (dirname == NULL) {
perror("clash");
}
fprintf(stdout, "%s\n", dirname);
free(dirname);
return 1;
}
int clash_mkdir(char **args) {
if (args[1] == NULL) {
fprintf(stderr, "clash: argument error");
} else {
if (mkdir(args[1], 0755) != 0) {
perror("clash");
}
}
return 1;
}
int clash_touch(char **args) {
FILE *fp;
fp = fopen(args[1], "w");
if (fp == NULL) {
perror("clash");
}
fclose(fp);
return 1;
}
int clash_ls(char **args) {
struct dirent *ent;
struct stat stat_buf;
DIR *pdir;
if (args[1] == NULL) {
pdir = opendir("./");
} else {
pdir = opendir(args[1]);
}
if (pdir == NULL) {
perror("clash");
}
for (; (ent = readdir(pdir)) != NULL;) {
fprintf(stdout, "%s\t", ent->d_name);
}
putchar('\n');
/*
if (args[1] != NULL && strcmp(args[1], "-l") == 0) {
if (args[2] == NULL) {
pdir = opendir("./");
} else {
pdir = opendir(args[2]);
}
if (pdir == NULL) {
perror("clash");
}
fprintf(stdout, "%-16s\t%s\n", "name", "update");
for (; (ent = readdir(pdir)) != NULL;) {
if (stat(ent->d_name, &stat_buf) == 0) {
fprintf(stdout, "%-16s\t%s", ent->d_name,
ctime(&stat_buf.st_ctimespec));
}
}
putchar('\n');
} else {
if (args[1] == NULL) {
pdir = opendir("./");
} else {
pdir = opendir(args[1]);
}
if (pdir == NULL) {
perror("clash");
}
for (; (ent = readdir(pdir)) != NULL;) {
fprintf(stdout, "%s\t", ent->d_name);
}
putchar('\n');
}
*/
closedir(pdir);
return 1;
}
int clash_exit(char **args) { return 0; }