[comp.unix.programmer] get character without waiting

nkkwan@crocus.uwaterloo.ca () (06/12/91)

How can I check whether there is character waiting in standard input or not? 
What I want to do is something like..
   
If there is a character, then I read it. But if not, I can do something else.

bhoughto@pima.intel.com (Blair P. Houghton) (06/12/91)

In article <1991Jun12.001056.24021@watdragon.waterloo.edu> nkkwan@crocus.uwaterloo.ca writes:
>
>How can I check whether there is character waiting in standard input or not? 
>What I want to do is something like..
>   
>If there is a character, then I read it. But if not, I can do something else.

Y'know, I've often wondered what it was that possessed the
forgers of unix to name that damn function "select(2)" instead
of "checkiobuffersforremainingcharacters(2)".

				--Blair
				  "This particular question has
				   passed beyond the realm of the
				   frequently-asked and into the
				   paradigm of the self-asking."

elston%boot.decnet@edwards-tems.af.mil (Mark Elston, NSI Inc., Edwards AFB CA) (06/12/91)

In article <4669@inews.intel.com>, bhoughto@pima.intel.com (Blair P. Houghton) writes:
> In article <1991Jun12.001056.24021@watdragon.waterloo.edu> nkkwan@crocus.uwaterloo.ca writes:
>>
>>How can I check whether there is character waiting in standard input or not? 
>>What I want to do is something like..
>>   
>>If there is a character, then I read it. But if not, I can do something else.
> 
> Y'know, I've often wondered what it was that possessed the
> forgers of unix to name that damn function "select(2)" instead
> of "checkiobuffersforremainingcharacters(2)".

That's great for all of you with "select(2)" in your libraries.  I'm forced to
live with SysVR2 without select()!!

Is there another way????

Mark.
-- 
---------------------------------------------------------------------------
elston@edwards-tems.af.mil    | "I know there are people in the world who |
Mark Elston, NSI Inc.         |  do not love their fellow human beings and|
"What business in their right |  I *HATE* people like that..."            |
 mind would want my opinion   |            -Tom Lehrer                    |
 anyway"                      |              on National Brotherhood Week |
---------------------------------------------------------------------------

bhoughto@pima.intel.com (Blair P. Houghton) (06/14/91)

In article <1991Jun12.085022.73@%boot.decnet@edwards-tems.af.mil> elston%boot.decnet@edwards-tems.af.mil (Mark Elston, NSI Inc., Edwards AFB CA) writes:
>In article <4669@inews.intel.com>, bhoughto@pima.intel.com (Blair P. Houghton) writes:
>> Y'know, I've often wondered what it was that possessed the
>> forgers of unix to name that damn function "select(2)" instead
>> of "checkiobuffersforremainingcharacters(2)".
>
>That's great for all of you with "select(2)" in your libraries.

I wasn't worried about how; I really do want to know
why it's called "select".

And the answer is, "RTFFAQ."

				--Blair
				  "What is poll(2) or rdchk(2), Alex?"

mouse@thunder.mcrcim.mcgill.edu (der Mouse) (06/16/91)

In article <1991Jun12.001056.24021@watdragon.waterloo.edu>, nkkwan@crocus.uwaterloo.ca writes:

> How can I check whether there is character waiting in standard input
> or not?

The only real answer is "there is no portable way".  Depending on
precisely what you mean by "standard input", there may be a
not-horribly-nonportable way.

If you mean "readable from file descriptor 0", you can try one or more
of the following, which have varying degrees of portability:

- FIONREAD ioctl
- select(2)
- poll(2)
- non-blocking mode via the FIONBIO ioctl
- non-blocking mode via the F_SETFL fcntl
- set a timeout (with alarm, or maybe setitimer) and do a read()
- if you care about only ttys, and you have the SysV tty driver
   interface, disable ICANON and set VTIME
- probably others I don't know of or have forgotten

If you mean "waiting in the stdio input buffer", there is no good
solution; about the closest you can come is to check stdin->_cnt and
hope your stdio implementation (a) has such a field and (b) keeps
something useful there.  (Neither of those is guaranteed by anything.)

					der Mouse

			old: mcgill-vision!mouse
			new: mouse@larry.mcrcim.mcgill.edu

mouse@thunder.mcrcim.mcgill.edu (der Mouse) (06/16/91)

In article <4669@inews.intel.com>, bhoughto@pima.intel.com (Blair P. Houghton) writes:

> Y'know, I've often wondered what it was that possessed the forgers of
> unix to name that damn function "select(2)" instead of
> "checkiobuffersforremainingcharacters(2)".

For one thing, the latter requires much more typing, and is harder to
spell correctly. :-)

More seriously, select() does more than just check for available
characters; it can also be used (in conjunction with non-blocking I/O)
for programs that can't afford to block in a write(), and also has
timeout facilities that are useful in their own right....

					der Mouse

			old: mcgill-vision!mouse
			new: mouse@larry.mcrcim.mcgill.edu

nash@ucselx.sdsu.edu (Ron Nash) (06/18/91)

mouse@thunder.mcrcim.mcgill.edu (der Mouse) writes:

>In article <1991Jun12.001056.24021@watdragon.waterloo.edu>, nkkwan@crocus.uwaterloo.ca writes:

>> How can I check whether there is character waiting in standard input
>> or not?

>The only real answer is "there is no portable way".  Depending on
>precisely what you mean by "standard input", there may be a
>not-horribly-nonportable way.

A while back, in comp.sources.misc, Dan Smith posted a routine named
"grabchars" that does this for Sun's and BSD systems.  Email me if you
want a copy.  Here is a brief description:

} v05i073: grabchars 1.3, get/filter keystrokes directly from user (BSD)

}         "grabchars" gets one or more keystrokes from the user, without
} requiring them to hit return.  It was written to make shell scripts
} (doesn't matter what type) more interactive.
...
}         Enjoy, and send me your comments/suggestions.  2.0 will provide
} SYS V support.


-- 
Ron Nash 	San Diego State University
Internet:  	nash@ucselx.sdsu.edu
Gin-N-Tonic	5 year old 1/2 Arab endurance prospect
Luv on Fire	8 year old Arab, trusty steed and friend