From 386d2bcb3a665720337a0a222c8b9e910bdcf199 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Mon, 7 Apr 2025 18:41:44 +0200 Subject: [PATCH] exec: correct error and return value when cmd is a directory minishell: /: Is a directory $? = 126 --- src/executing/simple_cmd/subprocess.c | 46 +++++++++++++++++++++++---- test.sh | 45 ++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 6 deletions(-) diff --git a/src/executing/simple_cmd/subprocess.c b/src/executing/simple_cmd/subprocess.c index 9d7b882..7783c8f 100644 --- a/src/executing/simple_cmd/subprocess.c +++ b/src/executing/simple_cmd/subprocess.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/02 18:19:23 by khais #+# #+# */ -/* Updated: 2025/04/02 18:20:54 by khais ### ########.fr */ +/* Updated: 2025/04/08 13:23:59 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,10 @@ #include "../../env/env_convert.h" #include "../../parser/simple_cmd/simple_cmd.h" #include "../../subst/path_split.h" +#include +#include "../../ft_errno.h" #include +#include static char **argv_from_wordlist(t_wordlist *wordlist) { @@ -31,20 +34,51 @@ static char **argv_from_wordlist(t_wordlist *wordlist) return (out); } -static void ft_execve(char *exe, char **argv, char **envp) +static bool ft_is_directory(char *path) { + DIR *dir; + bool retvalue; + + errno = 0; + retvalue = true; + dir = opendir(path); + if (errno == ENOTDIR) + retvalue = false; + if (dir != NULL) + closedir(dir); + return (retvalue); +} + +static int ft_execve(char *exe, char **argv, char **envp) +{ + int retvalue; + + retvalue = 127; execve(exe, argv, envp); - ft_dprintf(STDERR_FILENO, "minishell: %s: ", argv[0]); - perror(NULL); + if (ft_is_directory(exe)) + { + ft_dprintf(STDERR_FILENO, "minishell: %s: %s\n", argv[0], + strerror(EISDIR)); + retvalue = 126; + } + else + { + ft_dprintf(STDERR_FILENO, "minishell: %s: ", argv[0]); + ft_perror(NULL); + } free(exe); path_split_destroy(argv); path_split_destroy(envp); + return (retvalue); } void execute_subprocess(char *exe, t_simple_cmd *cmd, t_minishell *app) { - ft_execve(exe, argv_from_wordlist(cmd->words), envp_from_env(app->env)); + int retvalue; + + retvalue = ft_execve(exe, argv_from_wordlist(cmd->words), + envp_from_env(app->env)); simple_cmd_destroy(cmd); env_destroy(app->env); - exit(127); + exit(retvalue); } diff --git a/test.sh b/test.sh index 34bde3f..3c102ab 100755 --- a/test.sh +++ b/test.sh @@ -131,6 +131,51 @@ EOF expecting <