diff --git a/inc/invoke_cmd/pipeline_helper.h b/inc/invoke_cmd/pipeline_helper.h index c4c7c57..eb5b0a3 100644 --- a/inc/invoke_cmd/pipeline_helper.h +++ b/inc/invoke_cmd/pipeline_helper.h @@ -6,7 +6,7 @@ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 14:32:27 by dayano #+# #+# */ -/* Updated: 2025/05/04 19:18:38 by ttsubo ### ########.fr */ +/* Updated: 2025/05/09 11:33:04 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,6 @@ # include // pipeline_helper.c -void redirect_stdout(t_cmd *cmd); bool is_redirect(t_cmd *cmd); bool is_head(t_cmd *cmd, t_cmd *cmd_head); bool is_tail(t_cmd *cmd); diff --git a/inc/invoke_cmd/redirect.h b/inc/invoke_cmd/redirect.h index 117cacc..5d96fe4 100644 --- a/inc/invoke_cmd/redirect.h +++ b/inc/invoke_cmd/redirect.h @@ -6,7 +6,7 @@ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/28 15:20:03 by dayano #+# #+# */ -/* Updated: 2025/05/04 19:19:04 by ttsubo ### ########.fr */ +/* Updated: 2025/05/09 11:32:11 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,8 +15,8 @@ # include "common.h" -void redirect(t_cmd *cmd); -void redirect_stdout(t_cmd *cmd); -void redirect_stdin(t_cmd *cmd); +bool redirect(t_cmd *cmd); +bool redirect_stdout(t_cmd *cmd); +bool redirect_stdin(t_cmd *cmd); #endif diff --git a/src/invoke_cmd/invoke_command.c b/src/invoke_cmd/invoke_command.c index 3c7049e..d11fbff 100644 --- a/src/invoke_cmd/invoke_command.c +++ b/src/invoke_cmd/invoke_command.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* invoke_command.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dayano +#+ +:+ +#+ */ +/* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/20 21:19:33 by dayano #+# #+# */ -/* Updated: 2025/05/05 17:39:02 by dayano ### ########.fr */ +/* Updated: 2025/05/09 11:43:50 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,12 +65,14 @@ int execute_builtin(t_cmd *cmd, t_minish *minish) int exec_unit_builtin(t_cmd *cmd, t_minish *minish) { t_cmd *builtin_cmd; + bool redir_result; + redir_result = true; builtin_cmd = cmd; if (cmd && is_redirect(cmd)) { - redirect(cmd); - if (cmd->next) + redir_result = redirect(cmd); + if (redir_result && cmd->next) { cmd = cmd->next; builtin_cmd = cmd; @@ -79,7 +81,9 @@ int exec_unit_builtin(t_cmd *cmd, t_minish *minish) return (print_error(cmd->argv[0]), EXIT_FAILURE); } if (cmd->next && is_redirect(cmd->next)) - redirect(cmd->next); + redir_result = redirect(cmd->next); + if (!redir_result) + return (EXIT_FAILURE); return (execute_builtin(builtin_cmd, minish)); } diff --git a/src/invoke_cmd/redirect.c b/src/invoke_cmd/redirect.c index 877cb8a..2995368 100644 --- a/src/invoke_cmd/redirect.c +++ b/src/invoke_cmd/redirect.c @@ -3,16 +3,16 @@ /* ::: :::::::: */ /* redirect.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dayano +#+ +:+ +#+ */ +/* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/28 15:19:19 by dayano #+# #+# */ -/* Updated: 2025/04/28 16:57:45 by dayano ### ########.fr */ +/* Updated: 2025/05/09 14:23:25 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ #include "main.h" -void redirect_stdout(t_cmd *cmd) +bool redirect_stdout(t_cmd *cmd) { int fd; char *path; @@ -23,19 +23,22 @@ void redirect_stdout(t_cmd *cmd) else if (cmd->type == REDIR_APPEND) fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644); else - return ; + return (false); if (fd < 0) { ft_putstr_fd("minish: ", STDERR_FILENO); perror(path); - return ; + return (false); } if (dup2(fd, STDOUT_FILENO) < 0) { ft_putstr_fd("minish: ", STDERR_FILENO); perror(path); + close(fd); + return (false); } close(fd); + return (true); } bool is_limiter(int fd, char *limiter) @@ -81,39 +84,43 @@ void here_doc(char *limiter) close(pipefd[0]); } -void redirect_stdin(t_cmd *cmd) +bool redirect_stdin(t_cmd *cmd) { int fd; char *path; path = cmd->argv[0]; if (cmd->type == REDIR_HEREDOC) + return (here_doc(path), true); + if (cmd->type != REDIR_IN) + return (true); + fd = open(path, O_RDONLY); + if (fd < 0) { - here_doc(cmd->argv[0]); - return ; + ft_putstr_fd("minish: ", STDERR_FILENO); + perror(path); + return (false); } - else if (cmd->type == REDIR_IN) + if (dup2(fd, STDIN_FILENO) < 0) { - fd = open(path, O_RDONLY); - if (fd < 0) - { - ft_putstr_fd("minish: ", STDERR_FILENO); - perror(path); - return ; - } - if (dup2(fd, STDIN_FILENO) < 0) - { - ft_putstr_fd("minish: ", STDERR_FILENO); - perror(path); - } + ft_putstr_fd("minish: ", STDERR_FILENO); + perror(path); close(fd); + return (false); } + close(fd); + return (true); } -void redirect(t_cmd *cmd) +/** + * @note This function is only called if the caller's is_redirect is true + * @note false if the internal function is not executed. + */ +bool redirect(t_cmd *cmd) { if (cmd->type == REDIR_IN || cmd->type == REDIR_HEREDOC) - redirect_stdin(cmd); + return (redirect_stdin(cmd)); if (cmd->type == REDIR_OUT || cmd->type == REDIR_APPEND) - redirect_stdout(cmd); + return (redirect_stdout(cmd)); + return (false); }