diff --git a/headers/minishell_prototypes.h b/headers/minishell_prototypes.h index 1d85c13..7d6c7d5 100644 --- a/headers/minishell_prototypes.h +++ b/headers/minishell_prototypes.h @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */ -/* Updated: 2021/12/20 17:49:56 by lperrey ### ########.fr */ +/* Updated: 2021/12/20 18:06:36 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,6 +63,7 @@ int builtin_echo(int argc, char *argv[], t_all *c); int exit_free(t_all *c, int exit_status); void free_pipeline(t_cmd **pipeline_ptr[]); void close_pipeline_fd(t_cmd *pipeline[]); +void close_cmd_fd(t_cmd *cmd); void close_stdio(void); typedef void (*t_free_f)(void *); // generic diff --git a/srcs/exec/pipeline.c b/srcs/exec/pipeline.c index 150a4ca..2a9ebcb 100644 --- a/srcs/exec/pipeline.c +++ b/srcs/exec/pipeline.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */ -/* Updated: 2021/12/16 15:55:36 by lperrey ### ########.fr */ +/* Updated: 2021/12/20 18:08:16 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -97,9 +97,9 @@ static pid_t pipeline_exec(t_cmd *pipeline[], t_all *c) if (ret != EXIT_SUCCESS) exit_free(c, ret); } + close_cmd_fd(pipeline[i]); i++; } - close_pipeline_fd(c->pipeline); i -= 1; if (pipeline[i]->error) set_last_exit_status(pipeline[i]->error); diff --git a/srcs/misc/free.c b/srcs/misc/free.c index c950522..7e743c5 100644 --- a/srcs/misc/free.c +++ b/srcs/misc/free.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/10 23:53:17 by lperrey #+# #+# */ -/* Updated: 2021/12/18 14:15:13 by lperrey ### ########.fr */ +/* Updated: 2021/12/20 18:06:15 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,22 +57,27 @@ void close_pipeline_fd(t_cmd *pipeline[]) i = 0; while (pipeline[i]) { - if (pipeline[i]->fd_in != STDIN_FILENO && pipeline[i]->fd_in > 0) - { - if (close(pipeline[i]->fd_in) == -1) - perror("close()"); - pipeline[i]->fd_in = 0; - } - if (pipeline[i]->fd_out != STDOUT_FILENO && pipeline[i]->fd_out > 0) - { - if (close(pipeline[i]->fd_out) == -1) - perror("close()"); - pipeline[i]->fd_out = 0; - } + close_cmd_fd(pipeline[i]); i++; } } +void close_cmd_fd(t_cmd *cmd) +{ + if (cmd->fd_in != STDIN_FILENO && cmd->fd_in > 0) + { + if (close(cmd->fd_in) == -1) + perror("close()"); + cmd->fd_in = 0; + } + if (cmd->fd_out != STDOUT_FILENO && cmd->fd_out > 0) + { + if (close(cmd->fd_out) == -1) + perror("close()"); + cmd->fd_out = 0; + } +} + void close_stdio(void) { if (close(STDIN_FILENO) == -1)