ExternalStream.st
changeset 7801 d930c353588d
parent 7737 616d849b7838
child 7804 7128efd3b504
--- a/ExternalStream.st	Mon Dec 08 14:31:22 2003 +0100
+++ b/ExternalStream.st	Tue Dec 09 11:46:45 2003 +0100
@@ -2273,10 +2273,15 @@
 
     "report an error, that some write error occured"
 
+    (lastErrorNumber == (OperatingSystem errorNumberFor:#EWOULDBLOCK)) ifTrue:[
+        self isNonBlocking ifTrue:[
+            ^ 0
+        ].
+    ].
     ^ WriteErrorSignal
-	raiseRequestWith:self
-	     errorString:('write error: ' , self lastErrorString)
-		      "/ in:thisContext sender
+        raiseRequestWith:self
+             errorString:('write error: ' , self lastErrorString)
+                      "/ in:thisContext sender
 
     "Modified: / 8.5.1999 / 20:12:09 / cg"
 ! !
@@ -3729,30 +3734,30 @@
 
     __INST(lastErrorNumber) = nil;
     if (((fp = __INST(filePointer)) != nil)
-	&& (__INST(mode) != @symbol(readonly))
-	&& __isSmallInteger(aByteValue)
+        && (__INST(mode) != @symbol(readonly))
+        && __isSmallInteger(aByteValue)
 
     ) {
-	c = __intVal(aByteValue);
-	f = __FILEVal(fp);
-	if (_buffered = (__INST(buffered) == true)) {
-	    __WRITING__(f)
-	} 
-	__WRITEBYTE__(cnt, f, &c, _buffered);
-	if (cnt == 1) {
-	    pos = __INST(position);
-	    if (pos != nil)
-		__INST(position) = __MKSMALLINT(__intVal(pos) + 1);
-	    RETURN (self);
-	}
-	if (cnt < 0) {
-	    __INST(position) = nil;
-	}
-	__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno);
+        c = __intVal(aByteValue);
+        f = __FILEVal(fp);
+        if (_buffered = (__INST(buffered) == true)) {
+            __WRITING__(f)
+        } 
+        __WRITEBYTE__(cnt, f, &c, _buffered);
+        if (cnt == 1) {
+            pos = __INST(position);
+            if (pos != nil)
+                __INST(position) = __MKSMALLINT(__intVal(pos) + 1);
+            RETURN (self);
+        }
+        if (cnt < 0) {
+            __INST(position) = nil;
+        }
+        __INST(lastErrorNumber) = __MKSMALLINT(__threadErrno);
     }
 %}.
-    filePointer isNil ifTrue:[^ self errorNotOpen.].
-    (mode == #readonly) ifTrue:[^ self errorReadOnly.].
+    filePointer isNil ifTrue:[self errorNotOpen. ^ self].
+    (mode == #readonly) ifTrue:[self errorReadOnly. ^ self].
     self writeError.
 !
 
@@ -3860,8 +3865,8 @@
 %{
     int num;
     union {
-	char bytes[4];
-	int intVal;
+        char bytes[4];
+        int intVal;
     } u;
     FILEPOINTER f;
     int cnt, _buffered;
@@ -3869,72 +3874,72 @@
 
     __INST(lastErrorNumber) = nil;
     if (__isSmallInteger(aNumber)) {
-	num = __intVal(aNumber);
+        num = __intVal(aNumber);
     } else {
 #ifdef alpha64
-	goto badArg;
+        goto badArg;
 #else
-	num = __longIntVal(aNumber);
-	if (num == 0) {
-	    num = __signedLongIntVal(aNumber);
-	    if (num == 0) {
-		/* bad arg or out-of-range integer  
-		 * (handled by the fallBack code)
-		 */
-		goto badArg;
-	    }
-	}
+        num = __longIntVal(aNumber);
+        if (num == 0) {
+            num = __signedLongIntVal(aNumber);
+            if (num == 0) {
+                /* bad arg or out-of-range integer  
+                 * (handled by the fallBack code)
+                 */
+                goto badArg;
+            }
+        }
 #endif
     }
 
     if (((fp = __INST(filePointer)) != nil)
      && (__INST(mode) != @symbol(readonly))
     ) {
-	if (msbFlag == true) {
+        if (msbFlag == true) {
 #if defined(__MSBFIRST)
-	    u.intVal = num;
+            u.intVal = num;
 #else
-	    u.bytes[0] = (num >> 24) & 0xFF;
-	    u.bytes[1] = (num >> 16) & 0xFF;
-	    u.bytes[2] = (num >> 8) & 0xFF;
-	    u.bytes[3] = num & 0xFF;
+            u.bytes[0] = (num >> 24) & 0xFF;
+            u.bytes[1] = (num >> 16) & 0xFF;
+            u.bytes[2] = (num >> 8) & 0xFF;
+            u.bytes[3] = num & 0xFF;
 #endif
-	} else {
+        } else {
 #if defined(__LSBFIRST)
-	    u.intVal = num;
+            u.intVal = num;
 #else
-	    u.bytes[3] = (num >> 24) & 0xFF;
-	    u.bytes[2] = (num >> 16) & 0xFF;
-	    u.bytes[1] = (num >> 8) & 0xFF;
-	    u.bytes[0] = num & 0xFF;
+            u.bytes[3] = (num >> 24) & 0xFF;
+            u.bytes[2] = (num >> 16) & 0xFF;
+            u.bytes[1] = (num >> 8) & 0xFF;
+            u.bytes[0] = num & 0xFF;
 #endif
-	}
-
-	f = __FILEVal(fp);
-	if (_buffered = (__INST(buffered) == true)) {
-	    __WRITING__(f)
-	} 
-	__WRITEBYTES__(cnt, f, u.bytes, 4, _buffered);
-
-	if (cnt == 4) {
-	    OBJ pos;
-
-	    if ((pos = __INST(position)) != nil) {
-		__INST(position) = __MKSMALLINT(__intVal(pos) + 4);
-	    }
-	    RETURN ( self );
-	}
-	__INST(position) = nil;
-	__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno);
+        }
+
+        f = __FILEVal(fp);
+        if (_buffered = (__INST(buffered) == true)) {
+            __WRITING__(f)
+        } 
+        __WRITEBYTES__(cnt, f, u.bytes, 4, _buffered);
+
+        if (cnt == 4) {
+            OBJ pos;
+
+            if ((pos = __INST(position)) != nil) {
+                __INST(position) = __MKSMALLINT(__intVal(pos) + 4);
+            }
+            RETURN ( self );
+        }
+        __INST(position) = nil;
+        __INST(lastErrorNumber) = __MKSMALLINT(__threadErrno);
     }
 badArg: ;
 %}.
-    filePointer isNil ifTrue:[^ self errorNotOpen.].
-    (mode == #readonly) ifTrue:[^ self errorReadOnly.].
-    lastErrorNumber notNil ifTrue:[^ self writeError.].
+    filePointer isNil ifTrue:[self errorNotOpen. ^ self].
+    (mode == #readonly) ifTrue:[self errorReadOnly. ^ self].
+    lastErrorNumber notNil ifTrue:[self writeError. ^ self].
 
     aNumber isInteger ifTrue:[
-	^ super nextPutLong:aNumber MSB:msbFlag
+        ^ super nextPutLong:aNumber MSB:msbFlag
     ].
     self argumentMustBeInteger
 !
@@ -3948,8 +3953,8 @@
 %{
     int num;
     union {
-	char bytes[2];
-	short shortVal;
+        char bytes[2];
+        short shortVal;
     } u;
 
     FILEPOINTER f;
@@ -3958,47 +3963,47 @@
 
     __INST(lastErrorNumber) = nil;
     if (((fp = __INST(filePointer)) != nil)
-	&& (__INST(mode) != @symbol(readonly))
-	&& __isSmallInteger(aNumber)
+        && (__INST(mode) != @symbol(readonly))
+        && __isSmallInteger(aNumber)
     ) {
-	num = __intVal(aNumber);
-	if (msbFlag == true) {
+        num = __intVal(aNumber);
+        if (msbFlag == true) {
 #if defined(__MSBFIRST)
-	    u.shortVal = num;
+            u.shortVal = num;
 #else
-	    u.bytes[0] = (num >> 8) & 0xFF;
-	    u.bytes[1] = num & 0xFF;
+            u.bytes[0] = (num >> 8) & 0xFF;
+            u.bytes[1] = num & 0xFF;
 #endif
-	} else {
+        } else {
 #if defined(__LSBFIRST)
-	    u.shortVal = num;
+            u.shortVal = num;
 #else
-	    u.bytes[1] = (num >> 8) & 0xFF;
-	    u.bytes[0] = num & 0xFF;
+            u.bytes[1] = (num >> 8) & 0xFF;
+            u.bytes[0] = num & 0xFF;
 #endif
-	}
-
-	f = __FILEVal(fp);
-	if (_buffered = (__INST(buffered) == true)) {
-	    __WRITING__(f)
-	} 
-	__WRITEBYTES__(cnt, f, u.bytes, 2, _buffered);
-
-	if (cnt == 2) {
-	    OBJ pos;
-
-	    if ((pos = __INST(position)) != nil) {
-		__INST(position) = __MKSMALLINT(__intVal(pos) + 2);
-	    }
-	    RETURN ( self );
-	}
-	__INST(position) = nil;
-	__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno);
+        }
+
+        f = __FILEVal(fp);
+        if (_buffered = (__INST(buffered) == true)) {
+            __WRITING__(f)
+        } 
+        __WRITEBYTES__(cnt, f, u.bytes, 2, _buffered);
+
+        if (cnt == 2) {
+            OBJ pos;
+
+            if ((pos = __INST(position)) != nil) {
+                __INST(position) = __MKSMALLINT(__intVal(pos) + 2);
+            }
+            RETURN ( self );
+        }
+        __INST(position) = nil;
+        __INST(lastErrorNumber) = __MKSMALLINT(__threadErrno);
     }
 %}.
-    lastErrorNumber notNil ifTrue:[^ self writeError.].
-    filePointer isNil ifTrue:[^ self errorNotOpen.].
-    (mode == #readonly) ifTrue:[^ self errorReadOnly.].
+    lastErrorNumber notNil ifTrue:[self writeError. ^ self.].
+    filePointer isNil ifTrue:[self errorNotOpen. ^ self].
+    (mode == #readonly) ifTrue:[self errorReadOnly. ^ self].
     self argumentMustBeInteger
 ! !
 
@@ -4139,6 +4144,32 @@
     ^ true
 !
 
+isNonBlocking
+    "return true, if O_NONBLOCK is set in the fileDescriptor (propably UNIX specific)"
+
+%{
+    OBJ fp;
+
+    if ((fp = __INST(filePointer)) != nil) {
+        FILEPOINTER f;
+
+        f = __FILEVal(fp);
+        if (f) {
+            int fd;
+            int flags;
+
+            fd = fileno(f);
+
+# if defined(O_NONBLOCK) && defined(F_GETFL)
+            flags = fcntl(fd, F_GETFL);
+            RETURN ( (flags & O_NONBLOCK) ? true : false );
+# endif
+        }
+    }
+%}.
+    self primitiveFailed
+!
+
 isOpen 
     "return true, if this stream is open"
 
@@ -5690,86 +5721,86 @@
 
     __INST(lastErrorNumber) = nil;
     if (((fp = __INST(filePointer)) != nil)
-	&& (__INST(mode) != @symbol(readonly))
-	&& __bothSmallInteger(start, stop)
+        && (__INST(mode) != @symbol(readonly))
+        && __bothSmallInteger(start, stop)
     ) {
-	f = __FILEVal(fp);
-	if (_buffered = (__INST(buffered) == true)) {
-	    __WRITING__(f)
-	}
-	iStart = __intVal(start);
-	iStop = __intVal(stop);
-	if ((iStart < 1) || (iStop < iStart)) {
-	    RETURN(self);
-	}
-	if (__isString(aCollection) || __isSymbol(aCollection)) {
-	    len = __stringSize(aCollection);
-	    if (iStop > len) {
-		RETURN(self);
-	    }
-	    if (iStop > len)
-		iStop = len;
-	    len = iStop - iStart + 1;
+        f = __FILEVal(fp);
+        if (_buffered = (__INST(buffered) == true)) {
+            __WRITING__(f)
+        }
+        iStart = __intVal(start);
+        iStop = __intVal(stop);
+        if ((iStart < 1) || (iStop < iStart)) {
+            RETURN(self);
+        }
+        if (__isString(aCollection) || __isSymbol(aCollection)) {
+            len = __stringSize(aCollection);
+            if (iStop > len) {
+                RETURN(self);
+            }
+            if (iStop > len)
+                iStop = len;
+            len = iStop - iStart + 1;
 #ifdef WIN32
-	    if ((f == stdout) || (f == stderr)) {
-		cnt = fwrite(__stringVal(aCollection)+iStart-1, 1, len, f);
-	    } else 
+            if ((f == stdout) || (f == stderr)) {
+                cnt = fwrite(__stringVal(aCollection)+iStart-1, 1, len, f);
+            } else 
 #endif
-	    {
-		o_offs = (char *)__stringVal(aCollection)-(char *)__InstPtr(aCollection);
-		__WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs+iStart-1, len, _buffered);
-	    }
-	} else {
-	    if (__INST(binary) == true) {
-		int offs;
-
-		if (__isByteArray(aCollection)) {
-		    offs = 0;
-		    len = __byteArraySize(aCollection);
-		} else if (__isBytes(aCollection)) {
-		    offs = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(aCollection))->c_ninstvars));
-		    len = __byteArraySize(aCollection) - offs;
-		} else
-		    goto out;
-
-		if (iStop > len) {
-		    RETURN(self);
-		}
-		if (iStop > len)
-		    iStop = len;
-		len = iStop - iStart + 1;
-		offs += iStart - 1;
+            {
+                o_offs = (char *)__stringVal(aCollection)-(char *)__InstPtr(aCollection);
+                __WRITEBYTES_OBJ__(cnt, f, aCollection, o_offs+iStart-1, len, _buffered);
+            }
+        } else {
+            if (__INST(binary) == true) {
+                int offs;
+
+                if (__isByteArray(aCollection)) {
+                    offs = 0;
+                    len = __byteArraySize(aCollection);
+                } else if (__isBytes(aCollection)) {
+                    offs = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(aCollection))->c_ninstvars));
+                    len = __byteArraySize(aCollection) - offs;
+                } else
+                    goto out;
+
+                if (iStop > len) {
+                    RETURN(self);
+                }
+                if (iStop > len)
+                    iStop = len;
+                len = iStop - iStart + 1;
+                offs += iStart - 1;
 #ifdef WIN32
-		if ((f == stdout) || (f == stderr)) {
-		    cnt = fwrite(__stringVal(aCollection)+iStart-1, 1, len, f);
-		} else 
+                if ((f == stdout) || (f == stderr)) {
+                    cnt = fwrite(__stringVal(aCollection)+iStart-1, 1, len, f);
+                } else 
 #endif
-		{
-		    o_offs = (char *)(__ByteArrayInstPtr(aCollection)->ba_element)-(char *)__InstPtr(aCollection);
-		    __WRITEBYTES_OBJ__(cnt, f,  aCollection, o_offs+offs, len, _buffered);
-		}
-	    } else
-		goto out;
-	}
-	if (cnt == len) {
-	    pos = __INST(position);
-	    if (pos != nil) {
-		__INST(position) = __MKSMALLINT(__intVal(pos) + len);
-	    }
-	    RETURN (self);
-	}
-	__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno);
+                {
+                    o_offs = (char *)(__ByteArrayInstPtr(aCollection)->ba_element)-(char *)__InstPtr(aCollection);
+                    __WRITEBYTES_OBJ__(cnt, f,  aCollection, o_offs+offs, len, _buffered);
+                }
+            } else
+                goto out;
+        }
+        if (cnt == len) {
+            pos = __INST(position);
+            if (pos != nil) {
+                __INST(position) = __MKSMALLINT(__intVal(pos) + len);
+            }
+            RETURN (self);
+        }
+        __INST(lastErrorNumber) = __MKSMALLINT(__threadErrno);
     }
 out: ;
 %}.
-    lastErrorNumber notNil ifTrue:[^ self writeError].
+    lastErrorNumber notNil ifTrue:[self writeError. ^ self].
     ^ super nextPutAll:aCollection startingAt:start to:stop
 ! !
 
 !ExternalStream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.263 2003-11-10 11:46:38 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.264 2003-12-09 10:46:45 penk Exp $'
 ! !
 
 ExternalStream initialize!