2023-02-15 14:52:27 -05:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
2023-03-06 10:43:50 -05:00
|
|
|
/* infile.c :+: :+: :+: */
|
2023-02-15 14:52:27 -05:00
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
2023-03-06 10:43:50 -05:00
|
|
|
/* Created: 2023/02/15 17:52:10 by cchauvet #+# #+# */
|
|
|
|
/* Updated: 2023/02/20 13:05:43 by starnakin ### ########.fr */
|
2023-02-15 14:52:27 -05:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
2023-02-02 11:27:26 -05:00
|
|
|
#include "minishell.h"
|
|
|
|
|
2023-03-06 10:43:50 -05:00
|
|
|
static int ft_outfile_is_valid(t_data *data, const char *line)
|
2023-02-02 11:27:26 -05:00
|
|
|
{
|
2023-02-15 14:52:27 -05:00
|
|
|
char **tab;
|
2023-03-06 10:43:50 -05:00
|
|
|
char *path;
|
2023-03-06 10:06:12 -05:00
|
|
|
ssize_t i;
|
2023-02-02 11:27:26 -05:00
|
|
|
|
2023-02-15 14:52:27 -05:00
|
|
|
tab = ft_split_quoted(line, ' ');
|
|
|
|
if (tab == NULL)
|
|
|
|
{
|
|
|
|
ft_eprintf("minishell: malloc failed\n");
|
|
|
|
return (0);
|
|
|
|
}
|
2023-02-15 15:31:49 -05:00
|
|
|
if (tab[0] == NULL)
|
|
|
|
return (1);
|
2023-03-06 10:06:12 -05:00
|
|
|
i = -1;
|
|
|
|
while (tab[++i] != NULL)
|
2023-02-02 11:27:26 -05:00
|
|
|
{
|
2023-03-06 07:21:22 -05:00
|
|
|
if (tab[i][0] == '>')
|
|
|
|
{
|
2023-03-06 10:43:50 -05:00
|
|
|
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);
|
2023-03-06 07:21:22 -05:00
|
|
|
}
|
2023-02-15 14:52:27 -05:00
|
|
|
}
|
|
|
|
ft_freer_tab_ultimate(1, tab);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2023-03-06 10:43:50 -05:00
|
|
|
static int ft_get_outfile(t_data *data, const char *line)
|
2023-02-15 14:52:27 -05:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
int fd;
|
|
|
|
char **tab;
|
2023-03-06 10:43:50 -05:00
|
|
|
char *path;
|
2023-02-15 14:52:27 -05:00
|
|
|
|
|
|
|
tab = ft_split_quoted(line, ' ');
|
|
|
|
if (tab == NULL)
|
|
|
|
{
|
|
|
|
ft_eprintf("minishell: malloc failed\n");
|
|
|
|
return (-2);
|
|
|
|
}
|
2023-03-06 10:43:50 -05:00
|
|
|
fd = 0;
|
2023-02-15 14:52:27 -05:00
|
|
|
i = 0;
|
|
|
|
while (tab[i + 1] != NULL)
|
|
|
|
{
|
2023-02-16 12:28:10 -05:00
|
|
|
if (tab[i][0] == '>')
|
2023-03-06 10:43:50 -05:00
|
|
|
if (fd != 0)
|
2023-02-16 12:28:10 -05:00
|
|
|
close(fd);
|
2023-03-06 10:43:50 -05:00
|
|
|
path = ft_env_filler(data, tab[i + 1]);
|
|
|
|
if (path == NULL)
|
|
|
|
return (-2);
|
|
|
|
ft_quote_remover(path);
|
2023-02-15 14:52:27 -05:00
|
|
|
if (ft_strcmp(">", tab[i]) == 0)
|
2023-03-06 10:43:50 -05:00
|
|
|
fd = open(path, O_TRUNC | O_CREAT | O_WRONLY, 0644);
|
2023-02-15 14:52:27 -05:00
|
|
|
else if (ft_strcmp(">>", tab[i]) == 0)
|
2023-03-06 10:43:50 -05:00
|
|
|
fd = open(path, O_CREAT | O_APPEND, 0644);
|
|
|
|
free(path);
|
2023-02-02 11:27:26 -05:00
|
|
|
i++;
|
|
|
|
}
|
2023-02-15 14:52:27 -05:00
|
|
|
ft_freer_tab_ultimate(1, tab);
|
|
|
|
return (fd);
|
2023-02-02 11:27:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ft_remove_outfile(char *line)
|
|
|
|
{
|
|
|
|
size_t i;
|
2023-02-15 14:52:27 -05:00
|
|
|
size_t y;
|
|
|
|
char **tab;
|
2023-02-02 11:27:26 -05:00
|
|
|
|
2023-02-15 14:52:27 -05:00
|
|
|
tab = ft_split_quoted(line, ' ');
|
|
|
|
if (tab == NULL)
|
|
|
|
{
|
|
|
|
ft_eprintf("minishell: malloc failed\n");
|
|
|
|
return (1);
|
|
|
|
}
|
2023-02-02 11:27:26 -05:00
|
|
|
i = 0;
|
2023-02-15 14:52:27 -05:00
|
|
|
y = 0;
|
|
|
|
while (tab[i] != NULL)
|
2023-02-02 11:27:26 -05:00
|
|
|
{
|
2023-02-15 14:52:27 -05:00
|
|
|
if (tab[i][0] == '>')
|
2023-03-06 10:43:50 -05:00
|
|
|
{
|
2023-02-17 13:05:33 -05:00
|
|
|
ft_strshift(line + y,
|
2023-03-06 07:21:22 -05:00
|
|
|
(-1) * (ft_strlen(tab[i]) + ft_strlen(tab[i + 1]) + 1 + (line[ft_strlen(tab[i]) + ft_strlen(tab[i + 1]) + 1] != '\0')));
|
2023-03-06 10:43:50 -05:00
|
|
|
i++;
|
|
|
|
}
|
2023-02-15 14:52:27 -05:00
|
|
|
else
|
2023-02-17 13:05:33 -05:00
|
|
|
y = y + ft_strlen(tab[i]) + (y != 0);
|
2023-02-02 11:27:26 -05:00
|
|
|
i++;
|
|
|
|
}
|
2023-02-15 14:52:27 -05:00
|
|
|
ft_freer_tab_ultimate(1, tab);
|
2023-02-02 11:27:26 -05:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2023-02-24 14:30:01 -05:00
|
|
|
int ft_outfile(t_data *data, char *line)
|
2023-02-02 11:27:26 -05:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2023-03-06 10:43:50 -05:00
|
|
|
if (ft_outfile_is_valid(data, line) == 0)
|
2023-02-15 14:52:27 -05:00
|
|
|
return (-2);
|
2023-03-06 10:43:50 -05:00
|
|
|
fd = ft_get_outfile(data, line);
|
2023-02-15 14:52:27 -05:00
|
|
|
if (fd == -2)
|
|
|
|
return (-2);
|
|
|
|
ft_remove_outfile(line);
|
2023-02-02 11:27:26 -05:00
|
|
|
return (fd);
|
|
|
|
}
|