2 % (c) The GRASP/AQUA Project, Glasgow University, 1994
4 \subsection[inputReady.lc]{hReady Runtime Support}
8 /* select and supporting types is not */
10 #define NON_POSIX_SOURCE
16 #ifdef HAVE_SYS_TYPES_H
17 #include <sys/types.h>
21 /* this is included from sys/types.h only if _BSD is defined. */
22 /* Since it is not, I include it here. - andre */
23 #include <sys/select.h>
34 #ifdef HAVE_SYS_TIME_H
39 * inputReady(ptr, msecs) checks to see whether input is available
40 * on the file object 'ptr', timing out after (approx.) 'msec' milliseconds.
41 * Input meaning 'can I safely read at least a *character* from this file
42 * object without blocking?'
44 * If the file object has a non-empty buffer, the test is trivial. If not,
45 * we select() on the (readable) file descriptor.
47 * Notice that for file descriptors connected to ttys in non-canonical mode
48 * (i.e., it's buffered), inputReady will not return true until a *complete
53 inputReady(ptr, msecs)
57 IOFileObject* fo = (IOFileObject*)ptr;
58 int c, fd, maxfd, ready;
62 if ( FILEOBJ_IS_EOF(fo) )
65 if ( !FILEOBJ_BUFFER_EMPTY(fo) ) {
66 /* Don't look any further, there's stuff in the buffer */
72 /* Now try to get a character */
75 /* select() will consider the descriptor set in the range of 0 to (maxfd-1) */
77 tv.tv_sec = msecs / 1000;
78 tv.tv_usec = msecs % 1000;
79 while ((ready = select(maxfd, &rfd, NULL, NULL, &tv)) < 0 ) {
80 if (errno != EINTR ) {
88 /* 1 => Input ready, 0 => time expired (-1 error) */