diff --git a/headers/minishell_structs.h b/headers/minishell_structs.h index ee3d11b..0827910 100644 --- a/headers/minishell_structs.h +++ b/headers/minishell_structs.h @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/08 02:35:52 by lperrey #+# #+# */ -/* Updated: 2021/10/19 23:59:00 by hulamy ### ########.fr */ +/* Updated: 2021/10/20 09:11:41 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,6 +25,8 @@ typedef struct s_cmd char **argv; pid_t pid; void *builtin; + int pipe_in; + int pipe_out; int fd_redirect; int fd_in; int fd_out; diff --git a/srcs/main.c b/srcs/main.c index 0c6be51..d549f9f 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -1,6 +1,27 @@ #include "minishell.h" +void close_fd(t_cmd *cmd, pid_t pid) +{ + if (cmd->fd_in != 0) + close(cmd->fd_in); + if (pid == 0) + { + if (cmd->pipe_out != 1) + { + close(cmd->pipe_in); + close(cmd->pipe_out); + } + if (cmd->fd_out != 1) + close(cmd->fd_out); + } + else + { + if (cmd->fd_out != 1 && cmd->fd_redirect == 1) + close(cmd->fd_out); + } +} + void exec_cmd(char **envp, t_list *cmd_list) { t_cmd *cmd; @@ -15,23 +36,19 @@ void exec_cmd(char **envp, t_list *cmd_list) if (pid == 0) { if (cmd->fd_in != 0) - { dup2(cmd->fd_in, STDIN_FILENO); - close(cmd->fd_in); - } if (cmd->fd_out != 1) - { dup2(cmd->fd_out, STDOUT_FILENO); - close(cmd->fd_out); - } + close_fd(cmd, pid); execve(cmd->argv[0], cmd->argv, envp); } else { - if (cmd->fd_in != 0) - close(cmd->fd_in); - if (cmd->fd_out != 1 && cmd->fd_redirect == 1) - close(cmd->fd_out); + close_fd(cmd, pid); + // if (cmd->fd_in != 0) + // close(cmd->fd_in); + // if (cmd->fd_out != 1 && cmd->fd_redirect == 1) + // close(cmd->fd_out); } while ((wpid = wait(&status)) > 0); cmd_list = cmd_list->next; diff --git a/srcs/parser_hugo.c b/srcs/parser_hugo.c index 2c82367..5be851a 100644 --- a/srcs/parser_hugo.c +++ b/srcs/parser_hugo.c @@ -32,6 +32,8 @@ int handle_fd(char **input, int i, int fdin, t_cmd *cmd) cmd->fd_in = fdin; cmd->fd_out = 1; cmd->fd_redirect = 1; + cmd->pipe_in = 0; + cmd->pipe_out = 1; next_in = 0; if (input[i + 1]) { @@ -39,6 +41,8 @@ int handle_fd(char **input, int i, int fdin, t_cmd *cmd) pipe(pipes_fd); cmd->fd_out = pipes_fd[1]; next_in = pipes_fd[0]; + cmd->pipe_in = pipes_fd[0]; + cmd->pipe_out = pipes_fd[1]; } tmp = ft_strchr(input[i], '>'); if (tmp)