-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
267 lines (241 loc) · 7.74 KB
/
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
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
#include <fcntl.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#define MAX_COMMAND_LINE_LEN 1024
#define MAX_COMMAND_LINE_ARGS 128
char prompt[] = "> ";
char delimiters[] = " \t\r\n";
extern char **environ;
pid_t child_process_pid = (pid_t)-1;
void handler_interrupt(int signum) {
if (child_process_pid != -1) {
kill(child_process_pid, SIGKILL);
printf("\n");
}
}
void handler_alarm(int signum) { kill(child_process_pid, SIGKILL); }
int main() {
// Stores the string typed into the command line.
char command_line[MAX_COMMAND_LINE_LEN];
char cmd_bak[MAX_COMMAND_LINE_LEN];
// Stores the tokenized command line input.
char *arguments[MAX_COMMAND_LINE_ARGS];
while (true) {
memset(arguments, 0, MAX_COMMAND_LINE_ARGS); // clear former arguments
// getting path to current dir.
char curr_dir[MAX_COMMAND_LINE_LEN];
char *dir_ptr = getcwd(curr_dir, sizeof(curr_dir));
do {
// Print the shell prompt.
printf("%s%s", curr_dir, prompt);
fflush(stdout);
signal(SIGINT, handler_interrupt);
// Read input from stdin and store it in command_line. If there's an
// error, exit immediately. (If you want to learn more about this line,
// you can Google "man fgets")
if ((fgets(command_line, MAX_COMMAND_LINE_LEN, stdin) == NULL) &&
ferror(stdin)) {
fprintf(stderr, "fgets error");
exit(0);
}
} while (command_line[0] == 0x0A); // while just ENTER pressed
// If the user input was EOF (ctrl+d), exit the shell.
if (feof(stdin)) {
printf("\n");
fflush(stdout);
fflush(stderr);
return 0;
}
// TODO:
//
// 0. Modify the prompt to print the current working directory
// 1. Tokenize the command line input (split it on whitespace)
// command_line is a string containing what was typed into the terminal
char *token = strtok(command_line, delimiters);
int i = 0;
while (token != NULL) {
token[strcspn(token, "\r\n")] = 0;
arguments[i] = token;
token = strtok(NULL, " ");
i++;
}
// if arguments[last value of i] == "&", run in background? else
if (strcmp(arguments[i - 1], "&") == 0) { /*fork off child and run program*/
arguments[i - 1] = NULL;
pid_t background_process;
background_process = fork();
if (background_process < 0) {
printf("Error: Could not start process in the background\n");
} else if (background_process == 0) {
if (strcmp(arguments[0], "cd") == 0) {
if (arguments[1] == NULL) {
chdir(getenv("HOME"));
} else if (chdir(arguments[1]) != 0) {
printf("%s", arguments[1]);
perror("");
}
}
else if (strcmp(arguments[0], "pwd") == 0) {
printf("%s\n", curr_dir);
}
else if (strcmp(arguments[0], "echo") == 0) {
int j = 1;
// if the arguments[j] is an env (starts with $), print value stored
// instead of literal
if (arguments[j][0] == '$') {
char *actual_env = arguments[j] + 1;
printf("%s", getenv(actual_env));
} else {
printf("%s", arguments[j]);
}
j++;
while (arguments[j] != NULL) {
// if the arguments[j] is an env (starts with $), print value stored
// instead of literal
if (arguments[j][0] == '$') {
char *actual_env = arguments[j] + 1;
printf(" %s", getenv(actual_env));
} else {
printf(" %s", arguments[j]);
}
j++;
}
printf("\n");
}
else if (strcmp(arguments[0], "exit") == 0) {
exit(0);
}
else if (strcmp(arguments[0], "env") == 0) {
if (arguments[1] == NULL) {
size_t i = 0;
for (i = 0; environ[i] != NULL; i++) {
printf("%s\n", environ[i]);
}
} else {
printf("%s\n", getenv(arguments[1]));
}
}
else if (strcmp(arguments[0], "setenv") == 0) {
char *params[2]; // var_name and var_value
char *var_token = strtok(arguments[1], "=\n\r\t");
int x = 0;
while (var_token != NULL) {
// var_token[strcspn(var_token, "\r\n")] = 0;
params[x] = var_token;
var_token = strtok(NULL, " ");
x++;
}
setenv(params[0], params[1], 1);
}
// 3. Create a child process which will execute the command line input
else {
pid_t pid;
int status;
pid = fork();
if (pid < 0) {
printf("Error: Could not execute requested process\n");
} else if (pid == 0) {
if (execvp(arguments[0], arguments) == -1) {
perror("execvp() failed: ");
printf("An error occurred\n");
}
exit(0);
} else {
wait(NULL);
}
}
}
} else {
// 2. Implement Built-In Commands
if (strcmp(arguments[0], "cd") == 0) {
if (arguments[1] == NULL) {
chdir(getenv("HOME"));
} else if (chdir(arguments[1]) != 0) {
printf("%s", arguments[1]);
perror("");
}
}
else if (strcmp(arguments[0], "pwd") == 0) {
printf("%s\n", curr_dir);
}
else if (strcmp(arguments[0], "echo") == 0) {
int j = 1;
// if the arguments[j] is an env (starts with $), print value stored
// instead of literal
if (arguments[j][0] == '$') {
char *actual_env = arguments[j] + 1;
printf("%s", getenv(actual_env));
} else {
printf("%s", arguments[j]);
}
j++;
while (arguments[j] != NULL) {
// if the arguments[j] is an env (starts with $), print value stored
// instead of literal
if (arguments[j][0] == '$') {
char *actual_env = arguments[j] + 1;
printf(" %s", getenv(actual_env));
} else {
printf(" %s", arguments[j]);
}
j++;
}
printf("\n");
}
else if (strcmp(arguments[0], "exit") == 0) {
exit(0);
}
else if (strcmp(arguments[0], "env") == 0) {
if (arguments[1] == NULL) {
size_t i = 0;
for (i = 0; environ[i] != NULL; i++) {
printf("%s\n", environ[i]);
}
} else {
printf("%s\n", getenv(arguments[1]));
}
}
else if (strcmp(arguments[0], "setenv") == 0) {
char *params[2]; // var_name and var_value
char *var_token = strtok(arguments[1], "=\n\r\t");
int x = 0;
while (var_token != NULL) {
params[x] = var_token;
var_token = strtok(NULL, " ");
x++;
}
setenv(params[0], params[1], 1);
}
// 3. Create a child process which will execute the command line input
else {
pid_t pid;
int status;
pid = fork();
if (pid < 0) {
printf("Error: Could not execute requested process\n");
} else if (pid == 0) {
if (execvp(arguments[0], arguments) == -1) {
perror("execvp() failed: ");
printf("An error occurred\n");
}
exit(0);
} else {
// 4. The parent process should wait for the child to complete unless
// its a background process
child_process_pid = pid;
signal(SIGINT, handler_interrupt);
signal(SIGALRM, handler_alarm);
alarm(10);
wait(NULL);
}
}
}
}
// This should never be reached.
return -1;
}