67 lines
1.2 KiB
C
67 lines
1.2 KiB
C
#include "minishell.h"
|
|
|
|
int ft_file_is_readable(const char *path)
|
|
{
|
|
int readable;
|
|
int fd;
|
|
|
|
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);
|
|
}
|
|
|
|
int ft_file_is_writeable(const char *path)
|
|
{
|
|
int writeable;
|
|
int fd;
|
|
|
|
fd = open(path, O_WRONLY | O_CREAT, 0644);
|
|
if (fd == -1)
|
|
{
|
|
ft_eprintf("minishell: %s: Permission denied\n", path);
|
|
return (0);
|
|
}
|
|
writeable = write(fd, "", 0);
|
|
if (writeable == -1)
|
|
{
|
|
ft_eprintf("minishell: %s: Permission denied\n", path);
|
|
return (0);
|
|
}
|
|
close(fd);
|
|
return (1);
|
|
}
|
|
|
|
char *ft_get_file_path(const char *infile)
|
|
{
|
|
size_t i;
|
|
size_t n;
|
|
|
|
n = 1;
|
|
while (infile[-n] == infile[-1])
|
|
n++;
|
|
i = 0;
|
|
while (infile[i] == ' ')
|
|
i++;
|
|
if (infile[i] == '\0' || infile[i] == '>' || infile[i] == '<')
|
|
{
|
|
ft_eprintf("minishell: syntax error near ");
|
|
if ((infile[0] == '<' && n > 3) || (infile[0] == '>' && n > 2))
|
|
ft_eprintf("unexpected token `%c`\n", infile[i - 1]);
|
|
else
|
|
ft_eprintf("unexpected token `newline`\n");
|
|
return (NULL);
|
|
}
|
|
return (ft_getstr(infile, i + 1));
|
|
}
|