ExternalStream.st
changeset 370 20f04d9b371b
parent 369 730e0f5d2404
child 372 63f6b3823c29
--- a/ExternalStream.st	Fri Jul 28 04:38:43 1995 +0200
+++ b/ExternalStream.st	Thu Aug 03 03:17:14 1995 +0200
@@ -24,7 +24,7 @@
 COPYRIGHT (c) 1988 by Claus Gittinger
 	      All Rights Reserved
 
-$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.42 1995-07-28 02:36:22 claus Exp $
+$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.43 1995-08-03 01:15:09 claus Exp $
 '!
 
 !ExternalStream primitiveDefinitions!
@@ -86,7 +86,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.42 1995-07-28 02:36:22 claus Exp $
+$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.43 1995-08-03 01:15:09 claus Exp $
 "
 !
 
@@ -1729,7 +1729,7 @@
 
 next:count
     "return the next count elements of the stream as a collection.
-     Redefined to return a String or ByteArray instead of the default Array."
+     Redefined to return a String or ByteArray instead of the default: Array."
 
     |coll|
 
@@ -1738,6 +1738,10 @@
     ] ifFalse:[
 	coll := String new:count
     ].
+    "/
+    "/ Q: should we use: 
+    "/    self nextBytes:count into:coll startingAt:1
+    "/
     1 to:count do: [:index |
 	coll at:index put:(self next)
     ].
@@ -2052,7 +2056,7 @@
     FILE *f;
     int len;
     char buffer[1024];
-    char *rslt;
+    char *rslt, *limit;
     int fd, ch;
     int _buffered;
     OBJ fp;
@@ -2085,6 +2089,8 @@
 #else
 
 	    rslt = buffer;
+	    limit = buffer + sizeof(buffer) - 2;
+
 	    for (;;) {
 		if (_buffered) {
 /*
@@ -2125,7 +2131,7 @@
 		    *rslt = '\0';
 		    break;
 		}
-		if (rslt == (buffer + sizeof(buffer) - 1)) {
+		if (rslt >= limit) {
 		    *rslt = '\0';
 		    break;
 		}
@@ -2133,6 +2139,9 @@
 #endif
 	    __END_INTERRUPTABLE__
 	    if (rslt != NULL) {
+		/*
+		 * that strlen can be avoided and replaced by (rslt - buffer)
+		 */
 		len = strlen(buffer);
 		if (_INST(position) != nil) {
 		    _INST(position) = __MKSMALLINT(__intVal(_INST(position)) + len + 1);
@@ -2262,7 +2271,7 @@
 	    __WRITING__(dst)
 
 	    for (;;) {
-		if (fgets(buffer, sizeof(buffer), src) == NULL) {
+		if (fgets(buffer, sizeof(buffer)-1, src) == NULL) {
 		    if (ferror(src)) {
 			readError = __MKSMALLINT(errno);
 			__END_INTERRUPTABLE__
@@ -2340,8 +2349,9 @@
 
 	    __BEGIN_INTERRUPTABLE__
 	    do {
-		cp = fgets(buffer, sizeof(buffer), f);
+		cp = fgets(buffer, sizeof(buffer)-1, f);
 	    } while ((cp == NULL) && (errno == EINTR));
+	    buffer[sizeof(buffer)-1] = '\0';
 	    __END_INTERRUPTABLE__
 
 	    if (cp == NULL) {
@@ -2748,7 +2758,7 @@
 	    __READING__(f)
 
 	    __BEGIN_INTERRUPTABLE__
-	    if (fgets(buffer, sizeof(buffer), f) != NULL) {
+	    if (fgets(buffer, sizeof(buffer)-1, f) != NULL) {
 		__END_INTERRUPTABLE__
 		RETURN ( self );
 	    }
@@ -3056,6 +3066,9 @@
 	__BEGIN_INTERRUPTABLE__
 	__READING__(f)
 
+	/*
+	 * skip whiteSpace first ...
+	 */
 	for (;;) {
 	    do {
 		ch = getc(f);
@@ -3103,7 +3116,7 @@
 	    }
 	    cnt++;
 	    buffer[len++] = ch;
-	    if (len >= sizeof(buffer)-1) {
+	    if (len >= (sizeof(buffer)-1)) {
 		/* emergency */
 		break;
 	    }
@@ -3141,7 +3154,7 @@
     binary ifTrue:[^ self errorBinary].
     (mode == #writeonly) ifTrue:[^ self errorWriteOnly].
 
-%{  /* STACK: 2000 */
+%{  /* STACK: 4000 */
     FILE *f;
     int done = 0;
     REGISTER int c;
@@ -3297,15 +3310,21 @@
 	buffer = (char *)0;
     }
 %}.
-    lastErrorNumber notNil ifTrue:[^ self readError].
-    outOfMemory == true ifTrue:[
-	"
-	 memory allocation failed.
-	 When we arrive here, there was no (unix) memory available for the
-	 chunk. (seems to be too big of a chunk ...)
-	 Bad luck - you should increase the swap space on your machine.
-	"
-	^ ObjectMemory allocationFailureSignal raise.
+    retVal isNil ifTrue:[
+        "/
+        "/ arrive here with retVal==nil either on error or premature EOF
+        "/ or if running out of malloc-memory
+        "/
+        lastErrorNumber notNil ifTrue:[^ self readError].
+        outOfMemory == true ifTrue:[
+	    "
+	     memory allocation failed.
+	     When we arrive here, there was no (unix) memory available for the
+	     chunk. (seems to be too big of a chunk ...)
+	     Bad luck - you should increase the swap space on your machine.
+	    "
+	    ^ ObjectMemory allocationFailureSignal raise.
+	]
     ].
     ^ retVal
 ! !