From: Collin Funk Date: Sat, 21 Mar 2026 08:07:28 +0000 (-0700) Subject: tac: promptly diagnose write errors X-Git-Tag: v9.11~159 X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=30a5cbec0efc2941513c5f51652fb851b7b87bba;p=thirdparty%2Fcoreutils.git tac: promptly diagnose write errors This patch also fixes a bug where 'tac' would print a vague error on some inputs: $ seq 10000 | ./src/tac-prev > /dev/full tac-prev: write error $ seq 10000 | ./src/tac > /dev/full tac: write error: No space left on device In this case ferror (stdout) is true, but errno has been set back to zero by a successful fclose (stdout) call. * src/tac.c (output): Call write_error() if fwrite fails. * tests/misc/io-errors.sh: Check that 'tac' prints a detailed write error. * NEWS: Mention the improvement. --- diff --git a/NEWS b/NEWS index 42d1f2e2e9..2fabd07b76 100644 --- a/NEWS +++ b/NEWS @@ -59,6 +59,9 @@ GNU coreutils NEWS -*- outline -*- 'shuf -i' now operates up to two times faster on systems with unlocked stdio functions. + 'tac' will now exit sooner after a write error, which is significant when + operating on a file with many lines. + 'timeout' now properly detects when it is reparented by a subreaper process on Linux instead of init, e.g., the 'systemd --user' process. diff --git a/src/tac.c b/src/tac.c index 4cab99d0b3..e63f70fc30 100644 --- a/src/tac.c +++ b/src/tac.c @@ -158,7 +158,8 @@ output (char const *start, char const *past_end) if (!start) { - fwrite (buffer, 1, bytes_in_buffer, stdout); + if (fwrite (buffer, 1, bytes_in_buffer, stdout) != bytes_in_buffer) + write_error (); bytes_in_buffer = 0; return; } @@ -169,7 +170,8 @@ output (char const *start, char const *past_end) memcpy (buffer + bytes_in_buffer, start, bytes_available); bytes_to_add -= bytes_available; start += bytes_available; - fwrite (buffer, 1, WRITESIZE, stdout); + if (fwrite (buffer, 1, WRITESIZE, stdout) != WRITESIZE) + write_error (); bytes_in_buffer = 0; bytes_available = WRITESIZE; } diff --git a/tests/misc/io-errors.sh b/tests/misc/io-errors.sh index d5dde67a7c..d8efee08a2 100755 --- a/tests/misc/io-errors.sh +++ b/tests/misc/io-errors.sh @@ -55,6 +55,7 @@ od -v foo paste foo pr foo seq 1 +tac --version; seq 10000 | tac tail -n1 foo tee < foo tr . . < foo