/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   environment_get_arr.c                              :+:    :+:            */
/*                                                     +:+                    */
/*   By: willem <willem@student.codam.nl>             +#+                     */
/*                                                   +#+                      */
/*   Created: 2025/02/08 13:59:48 by willem        #+#    #+#                 */
/*   Updated: 2025/03/04 10:44:41 by whaffman      ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include "minishell.h"

static char	*create_variable(t_minishell *msh, t_environment *env)
{
	char	*var;
	size_t	len;

	len = ft_strlen(env->name) + ft_strlen(env->value) + 2;
	var = malloc_safe(msh, len);
	ft_strlcpy(var, env->name, len);
	ft_strlcat(var, "=", len);
	ft_strlcat(var, env->value, len);
	return (var);
}

char	**environment_get_arr(t_minishell *msh)
{
	char			**arr;
	int				i;
	t_list			*env;

	env = msh->environment;
	arr = malloc_safe(msh,
			sizeof(char *) * (ft_lstsize(env) + 1));
	i = 0;
	while (env != NULL)
	{
		if (((t_environment *)env->content)->value != NULL
			&& ((t_environment *)env->content)->name != NULL)
		{
			arr[i] = create_variable(msh, (t_environment *)env->content);
			i++;
		}
		env = env->next;
	}
	arr[i] = NULL;
	return (arr);
}
