/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* environment.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: sede-san variables.environment, name)); } /** * @brief Sets an environment variable in the minishell's environment hashmap. * * This function adds a new environment variable or updates an existing one * in the minishell's environment hashmap. If the variable already exists, * the old value is freed to prevent memory leaks. If the variable is new, * a duplicate of the key name is created for storage. * * @param name The name of the environment variable to set * @param value The value to assign to the environment variable * @param minishell Pointer to the minishell object. */ void set_env( const char *name, char *value, t_minishell *minishell ) { t_hashmap *environment; char *key; char *val; char *old_value; bool owns_key; environment = minishell->variables.environment; key = resolve_key(name, environment, minishell, &owns_key); if (key == NULL) return ; val = resolve_value(value, minishell); if (val == NULL) { if (owns_key) free(key); return ; } old_value = ft_hashmap_put(environment, key, val); if (old_value != NULL) free(old_value); } /** * @brief Removes an environment variable by name. * * @param name The name of the environment variable to remove. * @param minishell Pointer to the minishell structure. * * @note If the environment variable exists, it will be removed from the hashmap * and its associated value will be freed. */ void unset_env( const char *name, t_minishell *minishell ) { t_hashmap *environment; char *val; environment = minishell->variables.environment; val = ft_hashmap_remove(environment, (void *)name); if (val != NULL) free(val); } static char *resolve_key( const char *name, t_hashmap *environment, t_minishell *minishell, bool *owns_key ) { char *key; key = (char *)name; *owns_key = false; if (name == NULL) return (NULL); if (!ft_hashmap_contains_key(environment, name)) { key = ft_strdup(name); if (key == NULL) return (minishell->exit = true, malloc_error(), NULL); *owns_key = true; } return (key); } static char *resolve_value( char *value, t_minishell *minishell ) { char *val; if (value == NULL) val = ft_strdup(""); else val = ft_strdup(value); if (val == NULL) return (minishell->exit = true, malloc_error(), NULL); return (val); }