norma arreglada

This commit is contained in:
marcnava-42cursus
2026-02-14 15:12:25 +01:00
parent 39e5719183
commit 77a704a09a
9 changed files with 315 additions and 255 deletions

View File

@@ -15,6 +15,17 @@
#include "variables.h"
#include "errors.h"
static char *resolve_key(
const char *name,
t_hashmap *environment,
t_minishell *minishell,
bool *owns_key
);
static char *resolve_value(
char *value,
t_minishell *minishell
);
/**
* @brief Retrieves the value of an environment variable from the shell's
* environment hashmap.
@@ -53,31 +64,22 @@ void set_env(
t_minishell *minishell
)
{
t_hashmap *environment;
char *key;
char *val;
char *old_value;
bool owns_key;
t_hashmap *environment;
char *key;
char *val;
char *old_value;
bool owns_key;
environment = minishell->variables.environment;
owns_key = false;
key = (char *)name;
if (name != NULL && !ft_hashmap_contains_key(environment, name))
{
key = ft_strdup(name);
if (key == NULL)
return (minishell->exit = true, malloc_error());
owns_key = true;
}
if (value == NULL)
val = ft_strdup("");
else
val = ft_strdup(value);
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 (minishell->exit = true, malloc_error());
return ;
}
old_value = ft_hashmap_put(environment, key, val);
if (old_value != NULL)
@@ -100,130 +102,48 @@ void unset_env(
{
t_hashmap *environment;
char *val;
environment = minishell->variables.environment;
val = ft_hashmap_remove(environment, (void *)name);
if (val != NULL)
free(val);
}
/**
* @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 minishell 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(
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 **envp;
t_list *env_list;
t_list *env;
t_map_entry *entry;
size_t i;
char *val;
env_list = ft_hashmap_entries(minishell->variables.environment);
envp = (char **)malloc(
(minishell->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 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 minishell 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 *minishell
)
{
t_hashmap **environment;
char *equal;
char *key;
char *value;
char *new_shlvl;
int shlvl;
size_t key_len;
if (minishell == NULL || envp == NULL)
return ;
environment = &minishell->variables.environment;
*environment = ft_hashmap_new(32, ft_hashmap_hashstr, ft_hashmap_strcmp);
if (*environment == NULL)
return ;
while (*envp != NULL)
{
equal = ft_strchr(*envp, '=');
if (equal == NULL)
{
envp++;
continue ;
}
key_len = (size_t)(equal - *envp);
key = ft_substr(*envp, 0, key_len);
value = ft_strdup(equal + 1);
if (key == NULL || value == NULL)
{
free(key);
free(value);
minishell->exit = true;
return (malloc_error());
}
set_env(key, value, minishell);
free(key);
free(value);
envp++;
}
value = get_env("SHLVL", minishell);
shlvl = 0;
if (value != NULL)
shlvl = ft_atoi(value);
if (shlvl < 0)
shlvl = 0;
new_shlvl = ft_itoa(shlvl + 1);
if (new_shlvl == NULL)
return (minishell->exit = true, malloc_error());
set_env("SHLVL", new_shlvl, minishell);
free(new_shlvl);
if (value == NULL)
val = ft_strdup("");
else
val = ft_strdup(value);
if (val == NULL)
return (minishell->exit = true, malloc_error(), NULL);
return (val);
}