-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
298 lines (256 loc) · 6.49 KB
/
Copy pathmain.c
File metadata and controls
298 lines (256 loc) · 6.49 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
/* A shell program.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define close_pipe(fd) close(fd[0]); close(fd[1]);
const int BUF_SIZE = 1024;
const int MAX_TOKENS = 128;
const int MAX_CMDS = 16;
const int CMD_SIZE = 16;
void input(char* argv[], char buf[]);
void sigint_handler(int SIGNAL);
int partition_tokens(char *tokens[], char *cmds[MAX_CMDS][CMD_SIZE]);
void set_fd(char *cmd[]);
void filter_argv(char *argv[], char *cmd[]);
void run_cmds(int cmd_ind, char *cmds[MAX_CMDS][CMD_SIZE]);
int
main(void)
{
// Set signal handler for SIGINT
signal(SIGINT, sigint_handler);
while (1)
{
int num_cmds;
char buf[BUF_SIZE];
char *tokens[MAX_TOKENS];
char *cmds[MAX_CMDS][CMD_SIZE];
// Display shell prompt
write(1, "(ash) $ ", 8);
// Take user input
input(tokens, buf);
// Partition tokens into cmds array
num_cmds = partition_tokens(tokens, cmds);
if (num_cmds > 0)
{
pid_t pid;
if (!strcmp("exit", cmds[0][0]))
{
exit(0);
}
if ((pid = fork()) > 0)
{
wait(NULL);
}
else if (pid == 0) {
run_cmds(num_cmds - 1, cmds);
exit(0);
}
else if (pid == -1)
{
printf("Fork error!\n");
}
}
}
}
/* The child processes created by execvp will
have their default signal handlers and
will close on SIGINT. However, we need to
catch SIGINT from the top most parent
process shell for which we will set an empty
handler for SIGINT */
void
sigint_handler(int SIGNAL)
{
// Do nothing
}
/* Takes input from user and splits it in
tokens into argv. The last element in
argv will always be NULL. */
void
input(char* tokens[], char buf[])
{
buf[0] = '\0';
fgets(buf, BUF_SIZE, stdin);
tokens[0] = strtok(buf, " \n\0");
for (int i = 0; tokens[i] != NULL; ++i)
{
tokens[i + 1] = strtok(NULL, " \n\0");
}
}
/* Parition the array of tokens into NULL terminated
arrays of strings into the cmds array. The paritioning
is done using the delimiter pipe "|" and NULL.
The argument tokens must be a NULL terminated array
of strings.
The function returns the total number of cmds. */
int
partition_tokens(char *tokens[], char *cmds[MAX_CMDS][CMD_SIZE])
{
if (tokens[0] == NULL) return 0;
int num_cmds = 0;
int cmds_i = 0;
int col_i = 0;
int tok_i = 0;
while (1)
{
if (tokens[tok_i] == NULL)
{
cmds[cmds_i][col_i] = NULL;
++num_cmds;
break;
}
else if (!strcmp(tokens[tok_i], "|"))
{
cmds[cmds_i][col_i] = NULL;
++cmds_i;
++tok_i;
col_i = 0;
++num_cmds;
}
else {
cmds[cmds_i][col_i] = tokens[tok_i];
++col_i;
++tok_i;
}
}
return num_cmds;
}
/* Run all the commands in the cmds array and
pipe them sequentially. */
void
run_cmds(int cmd_ind, char *cmds[MAX_CMDS][CMD_SIZE])
{
char *argv[CMD_SIZE];
filter_argv(argv, cmds[cmd_ind]);
set_fd(cmds[cmd_ind]);
if (cmd_ind > 0)
{
pid_t pid;
int fd[2];
if (pipe(fd))
{
printf("Pipe error!\n");
}
else
{
if ((pid = fork()) > 0)
{
close(0);
dup(fd[0]);
close_pipe(fd);
execvp(argv[0], argv);
printf("%s: Command not found.\n", argv[0]);
}
else if (pid == 0)
{
close(1);
dup(fd[1]);
close_pipe(fd);
run_cmds(cmd_ind - 1, cmds);
}
else
{
printf("Fork error!\n");
}
}
}
else
{
execvp(argv[0], argv);
printf("%s: Command not found.\n", argv[0]);
}
}
/* Filter command and arguments from the cmd
array into argv array */
void
filter_argv(char *argv[], char *cmd[])
{
int argv_i = 0;
int cmd_i = 0;
while (1)
{
if (cmd[cmd_i] == NULL)
{
argv[argv_i] = NULL;
break;
}
else
{
if (!strcmp(cmd[cmd_i], ">") || !strcmp(cmd[cmd_i], ">>") || !strcmp(cmd[cmd_i], "<"))
{
cmd_i += 2;
}
else if (sizeof(cmd[cmd_i]) * sizeof(char) > 2 && *(cmd[cmd_i] + 1) == '>')
{
++cmd_i;
}
else
{
argv[argv_i++] = cmd[cmd_i++];
}
}
}
}
/* Set the file descriptors according to the
IO redirections specified in the cmd array.
The input is assumed to be valid. */
void
set_fd(char *cmd[])
{
for (int i = 0; cmd[i] != NULL; ++i)
{
int fd;
// command > filename
if (!strcmp(cmd[i], ">"))
{
fd = open(cmd[i + 1], O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
close(1);
dup(fd);
}
// command >> filename
else if (!strcmp(cmd[i], ">>"))
{
fd = open(cmd[i + 1], O_APPEND | O_RDWR, S_IRWXU);
if (fd < 1) fd = open(cmd[i + 1], O_CREAT | O_RDWR, S_IRWXU);
close(1);
dup(fd);
}
// command < filename
else if (!strcmp(cmd[i], "<"))
{
fd = open(cmd[i + 1], O_RDONLY | O_RDWR, S_IRWXU);
close(0);
dup(fd);
}
else if (sizeof(cmd[i]) * sizeof(char) > 2 && *(cmd[i] + 1) == '>')
{
// 2>&1
if (*(cmd[i] + 2) == '&' && *(cmd[i] + 3) == '1')
{
close(2);
dup(1);
}
// 1>filename
else if (*(cmd[i] + 0) == '1')
{
fd = open(cmd[i] + 2, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
close(1);
dup(fd);
}
// 2>filename
else if (*(cmd[i] + 0) == '2')
{
fd = open(cmd[i] + 2, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
close(2);
dup(fd);
}
}
}
}