From 86b25977265727217ec3360c3dc19b74f2bd448f Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Tue, 28 Apr 2020 15:49:58 +0200
Subject: exit is fine

---
 src/ft_b_cd.c       | 38 ++++++++++++++++++++++----------------
 src/ft_b_exit.c     | 11 +++--------
 src/ft_e_builtins.c | 17 ++++++++++++++---
 3 files changed, 39 insertions(+), 27 deletions(-)

(limited to 'src')

diff --git a/src/ft_b_cd.c b/src/ft_b_cd.c
index 54dc92e..fbd0282 100644
--- a/src/ft_b_cd.c
+++ b/src/ft_b_cd.c
@@ -20,11 +20,31 @@
 #include "ft_u_utils.h"
 #include "ft_u_vars.h"
 
+static void
+	ft_set_path(char **path,
+				char *args[],
+				t_msh *msh)
+{
+	if (!(*path = ft_strdup(*args)))
+	{
+		ft_s_destroy(msh);
+		ft_fail_alloc(msh);
+	}
+	if (!ft_strncmp("~/", *path, 2) || !ft_strncmp("~", *path, 2))
+	{
+		if (!(*path = ft_strsubst(*path,
+			"~", ft_subst_var_value("$HOME", msh))))
+		{
+			ft_s_destroy(msh);
+			ft_fail_alloc(msh);
+		}
+	}
+}
+
 uint8_t
 	ft_b_cd(char *args[],
 			t_msh *msh)
 {
-	/* TODO: norme */
 	const uint64_t	argc = ft_get_argc((const char**)args);
 	char			*path;
 
@@ -39,21 +59,7 @@ uint8_t
 			return (1);
 	}
 	else
-	{
-		if (!(path = ft_strdup(*args)))
-		{
-			ft_s_destroy(msh);
-			ft_fail_alloc(msh);
-		}
-		if (!ft_strncmp("~/", path, 2) || !ft_strncmp("~", path, 2))
-		{
-			if (!(path = ft_strsubst(path, "~", ft_subst_var_value("$HOME", msh))))
-			{
-				ft_s_destroy(msh);
-				ft_fail_alloc(msh);
-			}
-		}
-	}
+		ft_set_path(&path, args, msh);
 	if (chdir(path) != 0)
 	{
 		ft_fail_chd("cd", path, msh);
diff --git a/src/ft_b_exit.c b/src/ft_b_exit.c
index ae421cf..7a7e5e8 100644
--- a/src/ft_b_exit.c
+++ b/src/ft_b_exit.c
@@ -13,6 +13,8 @@
 #include <libft.h>
 #include <stdlib.h>
 #include <stdint.h>
+#include <unistd.h>
+
 #include "ft_f_fail.h"
 #include "ft_s_lcom.h"
 #include "ft_s_destroy.h"
@@ -36,13 +38,6 @@ uint8_t
 		ret = ft_atoi(args[0]);
 		 /* TODO: non numeric args[0] */
 	}
-	else
-	{
-		ret = msh->ret;
-	}
-	ft_lcom_clear(&msh->curr);
-	ft_s_destroy(msh);
-	ft_printf("exit\n");
-	exit(ret);
+	ft_dprintf(STDERR_FILENO, "exit\n");
 	return (0);
 }
diff --git a/src/ft_e_builtins.c b/src/ft_e_builtins.c
index c78a80c..382f85d 100644
--- a/src/ft_e_builtins.c
+++ b/src/ft_e_builtins.c
@@ -42,17 +42,28 @@ static void
 						t_msh *msh)
 {
 	int32_t	status;
+	int32_t	ret;
 
 	while (wait(&status) != pid)
 		;
-	msh->ret = WEXITSTATUS(status);
+	ret = WEXITSTATUS(status);
+	if (bu_id != 6)
+		msh->ret = ret;
 	if (bu_id == 1 && msh->ret == 0)
 	{
 		msh->bu_ptr[bu_id](ptr->argv + 1, msh);
 		/* TODO: export $PWD */
 	}
-	else if (bu_id == 6)
-		msh->bu_ptr[bu_id](ptr->argv + 1, msh);
+	else if (bu_id == 6 && ret == 0)
+	{
+		if (ptr->argv[1])
+			ret = ft_atoi(ptr->argv[1]);
+		else
+			ret = msh->ret;
+		ft_lcom_clear(&msh->curr);
+		ft_s_destroy(msh);
+		exit(ret);
+	}
 }
 
 void
-- 
cgit v1.2.3