From: Lukas Jiriste Date: Fri, 30 Aug 2024 07:20:56 +0000 (+0200) Subject: Fix export var validation X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=3830d5cfcb23473c6778f3ae4bc59867aacb64b8;p=42%2Fminishell.git Fix export var validation Assignment word recognition was changed so that export can reuse part of its logic. --- diff --git a/inc/minishell_structs.h b/inc/minishell_structs.h index e3894c7..0ebdff9 100644 --- a/inc/minishell_structs.h +++ b/inc/minishell_structs.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/26 09:08:46 by ljiriste #+# #+# */ -/* Updated: 2024/08/28 09:36:55 by ljiriste ### ########.fr */ +/* Updated: 2024/08/30 09:16:51 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,6 +54,7 @@ int init_vars(t_vars *vars, char **envp); int init_env(t_execution_env *env, char **envp); int init_tree(t_tree *tree); +int has_valid_name(const char *str); int is_assignment_word(const char *str); int cmp_var_name(const char *a, const char *b); int add_var_line(t_vec *vec, const char *line); diff --git a/src/builtins/export.c b/src/builtins/export.c index 0c85934..c919a3e 100644 --- a/src/builtins/export.c +++ b/src/builtins/export.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/23 09:40:38 by ljiriste #+# #+# */ -/* Updated: 2024/08/27 13:37:21 by ljiriste ### ########.fr */ +/* Updated: 2024/08/30 09:16:28 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ static int export_single(const char *var, t_execution_env *env) const char *const_value; int res; - if (!is_assignment_word(var)) + if (!has_valid_name(var)) { ft_dprintf(STDERR_FILENO, "export: '%s': not a valid identifier\n", var); return (1); diff --git a/src/vars.c b/src/vars.c index a59e40e..d9b445b 100644 --- a/src/vars.c +++ b/src/vars.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/02 13:21:32 by ljiriste #+# #+# */ -/* Updated: 2024/08/27 13:36:47 by ljiriste ### ########.fr */ +/* Updated: 2024/08/30 09:14:10 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,42 +14,34 @@ #include "libft.h" #include -static int is_valid_var_name(const char *str, size_t size) +// This function returns 0 when str does not contain valid variable name +// in front of = else it returns the length of the valid name +int has_valid_name(const char *str) { - size_t j; + size_t i; - j = 0; - if (!str || size == 0) + if (!str) return (0); if (ft_isdigit(str[0])) return (0); - while (j < size) + i = 0; + while (str[i]) { - if (!ft_isalnum(str[j]) && str[j] != '_') + if (str[i] == '=') + return (i); + if (!ft_isalnum(str[i]) && str[i] != '_') return (0); - ++j; + ++i; } - return (1); + return (i); } int is_assignment_word(const char *str) { - size_t i; + size_t len; - i = 0; - while (str[i]) - { - if (str[i] == '"') - while (str[i] && str[i] != '"') - ++i; - else if (str[i] == '\'') - while (str[i] && str[i] != '\'') - ++i; - else if (str[i] == '=') - return (is_valid_var_name(str, i)); - ++i; - } - return (0); + len = has_valid_name(str); + return (len > 0 && str[len] == '='); } int add_var_line(t_vec *vec, const char *line)