From 9f626ecdde8f9662468bb633e02854982ca80311 Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Wed, 5 Aug 2020 20:34:04 +0200
Subject: fixed pwd

---
 src/b_cd.c | 90 +++++++++++++++++++++-----------------------------------------
 1 file changed, 30 insertions(+), 60 deletions(-)

(limited to 'src')

diff --git a/src/b_cd.c b/src/b_cd.c
index 8bd1d2f..b083986 100644
--- a/src/b_cd.c
+++ b/src/b_cd.c
@@ -60,6 +60,7 @@ static void
 	{
 		ft_sprintf(fmt, "%s=%s", "OLDPWD", pwd);
 		b_export_with_equals(fmt, msh);
+		ft_memdel((void*)pwd);
 	}
 	else
 	{
@@ -70,21 +71,31 @@ static void
 }
 
 static void
-	b_fill_repath(char *repath[],
+	b_fill_repath(char repath[],
 				char *splited[])
 {
 	size_t	i;
 	size_t	j;
 
 	i = 0;
+	repath[0] = (splited[0] == NULL) ? '/' : repath[0];
 	while (splited[i] != NULL)
 	{
 		if (ft_strncmp(splited[i], "..", 3) == 0)
 		{
-			j = ft_strlen(*repath);
-			while (*repath[j] != '/')
+			j = ft_strlen(repath);
+			while (repath[j] != '/' && j > 0)
 				j--;
-			*repath[j] = '\0';
+			repath[j] = '\0';
+		}
+		else if (ft_strncmp(splited[i], ".", 2) == 0)
+		{
+		}
+		else
+		{
+			j = ft_strlen(repath) + 1;
+			repath[j - 1] = '/';
+			ft_strlcpy(repath + j, splited[i], ft_strlen(splited[i]) + 1);
 		}
 		i++;
 	}
@@ -93,71 +104,30 @@ static void
 static void
 	b_upgrade_pwd(const char path[], t_msh *msh)
 {
+	char	*tmp;
 	char	**splited;
 	char	repath[262144];
-	t_bool	slash_first;
+	char	fmt[262144];
 
 	b_set_oldpwd(msh);
-	slash_first = FALSE;
-	if (path[0] == '/')
-		slash_first = TRUE;
-	if (slash_first == TRUE)
-		ft_memcpy(repath, "/", 2);
-	else 
-	{
-		/* msh->cwd; */
-	}
+	repath[0] = '\0';
+	if (path[0] != '/')
+		ft_memcpy(repath, msh->cwd, ft_strlen(msh->cwd));
 	if ((splited = ft_split(path, '/')) == NULL)
 		f_fail_alloc_and_destroy(msh);
-	b_fill_repath((char**)&repath, splited);
+	b_fill_repath(repath, splited);
+	repath[0] = (repath[0] == '\0') ? '/' : repath[0];
 	ft_delwords(splited);
-	u_subst_var_value("$PWD", repath, msh);
-	ft_printf("[%s]\n", repath);
-}
-
-/* TODO: fix export OLDPWDQWE= */
-
-static void
-	b_old_upgrade_pwd(const char path[], t_msh *msh)
-{
-	char	**splited;
-	char	*rtmp;
-	char	repath[131072];
-	size_t	i;
-	size_t	j;
-
-	if (path[0] == '/')
-	{
-		u_subst_var_value("$PWD", path, msh);
-		/* TODO: /home///////////////////////////////////jozan////////////// */
-		return ;
-	}
-	rtmp = u_get_var_value("$PWD", msh);
-	ft_memcpy(repath, rtmp, ft_strlen(rtmp) + 1);
-	ft_memdel((void*)&rtmp);
-	splited = ft_split(path, '/');
-	i = 0;
-	while (splited[i] != '\0')
+	if ((tmp = u_get_var_value("$PWD", msh)) != NULL)
+		u_subst_var_value("$PWD", repath, msh);
+	else
 	{
-		ft_printf("{%s}\n{%s}\n", path, path + i);
-		if (splited[i][0] == '.' && splited[i][1] == '.')
-		{
-			j = ft_strlen(repath);
-			ft_printf("[%s]\n", repath);
-			while (repath[j] != '/')
-				j--;
-			repath[j] = '\0';
-		}
-		else
-		{
-			j = ft_strlen(repath) + 1;
-			repath[j - 1] = '/';
-			ft_strlcpy(repath + j, splited[i], ft_strlen(splited[i]) + 1);
-		}
-		i++;
+		ft_sprintf(fmt, "%s=%s", "PWD", repath);
+		b_export_with_equals(fmt, msh);
 	}
-	ft_delwords(splited);
-	u_subst_var_value("$PWD", repath, msh);
+	ft_memdel((void*)&msh->cwd);
+	if ((msh->cwd = ft_strdup(repath)) == NULL)
+		f_fail_alloc_and_destroy(msh);
 }
 
 uint8_t
-- 
cgit v1.2.3