echo: correctly handle write errors such as /dev/full

This commit is contained in:
Khaïs COLIN 2025-04-08 16:18:57 +02:00
parent 871180f6d8
commit cfc95994de
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo

View file

@ -6,13 +6,14 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 13:59:13 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 "builtins.h"
#include "libft.h" #include "libft.h"
#include "simple_cmd_execute.h" #include "simple_cmd_execute.h"
#include <stdio.h>
static bool should_print_newline(t_wordlist **arg) static bool should_print_newline(t_wordlist **arg)
{ {
@ -42,6 +43,13 @@ static bool should_print_newline(t_wordlist **arg)
return (print_newline); 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_builtin_type builtin_echo(t_simple_cmd *cmd, t_minishell *app)
{ {
t_wordlist *arg; 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); print_newline = should_print_newline(&arg);
while (arg != NULL) while (arg != NULL)
{ {
ft_printf("%s", arg->word->word); if (ft_printf("%s", arg->word->word) < 0)
return (write_error(app));
arg = arg->next; arg = arg->next;
if (arg != NULL) if (arg != NULL)
ft_printf(" "); {
if (ft_printf(" ") < 0)
return (write_error(app));
}
} }
if (print_newline) if (print_newline)
ft_printf("\n"); {
if (ft_printf("\n") < 0)
return (write_error(app));
}
app->last_return_value = 0; app->last_return_value = 0;
return (BUILTIN_ECHO); return (BUILTIN_ECHO);
} }