[ADD] multiple pipe

This commit is contained in:
joris schaft 2024-05-13 14:10:41 +02:00
parent 149bafef88
commit 9d70f4ccb2
4 changed files with 72 additions and 17 deletions

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* error.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 15:51:56 by mcolonna #+# #+# */
/* Updated: 2024/04/25 13:41:57 by mcolonna ### ########.fr */
/* Updated: 2024/05/13 09:39:38 by jschaft ### ########.fr */
/* */
/* ************************************************************************** */
@ -28,3 +28,10 @@ void fatal_error(const char *msg)
print_line(err_remember, 2, msg);
exit(1);
}
// Error creating pipes in exec_command
int exec_error(void)
{
perror("Error: Unable to create pipes\n");
return (10);
}

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 13:48:00 by jschaft #+# #+# */
/* Updated: 2024/04/26 12:31:49 by mcolonna ### ########.fr */
/* Updated: 2024/05/13 10:17:48 by jschaft ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,29 +27,43 @@ static void free_tab(char **tab)
}
// TODO is t_call.argv ended by NULL?
static void fork_exec(const char *path, t_call call, const int inout[2],
char *const envp[])
static void fork_exec(const char *path, t_call call, t_pipes in, t_pipes out,
char *const envp[])
{
dup2(inout[0], 0);
dup2(inout[1], 1);
if (in.i != 0)
dup2(in.pipe[1], 0);
else
dup2(in.inout, 0);
if (out.inout == 0)
dup2(out.pipe[0], 1);
else
dup2(out.inout, 1);
execve(path, call.argv, envp);
}
static int execute_call(t_memclass mc, t_call call, const int inout[2],
char *const envp[])
static int execute_call(t_memclass mc, t_command cmd, t_pipes in, t_pipes out,
char *const envp[])
{
char **const path = get_path(envp);
pid_t pid;
const char *program_path;
int status;
int i;
program_path = search_path(mc, (const char **)path, call.program);
i = in.i;
program_path = search_path(mc, (const char **)path, cmd.calls[i].program);
if (i == 0)
in.inout = cmd.input_fd;
if (out.inout == -1)
out.inout = cmd.output_fd;
if (!program_path)
return (127);
pid = fork();
if (pid < 0)
return (1);
if (pid == 0)
fork_exec(program_path, call, inout, envp);
fork_exec(program_path, cmd.calls[i], in, out, envp);
waitpid(pid, &status, 0);
free_tab(path);
return (0);
}
@ -57,7 +71,30 @@ static int execute_call(t_memclass mc, t_call call, const int inout[2],
// TODO works with only one call (no pipe).
int execute_command(t_memclass mc, t_command command, char *const envp[])
{
const int inout[] = {0, 1};
t_pipes *pipes;
int i;
return (execute_call(mc, command.calls[0], inout, envp));
i = 0;
while (command.calls[i].program != NULL)
i++;
pipes = malloc(sizeof(t_pipes) * i + 1);
i = 0;
while (command.calls[i].program != NULL)
{
if (pipe(pipes[i].pipe) < 0)
return (exec_error());
pipes[i].inout = 0;
i++;
}
pipes[i].inout = -1;
i = 0;
while (command.calls[i].program != NULL)
{
pipes[i].i = i;
pipes[i + 1].i = i + 1;
execute_call(mc, command, pipes[i], pipes[i + 1], envp);
i++;
}
free(pipes);
return (0);
}

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:33:45 by mcolonna #+# #+# */
/* Updated: 2024/04/26 12:24:58 by mcolonna ### ########.fr */
/* Updated: 2024/04/26 14:06:48 by jschaft ### ########.fr */
/* */
/* ************************************************************************** */