bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Possible bug with grep/sed/tail?


From: Jim Meyering
Subject: Re: Possible bug with grep/sed/tail?
Date: Fri, 21 Nov 2008 13:38:12 +0100

Brian Dessent <address@hidden> wrote:
> It has been said before in previous threads but I want to re-state that
> this is possible in a very simple and general way with the LD_PRELOAD 
> facility:
>
> $ echo '__attribute__((constructor)) void f() { setvbuf (stdout, NULL, 
> _IOLBF, 0); }' | \
>   gcc -include stdio.h -x c - -fPIC -shared -o linebuf.so
>
> $ ( while true; do echo "foo"; sleep 1; done ) | LD_PRELOAD=./linebuf.so grep 
> foo | cat
> foo
> foo
> foo
> ... # immediate output

Nice!
This deserves to be better known.

What we need is a convenient mechanism by which to use this technique.

I.e., we need a new tool, maybe named line-buffer, that sets
LD_PRELOAD as in your example, and then exec's the target program.

If the new program (use env.c as a model) were to come
with documentation and sufficient portability, I'd probably
be happy to add it to coreutils.  [hmm... that'd mean
coreutils would end up installing its first shared library. ]

I.e., with a line-buffer program, (C equivalent of this):

  $ cat line-buffer
  #!/bin/sh
  LD_PRELOAD=/path/to/linebuf.so exec "$@"

your example would become:

  $ ( while :; do echo foo; sleep 1; done ) | line-buffer grep foo | cat
  foo
  foo
  foo
  ... # immediate output

However, that does have the down-side of exec'ing grep,
and not running your shell function or alias.
But if the shared library is installed, you can
instead use your own line-buffer function:

line-buffer()
{
  LD_PRELOAD=/t/linebuf.so "$@"
}

but that doesn't expand the command name when it's an alias or function.
Hmm... I know you can do that in bash and zsh, but don't remember
how off-hand.  Time to read the manuals...

But now, when I see it's so easy to roll your own,
I wonder if it's worth adding a C program to do that for you.

Opinions?




reply via email to

[Prev in Thread] Current Thread [Next in Thread]