From 487a66394061f2d14a2fa421302966b5442d643f Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 27 Jul 2020 18:55:07 +0200 Subject: I couldn't see shit --- src/b_unset.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/b_unset.c (limited to 'src/b_unset.c') diff --git a/src/b_unset.c b/src/b_unset.c new file mode 100644 index 0000000..92db749 --- /dev/null +++ b/src/b_unset.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_unset.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 "s_struct.h" + +uint8_t + b_unset(char *args[], + t_msh *msh) +{ + (void)args; + (void)msh; + /* TODO: do unset */ + return (0); +} -- cgit v1.2.3 From 48acb01ff5a2cf22adebfc25213c2597911b2fd4 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 4 Aug 2020 13:55:12 +0200 Subject: Unset is fine --- src/b_unset.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 115 insertions(+), 4 deletions(-) (limited to 'src/b_unset.c') diff --git a/src/b_unset.c b/src/b_unset.c index 92db749..598db92 100644 --- a/src/b_unset.c +++ b/src/b_unset.c @@ -10,16 +10,127 @@ /* */ /* ************************************************************************** */ +#include #include +#include +#include "f_fail.h" +#include "s_lvars.h" #include "s_struct.h" +static t_bool + check_valid_identifier(const char arg[]) +{ + char *ptr; + t_bool r; + + ptr = (char*)arg; + r = TRUE; + if (ft_isalpha(ptr[0]) == 0) + { + r = FALSE; + } + if (ptr[0] == '_') + { + r = TRUE; + } + while (*ptr != '\0') + { + if (*ptr == '=') + { + r = FALSE; + } + ptr++; + } + return (r); +} + +static void + b_realloc_env(size_t skip, + t_msh *msh) +{ + char **nenvp; + int8_t skipped; + size_t i; + + i = 0; + while (msh->envp[i] != NULL) + i++; + if (!(nenvp = (char**)malloc(i * sizeof(char*)))) + f_fail_alloc_and_destroy(msh); + i = 0; + skipped = 0; + while (msh->envp[i] != NULL) + { + if (i == skip) + { + i += 1; + skipped = 1; + } + if (!(nenvp[i - skipped] = ft_strdup(msh->envp[i]))) + f_fail_alloc_and_destroy(msh); + i++; + } + nenvp[i - 1] = 0; + ft_delwords(msh->envp); + msh->envp = nenvp; +} + +static t_bool + b_removed_from_env(const char arg[], + t_msh *msh) +{ + size_t i; + + i = 0; + while (msh->envp[i] != NULL) + { + if (ft_strncmp(arg, msh->envp[i], ft_strlen(arg)) == 0) + { + b_realloc_env(i, msh); + return (TRUE); + } + i++; + } + return (FALSE); +} + +static void + b_remove_it(const char arg[], + t_msh *msh) +{ + if (b_removed_from_env(arg, msh) == FALSE) + { + lvars_delone(&msh->vars, arg); + } +} + uint8_t b_unset(char *args[], t_msh *msh) { - (void)args; - (void)msh; - /* TODO: do unset */ - return (0); + char **ptr; + t_bool next; + int8_t r; + + if (args[0] == NULL) + return (0); + r = 0; + ptr = args; + while (*ptr != NULL) + { + next = FALSE; + if (check_valid_identifier(*ptr) == FALSE) + { + f_fail_identifier("unset", *ptr); + next = TRUE; + r = 1; + } + if (next == FALSE) + { + b_remove_it(*ptr, msh); + } + ptr++; + } + return (r); } -- cgit v1.2.3 From d1ee0c0e55f75432da026f5e0e7cf15cee6918da Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 4 Aug 2020 14:28:35 +0200 Subject: New libft function strclen, bug fix --- src/b_unset.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/b_unset.c') diff --git a/src/b_unset.c b/src/b_unset.c index 598db92..03fd2a8 100644 --- a/src/b_unset.c +++ b/src/b_unset.c @@ -65,6 +65,8 @@ static void if (i == skip) { i += 1; + if (msh->envp[i] == NULL) + break ; skipped = 1; } if (!(nenvp[i - skipped] = ft_strdup(msh->envp[i]))) @@ -85,7 +87,7 @@ static t_bool i = 0; while (msh->envp[i] != NULL) { - if (ft_strncmp(arg, msh->envp[i], ft_strlen(arg)) == 0) + if (ft_strncmp(arg, msh->envp[i], ft_strclen(msh->envp[i], '=')) == 0) { b_realloc_env(i, msh); return (TRUE); -- cgit v1.2.3