diff --git a/headers/minishell_prototypes.h b/headers/minishell_prototypes.h index 8b7adc7..3f9203a 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/10/19 21:59:24 by hulamy ### ########.fr */ +/* Updated: 2021/10/19 23:41:25 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,7 +32,7 @@ char *ft_strjoinfree_s2(const char *s1, char *s2); void pipes_hugo(char *input, t_all *c); // parser hugo void **cmd_path(char **argv, char **envp); -int handle_fd(char **input, int i, int fdin, int *fd_in, int *fd_out); +int handle_fd(char **input, int i, int fdin, t_cmd *cmd); t_list *parser(char *input, char **envp); #endif diff --git a/headers/minishell_structs.h b/headers/minishell_structs.h index bc5dc09..ee3d11b 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 21:58:02 by hulamy ### ########.fr */ +/* Updated: 2021/10/19 23:59:00 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,6 +25,7 @@ typedef struct s_cmd char **argv; pid_t pid; void *builtin; + int fd_redirect; int fd_in; int fd_out; } t_cmd; diff --git a/srcs/main.c b/srcs/main.c index 2abf096..cd07334 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -30,6 +30,7 @@ void exec_cmd(char **envp, t_list *cmd_list) { if (cmd->fd_in != 0) close(cmd->fd_in); + // if (cmd->fd_out != 1 && cmd->fd_redirect == 1) if (cmd->fd_out != 1) close(cmd->fd_out); } diff --git a/srcs/parser_hugo.c b/srcs/parser_hugo.c index 4e2f9eb..25fabc2 100644 --- a/srcs/parser_hugo.c +++ b/srcs/parser_hugo.c @@ -23,20 +23,21 @@ void **cmd_path(char **argv, char **envp) return (NULL); } -int handle_fd(char **input, int i, int fdin, int *fd_in, int *fd_out) +int handle_fd(char **input, int i, int fdin, t_cmd *cmd) { int *pipes_fd; int next_in; char *tmp; - *fd_in = fdin; - *fd_out = 1; + cmd->fd_in = fdin; + cmd->fd_out = 1; + cmd->fd_redirect = 1; next_in = 0; if (input[i + 1]) { pipes_fd = calloc(2, sizeof(int)); pipe(pipes_fd); - *fd_out = pipes_fd[1]; + cmd->fd_out = pipes_fd[1]; next_in = pipes_fd[0]; } tmp = ft_strchr(input[i], '>'); @@ -44,8 +45,9 @@ int handle_fd(char **input, int i, int fdin, int *fd_in, int *fd_out) { tmp[0] = '\0'; tmp = ft_strtrim(tmp + 2, " "); // +2 for "> " - *fd_out = open(tmp, O_WRONLY | O_TRUNC); - next_in = *fd_out; + cmd->fd_out = open(tmp, O_WRONLY | O_TRUNC); + next_in = cmd->fd_out; + cmd->fd_redirect = cmd->fd_out; } return (next_in); } @@ -65,7 +67,7 @@ t_list *parser(char *input, char **envp) while (input_split[i]) { element = calloc(1, sizeof(t_cmd)); - tmp_fd = handle_fd(input_split, i, tmp_fd, &(element->fd_in), &(element->fd_out)); + tmp_fd = handle_fd(input_split, i, tmp_fd, element); element->argv = ft_split(input_split[i], ' '); element->builtin = cmd_path(element->argv, envp); ft_lstadd_back(&cmd, ft_lstnew(element));