From 90e569f2317d81f28f62b851b7cae552a27f1d1b Mon Sep 17 00:00:00 2001 From: tomoron Date: Sat, 13 Apr 2024 13:22:20 +0200 Subject: [PATCH] =?UTF-8?q?c'est=20encore=20cass=C3=A9=20mais=20un=20peut?= =?UTF-8?q?=20moins?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 3 ++- srcs/exec_bonus.c | 3 ++- srcs/here_doc.c | 4 +++- srcs/input_redirections.c | 5 ++++- srcs/is_fd_open.c | 24 +++++++++++++++++++++ srcs/minishell.h | 3 ++- srcs/pipe.c | 45 +++++++++++++++++++++------------------ 7 files changed, 61 insertions(+), 26 deletions(-) create mode 100644 srcs/is_fd_open.c diff --git a/Makefile b/Makefile index 8388b4f..a83daed 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: marde-vr +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/07/28 00:35:01 by tomoron #+# #+# # -# Updated: 2024/04/06 18:46:11 by tomoron ### ########.fr # +# Updated: 2024/04/13 12:56:12 by tomoron ### ########.fr # # # # **************************************************************************** # @@ -18,6 +18,7 @@ SRCS_RAW = main.c\ lst_token.c\ exec.c\ exit.c\ + is_fd_open.c\ echo.c\ pwd.c\ parsing.c\ diff --git a/srcs/exec_bonus.c b/srcs/exec_bonus.c index 6701c75..f55d546 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/07 17:47:44 by tomoron ### ########.fr */ +/* Updated: 2024/04/13 12:47:16 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -80,6 +80,7 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count) } msh->in_fd = fds[0]; msh->out_fd = fds[1]; + print_msh_struct(msh); } pid = fork(); if (pid == -1) diff --git a/srcs/here_doc.c b/srcs/here_doc.c index fec8290..9750580 100755 --- a/srcs/here_doc.c +++ b/srcs/here_doc.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/24 17:44:32 by marde-vr #+# #+# */ -/* Updated: 2024/04/03 17:20:55 by tomoron ### ########.fr */ +/* Updated: 2024/04/13 12:58:01 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,6 +43,7 @@ void here_doc_child(t_msh *msh, char *eof, char *here_doc_file) //signal(SIGINT, signal_handler_here_doc); get_here_doc_input(msh, eof); close(msh->in_fd); + printf("close2"); free(here_doc_file); ft_exit(msh, 0); } @@ -57,6 +58,7 @@ void here_doc_signal(t_msh *msh, int child_pid, char *here_doc_file) //signal(SIGINT, signal_handler_interactive); signal(SIGQUIT, signal_handler_interactive); close(msh->in_fd); + printf("close 1\n"); if (WIFEXITED(status) && WEXITSTATUS(status)) unlink(here_doc_file); msh->in_fd = open(here_doc_file, O_RDWR, 0644); diff --git a/srcs/input_redirections.c b/srcs/input_redirections.c index 8f7d9ef..de7aab9 100755 --- a/srcs/input_redirections.c +++ b/srcs/input_redirections.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:15:27 by marde-vr #+# #+# */ -/* Updated: 2024/04/07 19:51:29 by tomoron ### ########.fr */ +/* Updated: 2024/04/13 12:54:23 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,9 @@ void redirect_input(t_msh *msh) } else { + printf("in_fd : %d\n", msh->in_fd); + printf("cmd : %s\n", msh->cmds->value); + is_fd_open(msh->in_fd); if (dup2(msh->in_fd, 0) < 0) { perror("dup2"); diff --git a/srcs/is_fd_open.c b/srcs/is_fd_open.c new file mode 100644 index 0000000..aafb107 --- /dev/null +++ b/srcs/is_fd_open.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* is_fd_open.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: marde-vr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/05 17:31:53 by marde-vr #+# #+# */ +/* Updated: 2024/03/05 17:35:39 by marde-vr ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../srcs/minishell.h" + +int is_fd_open(int fd) +{ + if (fcntl(fd, F_GETFL) == -1) + { + ft_printf_fd(2, "%d:closed\n", fd); + return (0); + } + ft_printf_fd(2, "%d:open\n", fd); + return (1); +} diff --git a/srcs/minishell.h b/srcs/minishell.h index 570747e..4a723e3 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/03 16:52:11 by tomoron ### ########.fr */ +/* Updated: 2024/04/13 12:56:29 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -88,6 +88,7 @@ void child(t_msh *msh, char **cmd_args, int i); t_token *parse_command(char *command, t_env *env); void parent(t_msh *msh, int i, int cmd_count); char *ft_get_env(t_env *env, char *var_name); +int is_fd_open(int fd); void get_out_type(t_msh *msh, t_cmd *cmds); void exec_commands(t_msh *msh); void handle_here_doc(t_msh *msh, char *eof); diff --git a/srcs/pipe.c b/srcs/pipe.c index 34d58fb..ba3f278 100755 --- a/srcs/pipe.c +++ b/srcs/pipe.c @@ -6,25 +6,24 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */ -/* Updated: 2024/04/07 18:49:09 by tomoron ### ########.fr */ +/* Updated: 2024/04/13 13:21:52 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void close_pipe_fds(t_msh *msh, int i) +void close_pipe_fds(t_msh *msh) { - if (i != 0) + if (msh->in_fd > 2) { - if (msh->in_fd > 2) - close(msh->in_fd); - if (msh->out_fd > 2) - close(msh->out_fd); - } - if (msh->in_fd > 2) - close(msh->out_fd); - if (msh->in_fd > 2) close(msh->in_fd); + printf("close in"); + } + if (msh->out_fd > 2) + { + close(msh->out_fd); + printf("close out"); + } //double close } @@ -57,7 +56,7 @@ void child(t_msh *msh, char **cmd_args, int i) if (msh->out_type == PIPE || msh->out_type == RED_O || msh->out_type == RED_O_APP) redirect_output(msh); - close_pipe_fds(msh, i); + close_pipe_fds(msh); execute_command(msh, cmd_args); close(0); close(1); @@ -70,22 +69,26 @@ void parent(t_msh *msh, int i, int cmd_count) { //signal(SIGINT, signal_handler_command); signal(SIGQUIT, signal_handler_command); + (void)msh; + (void) i; + (void) cmd_count; + printf("i : %d\n", i); if (i != 0) { - if (msh->in_fd > 2) - close(msh->in_fd); - if (msh->out_fd > 2) - close(msh->out_fd); + 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 == cmd_count - 1) { - if (msh->in_fd > 2) - close(msh->in_fd); - if (msh->out_fd > 2) - close(msh->out_fd); + if (msh->fds[i][0] > 2) + close(msh->fds[i][0]); + if (msh->fds[i][1] > 2) + close(msh->fds[i][1]); } if (msh->in_fd > 2) close(msh->in_fd); if (msh->out_fd > 2) - close(msh->out_fd); + close(msh->out_fd); }