/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   executor_create_pipes.c                            :+:    :+:            */
/*                                                     +:+                    */
/*   By: willem <willem@student.codam.nl>             +#+                     */
/*                                                   +#+                      */
/*   Created: 2025/02/12 21:25:22 by willem        #+#    #+#                 */
/*   Updated: 2025/02/26 16:10:19 by whaffman      ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include "minishell.h"

/**
 * @brief Creates pipes for the executor in the minishell.
 *
 * This function sets up the necessary pipes for inter-process communication
 * within the minishell. It initializes the pipes based on the requirements
 * of the minishell structure.
 *
 * @param minishell A pointer to the t_minishell structure containing the
 *                  necessary information for pipe creation.
 */

void	executor_create_pipes(t_minishell *minishell)
{
	t_list		*current;
	t_command	*command;
	int			fd[2];
	int			fd_in;
	const int	n_fds = executor_count_fds(minishell);

	fd_in = 0;
	current = minishell->commands;
	while (current)
	{
		command = (t_command *)current->content;
		if (current->next)
		{
			if (pipe(fd) == -1)
				error_msg("pipe", "pipe creation failed");
			command->fd_out = fd[1];
		}
		else
			command->fd_out = 1;
		command->fd_in = fd_in;
		command->n_fds = n_fds;
		fd_in = fd[0];
		current = current->next;
	}
}
