-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.c
268 lines (250 loc) · 5.46 KB
/
exec.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
#include "list.h"
#include "tree.h"
#include "exec.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <setjmp.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
int is_com(cmd_tree t)
{
char *s;
s = t -> argv[0];
if (!strcmp(s, "cd") || !strcmp(s, "clear")) {
return 1;
}
return 0;
}
int exec_cd(cmd_tree t)
{
if (t -> argv[1] == NULL) {
chdir(getenv("HOME"));
return 0;
}
if (chdir(t -> argv[1]) == -1) {
fprintf(stderr, "cd: %s: no shuch file or directory\n", t -> argv[1]);
return 1;
}
return 0;
}
int exec_clear()
{
write(1, "\033[2J\033[H", sizeof("\033[2J\033[H"));
return 0;
}
int sh_com(cmd_tree t)
{
char *s;
s = t -> argv[0];
if (!strcmp(s, "cd")) {
return exec_cd(t);
}
if (!strcmp(s, "clear")) {
return exec_clear();
}
return 1;
}
void chng_iofiles(int in, int out, cmd_tree t)
{
int fd;
if (in != -1) {
dup2(in, 0);
close(in);
}
if (out != -1) {
dup2(out, 1);
close(out);
}
if (t -> infile != NULL) {
if ((fd = open(t -> infile, O_RDONLY)) == -1) {
printf("\x1B[31m");
perror("open");
printf("\x1B[0m");
exit(1);
}
dup2(fd, 0);
close(fd);
}
if (t -> outfile != NULL) {
fd = open(t -> outfile, O_WRONLY | O_CREAT | (t -> append ? O_APPEND : O_TRUNC), 0666);
if (fd == -1) {
printf("\x1B[31m");
perror("open");
printf("\x1B[0m");
exit(1);
}
dup2(fd, 1);
close(fd);
}
}
void add_elem (intlist **f, int pid) {
intlist *cur;
if ((*f) == NULL) {
*f = (intlist *) malloc(sizeof(intlist));
cur = *f;
}
else {
cur = *f;
while (cur -> next != NULL) {
cur = cur -> next;
}
cur -> next = (intlist *) malloc(sizeof(intlist));
cur = cur -> next;
}
cur -> pid = pid;
cur -> next = NULL;
}
void clear_intlist(intlist *f) {
intlist *cur;
cur = f;
while (cur != NULL) {
cur = cur -> next;
free(f);
f = cur;
}
}
int exec_com_sh(cmd_tree t)
{
return exec_com_list(t, 0);
}
int exec_com_list(cmd_tree t, int p)
{
int ret, to_do;
ret = exec_conv(t, p);
while (t -> next != NULL) {
to_do = 1;
if (t -> nxt == AND && ret) {
to_do = 0;
}
else if (t -> nxt == OR && !ret) {
to_do = 0;
}
t = t -> next;
if (to_do) {
ret = exec_conv(t, p);
}
}
return ret;
}
void print_intlist(intlist *f) {
while(f != NULL) {
fprintf(stderr, "%d\n", f -> pid);
f = f -> next;
}
}
int exec_conv(cmd_tree t, int p)
{
int ret, in, out, next_in, i = 0,status;
int fd[2];
intlist *frgrnd = NULL;
if (t -> pipe == NULL) {
return exec_command(t, -1, -1, -1, p, &frgrnd);
}
next_in = -1;
while (t -> pipe != NULL) {
i++;
pipe(fd);
in = next_in;
next_in = fd[0];
out = fd[1];
ret = exec_command(t, in, out, next_in, 1, &frgrnd);
close(in);
close(out);
t = t -> pipe;
}
ret = exec_command(t, next_in, -1, -1, 1, &frgrnd);
close(next_in);
while(frgrnd != NULL) {
int s = waitpid(frgrnd -> pid, &status, 0);
frgrnd = frgrnd -> next;
}
clear_intlist(frgrnd);
return ret;
}
int exec_command(cmd_tree t, int in, int out, int next_in, int p, intlist **pfrgrnd)
{
int status, pid;
if (t -> subcmd == NULL) {
return exec_simple_com(t, in, out, next_in, p, pfrgrnd);
}
if ((pid = fork()) == 0) {
clear_intlist(*pfrgrnd);
chng_iofiles(in, out, t);
if (next_in != -1) {
close(next_in);
}
exec_com_list(t->subcmd,0);
exit(0);
}
else {
if (t->bg) {
add_elem(&bckgrnd, pid);
waitpid(pid, &status, WNOHANG);
if (WIFEXITED(status)) {
return WEXITSTATUS(status);
}
return -1;
}
waitpid(pid,&status,0);
return WEXITSTATUS(status);
}
}
int exec_simple_com(cmd_tree t, int in, int out, int next_in, int p, intlist **pfrgrnd)
{
int status, pid;
if (is_com(t)) {
return sh_com(t);
}
if ((pid = fork()) == 0) {
signal(SIGINT, oldHandler);
chng_iofiles(in, out, t);
close(next_in);
execvp(t -> argv[0], t -> argv);
printf("\x1B[31m");
perror(t -> argv[0]);
printf("\x1B[0m");
exit(1);
}
if (t -> bg) {
waitpid(pid, &status, WNOHANG);
add_elem(&bckgrnd, pid);
zombie++;
}
else if (p) {
add_elem(pfrgrnd, pid);
return 0;
}
else {
waitpid(pid, &status, 0);
}
if (WIFEXITED(status)) {
return WEXITSTATUS(status);
}
return 1;
}
void clear_zombie(intlist *b)
{
intlist *p = b;
int pid, status;
b = b -> next;
while (b != NULL) {
pid = waitpid(b -> pid, &status, WNOHANG);
if (pid != 0) {
p->next = b->next;
free(b);
b = p ->next;
}
else {
p = b;
b = b -> next;
}
}
}
void exec_tree(cmd_tree tr)
{
exec_com_sh(tr);
}