[project @ 2001-04-02 16:10:32 by rrt]
[ghc-hetmet.git] / ghc / lib / std / cbits / fileSize.c
index 3720251..02ad1d4 100644 (file)
@@ -1,7 +1,7 @@
 /* 
  * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
  *
- * $Id: fileSize.c,v 1.1 1998/04/10 10:54:27 simonm Exp $
+ * $Id: fileSize.c,v 1.7 2001/04/02 16:10:32 rrt Exp $
  *
  * hClose Runtime Support
  */
 #endif
   
 StgInt
-fileSize(StgAddr fp, StgByteArray result)
+fileSize(StgForeignPtr ptr, StgByteArray result)
 {
+    IOFileObject* fo = (IOFileObject*)ptr;
     struct stat sb;
+    int rc = 0;
 
-    while (fstat(fileno((FILE *) fp), &sb) < 0) {
+    /* Flush buffer in order to get as an accurate size as poss. */
+    rc = flushFile(ptr);
+    if (rc < 0) return rc;
+
+   while (fstat(fo->fd, &sb) < 0) {
        /* highly unlikely */
        if (errno != EINTR) {
            cvtErrno();
@@ -32,7 +38,11 @@ fileSize(StgAddr fp, StgByteArray result)
     }
     if (S_ISREG(sb.st_mode)) {
        /* result will be word aligned */
+#if defined( macosx_TARGET_OS )
+       *(W_ *) result = (W_)sb.st_size;
+#else
        *(off_t *) result = sb.st_size;
+#endif
        return 0;
     } else {
        ghc_errtype = ERR_INAPPROPRIATETYPE;
@@ -40,3 +50,34 @@ fileSize(StgAddr fp, StgByteArray result)
        return -1;
     }
 }
+
+StgInt
+fileSize_int64(StgForeignPtr ptr, StgByteArray result)
+{
+    IOFileObject* fo = (IOFileObject*)ptr;
+    struct stat sb;
+    int rc = 0;
+
+    /* Flush buffer in order to get as an accurate size as poss. */
+    rc = flushFile(ptr);
+    if (rc < 0) return rc;
+
+   while (fstat(fo->fd, &sb) < 0) {
+       /* highly unlikely */
+       if (errno != EINTR) {
+           cvtErrno();
+           stdErrno();
+           return -1;
+       }
+    }
+    if (S_ISREG(sb.st_mode)) {
+       /* result will be word aligned */
+       *(StgInt64*) result = (StgInt64)sb.st_size;
+       return 0;
+    } else {
+       ghc_errtype = ERR_INAPPROPRIATETYPE;
+       ghc_errstr = "not a regular file";
+       return -1;
+    }
+}
+