From 1d849c4feb44f0bca45e1bd15f1d1fea7fd62c64 Mon Sep 17 00:00:00 2001 From: narnaud Date: Tue, 17 May 2022 17:29:34 +0200 Subject: [PATCH] fix cd error is not home and return for errors commands --- built-in.c | 14 +++++++++++--- caller.c | 4 ++-- errors.c | 6 +++--- minishell.h | 4 ++-- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/built-in.c b/built-in.c index 4f5671f..d843550 100755 --- a/built-in.c +++ b/built-in.c @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/06 09:02:57 by narnaud #+# #+# */ -/* Updated: 2022/05/17 15:38:18 by mea ### ########.fr */ +/* Updated: 2022/05/17 17:28:52 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -74,8 +74,10 @@ int ft_pwd(t_command *cmd) return (0); } -int ft_cd(t_command *cmd) +int ft_cd(t_datas *datas, t_command *cmd) { + char *home; + if (cmd->argv[1]) { if (access(cmd->argv[1], X_OK)) @@ -88,7 +90,13 @@ int ft_cd(t_command *cmd) return (0); } else - chdir(getenv("HOME")); + { + home = get_var_value(datas, "HOME", 5); + if (*home) + chdir(home); + else + return (cmd_error("cd", "HOME not set")); + } return (0); } diff --git a/caller.c b/caller.c index 92a9482..eb0006c 100755 --- a/caller.c +++ b/caller.c @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */ -/* Updated: 2022/05/17 16:28:32 by narnaud ### ########.fr */ +/* Updated: 2022/05/17 17:26:25 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ int call_builtin(t_datas *datas, t_command *cmd) else if (ft_strncmp(cmd->argv[0], "pwd", 4) == 0) return (ft_pwd(cmd)); else if (ft_strncmp(cmd->argv[0], "cd", 3) == 0) - return (ft_cd(cmd)); + return (ft_cd(datas, cmd)); else if (ft_strncmp(cmd->argv[0], "export", 7) == 0) return (ft_export(datas, cmd)); else if (ft_strncmp(cmd->argv[0], "unset", 6) == 0) diff --git a/errors.c b/errors.c index 5e76de4..27f9cfa 100644 --- a/errors.c +++ b/errors.c @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/06 13:15:59 by narnaud #+# #+# */ -/* Updated: 2022/05/17 09:59:56 by narnaud ### ########.fr */ +/* Updated: 2022/05/17 17:24:09 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ int file_error(char *cmd, char *file, char *msg) ft_putstr_fd(": ", 2); ft_putstr_fd(msg, 2); ft_putchar_fd('\n', 2); - return (0); + return (1); } int cmd_error(char *cmd, char *msg) @@ -31,7 +31,7 @@ int cmd_error(char *cmd, char *msg) ft_putstr_fd(": ", 2); ft_putstr_fd(msg, 2); ft_putchar_fd('\n', 2); - return (0); + return (1); } int is_valid_identifier(char *ident) diff --git a/minishell.h b/minishell.h index f7e1043..7f5096f 100755 --- a/minishell.h +++ b/minishell.h @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */ -/* Updated: 2022/05/17 15:55:14 by mea ### ########.fr */ +/* Updated: 2022/05/17 17:25:53 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -68,7 +68,7 @@ int is_valid_identifier(char *ident); int is_builtin(char *cmd); int ft_echo(t_command *cmd); int ft_pwd(t_command *cmd); -int ft_cd(t_command *cmd); +int ft_cd(t_datas *datas, t_command *cmd); int ft_exit(t_datas *datas, t_command *cmd); // ----------------------------------Env.c