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 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/ft_b_cd.c') 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) { -- 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 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'src/ft_b_cd.c') 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); } -- 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 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) (limited to 'src/ft_b_cd.c') 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); -- 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 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) (limited to 'src/ft_b_cd.c') 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); -- cgit v1.2.3