From 8b6c386883878eec839103305b20cb4d63eef873 Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Tue, 28 Apr 2020 16:05:07 +0200
Subject: duped completely envp

---
 src/ft_s_init.c | 44 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 43 insertions(+), 1 deletion(-)

(limited to 'src')

diff --git a/src/ft_s_init.c b/src/ft_s_init.c
index 76bf72d..463cb0b 100644
--- a/src/ft_s_init.c
+++ b/src/ft_s_init.c
@@ -11,6 +11,7 @@
 /* ************************************************************************** */
 
 #include <libft.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <unistd.h>
 
@@ -18,6 +19,45 @@
 #include "ft_m_funptr.h"
 #include "ft_s_init.h"
 
+static char
+	**ft_dupenv_del(char **nenvp,
+					uint64_t i)
+{
+	while (i > 0)
+	{
+		ft_memdel((void*)&nenvp[i]);
+		i--;
+	}
+	ft_memdel((void*)&nenvp);
+	return (NULL);
+}
+
+static char
+	**ft_dupenv(char *envp[])
+{
+	uint64_t	i;
+	char		**nenvp;
+
+	i = 0;
+	while (envp[i])
+	{
+		i++;
+	}
+	if (!(nenvp = (char**)malloc((i + 1) * sizeof(char*))))
+	{
+		return (NULL);
+	}
+	i = 0;
+	while (envp[i])
+	{
+		if (!(nenvp[i] = ft_strdup(envp[i])))
+			return (ft_dupenv_del(nenvp, i));
+		i++;
+	}
+	nenvp[i] = NULL;
+	return (nenvp);
+}
+
 t_msh
 	*ft_init_msh(const char *argv[],
 				char *envp[])
@@ -34,7 +74,9 @@ t_msh
 	msh->cwd = NULL;
 	msh->cwd = getcwd(NULL, 0);
 	/* TODO: handle getcwd failed */
-	msh->envp = envp;
+	msh->envp = NULL;
+	if (!(msh->envp = ft_dupenv(envp)))
+		return (NULL);
 	msh->ret = 0;
 	ft_init_buptr(msh);
 	msh->curr = NULL;
-- 
cgit v1.2.3