From 4081e962c073f6dc783ca269b850a6ce20331236 Mon Sep 17 00:00:00 2001 From: yosyo Date: Thu, 1 Aug 2024 16:39:48 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix(exec):=20f?= =?UTF-8?q?ixed=20open=20FD=20when=20redirection=20with=20pipe?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/execution.h | 5 +++-- src/exec/exec_split_cmd.c | 9 ++++----- src/exec/utils_exec_2.c | 14 ++++++++++++-- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/include/execution.h b/include/execution.h index bcbd49f..26c4ddb 100644 --- a/include/execution.h +++ b/include/execution.h @@ -6,7 +6,7 @@ /* By: mmoussou cmd) return (get_exit_code(0)); + ft_arrcpy(exec.pipe_fd, pipe_fd, 2); input = ft_strdup(cmd->cmd); - exec.pipe_fd[0] = pipe_fd[0]; - exec.pipe_fd[1] = pipe_fd[1]; exec.status = switch_cmd_path(cmd, env_t); if (exec.status == -1 || !input || check_file(cmd->cmd, input)) { @@ -106,7 +105,7 @@ t_exec exec_pipe(t_exec exec, t_list *list_cmd, t_env *env) ((t_cmd *)(list_cmd->next->content))->infile = exec.pipe_fd[0]; exec.status = exec_fork_cmd(list_cmd->content, exec.env_array, \ env, exec.pipe_fd); - __close(list_cmd->content); + __close(list_cmd->content, exec.pipe_fd[0], exec.pipe_fd[1]); if (exec.status != -1) exec.i++; list_cmd = list_cmd->next; @@ -114,7 +113,7 @@ t_exec exec_pipe(t_exec exec, t_list *list_cmd, t_env *env) exec.status = exec_fork_cmd(list_cmd->content, exec.env_array, \ env, exec.pipe_fd); __sig2(exec.status); - __close(list_cmd->content); + __close(list_cmd->content, exec.pipe_fd[0], exec.pipe_fd[1]); return (exec); } diff --git a/src/exec/utils_exec_2.c b/src/exec/utils_exec_2.c index ce3efc8..9747fd1 100644 --- a/src/exec/utils_exec_2.c +++ b/src/exec/utils_exec_2.c @@ -6,7 +6,7 @@ /* By: mmoussou outfile != STDOUT_FILENO) close(((t_cmd *)(content))->outfile); if (((t_cmd *)(content))->infile != STDIN_FILENO) close(((t_cmd *)(content))->infile); + if (fd1) + close(fd1); + if (fd2) + close(fd2); } int send_error_exec(char *input) @@ -36,3 +40,9 @@ int send_error_exec(char *input) free(input); return (-1); } + +void ft_arrcpy(int *dst, int *src, int n) +{ + while (n--) + dst[n] = src[n]; +}