From cfc95994de0fd59f959f164a369a8cb0872cb5fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 8 Apr 2025 16:18:57 +0200 Subject: [PATCH] echo: correctly handle write errors such as /dev/full --- src/executing/simple_cmd/builtin_echo.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/executing/simple_cmd/builtin_echo.c b/src/executing/simple_cmd/builtin_echo.c index 013a165..080dee3 100644 --- a/src/executing/simple_cmd/builtin_echo.c +++ b/src/executing/simple_cmd/builtin_echo.c @@ -6,13 +6,14 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/03 13:59:13 by khais #+# #+# */ -/* Updated: 2025/04/03 17:18:54 by khais ### ########.fr */ +/* Updated: 2025/04/14 11:52:37 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include "builtins.h" #include "libft.h" #include "simple_cmd_execute.h" +#include static bool should_print_newline(t_wordlist **arg) { @@ -42,6 +43,13 @@ static bool should_print_newline(t_wordlist **arg) return (print_newline); } +static t_builtin_type write_error(t_minishell *app) +{ + app->last_return_value = 1; + perror("minishell: echo: write error"); + return (BUILTIN_ECHO); +} + t_builtin_type builtin_echo(t_simple_cmd *cmd, t_minishell *app) { t_wordlist *arg; @@ -51,13 +59,20 @@ t_builtin_type builtin_echo(t_simple_cmd *cmd, t_minishell *app) print_newline = should_print_newline(&arg); while (arg != NULL) { - ft_printf("%s", arg->word->word); + if (ft_printf("%s", arg->word->word) < 0) + return (write_error(app)); arg = arg->next; if (arg != NULL) - ft_printf(" "); + { + if (ft_printf(" ") < 0) + return (write_error(app)); + } } if (print_newline) - ft_printf("\n"); + { + if (ft_printf("\n") < 0) + return (write_error(app)); + } app->last_return_value = 0; return (BUILTIN_ECHO); }