From 15d8385f8ecf30e1ca74025b12fed7e45349b706 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Fri, 25 Oct 2024 13:44:13 +0200 Subject: [PATCH] Handle all redirections except APPEND TODO: Add APPEND handling TODO: Fix permissions --- src/execute_cmd.c | 34 +++++++++++++++++++++++++++++++++- src/interpreter.c | 4 +++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/execute_cmd.c b/src/execute_cmd.c index 33ba11c..fa7677f 100644 --- a/src/execute_cmd.c +++ b/src/execute_cmd.c @@ -6,22 +6,54 @@ /* By: dkaiser #include +#include +#include #include +#include int execute_cmd(t_cmd *cmd, t_env *env) { int result; char *cmd_path; + int fd; cmd_path = get_cmd_path(cmd->args[0], env); cmd->args[0] = cmd_path; + + if (cmd->redirs[0].type == INPUT_FILE) + { + fd = open(cmd->redirs[0].specifier, O_RDONLY); + if (fd < 0) + return (EXIT_FAILURE); + dup2(fd, STDIN_FILENO); + } + else if (cmd->redirs[0].type == INPUT_LIMITER) + { + dbg("INPUT_LIMITER"); + } + if (cmd->redirs[1].type == OUTPUT_APPEND) + { + dbg("OUTPUT_APPEND"); + fd = open(cmd->redirs[1].specifier, O_WRONLY | O_CREAT | O_APPEND); + if (fd < 0) + return (EXIT_FAILURE); + dup2(fd, STDOUT_FILENO); + } + else if (cmd->redirs[1].type == OUTPUT_OVERRIDE) + { + fd = open(cmd->redirs[1].specifier, O_WRONLY | O_CREAT | O_TRUNC); + if (fd < 0) + return (EXIT_FAILURE); + dup2(fd, STDOUT_FILENO); + dbg("OUTPUT_OVERRIDE"); + } result = execve(cmd->args[0], cmd->args, env_to_strlst(env)); return (result); } diff --git a/src/interpreter.c b/src/interpreter.c index e08d289..458f2af 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,7 +6,7 @@ /* By: dkaiser