[project @ 1996-01-08 20:28:12 by partain]
[ghc-hetmet.git] / ghc / runtime / io / fileSize.lc
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1994
3 %
4 \subsection[fileSize.lc]{hfileSize 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_SYS_STAT_H
16 #include <sys/stat.h>
17 #endif
18   
19 StgInt
20 fileSize(fp, result)
21 StgAddr fp;
22 StgByteArray result;
23 {
24     struct stat sb;
25
26     while (fstat(fileno((FILE *) fp), &sb) < 0) {
27         /* highly unlikely */
28         if (errno != EINTR) {
29             cvtErrno();
30             stdErrno();
31             return -1;
32         }
33     }
34     if (S_ISREG(sb.st_mode)) {
35         /* result will be word aligned */
36         *(off_t *) result = sb.st_size;
37         return 0;
38     } else {
39         ghc_errtype = ERR_INAPPROPRIATETYPE;
40         ghc_errstr = "not a regular file";
41         return -1;
42     }
43 }
44
45 \end{code}