diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-04-01 15:55:57 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-04-01 15:55:57 +0200 |
| commit | 2eb59ee61e49b60472f82c000dd4f3536bd1987c (patch) | |
| tree | 190ac7bc42051ee9fcc7c1d781be16afa4d43075 /src/eval/pipe.c | |
| parent | 551e668e1b7a030fdff236067963100c7d8747a5 (diff) | |
| download | minishell-2eb59ee61e49b60472f82c000dd4f3536bd1987c.tar.gz minishell-2eb59ee61e49b60472f82c000dd4f3536bd1987c.tar.bz2 minishell-2eb59ee61e49b60472f82c000dd4f3536bd1987c.zip | |
Added builtin support in command eval, Refactoring eval/builtin function, Added doc
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); +} |
