From d72d88bdeda9aa754bb54a11568980293783b07f Mon Sep 17 00:00:00 2001 From: joris schaft Date: Fri, 28 Jun 2024 11:24:29 +0200 Subject: [PATCH 1/2] [FIX] fixed the cd builtin --- Makefile | 2 +- include/builtins.h | 3 +- src/builtin.c | 19 +--------- src/{echo_builtin.c => builtin2.c} | 61 ++++++++++++++++++++++++++---- src/main.c | 8 +++- 5 files changed, 64 insertions(+), 29 deletions(-) rename src/{echo_builtin.c => builtin2.c} (54%) diff --git a/Makefile b/Makefile index 87d92f7..8942573 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ SRCS = src/ # include directory INCLUDES = include/ libtf/ libft/ # .c files in src/ without the extension -CODE = main ask_command error path parse_command exec_command builtin echo_builtin\ +CODE = main ask_command error path parse_command exec_command builtin builtin2\ signals cool_readline # directories to 'make' LIBRARIES = libtf libft diff --git a/include/builtins.h b/include/builtins.h index 912103f..d927d41 100644 --- a/include/builtins.h +++ b/include/builtins.h @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */ -/* Updated: 2024/06/25 13:31:08 by jschaft ### ########.fr */ +/* Updated: 2024/06/27 14:36:14 by jschaft ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,5 +24,6 @@ int exec_builtin(t_call call, char *const envp[]); // Execute echo builtin int echo_builtin(const char **argv); +int cd_builtin(char *const *path, char *const envp[]); #endif diff --git a/src/builtin.c b/src/builtin.c index 8b467f2..63d1581 100644 --- a/src/builtin.c +++ b/src/builtin.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/03 12:02:45 by jschaft #+# #+# */ -/* Updated: 2024/06/25 13:58:48 by jschaft ### ########.fr */ +/* Updated: 2024/06/27 13:52:00 by jschaft ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,21 +26,6 @@ bool is_builtin(const char *str) || ft_strncmp(str, "unset", 6) == 0); } -// Execute the builtin cd for the specified path. -// Return 0 if success, errno if error. -static int cd_builtin(const char *path) -{ - int r; - - r = chdir(path); - if (r) - { - perror("cd"); - return (errno); - } - return (0); -} - //Execute the builtin pwd for the current directory. // Return 0 for the sake of returning something for exec_builtin static int pwd_builtin(void) @@ -79,7 +64,7 @@ void exit_builtin(void) int exec_builtin(t_call call, char *const envp[]) { if (ft_strncmp(call.program, "cd", 3) == 0) - return (cd_builtin(call.argv[1])); + return (cd_builtin(call.argv, envp)); if (ft_strncmp(call.program, "exit", 5) == 0) exit_builtin(); /* if (ft_strncmp(call.program, "export", 7) == 0) diff --git a/src/echo_builtin.c b/src/builtin2.c similarity index 54% rename from src/echo_builtin.c rename to src/builtin2.c index 5a80eeb..7a095df 100644 --- a/src/echo_builtin.c +++ b/src/builtin2.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* echo_builtin.c :+: :+: :+: */ +/* builtin2.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/25 13:28:29 by jschaft #+# #+# */ -/* Updated: 2024/06/25 13:55:12 by jschaft ### ########.fr */ +/* Updated: 2024/06/27 14:35:22 by jschaft ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,13 +19,13 @@ int echo_builtin(const char **argv) i = 1; opt = 0; - if (argv[1] != NULL) + if (argv[1] != NULL) { if (!ft_strncmp(argv[1], "-n", 3)) - { + { opt = 1; - i++; - } + i++; + } } while (argv[i] != NULL) { @@ -34,7 +34,52 @@ int echo_builtin(const char **argv) printf(" "); i++; } - if (opt == 0) - printf("\n"); + if (opt == 0) + printf("\n"); + return (0); +} + +static int go_home(char *const envp[]) +{ + char **path; + int r; + int i; + + i = 0; + while (ft_strncmp(envp[i], "HOME=", 5) != 0) + i++; + path = ft_split(envp[i], '='); + r = chdir(path[1]); + free(path[0]); + free(path[1]); + free(path); + return (r); +} + +// Execute the builtin cd for the specified path. +// Return 0 if success, errno if error. +int cd_builtin(char *const *path, char *const envp[]) +{ + int r; + int i; + + i = 0; + while (path[i] != NULL) + i++; + i--; + if (i == 1) + r = chdir(path[1]); + if (i == 0) + r = go_home(envp); + if (i != 0 && i != 1) + { + printf("cd: Too many arguments\n"); + return (0); + } + if (r) + { + perror("cd"); + return (errno); + } return (0); } diff --git a/src/main.c b/src/main.c index b4a00e1..f1b8d14 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:33:45 by mcolonna #+# #+# */ -/* Updated: 2024/06/25 14:08:55 by jschaft ### ########.fr */ +/* Updated: 2024/06/27 14:09:18 by jschaft ### ########.fr */ /* */ /* ************************************************************************** */ @@ -78,8 +78,12 @@ int main(const int argc, const char *argv[], char *const envp[]) t_env env; t_list variables; - (void)argc; (void)argv; + if (argc != 1) + { + printf("Error: No argument needed\n"); + return (0); + } env.exit = false; env.mc_global = mem_newclass(fatal_error); set_global_mc(env.mc_global); From 5b751b22faa06734dd42a787d07fe1ee8cd3f608 Mon Sep 17 00:00:00 2001 From: mcolonna Date: Fri, 28 Jun 2024 11:45:42 +0200 Subject: [PATCH 2/2] little fixes --- include/builtins.h | 3 ++- src/builtin.c | 4 ++-- src/main.c | 6 +++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/include/builtins.h b/include/builtins.h index d927d41..9621e51 100644 --- a/include/builtins.h +++ b/include/builtins.h @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */ -/* Updated: 2024/06/27 14:36:14 by jschaft ### ########.fr */ +/* Updated: 2024/06/28 11:35:07 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,7 @@ int exec_builtin(t_call call, char *const envp[]); // Execute echo builtin int echo_builtin(const char **argv); +// Execute cd builtin int cd_builtin(char *const *path, char *const envp[]); #endif diff --git a/src/builtin.c b/src/builtin.c index 63d1581..9893ab2 100644 --- a/src/builtin.c +++ b/src/builtin.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/03 12:02:45 by jschaft #+# #+# */ -/* Updated: 2024/06/27 13:52:00 by jschaft ### ########.fr */ +/* Updated: 2024/06/28 11:37:24 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ bool is_builtin(const char *str) || ft_strncmp(str, "unset", 6) == 0); } -//Execute the builtin pwd for the current directory. +// Execute the builtin pwd for the current directory. // Return 0 for the sake of returning something for exec_builtin static int pwd_builtin(void) { diff --git a/src/main.c b/src/main.c index f1b8d14..033dc2a 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:33:45 by mcolonna #+# #+# */ -/* Updated: 2024/06/27 14:09:18 by jschaft ### ########.fr */ +/* Updated: 2024/06/28 11:39:45 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -81,8 +81,8 @@ int main(const int argc, const char *argv[], char *const envp[]) (void)argv; if (argc != 1) { - printf("Error: No argument needed\n"); - return (0); + minishell_error("Takes no argument"); + return (2); } env.exit = false; env.mc_global = mem_newclass(fatal_error);