[project @ 1998-11-26 09:17:22 by sof]
[ghc-hetmet.git] / ghc / lib / std / cbits / 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(ptr, result)
21 StgForeignObj ptr;
22 StgByteArray result;
23 {
24     IOFileObject* fo = (IOFileObject*)ptr;
25     struct stat sb;
26     int rc = 0;
27
28     /* Flush buffer in order to get as an accurate size as poss. */
29     rc = flushFile(ptr);
30     if (rc < 0) return rc;
31
32    while (fstat(fo->fd, &sb) < 0) {
33         /* highly unlikely */
34         if (errno != EINTR) {
35             cvtErrno();
36             stdErrno();
37             return -1;
38         }
39     }
40     if (S_ISREG(sb.st_mode)) {
41         /* result will be word aligned */
42         *(off_t *) result = sb.st_size;
43         return 0;
44     } else {
45         ghc_errtype = ERR_INAPPROPRIATETYPE;
46         ghc_errstr = "not a regular file";
47         return -1;
48     }
49 }
50
51 \end{code}