From 418c6adb9b6c60dce662778257abb93c216da891 Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Mon, 24 Aug 2020 19:35:05 +0200
Subject: Freed leaks, fix in progress

---
 src/b_cd.c          | 25 +++++++++++++------------
 src/b_export_next.c |  1 +
 2 files changed, 14 insertions(+), 12 deletions(-)

(limited to 'src')

diff --git a/src/b_cd.c b/src/b_cd.c
index d82ab9c..daf839d 100644
--- a/src/b_cd.c
+++ b/src/b_cd.c
@@ -73,7 +73,7 @@ static void
 	{
 		if (ft_strncmp(splited[i], "..", 3) == 0)
 		{
-			j = ft_strlen(repath);
+			j = (repath[0] == '\0') ? (0) : (ft_strlen(repath));
 			while (repath[j] != '/' && j > 0)
 				j--;
 			repath[j] = '\0';
@@ -94,27 +94,28 @@ static void
 static void
 	b_upgrade_pwd(const char path[], t_msh *msh)
 {
-	char	*tmp;
 	char	**splited;
 	char	repath[262144];
 	char	fmt[262144];
 
 	b_set_oldpwd(msh);
+	splited = NULL;
 	repath[0] = '\0';
 	if (path[0] != '/')
-		ft_memcpy(repath, msh->cwd, ft_strlen(msh->cwd));
-	if ((splited = ft_split(path, '/')) == NULL)
+		ft_memcpy(repath, msh->cwd, (ft_strlen(msh->cwd) + 1) * sizeof(char));
+	if (path[0] == '/' && path[1] == '\0')
+		ft_memcpy(repath, "/", 2 * sizeof(char));
+	else if ((splited = ft_split(path, '/')) == NULL)
 		f_alloc_and_destroy_msh(msh);
-	b_fill_repath(repath, splited);
-	repath[0] = (repath[0] == '\0') ? '/' : repath[0];
-	ft_delwords(splited);
-	if ((tmp = u_get_var_value("$PWD", msh)) != NULL)
-		u_subst_var_value("$PWD", repath, msh);
-	else
+	if (splited != NULL)
 	{
-		ft_sprintf(fmt, "%s=%s", "PWD", repath);
-		b_export_with_equals(fmt, msh);
+		b_fill_repath(repath, splited);
+		ft_delwords(splited);
 	}
+	repath[0] = (repath[0] == '\0') ? '/' : repath[0];
+	ft_printf("[%s]\n", repath);
+	ft_sprintf(fmt, "%s=%s", "PWD", repath);
+	b_export_with_equals(fmt, msh);
 	ft_memdel((void*)&msh->cwd);
 	if ((msh->cwd = ft_strdup(repath)) == NULL)
 		f_alloc_and_destroy_msh(msh);
diff --git a/src/b_export_next.c b/src/b_export_next.c
index 90a4820..c46389a 100644
--- a/src/b_export_next.c
+++ b/src/b_export_next.c
@@ -56,6 +56,7 @@ static int64_t
 	{
 		if (ft_strncmp(varname, env_dup[i], ft_strlen(env_dup[i]) + 1) == 0)
 		{
+			ft_delwords(env_dup);
 			return (i);
 		}
 		i++;
-- 
cgit v1.2.3