From d6cee7ca8a59f8e5348a1d5dbbab723a6db35f93 Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Tue, 15 Sep 2020 18:51:24 +0200
Subject: type builtin fix

---
 src/b_type.c | 68 ++++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 39 insertions(+), 29 deletions(-)

(limited to 'src')

diff --git a/src/b_type.c b/src/b_type.c
index e55a87c..c09f441 100644
--- a/src/b_type.c
+++ b/src/b_type.c
@@ -22,8 +22,7 @@
 #include "e_externs_next.h"
 #include "u_utils.h"
 
-static int8_t
-	absolute_path_exists(char com[])
+static int8_t	absolute_path_exists(char com[])
 {
 	int32_t	fd;
 	DIR		*dir;
@@ -41,9 +40,7 @@ static int8_t
 	return (0);
 }
 
-static char
-	*type_get_path(char com[],
-					t_msh *msh)
+static char		*type_get_path(char com[], t_msh *msh)
 {
 	char	**envpath;
 	char	*fullpath;
@@ -70,49 +67,62 @@ static char
 	return (fullpath);
 }
 
-static uint8_t
-	chk_nonbuilt(char **ptr,
-					t_msh *msh)
+static uint8_t	b_check_nonbuilt(char *ptr, uint8_t ret, t_msh *msh)
 {
 	char	*fullpath;
-	int32_t	ret;
 
-	ret = 0;
-	fullpath = type_get_path(*ptr, msh);
-	if (fullpath)
-		ft_printf("%s is %s\n", *ptr, fullpath);
+	fullpath = type_get_path(ptr, msh);
+	if (fullpath != NULL)
+		ft_printf("%s is %s\n", ptr, fullpath);
 	else
 	{
-		ft_printf("minishell: type: %s: not found\n", *ptr);
+		ft_printf("minishell: type: %s: not found\n", ptr);
 		ret = 1;
 	}
 	ft_memdel((void*)&fullpath);
 	return (ret);
 }
 
-uint8_t
-	b_type(char *args[],
-			t_msh *msh)
+static uint8_t	b_check_builtins(char *ptr, t_msh *msh)
+{
+	char		**p_bu;
+
+	p_bu = msh->bu_ref;
+	while (*p_bu != NULL && ft_strncmp(ptr, *p_bu, ft_strlen(*p_bu) + 1) != 0)
+	{
+		p_bu++;
+	}
+	if (*p_bu != NULL)
+	{
+		ft_printf("%s is a shell builtin\n", ptr);
+		return (0);
+	}
+	return (1);
+}
+
+uint8_t			b_type(char *args[], t_msh *msh)
 {
-	char	**ptr;
-	char	**p_bu;
-	int32_t	ret;
+	t_lalias	*p_alias;
+	char		**ptr;
+	int32_t		ret;
 
 	ptr = args;
-	if (!*ptr)
+	if (*ptr == NULL)
 		return (0);
 	ret = 0;
-	while (*ptr)
+	while (*ptr != NULL)
 	{
-		p_bu = msh->bu_ref;
-		while (*p_bu && ft_strncmp(*ptr, *p_bu, ft_strlen(*p_bu) + 1))
-			p_bu++;
-		if (*p_bu != NULL)
-			ft_printf("%s is a shell builtin\n", *ptr);
-		else
+		p_alias = msh->alias;
+		while (p_alias != NULL &&
+			ft_strncmp(*ptr, p_alias->name,
+			ft_strlen(p_alias->name) + 1) != 0)
+			p_alias = p_alias->next;
+		if (p_alias != NULL)
 		{
-			ret = chk_nonbuilt(ptr, msh);
+			ft_printf("%s is aliased to `%s'\n", *ptr, p_alias->val);
 		}
+		else if (b_check_builtins(*ptr, msh) == 1)
+			ret = b_check_nonbuilt(*ptr, ret, msh);
 		ptr++;
 	}
 	return (ret);
-- 
cgit v1.2.3