update: added hashmap functionality to environment variables

This commit is contained in:
2025-12-01 13:47:17 +01:00
parent 214d7c24d2
commit daad208a00
3 changed files with 199 additions and 4 deletions

View File

@@ -6,7 +6,7 @@
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/22 19:10:13 by sede-san #+# #+# */
/* Updated: 2025/10/30 16:05:48 by sede-san ### ########.fr */
/* Updated: 2025/12/01 13:33:37 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
@@ -34,7 +34,7 @@ typedef struct s_command t_command;
*/
typedef struct s_variables
{
char **environment;
t_hashmap *environment;
// char **internal;
} t_variables;
@@ -82,4 +82,17 @@ extern u_int8_t minishell_run(t_minishell *minishell);
extern void minishell_clear(t_minishell *minishell);
/* environment.c */
extern void set_envp(char **envp, t_minishell *msh);
extern void set_env(const char *env_name, char *env_value,
t_minishell *msh);
extern char **get_envp(t_minishell *msh);
extern void free_envp(char **envp);
extern char *get_env(const char *env_name, t_minishell *msh);
#endif /* CORE_H */

View File

@@ -6,7 +6,7 @@
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/20 20:51:33 by sede-san #+# #+# */
/* Updated: 2025/10/30 22:36:03 by sede-san ### ########.fr */
/* Updated: 2025/12/01 13:38:35 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,7 +17,11 @@ int minishell_init(
char **envp
){
ft_bzero(minishell, sizeof(t_minishell));
minishell->variables.environment = envp;
minishell->variables.environment
= ft_hashmap_new(32, ft_hashmap_hashstr, ft_hashmap_strcmp);
set_envp(envp, minishell);
if (minishell->variables.environment == NULL)
return (0);
return (1);
}
@@ -46,5 +50,6 @@ void minishell_clear(
t_minishell *minishell
){
rl_clear_history();
ft_hashmap_clear(&minishell->variables.environment, free);
ft_bzero(minishell, sizeof(t_minishell));
}

View File

@@ -0,0 +1,177 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* environment.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/01 09:12:39 by sede-san #+# #+# */
/* Updated: 2025/12/01 13:01:56 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
/**
* @brief Parses and stores environment variables from envp array into a hashmap
*
* This function iterates through the environment variables array (envp) and
* splits each variable string on the '=' delimiter to separate the variable
* name from its value. Each name-value pair is then stored in the minishell's
* environment hashmap for later retrieval.
*
* @param envp Array of environment variable strings in "NAME=value" format
* @param msh Pointer to the minishell structure containing the environment
* hashmap
*
* @note The function assumes envp strings are in the standard format
* "NAME=value"
*/
void set_envp(
char **envp,
t_minishell *msh
) {
char **splitted_env;
if (msh == NULL)
{
ft_hashmap_clear(&msh->variables.environment, free);
return ;
}
while (*envp != NULL)
{
splitted_env = ft_split(*envp, '=');
if (splitted_env == NULL)
{
ft_hashmap_clear(&msh->variables.environment, free);
return ;
}
ft_hashmap_put(msh->variables.environment,
ft_strdup(splitted_env[0]), ft_strdup(splitted_env[1]));
ft_free_split(splitted_env);
envp++;
}
}
/**
* @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 env_name The name of the environment variable to set
* @param env_value The value to assign to the environment variable
* @param msh Pointer to the minishell structure containing the
* environment hashmap
*/
void set_env(
const char *env_name,
char *env_value,
t_minishell *msh
) {
t_hashmap *environment;
const char *key;
char *old_value;
environment = msh->variables.environment;
key = env_name;
if (!ft_hashmap_contains_key(environment, key))
key = ft_strdup(env_name);
old_value = ft_hashmap_put(environment, key, ft_strdup(env_value));
if (old_value != NULL)
free(old_value);
}
/**
* @brief Converts the environment variables hashmap to an envp array format.
*
* This function extracts all environment variables from the minishell's
* environment hashmap and converts them into a NULL-terminated array of
* strings in the format "KEY=VALUE".
*
* @param msh Pointer to the minishell structure containing the environment
* variables hashmap.
*
* @return A dynamically allocated array of strings representing environment
* variables in "KEY=VALUE" format, terminated by NULL. Returns NULL
* if memory allocation fails. The caller is responsible for freeing
* the returned array and its individual string elements using
* the `free_envp()` function.
*
* @note The function allocates memory for both the array and individual
* strings using malloc and ft_strnjoin respectively.
* @note The returned array size is environment->size + 1 to accommodate
* the NULL terminator.
*/
char **get_envp(
t_minishell *msh
) {
char **envp;
t_list *env_list;
t_list *env;
t_map_entry *entry;
size_t i;
env_list = ft_hashmap_entries(msh->variables.environment);
envp = (char **)malloc(
(msh->variables.environment->size + 1) * sizeof(char *));
if (envp != NULL)
{
i = 0;
env = env_list;
while (env != NULL)
{
entry = env->content;
envp[i++] = ft_strnjoin(3, entry->key, "=", entry->value);
env = env->next;
}
envp[i] = NULL;
}
ft_lstclear_nodes(&env_list);
return (envp);
}
/**
* @brief Frees a dynamically allocated environment variables array
*
* This function deallocates memory for an array of strings that was previously
* allocated by `get_envp()`. It iterates through each string in the array,
* frees the memory for individual strings, and then frees the array itself.
*
* @param envp Pointer to the array of environment variable strings to be freed.
* Each string in the array should be dynamically allocated.
* The array must be NULL-terminated.
*/
void free_envp(
char **envp
) {
size_t i;
i = -1;
while (envp[++i] != NULL)
free(envp[i]);
free(envp);
}
/**
* @brief Retrieves the value of an environment variable from the shell's
* environment hashmap.
*
* This function searches for the specified environment variable name in the
* minishell's environment variable hashmap and returns its associated value.
*
* @param env_name The name of the environment variable to retrieve (e.g.,
* "PATH", "HOME")
* @param msh Pointer to the minishell structure containing the environment
* variables hashmap
*
* @return The value of the environment variable if found, NULL if not found
*/
char *get_env(
const char *env_name,
t_minishell *msh
) {
return (ft_hashmap_get(msh->variables.environment, env_name));
}