-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutor.c
165 lines (137 loc) · 2.8 KB
/
executor.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
/*
@author: Huzaifa Naseer
*/
#include "executor.h"
#include "node.h"
#include "shell.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.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]);
}
}
/*It takes the AST and converts it to an argument list*/
int do_command(struct node_s *node) {
if (!node) {
return 0;
}
struct node_s *child = node->first_child;
if (!child) {
return 0;
}
int argc = 0;
long max_args = 255;
char *argv[max_args + 1]; /* keep 1 for the terminating NULL arg */
char *str;
while (child) {
str = child->val.str;
argv[argc] = malloc(strlen(str) + 1);
if (!argv[argc]) {
free_argv(argc, argv);
return 0;
}
strcpy(argv[argc], str);
if (++argc >= max_args) {
break;
}
child = child->next_sibling;
}
argv[argc] = NULL;
int i = 0;
for (; i < builtins_count; i++) {
if (!strcmp(argv[0], builtins[i].name)) {
builtins[i].func(argc, argv);
free_argv(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));
return 0;
}
int status = 0;
waitpid(child_pid, &status, 0);
free_argv(argc, argv);
return 1;
}