cd: with no arg, go to $HOME

This commit is contained in:
Khaïs COLIN 2025-03-31 16:40:01 +02:00
parent e93e6efb93
commit d66ca10a0c
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
2 changed files with 14 additions and 4 deletions

View file

@ -6,13 +6,13 @@
/* 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:32:52 by khais ### ########.fr */ /* Updated: 2025/03/31 16:39:30 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "builtins.h" #include "builtins.h"
#include <unistd.h> #include <unistd.h>
#include "libft.h" #include "../../env/env_manip.h"
#include "../../ft_errno.h" #include "../../ft_errno.h"
static void ft_chdir(char *path, t_minishell *app) static void ft_chdir(char *path, t_minishell *app)
@ -30,12 +30,13 @@ static void ft_chdir(char *path, t_minishell *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)
{ {
app->last_return_value = 1; home = env_get_val(app->env, "HOME");
ft_dprintf(STDERR_FILENO, "TODO: cd to $HOME\n"); ft_chdir(home, app);
} }
else else
ft_chdir(arg->word, app); ft_chdir(arg->word, app);

View file

@ -188,6 +188,15 @@ minishell: cd: Permission denied
/tmp/dir.minishell /tmp/dir.minishell
EOF EOF
HOME=/tmp/dir.minishell/fakehome when_run <<EOF "cd with no arguments goes to \$HOME"
mkdir fakehome
cd
pwd
EOF
expecting <<EOF
/tmp/dir.minishell/fakehome
EOF
when_run <<EOF "quoted parentheses are not operators" when_run <<EOF "quoted parentheses are not operators"
echo unclosed '(' echo unclosed '('
EOF EOF