[project @ 1999-09-16 13:14:38 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / cbits / openFile.c
index ae4d287..5f24491 100644 (file)
@@ -1,11 +1,14 @@
 /* 
  * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
  *
- * $Id: openFile.c,v 1.3 1998/12/02 13:27:44 simonm Exp $
+ * $Id: openFile.c,v 1.7 1999/09/16 13:14:43 simonmar Exp $
  *
  * openFile Runtime Support
  */
 
+/* We use lstat, which is sadly not POSIX */
+#define NON_POSIX_SOURCE
+
 #include "Rts.h"
 #include "stgio.h"
 #include "fileObject.h"
 #include <fcntl.h>
 #endif
 
+#ifdef mingw32_TARGET_OS
+#define O_NOCTTY 0
+#endif
+
 IOFileObject*
 openStdFile(fd,flags,rd)
 StgInt fd;
@@ -33,6 +40,7 @@ StgInt flags;
 StgInt rd;
 {
     IOFileObject* fo;
+    long fd_flags;
 
     if ((fo = malloc(sizeof(IOFileObject))) == NULL)
        return NULL;
@@ -42,7 +50,12 @@ StgInt rd;
     fo->bufRPtr  = 0;
     fo->flags   = flags | FILEOBJ_STD | ( rd ? FILEOBJ_READ : FILEOBJ_WRITE);
     fo->connectedTo = NULL;
-    return fo;
+    /* set the non-blocking flag on this file descriptor */
+    fd_flags = fcntl(fd, F_GETFL);
+    fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
+
+   return fo;
 }
 
 #define OPENFILE_APPEND 0
@@ -60,16 +73,11 @@ StgInt flags;
     FILE *fp;
     int fd;
     int oflags;
-    int exclusive;
+    int for_writing;
     int created = 0;
     struct stat sb;
     IOFileObject* fo;
 
-#ifdef __CONCURRENT_HASKELL__
-#warning FixMe: Ignoring bogus bit 7 in openFiles 2nd argument
-    how = how & 0x7f;
-#endif
-
     /*
      * Since we aren't supposed to succeed when we're opening for writing and
      * there's another writer, we can't just do an open() with O_WRONLY.
@@ -77,20 +85,20 @@ StgInt flags;
 
     switch (how) {
       case OPENFILE_APPEND:
-        oflags = O_WRONLY | O_NOCTTY | O_APPEND; 
-       exclusive = 1;
+        oflags = O_NONBLOCK | O_WRONLY | O_NOCTTY | O_APPEND; 
+       for_writing = 1;
        break;
       case OPENFILE_WRITE:
-       oflags = O_WRONLY | O_NOCTTY;
-       exclusive = 1;
+       oflags = O_NONBLOCK | O_WRONLY | O_NOCTTY;
+       for_writing = 1;
        break;
     case OPENFILE_READ_ONLY:
-        oflags = O_RDONLY | O_NOCTTY;
-       exclusive = 0;
+        oflags = O_NONBLOCK | O_RDONLY | O_NOCTTY;
+       for_writing = 0;
        break;
     case OPENFILE_READ_WRITE:
-       oflags = O_RDWR | O_NOCTTY;
-       exclusive = 0;
+       oflags = O_NONBLOCK | O_RDWR | O_NOCTTY;
+       for_writing = 1;
        break;
     default:
        fprintf(stderr, "openFile: unknown mode `%d'\n", how);
@@ -112,12 +120,14 @@ StgInt flags;
                return NULL;
            } else {
                /* If it is a dangling symlink, break off now, too. */
+#ifndef mingw32_TARGET_OS
                struct stat st;
                if ( lstat(file,&st) == 0) {
                   ghc_errtype = ERR_NOSUCHTHING;
                   ghc_errstr = "dangling symlink";
                   return NULL;
                }
+#endif
             }
            /* Now try to create it */
            while ((fd = open(file, oflags | O_CREAT | O_EXCL, 0666)) < 0) {
@@ -188,7 +198,7 @@ StgInt flags;
     }
     /* Use our own personal locking */
 
-    if (lockFile(fd, exclusive) < 0) {
+    if (lockFile(fd, for_writing, 1/*enforce single-writer, if needs be.*/) < 0) {
        cvtErrno();
        switch (ghc_errno) {
        default:
@@ -270,11 +280,13 @@ StgInt fd;
 StgInt oflags;
 StgInt flags;
 {
-    int exclusive;
+    int for_writing;
     FILE* fp;
     IOFileObject* fo;
 
-    if (lockFile(fd, exclusive) < 0) {
+    for_writing = ( ((oflags & O_WRONLY) || (oflags & O_RDWR)) ? 1 : 0);
+
+    if (lockFile(fd, for_writing, 1/* enforce single-writer */ ) < 0) {
        cvtErrno();
        switch (ghc_errno) {
        default: