From: Dominik Kaiser Date: Wed, 15 Jan 2025 13:43:35 +0000 (+0100) Subject: Add some fixes to execute_cmd X-Git-Url: https://git.dkaiser.de/?a=commitdiff_plain;h=2d2df595dcf01315d080aa1b4fcf810d28f2a3a4;p=42%2Fminishell.git Add some fixes to execute_cmd --- diff --git a/include/minishell.h b/include/minishell.h index c905ca5..505b21b 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */ -/* Updated: 2025/01/14 15:29:46 by chuhlig ### ########.fr */ +/* Updated: 2025/01/15 14:04:19 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,7 +39,7 @@ t_redirection *collect_redirs(t_token **tokens); void print_ast(t_node *ast); int eval(t_node *node, t_env **env); -char *get_cmd_path(char *cmd, t_env *env); +char *get_cmd_path(char *cmd, t_env *env, int *return_code); int execute_cmd(t_cmd *cmd, t_env **env); char *format_string(char *str, t_env *env); int set_return_code(int return_code, t_env **env); diff --git a/src/execute_cmd.c b/src/execute_cmd.c index 525f333..bad6cf5 100644 --- a/src/execute_cmd.c +++ b/src/execute_cmd.c @@ -6,20 +6,18 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:21:35 by chuhlig #+# #+# */ -/* Updated: 2025/01/14 19:55:18 by chuhlig ### ########.fr */ +/* Updated: 2025/01/15 14:42:00 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +#include int is_builtin(char *cmd) { - return ((ft_strcmp(cmd, "export") == 0) - || (ft_strcmp(cmd, "unset") == 0) - || (ft_strcmp(cmd, "cd") == 0) - || (ft_strcmp(cmd, "exit") == 0) - || (ft_strcmp(cmd, "echo") == 0) - || (ft_strcmp(cmd, "pwd") == 0) + return ((ft_strcmp(cmd, "export") == 0) || (ft_strcmp(cmd, "unset") == 0) + || (ft_strcmp(cmd, "cd") == 0) || (ft_strcmp(cmd, "exit") == 0) + || (ft_strcmp(cmd, "echo") == 0) || (ft_strcmp(cmd, "pwd") == 0) || (ft_strcmp(cmd, "env") == 0)); } @@ -50,6 +48,7 @@ int execute_cmd(t_cmd *cmd, t_env **env) int original_stdout; int original_stdin; int result; + int i; original_stdout = dup(STDOUT_FILENO); original_stdin = dup(STDIN_FILENO); @@ -82,15 +81,14 @@ int execute_cmd(t_cmd *cmd, t_env **env) } if (pid == 0) { - cmd_path = get_cmd_path(cmd->args[0], *env); + i = 0; + while (cmd->args[i][0] == '\0') + i++; + cmd_path = get_cmd_path(cmd->args[i], *env, &result); if (!cmd_path) - { - perror("command not found"); - exit(EXIT_FAILURE); - } - execve(cmd_path, cmd->args, env_to_strlst(*env)); - perror("execve"); - exit(EXIT_FAILURE); + exit(result); + execve(cmd_path, &(cmd->args[i]), env_to_strlst(*env)); + exit(EXIT_SUCCESS); } waitpid(pid, &status, 0); dup2(original_stdout, STDOUT_FILENO); diff --git a/src/get_cmd_path.c b/src/get_cmd_path.c index 8a27584..56d9876 100644 --- a/src/get_cmd_path.c +++ b/src/get_cmd_path.c @@ -6,28 +6,53 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:19:59 by chuhlig #+# #+# */ -/* Updated: 2024/12/17 19:20:08 by chuhlig ### ########.fr */ +/* Updated: 2025/01/15 14:33:58 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" #include "minishell.h" +#include +#include +#include +#include -static char *get_absolute_cmd_path(char *cmd, t_env *env); -static char *find_in_path(char *cmd, t_env *env); +static char *get_absolute_cmd_path(char *cmd, t_env *env, int *return_code); +static char *find_in_path(char *cmd, t_env *env, int *return_code); char **get_split_path(t_env *env); -char *get_cmd_path(char *cmd, t_env *env) +char *get_cmd_path(char *cmd, t_env *env, int *return_code) { + char *result; + if (cmd[0] == '/') - return (ft_strdup(cmd)); + { + result = ft_strdup(cmd); + if (access(result, F_OK) == -1) + { + free(result); + errno = ENOENT; + perror(cmd); + *return_code = 127; + return (NULL); + } + else if (access(result, X_OK) == -1) + { + free(result); + errno = EACCES; + perror(cmd); + *return_code = 126; + return (NULL); + } + return (result); + } else if (ft_strchr(cmd, '/')) - return (get_absolute_cmd_path(cmd, env)); + return (get_absolute_cmd_path(cmd, env, return_code)); else - return (find_in_path(cmd, env)); + return (find_in_path(cmd, env, return_code)); } -static char *get_absolute_cmd_path(char *cmd, t_env *env) +static char *get_absolute_cmd_path(char *cmd, t_env *env, int *return_code) { char *cur_dir; char *result; @@ -38,16 +63,32 @@ static char *get_absolute_cmd_path(char *cmd, t_env *env) result = ft_strjoin(cur_dir, cmd); free(cur_dir); if (!result) + { + *return_code = 127; + errno = ENOENT; + perror(cmd); return (NULL); + } + if (access(result, F_OK) == -1) + { + free(result); + errno = ENOENT; + *return_code = 127; + perror(cmd); + return (NULL); + } if (access(result, X_OK) == -1) { free(result); + errno = EACCES; + *return_code = 126; + perror(cmd); return (NULL); } return (result); } -static char *find_in_path(char *cmd, t_env *env) +static char *find_in_path(char *cmd, t_env *env, int *return_code) { char *cur_path; char *cmd_path; @@ -70,6 +111,9 @@ static char *find_in_path(char *cmd, t_env *env) return (cmd_path); path++; } + *return_code = 127; + printf("%s:", cmd); + ft_putstr_fd(" command not found", 2); return (NULL); }