From a485d8a2ba988df9d5433d2e13c326b2103e6412 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 28 Apr 2020 14:17:09 +0200 Subject: Removed useless function made obsolete by subst_var_value --- src/ft_b_cd.c | 4 +++- src/ft_u_utils.c | 20 -------------------- src/ft_u_utils.h | 1 - 3 files changed, 3 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/ft_b_cd.c b/src/ft_b_cd.c index cbb392a..7d4ef1a 100644 --- a/src/ft_b_cd.c +++ b/src/ft_b_cd.c @@ -16,6 +16,7 @@ #include "ft_s_struct.h" #include "ft_u_utils.h" +#include "ft_u_vars.h" /* static void */ /* ft_switch_env_var(char **envp) */ @@ -45,7 +46,8 @@ uint8_t if (argc == 0) { - path = ft_get_home_dir(msh->envp); + path = ft_subst_var_value("$HOME", msh); + ft_printf("%s\n", path); } if (chdir(path) != 0) { diff --git a/src/ft_u_utils.c b/src/ft_u_utils.c index 7e2eb53..b7ab7d8 100644 --- a/src/ft_u_utils.c +++ b/src/ft_u_utils.c @@ -26,23 +26,3 @@ uint64_t } return (argc); } - -char - *ft_get_home_dir(char **envp) -{ - char **ptr; - char *path; - - ptr = envp; - while (*ptr) - { - /* TODO: rework this correctly */ - if (ft_strncmp("HOME", *ptr, 4) == 0) - { - path = ft_substr(*ptr, 5, ft_strlen(*ptr + 5)); - return (path); - } - ptr++; - } - return (NULL); -} diff --git a/src/ft_u_utils.h b/src/ft_u_utils.h index d025cb2..1a3b324 100644 --- a/src/ft_u_utils.h +++ b/src/ft_u_utils.h @@ -16,6 +16,5 @@ #include uint64_t ft_get_argc(const char *args[]); -char *ft_get_home_dir(char *envp[]); #endif -- cgit v1.2.3 From 26debd1a134bfe6b4b400136c75b30d368a39252 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 28 Apr 2020 14:45:38 +0200 Subject: Ok cd --- src/ft_b_cd.c | 27 ++++++++++++++++++++++----- src/ft_e_builtins.c | 9 ++++++++- src/ft_e_lcom.c | 1 + src/ft_f_errno.c | 26 ++++++++++++++++++++++++++ src/ft_f_errno.h | 21 +++++++++++++++++++++ src/ft_f_fail.h | 2 ++ src/ft_m_redirs.c | 2 +- 7 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 src/ft_f_errno.c create mode 100644 src/ft_f_errno.h (limited to 'src') diff --git a/src/ft_b_cd.c b/src/ft_b_cd.c index 7d4ef1a..e36d60c 100644 --- a/src/ft_b_cd.c +++ b/src/ft_b_cd.c @@ -14,6 +14,8 @@ #include #include +#include "ft_f_fail.h" +#include "ft_s_destroy.h" #include "ft_s_struct.h" #include "ft_u_utils.h" #include "ft_u_vars.h" @@ -41,22 +43,37 @@ uint8_t ft_b_cd(char *args[], t_msh *msh) { + /* TODO: norme */ const uint64_t argc = ft_get_argc((const char**)args); char *path; - if (argc == 0) + if (argc >= 2) { - path = ft_subst_var_value("$HOME", msh); - ft_printf("%s\n", path); + ft_fail_too_many_args("cd", msh); + return (1); + } + else if (argc == 0) + { + if (!(path = ft_subst_var_value("$HOME", msh))) + return (1); + } + else + { + if (!(path = ft_strdup(*args))) + { + ft_s_destroy(msh); + ft_fail_alloc(msh); + } } if (chdir(path) != 0) { - /* TODO: handle cd failed */ + ft_f_dump_errno("cd", msh); + ft_memdel((void*)&path); + return (1); } /* TODO: ft_switch_env_var() */ ft_memdel((void*)&msh->cwd); msh->cwd = getcwd(NULL, 0); ft_memdel((void*)&path); - /* TODO: finish cd */ return (0); } diff --git a/src/ft_e_builtins.c b/src/ft_e_builtins.c index bc3eece..1045856 100644 --- a/src/ft_e_builtins.c +++ b/src/ft_e_builtins.c @@ -16,6 +16,7 @@ #include #include +#include "ft_b_builtins.h" #include "ft_m_redirs.h" #include "ft_s_destroy.h" #include "ft_s_lcom.h" @@ -37,6 +38,8 @@ static void static void ft_e_builtin_parent(pid_t pid, + const t_lcom *ptr, + uint8_t bu_id, t_msh *msh) { int32_t status; @@ -44,6 +47,10 @@ static void while (wait(&status) != pid) ; msh->ret = WEXITSTATUS(status); + if (bu_id == 1 && msh->ret == 0) + { + ft_b_cd(ptr->argv + 1, msh); + } } void @@ -64,6 +71,6 @@ void } else { - ft_e_builtin_parent(pid, msh); + ft_e_builtin_parent(pid, ptr, bu_id, msh); } } diff --git a/src/ft_e_lcom.c b/src/ft_e_lcom.c index 3288112..4a57f7b 100644 --- a/src/ft_e_lcom.c +++ b/src/ft_e_lcom.c @@ -11,6 +11,7 @@ /* ************************************************************************** */ #include + #include "ft_e_builtins.h" #include "ft_e_externs.h" #include "ft_s_struct.h" diff --git a/src/ft_f_errno.c b/src/ft_f_errno.c new file mode 100644 index 0000000..22ee7a9 --- /dev/null +++ b/src/ft_f_errno.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_f_errno.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include + +#include "ft_s_struct.h" + +void + ft_f_dump_errno(const char concern[], + t_msh *msh) +{ + ft_dprintf(STDERR_FILENO, "%s: %s: %s\n", + msh->shname, concern, strerror(errno)); +} diff --git a/src/ft_f_errno.h b/src/ft_f_errno.h new file mode 100644 index 0000000..86fce83 --- /dev/null +++ b/src/ft_f_errno.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_f_errno.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_F_ERRNO_H +#define FT_F_ERRNO_H + +#include "ft_s_struct.h" + +void ft_f_dump_errno(const char concern[], + t_msh *msh); + +#endif diff --git a/src/ft_f_fail.h b/src/ft_f_fail.h index 5091810..5a28b5e 100644 --- a/src/ft_f_fail.h +++ b/src/ft_f_fail.h @@ -13,6 +13,8 @@ #ifndef FT_F_FAIL_H #define FT_F_FAIL_H +#include "ft_f_errno.h" +#include "ft_f_redir.h" #include "ft_s_struct.h" void ft_fail_no_options(const char concern[], diff --git a/src/ft_m_redirs.c b/src/ft_m_redirs.c index bdb5b21..701a2a4 100644 --- a/src/ft_m_redirs.c +++ b/src/ft_m_redirs.c @@ -14,7 +14,7 @@ #include #include -#include "ft_f_redir.h" +#include "ft_f_fail.h" #include "ft_s_destroy.h" #include "ft_s_lcom.h" #include "ft_s_struct.h" -- cgit v1.2.3 From abec91e56b179d9d327da2d3d9ae960f5260e14f Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 28 Apr 2020 15:09:25 +0200 Subject: Nice functional cd --- src/ft_b_cd.c | 30 +++++++++--------------------- src/ft_e_builtins.c | 6 ++++-- src/ft_f_chdir.c | 27 +++++++++++++++++++++++++++ src/ft_f_chdir.h | 22 ++++++++++++++++++++++ src/ft_f_fail.h | 1 + src/ft_s_init.c | 1 + 6 files changed, 64 insertions(+), 23 deletions(-) create mode 100644 src/ft_f_chdir.c create mode 100644 src/ft_f_chdir.h (limited to 'src') diff --git a/src/ft_b_cd.c b/src/ft_b_cd.c index e36d60c..54dc92e 100644 --- a/src/ft_b_cd.c +++ b/src/ft_b_cd.c @@ -20,25 +20,6 @@ #include "ft_u_utils.h" #include "ft_u_vars.h" -/* static void */ -/* ft_switch_env_var(char **envp) */ -/* { */ -/* char **ptr; */ -/* char *path; */ - -/* ptr = envp; */ -/* while (*ptr) */ -/* { */ -/* if (ft_strncmp("HOME", *ptr, 4) == 0) */ -/* { */ -/* path = ft_substr(*ptr, 5, ft_strlen(*ptr + 5)); */ -/* return (path); */ -/* } */ -/* ptr++; */ -/* } */ -/* return (NULL); */ -/* } */ - uint8_t ft_b_cd(char *args[], t_msh *msh) @@ -64,14 +45,21 @@ uint8_t 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); + } + } } if (chdir(path) != 0) { - ft_f_dump_errno("cd", msh); + ft_fail_chd("cd", path, msh); ft_memdel((void*)&path); return (1); } - /* TODO: ft_switch_env_var() */ ft_memdel((void*)&msh->cwd); msh->cwd = getcwd(NULL, 0); ft_memdel((void*)&path); diff --git a/src/ft_e_builtins.c b/src/ft_e_builtins.c index 1045856..c78a80c 100644 --- a/src/ft_e_builtins.c +++ b/src/ft_e_builtins.c @@ -16,7 +16,6 @@ #include #include -#include "ft_b_builtins.h" #include "ft_m_redirs.h" #include "ft_s_destroy.h" #include "ft_s_lcom.h" @@ -49,8 +48,11 @@ static void msh->ret = WEXITSTATUS(status); if (bu_id == 1 && msh->ret == 0) { - ft_b_cd(ptr->argv + 1, msh); + 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); } void diff --git a/src/ft_f_chdir.c b/src/ft_f_chdir.c new file mode 100644 index 0000000..1386626 --- /dev/null +++ b/src/ft_f_chdir.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_f_chdir.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include + +#include "ft_s_struct.h" + +void + ft_fail_chd(const char concern[], + const char path[], + t_msh *msh) +{ + ft_dprintf(STDERR_FILENO, "%s: %s: %s: %s\n", + msh->shname, concern, path, strerror(errno)); +} diff --git a/src/ft_f_chdir.h b/src/ft_f_chdir.h new file mode 100644 index 0000000..fae82d7 --- /dev/null +++ b/src/ft_f_chdir.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_f_chdir.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_F_CHDIR_H +#define FT_F_CHDIR_H + +#include "ft_s_struct.h" + +void ft_fail_chd(const char concern[], + const char pathp[], + t_msh *msh); + +#endif diff --git a/src/ft_f_fail.h b/src/ft_f_fail.h index 5a28b5e..b176cbb 100644 --- a/src/ft_f_fail.h +++ b/src/ft_f_fail.h @@ -13,6 +13,7 @@ #ifndef FT_F_FAIL_H #define FT_F_FAIL_H +#include "ft_f_chdir.h" #include "ft_f_errno.h" #include "ft_f_redir.h" #include "ft_s_struct.h" diff --git a/src/ft_s_init.c b/src/ft_s_init.c index 46dac90..76bf72d 100644 --- a/src/ft_s_init.c +++ b/src/ft_s_init.c @@ -30,6 +30,7 @@ t_msh return (NULL); if (!(msh->shname = ft_strdup(argv[0]))) return (NULL); + /* TODO: shname: care about "./", try with symlinks */ msh->cwd = NULL; msh->cwd = getcwd(NULL, 0); /* TODO: handle getcwd failed */ -- cgit v1.2.3 From 86b25977265727217ec3360c3dc19b74f2bd448f Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 28 Apr 2020 15:49:58 +0200 Subject: exit is fine --- src/ft_b_cd.c | 38 ++++++++++++++++++++++---------------- src/ft_b_exit.c | 11 +++-------- src/ft_e_builtins.c | 17 ++++++++++++++--- 3 files changed, 39 insertions(+), 27 deletions(-) (limited to 'src') 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 #include #include +#include + #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 -- cgit v1.2.3 From 8b6c386883878eec839103305b20cb4d63eef873 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 28 Apr 2020 16:05:07 +0200 Subject: duped completely envp --- src/ft_s_init.c | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/ft_s_init.c b/src/ft_s_init.c index 76bf72d..463cb0b 100644 --- a/src/ft_s_init.c +++ b/src/ft_s_init.c @@ -11,6 +11,7 @@ /* ************************************************************************** */ #include +#include #include #include @@ -18,6 +19,45 @@ #include "ft_m_funptr.h" #include "ft_s_init.h" +static char + **ft_dupenv_del(char **nenvp, + uint64_t i) +{ + while (i > 0) + { + ft_memdel((void*)&nenvp[i]); + i--; + } + ft_memdel((void*)&nenvp); + return (NULL); +} + +static char + **ft_dupenv(char *envp[]) +{ + uint64_t i; + char **nenvp; + + i = 0; + while (envp[i]) + { + i++; + } + if (!(nenvp = (char**)malloc((i + 1) * sizeof(char*)))) + { + return (NULL); + } + i = 0; + while (envp[i]) + { + if (!(nenvp[i] = ft_strdup(envp[i]))) + return (ft_dupenv_del(nenvp, i)); + i++; + } + nenvp[i] = NULL; + return (nenvp); +} + t_msh *ft_init_msh(const char *argv[], char *envp[]) @@ -34,7 +74,9 @@ t_msh msh->cwd = NULL; msh->cwd = getcwd(NULL, 0); /* TODO: handle getcwd failed */ - msh->envp = envp; + msh->envp = NULL; + if (!(msh->envp = ft_dupenv(envp))) + return (NULL); msh->ret = 0; ft_init_buptr(msh); msh->curr = NULL; -- cgit v1.2.3 From 2c9ad3343cb47d9a83f39fd0077fadf98a0cf30b Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 28 Apr 2020 16:10:07 +0200 Subject: Ok argv, envp --- src/ft_m_argv.c | 2 +- src/ft_m_argv.h | 2 +- src/ft_s_init.c | 6 +++--- src/ft_s_init.h | 4 ++-- src/minishell.c | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/ft_m_argv.c b/src/ft_m_argv.c index 684d396..7b91fbb 100644 --- a/src/ft_m_argv.c +++ b/src/ft_m_argv.c @@ -20,7 +20,7 @@ uint8_t ft_m_argv(int argc, - const char *argv[], + char *const argv[], t_msh *msh) { /* TODO: better argv handling */ diff --git a/src/ft_m_argv.h b/src/ft_m_argv.h index 2f3260d..783c7e4 100644 --- a/src/ft_m_argv.h +++ b/src/ft_m_argv.h @@ -18,7 +18,7 @@ #include "ft_s_struct.h" uint8_t ft_m_argv(int argc, - const char *argv[], + char *const argv[], t_msh *msh); #endif diff --git a/src/ft_s_init.c b/src/ft_s_init.c index 463cb0b..26b196b 100644 --- a/src/ft_s_init.c +++ b/src/ft_s_init.c @@ -33,7 +33,7 @@ static char } static char - **ft_dupenv(char *envp[]) + **ft_dupenv(char *const envp[]) { uint64_t i; char **nenvp; @@ -59,8 +59,8 @@ static char } t_msh - *ft_init_msh(const char *argv[], - char *envp[]) + *ft_init_msh(char *const argv[], + char *const envp[]) { t_msh *msh; diff --git a/src/ft_s_init.h b/src/ft_s_init.h index e860925..bd8edc7 100644 --- a/src/ft_s_init.h +++ b/src/ft_s_init.h @@ -16,7 +16,7 @@ #include #include "ft_s_struct.h" -t_msh *ft_init_msh(const char *argv[], - char *envp[]); +t_msh *ft_init_msh(char *const argv[], + char *const envp[]); #endif diff --git a/src/minishell.c b/src/minishell.c index b5c07ed..96b51c8 100644 --- a/src/minishell.c +++ b/src/minishell.c @@ -23,8 +23,8 @@ int main(int argc, - const char *argv[], - char *envp[]) + char *const argv[], + char *const envp[]) { t_msh *msh; int32_t ret; -- cgit v1.2.3 From ed2f6193d141c6b8bab9ee916f74d11db517625b Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 28 Apr 2020 16:49:07 +0200 Subject: Trying vars --- src/ft_s_init.c | 1 + src/ft_s_struct.h | 8 ++++++++ 2 files changed, 9 insertions(+) (limited to 'src') diff --git a/src/ft_s_init.c b/src/ft_s_init.c index 26b196b..88a3817 100644 --- a/src/ft_s_init.c +++ b/src/ft_s_init.c @@ -80,5 +80,6 @@ t_msh msh->ret = 0; ft_init_buptr(msh); msh->curr = NULL; + msh->vars = NULL; return (msh); } diff --git a/src/ft_s_struct.h b/src/ft_s_struct.h index 38eeb43..5b99acb 100644 --- a/src/ft_s_struct.h +++ b/src/ft_s_struct.h @@ -26,6 +26,13 @@ ** 0: means no redirection */ +typedef struct s_vars +{ + char *name; + char *val; + struct s_vars *next; +} t_vars; + typedef struct s_lcom { char *com; @@ -46,6 +53,7 @@ typedef struct s_msh char **bu_ref; uint8_t (*bu_ptr[FT_BUILTINS_COUNT])(char **, struct s_msh*); struct s_lcom *curr; + struct s_vars *vars; } t_msh; #endif -- cgit v1.2.3