From df92e9fa7c15bead4e8c607037a9d36de9ede336 Mon Sep 17 00:00:00 2001 From: Maix0 Date: Mon, 12 Aug 2024 17:26:15 +0200 Subject: [PATCH] added cd builtin --- exec/src/builtins/cd.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/exec/src/builtins/cd.c b/exec/src/builtins/cd.c index 67bfd414..c8811ef9 100644 --- a/exec/src/builtins/cd.c +++ b/exec/src/builtins/cd.c @@ -6,17 +6,34 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/10 18:43:18 by maiboyer #+# #+# */ -/* Updated: 2024/08/11 11:21:35 by maiboyer ### ########.fr */ +/* Updated: 2024/08/12 17:25:25 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec/builtins.h" +#include "me/hashmap/hashmap_env.h" #include "me/printf/printf.h" #include "me/str/str.h" #include "me/string/string.h" #include "me/types.h" +#include "unistd.h" t_error builtin_cd____(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code) { - return (ERROR); + const t_str key = "HOME"; + t_str *home; + + if (info.args.len <= 1) + { + home = hmap_env_get(state->tmp_var, (t_str *)&key); + if (home == NULL || *home == NULL) + home = hmap_env_get(state->env, (t_str *)&key); + if (home == NULL || *home == NULL) + return (*exit_code = 0, NO_ERROR); + if (chdir(*home) == -1) + return (*exit_code = 2, me_printf_fd(info.stderr, "cd: Unable to change directory\n"), NO_ERROR); + } + else if (chdir(info.args.buffer[1]) == -1) + return (*exit_code = 2, me_printf_fd(info.stderr, "cd: Unable to change directory\n"), NO_ERROR); + return (*exit_code = 0, NO_ERROR); }