42_minishell/redirection/file.c

81 lines
2.1 KiB
C
Raw Normal View History

2023-02-15 14:52:27 -05:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* file.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 17:36:11 by cchauvet #+# #+# */
/* Updated: 2023/02/15 17:41:13 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "./redirection_private.h"
2023-02-01 11:28:38 -05:00
2023-03-13 09:23:27 -04:00
int ft_file_is_readable(t_data *data, const char *path)
2023-02-01 11:28:38 -05:00
{
int readable;
2023-02-02 11:27:26 -05:00
int fd;
2023-02-01 11:28:38 -05:00
fd = open(path, O_RDONLY);
if (fd == -1)
{
2023-03-13 09:23:27 -04:00
data->exit_code = 1;
2023-02-01 11:28:38 -05:00
ft_eprintf("minishell: %s: No such file or directory\n", path);
return (0);
2023-02-01 11:28:38 -05:00
}
readable = read(fd, "", 0);
if (readable == -1)
{
2023-03-13 09:23:27 -04:00
data->exit_code = 1;
2023-02-01 11:28:38 -05:00
ft_eprintf("minishell: %s: Permission denied\n", path);
return (0);
2023-02-01 11:28:38 -05:00
}
close(fd);
return (1);
2023-02-01 11:28:38 -05:00
}
2023-03-13 09:23:27 -04:00
int ft_file_is_writable(t_data *data, const char *path)
2023-02-01 11:28:38 -05:00
{
int writeable;
2023-02-02 11:27:26 -05:00
int fd;
2023-02-01 11:28:38 -05:00
2023-03-13 09:23:27 -04:00
fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644);
2023-02-01 11:28:38 -05:00
if (fd == -1)
{
2023-03-13 09:23:27 -04:00
data->exit_code = 1;
2023-02-02 11:27:26 -05:00
ft_eprintf("minishell: %s: Permission denied\n", path);
2023-03-06 10:43:50 -05:00
return (0);
2023-02-02 11:27:26 -05:00
}
2023-02-15 14:52:27 -05:00
writeable = write(fd, "", 0);
if (writeable == -1)
{
2023-03-13 09:23:27 -04:00
data->exit_code = 1;
2023-02-15 14:52:27 -05:00
ft_eprintf("minishell: %s: Permission denied\n", path);
2023-03-06 10:43:50 -05:00
return (0);
2023-02-15 14:52:27 -05:00
}
return (fd);
}
2023-03-13 09:23:27 -04:00
int ft_file_is_appendable(t_data *data, const char *path)
2023-02-15 14:52:27 -05:00
{
2023-03-13 09:23:27 -04:00
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);
}
return (fd);
2023-02-02 11:27:26 -05:00
}