diff --git a/src/executing/simple_cmd/simple_cmd_execute.c b/src/executing/simple_cmd/simple_cmd_execute.c index 0f0c2aa..87e8997 100644 --- a/src/executing/simple_cmd/simple_cmd_execute.c +++ b/src/executing/simple_cmd/simple_cmd_execute.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/27 16:21:56 by khais #+# #+# */ -/* Updated: 2025/04/04 17:09:39 by khais ### ########.fr */ +/* Updated: 2025/04/04 19:56:34 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ #include "libft.h" #include "../../subst/subst.h" #include "../common/do_waitpid.h" +#include "../../minishell.h" #include #include #include @@ -27,6 +28,40 @@ static void command_not_found(t_simple_cmd *cmd) cmd->words->word->word); } +static t_redirect *do_redirection(t_redirect *redirection) +{ + int fd; + + if (redirection->type == FT_HEREDOC) + fd = redirection->redirectee.dest; + else + { + fd = open(redirection->redirectee.filename->word, + redirection->open_flags, redirection->c_flags); + if (fd < 0) + return (perror("minishell: open"), NULL); + } + if (dup2(fd, redirection->source) < 0) + return (perror("minishell: dup2"), NULL); + close(fd); + return (redirection); +} + +static t_simple_cmd *handle_redirections(t_simple_cmd *cmd, t_minishell *app) +{ + t_redirect *redirections; + + (void)app; + redirections = cmd->redirections; + while (redirections != NULL) + { + if (do_redirection(redirections) == NULL) + return (NULL); + redirections = redirections->next; + } + return (cmd); +} + void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app) { char *exe; @@ -34,6 +69,8 @@ void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app) if (cmd == NULL || cmd->words == NULL || cmd->words->word == NULL) return ; + if (handle_redirections(cmd, app) == NULL) + return ; if (execute_builtin(cmd, app) != BUILTIN_INVALID) return ; exe = get_cmdpath(cmd->words->word->word, app);