/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   environment_get_arr.c                              :+:    :+:            */
/*                                                     +:+                    */
/*   By: willem <willem@student.codam.nl>             +#+                     */
/*                                                   +#+                      */
/*   Created: 2025/02/08 13:59:48 by willem        #+#    #+#                 */
/*   Updated: 2025/02/26 16:09:20 by whaffman      ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include "minishell.h"

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

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