14#include "../utils/utils.h"
16void dispatch(
char **paths,
char *args,
char **envp,
int *in_out);
17int wait_all(
int *pids,
int n,
int override);
19static int child1(
char ***str_arrs,
int *pair,
int *in_out)
25 return ((
int)
err(
"fork()", 0));
28 if (close(pair[0]) == -1)
29 return ((
int)
err(
"close()", (
void *) 1));
30 dispatch(str_arrs[0], str_arrs[1][0], str_arrs[2],
31 (
int []){in_out[0], pair[1]});
36static int child2(
char ***str_arrs,
int *pair,
int *in_out)
42 return ((
int)
err(
"fork()", 0));
44 dispatch(str_arrs[0], str_arrs[1][1], str_arrs[2],
45 (
int []){pair[0], in_out[1]});
49int dispatcher(
char **paths,
char **argv,
char **envp,
int *in_out)
55 return ((
int)
err(
"pipe()", (
void *) 1));
56 pids[0] = child1((
char **[]){paths, argv, envp}, pair, in_out);
59 if (in_out[0] != -1 && close(in_out[0]) == -1)
60 return (
wait_all(pids, 1, (
int)
err(
"close()", (
void *) 1)));
61 if (close(pair[1]) == -1)
62 return (
wait_all(pids, 1, (
int)
err(
"close()", (
void *) 1)));
63 pids[1] = child2((
char **[]){paths, argv, envp}, pair, in_out);
66 if (close(pair[0]) == -1)
67 return (
wait_all(pids, 2, (
int)
err(
"close()", (
void *) 1)));
68 if (in_out[1] != -1 && close(in_out[1]) == -1)
69 return (
wait_all(pids, 2, (
int)
err(
"close()", (
void *) 1)));
void dispatch(char **paths, char *args, char **envp, int *in_out)
Dispatch child process with argument string args, environment envp, and stdin / stdout in_out[0] / in...
int wait_all(int *pids, int n, int override)
Wait for first n processes in pids to change state and return the last process's exit code,...
int dispatcher(int n, char ***str_arrs, int *in_out)
Dispatch n child processes with argument strings str_arrs[1][i], environment pointer str_arrs[2],...
void * err(const char *str, void *retval)
Wrapper around perror() that always returns retval.