diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2021-08-21 10:44:17 -0700 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2021-08-21 10:45:06 -0700 |
commit | ad6de316cca655cd8b0b20b3e9dd18e7e98e443a (patch) | |
tree | 4845aa84a099cb911accd418fbc4eac6827eb77d /src | |
parent | 9f296c1238183261afd47e27bf63e505f0b358d5 (diff) | |
download | grep-ad6de316cca655cd8b0b20b3e9dd18e7e98e443a.tar.gz |
grep: avoid sticky problem with ‘-f - -f -’
Inspired by bug#50129 even though this is a different bug.
* src/grep.c (main): For ‘-f -’, use clearerr (stdin) after
reading, so that ‘grep -f - -f -’ reads stdin twice even
when stdin is a tty. Also, for ‘-f FILE’, report any
I/O error when closing FILE.
Diffstat (limited to 'src')
-rw-r--r-- | src/grep.c | 17 |
1 files changed, 11 insertions, 6 deletions
@@ -2477,7 +2477,6 @@ main (int argc, char **argv) int matcher = -1; int opt; int prev_optind, last_recursive; - int fread_errno; intmax_t default_context; FILE *fp; exit_failure = EXIT_TROUBLE; @@ -2648,11 +2647,17 @@ main (int argc, char **argv) if (cc == 0) break; } - fread_errno = errno; - if (ferror (fp)) - die (EXIT_TROUBLE, fread_errno, "%s", optarg); - if (fp != stdin) - fclose (fp); + int err = errno; + if (!ferror (fp)) + { + err = 0; + if (fp == stdin) + clearerr (fp); + else if (fclose (fp) != 0) + err = errno; + } + if (err) + die (EXIT_TROUBLE, err, "%s", optarg); /* Append final newline if file ended in non-newline. */ if (newkeycc != keycc && keys[newkeycc - 1] != '\n') keys[newkeycc++] = '\n'; |