From f67d334503a2a80576449e6956469f13d05cfeb4 Mon Sep 17 00:00:00 2001 From: mdev9 Date: Tue, 30 Apr 2024 14:05:25 +0200 Subject: [PATCH] fixed closing all pipe fds --- srcs/exec_bonus.c | 3 ++- srcs/minishell.h | 4 +++- srcs/path.c | 5 +++-- srcs/pipe.c | 23 +++++++++++++---------- srcs/utils.c | 12 +++++++++++- todo | 9 ++------- 6 files changed, 34 insertions(+), 22 deletions(-) diff --git a/srcs/exec_bonus.c b/srcs/exec_bonus.c index f493d1c..8e389bb 100755 --- a/srcs/exec_bonus.c +++ b/srcs/exec_bonus.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */ -/* Updated: 2024/04/29 22:06:15 by tomoron ### ########.fr */ +/* Updated: 2024/04/30 14:00:36 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -97,6 +97,7 @@ void end_execution(t_msh *msh, int cmd_count) status = 0; while (i < cmd_count) waitpid(msh->pids[i++], &status, 0); + close_all_pipes(msh, cmd_count, i); if (!g_return_code && WIFEXITED(status)) g_return_code = WEXITSTATUS(status); if (WIFSIGNALED(status)) diff --git a/srcs/minishell.h b/srcs/minishell.h index 21bdf3b..7b10b32 100755 --- a/srcs/minishell.h +++ b/srcs/minishell.h @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */ -/* Updated: 2024/04/29 21:50:52 by tomoron ### ########.fr */ +/* Updated: 2024/04/30 14:00:33 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -95,6 +95,7 @@ void redirect_input(t_msh *msh, int i, char **cmd_args); t_env *env_add_back(t_env *env, char *name, char *value); void print_syntax_error_bonus(t_cmd *cmd, t_cmd *cmds); int filename_corresponds(char *wildcard, char *value); +void close_all_pipes(t_msh *msh, int cmd_count, int i); t_token *parse_cmds_to_token(t_cmd *command, t_env *env); int ft_export(t_msh *msh, t_token *cmd, t_env *env); void print_env_declare(t_msh *msh, t_env *env_orig); @@ -126,6 +127,7 @@ void ft_exit(t_msh *msh, int exit_code); int get_parenthesis_cmd_len(char *cmd); char **split_paths_from_env(t_env *env); int add_return_code_to_str(char *res); +void close_pipe_fds(t_msh *msh, int i); void parse_var(t_msh *msh, char *line); int get_var_name_len(char *command); void handle_minishellrc(t_msh *msh); diff --git a/srcs/path.c b/srcs/path.c index f801f70..d82e9d9 100755 --- a/srcs/path.c +++ b/srcs/path.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/21 21:47:15 by marde-vr #+# #+# */ -/* Updated: 2024/04/24 20:37:41 by marde-vr ### ########.fr */ +/* Updated: 2024/04/30 14:02:25 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -60,7 +60,8 @@ void get_path(t_msh *msh, int *found) char **paths; paths = split_paths_from_env(msh->env); - if (!paths || !*(msh->tokens->value)) + if (!paths || !*(msh->tokens->value) + || ft_str_is_only_char(msh->tokens->value, '.')) { free_paths(paths); return ; diff --git a/srcs/pipe.c b/srcs/pipe.c index d68fefe..3194a1c 100755 --- a/srcs/pipe.c +++ b/srcs/pipe.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */ -/* Updated: 2024/04/26 14:43:52 by tomoron ### ########.fr */ +/* Updated: 2024/04/30 13:56:03 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,17 +14,20 @@ void close_pipe_fds(t_msh *msh, int i) { - if (i != 0) + if (msh->fds) { - if (msh->fds[i - 1][0] > 2) - close(msh->fds[i - 1][0]); - if (msh->fds[i - 1][1] > 2) - close(msh->fds[i - 1][1]); + if (i != 0) + { + if (msh->fds[i - 1][0] > 2) + close(msh->fds[i - 1][0]); + if (msh->fds[i - 1][1] > 2) + close(msh->fds[i - 1][1]); + } + if (msh->fds[i] && msh->fds[i][0] > 2) + close(msh->fds[i][0]); + if (msh->fds[i] && msh->fds[i][1] > 2) + close(msh->fds[i][1]); } - if (msh->fds[i][0] > 2) - close(msh->fds[i][0]); - if (msh->fds[i][1] > 2) - close(msh->fds[i][1]); } void handle_parenthesis(t_msh *msh) diff --git a/srcs/utils.c b/srcs/utils.c index eafa557..1f1996c 100755 --- a/srcs/utils.c +++ b/srcs/utils.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:19:26 by marde-vr #+# #+# */ -/* Updated: 2024/04/29 13:00:22 by tomoron ### ########.fr */ +/* Updated: 2024/04/30 14:03:28 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -73,3 +73,13 @@ int file_access(t_msh *msh, int *found) } return (1); } + +void close_all_pipes(t_msh *msh, int cmd_count, int i) +{ + i = 0; + while (i < cmd_count) + { + close_pipe_fds(msh, i); + i++; + } +} diff --git a/todo b/todo index 9138274..6c48932 100644 --- a/todo +++ b/todo @@ -1,16 +1,11 @@ -- .. # (pas de command not found) -- cat Makefile | commandnotfound | cat - cat < Makefile | rev | cat -e | rev | cat -CPT - - - - probably repaired: +- cat Makefile | commandnotfound | cat +- .. # (pas de command not found) - echo -nnn -n -nnnnnn aaa -nn # (invalid write/read) - echo -nnn -n -a -nnnnnn aaa -nn # (pas de print de flag apres le -a) (mais ca marche sur echo -nnn -n -nigga -nnnnnn aaa -nn ????????????????) - cat filenotfound -> exit # (exit avec le status 0 et pas 1)