-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysh1.c
More file actions
114 lines (88 loc) · 2.57 KB
/
mysh1.c
File metadata and controls
114 lines (88 loc) · 2.57 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
#include "mysh.h"
int main(int argc, char **argv) {
// Main loop of shell
while(1){
// Declare every variable that will be used
// pid : the process id used to determine the child/parent process and which process to wait
// waitPid : the process id return from the waitpid command of the parent
// input : the character array (string) of the input from the user
// params : the array of the command tokens used by execvp
// exitCode : used for error handling between the functions of functions.h
// status : used by waitpid command to get the status of the process waiting
pid_t pid,waitPid;
char* input;
char* params[2];
int exitCode,status;
// Print the prompt and get the user's input
input = getInput("mysh1",&exitCode);
// Handle possible errors using exitCode
// Exitcode: -1 EOF - Ctrl + D
// 1 input exceeds maximum characters
// 0 no problems
if(exitCode==1) {
// Free the data allocated from getInput
free(input);
// Goto start of the loop
continue;
}
if(exitCode==-1) {
// Free the data allocated from getInput
free(input);
// End loop - Terminate the shell
break;
}
// If the command from user's input is the command "exit" terminate the shell
if(strncmp(input,"exit",4)==0){
// Free the data allocated from getInput
free(input);
// End loop - Terminate the shell
break;
}
// Check if input is empty or whitespace characters
if(checkEmptyCommand(input)){
// Free the data allocated from getInput
free(input);
// Goto start of main loop
continue;
}
// Fork the process
pid = fork();
if(pid < 0){
// There was an error with fork
// Print the error
perror("Error with fork()\n");
// Free the data allocated from getInput
free(input);
// End loop - Terminate the shell
exit(1);
}
if(pid == 0){
// Child process
// Fill the null ended array needed by execvp with the command
params[0] = input;
params[1] = NULL;
// Execute the command
execvp(*params,params);
// There was an error
// Print the error
fprintf(stderr,"Execvp error : '%s' : ",params[0]);
perror("");
// Terminate the shell
exit(1);
}else{
// Parent process
// Wait until the child process executes the command
waitPid = waitpid(pid,&status,0);
// Free the data allocated from getInput
free(input);
// There was an error with waitpid
if (waitPid == -1) {
// Print the error
perror("ERROR: Waitpid failed.\n");
// Terminate the shell
exit(1);
}
}
}
return 0;
}