Repository navigation
Expand file tree
/
Copy pathSLOsh.c
More file actions
386 lines (346 loc) · 11.2 KB
/
Copy pathSLOsh.c
File metadata and controls
386 lines (346 loc) · 11.2 KB
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
* SLOsh - San Luis Obispo Shell
* CSC 453 - Operating Systems
*
* TODO: Complete the implementation according to the comments
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h>
#include <limits.h>
#include <errno.h>
/* Define PATH_MAX if it's not available */
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
#define MAX_INPUT_SIZE 1024
#define MAX_ARGS 64
/* Global variable for signal handling */
volatile sig_atomic_t child_running = 0;
/* Forward declarations */
void display_prompt(void);
/**
* Signal handler for SIGINT (Ctrl+C)
*
* TODO: Implement the signal handler to:
* 1. Print a newline
* 2. If no child is running, display a prompt
* 3. Make sure the shell doesn't exit when SIGINT is received
*/
void sigint_handler(int sig) {
/* TODO: Your implementation here */
printf("\n");
if(child_running == 0){
printf("No child process running\n");
display_prompt();
}else{
kill(child_running, SIGINT);
}
return;
}
/**
* Display the command prompt with current directory
*/
void display_prompt(void) {
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("%s> ", cwd);
} else {
perror("getcwd");
printf("SLOsh> ");
}
fflush(stdout);
}
/**
* Parse the input line into command arguments
*
* TODO: Parse the input string into tokens and store in args array
*
* @param input The input string to parse
* @param args Array to store parsed arguments
* @return Number of arguments parsed
*/
int parse_input(char *input, char **args) {
/* TODO: Your implementation here */
// Exit on command temporarily here for testing
int count = 0;
// Tokenize the input string using whitespace as the delimiter
char *token = strtok(input, " \t\n");
while (token != NULL && count < MAX_ARGS - 1) {
args[count++] = token;
token = strtok(NULL, " \t\n");
}
// Null-terminate the args array
args[count] = NULL;
return count;
}
/**
* Execute the pipe command
*
* @param args Array of command arguments (NULL-terminated)
* @param cmdIndex Pipe comman index in arg array
*/
void execute_pipe(char **cmdLeft, char **cmdRight) {
int fds[2];
pipe(fds);
pid_t child1 = fork();
pid_t child2 = fork();
if((child1 < 0) || (child2 < 0)){ //error while forking
perror("failed fork");
exit(EXIT_FAILURE);
} else{ //successful forking
if(child1 == 0){ // cmdLeft child process
signal(SIGINT, SIG_DFL); //reset signal handing
close(fds[0]);
dup2(fds[1], STDOUT_FILENO); // redirect stdout to pipe
close(fds[1]);
execvp(cmdLeft[0], cmdLeft); //execute the command
perror(cmdLeft[0]);
exit(EXIT_FAILURE);
}
if(child2 == 0){ // cmdRight child process
signal(SIGINT, SIG_DFL); // reset signal handing
close(fds[1]);
dup2(fds[0], STDIN_FILENO); // redirect stdin to pipe
close(fds[0]);
execvp(cmdRight[0], cmdRight); // execute the command
perror(cmdRight[0]);
exit(EXIT_FAILURE);
}
}
//parent process
int status;
close(fds[0]);
close(fds[1]);
child_running = child1;
waitpid(child_running, &status, 0);
//if child1 ended with failure
if(WEXITSTATUS(status)){printf("Child exited with status %d.\n", WEXITSTATUS(status));}
child_running = child2;
waitpid(child_running, &status, 0);
//if child2 ended with failure
if(WEXITSTATUS(status)){printf("Child exited with status %d.\n", WEXITSTATUS(status));}
}
/**
* Execute the redirect command
*
* @param args Array of command arguments (NULL-terminated)
* @param cmdIndex Pipe comman index in arg array
* @param clobber Flag for overwritting redirect destination
*/
void execute_redirect(char **args, int cmdIndex, int clobber) {
int fd;
char *filename = args[cmdIndex + 1];
args[cmdIndex] = NULL;
if(clobber){fd = open(filename, (O_WRONLY | O_CREAT | O_TRUNC), 0644);}
else{fd = open(filename, (O_WRONLY | O_CREAT | O_APPEND), 0644);}
if(fd < 0){
perror("open");
exit(EXIT_FAILURE);
}
if((child_running = fork()) == 0){ //child process
signal(SIGINT, SIG_DFL); //reset signal handing
dup2(fd, STDOUT_FILENO); // redirect stdout to the destination file
close(fd);
execvp(args[0], args); //execute the command
perror(args[0]);
exit(EXIT_FAILURE);
}
else if(child_running < 0){ //error while forking
perror("failed fork");
exit(EXIT_FAILURE);
} else { //parent process
int status;
waitpid(child_running, &status, 0);
//if child ended with failure
if(WEXITSTATUS(status)){
printf("Child exited with status %d.\n", WEXITSTATUS(status));
}
close(fd);
}
}
/**
* Execute the given command with its arguments
*
* TODO: Implement command execution with support for:
* 1. Basic command execution
* 2. Pipes (|)
* 3. Output redirection (> and >>)
*
* @param args Array of command arguments (NULL-terminated)
*/
void execute_command(char **args) {
char *commands[MAX_ARGS][MAX_ARGS];
int cmdIndex = 0, argIndex = 0, redirectIndex = -1, overwrite = 0;
// Check for redirection operators
for (int i = 0; args[i] != NULL; i++) {
if (strcmp(args[i], ">") == 0) {
redirectIndex = i;
overwrite = 1; // Overwrite mode
} else if (strcmp(args[i], ">>") == 0) {
redirectIndex = i;
overwrite = 0; // Append mode
}
}
// Split commands by pipe ('|')
for (int i = 0; args[i] != NULL; i++) {
if (strcmp(args[i], "|") == 0) {
commands[cmdIndex][argIndex] = NULL;
cmdIndex++;
argIndex = 0;
} else if (strcmp(args[i], ">") == 0) {
overwrite = 1; // Overwrite mode
redirectIndex = argIndex;
argIndex++;
} else if (strcmp(args[i], ">>") == 0) {
overwrite = 0; // Append mode
redirectIndex = argIndex;
argIndex++;
}else {
commands[cmdIndex][argIndex] = args[i];
argIndex++;
}
}
commands[cmdIndex][argIndex] = NULL;
cmdIndex++;
// Handle single command with redirection
if(cmdIndex == 1 && redirectIndex != -1) {
execute_redirect(commands[0], redirectIndex, overwrite);
return;
}
if (cmdIndex == 1) {
if (redirectIndex != -1) {
// Call execute_redirect for single command with redirection
execute_redirect(args, redirectIndex, overwrite);
return;
}
// Execute single command without redirection
if ((child_running = fork()) == 0) { // Child process
signal(SIGINT, SIG_DFL); // Reset signal handling
execvp(commands[0][0], commands[0]); // Execute the command
perror(commands[0][0]);
exit(EXIT_FAILURE);
} else if (child_running < 0) { // Error while forking
perror("fork");
return;
} else { // Parent process
int status;
waitpid(child_running, &status, 0);
if (WEXITSTATUS(status)) {
printf("Child exited with status %d.\n", WEXITSTATUS(status));
}
}
return;
}
// Handle multiple commands with pipes
int fds[2];
int in_fd = STDIN_FILENO; // Input file descriptor for the first command
for (int i = 0; i < cmdIndex; i++) {
pipe(fds); // Create a pipe
if ((child_running = fork()) == 0) { // Child process
signal(SIGINT, SIG_DFL); // Reset signal handling
// Redirect input
if (in_fd != STDIN_FILENO) {
dup2(in_fd, STDIN_FILENO);
close(in_fd);
}
// Redirect output if not the last command
if (i < cmdIndex - 1) {
dup2(fds[1], STDOUT_FILENO);
} else if (redirectIndex != -1) { // Last command with redirection
close(fds[0]);
close(fds[1]);
execute_redirect(commands[i], redirectIndex, overwrite);
exit(EXIT_SUCCESS);
}
close(fds[0]);
close(fds[1]);
// Debugging output
printf("Executing command: %s\n", commands[i][0]);
for (int j = 0; commands[i][j] != NULL; j++) {
printf("Arg[%d]: %s\n", j, commands[i][j]);
}
execvp(commands[i][0], commands[i]); // Execute the command
perror(commands[i][0]);
exit(EXIT_FAILURE);
} else if (child_running < 0) { // Error while forking
perror("fork");
return;
} else { // Parent process
int status;
waitpid(child_running, &status, 0);
if (WEXITSTATUS(status)) {
printf("Child exited with status %d.\n", WEXITSTATUS(status));
}
// Close the write end of the pipe and update in_fd
close(fds[1]);
in_fd = fds[0];
}
}
}
/**
* Check for and handle built-in commands
*
* TODO: Implement support for built-in commands:
* - exit: Exit the shell
* - cd: Change directory
*
* @param args Array of command arguments (NULL-terminated)
* @return 0 to exit shell, 1 to continue, -1 if not a built-in command
*/
int handle_builtin(char **args) {
/* TODO: Your implementation here */
if(strcmp(args[0], "exit") == 0){
return 0; /* Exit shell */
}else if(strcmp(args[0], "cd") == 0){
if(args[1] == NULL){
fprintf(stderr, "cd: missing argument\n");
return 1;
}
if(chdir(args[1]) != 0){
perror("cd");
}
return 1; /* Continue shell */
}
return -1; /* Not a builtin command */
}
int main(void) {
char input[MAX_INPUT_SIZE];
char *args[MAX_ARGS];
int status = 1;
int builtin_result;
/* TODO: Set up signal handling for SIGINT (Ctrl+C) */
if(signal(SIGINT, sigint_handler) == SIG_ERR) {
perror("signal");
exit(EXIT_FAILURE);
}
while (status) {
display_prompt();
/* Read input and handle signal interruption */
if (fgets(input, MAX_INPUT_SIZE, stdin) == NULL) {
/* TODO: Handle EOF and signal interruption */
break;
}
/* Parse input */
parse_input(input, args);
/* Handle empty command */
if (args[0] == NULL) {
continue;
}
/* Check for built-in commands */
builtin_result = handle_builtin(args);
if (builtin_result >= 0) {
status = builtin_result;
continue;
}
/* Execute external command */
execute_command(args);
}
printf("SLOsh exiting...\n");
return EXIT_SUCCESS;
}