42_minishell/infile.c

111 lines
2.5 KiB
C
Raw Normal View History

2023-02-15 14:52:27 -05:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* infile.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 17:52:10 by cchauvet #+# #+# */
2023-02-20 07:08:01 -05:00
/* Updated: 2023/02/20 13:05:43 by starnakin ### ########.fr */
2023-02-15 14:52:27 -05:00
/* */
/* ************************************************************************** */
#include "libftx/libftx.h"
2023-02-02 11:27:26 -05:00
#include "minishell.h"
2023-02-15 14:52:27 -05:00
#include "utils/utils.h"
static int ft_infile_is_valid(const char *line)
{
char **tab;
size_t i;
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-02-15 14:52:27 -05:00
i = 0;
while (tab[i + 1] != NULL)
i++;
if (tab[i][0] == '<')
{
ft_eprintf("minishell: %s: must be followed by an infile\n", tab[i]);
2023-02-17 07:21:50 -05:00
ft_freer_tab_ultimate(1, tab);
2023-02-15 14:52:27 -05:00
return (0);
}
ft_freer_tab_ultimate(1, tab);
return (1);
}
2023-02-02 11:27:26 -05:00
2023-02-15 14:52:27 -05:00
static int ft_get_infile(const char *line)
2023-02-02 11:27:26 -05:00
{
size_t i;
2023-02-03 10:02:52 -05:00
int fd;
2023-02-15 14:52:27 -05:00
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 (-2);
}
2023-02-03 10:02:52 -05:00
fd = 0;
2023-02-02 11:27:26 -05:00
i = 0;
2023-02-15 14:52:27 -05:00
while (tab[i + 1] != NULL)
2023-02-02 11:27:26 -05:00
{
if (tab[i][0] == '<')
if (fd != 0)
close(fd);
2023-02-15 14:52:27 -05:00
if (ft_strcmp("<", tab[i]) == 0)
fd = ft_file_is_readable(ft_quote_remover(tab[i + 1]));
else if (ft_strcmp("<<", tab[i]) == 0)
fd = ft_heredoc(tab[i + 1]);
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-03 10:02:52 -05:00
return (fd);
2023-02-02 11:27:26 -05:00
}
static int ft_remove_infile(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-02-20 07:08:01 -05:00
ft_strshift(line + y,
2023-02-23 12:05:21 -05:00
-1 * (ft_strlen(tab[i]) + ft_strlen(tab[i + 1]) + 2));
2023-02-15 14:52:27 -05:00
else
2023-02-20 07:08:01 -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);
}
int ft_infile(char *line)
{
int fd;
2023-02-15 14:52:27 -05:00
if (ft_infile_is_valid(line) == 0)
return (-2);
2023-02-02 11:27:26 -05:00
fd = ft_get_infile(line);
2023-02-15 14:52:27 -05:00
if (fd == -2)
return (-2);
ft_remove_infile(line);
2023-02-02 11:27:26 -05:00
return (fd);
}