From d9687a8e3a9a642010e0e3c9b24399483486f9d4 Mon Sep 17 00:00:00 2001 From: Loic Deridder Date: Sat, 15 Feb 2025 13:25:59 +0100 Subject: [PATCH] export += --- | 1 - includes/exec/builtins.h | 3 +++ includes/exec/env.h | 1 + srcs/builtins/export.c | 45 +++++++++++++++--------------------- srcs/builtins/export_utils.c | 41 ++++++++++++++++++++++++++++++++ srcs/env/var2.c | 27 ++++++++++++++++++++++ 6 files changed, 90 insertions(+), 28 deletions(-) delete mode 100644 create mode 100644 srcs/builtins/export_utils.c diff --git a/ b/ deleted file mode 100644 index f31fd07..0000000 --- a/ +++ /dev/null @@ -1 +0,0 @@ -Hello World tmp.txt diff --git a/includes/exec/builtins.h b/includes/exec/builtins.h index 7174b43..818fd9a 100644 --- a/includes/exec/builtins.h +++ b/includes/exec/builtins.h @@ -39,6 +39,9 @@ int builtin_export(char **arg, t_ast_n *head); //UTILS int count_char(char *str); +int is_append(char *str); +char **key_value(char *str); +void set_new_export(char *str, t_ast_n *node); int count_args(char **tab); int extractenv(char *str, char **envp); char *ft_getenv(char *str, char **envp); diff --git a/includes/exec/env.h b/includes/exec/env.h index f964792..b22f423 100644 --- a/includes/exec/env.h +++ b/includes/exec/env.h @@ -29,5 +29,6 @@ bool is_valid_key(char *key, t_msh *msh); int remove_env_var(char *key, t_msh *msh); void set_var_env(char *key, char *value, t_msh *msh); char **init_env(char **envp); +void append_var(char *key, char *add, t_msh *msh); #endif diff --git a/srcs/builtins/export.c b/srcs/builtins/export.c index 1f9c054..b96427a 100644 --- a/srcs/builtins/export.c +++ b/srcs/builtins/export.c @@ -15,28 +15,26 @@ int is_export_valid(char *str) { char *equal; + char *key_end; if (!ft_isalpha(*str) && str[0] != '_') return (0); equal = ft_strchr(str, '='); - if (!equal) + if (equal) { - while (*(++str)) - { - if (!ft_isalnum(*str) && *str != '_') - return (0); - } - return (1); + if (equal != str && *(equal - 1) == '+') + key_end = equal - 1; + else + key_end = equal; } else + key_end = str + ft_strlen(str); + while ((++str) < key_end) { - while (++str < equal) - { - if (!ft_isalnum(*str) && *str != '_') - return (0); - } - return (1); + if (!ft_isalnum(*str) && *str != '_') + return (0); } + return (1); } char **key_value(char *str) @@ -44,12 +42,17 @@ char **key_value(char *str) char **tmp; char *save; char *equal; + char *key_end; tmp = malloc(sizeof(char *) * (2 + 1)); if (!tmp) return (NULL); equal = ft_strchr(str, '='); - tmp[0] = ft_substr(str, 0, equal - str); + if (*(equal - 1) == '+') + key_end = equal - 1; + else + key_end = equal; + tmp[0] = ft_substr(str, 0, key_end - str); if (equal - str == 0) tmp[1] = ft_strdup(""); else @@ -109,7 +112,6 @@ int print_export(char **envp) int builtin_export(char **arg, t_ast_n *head) { int i; - char **tmp; i = 0; if (count_args(arg) == 1) @@ -117,18 +119,7 @@ int builtin_export(char **arg, t_ast_n *head) while (++i < count_args(arg)) { if (is_export_valid(arg[i])) - { - if (ft_strchr(arg[i], '=') != NULL) - { - tmp = key_value(arg[i]); - if (!tmp) - return (1); - set_var_env(tmp[0], tmp[1], head->msh); - free_tab(tmp); - } - else - set_var_env(arg[i], NULL, head->msh); - } + set_new_export(arg[i], head); else return (err_msg_cmd("export", arg[i], EXPRT_INV, 1)); } diff --git a/srcs/builtins/export_utils.c b/srcs/builtins/export_utils.c new file mode 100644 index 0000000..64d4830 --- /dev/null +++ b/srcs/builtins/export_utils.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* export_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lderidde +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/15 13:22:24 by lderidde #+# #+# */ +/* Updated: 2025/02/15 13:22:24 by lderidde ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../includes/minishell.h" + +int is_append(char *str) +{ + if (*(ft_strchr(str, '=') - 1) == '+') + return (1); + else + return (0); +} + +void set_new_export(char *str, t_ast_n *node) +{ + char **tmp; + + if (ft_strchr(str, '=') != NULL) + { + tmp = key_value(str); + if (!tmp) + return ; + if (is_append(str)) + append_var(tmp[0], tmp[1], node->msh); + else + set_var_env(tmp[0], tmp[1], node->msh); + free_tab(tmp); + } + else + set_var_env(str, NULL, node->msh); + +} diff --git a/srcs/env/var2.c b/srcs/env/var2.c index 9a474f9..3246fce 100644 --- a/srcs/env/var2.c +++ b/srcs/env/var2.c @@ -64,6 +64,33 @@ int remove_env_var(char *key, t_msh *msh) return (0); } +void append_var(char *key, char *add, t_msh *msh) +{ + int i; + char *tmp; + + tmp = NULL; + i = get_var_index(key, msh); + if (i != -1) + { + tmp = ft_strjoin(msh->env[i], add); + ft_free(&(msh->env[i])); + msh->env[i] = ft_strdup(tmp); + } + else + { + i = count_var(msh->env); + msh->env = copy_env_var(msh->env, i + 1); + if (!msh->env) + return ; + if (add) + tmp = ft_strjoin("=", add); + msh->env[i] = ft_strjoin(key, tmp); + } + ft_free(&tmp); + return ; +} + void set_var_env(char *key, char *value, t_msh *msh) { int i;