diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2021-08-22 11:54:44 -0700 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2021-08-22 13:10:26 -0700 |
commit | 88846befff58d181c5f77d50ec4b11980af16563 (patch) | |
tree | fec8553f98b934baed2efa7435b214c6836b479d /src/fold.c | |
parent | 4d355e52703633a0f8416aeacabd9586a35305a8 (diff) | |
download | coreutils-88846befff58d181c5f77d50ec4b11980af16563.tar.gz |
maint: use clearerr on stdin when appropriate
This is so that commands like ‘fmt - -’ read from stdin
both times, even when it is a tty. Fix some other minor
issues that are related.
* src/blake2/b2sum.c (main):
* src/cksum.c (cksum):
* src/cut.c (cut_file):
* src/expand-common.c (next_file):
* src/fmt.c (fmt):
* src/fold.c (fold_file):
* src/md5sum.c (digest_file, digest_check):
* src/nl.c (nl_file):
* src/od.c (check_and_close):
* src/paste.c (paste_parallel, paste_serial):
* src/pr.c (close_file):
* src/sum.c (bsd_sum_file):
Use clearerr on stdin so that stdin can be read multiple times
even if it is a tty. Do not assume that ferror preserves errno as
POSIX does not guarantee this. Coalesce duplicate diagnostic
calls.
* src/blake2/b2sum.c (main):
* src/fmt.c (main, fmt):
Report read error, even if it's merely fclose failure.
* src/fmt.c: Include die.h.
(fmt): New arg FILE. Close input (reporting error) if not stdin.
All callers changed.
* src/ptx.c (swallow_file_in_memory): Clear stdin's EOF flag.
* src/sort.c (xfclose): Remove unnecessary feof call.
Diffstat (limited to 'src/fold.c')
-rw-r--r-- | src/fold.c | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/src/fold.c b/src/fold.c index ae33dd368..94a6d378e 100644 --- a/src/fold.c +++ b/src/fold.c @@ -216,20 +216,20 @@ fold_file (char const *filename, size_t width) } saved_errno = errno; + if (!ferror (istream)) + saved_errno = 0; if (offset_out) fwrite (line_out, sizeof (char), (size_t) offset_out, stdout); - if (ferror (istream)) + if (STREQ (filename, "-")) + clearerr (istream); + else if (fclose (istream) != 0 && !saved_errno) + saved_errno = errno; + + if (saved_errno) { error (0, saved_errno, "%s", quotef (filename)); - if (!STREQ (filename, "-")) - fclose (istream); - return false; - } - if (!STREQ (filename, "-") && fclose (istream) == EOF) - { - error (0, errno, "%s", quotef (filename)); return false; } |