Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!cornell!uw-beaver!teknowledge-vaxc!sri-unix!quintus!ok
From: ok@quintus.uucp (Richard A. O'Keefe)
Newsgroups: comp.unix.wizards
Subject: Re: setlinebuf(stdin)
Keywords: open(2), close(2), exit(2)
Message-ID: <294@quintus.UUCP>
Date: 14 Aug 88 21:17:16 GMT
References: <1122@ssc-bee.ssc-vax.UUCP> <291@quintus.UUCP> <12994@mimsy.UUCP>
Sender: news@quintus.UUCP
Reply-To: ok@quintus.UUCP (Richard A. O'Keefe)
Organization: Quintus Computer Systems, Inc.
Lines: 29

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 $< .