diff options
| author | Dominik Kaiser | 2025-01-20 18:46:39 +0100 |
|---|---|---|
| committer | GitHub | 2025-01-20 18:46:39 +0100 |
| commit | 9fa887da20e409c2f25fac44b57f999e508a30ea (patch) | |
| tree | 7e3fee3edea18d0d81abddb89104858cfc0ec5e3 /src/handle_redir.c | |
| parent | af9d1a9b39daaf1b86cf94ee629e06503d8ab6d4 (diff) | |
| parent | 62845ce01f222fad126372c574c7ab084478adf0 (diff) | |
| download | minishell-9fa887da20e409c2f25fac44b57f999e508a30ea.tar.gz minishell-9fa887da20e409c2f25fac44b57f999e508a30ea.zip | |
Merge pull request #30 from dpu-kaiser/merge_format_redir
Merge format redir
Diffstat (limited to 'src/handle_redir.c')
| -rw-r--r-- | src/handle_redir.c | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/src/handle_redir.c b/src/handle_redir.c new file mode 100644 index 0000000..e8a1010 --- /dev/null +++ b/src/handle_redir.c @@ -0,0 +1,103 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* handle_redir.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/18 18:34:51 by chuhlig #+# #+# */ +/* Updated: 2025/01/20 14:59:38 by chuhlig ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int handle_input_redirection(t_redirection *redir) +{ + int fd; + + if (redir->type == INPUT_FILE) + { + fd = open_file(redir->specifier, O_RDONLY, 0); + if (fd < 0) + return (-1); + dup2(fd, STDIN_FILENO); + close(fd); + } + else if (redir->type == INPUT_LIMITER) + { + fd = open_file("/tmp/heredoc_tmp", O_WRONLY | O_TRUNC, 0644); + if (fd < 0) + return (-1); + write(fd, redir->specifier, ft_strlen(redir->specifier)); + close(fd); + fd = open_file("/tmp/heredoc_tmp", O_RDONLY, 0); + if (fd < 0) + return (-1); + dup2(fd, STDIN_FILENO); + close(fd); + } + return (0); +} + +int handle_output_redirection(t_redirection *redir) +{ + int fd; + + if (redir->type == OUTPUT_OVERRIDE) + { + fd = open_file(redir->specifier, O_WRONLY | O_TRUNC, 0644); + if (fd < 0) + return (-1); + dup2(fd, STDOUT_FILENO); + close(fd); + } + else if (redir->type == OUTPUT_APPEND) + { + fd = open_file(redir->specifier, O_WRONLY | O_APPEND, 0644); + if (fd < 0) + return (-1); + dup2(fd, STDOUT_FILENO); + close(fd); + } + return (0); +} + +int handle_redirections(t_redirection *redirs) +{ + if (redirs[0].type == INPUT_FILE || redirs[0].type == INPUT_LIMITER) + { + if (handle_input_redirection(&redirs[0]) < 0) + return (-1); + } + if (redirs[1].type == OUTPUT_OVERRIDE || redirs[1].type == OUTPUT_APPEND) + { + if (handle_output_redirection(&redirs[1]) < 0) + return (-1); + } + return (0); +} + +int handle_pipe_parent(int p[2], t_node *node, t_env **env) +{ + int original_stdin; + int result; + + close(p[1]); + original_stdin = dup(STDIN_FILENO); + dup2(p[0], STDIN_FILENO); + result = eval_rec(node->content.pipe.right, env, p[0]); + dup2(original_stdin, STDIN_FILENO); + close(original_stdin); + close(p[0]); + return (result); +} + +int handle_pipe_child(int p[2], t_node *node, t_env **env, int in_fd) +{ + close(p[0]); + dup2(in_fd, STDIN_FILENO); + dup2(p[1], STDOUT_FILENO); + close(p[1]); + exit(eval_rec(node->content.pipe.left, env, in_fd)); +} |
