diff options
Diffstat (limited to 'src/eval/pipe.c')
| -rw-r--r-- | src/eval/pipe.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/eval/pipe.c b/src/eval/pipe.c new file mode 100644 index 0000000..897a5f2 --- /dev/null +++ b/src/eval/pipe.c @@ -0,0 +1,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); +} |
