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