From 6ea24e899f982fdc65c61dc9080e978923a6ec78 Mon Sep 17 00:00:00 2001 From: mdev9 Date: Sat, 13 Apr 2024 13:44:56 +0200 Subject: [PATCH] reput fds into arrays --- srcs/exec_bonus.c | 10 ++++++---- srcs/input_redirections.c | 13 ++++--------- srcs/minishell.h | 6 +++--- srcs/output_redirections.c | 7 ++++--- srcs/pipe.c | 32 ++++++++++++++------------------ 5 files changed, 31 insertions(+), 37 deletions(-) diff --git a/srcs/exec_bonus.c b/srcs/exec_bonus.c index f55d546..3861b88 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/13 12:47:16 by tomoron ### ########.fr */ +/* Updated: 2024/04/13 13:28:48 by babonnet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -78,9 +78,6 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count) perror("minishell: pipe"); ft_exit(msh, 1); } - msh->in_fd = fds[0]; - msh->out_fd = fds[1]; - print_msh_struct(msh); } pid = fork(); if (pid == -1) @@ -102,6 +99,9 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count) void exec_command(t_msh *msh, int i, int cmd_count) { g_return_code = 0; + msh->fds[i] = ft_calloc(2, sizeof(int *)); + if (!msh->fds[i]) + ft_exit(msh, 1); if (!cmd_is_builtin(msh, msh->tokens->value)) get_cmd_path(msh); exec(msh, get_cmd_args(msh), i, cmd_count); @@ -137,6 +137,7 @@ void end_execution(t_msh *msh, int cmd_count) // TODO: (core dumped) WCOREDUMP free(msh->pids); msh->pids = 0; + free(msh->fds); // signal(SIGINT, signal_handler_interactive); //enables ctrl-C signal(SIGQUIT, signal_handler_interactive); set_echoctl(0); @@ -160,6 +161,7 @@ void exec_commands(t_msh *msh) while (i < cmd_count) { exec_command(msh, i, cmd_count); + free(msh->fds[i]); i++; } end_execution(msh, cmd_count); diff --git a/srcs/input_redirections.c b/srcs/input_redirections.c index de7aab9..a89fa3f 100755 --- a/srcs/input_redirections.c +++ b/srcs/input_redirections.c @@ -6,13 +6,13 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:15:27 by marde-vr #+# #+# */ -/* Updated: 2024/04/13 12:54:23 by tomoron ### ########.fr */ +/* Updated: 2024/04/13 13:42:07 by babonnet ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void redirect_input(t_msh *msh) +void redirect_input(t_msh *msh, int i) { if (msh->in_type != PIPE) { @@ -22,14 +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"); + ft_printf_fd(2, "%d: 0 -> %d\n", i, msh->fds[i - 1][0]); + if (dup2(msh->fds[i - 1][0], 0) < 0) ft_exit(msh, 1); - } } } diff --git a/srcs/minishell.h b/srcs/minishell.h index 4a723e3..a69bfb9 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/13 12:56:29 by tomoron ### ########.fr */ +/* Updated: 2024/04/13 13:31:40 by babonnet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -103,10 +103,10 @@ void remove_command_from_msh(t_msh *msh); void ft_exit(t_msh *msh, int error_code); void signal_handler_command(int signum); void ft_exit(t_msh *msh, int exit_code); -void redirect_output(t_msh *msh); +void redirect_output(t_msh *msh, int i); char **split_paths_from_env(t_env *env); int add_return_code_to_str(char *res); -void redirect_input(t_msh *msh); +void redirect_input(t_msh *msh, int i); void parse_var(t_msh *msh, char *line); void print_parsed_token(t_token *cmd);//debug int get_var_name_len(char *command); diff --git a/srcs/output_redirections.c b/srcs/output_redirections.c index 176bd3f..39ad793 100755 --- a/srcs/output_redirections.c +++ b/srcs/output_redirections.c @@ -6,13 +6,13 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 19:10:52 by marde-vr #+# #+# */ -/* Updated: 2024/04/07 17:34:58 by tomoron ### ########.fr */ +/* Updated: 2024/04/13 13:42:29 by babonnet ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void redirect_output(t_msh *msh) +void redirect_output(t_msh *msh, int i) { if (msh->out_type != PIPE) { @@ -21,7 +21,8 @@ void redirect_output(t_msh *msh) } else { - if (dup2(msh->out_fd, 1) < 0) + ft_printf_fd(2, "%d: 1 -> %d\n", i, msh->fds[i - 1][1]); + if (dup2(msh->fds[i][1], 1) < 0) ft_exit(msh, 1); } } diff --git a/srcs/pipe.c b/srcs/pipe.c index ba3f278..45d86bc 100755 --- a/srcs/pipe.c +++ b/srcs/pipe.c @@ -6,25 +6,25 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */ -/* Updated: 2024/04/13 13:21:52 by tomoron ### ########.fr */ +/* Updated: 2024/04/13 13:39:31 by babonnet ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void close_pipe_fds(t_msh *msh) +void close_pipe_fds(t_msh *msh, int i) { - if (msh->in_fd > 2) + if (i != 0) { - close(msh->in_fd); - printf("close in"); + 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->out_fd > 2) - { - close(msh->out_fd); - printf("close out"); - } - //double close + if (msh->fds[i][0] > 2) + close(msh->fds[i][0]); + if (msh->fds[i][1] > 2) + close(msh->fds[i][1]); } void execute_command(t_msh *msh, char **cmd_args) @@ -52,11 +52,11 @@ void child(t_msh *msh, char **cmd_args, int i) if ((msh->in_type != CMD && msh->in_type != PAREN && msh->in_type != AND && msh->in_type != OR && msh->in_type != PIPE) || (msh->in_type == PIPE && i > 0)) - redirect_input(msh); + redirect_input(msh, i); if (msh->out_type == PIPE || msh->out_type == RED_O || msh->out_type == RED_O_APP) - redirect_output(msh); - close_pipe_fds(msh); + redirect_output(msh, i); + close_pipe_fds(msh, i); execute_command(msh, cmd_args); close(0); close(1); @@ -69,10 +69,6 @@ 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->fds[i - 1][0] > 2)