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.

40 lines
736 B

5 years ago
#include "run.h"
#include <sys/wait.h>
5 years ago
struct Job {
bool done;
int ret_val;
char **argv;
int argc;
};
5 years ago
/* 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.
*/
5 years ago
int execute(int in_FD, int out_FD, char *args[], pid_t* child_ID) {
5 years ago
int err = fork();
if (err == -1) {
5 years ago
return ER_FAIL_TO_FORK;
5 years ago
}
if (err) { // parent
5 years ago
*child_ID = err;
5 years ago
return ER_SUCCESS;
5 years ago
} else { // child
dup2(in_FD, 0);
dup2(out_FD, 1);
5 years ago
err = execvp(args[0], args);
5 years ago
if (err) {
perror("Bad");
}
5 years ago
}
5 years ago
return ER_SUCCESS;
5 years ago
}