From ee7a879de55a1feec5d5d72127fd94045aaab35b Mon Sep 17 00:00:00 2001 From: Sergio Date: Fri, 31 Oct 2025 01:41:50 +0100 Subject: [PATCH] update: added cd builtin --- include/builtins.h | 6 ++++- src/builtins/cd/cd.c | 52 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/builtins/cd/cd.c diff --git a/include/builtins.h b/include/builtins.h index 97fcc15..ca56714 100644 --- a/include/builtins.h +++ b/include/builtins.h @@ -6,7 +6,7 @@ /* By: sede-san 2) + { + ft_eputendl("minishell: cd: too many arguments"); + return (2); + } + else if (cmd.argc == 1) + path = getenv("HOME"); + else + path = cmd.argv[1]; + if (chdir(path) == -1) + return (handle_error(cmd, msh, path)); + return (EXIT_SUCCESS); +} + +static u_int8_t handle_error( + t_command cmd, + t_minishell *msh, + char *path +){ + u_int8_t exit_code; + + if (access(path, F_OK) != -1) + // No such file or directory + exit_code = 1; + else if (access(path, X_OK) == -1) + // Permission denied + exit_code = 2; + perror(cmd.argv[0]); + return (exit_code); +}