diff options
| author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-06-19 16:15:48 +0200 | 
|---|---|---|
| committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-06-19 16:15:48 +0200 | 
| commit | ce171cc433670bab74982a1cd3f3347a7cc6c704 (patch) | |
| tree | 04db39f8f8c9995e2f356a5ead436f0289ab5e23 /src | |
| parent | Segv but it's ok (diff) | |
| download | 42-minishell-ce171cc433670bab74982a1cd3f3347a7cc6c704.tar.gz 42-minishell-ce171cc433670bab74982a1cd3f3347a7cc6c704.tar.bz2 42-minishell-ce171cc433670bab74982a1cd3f3347a7cc6c704.tar.xz 42-minishell-ce171cc433670bab74982a1cd3f3347a7cc6c704.tar.zst 42-minishell-ce171cc433670bab74982a1cd3f3347a7cc6c704.zip  | |
Linux test sec pls
Diffstat (limited to '')
| -rw-r--r-- | src/ft_e_externs_pipes.c | 127 | 
1 files changed, 78 insertions, 49 deletions
diff --git a/src/ft_e_externs_pipes.c b/src/ft_e_externs_pipes.c index 521eb90..277abb4 100644 --- a/src/ft_e_externs_pipes.c +++ b/src/ft_e_externs_pipes.c @@ -18,6 +18,7 @@  #include "ft_d_define.h"  #include "ft_e_externs_next.h" +#include "ft_f_fail.h"  #include "ft_m_redirs.h"  #include "ft_s_destroy.h"  #include "ft_s_lcom.h" @@ -33,48 +34,72 @@ static void  	/* TODO: handle execve failed */  } +static size_t +ft_e_get_pipes_count(struct s_lpipes *ptr) +{ +	struct s_lpipes	*rptr; +	size_t			pipes; + +	rptr = ptr; +	pipes = 0; +	while (rptr->next != NULL) +	{ +		rptr = rptr->next; +		pipes++; +	} +	return (pipes); +} +  static void  	ft_exec_path(const char *fullpath[], -				struct s_lpipes *ptr, +				struct s_lpipes *head,  				t_msh *msh)  { -	/* TODO: norme */ -	pid_t	pid; -	int32_t	pipefd[2]; -	int32_t	status; +	size_t	pipes; +	size_t	i; +	size_t	j; +	int		fd[256][2]; +	int		pid; +	int		status; -	pipe(pipefd); -	if ((pid = fork()) == 0) -	{ -		close(pipefd[FT_READ_END]); -		dup2(pipefd[FT_WRITE_END], STDOUT_FILENO); -		ft_e_extern_child(fullpath[0], ptr->one, msh); -	} -	else if (pid < 0) +	pipes = ft_e_get_pipes_count(head); +	i = 0; +	while (i < pipes)  	{ -		/* TODO: handle fork failed */ +		pipe(fd[i]); +		i++;  	} -	else +	i = 0; +	while (i < pipes)  	{ -		if ((pid = fork()) == 0) -		{ -			close(pipefd[FT_WRITE_END]); -			dup2(pipefd[FT_READ_END], STDIN_FILENO); -			ft_e_extern_child(fullpath[1], ptr->next->one, msh); -		} -		else if (pid < 0) +		pid = fork(); +		if (pid == 0)  		{ -			/* TODO: handle fork failed */ -		} -		else -		{ -			close(pipefd[FT_READ_END]); -			close(pipefd[FT_WRITE_END]); -			waitpid(pid, &status, 0); +			if (i != 0) +				dup2(fd[i - 1][FT_WRITE_END], STDIN_FILENO); +			if (i != pipes) +				dup2(fd[i][FT_READ_END], STDOUT_FILENO); +			j = 0; +			while (j < pipes) +			{ +				close(fd[j][FT_WRITE_END]); +				close(fd[j][FT_READ_END]); +				j++; +			} +			ft_e_extern_child(fullpath[i], head->one, msh);  		} -		waitpid(pid, &status, 0); -		msh->ret = WEXITSTATUS(status); +		head = head->next; +		i++; +	} +	i = 0; +	while (i < pipes) +	{ +		close(fd[i][FT_WRITE_END]); +		close(fd[i][FT_READ_END]); +		i++;  	} +	waitpid(pid, &status, 0); +	msh->ret = WEXITSTATUS(status);  }  void @@ -86,33 +111,37 @@ void  	char			**envpath;  	char			**fullpath;  	size_t			i; +	size_t			pipes;  	head = ptr;  	rptr = ptr; -	if (!(fullpath = (char**)malloc(3 * sizeof(char*)))) -		return ; -	fullpath[2] = NULL; +	pipes = ft_e_get_pipes_count(head); +	if (!(fullpath = (char**)malloc((pipes + 1) * sizeof(char*)))) +		ft_fail_alloc(msh); +	fullpath[pipes] = NULL;  	i = 0;  	while (rptr != NULL)  	{ -		while (i < 2) +		if (ft_ischarset("/.", rptr->one->com[0]))  		{ -			if (ft_ischarset("/.", ptr->one->com[0])) -			{ -				if (!(fullpath[i] = ft_strdup(ptr->one->com))) -					return ; -			} -			else if ((envpath = ft_get_env_path(msh)) != NULL) -			{ -				fullpath[i] = ft_search_in_path(ptr->one->com, envpath, msh); -				ft_delwords(envpath); -			} -			/* TODO: deal if not found etc */ -			ptr = ptr->next; -			i++; +			if (!(fullpath[i] = ft_strdup(rptr->one->com))) +				ft_fail_alloc(msh); +		} +		else if ((envpath = ft_get_env_path(msh)) != NULL) +		{ +			fullpath[i] = ft_search_in_path(rptr->one->com, envpath, msh); +			ft_delwords(envpath);  		} -		ft_exec_path((const char**)fullpath, rptr, msh); +		/* TODO: deal if not found etc */ +		i++;  		rptr = rptr->next;  	} +	i = 0; +	while (fullpath[i]) +	{ +		ft_printf("[%s]\n", fullpath[i]); +		i++; +	} +	ft_exec_path((const char**)fullpath, head, msh);  	ft_delwords(fullpath);  }  | 
