2023-02-17 10:33:52 -05:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* heredoc.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2023/02/17 15:36:26 by cchauvet #+# #+# */
|
|
|
|
/* Updated: 2023/02/17 16:21:02 by cchauvet ### ########.fr */
|
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
#include "libftx/libftx.h"
|
2023-02-03 10:02:52 -05:00
|
|
|
#include "minishell.h"
|
|
|
|
|
2023-02-23 09:14:08 -05:00
|
|
|
int *ft_get_heredoc()
|
|
|
|
{
|
|
|
|
static int heredoc;
|
|
|
|
|
|
|
|
return (&heredoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ft_heredoc_creator(char *stop)
|
2023-02-03 10:02:52 -05:00
|
|
|
{
|
|
|
|
int fds[2];
|
|
|
|
char *line;
|
|
|
|
|
2023-02-23 09:14:08 -05:00
|
|
|
if (pipe(fds) == -1)
|
|
|
|
return (-1);
|
2023-02-03 10:02:52 -05:00
|
|
|
line = readline("> ");
|
|
|
|
while (line != NULL)
|
|
|
|
{
|
|
|
|
if (ft_strcmp(line, stop) == 0)
|
|
|
|
break ;
|
2023-02-17 10:33:52 -05:00
|
|
|
ft_putendl_fd(line, fds[1]);
|
2023-02-03 10:02:52 -05:00
|
|
|
free(line);
|
|
|
|
line = readline("> ");
|
2023-02-23 09:14:08 -05:00
|
|
|
if (line == NULL)
|
|
|
|
{
|
|
|
|
close(fds[0]);
|
|
|
|
close(fds[1]);
|
|
|
|
return (-1);
|
|
|
|
}
|
2023-02-03 10:02:52 -05:00
|
|
|
}
|
|
|
|
close(fds[1]);
|
|
|
|
return (fds[0]);
|
|
|
|
}
|
2023-02-23 09:14:08 -05:00
|
|
|
|
|
|
|
int ft_heredoc(char *stop)
|
|
|
|
{
|
|
|
|
int pid;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
if (pid == -1)
|
|
|
|
return (-1);
|
|
|
|
if (pid == 0)
|
|
|
|
{
|
|
|
|
fd = ft_heredoc_creator(stop);
|
|
|
|
exit (fd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*ft_get_heredoc() = pid;
|
|
|
|
waitpid(pid, &fd, 0);
|
|
|
|
}
|
|
|
|
*ft_get_heredoc() = 0;
|
|
|
|
return (fd);
|
|
|
|
}
|