mirror of
https://github.com/KeyZox71/ft_minipowershell.git
synced 2025-03-16 03:46:50 +01:00
52 lines
1.5 KiB
C
52 lines
1.5 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* heredoc.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/05/20 09:19:39 by mmoussou #+# #+# */
|
|
/* Updated: 2024/05/20 11:07:21 by mmoussou ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "minishell.h"
|
|
|
|
int ft_heredoc(char *delimiter)
|
|
{
|
|
int fork_pid;
|
|
int tmp_fd;
|
|
int status;
|
|
char *line;
|
|
|
|
tmp_fd = open("/tmp/.minishell-heredoc", O_RDWR | O_CREAT, 0644);
|
|
if (tmp_fd == -1)
|
|
return (-1);
|
|
fork_pid = fork();
|
|
if (fork_pid == -1)
|
|
{
|
|
close(tmp_fd);
|
|
return (-1);
|
|
}
|
|
if (!fork_pid)
|
|
{
|
|
line = readline("heredoc> ");
|
|
while (ft_strcmp(line, delimiter))
|
|
{
|
|
status = write(tmp_fd, line, ft_strlen(line));
|
|
status = write(tmp_fd, "\n", 1);
|
|
free(line);
|
|
line = readline("heredoc> ");
|
|
}
|
|
status = write(tmp_fd, "\0", 1);
|
|
if (status == -1)
|
|
{
|
|
close(tmp_fd);
|
|
return (-1);
|
|
}
|
|
}
|
|
else
|
|
waitpid(fork_pid, NULL, 0);
|
|
return (tmp_fd);
|
|
}
|