From 0bf69437bb17f0a3db656e4bc300de175de0e59e Mon Sep 17 00:00:00 2001 From: Loic Deridder Date: Thu, 16 Jan 2025 13:57:52 +0100 Subject: [PATCH 1/2] lib --- lib/libft/libft.h | 1 + lib/libft/srcs/str/ft_strcmp.c | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 lib/libft/srcs/str/ft_strcmp.c diff --git a/lib/libft/libft.h b/lib/libft/libft.h index 56a9638..4639da7 100644 --- a/lib/libft/libft.h +++ b/lib/libft/libft.h @@ -35,6 +35,7 @@ size_t ft_strlcat(char *dst, const char *src, size_t dstsize); char *ft_strchr(const char *s, int c); char *ft_strrchr(const char *s, int c); int ft_strncmp(const char *s1, const char *s2, size_t n); +int ft_strcmp(const char *s1, const char *s2); char *ft_strnstr(const char *haystack, const char *needle, size_t len); char *ft_substr(const char *s, unsigned int start, size_t len); char *ft_strdup(const char *s1); diff --git a/lib/libft/srcs/str/ft_strcmp.c b/lib/libft/srcs/str/ft_strcmp.c new file mode 100644 index 0000000..3a69ecf --- /dev/null +++ b/lib/libft/srcs/str/ft_strcmp.c @@ -0,0 +1,11 @@ +#include "../../libft.h" + +int ft_strcmp(const char *s1, const char *s2) +{ + while (*s1 && *s2 && (*s1 == *s2)) + { + s1++; + s2++; + } + return ((unsigned char) *s1 - (unsigned char) *s2); +} From 583b3e034a46942730b25f5d35d262231d28baf3 Mon Sep 17 00:00:00 2001 From: Loic Deridder Date: Thu, 16 Jan 2025 14:42:38 +0100 Subject: [PATCH 2/2] builtins --- includes/builtins.h | 2 + lib/libft/libft.h | 1 - lib/libft/srcs/str/ft_strcmp.c | 11 ----- srcs/builtins/cd.c | 63 +++++++++++++++++++++++- srcs/builtins/env.c | 2 +- srcs/builtins/exit.c | 46 ++++++++++++++++-- srcs/builtins/export.c | 88 ++++++++++++++++++++++++++++++++++ srcs/main.c | 4 ++ 8 files changed, 199 insertions(+), 18 deletions(-) delete mode 100644 lib/libft/srcs/str/ft_strcmp.c diff --git a/includes/builtins.h b/includes/builtins.h index e2e940f..a5ec0e5 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -16,6 +16,8 @@ void builtin_exit(char *arg, bool depth); void builtin_pwd(char *arg); void builtin_env(char *str, char **envp); void builtin_unset(char *str, char **envp); +char **builtin_cd(char **arg, char **envp); +char **builtin_export(char **arg, char **envp); //UTILS int count_char(char *str); diff --git a/lib/libft/libft.h b/lib/libft/libft.h index 4639da7..56a9638 100644 --- a/lib/libft/libft.h +++ b/lib/libft/libft.h @@ -35,7 +35,6 @@ size_t ft_strlcat(char *dst, const char *src, size_t dstsize); char *ft_strchr(const char *s, int c); char *ft_strrchr(const char *s, int c); int ft_strncmp(const char *s1, const char *s2, size_t n); -int ft_strcmp(const char *s1, const char *s2); char *ft_strnstr(const char *haystack, const char *needle, size_t len); char *ft_substr(const char *s, unsigned int start, size_t len); char *ft_strdup(const char *s1); diff --git a/lib/libft/srcs/str/ft_strcmp.c b/lib/libft/srcs/str/ft_strcmp.c deleted file mode 100644 index 3a69ecf..0000000 --- a/lib/libft/srcs/str/ft_strcmp.c +++ /dev/null @@ -1,11 +0,0 @@ -#include "../../libft.h" - -int ft_strcmp(const char *s1, const char *s2) -{ - while (*s1 && *s2 && (*s1 == *s2)) - { - s1++; - s2++; - } - return ((unsigned char) *s1 - (unsigned char) *s2); -} diff --git a/srcs/builtins/cd.c b/srcs/builtins/cd.c index 0fbbd8f..b9945a5 100644 --- a/srcs/builtins/cd.c +++ b/srcs/builtins/cd.c @@ -1,6 +1,65 @@ #include "../../includes/builtins.h" -void builtin_cd(char *str) +char **update_oldpwd(char **envp) { - (void)str; + char **new_envp; + int i; + + i = 0; + while (envp[i]) + i++; + new_envp = malloc(sizeof(char *) * i + 1); + if (!new_envp) + return (perror("malloc"), NULL); + new_envp[i] = NULL; + i = -1; + while (envp[++i]) + { + if (ft_strnstr(envp[i], "OLDPWD=", 7) == NULL) + new_envp[i] = ft_strdup(envp[i]); + else + new_envp[i] = ft_strjoin("OLDPWD=", getcwd(NULL, 0)); + } + return (new_envp); +} + +char **update_pwd(char **envp) +{ + char **new_envp; + int i; + + i = 0; + while (envp[i]) + i++; + new_envp = malloc(sizeof(char *) * i + 1); + if (!new_envp) + return (perror("malloc"), NULL); + new_envp[i] = NULL; + i = -1; + while (envp[++i]) + { + if (ft_strnstr(envp[i], "PWD=", 4) == NULL) + new_envp[i] = ft_strdup(envp[i]); + else + new_envp[i] = ft_strjoin("PWD=", getcwd(NULL, 0)); + } + return (new_envp); +} + +char **builtin_cd(char **arg, char **envp) +{ + /* + * OLDPWD = getcwd() + */ + envp = update_oldpwd(envp); + if (chdir(arg[1]) == -1) + { + perror("cd"); + return (envp); + } + envp = update_pwd(envp); + /* + * PWD = getcwd() + */ + return (envp); } diff --git a/srcs/builtins/env.c b/srcs/builtins/env.c index e74dea6..175eefb 100644 --- a/srcs/builtins/env.c +++ b/srcs/builtins/env.c @@ -8,7 +8,7 @@ void builtin_env(char *str, char **envp) (void)str; while (envp[i]) { - if (envp[i][0]) + if (envp[i][0] && ft_strchr(envp[i], '=') != NULL) printf("%s\n", envp[i]); i++; } diff --git a/srcs/builtins/exit.c b/srcs/builtins/exit.c index c9a88a5..38f6203 100644 --- a/srcs/builtins/exit.c +++ b/srcs/builtins/exit.c @@ -23,7 +23,7 @@ void bash_exit(int code) exit(code); } -void bash_exit_error(char *arg) +void bash_exit_errornum(char *arg) { ft_putendl_fd("exit", 2); ft_put_s_fd("minishell: exit: ", 2); @@ -44,6 +44,39 @@ static char *get_arg(char *str) return (&str[i]); } +int is_sep(char c) +{ + if ((c >= 9 && c <= 13) || c == ' ' || c == '\0') + return (1); + else + return (0); +} + +int count_arg(char *str) +{ + int count; + int i; + + count = 0; + i = 0; + while (str[i] && str[i + 1]) + { + if (!is_sep(str[i]) && is_sep(str[i + 1])) + count++; + i++; + } + return (count); +} + +void bash_exiterrorcount(void) +{ + ft_putendl_fd("exit", 2); + ft_putendl_fd("minishell: exit: too many arguments", 2); + /* + * SET EXIT CODE WITHOUT EXITING MINISHELL + */ +} + void builtin_exit(char *str, bool depth) { char *arg; @@ -51,6 +84,11 @@ void builtin_exit(char *str, bool depth) arg = get_arg(str); if (depth == true) { + if (count_arg(str) >= 2) + { + ft_putendl_fd("minishell: exit: too many arguments", 2); + exit (1); + } if (!ft_isnumeric(arg)) { ft_put_s_fd("minishell: exit: ", 2); @@ -60,8 +98,10 @@ void builtin_exit(char *str, bool depth) } exit(ft_atoi(arg)); } - if (ft_isnumeric(arg)) + if (count_arg(str) >= 2) + bash_exiterrorcount(); + else if (ft_isnumeric(arg)) bash_exit(ft_atoi(arg)); else - bash_exit_error(arg); + bash_exit_errornum(arg); } diff --git a/srcs/builtins/export.c b/srcs/builtins/export.c index 6d3f2ef..cb7c1d8 100644 --- a/srcs/builtins/export.c +++ b/srcs/builtins/export.c @@ -1 +1,89 @@ #include "../../includes/builtins.h" + +void check_format(char **arg) +{ + (void)arg; +} + +void print_error(char *str, int *code) +{ + ft_put_s_fd("minishell: export: ", 2); + ft_put_s_fd(str, 2); + ft_putendl_fd(": not a valid identifier", 2); + *code = 1; +} + +void print_arr(char **envp) +{ + int i; + char *equal; + + (void)equal; + i = -1; + while (envp[++i]) + { + ft_printf("declare -x %s\n", envp[i]); + } +} + +void print_export(char **envp) +{ + int i; + int j; + char *tmp; + int len; + + i = 0; + len = 0; + while (envp[len]) + len++; + while (i < len - 1) + { + j = 0; + while (j < len - i - 1) + { + if (ft_strncmp(envp[j], envp[j + 1], ft_strlen(envp[j])) > 0) + { + tmp = envp[j]; + envp[j] = envp[j + 1]; + envp[j + 1] = tmp; + } + j++; + } + i++; + } + print_arr(envp); +} + +char **builtin_export(char **arg, char **envp) +{ + char **new_envp; + int i; + // int code; + + i =0; + while (envp[i]) + i++; + new_envp = malloc(sizeof(char *) * i + 1); + new_envp[i] = NULL; + i = -1; + while (envp[++i]) + new_envp[i] = ft_strdup(envp[i]); + // code = 0; + i = 0; + while (arg[i]) + i++; + if (i == 1) + return(print_export(new_envp), envp); + return (envp); + // i = -1; + // while (arg[++i]) + // { + // if (check_format(arg[i])) + // { + // + // } + // else + // print_error(arg[i], &code); + // } +} diff --git a/srcs/main.c b/srcs/main.c index 1ad2e67..21cc003 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -57,6 +57,10 @@ int main(int ac, char **av, char **envp) builtin_env(input, 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); + if (ft_strncmp(input, "export", 6) == 0) + envp = builtin_export(ft_split(input, ' '), envp); free(input); } return (0);