Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 22 Jul 1997 22:09:10 -0700
From:      Paul Traina <pst@shockwave.com>
To:        hackers@freebsd.org
Subject:   how to tell if getchar() will block -- select doesn't cut it
Message-ID:  <199707230509.WAA13342@precipice.shockwave.com>

next in thread | raw e-mail | index | archive | help
I had some fun this week -- I'd added some asynchronous event code to a
little line editor so that I could deal with some events while waiting
from user input...

The code looked something like:

	<select cruft to wait on several sockets>

	if (read data available on stdin) {
	    c = getchar();
	    go do more processing;
	}

This worked fine and dandy except that pasting into a window running this
program didn't work properly -- characters seemed to get lost.

The obvious problem is that the paste was sending down many characters,
stdio read more than one character and stored it in a local buffer, so
there was data available for getchar(), but of course, the low level buffer
had already been slurped in, so the select wasn't returning true.

I worked around this by checking, in a non-portable fashion, if stdio had
any data available for it (if (fp->_r >= 0 || fp->_ur >= 0)) but quite
frankly, non-portable code sucks.

I did a reasonable perusal of the source code in stdio and I can't seem
to find any portable way to ask the general question:

	Is getchar() going to block if I call it?

Ideas?



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199707230509.WAA13342>