22#include "../../mandatory/utils/utils.h"
24void dispatch(
char **paths,
char *args,
char **envp,
int *in_out);
25char **args_split(
char const *s);
26int wait_all(
int *pids,
int n,
int override);
28static int piping(
int i,
int n,
int *pair,
int **in_out__last_out)
31 *in_out__last_out[1] = in_out__last_out[0][0];
33 *in_out__last_out[1] = pair[0];
35 pair[1] = in_out__last_out[0][1];
36 else if (pipe(pair) == -1)
37 return ((
int)
err(
"pipe()", (
void *) 1));
41static int _fork(
int *i_n,
int **pids_pair,
int last_out,
char ***str_arrs)
47 return ((
int)
err(
"fork()", 0));
51 if (i_n[0] != (i_n[1] - 1) && pids_pair[1][0] != -1
52 && close(pids_pair[1][0]) == -1)
55 exit((
int)
err(
"close()", (
void *) 1));
57 dispatch(str_arrs[0], str_arrs[1][i_n[0]], str_arrs[2],
58 (
int []){last_out, pids_pair[1][1]});
63static int _wait_all(
int *pids,
int n,
int override)
67 status =
wait_all(pids, n,
override);
92 pids = malloc(
sizeof(
int) * n);
94 return ((
int)
err(
"malloc()", (
void *) 1));
98 if (piping(i, n, pair, (
int *[]){in_out, &last_out}))
100 pids[i] = _fork((
int []){i, n}, (
int *[]){pids, pair},
102 if (last_out != -1 && close(last_out) == -1)
103 return ((
int)
err(
"close()", (
void *) 1));
104 if (close(pair[1]) == -1)
105 return ((
int)
err(
"close()", (
void *) 1));
110 return (_wait_all(pids, i, !last_out));
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.