forked from afcarl/myshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
327 lines (276 loc) · 7.81 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
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
/*
* =====================================================================================
*
* Filename: shell.c
*
* Description: Simple bash shell made for CIS3207
*
* Version: 1.0
* Created: 03/01/2017 04:54:19
* Revision: none
* Compiler: gcc
*
* Author: Tu V.T. Tran (tuvttran), [email protected]
* Company: Temple University
*
* =====================================================================================
*/
#define _GNU_SOURCE
#define _XOPEN_SOURCE
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "shell.h"
// to print current working directory
char *prompt;
// list of built-in commands
char *builtin_cmd[] = {
"cd",
"clr",
"echo",
"environ",
"export",
"help",
"pause",
"quit"
};
// array of pointer to function that takes char ** as input and return int
int (*builtin_func[]) (char **) = {
&sh_cd,
&sh_clr,
&sh_echo,
&sh_environ,
&sh_export,
&sh_help,
&sh_pause,
&sh_quit
};
// array of special characters
int symbols[] = {
PIPE,
LEFT,
RIGHT
};
int main(int argc, char **argv) {
// running commang loop
shell_loop();
return EXIT_SUCCESS;
}
void shell_loop(void) {
char *line;
char **args;
int status;
do {
// this is the current working directory
prompt = getcwd(prompt, BUF_SIZE);
// print the prompt
printf("%s>", prompt);
// read and parse line
line = read_line();
args = split_line(line);
// update status
status = sh_execute(args);
free(line);
free(args);
} while(status);
}
int num_builtins() {
return sizeof(builtin_cmd) / sizeof(char *);
}
// read the line and return a pointer to the beginning of the string
char *read_line(void) {
char *line = NULL;
size_t len = 0;
getline(&line, &len, stdin);
return line;
}
// split the line to extract command name and arguments
char **split_line(char *line) {
char **tokens;
char *token;
int pos = 0;
// allocate an array of string tokens
if ((tokens = malloc(sizeof(char*) * TOK_BUFSIZE)) == NULL)
exit(EXIT_FAILURE);
token = strtok(line, TOK_DELIM);
while (token != NULL) {
tokens[pos++] = token;
token = strtok(NULL, TOK_DELIM);
}
tokens[pos] = NULL;
return tokens;
}
// process launch
int proc_launch(char **args) {
pid_t pid, wpid;
int status;
// background flag
int bg = FALSE;
// process name to run
char proc_name[strlen(args[0])];
memset(proc_name, '\0', sizeof(proc_name));
// check if there is '&' indicating background process
if (args[0][strlen(args[0]) - 1] == '&') {
strncpy(proc_name, args[0], strlen(args[0]) - 1);
bg = TRUE;
} else {
// if no & just copy args[0] into proc_name
strcpy(proc_name, args[0]);
}
pid = Fork();
if (pid == 0) {
// child process
if (execvp(proc_name, args) < 0) {
// error exec-ing
fprintf(stderr, "shell: command not found: %s\n", args[0]);
exit(EXIT_FAILURE);
}
exit(-1);
} else {
// parent process
// don't wait if it's a background process
if (!bg) {
do {
wpid = waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
}
return 1;
}
// shell execute and return a status
int sh_execute(char **args) {
if (args[0] == NULL) {
// empty command
return 1;
}
// detect the special character and its position
int sym = detect_symbol(args);
int sym_pos = detect_symbol_pos(args);
// in the event there is no pipe or redirection
if (sym < 0 || sym_pos < 0) {
for (int i = 0; i < num_builtins(); i++) {
if (strcmp(args[0], builtin_cmd[i]) == 0) {
// this is a built in command
return (*builtin_func[i])(args);
}
}
return proc_launch(args);
// there is pipe and redirection
} else {
// length of args array
int len = args_len(args);
// args to the left
char *args_left[sym_pos + 1];
// args to the right
char *args_right[len - sym_pos];
// copy arguments on the left side
int i = 0;
for (; i < sym_pos; i++)
args_left[i] = args[i];
args_left[i++] = NULL;
// copy arguments on the right side
int j = 0;
for (; args[i] != NULL; i++, j++)
args_right[j] = args[i];
args_right[j] = NULL;
// if the special character is a PIPE
if (symbols[sym] == PIPE)
return invoke_pipe(args_left, args_right);
else if (symbols[sym] == LEFT)
return redirect(args_left, args_right, TRUE);
else
return redirect(args_left, args_right, FALSE);
}
return 1;
}
int redirect(char **args1, char **args2, int left) {
pid_t pid;
int status;
int fd;
char *filename;
filename = args2[0];
if (left) {
// it means the file serves as the source of input
if ((fd = open(filename, O_RDONLY, 0755)) == -1) {
fprintf(stderr, "shell: no such file or directory: %s\n", strerror(errno));
return EXIT_FAILURE;
}
dup2(fd, STDIN_FILENO);
} else {
// it means the file serves as the target of output
if ((fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0755)) == -1) {
fprintf(stderr, "shell: error creating file: %s\n", strerror(errno));
return EXIT_FAILURE;
}
dup2(fd, STDOUT_FILENO);
}
return 1;
}
int invoke_pipe(char **args1, char **args2) {
pid_t pid1, pid2;
int status1, status2;
int fd[2]; // file descriptor
// pipe the fd
if (pipe(fd) == -1) {
fprintf(stderr, "pipe error\n");
return EXIT_FAILURE;
}
// check if args1 is builtin
for (int i = 0; i < num_builtins(); i++) {
if (strcmp(args1[0], builtin_cmd[i]) == 0) {
// this is a built in command
pid1 = Fork();
if (pid1 == 0) {
// child process, which is the exec'd program that will get input
// from the built in command
// close the WRITE interface of the fd
close(fd[WRITE]);
dup2(fd[READ], STDIN_FILENO);
if (execvp(args2[0], args2) < 0) {
fprintf(stderr, "shell: command not found: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
} else {
// parent process, which sends the input to the exec'd program
// close READ interface of the fd
close(fd[READ]);
dup2(fd[WRITE], STDOUT_FILENO);
(*builtin_func[i])(args1);
waitpid(pid1, &status1, WUNTRACED);
return 1;
}
}
}
// if args1 is not built in
pid1 = Fork();
if (pid1 == 0) {
// child 1
// send input to pipe
close(fd[READ]);
dup2(fd[WRITE], STDOUT_FILENO);
return proc_launch(args1);
} else {
// parent branch
pid2 = Fork();
if (pid2 == 0) {
// child 2
close(fd[WRITE]);
dup2(fd[READ], STDIN_FILENO);
return proc_launch(args2);
} else {
// parent branch
close(fd[WRITE]);
close(fd[READ]);
waitpid(pid1, &status1, WUNTRACED);
waitpid(pid2, &status2, WUNTRACED);
return 1;
}
}
}