summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPádraig Brady <P@draigBrady.com>2025-10-22 16:11:53 +0100
committerPádraig Brady <P@draigBrady.com>2025-10-22 16:18:06 +0100
commitc12a2ecb53e9c141e705a2e4645de5201237a1f6 (patch)
tree1a6d6527dfd46154be0fdcbffa4303579a5c6827
parent5971f450b5abffd5a335a8937d6d588431137594 (diff)
downloadcoreutils-c12a2ecb53e9c141e705a2e4645de5201237a1f6.tar.gz
coreutils-c12a2ecb53e9c141e705a2e4645de5201237a1f6.zip
fmt: promptly diagnose write errors
* NEWS: Mention the improvement. * src/fmt.c (put_line): Exit if any error writing line. (flush_paragraph): Exit if any error writing buffer. * tests/misc/write-errors.sh: Enable the (flush_paragraph) test case, and add another to check the put_line() case.
-rw-r--r--NEWS3
-rw-r--r--src/fmt.c8
-rwxr-xr-xtests/misc/write-errors.sh3
3 files changed, 12 insertions, 2 deletions
diff --git a/NEWS b/NEWS
index b49b73c54..55fe59503 100644
--- a/NEWS
+++ b/NEWS
@@ -43,6 +43,9 @@ GNU coreutils NEWS -*- outline -*-
** Improvements
+ 'fmt' will now exit promptly upon receiving a write error,
+ which is significant when reading large / unbounded inputs.
+
'install' now uses posix_spawn() to invoke the strip program more efficiently.
'numfmt':
diff --git a/src/fmt.c b/src/fmt.c
index 244a8a5c9..7a02205a8 100644
--- a/src/fmt.c
+++ b/src/fmt.c
@@ -810,7 +810,10 @@ flush_paragraph (void)
if (word_limit == word)
{
- fwrite (parabuf, sizeof *parabuf, wptr - parabuf, stdout);
+ size_t to_write = wptr - parabuf;
+ if (fwrite (parabuf, 1, to_write, stdout) != to_write)
+ write_error ();
+
wptr = parabuf;
return;
}
@@ -1010,6 +1013,9 @@ put_line (WORD *w, int indent)
put_word (w);
last_line_length = out_column;
putchar ('\n');
+
+ if (ferror (stdout))
+ write_error ();
}
/* Output to stdout the word W. */
diff --git a/tests/misc/write-errors.sh b/tests/misc/write-errors.sh
index 782ccae5a..48f9c0f1a 100755
--- a/tests/misc/write-errors.sh
+++ b/tests/misc/write-errors.sh
@@ -33,7 +33,8 @@ cut -z -f1- /dev/zero
dd if=/dev/zero
expand /dev/zero
factor --version; yes 1 | factor
-# TODO: fmt /dev/zero
+fmt /dev/zero
+fmt --version; yes | fmt
fold /dev/zero
fold -b /dev/zero
fold -c /dev/zero