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
43 int flags, c, fd, maxfd, ready;
47 if (feof((FILE *) fp))
50 fd = fileno((FILE *)fp);
52 /* Get the original file status flags */
53 while ((flags = fcntl(fd, F_GETFL)) < 0) {
62 /* If it's not already non-blocking, make it so */
63 if (!(flags & O_NONBLOCK)) {
64 while (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
65 /* still highly unlikely */
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 while ((c = getc((FILE *) fp)) == EOF && errno == EINTR)
90 clearerr((FILE *) fp);
93 /* If we made it non-blocking for this, switch it back */
94 if (!(flags & O_NONBLOCK)) {
95 while (fcntl(fd, F_SETFL, flags) < 0) {
96 /* still highly unlikely */
104 /* 1 => Input ready, 0 => time expired (-1 error) */
109 if (errno == EAGAIN || feof((FILE *) fp)) {
110 clearerr((FILE *) fp);
117 } else if (ungetc(c, (FILE *) fp) == EOF) {