From 6baf8c11080bc73f4a55b770c2adde9536bc513c Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Tue, 18 Aug 2020 18:11:04 +0200
Subject: HUGE pain in the ass finally over

---
 TODO.org         |  4 ++--
 src/e_builtins.c |  2 ++
 src/e_externs.c  |  2 ++
 src/u_utils.c    | 11 +++++++++++
 src/u_utils.h    |  1 +
 5 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/TODO.org b/TODO.org
index 49cdd51..ab74e4e 100644
--- a/TODO.org
+++ b/TODO.org
@@ -3,10 +3,10 @@
 ** DONE VISUAL=qweqwe crontab -e <=== deal with it
 ** DONE cd qweqwe <=== stderr
 ** DONE [#A] lpipes leaks <=== this is a big one (split lpipes | lcom | com)
-** TODO [#A] /true/path/to/non-existant/bin/fail-plus-bad-fork
+** DONE [#A] /true/path/to/non-existant/bin/fail-plus-bad-fork
 ** DONE [#B] forked write(2) stuff on cd
 ** TODO [#B] ./qwe.sh <=== handle bad shebang
-** TODO [#C] fix that goddamn exit(2) 25 leak
+** DONE [#C] fix that goddamn exit(2) 25 leak
 ** TODO [#C] lpipes leaks on builtins (same as above man)
 ** TODO [#C] leaks on builtins (same as above man)
 ** TODO [#C] Handle memory
diff --git a/src/e_builtins.c b/src/e_builtins.c
index e1dbfbb..18abc16 100644
--- a/src/e_builtins.c
+++ b/src/e_builtins.c
@@ -22,6 +22,7 @@
 #include "s_destroy.h"
 #include "s_line.h"
 #include "s_struct.h"
+#include "u_utils.h"
 
 static void
 	e_export_env_fork(const t_com *ptr,
@@ -48,6 +49,7 @@ static void
 		e_export_env_fork(ptr, msh);
 	dup_redirs(ptr, msh);
 	ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh);
+	u_eof_stdin();
 	s_line_clear(&msh->curr);
 	s_destroy(msh);
 	exit(ret);
diff --git a/src/e_externs.c b/src/e_externs.c
index ce0d41d..9e2f8f4 100644
--- a/src/e_externs.c
+++ b/src/e_externs.c
@@ -25,6 +25,7 @@
 #include "s_destroy.h"
 #include "s_line.h"
 #include "s_struct.h"
+#include "u_utils.h"
 
 static void
 	e_extern_child(const char *fullpath,
@@ -36,6 +37,7 @@ static void
 	{
 		f_exec(fullpath);
 		ft_memdel((void*)&fullpath);
+		u_eof_stdin();
 		s_line_clear(&msh->curr);
 		s_destroy(msh);
 		exit(errno);
diff --git a/src/u_utils.c b/src/u_utils.c
index 5e31060..f50d861 100644
--- a/src/u_utils.c
+++ b/src/u_utils.c
@@ -13,10 +13,21 @@
 #include <libft.h>
 #include <stdlib.h>
 #include <stdint.h>
+#include <unistd.h>
 
 #include "f_fail.h"
 #include "s_struct.h"
 
+void
+u_eof_stdin(void)
+{
+	char	*line;
+
+	close(STDIN_FILENO);
+	get_next_line(STDIN_FILENO, &line);
+	ft_memdel((void*)&line);
+}
+
 char
 	**u_get_env_var_names(t_msh *msh)
 {
diff --git a/src/u_utils.h b/src/u_utils.h
index d0cf323..6362e14 100644
--- a/src/u_utils.h
+++ b/src/u_utils.h
@@ -15,6 +15,7 @@
 
 #include <stdint.h>
 
+void		u_eof_stdin(void);
 uint64_t	u_builtins_get_argc(const char *args[]);
 char		**u_get_env_var_names(t_msh *msh);
 
-- 
cgit v1.2.3