2 % (c) The GRASP/AQUA Project, Glasgow University, 1994
4 \subsection[inputReady.lc]{hReady Runtime Support}
8 /* select and supporting types is not */
9 #define NON_POSIX_SOURCE
13 #ifdef HAVE_SYS_TYPES_H
14 #include <sys/types.h>
18 /* this is included from sys/types.h only if _BSD is defined. */
19 /* Since it is not, I include it here. - andre */
20 #include <sys/select.h>
31 #ifdef HAVE_SYS_TIME_H
40 int flags, c, fd, maxfd, ready;
44 if (feof((FILE *) fp))
47 fd = fileno((FILE *)fp);
49 /* Get the original file status flags */
50 while ((flags = fcntl(fd, F_GETFL)) < 0) {
59 /* If it's not already non-blocking, make it so */
60 if (!(flags & O_NONBLOCK)) {
61 while (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
62 /* still highly unlikely */
70 /* Now try to get a character */
73 /* select() will consider the descriptor set in the range of 0 to (maxfd-1) */
77 while ((ready = select(maxfd, &rfd, NULL, NULL, &tv)) < 0 ) {
78 if (errno != EINTR ) {
86 while ((c = getc((FILE *) fp)) == EOF && errno == EINTR)
87 clearerr((FILE *) fp);
90 /* If we made it non-blocking for this, switch it back */
91 if (!(flags & O_NONBLOCK)) {
92 while (fcntl(fd, F_SETFL, flags) < 0) {
93 /* still highly unlikely */
101 /* 1 => Input ready, 0 => time expired (-1 error) */
106 if (errno == EAGAIN || feof((FILE *) fp)) {
107 clearerr((FILE *) fp);
114 } else if (ungetc(c, (FILE *) fp) == EOF) {