From 4c4afad943d9aea3f84db3d069abebbfa20e46da Mon Sep 17 00:00:00 2001 From: Collin Funk Date: Tue, 28 Apr 2026 18:28:25 -0700 Subject: [PATCH] pinky: don't print output in the wrong order when fully buffered * NEWS: Mention the bug fix. * src/pinky.c (cat_file): Prefer streams to file descriptors when writing to standard output. --- NEWS | 4 ++++ src/pinky.c | 19 +++++++++---------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/NEWS b/NEWS index d783176691..8bab3d6a8e 100644 --- a/NEWS +++ b/NEWS @@ -8,6 +8,10 @@ GNU coreutils NEWS -*- outline -*- mistakenly exit with a nonzero status. [This bug was present in "the beginning".] + 'pinky -l' no longer no longer prints output in the incorrect order when + standard output is fully buffered, e.g., when redirected to a file. + [bug introduced in coreutils-9.10] + 'uniq -w' no longer overruns the read buffer in multibyte locales. [bug introduced in coreutils-9.5] diff --git a/src/pinky.c b/src/pinky.c index 625c426ba2..d336c51c74 100644 --- a/src/pinky.c +++ b/src/pinky.c @@ -318,22 +318,21 @@ static void cat_file (char const *header, char const *home, char const *file) { char *full_name = file_name_concat (home, file, NULL); - int fd = open (full_name, O_RDONLY); + FILE *fp = fopen (full_name, "r"); - if (0 <= fd) + if (fp) { - idx_t header_len = strlen (header); - if (write (STDOUT_FILENO, header, header_len) != header_len) - write_error (); + fputs (header, stdout); - fdadvise (fd, 0, 0, FADVISE_SEQUENTIAL); + fadvise (fp, FADVISE_SEQUENTIAL); - char buf[IO_BUFSIZE]; - for (ssize_t bytes_read; 0 < (bytes_read = read (fd, buf, sizeof buf));) - if (full_write (STDOUT_FILENO, buf, bytes_read) != bytes_read) + char buf[BUFSIZ]; + for (size_t bytes_read; + 0 < (bytes_read = fread (buf, 1, sizeof buf, fp));) + if (fwrite (buf, 1, bytes_read, stdout) != bytes_read) write_error (); - close (fd); + fclose (fp); } free (full_name); -- 2.47.3