From: Lukáš Jiřiště Date: Tue, 27 Aug 2024 11:21:21 +0000 (+0200) Subject: Fix export return value and error output X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=db5ede78a3a6932dfff85d4d9865c240f1714449;p=42%2Fminishell.git Fix export return value and error output --- diff --git a/inc/minishell_structs.h b/inc/minishell_structs.h index 3cfc088..4b8b980 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/27 11:35:10 by ljiriste ### ########.fr */ +/* Updated: 2024/08/27 13:37:07 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,6 +52,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 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); int add_var(t_vec *vec, const char *key, const char *value); diff --git a/src/builtins/export.c b/src/builtins/export.c index 1189227..0c85934 100644 --- a/src/builtins/export.c +++ b/src/builtins/export.c @@ -6,13 +6,14 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/23 09:40:38 by ljiriste #+# #+# */ -/* Updated: 2024/08/26 11:47:56 by ljiriste ### ########.fr */ +/* Updated: 2024/08/27 13:37:21 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #include "builtins.h" #include "minishell_structs.h" #include +#include static int export_single(const char *var, t_execution_env *env) { @@ -21,8 +22,13 @@ static int export_single(const char *var, t_execution_env *env) const char *const_value; int res; + if (!is_assignment_word(var)) + { + ft_dprintf(STDERR_FILENO, "export: '%s': not a valid identifier\n", var); + return (1); + } name = get_var_name(var); - if (!name || !name[0] || (var[ft_strlen(name)] != '=' && var[ft_strlen(name)] != '\0')) + if (!name) { free(name); return (1); @@ -50,13 +56,15 @@ static int export_single(const char *var, t_execution_env *env) int export(__attribute__((unused)) int argc, char **argv, t_execution_env *env) { + int res; size_t i; + res = 0; i = 1; while (argv[i]) { - export_single(argv[i], env); + res = export_single(argv[i], env) || res; ++i; } - return (0); + return (res); } diff --git a/src/tokenization.c b/src/tokenization.c index befbd28..d41179d 100644 --- a/src/tokenization.c +++ b/src/tokenization.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/21 16:34:43 by ljiriste #+# #+# */ -/* Updated: 2024/08/26 16:16:11 by ljiriste ### ########.fr */ +/* Updated: 2024/08/27 13:36:27 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -96,38 +96,6 @@ enum token_types RPARA, }; -static int is_assignment_word(const char *str) -{ - size_t i; - size_t j; - - 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] == '=') - { - j = 0; - if (ft_isdigit(str[0])) - return (0); - while (j < i) - { - if (!ft_isalnum(str[j]) && str[j] != '_') - return (0); - ++j; - } - return (1); - } - ++i; - } - return (0); -} - int only_contains_digits(const char *str) { while (ft_isdigit(*str)) diff --git a/src/vars.c b/src/vars.c index bebd8bf..a59e40e 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/26 09:20:20 by ljiriste ### ########.fr */ +/* Updated: 2024/08/27 13:36:47 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,44 @@ #include "libft.h" #include +static int is_valid_var_name(const char *str, size_t size) +{ + size_t j; + + j = 0; + if (!str || size == 0) + return (0); + if (ft_isdigit(str[0])) + return (0); + while (j < size) + { + if (!ft_isalnum(str[j]) && str[j] != '_') + return (0); + ++j; + } + return (1); +} + +int is_assignment_word(const char *str) +{ + size_t i; + + 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); +} + int add_var_line(t_vec *vec, const char *line) { char *tmp;