Repository navigation
Expand file tree
/
Copy pathshell.c
More file actions
344 lines (274 loc) · 8.68 KB
/
Copy pathshell.c
File metadata and controls
344 lines (274 loc) · 8.68 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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <linux/limits.h>
#include <fcntl.h>
#define MAX_COMMANDS 16
struct Command {
char* argv[64];
char* output_file;
char* input_file;
int append;
};
char* readInput() {
char* line = NULL;
size_t bufsize = 0;
ssize_t characters;
printf("myshell>");
fflush(stdout);
// getline allocates on heap so must free.
characters = getline(&line, &bufsize, stdin);
//EOF ctrl-d
if (characters == -1) { printf("\n"); return NULL; }
return line;
}
int isOperator(const char *token) {
if (token == NULL)
return 0;
return strcmp(token, "<") == 0 ||
strcmp(token, ">") == 0 ||
strcmp(token, ">>") == 0 ||
strcmp(token, "|") == 0;
}
int parse(char* line, struct Command* cmd) {
char* token = strtok(line, " \t\n");
int i = 0, cmd_i = 0;
while (token != NULL) {
//pipe
if (strcmp(token, "|") == 0) {
//if cmd is just "|"
if (i == 0) {printf("syntax error.\n"); return -1;}
cmd[cmd_i].argv[i] = NULL;
if(cmd_i >= MAX_COMMANDS-1) {printf("TOO MANY COMMANDS.\n"); return -1;}
cmd_i++;
i = 0;
token = strtok(NULL, " \t\n");
if (token == NULL) {
printf("syntax error: expected command.\n");
return -1;
}
if (isOperator(token)) {
printf("syntax error: expected command.\n");
return -1;
}
cmd[cmd_i].argv[i] = token;
i++;
}
//output redirection
else if (strcmp(token, ">") == 0) {
//multiple output redirections
if (cmd[cmd_i].output_file != NULL) {printf("multiple output redirection.\n"); return 1;}
token = strtok(NULL, " \t\n");
if (token == NULL) {
printf("syntax error: expected filename.\n");
return -1;
}
if (isOperator(token)) {
printf("syntax error: expected filename.\n");
return -1;
}
cmd[cmd_i].output_file = token;
printf("Output redirected to: %s\n", cmd[cmd_i].output_file);
}
//input redirection
else if(strcmp(token, "<") == 0) {
//multiple input redirections
if (cmd[cmd_i].input_file != NULL) {printf("multiple input redirection.\n"); return 1;}
token = strtok(NULL, " \t\n");
if (token == NULL) {
printf("syntax error: expected filename.\n");
return -1;
}
if (isOperator(token)) {
printf("syntax error: expected filename.\n");
return -1;
}
cmd[cmd_i].input_file = token;
printf("Input redirected to: %s\n", cmd[cmd_i].input_file);
}
//append redirection
else if (strcmp(token, ">>") == 0) {
//multiple output redirections
if (cmd[cmd_i].output_file != NULL) {printf("multiple output redirection.\n"); return 1;}
token = strtok(NULL, " \t\n");
if (token == NULL) {
printf("syntax error: expected filename.\n");
return -1;
}
if (isOperator(token)) {
printf("invalid syntax: expected filename.\n");
return -1;
}
cmd[cmd_i].output_file = token;
cmd[cmd_i].append = 1;
printf("Output redirected to: %s\n", cmd[cmd_i].output_file);
}
else {
cmd[cmd_i].argv[i] = token;
i++;
}
token = strtok(NULL, " \t\n");
}
cmd[cmd_i].argv[i] = NULL;
return cmd_i+1;
}
void writeToFile(struct Command* cmd) {
int fd;
if (cmd->append == 1)
fd = open(cmd->output_file, O_WRONLY | O_CREAT | O_APPEND, 0644);
else
fd = open(cmd->output_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1) {perror("open"); exit(1);}
if (dup2(fd, 1) == -1) {perror("dup2"); exit(1);}
if (close(fd) == -1) {perror("close"); exit(1);}
}
void readFromFile(struct Command* cmd) {
int fd = open(cmd->input_file, O_RDONLY);
if (fd == -1) {perror("open"); exit(1);}
if (dup2(fd, 0) == -1) {perror("dup2"); exit(1);}
if (close(fd) == -1) {perror("close"); exit(1);}
}
int isBuiltIn(struct Command* cmd) {
return strcmp(cmd->argv[0], "cd") == 0 || strcmp(cmd->argv[0], "pwd") == 0 || strcmp(cmd->argv[0], "exit") == 0;
}
int runsInParent(struct Command* cmd) {
return strcmp(cmd->argv[0], "cd") == 0 || strcmp(cmd->argv[0], "exit") == 0;
}
void runBuiltIn(struct Command* cmd) {
if (strcmp(cmd->argv[0], "exit") == 0) {
exit(EXIT_SUCCESS);
}
else if (strcmp(cmd->argv[0], "pwd") == 0) {
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) != NULL)
printf("%s\n", cwd);
else
perror("getcwd");
}
else if (strcmp(cmd->argv[0], "cd") == 0) {
const char* path;
if (cmd->argv[1] == NULL)
path = getenv("HOME");
else
path = cmd->argv[1];
if (path == NULL || chdir(path) != 0) {
perror("chdir");
}
}
else {
printf("command error.\n");
}
}
void execSingle(struct Command* cmd) {
// built-in running in parent donot implement redirection or
// piping since current such commands (cd, exit) donot require it.
if(isBuiltIn(cmd) && runsInParent(cmd)) {
runBuiltIn(cmd);
return;
}
pid_t pid = fork();
if (pid == 0) {
//child
if (cmd->input_file) readFromFile(cmd);
if (cmd->output_file) writeToFile(cmd);
//built-in that can runs in child
if (isBuiltIn(cmd)) {
runBuiltIn(cmd);
exit(EXIT_SUCCESS);
}
if (execvp(cmd->argv[0], cmd->argv) == -1) {
perror("execvp");
exit(1);
}
}
else if (pid > 0) {
//parent
waitpid(pid, 0, 0);
}
else {
perror("fork");
exit(EXIT_FAILURE);
}
}
void execPipe(struct Command *cmd, int ncmd) {
int prev_read = -1;
for (int i = 0; i < ncmd; i++) {
int fd[2];
// Every command except the last needs a new pipe.
if (i < ncmd - 1) {
if (pipe(fd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
}
pid_t pid = fork();
// CHILD
if (pid == 0) {
// stdin (input from prev pipe)
if (prev_read != -1) {
if (dup2(prev_read, STDIN_FILENO) == -1) {
perror("dup2");
exit(EXIT_FAILURE);
}
}
// stdout (output to next pipe)
if (i < ncmd - 1) {
if (dup2(fd[1], STDOUT_FILENO) == -1) {
perror("dup2");
exit(EXIT_FAILURE);
}
}
// input redirection
if (cmd[i].input_file)
readFromFile(&cmd[i]);
// output redirection
if (cmd[i].output_file)
writeToFile(&cmd[i]);
// Close unused descriptors.
if (prev_read != -1)
close(prev_read);
if (i < ncmd - 1) {
close(fd[0]);
close(fd[1]);
}
if (isBuiltIn(&cmd[i])) {
runBuiltIn(&cmd[i]);
exit(EXIT_SUCCESS);
}
execvp(cmd[i].argv[0], cmd[i].argv);
perror("execvp");
exit(EXIT_FAILURE);
}
// PARENT
if (prev_read != -1)
close(prev_read);
if (i < ncmd - 1) {
close(fd[1]); // parent doesn't write
prev_read = fd[0]; // save read end for next child
}
}
// Wait for all children.
for (int i = 0; i < ncmd; i++)
wait(NULL);
}
int main(void) {
do {
char* line = readInput();
//EOF
if (line == NULL) break;
struct Command cmd[MAX_COMMANDS] = {0}; //initialize to NULL/0
int ncmds = parse(line, cmd);
if (ncmds == -1) { free(line); continue; }
if (cmd[0].argv[0] == NULL) {free(line); continue;}
if (ncmds == 1)
execSingle(&cmd[0]);
else
execPipe(cmd, ncmds);
//allocated by getline
free(line);
} while (1);
return 0;
}