Subject: bug#8961: stdbuf has no effect on some programs

From: Pádraig Brady <[email-masked]>
Date: Mon, 04 Jul 2011 10:08:06 +0100

On 03/07/11 23:38, Pádraig Brady wrote:
> On 03/07/11 18:50, Bruno Haible wrote:
>> I wrote:
>>> Likewise for "-o0".
>>
>> Oops, please forget the statement about -o0. With the modified GNU libiconv
>> 'iconv' program, -o0 does make a difference:
>>   $ (echo -n abc; sleep 1; echo def) | iconv
>> vs.
>>   $ (echo -n abc; sleep 1; echo def) | stdbuf -o0 iconv
>> In the first case, output is line buffered. In the second case, "abc"
>> occurs immediately.
>>
>> Bruno
> 
> Also "-i0" is useful if a command doesn't consume all of input,
> and needs a subsequent command to process the rest, like:
> 
> producer | (stdbuf -i0 cmd1; cmd2)
> 
> I'll expand the info to make this function apparent.

Hmm, this especially useful with sed, where one
can [Qq]uit early depending on the input.

But I notice that sed no longer supports `setbuf -i0`.
This is because of 3a8e165a which discards the
original stdin and instead fdopens() a new one
to support reading in binary mode on windos.
http://git.sv.gnu.org/gitweb/?p=sed.git;a=commit;h=3a8e165a

Note in sed 4.2.2 the -u option was enhanced to
support this internally (the documentation had already
said that -u did this), so only sed 4.2 and 4.2.1
have no way to control this.

In any case there is no point I think opening
a new stream for stdin on non windos platforms
as it adds a bit of overhead, and precludes the
use of setbuf -i...

So how about the attached patch?

cheers,
Pádraig.
>From 80d5c3a49b7819b14abf3ad728bb6253689ed1ee Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <[email-masked]>
Date: Mon, 4 Jul 2011 08:24:21 +0100
Subject: [PATCH] avoid reopening stdin

This adjusts the change made in 3a8e165a,
to not reopen stdin on platforms where there
is no separate "binary mode".

That added some overhead and also precludes the
use of tools like `stdbuf -i...`.

* execute.c (open_next_file): Only reopen stdin on windos.
---
 sed/execute.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)

diff --git a/sed/execute.c b/sed/execute.c
index befc8f9..076ed0a 100644
--- a/sed/execute.c
+++ b/sed/execute.c
@@ -701,7 +701,11 @@ open_next_file(name, input)
   if (name[0] == '-' && name[1] == '\0' && !in_place_extension)
     {
       clearerr(stdin);	/* clear any stale EOF indication */
+#if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS) || defined(__EMX__)
       input->fp = ck_fdopen (fileno (stdin), "stdin", read_mode, false);
+#else
+      input->fp = stdin;
+#endif
     }
   else if ( ! (input->fp = ck_fopen(name, read_mode, false)) )
     {
-- 
1.7.5.2