From 1c4733b1cc127adc8a4ff8b5dfe7d41261c07d3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Fri, 28 Mar 2025 18:28:27 +0100 Subject: [PATCH] here_doc: print error if failed to create temp file --- Makefile | 1 + src/executing/here_doc/here_doc.c | 14 +++++++++----- src/executing/here_doc/here_doc_errors.c | 20 ++++++++++++++++++++ tests/test_here_doc.c | 8 +++++++- 4 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 src/executing/here_doc/here_doc_errors.c diff --git a/Makefile b/Makefile index 964255b..eec1abb 100644 --- a/Makefile +++ b/Makefile @@ -26,6 +26,7 @@ srcs = \ src/env/env_manip.c \ src/env/envp.c \ src/executing/here_doc/here_doc.c \ + src/executing/here_doc/here_doc_errors.c \ src/executing/here_doc/here_doc_expand_line.c \ src/executing/here_doc/random_filename.c \ src/executing/here_doc/strip_newline.c \ diff --git a/src/executing/here_doc/here_doc.c b/src/executing/here_doc/here_doc.c index 4085b29..c04fdf7 100644 --- a/src/executing/here_doc/here_doc.c +++ b/src/executing/here_doc/here_doc.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/07 11:42:29 by khais #+# #+# */ -/* Updated: 2025/03/28 16:43:47 by khais ### ########.fr */ +/* Updated: 2025/03/28 19:07:45 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,8 +15,10 @@ #include #include #include "libft.h" +#include "../../ft_errno.h" char *expand_line(char *line, t_minishell *app); +void failed_to_open_tmp_file(void); /* ** - check that arguments are not invalid @@ -30,13 +32,13 @@ static int setup_here_doc(t_worddesc *marker, int infd, char **filename) int outfd; if (infd < 0 || marker == NULL || marker->word == NULL) - return (-1); + return (ft_errno(FT_EINVAL), -1); *filename = here_doc_random_filename(); if (filename == NULL) - return (-1); + return (ft_errno(FT_EERRNO), -1); outfd = open(*filename, O_CREAT | O_WRONLY | O_EXCL, S_IRUSR | S_IWUSR); if (outfd < 0) - return (free(*filename), -1); + return (ft_errno(FT_EERRNO), free(*filename), -1); return (outfd); } @@ -60,6 +62,8 @@ static int finalize(int outfd, char *filename) outfd = open(filename, O_RDONLY, 0); unlink(filename); free(filename); + if (outfd < 0) + ft_errno(FT_EERRNO); return (outfd); } @@ -108,7 +112,7 @@ int here_doc(t_worddesc *marker, int infd, t_minishell *app) outfd = setup_here_doc(marker, infd, &filename); if (outfd < 0) - return (-1); + return (failed_to_open_tmp_file(), -1); line = get_next_line(infd); while (line != NULL) { diff --git a/src/executing/here_doc/here_doc_errors.c b/src/executing/here_doc/here_doc_errors.c new file mode 100644 index 0000000..12e7cfd --- /dev/null +++ b/src/executing/here_doc/here_doc_errors.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* here_doc_errors.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/28 18:29:44 by khais #+# #+# */ +/* Updated: 2025/03/28 19:03:45 by khais ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" +#include "../../ft_errno.h" + +void failed_to_open_tmp_file(void) +{ + ft_perror("minishell: cannot create temp file for here-document"); +} diff --git a/tests/test_here_doc.c b/tests/test_here_doc.c index 1275d6a..c160a55 100644 --- a/tests/test_here_doc.c +++ b/tests/test_here_doc.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/07 11:43:32 by khais #+# #+# */ -/* Updated: 2025/03/28 16:41:08 by khais ### ########.fr */ +/* Updated: 2025/03/28 19:05:55 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ #include #include "../src/executing/here_doc/here_doc.h" #include "../src/env/env_manip.h" +#include "../src/ft_errno.h" static void test_here_doc_filename_generation(void) { @@ -24,6 +25,7 @@ static void test_here_doc_filename_generation(void) char *filename2; ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); + ft_errno(FT_ESUCCESS); filename1 = here_doc_random_filename(); filename2 = here_doc_random_filename(); assert(filename1 != NULL); @@ -41,6 +43,7 @@ static void test_here_doc_invalid_args(void) t_worddesc *marker; ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); + ft_errno(FT_ESUCCESS); assert(-1 == here_doc(NULL, 0, NULL)); marker = worddesc_create(NULL, 0, NULL); assert(-1 == here_doc(marker, 0, NULL)); @@ -58,6 +61,7 @@ static void test_here_doc_only_end_marker(void) int result; ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); + ft_errno(FT_ESUCCESS); marker = worddesc_create(ft_strdup("EOF"), 0, NULL); infile = open("./here_doc_only_eof.input", O_RDONLY); result = here_doc(marker, infile, NULL); @@ -76,6 +80,7 @@ static void test_here_doc_input_plus_end_marker(void) int result; ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); + ft_errno(FT_ESUCCESS); marker = worddesc_create(ft_strdup("EOF"), 0, NULL); infile = open("./here_doc_input_plus_eof.input", O_RDONLY); result = here_doc(marker, infile, NULL); @@ -115,6 +120,7 @@ static void test_here_doc_with_expansion(void) ft_bzero(&app, sizeof(t_minishell)); app.env = env_set_entry(&app.env, ft_strdup("USER"), ft_strdup("kcolin")); ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); + ft_errno(FT_ESUCCESS); marker = worddesc_create(ft_strdup("EOF"), 0, NULL); infile = open("./here_doc_with_expansion.input", O_RDONLY); result = here_doc(marker, infile, &app);