42_minishell/heredoc.c
Camille Chauvet 405c1937cd fix
2023-02-23 15:14:08 +01:00

70 lines
1.6 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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"
#include "minishell.h"
int *ft_get_heredoc()
{
static int heredoc;
return (&heredoc);
}
int ft_heredoc_creator(char *stop)
{
int fds[2];
char *line;
if (pipe(fds) == -1)
return (-1);
line = readline("> ");
while (line != NULL)
{
if (ft_strcmp(line, stop) == 0)
break ;
ft_putendl_fd(line, fds[1]);
free(line);
line = readline("> ");
if (line == NULL)
{
close(fds[0]);
close(fds[1]);
return (-1);
}
}
close(fds[1]);
return (fds[0]);
}
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);
}