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/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);