From 0d4d084859348ecc41ac2b3997ddc2be984636bd Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Thu, 23 Apr 2020 19:02:13 +0200
Subject: Parsed bin path in $PATH, now time to execve(2)

---
 Makefile                |   2 +
 src/ft_b_builtins.h     |   1 +
 src/ft_b_type.c         |  24 +++++++++++
 src/ft_b_type.h         |  22 ++++++++++
 src/ft_d_define.h       |   4 +-
 src/ft_e_builtins.c     |   4 +-
 src/ft_e_externs.c      |  55 ++-----------------------
 src/ft_e_externs_next.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/ft_e_externs_next.h |  23 +++++++++++
 src/ft_m_funptr.c       |   3 +-
 10 files changed, 187 insertions(+), 57 deletions(-)
 create mode 100644 src/ft_b_type.c
 create mode 100644 src/ft_b_type.h
 create mode 100644 src/ft_e_externs_next.c
 create mode 100644 src/ft_e_externs_next.h

diff --git a/Makefile b/Makefile
index c08cabe..6f6008f 100644
--- a/Makefile
+++ b/Makefile
@@ -21,9 +21,11 @@ SRCS_NAME		+= ft_b_env.c
 SRCS_NAME		+= ft_b_exit.c
 SRCS_NAME		+= ft_b_export.c
 SRCS_NAME		+= ft_b_pwd.c
+SRCS_NAME		+= ft_b_type.c
 SRCS_NAME		+= ft_b_unset.c
 SRCS_NAME		+= ft_e_builtins.c
 SRCS_NAME		+= ft_e_externs.c
+SRCS_NAME		+= ft_e_externs_next.c
 SRCS_NAME		+= ft_e_lcom.c
 SRCS_NAME		+= ft_f_fail.c
 SRCS_NAME		+= ft_f_file.c
diff --git a/src/ft_b_builtins.h b/src/ft_b_builtins.h
index 36e6498..ed76f70 100644
--- a/src/ft_b_builtins.h
+++ b/src/ft_b_builtins.h
@@ -19,6 +19,7 @@
 #include "ft_b_exit.h"
 #include "ft_b_export.h"
 #include "ft_b_pwd.h"
+#include "ft_b_type.h"
 #include "ft_b_unset.h"
 
 #endif
diff --git a/src/ft_b_type.c b/src/ft_b_type.c
new file mode 100644
index 0000000..f17ca45
--- /dev/null
+++ b/src/ft_b_type.c
@@ -0,0 +1,24 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_b_type.c                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
+/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include <stdint.h>
+#include "ft_s_struct.h"
+
+uint8_t
+	ft_b_type(char *args[],
+			t_msh *msh)
+{
+	(void)args;
+	(void)msh;
+	/* TODO: do type */
+	return (0);
+}
diff --git a/src/ft_b_type.h b/src/ft_b_type.h
new file mode 100644
index 0000000..c3d5dd6
--- /dev/null
+++ b/src/ft_b_type.h
@@ -0,0 +1,22 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_b_type.h                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
+/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef FT_B_TYPE_H
+#define FT_B_TYPE_H
+
+#include <stdint.h>
+#include "ft_s_struct.h"
+
+uint8_t	ft_b_type(char *args[],
+				t_msh *msh);
+
+#endif
diff --git a/src/ft_d_define.h b/src/ft_d_define.h
index 2a8002d..f85fd46 100644
--- a/src/ft_d_define.h
+++ b/src/ft_d_define.h
@@ -20,8 +20,8 @@
 */
 
 #define FT_PS_ONE			"minishell ~> "
-#define FT_BUILTINS			"echo|cd|pwd|export|unset|env|exit"
-#define FT_BUILTINS_COUNT	7
+#define FT_BUILTINS			"echo|cd|pwd|export|unset|env|exit|type"
+#define FT_BUILTINS_COUNT	8
 
 /*
 ** ====== FAIL MSG ======
diff --git a/src/ft_e_builtins.c b/src/ft_e_builtins.c
index 4f2c551..57ba28b 100644
--- a/src/ft_e_builtins.c
+++ b/src/ft_e_builtins.c
@@ -74,9 +74,7 @@ static void
 	int32_t	status;
 
 	while (wait(&status) != pid)
-	{
-		/* TODO: fix this unnormed syntax */
-	}
+		;
 	msh->ret = WEXITSTATUS(status);
 }
 
diff --git a/src/ft_e_externs.c b/src/ft_e_externs.c
index 9b226d9..dbbaceb 100644
--- a/src/ft_e_externs.c
+++ b/src/ft_e_externs.c
@@ -11,63 +11,16 @@
 /* ************************************************************************** */
 
 #include <libft.h>
-#include <dirent.h>
 
-#include "ft_f_fail.h"
-#include "ft_s_destroy.h"
-#include "ft_s_lcom.h"
+#include "ft_e_externs_next.h"
 #include "ft_s_struct.h"
 
-static void
-	ft_search_in_path(const char *com,
-					char **envpath)
-{
-	char	**p_path;
-
-	(void)com;
-	p_path = envpath;
-	while (*p_path)
-	{
-		/* TODO: directory(3) */
-		/* opendir() | readdir() | closedir() */
-		p_path++;
-	}
-}
-
-static char
-	**ft_get_env_path(t_msh *msh)
-{
-	char	**p_env;
-	char	**envpath;
-	char	*envline;
-
-	p_env = msh->envp;
-	while (*p_env && ft_strncmp("PATH", *p_env, 4) != 0)
-	{
-		p_env++;
-	}
-	if (*p_env == NULL)
-		return (NULL);
-	envline = ft_strchr(*p_env, '=');
-	envline += 1;
-	if (*envline != '\0')
-	{
-		if (!(envpath = ft_split(envline, ':')))
-		{
-			ft_lcom_clear(&msh->curr);
-			ft_s_destroy(msh);
-			ft_fail_alloc();
-		}
-		return (envpath);
-	}
-	return (NULL);
-}
-
 void
 	ft_e_extern(t_lcom *ptr,
 				t_msh *msh)
 {
 	char	**envpath;
+	char	*fullpath;
 
 	if (ft_ischarset("/.", ptr->com[0]))
 	{
@@ -75,8 +28,8 @@ void
 	}
 	else if ((envpath = ft_get_env_path(msh)) != NULL)
 	{
-		ft_search_in_path(ptr->com, envpath);
+		fullpath = ft_search_in_path(ptr->com, envpath, msh);
+		/* TODO: exec $PATH stuff */
 		ft_delwords(envpath);
 	}
-	/* TODO: exec $PATH stuff | initiate all builtins first, even uncomplete */
 }
diff --git a/src/ft_e_externs_next.c b/src/ft_e_externs_next.c
new file mode 100644
index 0000000..492d7e5
--- /dev/null
+++ b/src/ft_e_externs_next.c
@@ -0,0 +1,106 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_e_externs_next.c                                :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
+/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include <libft.h>
+#include <dirent.h>
+#include <stdlib.h>
+#include <stddef.h>
+
+#include "ft_f_fail.h"
+#include "ft_s_destroy.h"
+#include "ft_s_lcom.h"
+#include "ft_s_struct.h"
+
+static char
+	*ft_get_fullpath(const char *p_path,
+					const char *d_name,
+					t_msh *msh)
+{
+	char			*fullpath;
+	const size_t	path_len = ft_strlen(p_path);
+	const size_t	name_len = ft_strlen(d_name);
+
+	if (!(fullpath = (char*)malloc((path_len + name_len + 2) * sizeof(char))))
+	{
+		ft_lcom_clear(&msh->curr);
+		ft_s_destroy(msh);
+		ft_fail_alloc();
+	}
+	ft_memcpy(fullpath, p_path, path_len);
+	*(fullpath + (path_len)) = '/';
+	ft_memcpy(fullpath + path_len + 1, d_name, name_len);
+	*(fullpath + (path_len + name_len + 1)) = '\0';
+	return (fullpath);
+}
+
+char
+	*ft_search_in_path(const char *com,
+					char **envpath,
+					t_msh *msh)
+{
+	/* TODO: norme */
+	struct dirent	*ent;
+	char			**p_path;
+	char			*fullpath;
+	DIR				*dir;
+
+	(void)com;
+	p_path = envpath;
+	while (*p_path)
+	{
+		if ((dir = opendir(*p_path)) != NULL)
+		{
+			while ((ent = readdir(dir)) != NULL)
+			{
+				/* TODO: check for not bins (dirs, etc) */
+				if (ft_strncmp(com, ent->d_name, ft_strlen(com) + 1) == 0)
+				{
+					fullpath = ft_get_fullpath(*p_path, ent->d_name, msh);
+					closedir(dir);
+					return (fullpath);
+				}
+			}
+			closedir(dir);
+		}
+		p_path++;
+	}
+	return (NULL);
+}
+
+char
+	**ft_get_env_path(t_msh *msh)
+{
+	char	**p_env;
+	char	**envpath;
+	char	*envline;
+
+	p_env = msh->envp;
+	while (*p_env && ft_strncmp("PATH", *p_env, 4) != 0)
+	{
+		p_env++;
+	}
+	if (*p_env == NULL)
+		return (NULL);
+	envline = ft_strchr(*p_env, '=');
+	envline += 1;
+	if (*envline != '\0')
+	{
+		if (!(envpath = ft_split(envline, ':')))
+		{
+			ft_lcom_clear(&msh->curr);
+			ft_s_destroy(msh);
+			ft_fail_alloc();
+		}
+		return (envpath);
+	}
+	return (NULL);
+}
diff --git a/src/ft_e_externs_next.h b/src/ft_e_externs_next.h
new file mode 100644
index 0000000..e59e10b
--- /dev/null
+++ b/src/ft_e_externs_next.h
@@ -0,0 +1,23 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_e_externs_next.h                                :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
+/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef FT_E_EXTERN_NEXT_H
+#define FT_E_EXTERN_NEXT_H
+
+#include "ft_s_struct.h"
+
+char	**ft_get_env_path(t_msh *msh);
+char	*ft_search_in_path(const char *com,
+						char **envpath,
+						t_msh *msh);
+
+#endif
diff --git a/src/ft_m_funptr.c b/src/ft_m_funptr.c
index 0682a20..de6acd3 100644
--- a/src/ft_m_funptr.c
+++ b/src/ft_m_funptr.c
@@ -28,7 +28,8 @@ void
 	msh->bu_ptr[4] = ft_b_unset;
 	msh->bu_ptr[5] = ft_b_env;
 	msh->bu_ptr[6] = ft_b_exit;
-	/* TODO: them builtins */
+	/* TODO: type builtin */
+	/* msh->bu_ptr[7] = ft_b_type; */
 	if (!(msh->bu_ref = ft_split(FT_BUILTINS, '|')))
 	{
 		ft_fail_alloc();
-- 
cgit v1.2.3