-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.c
More file actions
222 lines (189 loc) · 4.13 KB
/
executor.c
File metadata and controls
222 lines (189 loc) · 4.13 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "shell.h"
#include "node.h"
#include "executor.h"
char *search_path(char *file)
{
char *PATH = getenv("PATH");
char *p = PATH;
char *p2;
while(p && *p)
{
p2 = p;
while(*p2 && *p2 != ':')
{
p2++;
}
int plen = p2-p;
if(!plen)
{
plen = 1;
}
int alen = strlen(file);
char path[plen+1+alen+1];
strncpy(path, p, p2-p);
path[p2-p] = '\0';
if(p2[-1] != '/')
{
strcat(path, "/");
}
strcat(path, file);
struct stat st;
if(stat(path, &st) == 0)
{
if(!S_ISREG(st.st_mode))
{
errno = ENOENT;
p = p2;
if(*p2 == ':')
{
p++;
}
continue;
}
p = malloc(strlen(path)+1);
if(!p)
{
return NULL;
}
strcpy(p, path);
return p;
}
else /* file not found */
{
p = p2;
if(*p2 == ':')
{
p++;
}
}
}
errno = ENOENT;
return NULL;
}
int do_exec_cmd(int argc, char **argv)
{
if(strchr(argv[0], '/'))
{
execv(argv[0], argv);
}
else
{
char *path = search_path(argv[0]);
if(!path)
{
return 0;
}
execv(path, argv);
free(path);
}
return 0;
}
static inline void free_argv(int argc, char **argv)
{
if(!argc)
{
return;
}
while(argc--)
{
free(argv[argc]);
}
}
int do_simple_command(struct node_s *node)
{
if(!node)
{
return 0;
}
struct node_s *child = node->first_child;
if(!child)
{
return 0;
}
int argc = 0; /* arguments count */
int targc = 0; /* total alloc'd arguments count */
char **argv = NULL;
char *str;
while(child)
{
str = child->val.str;
/*perform word expansion */
struct word_s *w = word_expand(str);
/* word expansion failed */
if(!w)
{
child = child->next_sibling;
continue;
}
/* add the words to the arguments list */
struct word_s *w2 = w;
while(w2)
{
if(check_buffer_bounds(&argc, &targc, &argv))
{
str = malloc(strlen(w2->data)+1);
if(str)
{
strcpy(str, w2->data);
argv[argc++] = str;
}
}
w2 = w2->next;
}
/* free the memory used by the expanded words */
free_all_words(w);
/* check the next word */
child = child->next_sibling;
}
/* even if arc == 0, we need to alloc memory for argv */
if(check_buffer_bounds(&argc, &targc, &argv))
{
/* NULL-terminate the array */
argv[argc] = NULL;
}
int i = 0;
for( ; i < builtins_count; i++)
{
if(strcmp(argv[0], builtins[i].name) == 0)
{
builtins[i].func(argc, argv);
free_buffer(argc, argv);
return 1;
}
}
pid_t child_pid = 0;
if((child_pid = fork()) == 0)
{
do_exec_cmd(argc, argv);
fprintf(stderr, "error: failed to execute command: %s\n", strerror(errno));
if(errno == ENOEXEC)
{
exit(126);
}
else if(errno == ENOENT)
{
exit(127);
}
else
{
exit(EXIT_FAILURE);
}
}
else if(child_pid < 0)
{
fprintf(stderr, "error: failed to fork command: %s\n", strerror(errno));
free_buffer(argc, argv);
return 0;
}
int status = 0;
waitpid(child_pid, &status, 0);
free_buffer(argc, argv);
return 1;
}