-
Notifications
You must be signed in to change notification settings - Fork 0
/
w_run_command.c
50 lines (40 loc) · 1003 Bytes
/
w_run_command.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
#include "w_run_command.h"
#include "w_redirection.h"
int
runCmd(FILE* ofp, char** tokens, int* statLoc) {
/**
* FOLLOWING CODE SEGMENT WAS MODIFIED FROM THE ORIGINAL
* pipeToMore.c FROM THE COURSE EXAMPLES.
*/
int pipefds[2];
int pid;
int i;
pid_t child = -1;
int exitingPID = -123;
char** globTokens = NULL;
// create child process
if ((pid = fork()) < 0) {
perror("fork 1");
_exit(1);
}
// child process
if (pid == 0) {
// check if there are ">" or "<" characters, if so do redirection
if (redirectTree(ofp, tokens)) {
_exit(1);
}
// glob
globTokens = tokenGlob(tokens);
// replce the process image with the command specified
execvp(tokens[0], globTokens);
for (int i = 0; globTokens[i] != NULL; i++) {
free(globTokens[i]);
}
free(globTokens);
fprintf(stderr, "%s: command not found\n", tokens[0]);
//perror("exec 1");
_exit(1);
}
exitingPID = wait(statLoc);
return exitingPID;
}