/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* heredoc.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: mmoussou 0) fd = open(path, O_RDONLY); else fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644); return (fd); } static int get_input(char *delimiter, int fd) { char *line; int status; line = readline("heredoc> "); while (ft_strcmp(line, delimiter)) { status = write(fd, line, ft_strlen(line)); if (status == -1) fd_manager(fd); if (status == -1) return (-1); status = write(fd, "\n", 1); if (status == -1) fd_manager(fd); if (status == -1) return (-1); free(line); line = readline("heredoc> "); } free(line); status = write(fd, "\0", 1); if (status == -1) fd_manager(fd); return (-(status == -1)); } int ft_heredoc(char *delimiter) { int fork_pid; int fd; fd = fd_manager(0); if (fd == -1) { fd_manager(-1); return (-1); } fork_pid = fork(); if (fork_pid == -1) { fd_manager(-1); close(fd); return (-1); } if (!fork_pid) { get_input(delimiter, fd); exit(0); } else waitpid(fork_pid, NULL, 0); return (fd_manager(1)); }