Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!mouse From: mouse@thunder.mcrcim.mcgill.edu (der Mouse) Newsgroups: comp.unix.programmer Subject: Re: get character without waiting Message-ID: <1991Jun15.205136.4545@thunder.mcrcim.mcgill.edu> Date: 15 Jun 91 20:51:36 GMT References: <1991Jun12.001056.24021@watdragon.waterloo.edu> Organization: McGill Research Centre for Intelligent Machines Lines: 32 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