From 29bbb5e57297d13a57085b760d03b22b028109ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Fri, 25 Apr 2025 15:36:59 +0200 Subject: [PATCH] fix(command_not_found): unset or empty PATH leads to No such file or directory error --- src/executing/simple_cmd/simple_cmd_execute.c | 18 ++++++++++-- src/subst/simple_filename_exp_utils_utils.c | 4 +-- src/subst/simple_filename_exp_utils_utils.h | 3 +- test.sh | 29 +++++++++++++++++++ 4 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/executing/simple_cmd/simple_cmd_execute.c b/src/executing/simple_cmd/simple_cmd_execute.c index 6d5e04d..6c29349 100644 --- a/src/executing/simple_cmd/simple_cmd_execute.c +++ b/src/executing/simple_cmd/simple_cmd_execute.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/27 16:21:56 by khais #+# #+# */ -/* Updated: 2025/04/22 15:38:00 by khais ### ########.fr */ +/* Updated: 2025/04/25 15:47:14 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,11 +28,23 @@ #include "../../postprocess/expansion/expand_wildcard.h" #include "simple_cmd_execute_debug.h" #include "handle_redirections.h" +#include "../../subst/simple_filename_exp_utils_utils.h" +#include "../../subst/path_split.h" static void command_not_found(t_simple_cmd *cmd, t_minishell *app) { - ft_dprintf(STDERR_FILENO, "minishell: %s: command not found\n", - cmd->words->word->word); + char **path; + + path = get_paths_array(app->env); + if (path == NULL || path[0] == NULL) + { + ft_dprintf(STDERR_FILENO, "minishell: %s: No such file or directory\n", + cmd->words->word->word); + } + else + ft_dprintf(STDERR_FILENO, "minishell: %s: command not found\n", + cmd->words->word->word); + path_split_destroy(path); app->last_return_value = 127; } diff --git a/src/subst/simple_filename_exp_utils_utils.c b/src/subst/simple_filename_exp_utils_utils.c index b12bed6..f2c6a8b 100644 --- a/src/subst/simple_filename_exp_utils_utils.c +++ b/src/subst/simple_filename_exp_utils_utils.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 17:56:47 by khais #+# #+# */ -/* Updated: 2025/04/24 18:00:33 by khais ### ########.fr */ +/* Updated: 2025/04/25 15:42:20 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ ** in the PATH environnement variable. ** Returns NULL in case of an allocation error. */ -static char **get_paths_array(t_env *env) +char **get_paths_array(t_env *env) { char **path_array; t_env *path_node; diff --git a/src/subst/simple_filename_exp_utils_utils.h b/src/subst/simple_filename_exp_utils_utils.h index 359021a..8a56328 100644 --- a/src/subst/simple_filename_exp_utils_utils.h +++ b/src/subst/simple_filename_exp_utils_utils.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 17:57:22 by khais #+# #+# */ -/* Updated: 2025/04/24 18:00:56 by khais ### ########.fr */ +/* Updated: 2025/04/25 15:42:31 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,5 +16,6 @@ # include "../minishell.h" char *filepath_from_env(char *filename, t_minishell *app); +char **get_paths_array(t_env *env); #endif // SIMPLE_FILENAME_EXP_UTILS_UTILS_H diff --git a/test.sh b/test.sh index f65d5d1..e1cc80f 100755 --- a/test.sh +++ b/test.sh @@ -1196,4 +1196,33 @@ expecting <