From 8f3c15f096c4a40746c46eaf3709023ea332d349 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Mon, 31 Mar 2025 16:52:31 +0200 Subject: [PATCH] cd: when going to $HOME, show error message if it is unset --- src/executing/simple_cmd/builtin_cd.c | 23 +++++++++++++++++------ test.sh | 10 ++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/executing/simple_cmd/builtin_cd.c b/src/executing/simple_cmd/builtin_cd.c index 123dfdb..8d8fb0d 100644 --- a/src/executing/simple_cmd/builtin_cd.c +++ b/src/executing/simple_cmd/builtin_cd.c @@ -6,11 +6,12 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/31 16:20:17 by khais #+# #+# */ -/* Updated: 2025/03/31 16:39:30 by khais ### ########.fr */ +/* Updated: 2025/03/31 16:56:16 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include "builtins.h" +#include "libft.h" #include #include "../../env/env_manip.h" #include "../../ft_errno.h" @@ -27,17 +28,27 @@ static void ft_chdir(char *path, t_minishell *app) } } +static void chdir_home(t_minishell *app) +{ + char *home; + + home = env_get_val(app->env, "HOME"); + if (home == NULL) + { + app->last_return_value = 1; + ft_dprintf(STDERR_FILENO, "minishell: cd: HOME not set\n"); + } + else + ft_chdir(home, app); +} + t_builtin_type builtin_cd(t_simple_cmd *cmd, t_minishell *app) { t_worddesc *arg; - char *home; arg = wordlist_get(cmd->words, 1); if (arg == NULL) - { - home = env_get_val(app->env, "HOME"); - ft_chdir(home, app); - } + chdir_home(app); else ft_chdir(arg->word, app); return (BUILTIN_CD); diff --git a/test.sh b/test.sh index b0f0a3c..4300d33 100755 --- a/test.sh +++ b/test.sh @@ -200,6 +200,16 @@ expecting <