From 8c64e2c6f66ac6624eae2d1caaf115e31e27061d Mon Sep 17 00:00:00 2001 From: Loic Deridder Date: Fri, 17 Jan 2025 14:40:19 +0100 Subject: [PATCH] builtins --- includes/builtins.h | 4 +++ includes/minishell.h | 1 + srcs/builtins/cd.c | 58 +++++++++++++++++++++++++++++++++++++++----- srcs/builtins/echo.c | 2 +- srcs/main.c | 10 +++++--- 5 files changed, 65 insertions(+), 10 deletions(-) diff --git a/includes/builtins.h b/includes/builtins.h index a5ec0e5..479dd84 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -21,4 +21,8 @@ char **builtin_export(char **arg, char **envp); //UTILS int count_char(char *str); +int count_args(char **tab); +int extractenv(char *str, char **envp); +char *ft_getenv(char *str, char **envp); + #endif diff --git a/includes/minishell.h b/includes/minishell.h index cddcee8..b70af5f 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -3,6 +3,7 @@ # include # include +# include # include # include diff --git a/srcs/builtins/cd.c b/srcs/builtins/cd.c index b9945a5..073b1aa 100644 --- a/srcs/builtins/cd.c +++ b/srcs/builtins/cd.c @@ -1,5 +1,15 @@ #include "../../includes/builtins.h" +int count_args(char **tab) +{ + int i; + + i = 0; + while (tab[i]) + i++; + return (i); +} + char **update_oldpwd(char **envp) { char **new_envp; @@ -46,11 +56,50 @@ char **update_pwd(char **envp) return (new_envp); } +int check_path(char *str) +{ + if (access(str, F_OK | X_OK) == -1) + { + ft_put_s_fd(str, 2); + ft_put_s_fd(": ", 2); + perror("cd"); + return (0); + } + return (1); +} + +// void expand_var(char *str, char **envp) +// { +// char *new_arg; +// char *tmp; +// int i; +// int j; +// +// i = -1; +// while (str[++i]) +// { +// if (str[i] == '$') +// { +// j = 0; +// while (str[j] && str[j] != ' ') +// j++; +// if (j >= 1) +// } +// } +// } + char **builtin_cd(char **arg, char **envp) { - /* - * OLDPWD = getcwd() - */ + // char *path; + + if (count_args(arg) >= 3) + { + ft_putendl_fd("cd: too many arguments", 2); + return (envp); + } + // path = expand_var(arg[1], envp); + if (!check_path(arg[1])) + return (envp); envp = update_oldpwd(envp); if (chdir(arg[1]) == -1) { @@ -58,8 +107,5 @@ char **builtin_cd(char **arg, char **envp) return (envp); } envp = update_pwd(envp); - /* - * PWD = getcwd() - */ return (envp); } diff --git a/srcs/builtins/echo.c b/srcs/builtins/echo.c index 0ae6458..4e32d94 100644 --- a/srcs/builtins/echo.c +++ b/srcs/builtins/echo.c @@ -41,7 +41,7 @@ char *ft_getenv(char *str, char **envp) return (&envp[j][len]); } -static int extractenv(char *str, char **envp) +int extractenv(char *str, char **envp) { int i; char *var; diff --git a/srcs/main.c b/srcs/main.c index 21cc003..7ac9576 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -4,6 +4,7 @@ char *powerline(void) { char *pwd; char *tilt; + char *input; pwd = getcwd(NULL, 0); if (ft_strncmp(pwd, "/home/", 6) == 0) @@ -19,7 +20,10 @@ char *powerline(void) ----------------------------------%s", POW5, RESET); printf("\n%s  MMOAT %s%s%s%s%s %s%s%s ", POW1, POW2, POW3, POW4, tilt, pwd, RESET, POW5, RESET); - return (readline("")); + input = readline(""); + if (ft_strlen(input) > 0) + add_history(input); + return (input); } char **ft_setnewenv(void) @@ -58,9 +62,9 @@ int main(int ac, char **av, char **envp) if (ft_strncmp(input, "unset", 5) == 0) builtin_unset(input, envp); if (ft_strncmp(input, "cd", 2) == 0) - envp = builtin_cd(ft_split(input, ' '), envp); + envp = builtin_cd(ft_split(input, " "), envp); if (ft_strncmp(input, "export", 6) == 0) - envp = builtin_export(ft_split(input, ' '), envp); + envp = builtin_export(ft_split(input, " "), envp); free(input); } return (0);