From f441e00ea60a5e485d853d5dd3ac99a8535a40cc Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 30 Jul 2020 18:22:32 +0200 Subject: In progress --- src/b_export_next.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/b_export_next.c (limited to 'src/b_export_next.c') diff --git a/src/b_export_next.c b/src/b_export_next.c new file mode 100644 index 0000000..44d2b9b --- /dev/null +++ b/src/b_export_next.c @@ -0,0 +1,12 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_export_next.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + -- cgit v1.2.3 From 6ca882c8feb9fb88cf8e1421e882fb7e9efb97f8 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 3 Aug 2020 16:50:32 +0200 Subject: In progress --- src/b_export_next.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/b_export_next.c') diff --git a/src/b_export_next.c b/src/b_export_next.c index 44d2b9b..2641f65 100644 --- a/src/b_export_next.c +++ b/src/b_export_next.c @@ -10,3 +10,14 @@ /* */ /* ************************************************************************** */ +#include + +#include "s_struct.h" + +void + b_export_with_equals(const char arg[], + t_msh *msh) +{ + (void)arg; + (void)msh; +} -- cgit v1.2.3 From 1886374416e5dc436db68e3518603258ae32bac0 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 3 Aug 2020 17:26:41 +0200 Subject: Splitted var --- src/b_export_next.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'src/b_export_next.c') diff --git a/src/b_export_next.c b/src/b_export_next.c index 2641f65..f0bb804 100644 --- a/src/b_export_next.c +++ b/src/b_export_next.c @@ -11,13 +11,40 @@ /* ************************************************************************** */ #include +#include +#include "b_export_next.h" +#include "f_fail.h" #include "s_struct.h" +static char + **b_get_var(const char arg[], + t_msh *msh) +{ + size_t len; + char **var; + + if ((var = (char**)malloc(3 * sizeof(char*))) == NULL) + f_fail_alloc_and_destroy(msh); + len = 0; + while (arg[len] != '=' && arg[len] != '\0') + len++; + len += 1; + if ((var[FT_VAR_NAME] = (char*)malloc((len) * sizeof(char))) == NULL) + f_fail_alloc_and_destroy(msh); + ft_strlcpy(var[FT_VAR_NAME], arg, len); + if ((var[FT_VAR_VAL] = ft_strdup(arg + len)) == NULL) + f_fail_alloc_and_destroy(msh); + var[FT_VAR_NULL] = NULL; + return (var); +} + void b_export_with_equals(const char arg[], t_msh *msh) { - (void)arg; - (void)msh; + char **var; + + var = b_get_var(arg, msh); + ft_delwords(var); } -- cgit v1.2.3 From c3d540a4d1627ba8e4c140ba9aa76b85333d7a07 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 3 Aug 2020 17:30:33 +0200 Subject: Added $ for varname --- src/b_export_next.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/b_export_next.c') diff --git a/src/b_export_next.c b/src/b_export_next.c index f0bb804..683fdd0 100644 --- a/src/b_export_next.c +++ b/src/b_export_next.c @@ -30,9 +30,10 @@ static char while (arg[len] != '=' && arg[len] != '\0') len++; len += 1; - if ((var[FT_VAR_NAME] = (char*)malloc((len) * sizeof(char))) == NULL) + if ((var[FT_VAR_NAME] = (char*)malloc((len + 1) * sizeof(char))) == NULL) f_fail_alloc_and_destroy(msh); - ft_strlcpy(var[FT_VAR_NAME], arg, len); + var[FT_VAR_NAME][0] = '$'; + ft_strlcpy(var[FT_VAR_NAME] + 1, arg, len); if ((var[FT_VAR_VAL] = ft_strdup(arg + len)) == NULL) f_fail_alloc_and_destroy(msh); var[FT_VAR_NULL] = NULL; @@ -46,5 +47,6 @@ void char **var; var = b_get_var(arg, msh); + ft_printf("[%s] - [%s]\n", var[FT_VAR_NAME], var[FT_VAR_VAL]); ft_delwords(var); } -- cgit v1.2.3 From bfaf4c53a8406ea0bfbd8699807b567a5322a03f Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 3 Aug 2020 19:00:47 +0200 Subject: Export is working --- src/b_export_next.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) (limited to 'src/b_export_next.c') diff --git a/src/b_export_next.c b/src/b_export_next.c index 683fdd0..0c143b2 100644 --- a/src/b_export_next.c +++ b/src/b_export_next.c @@ -15,7 +15,9 @@ #include "b_export_next.h" #include "f_fail.h" +#include "s_lvars.h" #include "s_struct.h" +#include "u_vars.h" static char **b_get_var(const char arg[], @@ -40,13 +42,73 @@ static char return (var); } +static int64_t + b_is_it_in_env(const char varname[], + t_msh *msh) +{ + char **p_env; + + p_env = msh->envp; + while (*p_env != NULL) + { + if (ft_strncmp(varname, *p_env, ft_strlen(varname)) == 0) + { + return (p_env - msh->envp); + } + p_env++; + } + return (-1); +} + +static void + b_add_to_env(const char arg[], + t_msh *msh) +{ + size_t i; + char **nenvp; + + i = 0; + while (msh->envp[i] != NULL) + i++; + if (!(nenvp = (char**)malloc((i + 2) * sizeof(char*)))) + f_fail_alloc_and_destroy(msh); + i = 0; + while (msh->envp[i] != NULL) + { + if (!(nenvp[i] = ft_strdup(msh->envp[i]))) + f_fail_alloc_and_destroy(msh); + i++; + } + if (!(nenvp[i] = ft_strdup(arg))) + f_fail_alloc_and_destroy(msh); + nenvp[i + 1] = 0; + ft_delwords(msh->envp); + msh->envp = nenvp; +} + void b_export_with_equals(const char arg[], t_msh *msh) { + char *varval; char **var; + int64_t env_i; var = b_get_var(arg, msh); - ft_printf("[%s] - [%s]\n", var[FT_VAR_NAME], var[FT_VAR_VAL]); + if ((env_i = b_is_it_in_env(var[FT_VAR_NAME] + 1, msh)) != -1) + { + ft_memdel((void*)&msh->envp[env_i]); + if ((msh->envp[env_i] = ft_strdup(arg)) == NULL) + f_fail_alloc_and_destroy(msh); + } + else if ((varval = u_get_cstm_vr(var[FT_VAR_NAME], msh)) != NULL) + { + b_add_to_env(arg, msh); + lvars_delone(&msh->vars, var[FT_VAR_NAME] + 1); + } + else + { + b_add_to_env(arg, msh); + } ft_delwords(var); } -- cgit v1.2.3 From 647a984ac6b6e2629ad16d3892ad3c231e8e277a Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 4 Aug 2020 14:44:55 +0200 Subject: Huge bug fix --- src/b_export_next.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/b_export_next.c') diff --git a/src/b_export_next.c b/src/b_export_next.c index 0c143b2..0a56968 100644 --- a/src/b_export_next.c +++ b/src/b_export_next.c @@ -51,7 +51,7 @@ static int64_t p_env = msh->envp; while (*p_env != NULL) { - if (ft_strncmp(varname, *p_env, ft_strlen(varname)) == 0) + if (ft_strncmp(varname, *p_env, ft_strclen(*p_env, '=')) == 0) { return (p_env - msh->envp); } -- cgit v1.2.3