42_minishell/redirection/file.c

60 lines
1.7 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-02-09 12:47:05 -05:00
int ft_file_is_readable(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)
{
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)
{
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-02-15 14:52:27 -05:00
int ft_file_is_writable(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-06 10:43:50 -05:00
fd = open(path, O_WRONLY | O_CREAT, 0644);
2023-02-01 11:28:38 -05:00
if (fd == -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)
{
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);
}
int ft_file_is_executable(const char *path)
{
return (access(path, X_OK) == 0);
2023-02-02 11:27:26 -05:00
}