-
Notifications
You must be signed in to change notification settings - Fork 0
/
myshell.c
355 lines (292 loc) · 7.74 KB
/
myshell.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
/*
* This code implements a simple shell program
* It supports the internal shell command "exit",
* backgrounding processes with "&", input redirection
* with "<" and output redirection with ">".
* However, this is not complete.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <setjmp.h>
extern char **getaline();
/*
* Handle exit signals from child processes
*/
void sig_handler(int signal) {
int status;
int result = waitpid(-1, NULL, WNOHANG);
printf("Background process finished: %d\n", result);
}
struct sigaction sa;
void delete_zombies(void);
/*
* The main shell function
*/
main() {
int i;
char **args;
int result;
int block;
int output;
int input;
int append;
int child_id;
int status;
char *output_filename;
char *input_filename;
// Set up the signal handler
sigset(SIGCHLD, sig_handler);
// memset(&sa, 0, sizeof(sa));
// sa.sa_handler = delete_zombies;
// if(sigaction(SIGCHLD, &sa, 0)) {
// perror("sigaction");
// return 1;
// }
// Loop forever
while(1) {
// Print out the prompt and get the input
printf("prompt->");
args = getaline();
// No input, continue
if(args[0] == NULL)
continue;
// Check for internal shell commands, such as exit
if(internal_command(args))
continue;
// Check for an ampersand
int block = should_block(args);
if(check_for_pipes(args)) {
int in = 0;
char **tmp_args = args;
char **ptr;
// loop over args, set each | to NULL
for(ptr = args; *ptr != NULL; ptr++) {
//printf("tmpargs[0]: %s [1]: %s [2]: %s\n", tmp_args[0], tmp_args[1], tmp_args[2]);
if(strcmp(*ptr, "|") == 0) {
free(*ptr);
*ptr = NULL;
// do stuff with tmp_args as your new "args"
int pipefd[2];
pipe(pipefd);
child_id = do_command(tmp_args, in, pipefd[1], 1, block);
close(pipefd[1]);
if(child_id < 0) {
printf("syntax error\n");
continue;
}
in = pipefd[0];
tmp_args = ptr + 1;
}
}
//printf("tmpargs[0]: %s [1]: %s\n", tmp_args[0], tmp_args[1]);
child_id = do_command(tmp_args, in, 1, 1, block);
if(child_id < 0) {
printf("syntax error\n");
continue;
}
} else {
child_id = do_command(args, 0, 0, 0, block);
if(child_id < 0) {
printf("syntax error\n");
continue;
}
}
// Wait for the child process to complete, if necessary
if(block) {
printf("Waiting for child, pid = %d\n", child_id);
result = waitpid(child_id, &status, 0);
}
}
}
int check_for_pipes(char **args) {
int i;
for(i = 0; args[i] != NULL; i++) {
if(strcmp(args[i], "|") == 0) {
return 1;
}
}
return 0;
}
/*
* Check for ampersand as the last argument
*/
int should_block(char **args) {
int i;
for(i = 0; args[i] != NULL; i++) {
if(args[i][0] == '&') {
free(args[i]);
args[i] = NULL;
return 0;
}
}
return 1;
}
/*
* Check for internal commands
* Returns true if there is more to do, false otherwise
*/
int internal_command(char **args) {
if(strcmp(args[0], "exit") == 0) {
exit(0);
}
return 0;
}
/*
* Do the command
*/
int do_command(char **args, int in, int out, int pipe, int block) {
char *input_filename, *output_filename;
// Check for redirected input
int input = redirect_input(args, &input_filename);
switch(input) {
case -1:
printf("Syntax error!\n");
return -1;
break;
case 0:
break;
case 1:
printf("Redirecting input from: %s\n", input_filename);
break;
}
// check for append
int append = check_append(args, &output_filename);
// Check for redirected output
int output = redirect_output(args, &output_filename);
switch(append) {
case -1:
printf("Syntax error!\n");
return -1;
break;
case 0:
break;
case 1:
printf("Redirecting and appending output to: %s\n", output_filename);
break;
}
switch(output) {
case -1:
printf("Syntax error!\n");
return -1;
break;
case 0:
break;
case 1:
printf("Redirecting output to: %s\n", output_filename);
break;
}
int result;
pid_t child_id;
// Fork the child process
child_id = fork();
// Check for errors in fork()
switch(child_id) {
case EAGAIN:
perror("Error EAGAIN: ");
return child_id;
case ENOMEM:
perror("Error ENOMEM: ");
return child_id;
}
if(child_id == 0) {
// Set up redirection in the child process
if(out != 1) {
dup2(out, 1);
close(out);
}
if(in != 0) {
dup2(in, 0);
close(in);
}
if(input)
freopen(input_filename, "r", stdin);
if(output)
freopen(output_filename, "w+", stdout);
if(append)
freopen(output_filename, "a", stdout);
result = execvp(args[0], args);
exit(1);
}else {
return child_id;
}
return result;
}
/*
* Check for input redirection
*/
int redirect_input(char **args, char **input_filename) {
int i;
int j;
for(i = 0; args[i] != NULL; i++) {
// Look for the <
if(args[i][0] == '<') {
free(args[i]);
// Read the filename
if(args[i+1] != NULL) {
*input_filename = args[i+1];
} else {
return -1;
}
// Adjust the rest of the arguments in the array
for(j = i; args[j-1] != NULL; j++) {
args[j] = args[j+2];
}
return 1;
}
}
return 0;
}
/*
* Check for output redirection: return 1 for regular output, 2 for appending
*/
int redirect_output(char **args, char **output_filename) {
int i;
int j;
for(i = 0; args[i] != NULL; i++) {
// Look for the >
if(args[i][0] == '>') {
free(args[i]);
// Get the filename
if(args[i+1] != NULL) {
*output_filename = args[i+1];
} else {
return -1;
}
// Adjust the rest of the arguments in the array
for(j = i; args[j-1] != NULL; j++) {
args[j] = args[j+2];
}
return 1;
}
}
return 0;
}
int check_append(char **args, char **output_filename) {
int i;
int j;
for(i = 1; args[i] != NULL; i++) {
// look for >>
if(args[i-1][0] == '>' && args[i][0] == '>') {
free(args[i-1]);
free(args[i]);
// get filename
if(args[i+1] != NULL) {
*output_filename = args[i+1];
} else {
return -1;
}
// adjust rest of arg array
for(j = i - 1; args[j-1] != NULL; j++) {
args[j] = args[j+2];
}
return 1;
}
}
return 0;
}