-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecution.c
More file actions
356 lines (323 loc) · 11.4 KB
/
execution.c
File metadata and controls
356 lines (323 loc) · 11.4 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<stdbool.h>
#include <fcntl.h>
#include<sys/wait.h>
#define RESET "\033[0m"
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
#define YELLOW "\033[33m" /* Yellow */
#ifndef DEBUG
#define DEBUG 0
#endif
int saved_in, saved_out;
char* findPath(char* prog_name) {
bool isPathGiven = false;
if (prog_name[0] == '/') isPathGiven = true;
// Infos about the filePath, and stat_buf (using stat())
struct stat stat_buf;
char* filePath = NULL;
if (isPathGiven){
int pathSize = strlen(prog_name) + 1;
filePath = malloc(pathSize);
memcpy(filePath, prog_name, pathSize); // take program path from command
}
else
{
// look for program based on bare names from command
char *PATH[6] = {
"/usr/local/sbin/",
"/usr/local/bin/",
"/usr/sbin/",
"/usr/bin/",
"/sbin/",
"/bin/"
};
int path_count = 6;
char* tryPath;
int pathSize;
for (int i = 0; i < path_count; i++)
{
// concatenate PATH[i] and prog_name
pathSize = strlen(PATH[i]) + strlen(prog_name) + 1;
pathSize = ((pathSize + 7)/8) * 8;
//assign and try paths
tryPath = (char*) malloc(pathSize * sizeof(char));
strcpy(tryPath, PATH[i]);
strcat(tryPath, prog_name);
if (stat(tryPath, &stat_buf) == 0) // found program
{
if (DEBUG) {
printf(YELLOW "stat() found file %s at dir: %s" RESET "\n", prog_name, tryPath);
}
filePath = (char*) malloc(pathSize);
memcpy(filePath, tryPath, pathSize);
break; // stop searching
}
free(tryPath);
}
}
return filePath;
}
int execProgram(char*** commands, int* idx, int len, int* pd, int lastPipe){
// Create a child process
char** prog_args = commands[*idx];
int nfd;
// Implemented for single command so far
// TODO: split arguments corresponding each command and execute
if (strcmp(*prog_args, "pwd") == 0){
char buffer[512];
memset(buffer, 0, sizeof(buffer));
char* res = getcwd(buffer, sizeof(buffer));
if (res != NULL){
if (*idx == (len-1)){
if (dup2(saved_out, STDOUT_FILENO) < 0)
{
perror("Set stdout to terminal");
return -1;
}
}
else{
if(DEBUG) printf("Writing to pipleline %d \n", (*idx)*2 + 1);
nfd = dup2(pd[(*idx)*2 + 1], STDOUT_FILENO);
if (nfd == -1) {
perror(RED "ERROR: write to pipefailed" RESET "\n");
}
}
// Look for redirection
while (((*idx)+1 < len) && commands[(*idx)+1][0][0] != '|') {
if (commands[*(idx)+1][0] != NULL)
{
*idx = (*idx) + 1;
if (commands[*idx][0][0] == '<')
{
if (commands[*idx][1] == NULL) {
printf(RED "Invalid Redirection Syntax" RESET "\n");
return -1;
}
// Redirect input from a file
int fd = open(commands[*idx][1], O_RDONLY);
int nfd = dup2(fd, STDIN_FILENO);
if (nfd == -1) {
if (DEBUG) printf( RED "ERROR: Redirect input failed" RESET "\n");
return -1;
}
close(fd);
}
if (commands[*idx][0][0] == '>')
{
if (commands[*idx][1] == NULL) {
if (DEBUG) printf(RED "Invalid Redirection Syntax" RESET "\n");
return -1;
}
// Redirect output to a file
int fd = open(commands[*idx][1], O_WRONLY|O_CREAT|O_TRUNC, 0640);
int nfd = dup2(fd, STDOUT_FILENO);
if (nfd == -1) {
if (DEBUG) printf( RED "ERROR: Redirect output failed" RESET "\n");
return -1;
}
close(fd);
}
}
}
// add \n to end of char*
res[strlen(res)] = '\n';
int writeBytes = write(STDOUT_FILENO, res, strlen(res));
if (writeBytes == -1) {
if (DEBUG) printf(RED "Didn't write any byte!!!" RESET "\n");
return -1;
}
else if (DEBUG) printf(YELLOW "Wrote %d bytes!!" RESET "\n", writeBytes);
//memset(buffer,0, sizeof(buffer));
}
else {
printf("pwd failed\n");
return -1;
}
// reset stdin stdout to terminal and return
if (dup2(saved_out, STDOUT_FILENO) < 0)
{
perror("Set stdout to terminal");
return -1;
}
if (dup2(saved_in, STDOUT_FILENO) < 0){
perror("Set stdin to terminal");
return -1;
}
return 0;
}
if (strcmp(*prog_args, "cd") == 0){
if (*(*commands+1) == NULL) {
char* home = getenv("HOME");
if (chdir(home) != 0){
perror("cd");
return -1;
}
return 0;
}
if (*(*commands+2) != NULL) {
printf(RED "Invalid cd arguments" RESET "\n");
return -1;
}
if (chdir(*(*commands+1)) != 0){
perror("cd");
return -1;
}
return 0;
}
char* filePath = findPath(*prog_args);
int pid = 0;
// printf("len is %d and idx is %d \n", len, *idx);
if (filePath == NULL) {
// program not found
printf("Program %s not found!\n", *prog_args);
}
else
{
// Run program in filePath
pid = fork();
if (pid == -1) {
perror("fork failed");
return -1;
}
if (pid == 0) {// we are in the child process
//Get input from pipeline
//if not first command.
if (*idx != 0 && lastPipe >= 0){
if (DEBUG) printf(YELLOW "reading from pipeline[%d]" RESET "\n", (lastPipe));
nfd = dup2(pd[lastPipe], STDIN_FILENO);
if (nfd == -1){
perror(RED "ERROR: Read from pipeline failed" RESET "\n");
return -1;
}
}
//write output to pipeline
//if not last command
if (*idx < (len-1))
{
if (DEBUG) printf(YELLOW "writing to pipeline[%d]" RESET "\n", (*idx)*2 + 1);
nfd = dup2(pd[(*idx)*2 + 1], STDOUT_FILENO);
if (nfd == -1){
perror(RED "ERROR: Write to pipeline failed" RESET "\n");
return -1;
}
}
//close all pipes;
for (int i = 0; i < 2*len; i++) close(pd[i]);
// Look for redirection
while (((*idx)+1 < len) && commands[(*idx)+1][0][0] != '|') {
if (commands[*(idx)+1][0] != NULL)
{
*idx = (*idx) + 1;
if (commands[*idx][0][0] == '<')
{
if (commands[*idx][1] == NULL) {
printf(RED "Invalid Redirection Syntax" RESET "\n");
return -1;
}
// Redirect input from a file
int fd = open(commands[*idx][1], O_RDONLY);
int nfd = dup2(fd, STDIN_FILENO);
if (nfd == -1) {
if (DEBUG) printf( RED "ERROR: Redirect input failed" RESET "\n");
return -1;
}
close(fd);
}
if (commands[*idx][0][0] == '>')
{
if (commands[*idx][1] == NULL) {
if (DEBUG) printf(RED "Invalid Redirection Syntax" RESET "\n");
return -1;
}
// Redirect output to a file
int fd = open(commands[*idx][1], O_WRONLY|O_CREAT|O_TRUNC, 0640);
int nfd = dup2(fd, STDOUT_FILENO);
if (nfd == -1) {
if (DEBUG) printf( RED "ERROR: Redirect output failed" RESET "\n");
return -1;
}
close(fd);
}
}
}
// Execute program
if (execv(filePath, prog_args) < 0){
perror("execv failed");
return -1;
}
// if we reach here, something went wrong
// return error flag
return -1;
}
}
free(filePath);
return pid;
}
int execute(char ***commands, int len) {
if (commands == NULL) return EXIT_SUCCESS;
// Save initial stdin stdout
saved_in = dup(STDIN_FILENO);
saved_out = dup(STDOUT_FILENO);
int* pids = malloc(sizeof(int) * 4);
int pids_count = 0, pids_size = 4;
int command_iterator = 0;
int pd[2*len];
for (int i = 0; i < len;i++){
if (pipe(pd + i*2) == -1){
perror("pipe");
return -1;
}
}
int pid;
int lastWritePipe = -1;
while (command_iterator < len){
if (commands[command_iterator][0][0] != '<' && \
commands[command_iterator][0][0] != '>' && \
commands[command_iterator][0][0] != '|')
{
pid = execProgram(commands, &command_iterator, len, pd, lastWritePipe);
if (pid == 0) lastWritePipe = command_iterator * 2;
if (pid > 0){
if (pids_count + 1> pids_size){
pids_size *= 2;
pids = realloc(pids, pids_size);
}
lastWritePipe = command_iterator * 2;
if (DEBUG) printf("lastWritePipe value %d\n", lastWritePipe);
pids[pids_count++] = pid;
}
}
command_iterator++;
}
//close all pipes;
for (int i = 0; i < 2 * len; i++)
close(pd[i]);
if (DEBUG) printf(YELLOW "### Number of process is %d" RESET "\n", pids_count);
int clid, wstatus;
// wait all processes
for (int i = 0; i < pids_count; i++)
{
clid = wait(&wstatus); // wait for child to finish
if (clid == -1) // wait failed
{
perror("wait failed");
return -1;
}
if (WIFEXITED(wstatus)){
// child exited normally
if (DEBUG) printf("\nchild exited with status %d\n", WEXITSTATUS(wstatus));
}else{
if (DEBUG) printf("\nexited abnormally\n");
return -1;
}
}
free(pids);
close(saved_in);
close(saved_out);
return EXIT_SUCCESS;
}