From 9d70f4ccb2a5784bd494dbc50d17785d503063b6 Mon Sep 17 00:00:00 2001 From: joris schaft Date: Mon, 13 May 2024 14:10:41 +0200 Subject: [PATCH] [ADD] multiple pipe --- include/include.h | 15 ++++++++++-- src/error.c | 11 +++++++-- src/exec_command.c | 59 +++++++++++++++++++++++++++++++++++++--------- src/main.c | 4 ++-- 4 files changed, 72 insertions(+), 17 deletions(-) diff --git a/include/include.h b/include/include.h index c17a69f..ceff3ef 100644 --- a/include/include.h +++ b/include/include.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* include.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: mcolonna +#+ +:+ +#+ */ +/* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */ -/* Updated: 2024/04/26 12:33:20 by mcolonna ### ########.fr */ +/* Updated: 2024/05/13 10:22:54 by jschaft ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,7 @@ # include # include # include +# include # include # include # include @@ -51,6 +52,14 @@ typedef struct s_command int output_fd; // fd to use with '>' redirection (1 by default) } t_command; +// To make a list of pipe when needed +typedef struct s_pipes +{ + int pipe[2]; // simple pipe + int i; //index of the pipe + int inout; //in case of first or last pipe +} t_pipes; + // Return the t_command representing the command given by the user. // If error, return a t_command wth the value .error = true. t_command parse_command(const t_memclass mc, const char *command); @@ -70,6 +79,8 @@ void minishell_error(const char *msg); // Call to write the error and exit the program. void fatal_error(const char *msg); +// Call to write the error and return in exec_command +int exec_error(void); ///// PATH ///// // Get the PATH values. diff --git a/src/error.c b/src/error.c index 4316050..ea89066 100644 --- a/src/error.c +++ b/src/error.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* error.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: mcolonna +#+ +:+ +#+ */ +/* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); +} \ No newline at end of file diff --git a/src/exec_command.c b/src/exec_command.c index 50c83ca..d431226 100644 --- a/src/exec_command.c +++ b/src/exec_command.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } diff --git a/src/main.c b/src/main.c index 0b5483d..a4dd0cc 100644 --- a/src/main.c +++ b/src/main.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* main.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: mcolonna +#+ +:+ +#+ */ +/* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */