86 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			86 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| /* ************************************************************************** */
 | |
| /*                                                                            */
 | |
| /*                                                        :::      ::::::::   */
 | |
| /*   check.c                                            :+:      :+:    :+:   */
 | |
| /*                                                    +:+ +:+         +:+     */
 | |
| /*   By: cchauvet <cchauvet@student.42angouleme.fr  +#+  +:+       +#+        */
 | |
| /*                                                +#+#+#+#+#+   +#+           */
 | |
| /*   Created: 2023/03/29 17:32:06 by cchauvet          #+#    #+#             */
 | |
| /*   Updated: 2023/03/29 19:21:59 by cchauvet         ###   ########.fr       */
 | |
| /*                                                                            */
 | |
| /* ************************************************************************** */
 | |
| 
 | |
| #include "./redirection_private.h"
 | |
| 
 | |
| static bool	ft_check_heredoc(t_data *data, t_cmd *cmd,
 | |
| 				char *redirection_identifier, char *redirection)
 | |
| {
 | |
| 	if (ft_strcmp(redirection_identifier, "<<") == 0)
 | |
| 	{
 | |
| 		cmd->fd_in[0] = ft_heredoc(data, redirection);
 | |
| 		if (cmd->fd_in[0] == -2)
 | |
| 			return (1);
 | |
| 	}
 | |
| 	return (0);
 | |
| }
 | |
| 
 | |
| static bool	ft_check_infile(t_data *data, t_cmd *cmd,
 | |
| 				char *redirection_identifier, char *redirection)
 | |
| {
 | |
| 	if (ft_strcmp(redirection_identifier, "<") == 0)
 | |
| 	{
 | |
| 		if (ft_file_is_readable(data, redirection))
 | |
| 			cmd->fd_in[0] = open(redirection, O_RDONLY);
 | |
| 		else
 | |
| 		{
 | |
| 			cmd->fd_in[0] = -2;
 | |
| 			return (1);
 | |
| 		}
 | |
| 	}
 | |
| 	return (0);
 | |
| }
 | |
| 
 | |
| static bool	ft_check_outfile(t_data *data, t_cmd *cmd,
 | |
| 				char *redirection_identifier, char *redirection)
 | |
| {
 | |
| 	if (ft_strcmp(redirection_identifier, ">") == 0)
 | |
| 	{
 | |
| 		if (ft_file_is_writable(data, redirection))
 | |
| 			cmd->fd_out[0] = open(redirection,
 | |
| 					O_WRONLY | O_TRUNC | O_CREAT, 0644);
 | |
| 		else
 | |
| 		{
 | |
| 			cmd->fd_out[0] = -2;
 | |
| 			return (1);
 | |
| 		}
 | |
| 	}
 | |
| 	return (0);
 | |
| }
 | |
| 
 | |
| static bool	ft_check_outfile_append(t_data *data, t_cmd *cmd,
 | |
| 				char *redirection_identifier, char *redirection)
 | |
| {
 | |
| 	if (ft_strcmp(redirection_identifier, ">>") == 0)
 | |
| 	{
 | |
| 		if (ft_file_is_writable(data, redirection))
 | |
| 			cmd->fd_out[0] = open(redirection,
 | |
| 					O_WRONLY | O_APPEND | O_CREAT, 0644);
 | |
| 		else
 | |
| 		{
 | |
| 			cmd->fd_out[0] = -2;
 | |
| 			return (1);
 | |
| 		}
 | |
| 	}
 | |
| 	return (0);
 | |
| }
 | |
| 
 | |
| bool	ft_check_redirection(t_data *data, t_cmd *cmd,
 | |
| 				char *redirection_identifier, char *redirection)
 | |
| {
 | |
| 	return (ft_check_heredoc(data, cmd, redirection_identifier, redirection)
 | |
| 		|| ft_check_infile(data, cmd, redirection_identifier, redirection)
 | |
| 		|| ft_check_outfile(data, cmd, redirection_identifier, redirection)
 | |
| 		|| ft_check_outfile_append(data, cmd, redirection_identifier,
 | |
| 			redirection));
 | |
| }
 |