Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 23 Jul 1997 00:18:40 -0600 (MDT)
From:      Marc Slemko <marcs@znep.com>
To:        Paul Traina <pst@shockwave.com>
Cc:        hackers@FreeBSD.ORG
Subject:   Re: how to tell if getchar() will block -- select doesn't cut it
Message-ID:  <Pine.BSF.3.95.970723001420.24312B-100000@alive.znep.com>
In-Reply-To: <199707230509.WAA13342@precipice.shockwave.com>

next in thread | previous in thread | raw e-mail | index | archive | help
If you can't tell if getchar() will block, how about just making it so it
won't block?

Say something like:

	fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK)

Then just do a getchar() and make the appropriate checks to see if it
actually got something or if it returned EAGAIN in errno.

Not completely portable, and some systems return EWOULDBLOCK instead, but
it may do what you want...

On Tue, 22 Jul 1997, Paul Traina wrote:

> 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?Pine.BSF.3.95.970723001420.24312B-100000>