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 */
|
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
2023-02-02 11:27:26 -05:00
|
|
|
#include "minishell.h"
|
2023-02-15 14:52:27 -05:00
|
|
|
#include <unistd.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);
|
2023-03-06 10:06:12 -05:00
|
|
|
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);
|
2023-03-06 10:06:12 -05:00
|
|
|
return (0);
|
2023-02-01 11:28:38 -05:00
|
|
|
}
|
2023-03-06 10:06:12 -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-02-15 14:52:27 -05:00
|
|
|
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 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-02-15 14:52:27 -05:00
|
|
|
return (-1);
|
2023-02-01 11:28:38 -05:00
|
|
|
}
|
|
|
|
writeable = write(fd, "", 0);
|
|
|
|
if (writeable == -1)
|
|
|
|
{
|
|
|
|
ft_eprintf("minishell: %s: Permission denied\n", path);
|
2023-02-15 14:52:27 -05:00
|
|
|
return (-1);
|
2023-02-01 11:28:38 -05:00
|
|
|
}
|
2023-02-15 14:52:27 -05:00
|
|
|
return (fd);
|
2023-02-01 11:28:38 -05:00
|
|
|
}
|
2023-02-02 11:27:26 -05:00
|
|
|
|
2023-02-15 14:52:27 -05:00
|
|
|
int ft_file_is_appendable(const char *path)
|
2023-02-02 11:27:26 -05:00
|
|
|
{
|
2023-02-15 14:52:27 -05:00
|
|
|
int writeable;
|
|
|
|
int fd;
|
2023-02-02 11:27:26 -05:00
|
|
|
|
2023-02-15 14:52:27 -05:00
|
|
|
fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644);
|
|
|
|
if (fd == -1)
|
2023-02-02 11:27:26 -05:00
|
|
|
{
|
2023-02-15 14:52:27 -05:00
|
|
|
ft_eprintf("minishell: %s: Permission denied\n", path);
|
|
|
|
return (-1);
|
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);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ft_file_is_executable(const char *path)
|
|
|
|
{
|
|
|
|
return (access(path, X_OK) == 0);
|
2023-02-02 11:27:26 -05:00
|
|
|
}
|