diff options
| author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-05-11 17:57:19 +0200 | 
|---|---|---|
| committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-05-11 17:57:19 +0200 | 
| commit | 74b24cd53aa4bf2c95230e2613975194855d2137 (patch) | |
| tree | a5d5d82fe78013edde0bb75443fb246f116e73d8 | |
| parent | Can't pipe(2) (diff) | |
| download | 42-minishell-74b24cd53aa4bf2c95230e2613975194855d2137.tar.gz 42-minishell-74b24cd53aa4bf2c95230e2613975194855d2137.tar.bz2 42-minishell-74b24cd53aa4bf2c95230e2613975194855d2137.tar.xz 42-minishell-74b24cd53aa4bf2c95230e2613975194855d2137.tar.zst 42-minishell-74b24cd53aa4bf2c95230e2613975194855d2137.zip  | |
Almost there
Diffstat (limited to '')
| -rw-r--r-- | src/ft_d_enum.h | 6 | ||||
| -rw-r--r-- | src/ft_e_externs_pipes.c | 45 | 
2 files changed, 31 insertions, 20 deletions
diff --git a/src/ft_d_enum.h b/src/ft_d_enum.h index b712097..680ff00 100644 --- a/src/ft_d_enum.h +++ b/src/ft_d_enum.h @@ -32,4 +32,10 @@ enum  	FT_RET_ALLOC  }; +enum +{ +	FT_WRITE_END, +	FT_READ_END +}; +  #endif diff --git a/src/ft_e_externs_pipes.c b/src/ft_e_externs_pipes.c index e5368f2..df460df 100644 --- a/src/ft_e_externs_pipes.c +++ b/src/ft_e_externs_pipes.c @@ -16,6 +16,7 @@  #include <stdint.h>  #include <unistd.h> +#include "ft_d_define.h"  #include "ft_e_externs_next.h"  #include "ft_m_redirs.h"  #include "ft_s_destroy.h" @@ -38,37 +39,39 @@ static void  				t_msh *msh)  {  	/* TODO: norme */ -	pid_t	pid[2]; +	pid_t	pid;  	int32_t	pipefd[2];  	int32_t	status;  	pipe(pipefd); -	if ((pid[0] = fork()) == 0) +	if ((pid = fork()) == 0)  	{ -		close(pipefd[0]); -		dup2(pipefd[1], STDOUT_FILENO); +		close(pipefd[FT_WRITE_END]); +		dup2(pipefd[FT_READ_END], STDOUT_FILENO); +		close(pipefd[FT_READ_END]);  		ft_e_extern_child(fullpath[0], ptr->one, msh);  	} -	else if (pid[0] < 0) +	else if (pid < 0)  	{  		/* TODO: handle fork failed */  	}  	else  	{ -		if ((pid[1] = fork()) == 0) +		if ((pid = fork()) == 0)  		{ -			close(pipefd[1]); -			dup2(pipefd[0], STDIN_FILENO); +			dup2(pipefd[FT_READ_END], STDIN_FILENO); +			close(pipefd[FT_WRITE_END]); +			close(pipefd[FT_WRITE_END]);  			ft_e_extern_child(fullpath[1], ptr->next->one, msh);  		} -		else if (pid[1] < 0) +		else if (pid < 0)  		{  			/* TODO: handle fork failed */  		} -		close(pipefd[0]); -		close(pipefd[1]); -		waitpid(pid[1], &status, 0); -		waitpid(pid[0], &status, 0); +		close(pipefd[FT_READ_END]); +		close(pipefd[FT_WRITE_END]); +		waitpid(-1, &status, 0); +		waitpid(-1, &status, 0);  		msh->ret = WEXITSTATUS(status);  	}  } @@ -77,28 +80,30 @@ void  	ft_e_externs_pipes(struct s_lpipes *ptr,  					t_msh *msh)  { -	char	**envpath; -	char	**fullpath; -	size_t	i; +	struct s_lpipes	*p_ptr; +	char			**envpath; +	char			**fullpath; +	size_t			i; +	p_ptr = ptr;  	if (!(fullpath = (char**)malloc(3 * sizeof(char*))))  		return ;  	fullpath[2] = NULL;  	i = 0;  	while (i < 2)  	{ -		if (ft_ischarset("/.", ptr->one->com[0])) +		if (ft_ischarset("/.", p_ptr->one->com[0]))  		{ -			if (!(fullpath[i] = ft_strdup(ptr->one->com))) +			if (!(fullpath[i] = ft_strdup(p_ptr->one->com)))  				return ;  		}  		else if ((envpath = ft_get_env_path(msh)) != NULL)  		{ -			fullpath[i] = ft_search_in_path(ptr->one->com, envpath, msh); +			fullpath[i] = ft_search_in_path(p_ptr->one->com, envpath, msh);  			ft_delwords(envpath);  		}  		/* TODO: deal if not found etc */ -		ptr = ptr->next; +		p_ptr = p_ptr->next;  		i++;  	}  	ft_exec_path((const char**)fullpath, ptr, msh);  | 
