From 3d68490320084114d3ef5b86bc89266eeb68d538 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Mon, 13 Mar 2023 12:45:29 +0100 Subject: [PATCH] fix: execve failed --- execution/execution.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/execution/execution.c b/execution/execution.c index f18548d..bc64e6c 100644 --- a/execution/execution.c +++ b/execution/execution.c @@ -11,6 +11,7 @@ /* ************************************************************************** */ #include "execution_private.h" +#include static int ft_own_cmd(t_data *data, t_cmd *cmd) { @@ -41,7 +42,7 @@ static int ft_own_cmd(t_data *data, t_cmd *cmd) return (return_code); } -static int ft_executor(t_cmd *cmd, char **env) +static bool ft_executor(t_cmd *cmd, char **env) { if (cmd->fd_in[0] == -1 || cmd->fd_out[0] == -1 || cmd->executable == NULL) return (0); @@ -52,11 +53,15 @@ static int ft_executor(t_cmd *cmd, char **env) { dup2(cmd->fd_in[0], 0); dup2(cmd->fd_out[0], 1); - if (cmd->fd_in[0] > 2) - close(cmd->fd_in[0]); - if (cmd->fd_out[0] > 2) - close(cmd->fd_out[0]); + if (cmd->fd_in[1] != -1) + dup2(cmd->fd_in[1], 0); + if (cmd->fd_out[1] != -1) + dup2(cmd->fd_out[1], 1); + ft_closer(cmd->fd_in); + ft_closer(cmd->fd_out); execve(cmd->executable, cmd->args, env); + ft_eprintf("minishell: permission denied: %s\n", cmd->executable); + return (1); } return (0); } @@ -78,9 +83,9 @@ static int ft_cmd_executor(t_data *data, t_cmd *cmd) if (env == NULL) return (1); exit_code = ft_executor(cmd, env); - if (exit_code == -1) - return (1); ft_freer_tab_ultimate(1, env); + if (exit_code == 1) + return (1); } if (ft_gen_exit_code(data)) return (1);