You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
736 B
39 lines
736 B
#include "run.h" |
|
#include <sys/wait.h> |
|
|
|
struct Job { |
|
bool done; |
|
int ret_val; |
|
char **argv; |
|
int argc; |
|
}; |
|
|
|
/* Fork and exec a command given in args taking input from in and sending |
|
* output to out. The arguments array must end in a NULL. |
|
* |
|
* Returns true on success or 1 on failure. |
|
*/ |
|
int execute(int in_FD, int out_FD, char *args[], pid_t* child_ID) { |
|
|
|
int err = fork(); |
|
|
|
if (err == -1) { |
|
return ER_FAIL_TO_FORK; |
|
} |
|
|
|
if (err) { // parent |
|
*child_ID = err; |
|
return ER_SUCCESS; |
|
} else { // child |
|
dup2(in_FD, 0); |
|
dup2(out_FD, 1); |
|
|
|
err = execvp(args[0], args); |
|
if (err) { |
|
perror("Bad"); |
|
} |
|
} |
|
|
|
return ER_SUCCESS; |
|
} |
|
|
|
|