diff options
| author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-08-04 13:55:12 +0200 | 
|---|---|---|
| committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-08-04 13:55:12 +0200 | 
| commit | 48acb01ff5a2cf22adebfc25213c2597911b2fd4 (patch) | |
| tree | d19162a4fad16be55ea90d7e661866224bdd3de7 | |
| parent | Export is working (diff) | |
| download | 42-minishell-48acb01ff5a2cf22adebfc25213c2597911b2fd4.tar.gz 42-minishell-48acb01ff5a2cf22adebfc25213c2597911b2fd4.tar.bz2 42-minishell-48acb01ff5a2cf22adebfc25213c2597911b2fd4.tar.xz 42-minishell-48acb01ff5a2cf22adebfc25213c2597911b2fd4.tar.zst 42-minishell-48acb01ff5a2cf22adebfc25213c2597911b2fd4.zip | |
Unset is fine
Diffstat (limited to '')
| -rw-r--r-- | src/b_export.c | 23 | ||||
| -rw-r--r-- | src/b_unset.c | 119 | ||||
| -rw-r--r-- | src/f_fail.c | 7 | ||||
| -rw-r--r-- | src/f_fail.h | 4 | 
4 files changed, 131 insertions, 22 deletions
| diff --git a/src/b_export.c b/src/b_export.c index 43e1c7d..fa9a6d1 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -26,7 +26,7 @@  #include "u_vars.h"  static t_bool -	check_valid_identifier(const char *arg) +	check_valid_identifier(const char arg[])  {  	char	*ptr; @@ -47,7 +47,9 @@ static t_bool  	while (*ptr != '\0')  	{  		if (*ptr == '=') +		{  			return (TRUE); +		}  		ptr++;  	}  	return (FALSE); @@ -86,25 +88,24 @@ uint8_t  			t_msh *msh)  {  	/* TODO: norme */ -	const uint64_t	argc = u_builtins_get_argc((const char**)args); -	char			**ptr; -	char			*varval; -	char			fmt[4096]; -	t_bool			next; -	uint8_t			r; +	char	**ptr; +	char	*varval; +	char	fmt[4096]; +	t_bool	next; +	uint8_t	r; -	if (argc == 0) +	if (args[0] == NULL)  	{  		return (b_env(NULL, msh));  	} -	ptr = args;  	r = 0; -	while (*ptr) +	ptr = args; +	while (*ptr != NULL)  	{  		next = FALSE;  		if (check_valid_identifier(*ptr) == FALSE)  		{ -			f_fail_identifier("export", *ptr, msh); +			f_fail_identifier("export", *ptr);  			next = TRUE;  			r = 1;  		} 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 <libft.h>  #include <stdint.h> +#include <stdlib.h> +#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);  } diff --git a/src/f_fail.c b/src/f_fail.c index 5e6824f..3704b1f 100644 --- a/src/f_fail.c +++ b/src/f_fail.c @@ -36,11 +36,10 @@ void  void  	f_fail_identifier(const char concern[], -					const char identifier[], -					t_msh *msh) +					const char identifier[])  { -	ft_dprintf(STDERR_FILENO, "%s: %s: `%s': not a valid identifier\n", -		msh->shname, concern, identifier); +	ft_dprintf(STDERR_FILENO, "minishell: %s: `%s': not a valid identifier\n", +			concern, identifier);  }  void diff --git a/src/f_fail.h b/src/f_fail.h index 9a4b37c..0d3c19b 100644 --- a/src/f_fail.h +++ b/src/f_fail.h @@ -22,8 +22,6 @@  void	f_fail_no_options(const char concern[], t_msh *msh);  void	f_fail_too_many_args(const char concern[], t_msh *msh); -void	f_fail_identifier(const char concern[], -						const char identifier[], -						t_msh *msh); +void	f_fail_identifier(const char concern[], const char identifier[]);  #endif | 
