get character without waiting

der Mouse mouse at thunder.mcrcim.mcgill.edu
Sun Jun 16 06:51:36 AEST 1991


In article <1991Jun12.001056.24021 at watdragon.waterloo.edu>, nkkwan at 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 at larry.mcrcim.mcgill.edu



More information about the Comp.unix.programmer mailing list