83 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			83 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| /* ************************************************************************** */
 | |
| /*                                                                            */
 | |
| /*                                                        :::      ::::::::   */
 | |
| /*   file.c                                             :+:      :+:    :+:   */
 | |
| /*                                                    +:+ +:+         +:+     */
 | |
| /*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
 | |
| /*                                                +#+#+#+#+#+   +#+           */
 | |
| /*   Created: 2023/02/15 17:36:11 by cchauvet          #+#    #+#             */
 | |
| /*   Updated: 2023/03/30 13:26:31 by cchauvet         ###   ########.fr       */
 | |
| /*                                                                            */
 | |
| /* ************************************************************************** */
 | |
| 
 | |
| #include "./redirection_private.h"
 | |
| 
 | |
| int	ft_file_is_readable(t_data *data, const char *path)
 | |
| {
 | |
| 	int	readable;
 | |
| 	int	fd;
 | |
| 
 | |
| 	fd = open(path, O_RDONLY);
 | |
| 	if (fd == -1)
 | |
| 	{
 | |
| 		data->exit_code = 1;
 | |
| 		ft_eprintf("minishell: %s: No such file or directory\n", path);
 | |
| 		return (0);
 | |
| 	}
 | |
| 	readable = read(fd, "", 0);
 | |
| 	if (readable == -1)
 | |
| 	{
 | |
| 		data->exit_code = 1;
 | |
| 		ft_eprintf("minishell: %s: Permission denied\n", path);
 | |
| 		return (0);
 | |
| 	}
 | |
| 	close(fd);
 | |
| 	return (1);
 | |
| }
 | |
| 
 | |
| int	ft_file_is_writable(t_data *data, const char *path)
 | |
| {
 | |
| 	int	writeable;
 | |
| 	int	fd;
 | |
| 
 | |
| 	fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644);
 | |
| 	if (fd == -1)
 | |
| 	{
 | |
| 		data->exit_code = 1;
 | |
| 		ft_eprintf("minishell: %s: Permission denied\n", path);
 | |
| 		return (0);
 | |
| 	}
 | |
| 	writeable = write(fd, "", 0);
 | |
| 	if (writeable == -1)
 | |
| 	{
 | |
| 		data->exit_code = 1;
 | |
| 		ft_eprintf("minishell: %s: Permission denied\n", path);
 | |
| 		return (0);
 | |
| 	}
 | |
| 	close(fd);
 | |
| 	return (1);
 | |
| }
 | |
| 
 | |
| int	ft_file_is_appendable(t_data *data, const char *path)
 | |
| {
 | |
| 	int	writeable;
 | |
| 	int	fd;
 | |
| 
 | |
| 	fd = open(path, O_WRONLY | O_APPEND | O_CREAT, 0644);
 | |
| 	if (fd == -1)
 | |
| 	{
 | |
| 		data->exit_code = 1;
 | |
| 		ft_eprintf("minishell: %s: Permission denied\n", path);
 | |
| 		return (0);
 | |
| 	}
 | |
| 	writeable = write(fd, "", 0);
 | |
| 	if (writeable == -1)
 | |
| 	{
 | |
| 		data->exit_code = 1;
 | |
| 		ft_eprintf("minishell: %s: Permission denied\n", path);
 | |
| 		return (0);
 | |
| 	}
 | |
| 	close(fd);
 | |
| 	return (1);
 | |
| }
 |