🏗️」 wip(heredoc): working on the heredoc...

This commit is contained in:
y-syo
2024-05-20 11:22:25 +02:00
parent 117c2e7fbe
commit 7b18eb6327
3 changed files with 56 additions and 1 deletions

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/29 13:20:22 by mmoussou #+# #+# */ /* Created: 2024/04/29 13:20:22 by mmoussou #+# #+# */
/* Updated: 2024/05/19 04:27:24 by mmoussou ### ########.fr */ /* Updated: 2024/05/20 10:57:33 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,6 +22,8 @@ typedef struct s_env
struct s_env *next; struct s_env *next;
} t_env; } t_env;
int ft_heredoc(char *delimiter);
int env_init(char **env_d, t_env *env); int env_init(char **env_d, t_env *env);
void env_print(t_env *env); void env_print(t_env *env);
char **env_get(t_env *env); char **env_get(t_env *env);

View File

@ -20,6 +20,8 @@
# include <stdint.h> # include <stdint.h>
# include <sys/types.h> # include <sys/types.h>
# include <sys/stat.h> # include <sys/stat.h>
# include <sys/wait.h>
# include <fcntl.h>
# include <unistd.h> # include <unistd.h>
# include <dirent.h> # include <dirent.h>
# include "libft.h" # include "libft.h"

51
src/exec/heredoc.c Normal file
View File

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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);
}