/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* heredoc.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cchauvet int *ft_get_heredoc() { static int heredoc; return (&heredoc); } int ft_heredoc_creator(char *stop, int fd) { char *line; line = readline("> "); while (line != NULL) { if (ft_strcmp(line, stop) == 0) break ; ft_putendl_fd(line, fd); free(line); line = readline("> "); if (line == NULL) return (1); } return (0); } int ft_heredoc(char *stop) { int pid; int fds[2]; int exit_code; if (pipe(fds) == -1) return (-1); pid = fork(); if (pid == -1) return (-1); if (pid == 0) { exit_code = ft_heredoc_creator(stop, fds[1]); exit (exit_code + 1); } else { *ft_get_heredoc() = pid; waitpid(pid, &exit_code, 0); *ft_get_heredoc() = 0; close(fds[1]); return (fds[0]); } }