2023-02-02 11:27:26 -05:00
|
|
|
#include "minishell.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);
|
|
|
|
}
|
|
|
|
readable = read(fd, "", 0);
|
|
|
|
if (readable == -1)
|
|
|
|
{
|
|
|
|
ft_eprintf("minishell: %s: Permission denied\n", path);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2023-02-09 12:47:05 -05:00
|
|
|
int ft_file_is_writeable(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
|
|
|
|
|
|
|
fd = open(path, O_WRONLY | O_CREAT, 0644);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
2023-02-02 11:27:26 -05:00
|
|
|
ft_eprintf("minishell: %s: Permission denied\n", path);
|
2023-02-01 11:28:38 -05:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
writeable = write(fd, "", 0);
|
|
|
|
if (writeable == -1)
|
|
|
|
{
|
|
|
|
ft_eprintf("minishell: %s: Permission denied\n", path);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return (1);
|
|
|
|
}
|
2023-02-02 11:27:26 -05:00
|
|
|
|
2023-02-09 12:47:05 -05:00
|
|
|
char *ft_get_file_path(const char *infile)
|
2023-02-02 11:27:26 -05:00
|
|
|
{
|
|
|
|
size_t i;
|
2023-02-02 12:40:41 -05:00
|
|
|
size_t n;
|
2023-02-02 11:27:26 -05:00
|
|
|
|
2023-02-03 10:02:52 -05:00
|
|
|
n = 1;
|
|
|
|
while (infile[-n] == infile[-1])
|
2023-02-02 12:40:41 -05:00
|
|
|
n++;
|
2023-02-03 10:02:52 -05:00
|
|
|
i = 0;
|
2023-02-02 11:27:26 -05:00
|
|
|
while (infile[i] == ' ')
|
|
|
|
i++;
|
2023-02-03 10:02:52 -05:00
|
|
|
if (infile[i] == '\0' || infile[i] == '>' || infile[i] == '<')
|
2023-02-02 11:27:26 -05:00
|
|
|
{
|
|
|
|
ft_eprintf("minishell: syntax error near ");
|
2023-02-03 10:02:52 -05:00
|
|
|
if ((infile[0] == '<' && n > 3) || (infile[0] == '>' && n > 2))
|
|
|
|
ft_eprintf("unexpected token `%c`\n", infile[i - 1]);
|
2023-02-02 12:40:41 -05:00
|
|
|
else
|
2023-02-03 10:02:52 -05:00
|
|
|
ft_eprintf("unexpected token `newline`\n");
|
2023-02-02 11:27:26 -05:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (ft_getstr(infile, i + 1));
|
|
|
|
}
|