Skip to content

Commit dae4750

Browse files
authored
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
1 parent 1ed34b9 commit dae4750

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/pcre2grep.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3275,6 +3275,7 @@ FILE *zos_test_file;
32753275

32763276
if (strcmp(pathname, "-") == 0)
32773277
{
3278+
if (count_limit >= 0) setbuf(stdin, NULL);
32783279
return pcre2grep(stdin, FR_PLAIN, stdin_name,
32793280
(filenames > FN_DEFAULT || (filenames == FN_DEFAULT && !only_one_at_top))?
32803281
stdin_name : NULL);
@@ -4482,6 +4483,11 @@ no file arguments, search stdin, and then exit. */
44824483

44834484
if (file_lists == NULL && i >= argc)
44844485
{
4486+
/* Using a buffered stdin, that then is seek is not portable,
4487+
so attempt to remove the buffer, to workaround reported issues
4488+
affecting several BSD and AIX */
4489+
if (count_limit >= 0)
4490+
setbuf(stdin, NULL);
44854491
rc = pcre2grep(stdin, FR_PLAIN, stdin_name,
44864492
(filenames > FN_DEFAULT)? stdin_name : NULL);
44874493
goto EXIT;

0 commit comments

Comments
 (0)