2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: inputReady.c,v 1.3 1998/12/02 13:27:42 simonm Exp $
6 * hReady Runtime Support
9 /* select and supporting types is not */
11 #define NON_POSIX_SOURCE
17 #ifdef HAVE_SYS_TYPES_H
18 #include <sys/types.h>
22 /* this is included from sys/types.h only if _BSD is defined. */
23 /* Since it is not, I include it here. - andre */
24 #include <sys/select.h>
35 #ifdef HAVE_SYS_TIME_H
40 * inputReady(ptr, msecs) checks to see whether input is available
41 * on the file object 'ptr', timing out after (approx.) 'msec' milliseconds.
42 * Input meaning 'can I safely read at least a *character* from this file
43 * object without blocking?'
45 * If the file object has a non-empty buffer, the test is trivial. If not,
46 * we select() on the (readable) file descriptor.
48 * Notice that for file descriptors connected to ttys in non-canonical mode
49 * (i.e., it's buffered), inputReady will not return true until a *complete
54 inputReady(ptr, msecs)
58 IOFileObject* fo = (IOFileObject*)ptr;
59 int c, fd, maxfd, ready;
63 if ( FILEOBJ_IS_EOF(fo) )
66 if ( !FILEOBJ_BUFFER_EMPTY(fo) ) {
67 /* Don't look any further, there's stuff in the buffer */
73 /* Now try to get a character */
76 /* select() will consider the descriptor set in the range of 0 to (maxfd-1) */
78 tv.tv_sec = msecs / 1000;
79 tv.tv_usec = msecs % 1000;
80 while ((ready = select(maxfd, &rfd, NULL, NULL, &tv)) < 0 ) {
81 if (errno != EINTR ) {
89 /* 1 => Input ready, 0 => time expired (-1 error) */