From 5caeed312ccf9671ce3a5b5429c355b522aa2ae3 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Tue, 21 Feb 2023 14:37:41 +0100 Subject: [PATCH] fix: leak --- execution.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/execution.c b/execution.c index 4fd28c2..fa73ae0 100644 --- a/execution.c +++ b/execution.c @@ -1,7 +1,17 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* execution.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet static char *ft_get_executable_path(char *executable_name, t_list **env) { @@ -83,10 +93,31 @@ static int ft_excutor(t_cmd *cmd, t_list **env) return (return_value); } +static int ft_own_cmd(t_list **env, t_cmd *cmd) +{ + /* if (ft_strcmp(cmd->executable, "pwd") == 0) */ + /* return (pwd(env, cmd->fd_out)); */ + if (ft_strcmp(cmd->executable, "env") == 0) + return (print_env(env, cmd->fd_out)); + if (ft_strcmp(cmd->executable, "export") == 0) + return (print_export(env, cmd->fd_out)); + /* if (ft_strcmp(cmd->executable, "cd") == 0) */ + /* return (move_folder(cmd->args[0], cmd->fd_out)); */ + /* if (ft_strcmp(cmd->executable, "unset") == 0) */ + /* return (unset(env, cmd->args, cmd->fd_out)); */ + /* if (ft_strcmp(cmd->executable, "echo") == 0) */ + /* return (echo(cmd->fd_out, cmd->args[0])); */ + if (ft_strcmp(cmd->executable, "exit") == 0) + return (-2); + return (-1); +} + int ft_cmds_executor(t_list **cmds, t_list **env) { t_cmd *content; t_list *current; + char *return_value; + int cmd_return; int fds[2]; current = *cmds; @@ -103,13 +134,25 @@ int ft_cmds_executor(t_list **cmds, t_list **env) content->fd_out = fds[1]; ((t_cmd *) current->next->content)->fd_in = fds[0]; } - content->executable = ft_get_executable_path(content->executable, env); - if (content->executable != NULL) - ft_excutor(content, env); + cmd_return = ft_own_cmd(env, content); + if (cmd_return == -1) + { + content->executable = ft_get_executable_path( + content->executable, env); + if (content->executable != NULL) + cmd_return = ft_excutor(content, env); + } if (content->fd_in != 0) close(content->fd_in); if (content->fd_out != 1 && content->fd_out != 2) close(content->fd_out); + return_value = ft_itoa(cmd_return); + if (return_value == NULL) + { + ft_eprintf("minishell: malloc failed\n"); + return (1); + } + set_value_by_key("?", return_value, env); current = current->next; } return (0);