diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-04-28 15:49:58 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-04-28 15:49:58 +0200 |
commit | 86b25977265727217ec3360c3dc19b74f2bd448f (patch) | |
tree | eefc1440e2ae18faaab0e5409dac58bfef893813 | |
parent | Nice functional cd (diff) | |
download | 42-minishell-86b25977265727217ec3360c3dc19b74f2bd448f.tar.gz 42-minishell-86b25977265727217ec3360c3dc19b74f2bd448f.tar.bz2 42-minishell-86b25977265727217ec3360c3dc19b74f2bd448f.tar.xz 42-minishell-86b25977265727217ec3360c3dc19b74f2bd448f.tar.zst 42-minishell-86b25977265727217ec3360c3dc19b74f2bd448f.zip |
exit is fine
Diffstat (limited to '')
-rw-r--r-- | src/ft_b_cd.c | 38 | ||||
-rw-r--r-- | src/ft_b_exit.c | 11 | ||||
-rw-r--r-- | src/ft_e_builtins.c | 17 |
3 files changed, 39 insertions, 27 deletions
diff --git a/src/ft_b_cd.c b/src/ft_b_cd.c index 54dc92e..fbd0282 100644 --- a/src/ft_b_cd.c +++ b/src/ft_b_cd.c @@ -20,11 +20,31 @@ #include "ft_u_utils.h" #include "ft_u_vars.h" +static void + ft_set_path(char **path, + char *args[], + t_msh *msh) +{ + if (!(*path = ft_strdup(*args))) + { + ft_s_destroy(msh); + ft_fail_alloc(msh); + } + if (!ft_strncmp("~/", *path, 2) || !ft_strncmp("~", *path, 2)) + { + if (!(*path = ft_strsubst(*path, + "~", ft_subst_var_value("$HOME", msh)))) + { + ft_s_destroy(msh); + ft_fail_alloc(msh); + } + } +} + uint8_t ft_b_cd(char *args[], t_msh *msh) { - /* TODO: norme */ const uint64_t argc = ft_get_argc((const char**)args); char *path; @@ -39,21 +59,7 @@ uint8_t return (1); } else - { - if (!(path = ft_strdup(*args))) - { - ft_s_destroy(msh); - ft_fail_alloc(msh); - } - if (!ft_strncmp("~/", path, 2) || !ft_strncmp("~", path, 2)) - { - if (!(path = ft_strsubst(path, "~", ft_subst_var_value("$HOME", msh)))) - { - ft_s_destroy(msh); - ft_fail_alloc(msh); - } - } - } + ft_set_path(&path, args, msh); if (chdir(path) != 0) { ft_fail_chd("cd", path, msh); diff --git a/src/ft_b_exit.c b/src/ft_b_exit.c index ae421cf..7a7e5e8 100644 --- a/src/ft_b_exit.c +++ b/src/ft_b_exit.c @@ -13,6 +13,8 @@ #include <libft.h> #include <stdlib.h> #include <stdint.h> +#include <unistd.h> + #include "ft_f_fail.h" #include "ft_s_lcom.h" #include "ft_s_destroy.h" @@ -36,13 +38,6 @@ uint8_t ret = ft_atoi(args[0]); /* TODO: non numeric args[0] */ } - else - { - ret = msh->ret; - } - ft_lcom_clear(&msh->curr); - ft_s_destroy(msh); - ft_printf("exit\n"); - exit(ret); + ft_dprintf(STDERR_FILENO, "exit\n"); return (0); } diff --git a/src/ft_e_builtins.c b/src/ft_e_builtins.c index c78a80c..382f85d 100644 --- a/src/ft_e_builtins.c +++ b/src/ft_e_builtins.c @@ -42,17 +42,28 @@ static void t_msh *msh) { int32_t status; + int32_t ret; while (wait(&status) != pid) ; - msh->ret = WEXITSTATUS(status); + ret = WEXITSTATUS(status); + if (bu_id != 6) + msh->ret = ret; if (bu_id == 1 && msh->ret == 0) { msh->bu_ptr[bu_id](ptr->argv + 1, msh); /* TODO: export $PWD */ } - else if (bu_id == 6) - msh->bu_ptr[bu_id](ptr->argv + 1, msh); + else if (bu_id == 6 && ret == 0) + { + if (ptr->argv[1]) + ret = ft_atoi(ptr->argv[1]); + else + ret = msh->ret; + ft_lcom_clear(&msh->curr); + ft_s_destroy(msh); + exit(ret); + } } void |