Subject: bug#18750: closed (Re: --line-buffered option doesn't work with --files-with-matches)

From: [email-masked] (GNU bug Tracking System)
Date: Thu, 31 Dec 2015 08:30:03 +0000

Your bug report

#18750: --line-buffered option doesn't work with --files-with-matches

which was filed against the grep package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
18750: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=18750
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: --line-buffered option doesn't work with --files-with-matches
From: Paul Eggert <[email-masked]>
To: [email-masked]
Cc: "Louis S." <[email-masked]>, Norihiro Tanaka <[email-masked]>
Date: Thu, 31 Dec 2015 00:29:46 -0800
List-Id: <debbugs-submit.debbugs.gnu.org>

Thanks for the bug report. It also seems to me that --line-buffered should apply 
to nulls, not newlines, when -z is used. Also, there are a couple of other 
places where stdout should get flushed after a newline. Perhaps we should use 
setvbuf instead (is it faster?) when -z is not also used, but right now I just 
want to get the bug fixed for the next release, so I installed the attached, 
more-conservative, patch, and am marking this bug as done.
>From 01304357cbfd655ebe4c27f5ccde8d07c2f3bbb1 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Thu, 31 Dec 2015 00:20:54 -0800
Subject: [PATCH] grep: fix -l --line-buffer bug

Problem reported by Louis Sautier in: http://bugs.gnu.org/18750
* NEWS: Document this.
* src/grep.c (grep, grepdesc): If --line-buffered, flush
stdout after outputting newline (or null byte, if applicable).
---
 NEWS       |  3 +++
 src/grep.c | 10 +++++++++-
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index 4bf408c..b451d76 100644
--- a/NEWS
+++ b/NEWS
@@ -23,6 +23,9 @@ GNU grep NEWS                                    -*- outline -*-
   This partly reverts the --exclude-related change in 2.22.
   [bug introduced in grep-2.22]
 
+  --line-buffer is no longer ineffective when combined with -l
+  [bug introduced in grep-2.5]
+
 
 * Noteworthy changes in release 2.22 (2015-11-01) [stable]
 
diff --git a/src/grep.c b/src/grep.c
index 03a1bff..06f3345 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -1481,7 +1481,11 @@ grep (int fd, struct stat const *st)
   done_on_match = done_on_match_0;
   out_quiet = out_quiet_0;
   if ((has_nulls || encoding_error_output) && !out_quiet && nlines != 0)
-    printf (_("Binary file %s matches\n"), filename);
+    {
+      printf (_("Binary file %s matches\n"), filename);
+      if (line_buffered)
+        fflush (stdout);
+    }
   return nlines;
 }
 
@@ -1725,6 +1729,8 @@ grepdesc (int desc, bool command_line)
                 fputc (0, stdout);
             }
           printf ("%" PRIdMAX "\n", count);
+          if (line_buffered)
+            fflush (stdout);
         }
 
       status = !count;
@@ -1732,6 +1738,8 @@ grepdesc (int desc, bool command_line)
         {
           print_filename ();
           fputc ('\n' & filename_mask, stdout);
+          if (line_buffered)
+            fflush (stdout);
         }
 
       if (desc == STDIN_FILENO)
-- 
2.5.0

Subject: --line-buffered option doesn't work with --files-with-matches
From: "Louis S." <[email-masked]>
To: [email-masked]
Date: Thu, 16 Oct 2014 23:16:30 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

Hi, I'm not quite sure this is a bug but it appears that the
--line-buffered option has no effect when used in conjunction with
--files-with-matches.
Here are some tests I ran with grep 2.20:
$ time for i in {1..100}; do grep --line-buffered -Rl gnome /usr/ |
head -n1 >& /dev/null; done
real    0m54.831s
user    0m37.077s
sys     0m17.550s

$ time for i in {1..100}; do grep -Rl gnome /usr/ | head -n1 >& /dev/null; done
real    0m54.858s
user    0m36.987s
sys     0m17.657s

$ time for i in {1..100}; do grep --line-buffered -R gnome /usr/ |
head -n1 >& /dev/null; done
real    0m7.414s
user    0m4.727s
sys     0m2.613s

Please let me know if line-buffering is supposed to work in that case.

Regards,

Louis