aboutsummaryrefslogtreecommitdiff
path: root/src/eval/pipe.c
blob: 897a5f2fa5039effefe7e91a00765e8ee0efcee4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
** \file   pipe.c
** \brief  Pipes setup
*/

#include "eval.h"

/**
** \brief           Setup STDIN and STDOUT pipe in the parent process
** \param cmd       Command to setup
** \param pipe_in   STDIN pipe
** \param pipe_out  STDOUT pipe
** \return          -1 on error, 0 otherwise
*/

int	pipe_setup_parent(t_cmd *cmd, int pipe_in[2], int pipe_out[2])
{
	if (cmd->in != NULL)
	{
		if ((pipe_in[PIPE_WRITE] = open(cmd->in, O_RDONLY)) < 0)
			return (-1);
	}
	if (cmd->out != NULL)
	{
		if ((pipe_out[PIPE_READ] = open(cmd->out,
				(cmd->is_append ? O_WRONLY : O_APPEND) | O_CREAT)) < 0)
			return (-1);
	}
	return (0);
}

/**
** \brief           Setup STDIN and STDOUT pipe in the child process
** \param pipe_in   STDIN pipe
** \param pipe_out  STDOUT pipe
** \return          -1 on error, 0 otherwise
*/

int	pipe_setup_child(int pipe_in[2], int pipe_out[2])
{
	if (pipe_in[PIPE_READ] != PIPE_CLOSED)
		if (dup2(STDIN_FILENO, pipe_in[PIPE_READ]) == -1)
			return (-1);
	if (pipe_out[PIPE_WRITE] != PIPE_CLOSED)
		if (dup2(STDOUT_FILENO, pipe_out[PIPE_WRITE]) == -1)
			return (-1);
	return (0);
}