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 <egg...@cs.ucla.edu>
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