42_minishell/redirection/file.c

83 lines
2.2 KiB
C
Raw Permalink 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 #+# #+# */
2023-04-05 11:32:04 -04:00
/* Updated: 2023/04/05 15:12:41 by alouis-j ### ########.fr */
2023-02-15 14:52:27 -05:00
/* */
/* ************************************************************************** */
#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-04-05 11:32:04 -04:00
*data->exit_code = 1;
2023-04-14 10:17:35 -04:00
ft_eprintf("bozoshell: %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-04-05 11:32:04 -04:00
*data->exit_code = 1;
2023-04-14 10:17:35 -04:00
ft_eprintf("bozoshell: %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-04-05 11:32:04 -04:00
*data->exit_code = 1;
2023-04-14 10:17:35 -04:00
ft_eprintf("bozoshell: %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-04-05 11:32:04 -04:00
*data->exit_code = 1;
2023-04-14 10:17:35 -04:00
ft_eprintf("bozoshell: %s: Permission denied\n", path);
2023-03-06 10:43:50 -05:00
return (0);
2023-02-15 14:52:27 -05:00
}
2023-03-30 07:27:23 -04:00
close(fd);
return (1);
2023-02-15 14:52:27 -05:00
}
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)
{
2023-04-05 11:32:04 -04:00
*data->exit_code = 1;
2023-04-14 10:17:35 -04:00
ft_eprintf("bozoshell: %s: Permission denied\n", path);
2023-03-13 09:23:27 -04:00
return (0);
}
writeable = write(fd, "", 0);
if (writeable == -1)
{
2023-04-05 11:32:04 -04:00
*data->exit_code = 1;
2023-04-14 10:17:35 -04:00
ft_eprintf("bozoshell: %s: Permission denied\n", path);
2023-03-13 09:23:27 -04:00
return (0);
}
2023-03-30 07:27:23 -04:00
close(fd);
return (1);
2023-02-02 11:27:26 -05:00
}