From 9db47dc96df42a0bdf595a028e1732d1e69cca1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 22 Apr 2025 16:21:10 +0200 Subject: [PATCH] feat(redirection): print path of redirection target on redirection error --- src/executing/simple_cmd/handle_redirections.c | 18 ++++++++++++++++-- test.sh | 9 +++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/executing/simple_cmd/handle_redirections.c b/src/executing/simple_cmd/handle_redirections.c index d2025f8..4bd19d6 100644 --- a/src/executing/simple_cmd/handle_redirections.c +++ b/src/executing/simple_cmd/handle_redirections.c @@ -6,12 +6,23 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/17 11:14:40 by khais #+# #+# */ -/* Updated: 2025/04/17 11:15:44 by khais ### ########.fr */ +/* Updated: 2025/04/24 13:41:23 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include "handle_redirections.h" #include +#include + +static void *redirection_error(char *filename) +{ + int errno_save; + + errno_save = errno; + ft_dprintf(STDERR_FILENO, "minishell: %s: %s\n", filename, + strerror(errno_save)); + return (NULL); +} static t_redirect *do_redirection(t_redirect *redirection) { @@ -24,7 +35,7 @@ static t_redirect *do_redirection(t_redirect *redirection) fd = open(redirection->redirectee.filename->word, redirection->open_flags, redirection->c_flags); if (fd < 0) - return (perror("minishell: open"), NULL); + return (redirection_error(redirection->redirectee.filename->word)); } if (dup2(fd, redirection->source) < 0) return (perror("minishell: dup2"), NULL); @@ -56,7 +67,10 @@ t_simple_cmd *handle_redirections(t_simple_cmd *cmd, t_minishell *app) while (redirections != NULL) { if (do_redirection(redirections) == NULL) + { + app->last_return_value = 1; return (NULL); + } redirections = redirections->next; } return (cmd); diff --git a/test.sh b/test.sh index 6b66e70..e7708ef 100755 --- a/test.sh +++ b/test.sh @@ -968,4 +968,13 @@ hello the answer EOF +when_run < /root/outfile +echo \$? +EOF +expecting <