From dae475092d9bc9c60ffef8f130b2333ea4a88eb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlo=20Marcelo=20Arenas=20Bel=C3=B3n?= Date: Tue, 9 Nov 2021 09:15:38 -0800 Subject: [PATCH] pcre2grep: avoid portability minefield with buffered fseek(stdin) (#36) To allow pcre2grep to do an early exit in a resumable way, -m uses fseek on stdin, which is sadly not supported in several platforms. Most of the conflicting issues come from the fact that managing the position while buffering is not trivial, and is therefore an optional feature[1] of POSIX.1-2017 Workaround this by removing the buffer to stdin, if the -m option is being used. There is likely not a significant performance benefit even for the platforms that support it, but it could be conditionally added in that case, later. Fixes: #10 [1] https://pubs.opengroup.org/onlinepubs/9699919799/functions/fseek.html --- src/pcre2grep.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/pcre2grep.c b/src/pcre2grep.c index 519346b..6e397eb 100644 --- a/src/pcre2grep.c +++ b/src/pcre2grep.c @@ -3275,6 +3275,7 @@ FILE *zos_test_file; if (strcmp(pathname, "-") == 0) { + if (count_limit >= 0) setbuf(stdin, NULL); return pcre2grep(stdin, FR_PLAIN, stdin_name, (filenames > FN_DEFAULT || (filenames == FN_DEFAULT && !only_one_at_top))? stdin_name : NULL); @@ -4482,6 +4483,11 @@ no file arguments, search stdin, and then exit. */ if (file_lists == NULL && i >= argc) { + /* Using a buffered stdin, that then is seek is not portable, + so attempt to remove the buffer, to workaround reported issues + affecting several BSD and AIX */ + if (count_limit >= 0) + setbuf(stdin, NULL); rc = pcre2grep(stdin, FR_PLAIN, stdin_name, (filenames > FN_DEFAULT)? stdin_name : NULL); goto EXIT;