-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.c
159 lines (147 loc) · 4.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
#include "exec.h"
void cmd_exec(token_list **t, int *status, int *conv_desc) {
int child;
if ((child = fork()) < 0) {
fatal_error(PROC);
} else if (child == 0) {
if (isbgcmd(*t)) {
int null_desc;
if ((null_desc = open("/dev/null", O_RDONLY)) != -1) {
dup2(null_desc, 0);
close(null_desc);
signal(SIGINT, SIG_IGN);
} else {
fprintf(stderr, "Error... so sad........\n");
*status = -11;
return;
}
} else {
signal(SIGINT, SIG_DFL);
}
int exec_status;
conv_exec(t, status, conv_desc, &exec_status);
// log_msg(((*t)->token));
if (strcmp(";", (*t)->token) == 0 || strcmp("&", (*t)->token) == 0) {
(*t) = (*t)->next;
exit(0);
} else if (strcmp("||", (*t)->token) == 0) {
if (!WIFEXITED(exec_status) || WEXITSTATUS(exec_status) != 0) {
(*t) = (*t)->next;
shell_cmd(t, status);
exit(0);
} else {
next_cmd(t);
shell_cmd(t, status);
exit(0);
}
} else if (strcmp("&&", (*t)->token) == 0) {
if (!WIFEXITED(exec_status) || WEXITSTATUS(exec_status) != 0) {
next_cmd(t);
shell_cmd(t, status);
exit(0);
} else {
(*t) = (*t)->next;
shell_cmd(t, status);
exit(0);
}
}
} else {
int child_status;
if (!isbgcmd(*t))
waitpid(child, &child_status, 0);
next_cmd(t);
// if (*t)
// log_msg((*t)->token);
}
}
void conv_exec(token_list **t, int *status, int *conv_desc, int *exec_status) {
if (!(*t) || *status != 0) {
*status = -9;
return;
}
simple_cmd(t, status, conv_desc, exec_status);
if ((*t)) {
if (strcmp((*t)->token, "|") == 0) {
(*t) = (*t)->next;
conv_exec(t, status, conv_desc, exec_status);
}
}
}
void simple_cmd(token_list **t, int *status, int *conv_desc, int *exec_status) {
if (!(*t) || *status != 0) {
*status = -10;
return;
}
if (strcmp((*t)->token, ">") == 0 || strcmp((*t)->token, "<") == 0 || strcmp((*t)->token, ">>") == 0) {
return;
}
char *command = (*t)->token;
int size = 1, argc = 0;
char **argv = malloc(sizeof(char*) * size);
if (!argv)
fatal_error(HEAP_OVERFLOW);
while (!iscmdtoken(*t)) {
if (argc == size - 1) {
argv = realloc(argv, sizeof(char*) * (size *= 2));
if (!argv)
fatal_error(HEAP_OVERFLOW);
}
argv[argc] = (*t)->token;
(*t) = (*t)->next;
argc++;
}
argv[argc] = NULL;
simple_cmd_exec(t, command, argv, conv_desc, status, exec_status);
free(argv);
}
void simple_cmd_exec(token_list **t, char *command, char **argv, int *conv_desc, int *status, int *exec_status) {
if (strcmp("cd", command) == 0) {
if (argv[1] == NULL)
argv[1] = getenv("HOME");
if (chdir(argv[1]) != 0) {
fprintf(stderr, "%s: no such file or directory\n", argv[1]);
}
return;
}
int child;
if (pipe(conv_desc) < 0)
fatal_error(PIPE_ERR);
if ((child = fork()) < 0) {
fatal_error(PROC);
} else if (child == 0) {
if (*t && strcmp((*t)->token, "|") == 0)
dup2(conv_desc[1], 1);
close(conv_desc[0]);
close(conv_desc[1]);
if (execvp(command, argv) == -1) {
fprintf(stderr, "%s: command not found\n", command);
*exec_status = -1;
}
exit(1);
} else {
dup2(conv_desc[0], 0);
close(conv_desc[0]);
close(conv_desc[1]);
waitpid(child, exec_status, 0);
return;
}
}
void next_cmd(token_list **t) {
int brackets = 0;
while (*t) {
if (strcmp((*t)->token, "(") == 0)
brackets++;
if (strcmp((*t)->token, ")") == 0)
brackets--;
if (brackets == 0 && strcmp((*t)->token, "&") == 0) {
// (*t) = (*t)->next;
return;
}
if ((brackets == 0 && strcmp((*t)->token, ";") == 0) || brackets < 0) {
// (*t) = (*t)->next;
return;
}
(*t) = (*t)->next;
}
(*t) = new_token(";");
}