fix(command_not_found): unset or empty PATH leads to No such file or directory error

This commit is contained in:
Khaïs COLIN 2025-04-25 15:36:59 +02:00
parent c756d3783f
commit 29bbb5e572
4 changed files with 48 additions and 6 deletions

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
{
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;
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

29
test.sh
View file

@ -1196,4 +1196,33 @@ expecting <<EOF
A=
EOF
when_run <<EOF "unset PATH leads to No such file or directory error"
export EXTRAPATH="$(dirname $(which ls))"
qwertyuiop
unset PATH
qwertyuiop
export PATH=""
qwertyuiop
export PATH=":"
qwertyuiop
export PATH="::"
qwertyuiop
export PATH="\$EXTRAPATH"
qwertyuiop
export PATH=":\$EXTRAPATH"
qwertyuiop
export PATH="\$EXTRAPATH:"
qwertyuiop
EOF
expecting <<EOF
minishell: qwertyuiop: command not found
minishell: qwertyuiop: No such file or directory
minishell: qwertyuiop: No such file or directory
minishell: qwertyuiop: command not found
minishell: qwertyuiop: command not found
minishell: qwertyuiop: command not found
minishell: qwertyuiop: command not found
minishell: qwertyuiop: command not found
EOF
finalize