I pushed this patch to fix a silly mistake of mine in a coreutils-9.10
commit [1]. It can be easily seen like so:

    $ echo a > ~/.plan
    $ pinky -l collin
    Login name: collin                      In real life:  Collin Funk
    Directory: /home/collin                 Shell:  /bin/bash
    Plan:
    a
    
    $ stdbuf -o8K pinky -l collin
    Plan:
    a
    Login name: collin                      In real life:  Collin Funk
    Directory: /home/collin                 Shell:  /bin/bash

A another reasonable way to fix this is to flush standard output
before printing the ~/.plan or ~/.project files. However, since those
files are typically very small, it seems better to just use fread
here and avoid flushing.

[1] 
https://github.com/coreutils/coreutils/commit/3c405ae0b6c719f36903297a8c940172b0d4cb4c

-- 8< --

* 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 d78317669..8bab3d6a8 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 625c426ba..d336c51c7 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.53.0


Reply via email to