ExternalStream.st
changeset 447 7e27756077fa
parent 443 fae13c0f1512
child 454 344ccfa720d9
--- a/ExternalStream.st	Wed Oct 25 14:40:57 1995 +0100
+++ b/ExternalStream.st	Fri Oct 27 13:19:56 1995 +0100
@@ -24,7 +24,7 @@
 COPYRIGHT (c) 1988 by Claus Gittinger
 	      All Rights Reserved
 
-$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.56 1995-10-23 16:53:43 cg Exp $
+$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.57 1995-10-27 12:19:30 cg Exp $
 '!
 
 !ExternalStream primitiveDefinitions!
@@ -73,6 +73,66 @@
 %}
 ! !
 
+!ExternalStream primitiveFunctions!
+%{
+
+static int 
+__fwrite(file, cp, len, buffered)
+    FILE *file;
+    char *cp;
+{
+	int cnt;
+
+#ifdef LINUX
+        errno = 0;
+
+        /*
+         * stdio library has a bug if interrupted
+         * therefore, we go directly into write()
+         */
+        if (! buffered) {
+            int cc, rest;
+
+            cnt = 0;
+            rest = len;
+            do {
+                cc = write(fileno(file), cp, rest);
+                if (cc >= 0) {
+                    cp += cc;
+                    rest -= cc;
+                    cnt += cc;
+                    errno = EINTR; /* kludge */
+                }
+            } while ((cnt != len) && (errno == EINTR));
+        } else {
+            cnt = fwrite(cp, 1, len, file);
+            if (errno == EINTR) errno = 0;
+        }
+#else
+        errno = 0;
+        do {
+            cnt = fwrite(cp, 1, len, file);
+            if (cnt != len) {
+                if (cnt >= 0) {
+                    cp += cnt;
+                    len -= cnt;
+# ifdef HPUX
+                    clearerr(file);
+# endif
+                }
+            }
+        } while ((cnt != len) && (errno == EINTR));
+        if (! buffered) {
+            fflush(file);
+        }
+#endif /* LINUX */
+
+	return cnt;
+}
+
+%}
+! !
+
 !ExternalStream class methodsFor:'documentation'!
 
 copyright
@@ -91,7 +151,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.56 1995-10-23 16:53:43 cg Exp $
+$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.57 1995-10-27 12:19:30 cg Exp $
 "
 !
 
@@ -1363,20 +1423,24 @@
 	    c = __intVal(aByteValue);
 	    f = MKFD(fp);
 	    __BEGIN_INTERRUPTABLE__
-#ifdef OLD
-	    if (_INST(buffered) == false) {
-		cnt = write(fileno(f), &c, 1);
-	    } else 
-#endif
-	    {
-		__WRITING__(f)
-		cnt = fwrite(&c, 1, 1, f);
-#ifndef OLD
-		if (_INST(buffered) == false) {
-		    fflush(f);
-		}
-#endif
-	    }
+/*
+ *#ifdef OLD
+ *	    if (_INST(buffered) == false) {
+ *		cnt = write(fileno(f), &c, 1);
+ *	    } else 
+ *#endif
+ *	    {
+ *		__WRITING__(f)
+ *		cnt = fwrite(&c, 1, 1, f);
+ *#ifndef OLD
+ *		if (_INST(buffered) == false) {
+ *		    fflush(f);
+ *		}
+ *#endif
+ *	    }
+ */
+ 	    __WRITING__(f)
+	    cnt = __fwrite(f, &c, 1, (_INST(buffered) == true));
 	    __END_INTERRUPTABLE__
 
 	    if (cnt == 1) {
@@ -1462,20 +1526,25 @@
 	    if ( (offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs)) ) {
 		cp = (char *)__InstPtr(anObject) + nInstBytes + offs;
 		__BEGIN_INTERRUPTABLE__
-#ifdef OLD
-		if (_INST(buffered) == false) {
-		    cnt = write(fileno(f), cp, cnt);
-		} else
-#endif
-		{
-		    __WRITING__(f)
-		    cnt = fwrite(cp, 1, cnt, f);
-		}
-#ifndef OLD
-		if (_INST(buffered) == false) {
-		    fflush(f);
-		}
-#endif
+/*
+ *#ifdef OLD
+ *		if (_INST(buffered) == false) {
+ *		    cnt = write(fileno(f), cp, cnt);
+ *		} else
+ *#endif
+ *		{
+ *		    __WRITING__(f)
+ *		    cnt = fwrite(cp, 1, cnt, f);
+ *		}
+ *#ifndef OLD
+ *		if (_INST(buffered) == false) {
+ *		    fflush(f);
+ *		}
+ *#endif
+ */
+ 	        __WRITING__(f)
+		cnt = __fwrite(f, cp, cnt, (_INST(buffered) == true));
+
 		__END_INTERRUPTABLE__
 
 		if (cnt >= 0) {
@@ -1891,13 +1960,16 @@
 	    f = MKFD(fp);
 
 	    __BEGIN_INTERRUPTABLE__
+#ifdef NONONO
+
 #ifdef OLD
 	    if (_INST(buffered) == false) {
 		cnt = write(fileno(f), cp, len);
-	    } else 
+ 	    } else 
 #endif
 	    { 
 		__WRITING__(f)
+
 #ifdef LINUX
 		errno = 0;
 
@@ -1946,8 +2018,12 @@
 	    if (_INST(buffered) == false) {
 		fflush(f);
 	    }
-
+#endif
+
+	    __WRITING__(f)
+	    cnt = __fwrite(f, cp, len, (_INST(buffered) == true));
 	    __END_INTERRUPTABLE__
+
 	    if (cnt == len) {
 		pos = _INST(position);
 		if (pos != nil) {
@@ -2014,23 +2090,27 @@
 		__BEGIN_INTERRUPTABLE__
 		len = index2 - index1 + 1;
 
-		__WRITING__(f)
-                
-		do {
-		    cnt = fwrite(cp + index1 - 1, 1, len, f);
-		    if (cnt != len) {
-			if (cnt >= 0) {
-			    if (errno == EINTR) {
-				cp += cnt;
-				len -= cnt;
-			    }
-			}
-		    }
-		} while ((cnt != len) && (errno == EINTR));
-
-		if (_INST(buffered) == false) {
-		    fflush(f);
-		}
+/*
+ *		__WRITING__(f)
+ *               
+ *		do {
+ *		    cnt = fwrite(cp + index1 - 1, 1, len, f);
+ *		    if (cnt != len) {
+ *			if (cnt >= 0) {
+ *			    if (errno == EINTR) {
+ *				cp += cnt;
+ *				len -= cnt;
+ *			    }
+ *			}
+ *		    }
+ *		} while ((cnt != len) && (errno == EINTR));
+ *
+ *		if (_INST(buffered) == false) {
+ *		    fflush(f);
+ *		}
+ */
+	        __WRITING__(f)
+	        cnt = __fwrite(f, cp+index1-1, len, (_INST(buffered) == true));
 
 		__END_INTERRUPTABLE__
 		if (cnt == len) {
@@ -2178,6 +2258,7 @@
 		    }
 		} else {
 		    do {
+			errno = 0;
 			len = read(fd, rslt, 1);
 		    } while ((len < 0) && (errno == EINTR));
 		}
@@ -2253,29 +2334,44 @@
 		len = _stringSize(aString);
 
 		__BEGIN_INTERRUPTABLE__
-#ifdef OLD
-		if (_INST(buffered) == false) {
-		    cnt = write(fileno(f), s, len);
-		} else 
-#endif
-		{ 
-		    __WRITING__(f)
-		    cnt = fwrite(s, 1, len, f);
-		}
+/*
+ *#ifdef OLD
+ *		if (_INST(buffered) == false) {
+ *		    cnt = write(fileno(f), s, len);
+ *		} else 
+ *#endif
+ *		{ 
+ *		    __WRITING__(f)
+ *		    cnt = fwrite(s, 1, len, f);
+ *		}
+ *		if (cnt == len) {
+ *#ifdef OLD
+ *		    if (_INST(buffered) == false) {
+ *			cnt = write(fileno(f), "\n", 1);
+ *		    } else 
+ *#endif
+ *		    { 
+ *			cnt = fwrite("\n", 1, 1, f);
+ *		    }
+ *#ifndef OLD
+ *		    if (_INST(buffered) == false) {
+ *			if (fflush(f) == EOF) goto end;
+ *		    }
+ *#endif
+ *		    if (cnt == 1) {
+ *			pos = _INST(position);
+ *			if (pos != nil) {
+ *			    _INST(position) = __MKSMALLINT(__intVal(pos)+len+1);
+ *			}
+ *			__END_INTERRUPTABLE__
+ *			RETURN ( self );
+ *		    }
+ *		}
+ */
+		__WRITING__(f)
+		cnt = __fwrite(f, s, len, (_INST(buffered) == true));
 		if (cnt == len) {
-#ifdef OLD
-		    if (_INST(buffered) == false) {
-			cnt = write(fileno(f), "\n", 1);
-		    } else 
-#endif
-		    { 
-			cnt = fwrite("\n", 1, 1, f);
-		    }
-#ifndef OLD
-		    if (_INST(buffered) == false) {
-			if (fflush(f) == EOF) goto end;
-		    }
-#endif
+		    cnt = __fwrite(f, "\n", 1, (_INST(buffered) == true));
 		    if (cnt == 1) {
 			pos = _INST(position);
 			if (pos != nil) {
@@ -2285,6 +2381,7 @@
 			RETURN ( self );
 		    }
 		}
+		
 end:
 		__END_INTERRUPTABLE__
 		_INST(lastErrorNumber) = __MKSMALLINT(errno);