-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyshell.c
More file actions
133 lines (113 loc) · 3.36 KB
/
myshell.c
File metadata and controls
133 lines (113 loc) · 3.36 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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#define debug(S, ...) {printf("????? ");printf(S __VA_OPT__(,) __VA_ARGS__);printf("\n");}
#define return_err(ERR) if (ERR != -1) return ERR;
typedef enum ExecutionMode {
Exit = 0,
WriteToFile = 1,
AppendToFile = 2,
Normal = 3,
Pipe,
} ExecutionMode;
#define COMMAND_BUFFER_SIZE 256
#define WRITE_BUFFER_SIZE 1024
#define DIR_BUFFER_SIZE 256
#define WS " \n\r\t"
int exec_cmd(char **tokens, char *writeBuffer, int hidden) {
ExecutionMode mode = Normal;
int p[2], bufferCount, err;
FILE *fd;
char const fileModes[][3] = {"w+", "a+"};
char *output, **token = tokens;
while (*++token = strtok(NULL, WS)) {
if ((*token)[0] == '>') {
if ((*token)[1] == '>')
mode = AppendToFile;
else
mode = WriteToFile;
*token = NULL;
output = strtok(NULL, WS);
break;
} else if (!strcmp(*token, "|")) {
mode = Pipe;
*token = NULL;
break;
}
}
if (mode == Normal) {
if (!strcmp(tokens[0], "cd")) {
if (chdir(tokens[1])) {
printf("couldn't find a directory with the name \"%s\"\n", tokens[1]);
}
} else if (!fork()) {
execvp(tokens[0], tokens);
printf("%s was not recognized as a command\n", tokens[0]);
return 1;
} else if (!hidden) {
wait(0);
}
} else {
pipe(p);
if (!fork()) {
dup2(p[1], 1);
close(p[0]);
execvp(tokens[0], tokens);
printf("%s was not recognized as a command\n", tokens[0]);
return 2;
}
if (!fork()) {
dup2(p[0], 0);
close(p[1]);
if (mode == WriteToFile || mode == AppendToFile) {
if (!(fd = fopen(output, fileModes[mode - WriteToFile]))) {
printf("could not write in file %s\n", output);
return 3;
}
while (fgets(writeBuffer, WRITE_BUFFER_SIZE, stdin)) {
fprintf(fd, "%s", writeBuffer);
}
fclose(fd);
}
if (mode == Pipe) {
*tokens = strtok(NULL, WS);
err = exec_cmd(tokens, writeBuffer, hidden);
return_err(err);
}
return 0;
}
close(p[0]);
close(p[1]);
if (!hidden) {
wait(0);
wait(0);
}
}
return -1;
}
int main() {
int err, hidden;
size_t end;
char line[COMMAND_BUFFER_SIZE], *tokens[COMMAND_BUFFER_SIZE >> 1], writeBuffer[WRITE_BUFFER_SIZE + 1], dir[DIR_BUFFER_SIZE];
writeBuffer[WRITE_BUFFER_SIZE] = 0;
while (1) {
printf("%s => ", getcwd(dir, DIR_BUFFER_SIZE));
if (!fgets(line, COMMAND_BUFFER_SIZE, stdin)) {
printf("couldn't read line:\n");
return -1;
}
end = strlen(line) - 2;
if (line[end] == '&') {
hidden = 1;
line[end] = 0;
} else {
hidden = 0;
}
if (!(*tokens = strtok(line, WS)))
continue;
err = exec_cmd(tokens, writeBuffer, hidden);
return_err(err);
}
}