[comp.unix.wizards] setlinebuf

chris@mimsy.UUCP (Chris Torek) (08/14/88)

In article <291@quintus.UUCP> ok@quintus.uucp (Richard A. O'Keefe) writes:
>The remedy is simple.  Only your program needs to change.
>Before reading from stdin, do
>	setbuf(stdin, (char*)NULL);	/* all flavours */
>or	setlinebuf(stdin);		/* BSD */
>or use setvbuf in System V.

Setlinebuf() has no effect on input streams.  I imagine setvbuf will
not set line-at-a-time reading on SysV either.  Think about it:  how
will you tell the system to read only up through a newline?

Using setbuf(stdin, (char *)NULL) will do the trick, but is terribly
inefficient.  I prefer my fseek-before-exit suggestion . . . .
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

ok@quintus.uucp (Richard A. O'Keefe) (08/15/88)

In article <12994@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
>In article <291@quintus.UUCP> ok@quintus.uucp (Richard A. O'Keefe) writes:
>>The remedy is simple.  Only your program needs to change.
>>Before reading from stdin, do
>>	setbuf(stdin, (char*)NULL);	/* all flavours */
>>or	setlinebuf(stdin);		/* BSD */
>>or use setvbuf in System V.
>
>Setlinebuf() has no effect on input streams.  I imagine setvbuf will
>not set line-at-a-time reading on SysV either.  Think about it:  how
>will you tell the system to read only up through a newline?

Obviously, you can't.  Just as obviously, if you are reading from a
device which can seek, after reading the line you could seek back to
the end of the line.  Or you could simulate line buffering by doing
character buffering.

I made a classic mistake:  I assumed stdio was smart and, mea culpa,
tested one method and suggested an additional one I hadn't tested.
Stupid, stupid!

>Using setbuf(stdin, (char *)NULL) will do the trick, but is terribly
>inefficient.  I prefer my fseek-before-exit suggestion . . . .

The snag with that (which is what I *thought* setlinebuf did on input,
silly me) is that it won't work on pipes.  setbuf(stdin, (char*)NULL)
_will_ work on pipes, and we're only talking about reading a couple
of dozen characters, so the overhead is small compared with the overhead
of starting up a new process instead of using $< .