From a6d1386574dd5efe71022b47eaac5e490ceea3fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 11 Mar 2025 11:26:17 +0100 Subject: [PATCH] here_doc: handle input with only an end marker --- .gitignore | 1 + src/executing/here_doc/here_doc.c | 24 +++++++++++++++------ src/executing/here_doc/here_doc.h | 4 ++-- tests/here_doc_only_eof.input | 1 + tests/test_here_doc.c | 36 +++++++++++++++++++++++++++---- 5 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 tests/here_doc_only_eof.input diff --git a/.gitignore b/.gitignore index ae9a1a7..1c5afbd 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,7 @@ tests/* !tests/Makefile !tests/*.c !tests/*.h +!tests/*.input infile outfile mallocfail_hashes diff --git a/src/executing/here_doc/here_doc.c b/src/executing/here_doc/here_doc.c index eb8582e..3a64a13 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/11 11:24:30 by khais ### ########.fr */ +/* Updated: 2025/03/11 11:38:42 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,15 +57,27 @@ char *here_doc_random_filename(void) } /* -** Read from STDIN into some file until a line that is exactly equal to the +** Read from infd into some file until a line that is exactly equal to the ** marker is read. ** ** Then unlink the file, and return a filedescriptor to it, seeked to the start. ** -** If NULL is given as argument, or any other error occurs, return -1. +** If NULL is given as argument, or marker->word is null, or infd is negative, +** or getting a random filename fails, or we are unable to create the file, or +** the file already exists, or any other error occurs, return -1. */ -int here_doc(t_worddesc *marker) +int here_doc(t_worddesc *marker, int infd) { - (void)marker; - return (-1); + int outfd; + char *filename; + + if (infd < 0 || marker == NULL || marker->word == NULL) + return (-1); + filename = here_doc_random_filename(); + if (filename == NULL) + return (-1); + outfd = open(filename, O_CREAT | O_WRONLY | O_EXCL, S_IRUSR | S_IWUSR); + if (outfd < 0) + return (free(filename), -1); + return (outfd); } diff --git a/src/executing/here_doc/here_doc.h b/src/executing/here_doc/here_doc.h index 8d60f16..785c8c5 100644 --- a/src/executing/here_doc/here_doc.h +++ b/src/executing/here_doc/here_doc.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/07 11:41:27 by khais #+# #+# */ -/* Updated: 2025/03/11 11:18:47 by khais ### ########.fr */ +/* Updated: 2025/03/11 11:32:12 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ # include "../../parser/worddesc/worddesc.h" -int here_doc(t_worddesc *marker); +int here_doc(t_worddesc *marker, int infd); char *here_doc_random_filename(void); #endif // HERE_DOC_H diff --git a/tests/here_doc_only_eof.input b/tests/here_doc_only_eof.input new file mode 100644 index 0000000..1a2b1dc --- /dev/null +++ b/tests/here_doc_only_eof.input @@ -0,0 +1 @@ +EOF diff --git a/tests/test_here_doc.c b/tests/test_here_doc.c index 1c5019d..7f644e4 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/11 11:19:16 by khais ### ########.fr */ +/* Updated: 2025/03/11 11:42:29 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ #include "testutil.h" #include "libft.h" #include +#include #include "../src/executing/here_doc/here_doc.h" static void test_here_doc_filename_generation(void) @@ -34,15 +35,42 @@ static void test_here_doc_filename_generation(void) do_leak_check(); } -static void test_here_doc_null_args(void) +static void test_here_doc_invalid_args(void) { + t_worddesc *marker; + ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); - assert(-1 == here_doc(NULL)); + assert(-1 == here_doc(NULL, 0)); + marker = worddesc_create(NULL, 0, NULL); + assert(-1 == here_doc(marker, 0)); + worddesc_destroy(marker); + marker = worddesc_create(ft_strdup("EOF"), 0, NULL); + assert(-1 == here_doc(marker, -1)); + worddesc_destroy(marker); + do_leak_check(); +} + +static void test_here_doc_only_end_marker(void) +{ + t_worddesc *marker; + int infile; + int result; + + ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); + marker = worddesc_create(ft_strdup("EOF"), 0, NULL); + infile = open("./here_doc_only_eof.input", O_RDONLY); + result = here_doc(marker, infile); + close(infile); + assert(result != -1); + assert(NULL == get_next_line(result)); + close(result); + worddesc_destroy(marker); do_leak_check(); } int main(void) { test_here_doc_filename_generation(); - test_here_doc_null_args(); + test_here_doc_invalid_args(); + test_here_doc_only_end_marker(); return (0); }