[project @ 1996-01-08 20:28:12 by partain]
[ghc-hetmet.git] / ghc / runtime / io / inputReady.lc
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1994
3 %
4 \subsection[inputReady.lc]{hReady Runtime Support}
5
6 \begin{code}
7
8 #include "rtsdefs.h"
9 #include "stgio.h"
10
11 #ifdef HAVE_SYS_TYPES_H
12 #include <sys/types.h>
13 #endif
14
15 #ifdef HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18
19 #ifdef HAVE_FCNTL_H
20 #include <fcntl.h>
21 #endif
22
23 StgInt
24 inputReady(fp)
25 StgAddr fp;
26 {
27     int flags;
28     int c;
29
30     if (feof((FILE *) fp))
31         return 0;
32
33     /* Get the original file status flags */
34     while ((flags = fcntl(fileno((FILE *) fp), F_GETFL)) < 0) {
35         /* highly unlikely */
36         if (errno != EINTR) {
37             cvtErrno();
38             stdErrno();
39             return -1;
40         }
41     }
42
43     /* If it's not already non-blocking, make it so */
44     if (!(flags & O_NONBLOCK)) {
45         while (fcntl(fileno((FILE *) fp), F_SETFL, flags | O_NONBLOCK) < 0) {
46             /* still highly unlikely */
47             if (errno != EINTR) {
48                 cvtErrno();
49                 stdErrno();
50                 return -1;
51             }
52         }
53     }
54     /* Now try to get a character */
55     while ((c = getc((FILE *) fp)) == EOF && errno == EINTR)
56         clearerr((FILE *) fp);
57
58     /* If we made it non-blocking for this, switch it back */
59     if (!(flags & O_NONBLOCK)) {
60         while (fcntl(fileno((FILE *) fp), F_SETFL, flags) < 0) {
61             /* still highly unlikely */
62             if (errno != EINTR) {
63                 cvtErrno();
64                 stdErrno();
65                 return -1;
66             }
67         }
68     }
69
70     if (c == EOF) {
71         if (errno == EAGAIN || feof((FILE *) fp)) {
72             clearerr((FILE *) fp);
73             return 0;
74         } else {
75             cvtErrno();
76             stdErrno();
77             return -1;
78         }
79     } else if (ungetc(c, (FILE *) fp) == EOF) {
80         cvtErrno();
81         stdErrno();
82         return -1;
83     } else
84         return 1;
85 }
86
87 \end{code}