[project @ 2000-05-11 13:15:38 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / cbits / openFile.c
index 90ae2d8..57b0c2f 100644 (file)
@@ -1,7 +1,7 @@
 /* 
  * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
  *
- * $Id: openFile.c,v 1.12 1999/11/26 16:25:56 simonmar Exp $
+ * $Id: openFile.c,v 1.17 2000/05/11 13:15:38 simonmar Exp $
  *
  * openFile Runtime Support
  */
@@ -33,9 +33,7 @@
 #endif
 
 IOFileObject*
-openStdFile(fd,rd)
-StgInt fd;
-StgInt rd;
+openStdFile(StgInt fd, StgInt rd)
 {
     IOFileObject* fo;
     long fd_flags;
@@ -49,14 +47,21 @@ StgInt rd;
     fo->flags    = FILEOBJ_STD | ( rd ? FILEOBJ_READ : FILEOBJ_WRITE);
     fo->connectedTo = NULL;
  
-    /* 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 !defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__)
-    /* set the non-blocking flag on this file descriptor */
-    fd_flags = fcntl(fd, F_GETFL);
-    fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
+    /* 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;
@@ -68,12 +73,8 @@ StgInt rd;
 #define OPENFILE_READ_WRITE 3
 
 IOFileObject*
-openFile(file, how, binary)
-StgByteArray file;
-StgInt how;
-StgInt binary;
+openFile(StgByteArray file, StgInt how, StgInt binary)
 {
-    FILE *fp;
     int fd;
     int oflags;
     int for_writing;
@@ -93,22 +94,22 @@ StgInt binary;
 
     switch (how) {
       case OPENFILE_APPEND:
-        oflags = O_NONBLOCK | O_WRONLY | O_NOCTTY | O_APPEND; 
+        oflags = O_WRONLY | O_NOCTTY | O_APPEND; 
        for_writing = 1;
        flags |= FILEOBJ_WRITE;
        break;
       case OPENFILE_WRITE:
-       oflags = O_NONBLOCK | O_WRONLY | O_NOCTTY;
+       oflags = O_WRONLY | O_NOCTTY;
        flags |= FILEOBJ_WRITE;
        for_writing = 1;
        break;
     case OPENFILE_READ_ONLY:
-        oflags = O_NONBLOCK | O_RDONLY | O_NOCTTY;
+        oflags = O_RDONLY | O_NOCTTY;
        flags |= FILEOBJ_READ;
        for_writing = 0;
        break;
     case OPENFILE_READ_WRITE:
-       oflags = O_NONBLOCK | O_RDWR | O_NOCTTY;
+       oflags = O_RDWR | O_NOCTTY;
        flags |= FILEOBJ_READ | FILEOBJ_WRITE;
        for_writing = 1;
        break;
@@ -190,8 +191,17 @@ StgInt binary;
        }
     }
 
-    /* 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) {
@@ -279,7 +289,6 @@ StgInt binary;
 
     fo->fd       = fd;
     fo->buf      = NULL;
-    fo->bufStart = 0;
     fo->bufWPtr  = 0;
     fo->bufRPtr  = 0;
     fo->flags    = flags;
@@ -289,10 +298,9 @@ StgInt binary;
 
 /* `Lock' file descriptor and return file object. */
 IOFileObject*
-openFd(StgInt fd,StgInt oflags,StgInt flags)
+openFd(StgInt fd, StgInt oflags, StgInt flags)
 {
     int for_writing;
-    FILE* fp;
     IOFileObject* fo;
 
     for_writing = ( ((oflags & O_WRONLY) || (oflags & O_RDWR)) ? 1 : 0);
@@ -317,7 +325,6 @@ openFd(StgInt fd,StgInt oflags,StgInt flags)
        return NULL;
     fo->fd       = fd;
     fo->buf      = NULL;
-    fo->bufStart = 0;
     fo->bufWPtr  = 0;
     fo->bufRPtr  = 0;
     fo->flags    = flags | ( oflags & O_RDONLY ? FILEOBJ_READ