[project @ 2000-05-11 13:15:38 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / cbits / openFile.c
index ae4d287..57b0c2f 100644 (file)
@@ -1,14 +1,16 @@
 /* 
  * (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.17 2000/05/11 13:15:38 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"
 
 #ifdef HAVE_SYS_TYPES_H
 #include <sys/types.h>
 #include <fcntl.h>
 #endif
 
+#if defined(mingw32_TARGET_OS) && !defined(O_NOCTTY)
+#define O_NOCTTY 0
+#endif
+
 IOFileObject*
-openStdFile(fd,flags,rd)
-StgInt fd;
-StgInt flags;
-StgInt rd;
+openStdFile(StgInt fd, StgInt rd)
 {
     IOFileObject* fo;
+    long fd_flags;
 
     if ((fo = malloc(sizeof(IOFileObject))) == NULL)
        return NULL;
@@ -40,9 +44,27 @@ StgInt rd;
     fo->buf      = NULL;
     fo->bufWPtr  = 0;
     fo->bufRPtr  = 0;
-    fo->flags   = flags | FILEOBJ_STD | ( rd ? FILEOBJ_READ : FILEOBJ_WRITE);
+    fo->flags    = FILEOBJ_STD | ( rd ? FILEOBJ_READ : FILEOBJ_WRITE);
     fo->connectedTo = NULL;
-    return fo;
+#if !defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__)
+    /* Set the non-blocking flag on this file descriptor.
+     *
+     * Don't do it for stdout and stderr: some shells (actually most)
+     * don't reset the nonblocking flag after running a program, and
+     * this causes all sorts of problems.  --SDM (12/99)
+     *
+     * MS Win32 CRT doesn't support fcntl() -- the workaround is to
+     * start using 'completion ports', but I'm punting on implementing
+     * support for using those.
+     */
+    if (fd != 1 && fd != 2) {
+      fd_flags = fcntl(fd, F_GETFL);
+      fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
+    }
+#endif
+
+   return fo;
 }
 
 #define OPENFILE_APPEND 0
@@ -51,23 +73,18 @@ StgInt rd;
 #define OPENFILE_READ_WRITE 3
 
 IOFileObject*
-openFile(file, how, binary, flags)
-StgByteArray file;
-StgInt how;
-StgInt binary;
-StgInt flags;
+openFile(StgByteArray file, StgInt how, StgInt binary)
 {
-    FILE *fp;
     int fd;
     int oflags;
-    int exclusive;
+    int for_writing;
     int created = 0;
     struct stat sb;
     IOFileObject* fo;
+    int flags = 0;
 
-#ifdef __CONCURRENT_HASKELL__
-#warning FixMe: Ignoring bogus bit 7 in openFiles 2nd argument
-    how = how & 0x7f;
+#if defined(_WIN32) && !(defined(__CYGWIN__) || defined(__CYGWIN32__))
+#define O_NONBLOCK 0
 #endif
 
     /*
@@ -78,19 +95,23 @@ StgInt flags;
     switch (how) {
       case OPENFILE_APPEND:
         oflags = O_WRONLY | O_NOCTTY | O_APPEND; 
-       exclusive = 1;
+       for_writing = 1;
+       flags |= FILEOBJ_WRITE;
        break;
       case OPENFILE_WRITE:
        oflags = O_WRONLY | O_NOCTTY;
-       exclusive = 1;
+       flags |= FILEOBJ_WRITE;
+       for_writing = 1;
        break;
     case OPENFILE_READ_ONLY:
         oflags = O_RDONLY | O_NOCTTY;
-       exclusive = 0;
+       flags |= FILEOBJ_READ;
+       for_writing = 0;
        break;
     case OPENFILE_READ_WRITE:
        oflags = O_RDWR | O_NOCTTY;
-       exclusive = 0;
+       flags |= FILEOBJ_READ | FILEOBJ_WRITE;
+       for_writing = 1;
        break;
     default:
        fprintf(stderr, "openFile: unknown mode `%d'\n", how);
@@ -98,8 +119,10 @@ StgInt flags;
     }
 
 #if HAVE_O_BINARY
-    if (binary) 
+    if (binary) {
       oflags |= O_BINARY;
+      flags  |= FILEOBJ_BINARY;
+    }
 #endif
 
     /* First try to open without creating */
@@ -112,12 +135,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) {
@@ -166,8 +191,17 @@ StgInt flags;
        }
     }
 
-    /* Make sure that we aren't looking at a directory */
+    /* Set non-blocking mode *after* the open.  The reason is that
+     * when reading from a FIFO, if we open in non-blocking mode
+     * then any reads from the FIFO will return EOF straight away
+     * without waiting for a writing process.  If we set O_NONBLOCK
+     * after doing the open, then we apparently get to wait for a
+     * writer.  This broken behaviour has been observed on both Linux
+     * & Solaris.   --SDM
+     */
+    fcntl(fd, F_SETFL, oflags | O_NONBLOCK);
 
+    /* Make sure that we aren't looking at a directory */
     while (fstat(fd, &sb) < 0) {
        /* highly unlikely */
        if (errno != EINTR) {
@@ -188,7 +222,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:
@@ -257,24 +291,21 @@ StgInt flags;
     fo->buf      = NULL;
     fo->bufWPtr  = 0;
     fo->bufRPtr  = 0;
-    fo->flags   = flags | ( (how == OPENFILE_READ_ONLY || how == OPENFILE_READ_WRITE) ? FILEOBJ_READ  : 0)
-                       | ( (how == OPENFILE_APPEND    || how == OPENFILE_READ_WRITE) ? FILEOBJ_WRITE : 0);
+    fo->flags    = flags;
     fo->connectedTo = NULL;
     return fo;
 }
 
 /* `Lock' file descriptor and return file object. */
 IOFileObject*
-openFd(fd,oflags,flags)
-StgInt fd;
-StgInt oflags;
-StgInt flags;
+openFd(StgInt fd, StgInt oflags, StgInt flags)
 {
-    int exclusive;
-    FILE* fp;
+    int for_writing;
     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:
@@ -292,11 +323,11 @@ StgInt flags;
     /* See openFileObject() comment */
     if ((fo = malloc(sizeof(IOFileObject))) == NULL)
        return NULL;
-    fo->fd      = fd;
-    fo->buf     = NULL;
-    fo->bufWPtr = 0;
-    fo->bufRPtr = 0;
-    fo->flags   = flags | ( oflags & O_RDONLY ? FILEOBJ_READ 
+    fo->fd       = fd;
+    fo->buf      = NULL;
+    fo->bufWPtr  = 0;
+    fo->bufRPtr  = 0;
+    fo->flags    = flags | ( oflags & O_RDONLY ? FILEOBJ_READ 
                          : oflags & O_RDWR   ? FILEOBJ_READ 
                          : 0)
                        | ( oflags & O_WRONLY ? FILEOBJ_WRITE