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
|
#include <assert.h>
#include <fcntl.h>
#include <limits.h>
#include <poll.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MAX_ARGS 20
#define IS_SPECIAL(_c) (';' == _c || '\n' == _c || '&' == _c || '|' == _c)
char *PATH = "/:/bin";
int can_open(char *f) {
int rc;
if (-1 == (rc = open(f, O_RDONLY, 0)))
return 0;
close(rc);
return 1;
}
int find_path(char *file, char *buf) {
if ('/' == *file || '.' == *file) { // Real path
if (!can_open(file)) {
return 0;
}
strcpy(buf, file);
return 1;
}
char *p = PATH;
for (;;) {
char *b = p;
for (; *p && ':' != *p; p++)
;
size_t l = p - b;
strlcpy(buf, b, l);
strcat(buf, "/");
strcat(buf, file);
if (can_open(buf)) {
return 1;
}
if (!*p)
break;
p++;
}
return 0;
}
int internal_exec(char *file, char **argv) {
char r_path[PATH_MAX];
if (!find_path(file, r_path))
return 0;
if (-1 == execv(r_path, argv)) {
perror("exec");
return 0;
}
return 1;
}
int execute_program(char *s, size_t l, int ignore_rc, int f_stdout,
int *new_out) {
if (!l)
return -1;
char c[l + 1];
memcpy(c, s, l);
c[l] = 0;
char *argv[MAX_ARGS];
int args = 0;
char *p = c;
for (size_t i = 0; i <= l; i++) {
if (' ' == c[i]) {
if (p == c + i) {
for (; ' ' == *p; p++, i++)
;
i--;
continue;
}
c[i] = '\0';
if (strlen(p) == 0)
break;
argv[args] = p;
args++;
p = c + i + 1;
} else if (i == l) {
if (strlen(p) == 0)
break;
argv[args] = p;
args++;
}
}
argv[args] = NULL;
int fd[2];
pipe(fd);
int pid = fork();
if (0 == pid) {
if (new_out)
dup2(fd[1], 1);
if (-1 != f_stdout)
dup2(f_stdout, 0);
close(fd[0]);
if (!internal_exec(argv[0], argv)) {
printf("exec failed\n");
exit(1);
}
}
close(fd[1]);
if (new_out)
*new_out = fd[0];
if (ignore_rc)
return 0;
int rc;
wait(&rc);
return rc;
}
int compute_expression(char *exp) {
char *n = exp;
int ignore_next = 0;
int rc;
int f_stdout = -1;
int new_out = f_stdout;
for (; *exp; exp++) {
if (!IS_SPECIAL(*exp)) {
continue;
}
if ('\n' == *exp)
break;
if (';' == *exp) {
if (!ignore_next) {
execute_program(n, exp - n, 0, f_stdout, NULL);
}
n = exp + 1;
continue;
}
if ('&' == *exp && '&' == *(exp + 1)) {
if (!ignore_next) {
rc = execute_program(n, exp - n, 0, f_stdout, NULL);
if (0 != rc)
ignore_next = 1;
}
n = exp + 2;
exp++;
continue;
} else if ('&' == *exp) {
if (!ignore_next) {
execute_program(n, exp - n, 1, f_stdout, NULL);
}
n = exp + 1;
continue;
}
if ('|' == *exp && '|' == *(exp + 1)) {
if (!ignore_next) {
rc = execute_program(n, exp - n, 0, f_stdout, NULL);
if (0 == rc)
ignore_next = 1;
}
n = exp + 2;
exp++;
continue;
} else if ('|' == *exp) {
if (!ignore_next) {
execute_program(n, exp - n, 1, f_stdout, &new_out);
f_stdout = new_out;
}
n = exp + 1;
continue;
}
}
if (!ignore_next)
rc = execute_program(n, exp - n, 0, f_stdout, NULL);
return rc;
}
char *get_line(void) {
char *str = malloc(1024);
char *p = str;
int rc;
for (;;) {
if (0 == (rc = read(0, p, 1))) {
continue;
}
if (0 > rc) {
perror("read");
continue;
}
if (8 == *p) {
if (p == str)
continue;
putchar(*p);
p--;
continue;
}
putchar(*p);
if ('\n' == *p) {
break;
}
p++;
}
p++;
*p = '\0';
return str;
}
int main(void) {
for (;;) {
printf("/ : ");
char *l = get_line();
compute_expression(l);
free(l);
}
return 0;
}
|