[comp.bugs.4bsd] linebuffering & input

ok@quintus (Richard A. O'Keefe) (09/29/88)

This problem exists in SunOS 3.2 and DYNIX "V3.0.12 NFS", so I suspect
that it is a 4.2BSD problem.

% man 3 setbuf
tells us that "when <an output stream> is line buffered, characters are
saved up until a newline is encountered or input is read from stdin."
"setlinebuf is used to change the buffering on a stream from block
buffered or unbuffered to line buffered.  Unlike setbuf and setbuffer
it can be used at any time that the file descriptor is active".

However, the ONLY streams which are flushed when input is read from
stdin are stdout and stderr.

cat >linebug.c <<'END-OF-BUG'
#include <stdio.h>
main()
    {
        char buffer[80];
        FILE *terminal = fopen("/dev/tty", "w");
        
        setlinebuf(terminal);
        fprintf(terminal, "Prompt: ");
        gets(buffer);
	exit(0);
    }
END-OF-BUG
cc -o linebug linebug.c
./linebug

The prompt does not appear until after you have typed the input.

chris@mimsy.UUCP (Chris Torek) (09/29/88)

In article <486@quintus.UUCP> ok@quintus (Richard A. O'Keefe) writes:
>This problem exists in SunOS 3.2 and DYNIX "V3.0.12 NFS", so I suspect
>that it is a 4.2BSD problem.

It is; it persists in 4.3BSD and 4.3BSD-tahoe; and there is no really
easy fix.  You can add your own flush for all line buffered streams:

	static
	flushifl(f)
		FILE *f;
	{

		if (f->_flag & _IOLBF)
			(void) fflush(f);
	}

	flushlb()
	{

		_fwalk(flushifl);
	}

_fwalk is undocumented, so this should be used only `#if needed' as
a workaround.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris