From 5642c267f046fc02323b2e7ed86fab391d90a67e Mon Sep 17 00:00:00 2001
From: y-syo <mmoussou@student.42angouleme.fr>
Date: Tue, 4 Jun 2024 22:28:13 +0200
Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20fixed?=
 =?UTF-8?q?=20split=20cmd=20when=20there=20is=20no=20path=20var,=20added?=
 =?UTF-8?q?=20split=20cmd=20execution=20to=20the=20main?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 include/execution.h       |  5 ++-
 src/exec/exec_split_cmd.c | 64 +++++++++++++++++++++++----------------
 src/exec/get_path.c       |  4 ++-
 src/main.c                |  8 ++---
 4 files changed, 49 insertions(+), 32 deletions(-)

diff --git a/include/execution.h b/include/execution.h
index cb1a296..b5c5473 100644
--- a/include/execution.h
+++ b/include/execution.h
@@ -6,14 +6,17 @@
 /*   By: mmoussou <mmoussou@student.42angouleme.fr  +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/04/29 13:20:22 by mmoussou          #+#    #+#             */
-/*   Updated: 2024/06/03 15:26:03 by mmoussou         ###   ########.fr       */
+/*   Updated: 2024/06/04 21:43:34 by mmoussou         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #ifndef EXECUTION_H
 # define EXECUTION_H
 
+# include "env.h"
+
 char	**get_path(char *path);
+int	exec_split_cmd(t_list *list_cmd, t_env *env);
 
 /**
  * @brief				spawn a heredoc
diff --git a/src/exec/exec_split_cmd.c b/src/exec/exec_split_cmd.c
index 792d32e..f732ce6 100644
--- a/src/exec/exec_split_cmd.c
+++ b/src/exec/exec_split_cmd.c
@@ -6,7 +6,7 @@
 /*   By: mmoussou <mmoussou@student.42angouleme.fr  +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/01 14:55:06 by mmoussou          #+#    #+#             */
-/*   Updated: 2024/06/02 21:15:03 by mmoussou         ###   ########.fr       */
+/*   Updated: 2024/06/04 22:03:56 by mmoussou         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -19,7 +19,9 @@ char	*get_cmd_path(char *cmd, t_env *env)
 	char	**path_list;
 
 	path = NULL;
-	path_list = get_path(env_get_value("PATH2", env));
+	path_list = get_path(env_get_value("PATH", env));
+	if (!path_list)
+		return (NULL);
 	i = 0;
 	while (path_list[i])
 	{
@@ -36,6 +38,7 @@ char	*get_cmd_path(char *cmd, t_env *env)
 
 int	exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t)
 {
+	int	fork_pid;
 	int	status;
 
 	status = dup2(cmd->infile, STDIN_FILENO);
@@ -51,8 +54,37 @@ int	exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t)
 		cmd->cmd = get_cmd_path(cmd->cmd, env_t);
 	if (!(cmd->cmd))
 		return (-1);
-	
-	int	fork_pid = fork();
+	fork_pid = fork();
+	if (fork_pid == -1)
+		return (-1);
+	if (!fork_pid)
+	{
+		status = execve(cmd->cmd, cmd->argv, env);
+		exit(-1);
+	}
+	else
+		waitpid(fork_pid, NULL, 0);
+	if (status == -1)
+		return (-1);
+	return (0);
+}
+
+int	exec_last_cmd(t_cmd *cmd, char **env, t_env *env_t)
+{
+	int	fork_pid;
+	int	status;
+
+	status = dup2(cmd->infile, STDIN_FILENO);
+	if (status == -1)
+		return (-1);
+	status = dup2(STDOUT_FILENO, cmd->outfile);
+	if (status == -1)
+		return (-1);
+	if (cmd->cmd[0] != '/')
+		cmd->cmd = get_cmd_path(cmd->cmd, env_t);
+	if (!(cmd->cmd))
+		return (-1);
+	fork_pid = fork();
 	if (fork_pid == -1)
 		return (-1);
 	if (!fork_pid)
@@ -71,7 +103,6 @@ int	exec_split_cmd(t_list *list_cmd, t_env *env)
 {
 	char	**env_array;
 	int		status;
-	t_cmd	*cmd;
 
 	env_array = env_get(env);
 	if (!env_array)
@@ -86,27 +117,8 @@ int	exec_split_cmd(t_list *list_cmd, t_env *env)
 		}
 		list_cmd = list_cmd->next;
 	}
-	cmd = list_cmd->content;
-	status = dup2(cmd->infile, STDIN_FILENO);
-	if (status == -1)
-		return (-1);
-	status = dup2(STDOUT_FILENO, cmd->outfile);
-	if (status == -1)
-		return (-1);
-	if (cmd->cmd[0] != '/')
-		cmd->cmd = get_cmd_path(cmd->cmd, env);
-	if (!(cmd->cmd))
-		return (-1);
-	int	fork_pid = fork();
-	if (fork_pid == -1)
-		return (-1);
-	if (!fork_pid)
-	{
-		status = execve(cmd->cmd, cmd->argv, env_array);
-		exit(-1);
-	}
-	else
-		waitpid(fork_pid, NULL, 0);
+	status = exec_last_cmd(list_cmd->content, env_array, env);
+	ft_free("a", &env_array);
 	if (status == -1)
 		return (-1);
 	return (0);
diff --git a/src/exec/get_path.c b/src/exec/get_path.c
index a46e80a..ae263a1 100644
--- a/src/exec/get_path.c
+++ b/src/exec/get_path.c
@@ -6,7 +6,7 @@
 /*   By: mmoussou <mmoussou@student.42angouleme.fr  +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/19 01:42:17 by mmoussou          #+#    #+#             */
-/*   Updated: 2024/06/02 21:05:25 by mmoussou         ###   ########.fr       */
+/*   Updated: 2024/06/04 21:53:30 by mmoussou         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -67,6 +67,8 @@ char	**get_path(char *path)
 	t_list	*list_entry;
 	int		i;
 
+	if (!path)
+		return (NULL);
 	list_entry = ft_lstnew(NULL);
 	path_dir = ft_split(path, ':');
 	if (!path_dir)
diff --git a/src/main.c b/src/main.c
index 9a8bc0c..5effa96 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,7 +6,7 @@
 /*   By: mmoussou <mmoussou@student.42angouleme.fr  +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/04/24 11:18:04 by adjoly            #+#    #+#             */
-/*   Updated: 2024/05/31 13:29:26 by adjoly           ###   ########.fr       */
+/*   Updated: 2024/06/04 21:42:55 by mmoussou         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -67,7 +67,6 @@ int	main(int ac, char **av, char **env)
 	char	**lll;
 	t_list	*piped;
 	t_env	env_l;
-	t_cmd	*cmd;
 	t_list	*cmd_list;
 
 	(void)ac;
@@ -90,12 +89,13 @@ int	main(int ac, char **av, char **env)
 		piped = tokenizer(test);
 		check_redir(((t_token *)(piped->content))->redirection, av);
 		cmd_list = get_cmd_list(piped);
-		while (cmd_list)
+		exec_split_cmd(cmd_list, &env_l);
+		/*while (cmd_list)
 		{
 			cmd = cmd_list->content;
 			cmd_list = cmd_list->next;
 		}
-		print_cmd(cmd);
+		print_cmd(cmd);*/
 		free(test);
 		ft_lstclear(&piped, free_token);
 		ft_free("a", &lll);