-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-shell.c
55 lines (49 loc) · 1.66 KB
/
simple-shell.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
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
/* Acts as a simple shell to process commands with up to 64 args.
* Precondition: None.
* Postcondition: The main process does not return The user must terminate it.
* However, its children will return 1 if the command they were given is not
* recognized or 0 if they were able to exit successfully.
* Side effects: Whenever a command is entered into the shell prompt, the current
* process will attempt to create a new process to run the command. This will
* produce any side effects that command might have.
*/
int main() {
size_t nbytes = 0;
char *string = NULL;
char *space = " ";
fprintf(stderr, "input> ");
while(getline(&string, &nbytes, stdin) != -1){
string[strlen(string) - 1] = '\0';
char *ptr = strtok(string, space);
char *argv[66];
int count = 0;
while(ptr != NULL && count < 65){
argv[count] = ptr;
ptr = strtok(NULL, space);
count++;
}
if(ptr != NULL){
fprintf(stderr, "Warning: More than 64 arguments passed, ignoring arguments " \
"from %s onwards...\n", ptr);
}
argv[count] = NULL;
pid_t pid = fork();
if(pid == 0){
if(execvp(argv[0], argv) == -1){
fprintf(stderr, "Error: The command %s could not be found.\n", argv[0]);
return 1;
}
}
else{
int status;
waitpid(pid, &status, 0);
fprintf(stderr, "<exit status %d>\n", WEXITSTATUS(status));
fprintf(stderr, "input> ");
}
}
return 0;
}