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>
25 #ifdef HAVE_SYS_TIME_H
34 int flags, c, fd, maxfd, ready;
38 if (feof((FILE *) fp))
41 fd = fileno((FILE *)fp);
43 /* Get the original file status flags */
44 while ((flags = fcntl(fd, F_GETFL)) < 0) {
53 /* If it's not already non-blocking, make it so */
54 if (!(flags & O_NONBLOCK)) {
55 while (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
56 /* still highly unlikely */
64 /* Now try to get a character */
67 /* select() will consider the descriptor set in the range of 0 to (maxfd-1) */
71 while ((ready = select(maxfd, &rfd, NULL, NULL, &tv)) < 0 ) {
72 if (errno != EINTR ) {
80 while ((c = getc((FILE *) fp)) == EOF && errno == EINTR)
81 clearerr((FILE *) fp);
84 /* If we made it non-blocking for this, switch it back */
85 if (!(flags & O_NONBLOCK)) {
86 while (fcntl(fd, F_SETFL, flags) < 0) {
87 /* still highly unlikely */
95 /* 1 => Input ready, 0 => time expired (-1 error) */
100 if (errno == EAGAIN || feof((FILE *) fp)) {
101 clearerr((FILE *) fp);
108 } else if (ungetc(c, (FILE *) fp) == EOF) {