139 lines
3.2 KiB
C
139 lines
3.2 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* infile.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* 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_infile_is_valid(t_data *data, const char *line)
|
|
{
|
|
char **tab;
|
|
char *path;
|
|
ssize_t i;
|
|
int return_code;
|
|
|
|
tab = ft_split_quoted(line, ' ');
|
|
if (tab == NULL)
|
|
{
|
|
ft_eprintf("minishell: malloc failed\n");
|
|
return (0);
|
|
}
|
|
if (tab[0] == NULL)
|
|
return (1);
|
|
return_code = 1;
|
|
i = -1;
|
|
while (tab[++i] != NULL && return_code == 1)
|
|
{
|
|
if (tab[i][0] == '<')
|
|
{
|
|
if (ft_strcmp(tab[i], "<") == 0)
|
|
{
|
|
path = ft_env_filler(data, tab[i + 1]);
|
|
if (path == NULL)
|
|
return_code = 0;
|
|
ft_quote_remover(path);
|
|
free(tab[i + 1]);
|
|
tab[i + 1] = path;
|
|
if (ft_file_is_readable(tab[i + 1]) == 0)
|
|
{
|
|
data->exit_code = 1;
|
|
return_code = -1;
|
|
}
|
|
}
|
|
if (ft_contain_only_str(tab[i + 1], "| <>"))
|
|
return_code = 0;
|
|
}
|
|
}
|
|
if (return_code == 0)
|
|
ft_eprintf("minishell: %s: must be followed by an infile\n", tab[i]);
|
|
ft_freer_tab_ultimate(1, tab);
|
|
return (return_code);
|
|
}
|
|
|
|
static int ft_get_infile(t_data *data, const char *line)
|
|
{
|
|
size_t i;
|
|
int fd;
|
|
char **tab;
|
|
char *path;
|
|
|
|
tab = ft_split_quoted(line, ' ');
|
|
if (tab == NULL)
|
|
{
|
|
ft_eprintf("minishell: malloc failed\n");
|
|
return (-2);
|
|
}
|
|
fd = 0;
|
|
i = 0;
|
|
while (tab[i + 1] != NULL)
|
|
{
|
|
if (tab[i][0] == '<')
|
|
if (fd != 0)
|
|
close(fd);
|
|
if (ft_strcmp("<", tab[i]) == 0)
|
|
{
|
|
path = ft_env_filler(data, tab[i + 1]);
|
|
if (path == NULL)
|
|
return (-2);
|
|
ft_quote_remover(path);
|
|
fd = open(path, O_RDONLY);
|
|
free(path);
|
|
}
|
|
else if (ft_strcmp("<<", tab[i]) == 0)
|
|
fd = ft_heredoc(data, ft_quote_remover(tab[i + 1]));
|
|
i++;
|
|
}
|
|
ft_freer_tab_ultimate(1, tab);
|
|
return (fd);
|
|
}
|
|
|
|
static int ft_remove_infile(char *line)
|
|
{
|
|
size_t i;
|
|
size_t y;
|
|
char **tab;
|
|
|
|
tab = ft_split_quoted(line, ' ');
|
|
if (tab == NULL)
|
|
{
|
|
ft_eprintf("minishell: malloc failed\n");
|
|
return (1);
|
|
}
|
|
i = 0;
|
|
y = 0;
|
|
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++;
|
|
}
|
|
ft_freer_tab_ultimate(1, tab);
|
|
return (0);
|
|
}
|
|
|
|
int ft_infile(t_data *data, char *line)
|
|
{
|
|
int fd;
|
|
|
|
if (ft_infile_is_valid(data, line) == 0)
|
|
return (-2);
|
|
fd = ft_get_infile(data, line);
|
|
if (fd == -2)
|
|
return (-2);
|
|
ft_remove_infile(line);
|
|
return (fd);
|
|
}
|