Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions src/initialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 15:42:46 by dayano #+# #+# */
/* Updated: 2025/04/20 18:39:53 by ttsubo ### ########.fr */
/* Updated: 2025/04/29 16:18:49 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#include "main.h"

static void _split_key_value(char *env_val, char **key_out, char **val_out)
static int _split_key_value(char *env_val, char **key_out, char **val_out)
{
size_t env_len;
size_t eq_pos;
Expand All @@ -22,9 +22,31 @@ static void _split_key_value(char *env_val, char **key_out, char **val_out)
*key_out = NULL;
*val_out = NULL;
if (eq_pos == ft_strlen(env_val))
return ;
return (1);
*key_out = ft_substr(env_val, 0, eq_pos);
*val_out = ft_substr(env_val, eq_pos + 1, env_len - eq_pos - 1);
if (!*key_out || !*val_out)
return (1);
return (0);
}

static void _free_node(t_env *node)
{
free(node->key);
free(node->value);
free(node);
}

static void _free_nodes(t_env **head)
{
t_env *tmp;

while (*head)
{
tmp = (*head)->next;
_free_node(*head);
*head = tmp;
}
}

static t_env *create_envp_list(char **envp)
Expand All @@ -42,9 +64,8 @@ static t_env *create_envp_list(char **envp)
node = ft_calloc(1, sizeof(t_env));
if (!node)
return (NULL);
_split_key_value(envp[i], &(node->key), &(node->value));
if (!node->key || !node->value)
return (free(node), NULL);
if (_split_key_value(envp[i], &(node->key), &(node->value)))
return (_free_nodes(&head), _free_node(node), NULL);
node->next = NULL;
if (!head)
head = node;
Expand Down