cd: when going to $HOME, show error message if it is unset

This commit is contained in:
Khaïs COLIN 2025-03-31 16:52:31 +02:00
parent e046d151a2
commit 8f3c15f096
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
2 changed files with 27 additions and 6 deletions

View file

@ -6,11 +6,12 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/31 16:20:17 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 "builtins.h"
#include "libft.h"
#include <unistd.h> #include <unistd.h>
#include "../../env/env_manip.h" #include "../../env/env_manip.h"
#include "../../ft_errno.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_builtin_type builtin_cd(t_simple_cmd *cmd, t_minishell *app)
{ {
t_worddesc *arg; t_worddesc *arg;
char *home;
arg = wordlist_get(cmd->words, 1); arg = wordlist_get(cmd->words, 1);
if (arg == NULL) if (arg == NULL)
{ chdir_home(app);
home = env_get_val(app->env, "HOME");
ft_chdir(home, app);
}
else else
ft_chdir(arg->word, app); ft_chdir(arg->word, app);
return (BUILTIN_CD); return (BUILTIN_CD);

10
test.sh
View file

@ -200,6 +200,16 @@ expecting <<EOF
/tmp/dir.minishell/fakehome /tmp/dir.minishell/fakehome
EOF EOF
EXTRAENV="-u HOME"
when_run <<EOF "cd with no arguments, with HOME unset, prints error message and does nothing"
cd
pwd
EOF
expecting <<EOF
minishell: cd: HOME not set
/tmp/dir.minishell
EOF
when_run <<EOF "quoted parentheses are not operators" when_run <<EOF "quoted parentheses are not operators"
echo unclosed '(' echo unclosed '('
EOF EOF