/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   executor_open_fds.c                                :+:    :+:            */
/*                                                     +:+                    */
/*   By: whaffman <whaffman@student.codam.nl>         +#+                     */
/*                                                   +#+                      */
/*   Created: 2025/02/21 12:50:42 by whaffman      #+#    #+#                 */
/*   Updated: 2025/02/26 15:21:56 by whaffman      ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include "minishell.h"

int	executor_open_fds(t_list *redirect_list, int og_fd)
{
	t_redirect	*redirect;
	int			fd;

	fd = og_fd;
	while (redirect_list)
	{
		redirect = (t_redirect *)redirect_list->content;
		if (redirect->type == T_REDIRECT_IN)
			fd = open(redirect->value, O_RDONLY);
		else if (redirect->type == T_REDIRECT_OUT)
			fd = open(redirect->value, O_WRONLY | O_CREAT | O_TRUNC, 0644);
		else if (redirect->type == T_APPEND_OUT)
			fd = open(redirect->value, O_WRONLY | O_CREAT | O_APPEND, 0644);
		if (fd < 0)
		{
			error_msg("executor_open_fds", "can't open file");
			return (og_fd);
		}
		redirect_list = redirect_list->next;
	}
	return (fd);
}
