[project @ 1998-04-10 10:49:39 by simonm]
[ghc-hetmet.git] / ghc / lib / std / cbits / fileSize.c
1 /* 
2  * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
3  *
4  * $Id: fileSize.c,v 1.1 1998/04/10 10:54:27 simonm Exp $
5  *
6  * hClose Runtime Support
7  */
8
9 #include "Rts.h"
10 #include "stgio.h"
11
12 #ifdef HAVE_SYS_TYPES_H
13 #include <sys/types.h>
14 #endif
15
16 #ifdef HAVE_SYS_STAT_H
17 #include <sys/stat.h>
18 #endif
19   
20 StgInt
21 fileSize(StgAddr fp, StgByteArray result)
22 {
23     struct stat sb;
24
25     while (fstat(fileno((FILE *) fp), &sb) < 0) {
26         /* highly unlikely */
27         if (errno != EINTR) {
28             cvtErrno();
29             stdErrno();
30             return -1;
31         }
32     }
33     if (S_ISREG(sb.st_mode)) {
34         /* result will be word aligned */
35         *(off_t *) result = sb.st_size;
36         return 0;
37     } else {
38         ghc_errtype = ERR_INAPPROPRIATETYPE;
39         ghc_errstr = "not a regular file";
40         return -1;
41     }
42 }