git: 7a7f74dbcc4f - main - grep: toss in some explicit fflush()

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Fri, 08 Aug 2025 05:06:03 UTC
The branch main has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=7a7f74dbcc4f41d98218471297eaa629e34326e2

commit 7a7f74dbcc4f41d98218471297eaa629e34326e2
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2025-08-08 04:52:53 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2025-08-08 05:05:10 +0000

    grep: toss in some explicit fflush()
    
    grep|tee of the src/ tree for infrequently-occurring strings is fairly
    annoying; drop some tactical flushes at line-match boundaries to reduce
    the long stalls.  In the case of `grep -o`, we'll flush after multiple
    lines if there are multiple matches within a single line of text, while
    for non`-o` we'll flush generally after every line.
---
 usr.bin/grep/util.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/usr.bin/grep/util.c b/usr.bin/grep/util.c
index ed87e56956f6..5b40405852b3 100644
--- a/usr.bin/grep/util.c
+++ b/usr.bin/grep/util.c
@@ -728,6 +728,8 @@ void grep_printline(struct str *line, int sep) {
 	printline_metadata(line, sep);
 	fwrite(line->dat, line->len, 1, stdout);
 	putchar(fileeol);
+
+	fflush(stdout);
 }
 
 static void
@@ -834,6 +836,7 @@ printline(struct parsec *pc, int sep, size_t *last_out)
 				*last_out = pc->ln.len;
 			}
 			putchar('\n');
+			fflush(stdout);
 		} else if (!oflag) {
 			/*
 			 * -o is terminated on every match output, so this
@@ -843,6 +846,8 @@ printline(struct parsec *pc, int sep, size_t *last_out)
 			 * to terminate if it needs to.
 			 */
 			terminated = false;
+		} else {
+			fflush(stdout);
 		}
 	} else
 		grep_printline(&pc->ln, sep);