-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwish.c
363 lines (314 loc) · 7.26 KB
/
wish.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
CT30A3370_03.09.2018 Käyttöjärjestelmät ja systeemiohjelmointi
Harjoitustyö
Project 2: Unix Shell
16.12.2018
Miikka Mättölä
---
Changelog:
2018-12-13 Initial version (MM)
2018-12-15 Path implementation (MM)
2018-12-15 Execute processes (MM)
2018-12-16 Change directories (MM)
2018-12-16 Implement redirection (MM)
*/
/* This is needed for getline() */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#define PATH_SIZE 128
/* Define default path */
char *path[PATH_SIZE] = {"/bin"};
int match(const char *a, const char *b) {
/* Compare two strings */
if (strcmp(a, b) == 0) {
return 1;
}
return 0;
}
int has_string(char *haystack, char *needle) {
char *ret;
/* Search for needle in the haystack */
ret = strstr(haystack, needle);
if (!ret) {
/* String not found (NULL) */
return 0;
} else {
/* String was found */
return 1;
}
}
void change_dir(const char *dir) {
int change;
char cwd[PATH_MAX];
change = chdir(dir);
if (change == 0) {
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("%s\n", cwd);
} else {
perror("Unable to get current dir");
}
} else {
/* Can not change dir */
perror("Unable to change directory");
}
}
char *locate(char *bin) {
int i = 0;
char *current;
while (path[i]) {
/* Reserve memory for path */
current = malloc(strlen(path[i]) + 1 + strlen(bin) + 1);
if (!current) {
perror("Unable to allocate memory");
exit(EXIT_FAILURE);
}
strcpy(current, path[i]);
strcat(current, "/");
strcat(current, bin);
if (access(current, X_OK) == 0) {
/* Found binary */
return current;
}
i++;
}
/* File not found */
return NULL;
}
void execute(char *filepath, char **arg, char *redirect) {
pid_t cpid;
int w, e, fd, d;
cpid = fork();
if (cpid == -1) {
/* Error with fork() */
perror("Unable to create process");
exit(EXIT_FAILURE);
} else if (cpid == 0) {
/* Child process */
/* Check if redirection is needed */
if (redirect) {
/* Open file for redirection */
fd = open(redirect, O_CREAT|O_TRUNC|O_WRONLY, 0666);
if (fd == -1) {
/* Error with open() */
perror("Unable to open file");
exit(EXIT_FAILURE);
}
d = dup2(STDOUT_FILENO, STDERR_FILENO);
if (d == -1) {
/* Error with dup2() */
perror("Unable to duplicate");
exit(EXIT_FAILURE);
}
d = dup2(fd, STDOUT_FILENO);
if (d == -1) {
/* Error with dup2() */
perror("Unable to duplicate");
exit(EXIT_FAILURE);
}
}
e = execv(filepath, arg);
if (e == -1) {
/* Error creating process */
perror("Execute error");
exit(EXIT_FAILURE);
}
} else {
/* Parent process */
w = wait(NULL);
if (w == -1) {
/* Error with child process */
perror("Wait error");
exit(EXIT_FAILURE);
}
}
}
void set_path(char **list, int count) {
int i;
/* Clear old path */
for (i = 0; i < PATH_SIZE; i++) {
path[0] = NULL;
}
if (count < 2) {
/* Empty path */
return;
}
/* Set and reserve memory for new path */
for (i = 1; i < count; i++) {
path[i - 1] = malloc(strlen(list[i]) + 1);
strcpy(path[i - 1], list[i]);
}
/* Print out new path */
printf("Path set:\n%s", path[0]);
for (i = 1; i < count - 1; i++) {
printf(":%s", path[i]);
}
printf("\n");
}
int parse_string(char* string, char* delim, char ***ret) {
char **list;
char *token;
int i = 0;
/* Reserve memory for list */
list = (char**) malloc(sizeof(char*) * strlen(string));
if (!list)
{
perror("Unable to allocate memory");
exit(EXIT_FAILURE);
}
/* Parse string */
while ((token = strtok(string, delim)) != NULL) {
list[i] = malloc(strlen(token) + 1);
strcpy(list[i], token);
i++;
string = NULL;
}
list = realloc(list, sizeof(char*) * (i + 1));
/* Add null pointer to end of list */
/* This is required for execv() */
list[i] = NULL;
*ret = list;
return i;
}
void process_command(char *command) {
char **arg_list;
int arg_count;
char *cmd;
int i;
char *filepath;
char **red_list;
int red_count;
char *redirect = NULL;
char **test_list;
int test_count;
/* Redirection feature */
if (has_string(command, ">")) {
/* Parse command string for redirection */
red_count = parse_string(command, ">", &red_list);
if (red_count == 2) {
/* Test if string is valid and remove spaces */
test_count = parse_string(red_list[1], " ", &test_list);
if (test_count == 1) {
/* Valid command for redirection */
command = strdup(red_list[0]);
redirect = strdup(test_list[0]);
} else {
/* Error: multiple files specified */
printf("Invalid command\n");
return;
}
/* Free memory */
for (i = 0; i < test_count; i++) {
free(test_list[i]);
}
free(test_list);
} else {
/* Error: multiple operators or missing arguments */
printf("Invalid command\n");
return;
}
/* Free memory */
for (i = 0; i < red_count; i++) {
free(red_list[i]);
}
free(red_list);
}
/* Parse command string and return number of arguments */
arg_count = parse_string(command, " ", &arg_list);
/* Process built-in commands */
cmd = strdup(arg_list[0]);
if (match(cmd, "exit")) {
if (arg_count == 1) {
exit(EXIT_SUCCESS);
} else {
printf("Invalid command\n");
}
}
else if (match(cmd, "cd")) {
if (arg_count == 2) {
/* Change directory */
change_dir(arg_list[1]);
} else {
printf("Invalid command\n");
}
}
else if (match(cmd, "path")) {
/* Set path */
set_path(arg_list, arg_count);
}
else {
/* Locate program binary and execute */
filepath = locate(cmd);
if (filepath) {
/* Execute program */
execute(filepath, arg_list, redirect);
} else {
printf("Command not found\n");
}
}
/* Free memory */
for (i = 0; i < arg_count; i++) {
free(arg_list[i]);
}
free(arg_list);
}
void prompt(void) {
char *line = NULL;
size_t len = 0;
ssize_t ret;
printf("wish> ");
while ((ret = getline(&line, &len, stdin)) != -1) {
/* Remove newline char */
line[strcspn(line, "\r\n")] = 0;
/* Execute line if it is not empty */
if (strlen(line) > 0) {
process_command(line);
}
printf("wish> ");
}
free(line);
}
void read_file(char *filename) {
FILE *file;
char *line = NULL;
size_t len = 0;
ssize_t ret;
/* Open file in read mode */
file = fopen(filename, "r");
if (!file) {
printf("Can not open file\n");
perror(filename);
exit(EXIT_FAILURE);
}
while ((ret = getline(&line, &len, file)) != -1) {
/* Remove newline char */
line[strcspn(line, "\r\n")] = 0;
/* Execute line if it is not empty */
if (strlen(line) > 0) {
process_command(line);
}
}
free(line);
fclose(file);
}
int main(int argc, char *argv[]) {
if (argc < 2) {
/* Interactive mode */
prompt();
} else if (argc == 2) {
/* Batch mode */
read_file(argv[1]);
} else {
/* Too many arguments */
printf("Too many arguments\n");
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}