[project @ 1996-07-25 20:43:49 by partain]
[ghc-hetmet.git] / ghc / lib / cbits / readFile.lc
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1994
3 %
4 \subsection[readFile.lc]{hGetContents Runtime Support}
5
6 \begin{code}
7
8 #include "rtsdefs.h"
9 #include "stgio.h"
10
11 #define EOT 4
12
13 StgInt
14 readBlock(buf, fp, size)
15 StgAddr buf;
16 StgAddr fp;
17 StgInt size;
18 {
19     int count;
20
21     if (feof((FILE *) fp)) {
22         ghc_errtype = ERR_EOF;
23         ghc_errstr = "";
24         return -1;
25     }
26
27     while ((count = fread(buf, 1, size, (FILE *) fp)) == 0) {
28         if (feof((FILE *) fp)) {
29             ghc_errtype = ERR_EOF;
30             ghc_errstr = "";
31             return -1;
32         } else if (errno != EINTR) {
33             cvtErrno();
34             stdErrno();
35             return -1;
36         }
37         clearerr((FILE *) fp);
38     }
39
40     return count;
41 }
42
43 StgInt
44 readLine(buf, fp, size)
45 StgAddr buf;
46 StgAddr fp;
47 StgInt size;
48 {
49     if (feof((FILE *) fp)) {
50         ghc_errtype = ERR_EOF;
51         ghc_errstr = "";
52         return -1;
53     }
54
55     while (fgets(buf, size, (FILE *) fp) == NULL) {
56         if (feof((FILE *) fp)) {
57             ghc_errtype = ERR_EOF;
58             ghc_errstr = "";
59             return -1;
60         } else if (errno != EINTR) {
61             cvtErrno();
62             stdErrno();
63             return -1;
64         }
65         clearerr((FILE *) fp);
66     }
67
68     return strlen(buf);
69 }
70
71 StgInt
72 readChar(fp)
73 StgAddr fp;
74 {
75     int c;
76
77     if (feof((FILE *) fp)) {
78         ghc_errtype = ERR_EOF;
79         ghc_errstr = "";
80         return -1;
81     }
82
83     while ((c = getc((FILE *) fp)) == EOF) {
84         if (feof((FILE *) fp)) {
85             ghc_errtype = ERR_EOF;
86             ghc_errstr = "";
87             return -1;
88         } else if (errno != EINTR) {
89             cvtErrno();
90             stdErrno();
91             return -1;
92         }
93         clearerr((FILE *) fp);
94     }
95
96     if (isatty(fileno((FILE *) fp)) && c == EOT) 
97         return EOF;
98     else
99         return c;
100 }
101
102 \end{code}