The change in behavior can be seen with the invocations below:

    $ time ./src/groups-prev $(yes collin | head -n 10000 | tr '\n' ' ') >&-
    groups-prev: write error: Bad file descriptor
    
    real        0m35.295s
    user        0m9.271s
    sys 0m17.325s
    $ time ./src/groups $(yes collin | head -n 10000 | tr '\n' ' ') >&-
    groups: write error: Bad file descriptor
    
    real        0m0.021s
    user        0m0.006s
    sys 0m0.018s

-- 8< --

* NEWS: Mention the improvement.
* src/groups.c (main): Call fflush for each listed user to check for
write errors.
---
 NEWS         | 3 +++
 src/groups.c | 3 +++
 2 files changed, 6 insertions(+)

diff --git a/NEWS b/NEWS
index cf1b1414e..13bb6031a 100644
--- a/NEWS
+++ b/NEWS
@@ -18,6 +18,9 @@ GNU coreutils NEWS                                    -*- 
outline -*-
 
 ** Improvements
 
+  'group' will now exit immediately upon receiving a write error, which is
+  significant when listing information for many users.
+
   'nl' now supports multi-byte --section-delimiter characters.
 
   'shuf -i' now operates up to two times faster on systems with unlocked stdio
diff --git a/src/groups.c b/src/groups.c
index faed5c810..3daf410d9 100644
--- a/src/groups.c
+++ b/src/groups.c
@@ -134,6 +134,9 @@ main (int argc, char **argv)
           if (!print_group_list (argv[optind], ruid, rgid, egid, true, ' '))
             ok = false;
           putchar ('\n');
+
+          if (fflush (stdout) < 0)
+            write_error ();
         }
     }
 
-- 
2.53.0


Reply via email to