2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: inputReady.c,v 1.1 1998/04/10 10:54:40 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(StgAddr fp, StgInt nsecs)
42 int flags, c, fd, maxfd, ready;
46 if (feof((FILE *) fp))
49 fd = fileno((FILE *)fp);
51 /* Get the original file status flags */
52 while ((flags = fcntl(fd, F_GETFL)) < 0) {
61 /* If it's not already non-blocking, make it so */
62 if (!(flags & O_NONBLOCK)) {
63 while (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
64 /* still highly unlikely */
72 /* Now try to get a character */
75 /* select() will consider the descriptor set in the range of 0 to (maxfd-1) */
79 while ((ready = select(maxfd, &rfd, NULL, NULL, &tv)) < 0 ) {
80 if (errno != EINTR ) {
88 while ((c = getc((FILE *) fp)) == EOF && errno == EINTR)
89 clearerr((FILE *) fp);
92 /* If we made it non-blocking for this, switch it back */
93 if (!(flags & O_NONBLOCK)) {
94 while (fcntl(fd, F_SETFL, flags) < 0) {
95 /* still highly unlikely */
103 /* 1 => Input ready, 0 => time expired (-1 error) */
108 if (errno == EAGAIN || feof((FILE *) fp)) {
109 clearerr((FILE *) fp);
116 } else if (ungetc(c, (FILE *) fp) == EOF) {