This commit is contained in:
Camille Chauvet 2023-03-06 16:43:50 +01:00
parent 3540743135
commit 2930755dd3
5 changed files with 44 additions and 45 deletions

Binary file not shown.

26
file.c
View File

@ -39,37 +39,17 @@ int ft_file_is_writable(const char *path)
int writeable; int writeable;
int fd; int fd;
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); fd = open(path, O_WRONLY | O_CREAT, 0644);
if (fd == -1) if (fd == -1)
{ {
ft_eprintf("minishell: %s: Permission denied\n", path); ft_eprintf("minishell: %s: Permission denied\n", path);
return (-1); return (0);
} }
writeable = write(fd, "", 0); writeable = write(fd, "", 0);
if (writeable == -1) if (writeable == -1)
{ {
ft_eprintf("minishell: %s: Permission denied\n", path); ft_eprintf("minishell: %s: Permission denied\n", path);
return (-1); return (0);
}
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 (fd); return (fd);
} }

View File

@ -40,6 +40,7 @@ static int ft_infile_is_valid(t_data *data, const char *line)
ft_quote_remover(path); ft_quote_remover(path);
if (ft_file_is_readable(path) == 0) if (ft_file_is_readable(path) == 0)
{ {
data->exit_code = 1;
free(path); free(path);
ft_freer_tab_ultimate(1, tab); ft_freer_tab_ultimate(1, tab);
return (0); return (0);
@ -130,10 +131,7 @@ int ft_infile(t_data *data, char *line)
int fd; int fd;
if (ft_infile_is_valid(data, line) == 0) if (ft_infile_is_valid(data, line) == 0)
{
data->exit_code = 2;
return (-2); return (-2);
}
fd = ft_get_infile(data, line); fd = ft_get_infile(data, line);
if (fd == -2) if (fd == -2)
return (-2); return (-2);

View File

@ -1,20 +1,21 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* outfile.c :+: :+: :+: */ /* infile.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 18:01:07 by cchauvet #+# #+# */ /* Created: 2023/02/15 17:52:10 by cchauvet #+# #+# */
/* Updated: 2023/02/17 19:03:00 by cchauvet ### ########.fr */ /* Updated: 2023/02/20 13:05:43 by starnakin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #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 **tab;
char *path;
ssize_t i; ssize_t i;
tab = ft_split_quoted(line, ' '); 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][0] == '>')
{ {
if (tab[i + 1] != NULL && !ft_contain_only_str(tab[i + 1], "| <>")) path = ft_env_filler(data, tab[i + 1]);
continue ; if (path == NULL)
ft_eprintf("minishell: %s: must be followed by an outfile\n", tab[i]); 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); ft_freer_tab_ultimate(1, tab);
return (0); 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); ft_freer_tab_ultimate(1, tab);
return (1); return (1);
} }
static int ft_get_outfile(const char *line) static int ft_get_outfile(t_data *data, const char *line)
{ {
size_t i; size_t i;
int fd; int fd;
char **tab; char **tab;
char *path;
tab = ft_split_quoted(line, ' '); tab = ft_split_quoted(line, ' ');
if (tab == NULL) if (tab == NULL)
@ -53,17 +69,22 @@ static int ft_get_outfile(const char *line)
ft_eprintf("minishell: malloc failed\n"); ft_eprintf("minishell: malloc failed\n");
return (-2); return (-2);
} }
fd = 1; fd = 0;
i = 0; i = 0;
while (tab[i + 1] != NULL) while (tab[i + 1] != NULL)
{ {
if (tab[i][0] == '>') if (tab[i][0] == '>')
if (fd != 1) if (fd != 0)
close(fd); 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) 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) 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++; i++;
} }
ft_freer_tab_ultimate(1, tab); ft_freer_tab_ultimate(1, tab);
@ -87,8 +108,11 @@ static int ft_remove_outfile(char *line)
while (tab[i] != NULL) while (tab[i] != NULL)
{ {
if (tab[i][0] == '>') if (tab[i][0] == '>')
{
ft_strshift(line + y, 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'))); (-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 else
y = y + ft_strlen(tab[i]) + (y != 0); y = y + ft_strlen(tab[i]) + (y != 0);
i++; i++;
@ -101,12 +125,9 @@ int ft_outfile(t_data *data, char *line)
{ {
int fd; int fd;
if (ft_outfile_is_valid(line) == 0) if (ft_outfile_is_valid(data, line) == 0)
{
data->exit_code = 2;
return (-2); return (-2);
} fd = ft_get_outfile(data, line);
fd = ft_get_outfile(line);
if (fd == -2) if (fd == -2)
return (-2); return (-2);
ft_remove_outfile(line); ft_remove_outfile(line);

0
t
View File