setlinebuf(stdin)

Richard A. O'Keefe ok at quintus.uucp
Mon Aug 15 07:17:16 AEST 1988


In article <12994 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:
>In article <291 at quintus.UUCP> ok at 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 $< .



More information about the Comp.unix.wizards mailing list