[project @ 1999-09-16 13:14:38 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / cbits / writeFile.c
index 194c1dd..0b459f3 100644 (file)
@@ -1,7 +1,7 @@
 /* 
  * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
  *
- * $Id: writeFile.c,v 1.5 1999/07/03 18:45:04 sof Exp $
+ * $Id: writeFile.c,v 1.8 1999/09/16 13:14:43 simonmar Exp $
  *
  * hPutStr Runtime Support
  */
@@ -9,7 +9,7 @@
 #include "Rts.h"
 #include "stgio.h"
 
-#if defined(HAVE_WINSOCK_H) && !defined(__CYGWIN__)
+#if defined(HAVE_WINSOCK_H) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
 #define USE_WINSOCK
 #endif
 
@@ -47,23 +47,20 @@ StgInt bytes;
     int count, rc=0;
     IOFileObject* fo = (IOFileObject*)ptr;
 
-    char *p = (char *) fo->buf;
+    char *pBuf = (char *) fo->buf;
 
     /* Disallow short writes */
     if (bytes == 0  || fo->buf == NULL)
        return 0;
 
-    if ( fo->flags & FILEOBJ_NONBLOCKING_IO && inputReady(ptr,0) != 1 )
-       return FILEOBJ_BLOCKED_WRITE;
-
     while ((count = 
               (
 #ifdef USE_WINSOCK
                 fo->flags & FILEOBJ_WINSOCK ?
-                send(fo->fd, fo->buf, bytes, 0) :
-                write(fo->fd, fo->buf, bytes))) < bytes) {
+                send(fo->fd,  pBuf, bytes, 0) :
+                write(fo->fd, pBuf, bytes))) < bytes) {
 #else
-                write(fo->fd, fo->buf, bytes))) < bytes) {
+                write(fo->fd, pBuf, bytes))) < bytes) {
 #endif
        if (errno != EINTR) {
            cvtErrno();
@@ -71,7 +68,7 @@ StgInt bytes;
            return -1;
        }
        bytes -= count;
-       p += count;
+       pBuf  += count;
     }
     /* Signal that we've emptied the buffer */
     fo->bufWPtr=0;
@@ -88,7 +85,7 @@ StgInt  len;
     IOFileObject* fo = (IOFileObject*)ptr;
     int count;
     int rc = 0;
-    char *p = (char *) buf;
+    char *pBuf = (char *) buf;
 
     if (len == 0 )
        return 0;
@@ -121,26 +118,27 @@ StgInt  len;
        return rc;
     }
 
-    if ( fo->flags & FILEOBJ_NONBLOCKING_IO && inputReady(ptr,0) != 1 )
-       return FILEOBJ_BLOCKED_WRITE;
-
-    /* Disallow short writes */
     while ((count = 
                (
 #ifdef USE_WINSOCK
                 fo->flags & FILEOBJ_WINSOCK ?
-                send(fo->fd,  (char*)buf, (int)len, 0) :
-                write(fo->fd, (char*)buf, (int)len))) < len ) {
+                send(fo->fd,  pBuf, (int)len, 0) :
+                write(fo->fd, pBuf, (int)len))) < len ) {
 #else
-                write(fo->fd, (char*)buf, (int)len))) < len ) {
+                write(fo->fd, pBuf, (int)len))) < len ) {
 #endif
-       if (errno != EINTR) {
+        if ( count >= 0 ) {
+            len -= count;
+           pBuf += count;
+           continue;
+       } else if ( errno == EAGAIN ) {
+           errno = 0;
+           return FILEOBJ_BLOCKED_WRITE;
+       } else if ( errno != EINTR ) {
            cvtErrno();
            stdErrno();
            return -1;
        }
-       len -= count;
-       p += count;
     }
 
     return 0;