-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.c
142 lines (117 loc) · 3.51 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
#include "exec.h"
#include "cmd_utils.h"
bool bg_process = false;
void fork_process(char* command){
int ret, wstatus, w;
ret = fork();
char** args = malloc(sizeof(command)*8);
switch (ret) {
case -1:
printf("Fork failed. Error code: %d\n", errno);
exit(1);
case 0:
exec_process(get_args(command));
default:
args = get_args(command);
if(!bg_process){
do {
w = waitpid(ret, &wstatus, WUNTRACED | WCONTINUED);
if (w == -1) {
perror("waitpid");
exit(EXIT_FAILURE);
}
if (WIFEXITED(wstatus)) {
printf("\n");
} else if (WIFSIGNALED(wstatus)) {
printf("killed by signal %d\n", WTERMSIG(wstatus));
} else if (WIFSTOPPED(wstatus)) {
printf("stopped by signal %d\n", WSTOPSIG(wstatus));
} else if (WIFCONTINUED(wstatus)) {
printf("continued\n");
}
if(WEXITSTATUS(wstatus)==255){
printf("invalid command.\n");
print_help();
}
} while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus));
}else{
printf("[1] %d\n", ret);
}
free(args);
break;
}
}
void exec_process(char** args){
if(execvp(args[0], args)!=-1){
}else{
free(args);
exit(-1);
}
free(args); //Teoricamente, esta linea nunca se ejecutaria. No estoy seguro de que se libere args.
}
char** get_args(char* command){
char **args;
args=(char**)malloc(0);
int i=0;
while(command!=NULL){
args = (char **) realloc(args, sizeof(command)); //Array size gets modified upon command size
if(args == NULL){
printf("error trying to reallocate memory\n");
free(args);
exit(1);
}
args[i]=command; //First element must be, by convention, the program name
command = strtok(NULL, " ");
i++;
}
args[i]=NULL; //Last element must be NULL
if(strcmp(args[i-1], "&") == 0){
bg_process = true;
}else{
bg_process = false;
}
return args;
}
int mypipe(char* command){
int fd[2]; //Declare file descriptors array
command = strtok(command, "|");
//Open up a pipe
if(pipe(fd) == -1){
printf("%s\n", strerror(errno));
return 1; //Error
}
//First fork, for the stdout
int pid1 = fork();
if(pid1 < 0){
printf("%s\n", strerror(errno));
return 1; //Error
}
if(pid1 == 0){
//We are in child process No.1
dup2(fd[1], STDOUT_FILENO);
close(fd[0]);
close(fd[1]);
command = strtok(command, " ");
exec_process(get_args(command));
}
//ping google.com -c 4 | grep "rtt"
//Second fork, for the stdin
int pid2 = fork();
if(pid2 < 0){
printf("%s\n", strerror(errno));
}
command = strtok(NULL, "\0");
if(pid2 == 0){
//Child process No.2
dup2(fd[0], STDIN_FILENO);
close(fd[0]);
close(fd[1]);
command = strtok(command+1, " ");
exec_process(get_args(command));
}
close(fd[0]);
close(fd[1]);
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
return 0;
}