fix
This commit is contained in:
parent
3540743135
commit
2930755dd3
Binary file not shown.
26
file.c
26
file.c
@ -39,37 +39,17 @@ int ft_file_is_writable(const char *path)
|
||||
int writeable;
|
||||
int fd;
|
||||
|
||||
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
fd = open(path, O_WRONLY | O_CREAT, 0644);
|
||||
if (fd == -1)
|
||||
{
|
||||
ft_eprintf("minishell: %s: Permission denied\n", path);
|
||||
return (-1);
|
||||
return (0);
|
||||
}
|
||||
writeable = write(fd, "", 0);
|
||||
if (writeable == -1)
|
||||
{
|
||||
ft_eprintf("minishell: %s: Permission denied\n", path);
|
||||
return (-1);
|
||||
}
|
||||
return (fd);
|
||||
}
|
||||
|
||||
int ft_file_is_appendable(const char *path)
|
||||
{
|
||||
int writeable;
|
||||
int fd;
|
||||
|
||||
fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644);
|
||||
if (fd == -1)
|
||||
{
|
||||
ft_eprintf("minishell: %s: Permission denied\n", path);
|
||||
return (-1);
|
||||
}
|
||||
writeable = write(fd, "", 0);
|
||||
if (writeable == -1)
|
||||
{
|
||||
ft_eprintf("minishell: %s: Permission denied\n", path);
|
||||
return (-1);
|
||||
return (0);
|
||||
}
|
||||
return (fd);
|
||||
}
|
||||
|
4
infile.c
4
infile.c
@ -40,6 +40,7 @@ static int ft_infile_is_valid(t_data *data, const char *line)
|
||||
ft_quote_remover(path);
|
||||
if (ft_file_is_readable(path) == 0)
|
||||
{
|
||||
data->exit_code = 1;
|
||||
free(path);
|
||||
ft_freer_tab_ultimate(1, tab);
|
||||
return (0);
|
||||
@ -130,10 +131,7 @@ int ft_infile(t_data *data, char *line)
|
||||
int fd;
|
||||
|
||||
if (ft_infile_is_valid(data, line) == 0)
|
||||
{
|
||||
data->exit_code = 2;
|
||||
return (-2);
|
||||
}
|
||||
fd = ft_get_infile(data, line);
|
||||
if (fd == -2)
|
||||
return (-2);
|
||||
|
55
outfile.c
55
outfile.c
@ -1,20 +1,21 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* outfile.c :+: :+: :+: */
|
||||
/* infile.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/15 18:01:07 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/17 19:03:00 by cchauvet ### ########.fr */
|
||||
/* Created: 2023/02/15 17:52:10 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/20 13:05:43 by starnakin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
static int ft_outfile_is_valid(const char *line)
|
||||
static int ft_outfile_is_valid(t_data *data, const char *line)
|
||||
{
|
||||
char **tab;
|
||||
char *path;
|
||||
ssize_t i;
|
||||
|
||||
tab = ft_split_quoted(line, ' ');
|
||||
@ -30,22 +31,37 @@ static int ft_outfile_is_valid(const char *line)
|
||||
{
|
||||
if (tab[i][0] == '>')
|
||||
{
|
||||
if (tab[i + 1] != NULL && !ft_contain_only_str(tab[i + 1], "| <>"))
|
||||
continue ;
|
||||
ft_eprintf("minishell: %s: must be followed by an outfile\n", tab[i]);
|
||||
path = ft_env_filler(data, tab[i + 1]);
|
||||
if (path == NULL)
|
||||
return (0);
|
||||
ft_quote_remover(path);
|
||||
if (ft_file_is_writable(path) == 0)
|
||||
{
|
||||
data->exit_code = 1;
|
||||
free(path);
|
||||
ft_freer_tab_ultimate(1, tab);
|
||||
return (0);
|
||||
}
|
||||
if (ft_contain_only_str(path, "| >>"))
|
||||
{
|
||||
free(path);
|
||||
ft_eprintf("minishell: %s: must be followed by an outfile\n", path);
|
||||
ft_freer_tab_ultimate(1, tab);
|
||||
return (0);
|
||||
}
|
||||
free(path);
|
||||
}
|
||||
}
|
||||
ft_freer_tab_ultimate(1, tab);
|
||||
return (1);
|
||||
}
|
||||
|
||||
static int ft_get_outfile(const char *line)
|
||||
static int ft_get_outfile(t_data *data, const char *line)
|
||||
{
|
||||
size_t i;
|
||||
int fd;
|
||||
char **tab;
|
||||
char *path;
|
||||
|
||||
tab = ft_split_quoted(line, ' ');
|
||||
if (tab == NULL)
|
||||
@ -53,17 +69,22 @@ static int ft_get_outfile(const char *line)
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
return (-2);
|
||||
}
|
||||
fd = 1;
|
||||
fd = 0;
|
||||
i = 0;
|
||||
while (tab[i + 1] != NULL)
|
||||
{
|
||||
if (tab[i][0] == '>')
|
||||
if (fd != 1)
|
||||
if (fd != 0)
|
||||
close(fd);
|
||||
path = ft_env_filler(data, tab[i + 1]);
|
||||
if (path == NULL)
|
||||
return (-2);
|
||||
ft_quote_remover(path);
|
||||
if (ft_strcmp(">", tab[i]) == 0)
|
||||
fd = ft_file_is_writable(ft_quote_remover(tab[i + 1]));
|
||||
fd = open(path, O_TRUNC | O_CREAT | O_WRONLY, 0644);
|
||||
else if (ft_strcmp(">>", tab[i]) == 0)
|
||||
fd = ft_file_is_appendable(ft_quote_remover(tab[i + 1]));
|
||||
fd = open(path, O_CREAT | O_APPEND, 0644);
|
||||
free(path);
|
||||
i++;
|
||||
}
|
||||
ft_freer_tab_ultimate(1, tab);
|
||||
@ -87,8 +108,11 @@ static int ft_remove_outfile(char *line)
|
||||
while (tab[i] != NULL)
|
||||
{
|
||||
if (tab[i][0] == '>')
|
||||
{
|
||||
ft_strshift(line + y,
|
||||
(-1) * (ft_strlen(tab[i]) + ft_strlen(tab[i + 1]) + 1 + (line[ft_strlen(tab[i]) + ft_strlen(tab[i + 1]) + 1] != '\0')));
|
||||
i++;
|
||||
}
|
||||
else
|
||||
y = y + ft_strlen(tab[i]) + (y != 0);
|
||||
i++;
|
||||
@ -101,12 +125,9 @@ int ft_outfile(t_data *data, char *line)
|
||||
{
|
||||
int fd;
|
||||
|
||||
if (ft_outfile_is_valid(line) == 0)
|
||||
{
|
||||
data->exit_code = 2;
|
||||
if (ft_outfile_is_valid(data, line) == 0)
|
||||
return (-2);
|
||||
}
|
||||
fd = ft_get_outfile(line);
|
||||
fd = ft_get_outfile(data, line);
|
||||
if (fd == -2)
|
||||
return (-2);
|
||||
ft_remove_outfile(line);
|
||||
|
Loading…
Reference in New Issue
Block a user