*** empty log message ***
authorclaus
Sat, 11 Dec 1993 01:46:55 +0100
changeset 12 8e03bd717355
parent 11 6bf3080856be
child 13 62303f84ff5f
*** empty log message ***
Boolean.st
ByteArray.st
CCReader.st
Character.st
ClassCategoryReader.st
Coll.st
Collection.st
Context.st
Dict.st
Dictionary.st
Exception.st
ExtStream.st
ExternalStream.st
--- a/Boolean.st	Sat Dec 11 01:42:02 1993 +0100
+++ b/Boolean.st	Sat Dec 11 01:46:55 1993 +0100
@@ -35,7 +35,7 @@
 
 written 1988 by claus
 
-$Header: /cvs/stx/stx/libbasic/Boolean.st,v 1.4 1993-10-13 02:11:10 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Boolean.st,v 1.5 1993-12-11 00:43:46 claus Exp $
 '!
 
 !Boolean class methodsFor:'instance creation'!
@@ -69,6 +69,20 @@
      - since both true and false are unique, return the receiver"
 
     ^ self
+!
+
+deepCopyUsing:aDictionary
+    "return a deep copy of the receiver
+     - since both true and false are unique, return the receiver"
+
+    ^ self
+!
+
+simpleDeepCopy
+    "return a deep copy of the receiver
+     - since both true and false are unique, return the receiver"
+
+    ^ self
 ! !
 
 !Boolean methodsFor:'printing & storing'!
@@ -86,4 +100,3 @@
 hasSpecialBinaryRepresentation
         ^true
 ! !
-
--- a/ByteArray.st	Sat Dec 11 01:42:02 1993 +0100
+++ b/ByteArray.st	Sat Dec 11 01:46:55 1993 +0100
@@ -25,7 +25,7 @@
 ByteArrays store integers in the range 0..255
 unlike Smalltalk/80, my ByteArrays have fixed size - may change
 
-$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.5 1993-11-08 02:29:19 claus Exp $
+$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.6 1993-12-11 00:44:06 claus Exp $
 
 written spring 89 by claus
 '!
@@ -68,7 +68,6 @@
      value. use, when contents will be set anyway shortly after."
 
 %{  /* NOCONTEXT */
-
     OBJ newobj;
     INT instsize, nInstVars, nindexedinstvars;
     REGISTER OBJ *op;
@@ -80,26 +79,36 @@
             nInstVars = _intVal(_ClassInstPtr(self)->c_ninstvars);
             if ((_intVal(_ClassInstPtr(self)->c_flags) & ARRAYMASK) == BYTEARRAY) {
                 instsize = OHDR_SIZE + nInstVars * sizeof(OBJ) + nindexedinstvars * sizeof(char);
-                PROTECT(self);
+                PROTECT_CONTEXT
                 _qNew(newobj, instsize, SENDER);
-                UNPROTECT(self);
-                _InstPtr(newobj)->o_class = self;
+                UNPROTECT_CONTEXT
+                if (newobj) {
+                    _InstPtr(newobj)->o_class = self;
 #if defined(FAST_MEMSET) && ! defined(NEGATIVE_ADDRESSES)
-                /*
-                 * knowing that nil is 0
-                 */
-                memset(_InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE);
+                    /*
+                     * knowing that nil is 0
+                     */
+                    memset(_InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE);
 #else
-                op = _InstPtr(newobj)->i_instvars;
-                while (nInstVars--)
-                    *op++ = nil;
+                    op = _InstPtr(newobj)->i_instvars;
+                    while (nInstVars--)
+                        *op++ = nil;
 #endif
-                RETURN ( newobj );
+                    RETURN ( newobj );
+                }
             }
         }
     }
 %}
 .
+    (anInteger isMemberOf:SmallInteger) ifTrue:[
+        (anInteger < 0) ifTrue:[
+            self error:'bad (negative) argument to new'
+        ] ifFalse:[
+            ObjectMemory allocationFailureSignal raise.
+        ].
+        ^ nil
+    ].
     ^ self basicNew:anInteger
 
 ! !
@@ -295,6 +304,32 @@
     ^ value
 ! !
 
+!ByteArray methodsFor:'converting'!
+
+asByteArray
+    "return the receiver as a byteArray"
+
+    "could be an instance of a subclass..."
+    self class == ByteArray ifTrue:[
+        ^ self
+    ].
+    ^ super asByteArray
+! !
+
+!ByteArray methodsFor:'printing & storing'!
+
+isLiteral
+    "return true, if the receiver can be used as a literal"
+
+    ^ true
+!
+
+storeOn:aStream
+    aStream nextPutAll:'#['.
+    self do:[:byte | byte storeOn:aStream. aStream space. ].
+    aStream nextPutAll:']'
+! !
+
 !ByteArray methodsFor:'queries'!
 
 indexOf:aByte startingAt:start
@@ -349,7 +384,9 @@
      needed specially for Image class."
 
     |result l|
-%{
+
+%{  /* STACK: 400 */
+
     REGISTER unsigned char *cp;
     REGISTER int len;
     unsigned char flags[256];
@@ -373,12 +410,7 @@
             if (*cp) len++;
 
         /* create ByteArray of used values */
-#ifdef PASS_ARG_REF
-        l = _MKSMALLINT(len);
-        result = (*nw.ilc_func)(ByteArray, _new_, CON_COMMA nil, &nw, &l);
-#else
         result = (*nw.ilc_func)(ByteArray, _new_, CON_COMMA nil, &nw, _MKSMALLINT(len));
-#endif
         if (_Class(result) == ByteArray) {
             cp = &(_ByteArrayInstPtr(result)->ba_element[0]);
             for (len=0; len < 256; len++) {
@@ -400,7 +432,9 @@
     |counts|
 
     counts := Array new:256.
-%{
+
+%{  /* STACK: 2000 */
+
     REGISTER unsigned char *cp;
     REGISTER int nByte;
     REGISTER int index;
@@ -486,6 +520,9 @@
               nIndex = _qSize(self) - OHDR_SIZE;
               stopIndex = _intVal(stop) - 1;
               count = stopIndex - startIndex + 1;
+              if (count == 0) {
+                  RETURN ( self );
+              }
               if ((count > 0) && (stopIndex < nIndex)) {
                 repStartIndex = _intVal(repStart) - 1;
                 if (repStartIndex >= 0) {
@@ -560,6 +597,33 @@
     self primitiveFailed
 !
 
+reverse
+    "reverse order of elements inplace - 
+     written as a primitive for speed on image manipulations"
+
+%{  /* NOCONTEXT */
+
+    REGISTER unsigned char *p1, *p2;
+    REGISTER int cnt;
+    REGISTER unsigned t;
+
+    if (_qClass(self) == ByteArray) {
+        cnt = _qSize(self) - OHDR_SIZE;
+        p1 = _ByteArrayInstPtr(self)->ba_element;
+        p2 = _ByteArrayInstPtr(self)->ba_element + cnt - 1;
+        while (cnt > 0) {
+            t = *p1;
+            *p1++ = *p2;
+            *p2-- = t;
+            cnt-=2;
+        }
+        RETURN ( self );
+    }
+%}
+.
+    ^ super reverse
+!
+
 expandPixels:nBitsPerPixel width:width height:height into:aByteArray
                          mapping:aMapByteArray
     "given the receiver with nBitsPerPixel-depth pixels, expand them into
--- a/CCReader.st	Sat Dec 11 01:42:02 1993 +0100
+++ b/CCReader.st	Sat Dec 11 01:46:55 1993 +0100
@@ -24,7 +24,7 @@
 
 a helper class for fileIn - keeps track of class and category to filein for.
 
-$Header: /cvs/stx/stx/libbasic/Attic/CCReader.st,v 1.5 1993-11-08 02:29:30 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Attic/CCReader.st,v 1.6 1993-12-11 00:44:33 claus Exp $
 written 89 by claus
 '!
 
@@ -51,7 +51,7 @@
 fileInFrom:aStream notifying:requestor
     "read method-chunks from the input stream, aStream; compile them
      and add the methods to the class defined by the class-instance var;
-     errors notifications are passed to requestor"
+     errors and notifications are passed to requestor"
 
     |aString done method|
 
--- a/Character.st	Sat Dec 11 01:42:02 1993 +0100
+++ b/Character.st	Sat Dec 11 01:46:55 1993 +0100
@@ -36,7 +36,7 @@
 instances for $x and, since equality check on the Character class is
 wired into the system in many places.)
 
-$Header: /cvs/stx/stx/libbasic/Character.st,v 1.5 1993-11-08 02:29:37 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Character.st,v 1.6 1993-12-11 00:44:49 claus Exp $
 
 '!
 
@@ -198,6 +198,20 @@
      ^ self
 !
 
+simpleDeepCopy
+    "return a depp copy of myself
+     reimplemented since characters are unique"
+
+     ^ self
+!
+
+deepCopyUsing:aDictionary
+    "return a depp copy of myself
+     reimplemented since characters are unique"
+
+     ^ self
+!
+
 deepCopy
     "return a depp copy of myself
      reimplemented since characters are unique"
@@ -577,6 +591,12 @@
 
 !Character methodsFor:'printing & storing'!
 
+isLiteral
+    "return true, if the receiver can be used as a literal"
+
+    ^ true
+!
+
 printString
     "return a string to print me"
 
--- a/ClassCategoryReader.st	Sat Dec 11 01:42:02 1993 +0100
+++ b/ClassCategoryReader.st	Sat Dec 11 01:46:55 1993 +0100
@@ -24,7 +24,7 @@
 
 a helper class for fileIn - keeps track of class and category to filein for.
 
-$Header: /cvs/stx/stx/libbasic/ClassCategoryReader.st,v 1.5 1993-11-08 02:29:30 claus Exp $
+$Header: /cvs/stx/stx/libbasic/ClassCategoryReader.st,v 1.6 1993-12-11 00:44:33 claus Exp $
 written 89 by claus
 '!
 
@@ -51,7 +51,7 @@
 fileInFrom:aStream notifying:requestor
     "read method-chunks from the input stream, aStream; compile them
      and add the methods to the class defined by the class-instance var;
-     errors notifications are passed to requestor"
+     errors and notifications are passed to requestor"
 
     |aString done method|
 
--- a/Coll.st	Sat Dec 11 01:42:02 1993 +0100
+++ b/Coll.st	Sat Dec 11 01:46:55 1993 +0100
@@ -24,7 +24,7 @@
 
 Abstract superclass for all collections
 
-$Header: /cvs/stx/stx/libbasic/Attic/Coll.st,v 1.5 1993-11-08 02:29:51 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Attic/Coll.st,v 1.6 1993-12-11 00:45:11 claus Exp $
 '!
 
 !Collection class methodsFor:'initialization'!
@@ -150,6 +150,12 @@
     ^ self add:anObject
 !
 
+addFirst:anObject
+    "add the argument, anObject to the receiver"
+
+    ^ self subclassResponsibility
+!
+
 addAll:aCollection
     "add all elements of the argument, aCollection to the receiver"
 
@@ -159,6 +165,28 @@
     ^ aCollection
 !
 
+addAllLast:aCollection
+    "add all elements of the argument, aCollection to the receiver"
+
+    ^ self addAll:aCollection
+!
+
+addAllFirst:aCollection
+    "insert all elements of the argument, aCollection at the beginning
+     of the receiver"
+
+    aCollection reverseDo:[:element | 
+        self addFirst: element 
+    ].
+    ^ aCollection
+
+    "
+     |c|
+     c := #(4 3 2 1) asOrderedCollection.
+     c addAllFirst:#(9 8 7 6 5)
+    "
+!
+
 remove:anObject ifAbsent:exceptionBlock
     "remove the argument, anObject from the receiver - if it was not
      in the collection returns the the value of the exceptionBlock"
@@ -337,6 +365,14 @@
         (aBlock value:each) ifTrue:[newCollection add:each].
     ].
     ^ newCollection
+!
+
+addAllTo:aCollection
+    "add all elements of the receiver, to aCollection.
+     Return aCollection."
+
+    self do:[:each | aCollection add:each].
+    ^ aCollection
 ! !
 
 !Collection methodsFor:'converting'!
@@ -405,31 +441,25 @@
 asBag
     "return a new Bag with the receiver collections elements"
 
-    |aBag|
-
-    aBag := Bag new.
-    self do:[:each | aBag add:each].
-    ^ aBag
+    ^ self addAllTo:(Bag new)
 !
 
 asOrderedCollection
     "return a new OrderedCollection with the receiver collections elements"
 
-    |anOrderedCollection|
-
-    anOrderedCollection := OrderedCollection new:self size.
-    self do:[:each | anOrderedCollection addLast:each].
-    ^ anOrderedCollection
+    ^ self addAllTo:(OrderedCollection new:self size)
 !
 
 asSet
     "return a new Set with the receiver collections elements"
 
-    |aSet|
+    ^ self addAllTo:(Set new:self size)
+!
 
-    aSet := Set new: self size.
-    self do:[:each | aSet add:each].
-    ^ aSet
+asIdentitySet
+    "return a new IdentitySet with the receiver collections elements"
+
+    ^ self addAllTo:(IdentitySet new:self size)
 !
 
 asSortedCollection
@@ -471,11 +501,9 @@
     ^ 5000
 !
 
-printString
-    "return the printString of a big collection can take a long time
-     due to long temporary strings - I use a buffer here collecting some
-     elements to reduce the GC overhead ...
-    "
+printOrDisplayStringUsing:aSelector
+    "common code for printString and displayString; they only differ in
+     the print-message sent to the elements"
 
     |thisString buffer count string noneYet total|
 
@@ -485,7 +513,7 @@
     count := 0.
     total := 0.
     self do: [:element |
-        thisString := element printString.
+        thisString := element perform:aSelector.
         noneYet ifTrue:[
             noneYet := false.
             buffer := buffer , thisString
@@ -508,41 +536,16 @@
     ^string
 !
 
-displayString
-    "return the printString of a big collection can take a long time
-     due to long temporary strings - I use a buffer here collecting some
-     elements to reduce the GC overhead ...
-    "
-
-    |thisString buffer count string noneYet total|
+printString
+    "return a printed representation of the receiver"
 
-    string := (self class name) , '('.
-    noneYet := true.
-    buffer := ''.
-    count := 0.
-    total := 0.
-    self do: [:element |
-        thisString := element displayString.
-        noneYet ifTrue:[
-            noneYet := false.
-            buffer := buffer , thisString
-        ] ifFalse:[
-            buffer := buffer , (' ' , thisString)
-        ].
-        count := count + 1.
-        (count == 20) ifTrue:[
-            string := string , buffer.
-            buffer := ''.
-            count := 0
-        ].
-        total := total + 1.
-        (total > 5000) ifTrue:[
-            string := string , buffer , '... )'.
-            ^string
-        ]
-    ].
-    string := string , buffer , ')'.
-    ^string
+    ^ self printOrDisplayStringUsing:#printString 
+!
+
+displayString
+    "return a printed representation of the receiver for display in inspectors etc."
+
+    ^ self printOrDisplayStringUsing:#displayString 
 !
 
 printOn:aStream
--- a/Collection.st	Sat Dec 11 01:42:02 1993 +0100
+++ b/Collection.st	Sat Dec 11 01:46:55 1993 +0100
@@ -24,7 +24,7 @@
 
 Abstract superclass for all collections
 
-$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.5 1993-11-08 02:29:51 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.6 1993-12-11 00:45:11 claus Exp $
 '!
 
 !Collection class methodsFor:'initialization'!
@@ -150,6 +150,12 @@
     ^ self add:anObject
 !
 
+addFirst:anObject
+    "add the argument, anObject to the receiver"
+
+    ^ self subclassResponsibility
+!
+
 addAll:aCollection
     "add all elements of the argument, aCollection to the receiver"
 
@@ -159,6 +165,28 @@
     ^ aCollection
 !
 
+addAllLast:aCollection
+    "add all elements of the argument, aCollection to the receiver"
+
+    ^ self addAll:aCollection
+!
+
+addAllFirst:aCollection
+    "insert all elements of the argument, aCollection at the beginning
+     of the receiver"
+
+    aCollection reverseDo:[:element | 
+        self addFirst: element 
+    ].
+    ^ aCollection
+
+    "
+     |c|
+     c := #(4 3 2 1) asOrderedCollection.
+     c addAllFirst:#(9 8 7 6 5)
+    "
+!
+
 remove:anObject ifAbsent:exceptionBlock
     "remove the argument, anObject from the receiver - if it was not
      in the collection returns the the value of the exceptionBlock"
@@ -337,6 +365,14 @@
         (aBlock value:each) ifTrue:[newCollection add:each].
     ].
     ^ newCollection
+!
+
+addAllTo:aCollection
+    "add all elements of the receiver, to aCollection.
+     Return aCollection."
+
+    self do:[:each | aCollection add:each].
+    ^ aCollection
 ! !
 
 !Collection methodsFor:'converting'!
@@ -405,31 +441,25 @@
 asBag
     "return a new Bag with the receiver collections elements"
 
-    |aBag|
-
-    aBag := Bag new.
-    self do:[:each | aBag add:each].
-    ^ aBag
+    ^ self addAllTo:(Bag new)
 !
 
 asOrderedCollection
     "return a new OrderedCollection with the receiver collections elements"
 
-    |anOrderedCollection|
-
-    anOrderedCollection := OrderedCollection new:self size.
-    self do:[:each | anOrderedCollection addLast:each].
-    ^ anOrderedCollection
+    ^ self addAllTo:(OrderedCollection new:self size)
 !
 
 asSet
     "return a new Set with the receiver collections elements"
 
-    |aSet|
+    ^ self addAllTo:(Set new:self size)
+!
 
-    aSet := Set new: self size.
-    self do:[:each | aSet add:each].
-    ^ aSet
+asIdentitySet
+    "return a new IdentitySet with the receiver collections elements"
+
+    ^ self addAllTo:(IdentitySet new:self size)
 !
 
 asSortedCollection
@@ -471,11 +501,9 @@
     ^ 5000
 !
 
-printString
-    "return the printString of a big collection can take a long time
-     due to long temporary strings - I use a buffer here collecting some
-     elements to reduce the GC overhead ...
-    "
+printOrDisplayStringUsing:aSelector
+    "common code for printString and displayString; they only differ in
+     the print-message sent to the elements"
 
     |thisString buffer count string noneYet total|
 
@@ -485,7 +513,7 @@
     count := 0.
     total := 0.
     self do: [:element |
-        thisString := element printString.
+        thisString := element perform:aSelector.
         noneYet ifTrue:[
             noneYet := false.
             buffer := buffer , thisString
@@ -508,41 +536,16 @@
     ^string
 !
 
-displayString
-    "return the printString of a big collection can take a long time
-     due to long temporary strings - I use a buffer here collecting some
-     elements to reduce the GC overhead ...
-    "
-
-    |thisString buffer count string noneYet total|
+printString
+    "return a printed representation of the receiver"
 
-    string := (self class name) , '('.
-    noneYet := true.
-    buffer := ''.
-    count := 0.
-    total := 0.
-    self do: [:element |
-        thisString := element displayString.
-        noneYet ifTrue:[
-            noneYet := false.
-            buffer := buffer , thisString
-        ] ifFalse:[
-            buffer := buffer , (' ' , thisString)
-        ].
-        count := count + 1.
-        (count == 20) ifTrue:[
-            string := string , buffer.
-            buffer := ''.
-            count := 0
-        ].
-        total := total + 1.
-        (total > 5000) ifTrue:[
-            string := string , buffer , '... )'.
-            ^string
-        ]
-    ].
-    string := string , buffer , ')'.
-    ^string
+    ^ self printOrDisplayStringUsing:#printString 
+!
+
+displayString
+    "return a printed representation of the receiver for display in inspectors etc."
+
+    ^ self printOrDisplayStringUsing:#displayString 
 !
 
 printOn:aStream
--- a/Context.st	Sat Dec 11 01:42:02 1993 +0100
+++ b/Context.st	Sat Dec 11 01:46:55 1993 +0100
@@ -33,7 +33,7 @@
 
 Warning: layout and size known by the compiler and runtime system - do not change.
 
-$Header: /cvs/stx/stx/libbasic/Context.st,v 1.5 1993-11-08 02:29:53 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Context.st,v 1.6 1993-12-11 00:45:34 claus Exp $
 '!
 
 !Context class methodsFor:'queries'!
@@ -271,6 +271,10 @@
     ^ self receiverPrintString , ' ' , self selector printString
 !
 
+displayString
+    ^ self class name , '(' , self receiverPrintString , ' ' , self selector printString, ')'
+!
+
 printOn:aStream
     aStream nextPutAll:(self receiverPrintString).
     aStream space.
@@ -415,7 +419,7 @@
     [con ~~ self] whileTrue:[
         con isBlockContext ifFalse:[
             "the way we find those unwind contexts seems kludgy ..."
-	    sel := con selector.
+            sel := con selector.
             ((sel == #valueNowOrOnUnwindDo:) or:[sel == #valueOnUnwindDo:]) ifTrue:[
                 "... the way we evaluate the unwind blocks too"
                 (con argAt:1) value
--- a/Dict.st	Sat Dec 11 01:42:02 1993 +0100
+++ b/Dict.st	Sat Dec 11 01:46:55 1993 +0100
@@ -10,8 +10,8 @@
  hereby transferred.
 "
 
-Collection subclass:#Dictionary
-         instanceVariableNames:'valueArray keyArray tally'
+Set subclass:#Dictionary
+         instanceVariableNames:'valueArray'
          classVariableNames:''
          poolDictionaries:''
          category:'Collections-Unordered'
@@ -26,54 +26,25 @@
 (The implementation uses two array to store the keys and values separately.)
 Searching for an element is done using a hash into the key array.
 
-$Header: /cvs/stx/stx/libbasic/Attic/Dict.st,v 1.5 1993-11-08 02:30:03 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Attic/Dict.st,v 1.6 1993-12-11 00:45:55 claus Exp $
 
 written jun 91 by claus
 rewritten 92 to use hash scheme
 '!
 
-!Dictionary class methodsFor:'instance creation'!
-
-new
-    "return a new empty Dictionary"
-
-    ^ self new:5
-!
-
-new:anInteger
-    "return a new empty Dictionary with space for anInteger elements"
-
-    ^ self basicNew setTally:anInteger
-! !
-
-!Dictionary methodsFor:'private'!
-
-keyContainerOfSize:n
-    "return a container for keys and values of size n.
-     Extracted to make life of WeakDictionary easier ..."
-
-    ^ Array new:n
-! !
-
 !Dictionary methodsFor:'testing'!
 
-size
-    "return the number of elements in the receiver"
-
-    ^ tally
-!
-
 includesKey:aKey
     "return true, if the argument, aKey is a key in the receiver"
 
-    ^ (self findKey:aKey ifAbsent:[0]) ~~ 0
+    ^ (self find:aKey ifAbsent:[0]) ~~ 0
 !
 
-isFixedSize
-    "return true if the receiver cannot grow - this will vanish once
-     Arrays and Strings learn how to grow ..."
+includes:aValue
+    "return true, if the argument, aValue is stoerd in the dictionary,
+     This is a slow search, since there is no fast reverse mapping"
 
-    ^ false
+    ^ valueArray includes:aValue
 ! !
 
 !Dictionary methodsFor:'accessing'!
@@ -86,7 +57,7 @@
     aKey isNil ifTrue:[
         self errorInvalidKey
     ] ifFalse:[
-        index := self findKey:aKey ifAbsent:[0].
+        index := self find:aKey ifAbsent:[0].
         index == 0 ifTrue:[^ self errorKeyNotFound].
         ^ valueArray basicAt:index
     ]
@@ -101,7 +72,7 @@
     aKey isNil ifTrue:[
         self errorInvalidKey
     ] ifFalse:[
-        index := self findKey:aKey ifAbsent:[0].
+        index := self find:aKey ifAbsent:[0].
         index == 0 ifTrue:[^ exceptionBlock value].
         ^ valueArray basicAt:index
     ]
@@ -158,9 +129,9 @@
      This is a slow access, since there is no fast reverse mapping"
 
     keyArray keysAndValuesDo:[:index :aKey |
-	aKey notNil ifTrue:[
-	    (valueArray at:index) = aValue ifTrue:[^ aKey].
-	].
+        aKey notNil ifTrue:[
+            (valueArray at:index) = aValue ifTrue:[^ aKey].
+        ].
     ].
     ^ exceptionBlock value
 ! !
@@ -192,31 +163,7 @@
     "remove the association under aKey from the collection.
      If it was not in the collection report an error"
 
-    |index "{ Class:SmallInteger }"
-     next  "{ Class:SmallInteger }" |
-
-    aKey isNil ifTrue:[
-        self errorInvalidKey
-    ] ifFalse:[
-        index := self findKey:aKey ifAbsent:[0].
-        (index == 0) ifTrue:[^ self errorNotFound].
-        valueArray basicAt:index put:nil.
-        keyArray basicAt:index put:nil.
-        tally := tally - 1.
-        tally == 0 ifTrue:[
-            self setTally:0
-        ] ifFalse:[
-            index == keyArray basicSize ifTrue:[
-                next := 1
-            ] ifFalse:[
-                next := index + 1.
-            ].
-            "redundant check to save a send sometimes"
-            (keyArray basicAt:next) notNil ifTrue:[
-                self rehashFrom:next.
-            ]
-        ]
-    ]
+    ^ self removeKey:aKey ifAbsent:[^ self errorNotFound]
 !
 
 removeKey:aKey ifAbsent:aBlock
@@ -229,7 +176,7 @@
     aKey isNil ifTrue:[
         self errorInvalidKey
     ] ifFalse:[
-        index := self findKey:aKey ifAbsent:[0].
+        index := self find:aKey ifAbsent:[0].
         index == 0 ifTrue:[^ aBlock value].
         valueArray basicAt:index put:nil.
         keyArray basicAt:index put:nil.
@@ -311,7 +258,7 @@
 
     newCollection := Bag new.
     self do:[:each |
-        newCollection add:each
+        newCollection add:(aBlock value:each)
     ].
     ^ newCollection
 !
@@ -333,28 +280,6 @@
 
 !Dictionary methodsFor:'private'!
 
-fullCheck
-    "check if dictionary is full, grow if so.
-     Definition of full is currently:'filled more than 70%'"
-
-    "grow if filled more than 70% "
-    tally > (keyArray basicSize * 7 // 10) ifTrue:[
-       self grow
-    ]
-!
-
-goodSizeFor:arg
-    "return a good array size for the given argument.
-     Returns the next prime after arg"
-
-    arg <= 7 ifTrue:[^ 7].
-    arg <= 131072 ifTrue:[
-           "2 4 8  16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072"
-        ^ #(7 7 11 17 37 67 131 257 521 1031 2053 4099 8209 16411 32771 65537 131101) at:(arg highBit)
-    ].
-    ^ arg bitOr:1
-!
-
 setTally:count
     "initialize the contents array (for at least count slots)
      and set tally to zero.
@@ -368,75 +293,6 @@
     tally := 0
 !
 
-findKeyOrNil:key  
-    "Look for the key in the receiver.  If it is found, return
-     the index of the association containing the key, otherwise
-     return the index of the first unused slot. Grow the receiver,
-     if key was not found, and no unused slots where present"
-
-    |index  "{ Class:SmallInteger }"
-     length startIndex probe|
-
-    length := keyArray basicSize.
-    startIndex := key hash \\ length + 1.
-
-    index := startIndex.
-    [true] whileTrue:[
-        probe := keyArray basicAt:index.
-        (probe isNil or: [key = probe]) ifTrue:[^ index].
-
-        index == length ifTrue:[
-            index := 1
-        ] ifFalse:[
-            index := index + 1
-        ].
-        index == startIndex ifTrue:[^ self grow findKeyOrNil:key]
-    ]
-!
-
-findKey:key ifAbsent:aBlock 
-    "Look for the key in the receiver.  If it is found, return
-     the index of the association containing the key, otherwise
-     return the value of evaluating aBlock."
-
-    |index  "{ Class:SmallInteger }"
-     length "{ Class:SmallInteger }"
-     startIndex
-     probe|
-
-    length := keyArray basicSize.
-    length < 10 ifTrue:[
-        "assuming, that for small dictionaries the overhead of hashing
-         is large ... maybe that proves wrong (if overhead of comparing
-         is high)"
-        ^ keyArray indexOf:key ifAbsent:aBlock
-    ].
-
-    startIndex := key hash \\ length + 1.
-
-    index := startIndex.
-    [true] whileTrue:[
-        probe := (keyArray basicAt:index).
-        key = probe ifTrue:[^ index].
-
-        index == length ifTrue:[
-            index := 1
-        ] ifFalse:[
-            index := index + 1
-        ].
-        ((probe isNil) or:[index == startIndex]) ifTrue:[
-            ^ aBlock value
-        ]
-    ]
-!
-
-grow
-    "change the number of element slots of the collection to a useful
-     new size"
-
-    self grow:(keyArray basicSize * 2)
-!
-
 grow:newSize
     "grow the receiver to make space for at least newSize elements.
      To do this, we have to rehash into the new arrays.
@@ -452,13 +308,12 @@
     n := self goodSizeFor:newSize.
     keyArray := self keyContainerOfSize:n.
     valueArray := Array new:n.
-    tally := 0.
 
     oldSize := oldKeyArray size.
     1 to:oldSize do:[:index |
         key := oldKeyArray basicAt:index.
         key notNil ifTrue:[
-            newIndex := self findKeyOrNil:key.
+            newIndex := self findNil:key.
             keyArray basicAt:newIndex put:key.
             valueArray basicAt:newIndex put:(oldValueArray basicAt:index).
         ]
@@ -482,7 +337,7 @@
     1 to:n do:[:index |
         key := oldKeyArray basicAt:index.
         key notNil ifTrue:[
-            newIndex := self findKeyOrNil:key.
+            newIndex := self findNil:key.
             keyArray basicAt:newIndex put:key.
             valueArray basicAt:newIndex put:(oldValueArray basicAt:index).
         ]
@@ -499,7 +354,7 @@
     index := startIndex.
     key := keyArray basicAt:index.
     [key notNil] whileTrue:[
-        i := self findKeyOrNil:key.
+        i := self findNil:key.
         i == index ifTrue:[
             ^ self
         ].
@@ -517,13 +372,6 @@
     ]
 ! !
 
-!Dictionary methodsFor: 'binary storage'!
-
-readBinaryContentsFrom: stream manager: manager
-    super readBinaryContentsFrom: stream manager: manager.
-    self rehash
-! !
-
 !Dictionary methodsFor:'printing & storing'!
 
 stringWith:aSelector
--- a/Dictionary.st	Sat Dec 11 01:42:02 1993 +0100
+++ b/Dictionary.st	Sat Dec 11 01:46:55 1993 +0100
@@ -10,8 +10,8 @@
  hereby transferred.
 "
 
-Collection subclass:#Dictionary
-         instanceVariableNames:'valueArray keyArray tally'
+Set subclass:#Dictionary
+         instanceVariableNames:'valueArray'
          classVariableNames:''
          poolDictionaries:''
          category:'Collections-Unordered'
@@ -26,54 +26,25 @@
 (The implementation uses two array to store the keys and values separately.)
 Searching for an element is done using a hash into the key array.
 
-$Header: /cvs/stx/stx/libbasic/Dictionary.st,v 1.5 1993-11-08 02:30:03 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Dictionary.st,v 1.6 1993-12-11 00:45:55 claus Exp $
 
 written jun 91 by claus
 rewritten 92 to use hash scheme
 '!
 
-!Dictionary class methodsFor:'instance creation'!
-
-new
-    "return a new empty Dictionary"
-
-    ^ self new:5
-!
-
-new:anInteger
-    "return a new empty Dictionary with space for anInteger elements"
-
-    ^ self basicNew setTally:anInteger
-! !
-
-!Dictionary methodsFor:'private'!
-
-keyContainerOfSize:n
-    "return a container for keys and values of size n.
-     Extracted to make life of WeakDictionary easier ..."
-
-    ^ Array new:n
-! !
-
 !Dictionary methodsFor:'testing'!
 
-size
-    "return the number of elements in the receiver"
-
-    ^ tally
-!
-
 includesKey:aKey
     "return true, if the argument, aKey is a key in the receiver"
 
-    ^ (self findKey:aKey ifAbsent:[0]) ~~ 0
+    ^ (self find:aKey ifAbsent:[0]) ~~ 0
 !
 
-isFixedSize
-    "return true if the receiver cannot grow - this will vanish once
-     Arrays and Strings learn how to grow ..."
+includes:aValue
+    "return true, if the argument, aValue is stoerd in the dictionary,
+     This is a slow search, since there is no fast reverse mapping"
 
-    ^ false
+    ^ valueArray includes:aValue
 ! !
 
 !Dictionary methodsFor:'accessing'!
@@ -86,7 +57,7 @@
     aKey isNil ifTrue:[
         self errorInvalidKey
     ] ifFalse:[
-        index := self findKey:aKey ifAbsent:[0].
+        index := self find:aKey ifAbsent:[0].
         index == 0 ifTrue:[^ self errorKeyNotFound].
         ^ valueArray basicAt:index
     ]
@@ -101,7 +72,7 @@
     aKey isNil ifTrue:[
         self errorInvalidKey
     ] ifFalse:[
-        index := self findKey:aKey ifAbsent:[0].
+        index := self find:aKey ifAbsent:[0].
         index == 0 ifTrue:[^ exceptionBlock value].
         ^ valueArray basicAt:index
     ]
@@ -158,9 +129,9 @@
      This is a slow access, since there is no fast reverse mapping"
 
     keyArray keysAndValuesDo:[:index :aKey |
-	aKey notNil ifTrue:[
-	    (valueArray at:index) = aValue ifTrue:[^ aKey].
-	].
+        aKey notNil ifTrue:[
+            (valueArray at:index) = aValue ifTrue:[^ aKey].
+        ].
     ].
     ^ exceptionBlock value
 ! !
@@ -192,31 +163,7 @@
     "remove the association under aKey from the collection.
      If it was not in the collection report an error"
 
-    |index "{ Class:SmallInteger }"
-     next  "{ Class:SmallInteger }" |
-
-    aKey isNil ifTrue:[
-        self errorInvalidKey
-    ] ifFalse:[
-        index := self findKey:aKey ifAbsent:[0].
-        (index == 0) ifTrue:[^ self errorNotFound].
-        valueArray basicAt:index put:nil.
-        keyArray basicAt:index put:nil.
-        tally := tally - 1.
-        tally == 0 ifTrue:[
-            self setTally:0
-        ] ifFalse:[
-            index == keyArray basicSize ifTrue:[
-                next := 1
-            ] ifFalse:[
-                next := index + 1.
-            ].
-            "redundant check to save a send sometimes"
-            (keyArray basicAt:next) notNil ifTrue:[
-                self rehashFrom:next.
-            ]
-        ]
-    ]
+    ^ self removeKey:aKey ifAbsent:[^ self errorNotFound]
 !
 
 removeKey:aKey ifAbsent:aBlock
@@ -229,7 +176,7 @@
     aKey isNil ifTrue:[
         self errorInvalidKey
     ] ifFalse:[
-        index := self findKey:aKey ifAbsent:[0].
+        index := self find:aKey ifAbsent:[0].
         index == 0 ifTrue:[^ aBlock value].
         valueArray basicAt:index put:nil.
         keyArray basicAt:index put:nil.
@@ -311,7 +258,7 @@
 
     newCollection := Bag new.
     self do:[:each |
-        newCollection add:each
+        newCollection add:(aBlock value:each)
     ].
     ^ newCollection
 !
@@ -333,28 +280,6 @@
 
 !Dictionary methodsFor:'private'!
 
-fullCheck
-    "check if dictionary is full, grow if so.
-     Definition of full is currently:'filled more than 70%'"
-
-    "grow if filled more than 70% "
-    tally > (keyArray basicSize * 7 // 10) ifTrue:[
-       self grow
-    ]
-!
-
-goodSizeFor:arg
-    "return a good array size for the given argument.
-     Returns the next prime after arg"
-
-    arg <= 7 ifTrue:[^ 7].
-    arg <= 131072 ifTrue:[
-           "2 4 8  16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072"
-        ^ #(7 7 11 17 37 67 131 257 521 1031 2053 4099 8209 16411 32771 65537 131101) at:(arg highBit)
-    ].
-    ^ arg bitOr:1
-!
-
 setTally:count
     "initialize the contents array (for at least count slots)
      and set tally to zero.
@@ -368,75 +293,6 @@
     tally := 0
 !
 
-findKeyOrNil:key  
-    "Look for the key in the receiver.  If it is found, return
-     the index of the association containing the key, otherwise
-     return the index of the first unused slot. Grow the receiver,
-     if key was not found, and no unused slots where present"
-
-    |index  "{ Class:SmallInteger }"
-     length startIndex probe|
-
-    length := keyArray basicSize.
-    startIndex := key hash \\ length + 1.
-
-    index := startIndex.
-    [true] whileTrue:[
-        probe := keyArray basicAt:index.
-        (probe isNil or: [key = probe]) ifTrue:[^ index].
-
-        index == length ifTrue:[
-            index := 1
-        ] ifFalse:[
-            index := index + 1
-        ].
-        index == startIndex ifTrue:[^ self grow findKeyOrNil:key]
-    ]
-!
-
-findKey:key ifAbsent:aBlock 
-    "Look for the key in the receiver.  If it is found, return
-     the index of the association containing the key, otherwise
-     return the value of evaluating aBlock."
-
-    |index  "{ Class:SmallInteger }"
-     length "{ Class:SmallInteger }"
-     startIndex
-     probe|
-
-    length := keyArray basicSize.
-    length < 10 ifTrue:[
-        "assuming, that for small dictionaries the overhead of hashing
-         is large ... maybe that proves wrong (if overhead of comparing
-         is high)"
-        ^ keyArray indexOf:key ifAbsent:aBlock
-    ].
-
-    startIndex := key hash \\ length + 1.
-
-    index := startIndex.
-    [true] whileTrue:[
-        probe := (keyArray basicAt:index).
-        key = probe ifTrue:[^ index].
-
-        index == length ifTrue:[
-            index := 1
-        ] ifFalse:[
-            index := index + 1
-        ].
-        ((probe isNil) or:[index == startIndex]) ifTrue:[
-            ^ aBlock value
-        ]
-    ]
-!
-
-grow
-    "change the number of element slots of the collection to a useful
-     new size"
-
-    self grow:(keyArray basicSize * 2)
-!
-
 grow:newSize
     "grow the receiver to make space for at least newSize elements.
      To do this, we have to rehash into the new arrays.
@@ -452,13 +308,12 @@
     n := self goodSizeFor:newSize.
     keyArray := self keyContainerOfSize:n.
     valueArray := Array new:n.
-    tally := 0.
 
     oldSize := oldKeyArray size.
     1 to:oldSize do:[:index |
         key := oldKeyArray basicAt:index.
         key notNil ifTrue:[
-            newIndex := self findKeyOrNil:key.
+            newIndex := self findNil:key.
             keyArray basicAt:newIndex put:key.
             valueArray basicAt:newIndex put:(oldValueArray basicAt:index).
         ]
@@ -482,7 +337,7 @@
     1 to:n do:[:index |
         key := oldKeyArray basicAt:index.
         key notNil ifTrue:[
-            newIndex := self findKeyOrNil:key.
+            newIndex := self findNil:key.
             keyArray basicAt:newIndex put:key.
             valueArray basicAt:newIndex put:(oldValueArray basicAt:index).
         ]
@@ -499,7 +354,7 @@
     index := startIndex.
     key := keyArray basicAt:index.
     [key notNil] whileTrue:[
-        i := self findKeyOrNil:key.
+        i := self findNil:key.
         i == index ifTrue:[
             ^ self
         ].
@@ -517,13 +372,6 @@
     ]
 ! !
 
-!Dictionary methodsFor: 'binary storage'!
-
-readBinaryContentsFrom: stream manager: manager
-    super readBinaryContentsFrom: stream manager: manager.
-    self rehash
-! !
-
 !Dictionary methodsFor:'printing & storing'!
 
 stringWith:aSelector
--- a/Exception.st	Sat Dec 11 01:42:02 1993 +0100
+++ b/Exception.st	Sat Dec 11 01:46:55 1993 +0100
@@ -13,7 +13,7 @@
 Object subclass:#Exception
          instanceVariableNames:'signal parameter suspendedContext handlerContext
                                 resumeBlock rejectBlock'
-         classVariableNames:''
+         classVariableNames:'EmergencyHandler'
          poolDictionaries:''
          category:'Kernel-Exceptions'
 !
@@ -23,6 +23,13 @@
 COPYRIGHT (c) 1993 by Claus Gittinger
               All Rights Reserved
 
+$Header: /cvs/stx/stx/libbasic/Exception.st,v 1.4 1993-12-11 00:46:23 claus Exp $
+'!
+
+!Exception class methodsFor:'documentation'!
+
+documentation
+"
 Instances of Exception are passed to a Signal handling block as argument.
 The handler block may perform various actions by sending corresponding messages
 to the exception handler. The following actions are possible:
@@ -41,9 +48,30 @@
 
 Via the Exception object, the handler can also query the state of execution,
 where the Signal was raised.
+"
+! !
 
-$Header: /cvs/stx/stx/libbasic/Exception.st,v 1.3 1993-10-13 00:15:42 claus Exp $
-'!
+!Exception class methodsFor:'initialization'!
+
+initialize
+    EmergencyHandler := [:ex :con |
+        (ex signal) error:('unhandled exception: ' , ex signal notifierString)
+    ]
+! !
+
+!Exception class methodsFor:'defaults'!
+
+emergencyHandler
+    "return the handler used for unhandled exceptions"
+
+    ^ EmergencyHandler
+!
+
+emergencyHandler:aTwoArgBlock
+    "set the handler used for unhandled exceptions"
+
+    EmergencyHandler := aTwoArgBlock
+! !
 
 !Exception methodsFor:'accessing'!
 
--- a/ExtStream.st	Sat Dec 11 01:42:02 1993 +0100
+++ b/ExtStream.st	Sat Dec 11 01:46:55 1993 +0100
@@ -11,7 +11,7 @@
 "
 
 ReadWriteStream subclass:#ExternalStream
-       instanceVariableNames:'filePointer mode buffered binary'
+       instanceVariableNames:'filePointer mode buffered binary useCRLF'
        classVariableNames:'lobby'
        poolDictionaries:''
        category:'Streams-External'
@@ -29,7 +29,7 @@
 In text-mode, the elements read/written are characters; while in binary-mode the basic
 elements are bytes which read/write as SmallIntegers in the range 0..255.
 
-$Header: /cvs/stx/stx/libbasic/Attic/ExtStream.st,v 1.5 1993-11-08 02:30:14 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Attic/ExtStream.st,v 1.6 1993-12-11 00:46:55 claus Exp $
 
 written 88 by claus
 '!
@@ -73,7 +73,7 @@
     |newStream|
 
     newStream := self basicNew.
-    newStream text.
+    newStream text; buffered:true; useCRLF:false.
     ^ newStream
 ! !
 
@@ -90,7 +90,15 @@
 
 %{  /* NOCONTEXT */
 
-    fclose(MKFD(_INST(filePointer)));
+    if (_INST(filePointer) != nil) {
+        int savInt;
+        extern int _immediateInterrupt;
+
+        savInt = _immediateInterrupt;
+        _immediateInterrupt = 1;
+        fclose(MKFD(_INST(filePointer)));
+        _immediateInterrupt = savInt;
+    }
 %}
 ! !
 
@@ -214,6 +222,12 @@
     buffered := aBoolean
 !
 
+useCRLF:aBoolean
+    "turn on or off CRLF sending (instead of LF only) - default is off"
+
+    useCRLF := aBoolean
+!
+
 binary
     "switch to binary mode - default is text"
 
@@ -361,9 +375,9 @@
             f = MKFD(_INST(filePointer));
             savInt = _immediateInterrupt;
             _immediateInterrupt = 1;
-	    do {
+            do {
                 ret = ioctl(fileno(f), ioNum);
-	    } while ((ret < 0) && (errno == EINTR));
+            } while ((ret < 0) && (errno == EINTR));
             _immediateInterrupt = savInt;
             if (ret >= 0) {
                 RETURN ( _MKSMALLINT(ret) );
@@ -403,13 +417,13 @@
             ioNum = _intVal(ioctlNumber);
             savInt = _immediateInterrupt;
             _immediateInterrupt = 1;
-	    do {
+            do {
                 if (isStructure == true) {
                     ret = ioctl(fileno(f), ioNum, _ByteArrayInstPtr(arg)->ba_element);
                 } else {
                     ret = ioctl(fileno(f), ioNum, _intVal(arg));
                 }
-	    } while ((ret < 0) && (errno == EINTR));
+            } while ((ret < 0) && (errno == EINTR));
             _immediateInterrupt = savInt;
             if (ret >= 0) {
                 RETURN ( _MKSMALLINT(ret) );
@@ -439,31 +453,29 @@
     extern errno;
     extern int _immediateInterrupt;
 
-    if (_INST(filePointer) != nil) {
-        if (_INST(mode) != _writeonly) {
-            f = MKFD(_INST(filePointer));
-            savInt = _immediateInterrupt;
-            _immediateInterrupt = 1;
-	    do {
-                if (_INST(buffered) == false) {
-                    cnt = read(fileno(f), &byte, 1);
-                } else {
-                    if (_INST(mode) == _readwrite)
-                        fseek(f, 0L, 1); /* needed in stdio */
-                    cnt = fread(&byte, 1, 1, f);
-                }
-	    } while ((cnt < 0) && (errno == EINTR));
-            _immediateInterrupt = savInt;
-            if (cnt == 1) {
-                if (_INST(position) != nil)
-                    _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 1);
-                RETURN ( _MKSMALLINT(byte) );
+    if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
+        f = MKFD(_INST(filePointer));
+        savInt = _immediateInterrupt;
+        _immediateInterrupt = 1;
+        do {
+            if (_INST(buffered) == false) {
+                cnt = read(fileno(f), &byte, 1);
+            } else {
+                if (_INST(mode) == _readwrite)
+                    fseek(f, 0L, 1); /* needed in stdio */
+                cnt = fread(&byte, 1, 1, f);
             }
-            if (cnt < 0) {
-                ErrorNumber = _MKSMALLINT(errno);
-            }
-            RETURN ( nil );
+        } while ((cnt < 0) && (errno == EINTR));
+        _immediateInterrupt = savInt;
+        if (cnt == 1) {
+            if (_INST(position) != nil)
+                _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 1);
+            RETURN ( _MKSMALLINT(byte) );
         }
+        if (cnt < 0) {
+            ErrorNumber = _MKSMALLINT(errno);
+        }
+        RETURN ( nil );
     }
 %}
 .
@@ -497,36 +509,34 @@
     OBJ pos;
     extern int _immediateInterrupt;
 
-    if (_INST(filePointer) != nil) {
-        if (_INST(mode) != _writeonly) {
-            if (_isSmallInteger(count) && _isSmallInteger(start)) {
-                cnt = _intVal(count);
-                offs = _intVal(start) - 1;
-                f = MKFD(_INST(filePointer));
-                objSize = _Size(anObject) - OHDR_SIZE;
-                if ((offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs))) {
-                    cp = (char *)_InstPtr(anObject) + OHDR_SIZE + offs;
-                    savInt = _immediateInterrupt;
-                    _immediateInterrupt = 1;
-		    do {
-                        if (_INST(buffered) == false) {
-                            cnt = read(fileno(f), cp, cnt);
-                        } else {
-                            if (_INST(mode) == _readwrite)
-                                fseek(f, 0L, 1); /* needed in stdio */
-                            cnt = fread(cp, 1, cnt, f);
-                        }
-		    } while ((cnt < 0) && (errno == EINTR));
-                    _immediateInterrupt = savInt;
-                    if (cnt >= 0) {
-                        pos = _INST(position);
-                        if (pos != nil)
-                            _INST(position) = _MKSMALLINT(_intVal(pos) + cnt);
-                        RETURN ( _MKSMALLINT(cnt) );
+    if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
+        if (_isSmallInteger(count) && _isSmallInteger(start)) {
+            cnt = _intVal(count);
+            offs = _intVal(start) - 1;
+            f = MKFD(_INST(filePointer));
+            objSize = _Size(anObject) - OHDR_SIZE;
+            if ((offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs))) {
+                cp = (char *)_InstPtr(anObject) + OHDR_SIZE + offs;
+                savInt = _immediateInterrupt;
+                _immediateInterrupt = 1;
+                do {
+                    if (_INST(buffered) == false) {
+                        cnt = read(fileno(f), cp, cnt);
+                    } else {
+                        if (_INST(mode) == _readwrite)
+                            fseek(f, 0L, 1); /* needed in stdio */
+                        cnt = fread(cp, 1, cnt, f);
                     }
-                    ErrorNumber = _MKSMALLINT(errno);
-                    RETURN ( nil );
+                } while ((cnt < 0) && (errno == EINTR));
+                _immediateInterrupt = savInt;
+                if (cnt >= 0) {
+                    pos = _INST(position);
+                    if (pos != nil)
+                        _INST(position) = _MKSMALLINT(_intVal(pos) + cnt);
+                    RETURN ( _MKSMALLINT(cnt) );
                 }
+                ErrorNumber = _MKSMALLINT(errno);
+                RETURN ( nil );
             }
         }
     }
@@ -550,56 +560,54 @@
     int savInt;
 
     if (_INST(binary) == true) {
-        if (_INST(filePointer) != nil) {
-            if (_INST(mode) != _writeonly) {
-                FILE *f;
-                unsigned char hi, low;
-		int cnt;
+        if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
+            FILE *f;
+            unsigned char hi, low;
+            int cnt;
 
-                savInt = _immediateInterrupt;
-                _immediateInterrupt = 1;
-                f = MKFD(_INST(filePointer));
-		do {
-                    if (_INST(buffered) == false) {
-                        cnt = read(fileno(f), &hi, 1);
-                    } else {
-                        if (_INST(mode) == _readwrite)
-                            fseek(f, 0L, 1); /* needed in stdio */
-                        cnt = fread(&hi, 1, 1, f);
-                    }
-		} while ((cnt < 0) && (errno == EINTR));
-
-		if (cnt < 0) {
-                    _immediateInterrupt = savInt;
-                    RETURN ( nil );
+            savInt = _immediateInterrupt;
+            _immediateInterrupt = 1;
+            f = MKFD(_INST(filePointer));
+            do {
+                if (_INST(buffered) == false) {
+                    cnt = read(fileno(f), &hi, 1);
+                } else {
+                    if (_INST(mode) == _readwrite)
+                        fseek(f, 0L, 1); /* needed in stdio */
+                    cnt = fread(&hi, 1, 1, f);
                 }
-                do {
-                    if (_INST(buffered) == false) {
-                        cnt = read(fileno(f), &low, 1);
-                    } else {
-                        if (_INST(mode) == _readwrite)
-                            fseek(f, 0L, 1); /* needed in stdio */
-                        cnt = fread(&low, 1, 1, f);
-                    }
-                } while ((cnt < 0) && (errno == EINTR));
+            } while ((cnt < 0) && (errno == EINTR));
 
+            if (cnt < 0) {
                 _immediateInterrupt = savInt;
-                if (cnt < 0) {
-                    if (_INST(position) != nil) {
-                        _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 1);
-                    }
-                    RETURN ( _MKSMALLINT(hi & 0xFF) );
+                RETURN ( nil );
+            }
+            do {
+                if (_INST(buffered) == false) {
+                    cnt = read(fileno(f), &low, 1);
+                } else {
+                    if (_INST(mode) == _readwrite)
+                        fseek(f, 0L, 1); /* needed in stdio */
+                    cnt = fread(&low, 1, 1, f);
                 }
+            } while ((cnt < 0) && (errno == EINTR));
+
+            _immediateInterrupt = savInt;
+            if (cnt < 0) {
                 if (_INST(position) != nil) {
-                    _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 2);
+                    _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 1);
                 }
-                RETURN ( _MKSMALLINT(((hi & 0xFF)<<8) | (low & 0xFF)) );
+                RETURN ( _MKSMALLINT(hi & 0xFF) );
             }
+            if (_INST(position) != nil) {
+                _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 2);
+            }
+            RETURN ( _MKSMALLINT(((hi & 0xFF)<<8) | (low & 0xFF)) );
         }
     }
 %}
 .
-%{
+%{  /* STACK: 2000 */
     FILE *f;
     int len;
     char buffer[1024];
@@ -607,54 +615,52 @@
     int cnt = 0;
     extern int _immediateInterrupt;
 
-    if (_INST(filePointer) != nil) {
-        if (_INST(mode) != _writeonly) {
-            f = MKFD(_INST(filePointer));
-            savInt = _immediateInterrupt;
-            _immediateInterrupt = 1;
+    if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
+        f = MKFD(_INST(filePointer));
+        savInt = _immediateInterrupt;
+        _immediateInterrupt = 1;
 
-            /* text-mode */
-            for (;;) {
-                ch = getc(f);
-                cnt++;
+        /* text-mode */
+        for (;;) {
+            ch = getc(f);
+            cnt++;
 
-                if (ch >= ' ') break;
-                if ((ch != ' ') && (ch != '\t') && (ch != '\r')
-                 && (ch != '\n') && (ch != 0x0b)) break;
-            }
-            ungetc(ch, f);
-            cnt--;
+            if (ch >= ' ') break;
+            if ((ch != ' ') && (ch != '\t') && (ch != '\r')
+             && (ch != '\n') && (ch != 0x0b)) break;
+        }
+        ungetc(ch, f);
+        cnt--;
 
-            len = 0;
-            for (;;) {
-                ch = getc(f);
-                if (ch == EOF)
-                    break;
-                ch &= 0xFF;
-                if (! (((ch >= 'a') && (ch <= 'z')) ||
-                       ((ch >= 'A') && (ch <= 'Z')) ||
-                       ((ch >= '0') && (ch <= '9')))) {
-                    ungetc(ch, f);
-                    break;
-                }
-                cnt++;
-                buffer[len++] = ch;
-                if (len >= sizeof(buffer)-1) {
-                    /* emergency */
-                    break;
-                }
+        len = 0;
+        for (;;) {
+            ch = getc(f);
+            if (ch == EOF)
+                break;
+            ch &= 0xFF;
+            if (! (((ch >= 'a') && (ch <= 'z')) ||
+                   ((ch >= 'A') && (ch <= 'Z')) ||
+                   ((ch >= '0') && (ch <= '9')))) {
+                ungetc(ch, f);
+                break;
             }
-            _immediateInterrupt = savInt;
-
-            if (_INST(position) != nil) {
-                _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + cnt);
+            cnt++;
+            buffer[len++] = ch;
+            if (len >= sizeof(buffer)-1) {
+                /* emergency */
+                break;
             }
-            buffer[len] = '\0';
-            if (len != 0) {
-                RETURN ( _MKSTRING(buffer COMMA_CON) );
-            }
-            RETURN ( nil );
+        }
+        _immediateInterrupt = savInt;
+
+        if (_INST(position) != nil) {
+            _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + cnt);
         }
+        buffer[len] = '\0';
+        if (len != 0) {
+            RETURN ( _MKSTRING(buffer COMMA_CON) );
+        }
+        RETURN ( nil );
     }
 %}
 .
@@ -674,35 +680,33 @@
     extern int _immediateInterrupt;
 
     if (_INST(binary) == true) {
-        if (_INST(filePointer) != nil) {
-            if (_INST(mode) != _writeonly) {
-                FILE *f;
-                int first, second;
-                short value;
+        if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
+            FILE *f;
+            int first, second;
+            short value;
 
-                savInt = _immediateInterrupt;
-                _immediateInterrupt = 1;
+            savInt = _immediateInterrupt;
+            _immediateInterrupt = 1;
 
-                f = MKFD(_INST(filePointer));
-                first = getc(f);
-                if (first == EOF) {
-                    _immediateInterrupt = savInt;
-                    RETURN ( nil );
-                }
-                second = getc(f);
+            f = MKFD(_INST(filePointer));
+            first = getc(f);
+            if (first == EOF) {
                 _immediateInterrupt = savInt;
+                RETURN ( nil );
+            }
+            second = getc(f);
+            _immediateInterrupt = savInt;
 
-                if (second == EOF) {
-                    RETURN ( nil );
-                }
-                if (_INST(position) != nil) {
-                    _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 2);
-                }
-                if (msbFlag == true) {
-                    RETURN ( _MKSMALLINT(((first & 0xFF)<<8) | (second & 0xFF)) );
-                }
-                RETURN ( _MKSMALLINT(((second & 0xFF)<<8) | (first & 0xFF)) );
+            if (second == EOF) {
+                RETURN ( nil );
             }
+            if (_INST(position) != nil) {
+                _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 2);
+            }
+            if (msbFlag == true) {
+                RETURN ( _MKSMALLINT(((first & 0xFF)<<8) | (second & 0xFF)) );
+            }
+            RETURN ( _MKSMALLINT(((second & 0xFF)<<8) | (first & 0xFF)) );
         }
     }
 %}
@@ -725,33 +729,31 @@
     extern int _immediateInterrupt;
 
     if (_INST(binary) == true) {
-        if (_INST(filePointer) != nil) {
-            if (_INST(mode) != _writeonly) {
-                FILE *f;
-                int first, second;
+        if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
+            FILE *f;
+            int first, second;
 
-                savInt = _immediateInterrupt;
-                _immediateInterrupt = 1;
-                f = MKFD(_INST(filePointer));
-                first = getc(f);
-                if (first == EOF) {
-                    _immediateInterrupt = savInt;
-                    RETURN ( nil );
-                }
-                second = getc(f);
+            savInt = _immediateInterrupt;
+            _immediateInterrupt = 1;
+            f = MKFD(_INST(filePointer));
+            first = getc(f);
+            if (first == EOF) {
                 _immediateInterrupt = savInt;
+                RETURN ( nil );
+            }
+            second = getc(f);
+            _immediateInterrupt = savInt;
 
-                if (second == EOF) {
-                    RETURN ( nil );
-                }
-                if (_INST(position) != nil) {
-                    _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 2);
-                }
-                if (msbFlag == true) {
-                    RETURN ( _MKSMALLINT(((first & 0xFF)<<8) | (second & 0xFF)) );
-                }
-                RETURN ( _MKSMALLINT(((second & 0xFF)<<8) | (first & 0xFF)) );
+            if (second == EOF) {
+                RETURN ( nil );
             }
+            if (_INST(position) != nil) {
+                _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 2);
+            }
+            if (msbFlag == true) {
+                RETURN ( _MKSMALLINT(((first & 0xFF)<<8) | (second & 0xFF)) );
+            }
+            RETURN ( _MKSMALLINT(((second & 0xFF)<<8) | (first & 0xFF)) );
         }
     }
 %}
@@ -775,54 +777,52 @@
     extern int _immediateInterrupt;
 
     if (_INST(binary) == true) {
-        if (_INST(filePointer) != nil) {
-            if (_INST(mode) != _writeonly) {
-                FILE *f;
-                int first, second, third, fourth;
-                int value;
+        if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
+            FILE *f;
+            int first, second, third, fourth;
+            int value;
 
-                savInt = _immediateInterrupt;
-                _immediateInterrupt = 1;
-                f = MKFD(_INST(filePointer));
-                first = getc(f);
-                if (first == EOF) {
-                    _immediateInterrupt = savInt;
-                    RETURN ( nil );
-                }
-                second = getc(f);
-                if (second == EOF) {
-                    _immediateInterrupt = savInt;
-                    RETURN ( nil );
-                }
-                third = getc(f);
-                if (third == EOF) {
-                    _immediateInterrupt = savInt;
-                    RETURN ( nil );
-                }
-                fourth = getc(f);
+            savInt = _immediateInterrupt;
+            _immediateInterrupt = 1;
+            f = MKFD(_INST(filePointer));
+            first = getc(f);
+            if (first == EOF) {
+                _immediateInterrupt = savInt;
+                RETURN ( nil );
+            }
+            second = getc(f);
+            if (second == EOF) {
+                _immediateInterrupt = savInt;
+                RETURN ( nil );
+            }
+            third = getc(f);
+            if (third == EOF) {
                 _immediateInterrupt = savInt;
-                if (fourth == EOF) {
-                    RETURN ( nil );
-                }
-                if (_INST(position) != nil) {
-                    _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 4);
-                }
-                if (msbFlag == true) {
-                    value = ((first & 0xFF) << 24)
-                            | ((second & 0xFF) << 16)
-                            | ((third & 0xFF) << 8)
-                            | (fourth & 0xFF);
-                } else {
-                    value = ((fourth & 0xFF) << 24)
-                            | ((third & 0xFF) << 16)
-                            | ((second & 0xFF) << 8)
-                            | (first & 0xFF);
-                }
-                if ((value >= _MIN_INT) && (value <= _MAX_INT)) {
-                    RETURN ( _MKSMALLINT(value));
-                }
-                RETURN ( _makeLarge(value) );
+                RETURN ( nil );
+            }
+            fourth = getc(f);
+            _immediateInterrupt = savInt;
+            if (fourth == EOF) {
+                RETURN ( nil );
+            }
+            if (_INST(position) != nil) {
+                _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 4);
             }
+            if (msbFlag == true) {
+                value = ((first & 0xFF) << 24)
+                        | ((second & 0xFF) << 16)
+                        | ((third & 0xFF) << 8)
+                        | (fourth & 0xFF);
+            } else {
+                value = ((fourth & 0xFF) << 24)
+                        | ((third & 0xFF) << 16)
+                        | ((second & 0xFF) << 8)
+                        | (first & 0xFF);
+            }
+            if ((value >= _MIN_INT) && (value <= _MAX_INT)) {
+                RETURN ( _MKSMALLINT(value));
+            }
+            RETURN ( _makeLarge(value) );
         }
     }
 %}
@@ -847,54 +847,52 @@
     extern OBJ _makeULarge();
 
     if (_INST(binary) == true) {
-        if (_INST(filePointer) != nil) {
-            if (_INST(mode) != _writeonly) {
-                FILE *f;
-                int first, second, third, fourth;
-                unsigned int value;
+        if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
+            FILE *f;
+            int first, second, third, fourth;
+            unsigned int value;
 
-                savInt = _immediateInterrupt;
-                _immediateInterrupt = 1;
-                f = MKFD(_INST(filePointer));
-                first = getc(f);
-                if (first == EOF) {
-                    _immediateInterrupt = savInt;
-                    RETURN ( nil );
-                }
-                second = getc(f);
-                if (second == EOF) {
-                    _immediateInterrupt = savInt;
-                    RETURN ( nil );
-                }
-                third = getc(f);
-                if (third == EOF) {
-                    _immediateInterrupt = savInt;
-                    RETURN ( nil );
-                }
-                fourth = getc(f);
+            savInt = _immediateInterrupt;
+            _immediateInterrupt = 1;
+            f = MKFD(_INST(filePointer));
+            first = getc(f);
+            if (first == EOF) {
+                _immediateInterrupt = savInt;
+                RETURN ( nil );
+            }
+            second = getc(f);
+            if (second == EOF) {
+                _immediateInterrupt = savInt;
+                RETURN ( nil );
+            }
+            third = getc(f);
+            if (third == EOF) {
                 _immediateInterrupt = savInt;
-                if (fourth == EOF) {
-                    RETURN ( nil );
-                }
-                if (_INST(position) != nil) {
-                    _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 4);
-                }
-                if (msbFlag == true) {
-                    value = ((first & 0xFF) << 24)
-                            | ((second & 0xFF) << 16)
-                            | ((third & 0xFF) << 8)
-                            | (fourth & 0xFF);
-                } else {
-                    value = ((fourth & 0xFF) << 24)
-                            | ((third & 0xFF) << 16)
-                            | ((second & 0xFF) << 8)
-                            | (first & 0xFF);
-                }
-                if (value <= _MAX_INT) {
-                    RETURN ( _MKSMALLINT(value));
-                }
-                RETURN ( _makeULarge(value) );
+                RETURN ( nil );
+            }
+            fourth = getc(f);
+            _immediateInterrupt = savInt;
+            if (fourth == EOF) {
+                RETURN ( nil );
+            }
+            if (_INST(position) != nil) {
+                _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 4);
             }
+            if (msbFlag == true) {
+                value = ((first & 0xFF) << 24)
+                        | ((second & 0xFF) << 16)
+                        | ((third & 0xFF) << 8)
+                        | (fourth & 0xFF);
+            } else {
+                value = ((fourth & 0xFF) << 24)
+                        | ((third & 0xFF) << 16)
+                        | ((second & 0xFF) << 8)
+                        | (first & 0xFF);
+            }
+            if (value <= _MAX_INT) {
+                RETURN ( _MKSMALLINT(value));
+            }
+            RETURN ( _makeULarge(value) );
         }
     }
 %}
@@ -929,30 +927,36 @@
     int cnt, savInt;
     extern int _immediateInterrupt;
 
-    if (_INST(filePointer) != nil) {
-        if (_INST(mode) != _readonly) {
-            if (_isSmallInteger(aByteValue)) {
-                c = _intVal(aByteValue);
-                f = MKFD(_INST(filePointer));
-                savInt = _immediateInterrupt;
-                _immediateInterrupt = 1;
+    if ((_INST(filePointer) != nil) && (_INST(mode) != _readonly)) {
+        if (_isSmallInteger(aByteValue)) {
+            c = _intVal(aByteValue);
+            f = MKFD(_INST(filePointer));
+            savInt = _immediateInterrupt;
+            _immediateInterrupt = 1;
+#ifdef OLD
+            if (_INST(buffered) == false) {
+                cnt = write(fileno(f), &c, 1);
+            } else 
+#endif
+            {
+                if (_INST(mode) == _readwrite)
+                    fseek(f, 0L, 1); /* needed in stdio */
+                cnt = fwrite(&c, 1, 1, f);
+#ifndef OLD
                 if (_INST(buffered) == false) {
-                    cnt = write(fileno(f), &c, 1);
-                } else {
-                    if (_INST(mode) == _readwrite)
-                        fseek(f, 0L, 1); /* needed in stdio */
-                    cnt = fwrite(&c, 1, 1, f);
+                    fflush(f);
                 }
-                _immediateInterrupt = savInt;
-                if (cnt == 1) {
-                    pos = _INST(position);
-                    if (pos != nil)
-                        _INST(position) = _MKSMALLINT(_intVal(pos) + 1);
-                    RETURN ( self );
-                }
-                ErrorNumber = _MKSMALLINT(errno);
-                RETURN (nil);
+#endif
             }
+            _immediateInterrupt = savInt;
+            if (cnt == 1) {
+                pos = _INST(position);
+                if (pos != nil)
+                    _INST(position) = _MKSMALLINT(_intVal(pos) + 1);
+                RETURN ( self );
+            }
+            ErrorNumber = _MKSMALLINT(errno);
+            RETURN (nil);
         }
     }
 %}
@@ -999,13 +1003,21 @@
                     cp = (char *)_InstPtr(anObject) + OHDR_SIZE + offs;
                     savInt = _immediateInterrupt;
                     _immediateInterrupt = 1;
+#ifdef OLD
                     if (_INST(buffered) == false) {
                         cnt = write(fileno(f), cp, cnt);
-                    } else {
+                    } else
+#endif
+                    {
                         if (_INST(mode) == _readwrite)
                             fseek(f, 0L, 1); /* needed in stdio */
                         cnt = fwrite(cp, 1, cnt, f);
                     }
+#ifndef OLD
+                    if (_INST(buffered) == false) {
+                        fflush(f);
+                    }
+#endif
                     _immediateInterrupt = savInt;
                     if (cnt >= 0) {
                         pos = _INST(position);
@@ -1067,13 +1079,21 @@
                     f = MKFD(_INST(filePointer));
                     savInt = _immediateInterrupt;
                     _immediateInterrupt = 1;
+#ifdef OLD
                     if (_INST(buffered) == false) {
                         cnt = write(fileno(f), bytes, 2);
-                    } else {
+                    } else 
+#endif
+                    {
                         if (_INST(mode) == _readwrite)
                             fseek(f, 0L, 1); /* needed in stdio */
                         cnt = fwrite(bytes, 1, 2, f);
                     }
+#ifndef OLD
+                    if (_INST(buffered) == false) {
+                        fflush(f);
+                    }
+#endif
                     _immediateInterrupt = savInt;
                     if (cnt == 2) {
                         if (_INST(position) != nil) {
@@ -1131,11 +1151,19 @@
                     f = MKFD(_INST(filePointer));
                     savInt = _immediateInterrupt;
                     _immediateInterrupt = 1;
+#ifdef OLD
                     if (_INST(buffered) == false) {
                         cnt = write(fileno(f), bytes, 4);
-                    } else {
+                    } else 
+#endif
+                    {
                         cnt = fwrite(bytes, 1, 4, f);
                     }
+#ifndef OLD
+                    if (_INST(buffered) == false) {
+                        fflush(f);
+                    }
+#endif
                     _immediateInterrupt = savInt;
                     if (cnt == 4) {
                         if (_INST(position) != nil) {
@@ -1155,7 +1183,7 @@
     (mode == #readonly) ifTrue:[^ self errorReadOnly].
     binary ifFalse:[^ self errorNotBinary].
 
-    (aNumber isKindOf:Integer) ifTrue:[
+    aNumber isInteger ifTrue:[
         (self nextShortPut:(aNumber // 16r10000) MSB:msbFlag) isNil ifTrue:[^ nil].
         ^ self nextShortPut:(aNumber \\ 16r10000) MSB:msbFlag.
     ].
@@ -1179,7 +1207,10 @@
 
     if (_INST(filePointer) != nil) {
         if (_INST(mode) != _writeonly) {
-            if (_INST(buffered) == true) {
+#ifdef OLD
+            if (_INST(buffered) == true) 
+#endif
+            {
                 f = MKFD(_INST(filePointer));
                 savInt = _immediateInterrupt;
                 _immediateInterrupt = 1;
@@ -1223,18 +1254,21 @@
             f = MKFD(_INST(filePointer));
             savInt = _immediateInterrupt;
             _immediateInterrupt = 1;
-	    do {
+            do {
+#ifdef OLD
                 if (_INST(buffered) == false) {
                     if (read(fileno(f), &ch, 1) != 1)
                         c = EOF;
                     else
                         c = ch;
-                } else {
+                } else 
+#endif
+                {
                     if (_INST(mode) == _readwrite)
                         fseek(f, 0L, 1); /* needed in stdio */
                     c = getc(f);
                 }
-	    } while ((c < 0) && (errno == EINTR));
+            } while ((c < 0) && (errno == EINTR));
 
             _immediateInterrupt = savInt;
             if (c != EOF) {
@@ -1283,12 +1317,10 @@
 
 %{  /* NOCONTEXT */
 
-    FILE *f;
-
     if (_INST(filePointer) != nil) {
         if (_INST(mode) != _readonly) {
             if (_INST(buffered) == true) {
-                fflush(f);
+                fflush( MKFD(_INST(filePointer)) );
             }
         }
     }
@@ -1319,13 +1351,21 @@
 
                     savInt = _immediateInterrupt;
                     _immediateInterrupt = 1;
+#ifdef OLD
                     if (_INST(buffered) == false) {
                         cnt = write(fileno(f), &c, 1);
-                    } else { 
+                    } else 
+#endif
+                    { 
                         if (_INST(mode) == _readwrite)
                             fseek(f, 0L, 1); /* needed in stdio */
                         cnt = fwrite(&c, 1, 1, f);
                     }
+#ifndef OLD
+                    if (_INST(buffered) == false) {
+                        fflush(f);
+                    }
+#endif
                     _immediateInterrupt = savInt;
                     if (cnt == 1) {
                         pos = _INST(position);
@@ -1385,13 +1425,21 @@
 
             savInt = _immediateInterrupt;
             _immediateInterrupt = 1;
+#ifdef OLD
             if (_INST(buffered) == false) {
                 cnt = write(fileno(f), cp, len);
-            } else { 
+            } else 
+#endif
+            { 
                 if (_INST(mode) == _readwrite)
                     fseek(f, 0L, 1); /* needed in stdio */
                 cnt = fwrite(cp, 1, len, f);
             }
+#ifndef OLD
+                    if (_INST(buffered) == false) {
+                        fflush(f);
+                    }
+#endif
             _immediateInterrupt = savInt;
             if (cnt == len) {
                 pos = _INST(position);
@@ -1454,13 +1502,21 @@
                 savInt = _immediateInterrupt;
                 _immediateInterrupt = 1;
                 len = index2 - index1 + 1;
+#ifdef OLD
                 if (_INST(buffered) == false) {
                     cnt = write(fileno(f), cp + index1 - 1, len);
-                } else { 
+                } else 
+#endif
+                { 
                     if (_INST(mode) == _readwrite)
                         fseek(f, 0L, 1); /* needed in stdio */
                     cnt = fwrite(cp + index1 - 1, 1, len, f);
                 }
+#ifndef OLD
+                    if (_INST(buffered) == false) {
+                        fflush(f);
+                    }
+#endif
                 _immediateInterrupt = savInt;
                 if (cnt == len) {
                     if (_INST(position) != nil) {
@@ -1500,13 +1556,21 @@
 
             savInt = _immediateInterrupt;
             _immediateInterrupt = 1;
+#ifdef OLD
             if (_INST(buffered) == false) {
                 cnt = write(fileno(f), "\n", 1);
-            } else { 
+            } else 
+#endif
+            { 
                 if (_INST(mode) == _readwrite)
                     fseek(f, 0L, 1); /* needed in stdio */
                 cnt = fwrite("\n", 1, 1, f);
             }
+#ifndef OLD
+                    if (_INST(buffered) == false) {
+                        fflush(f);
+                    }
+#endif
             _immediateInterrupt = savInt;
             if (cnt == 1) {
                 if (_INST(position) != nil) {
@@ -1533,32 +1597,42 @@
      If the previous-to-last character is a cr, this is also removed,
      so its possible to read alien (i.e. ms-dos) text as well."
 
-%{  /* NOCONTEXT */
+%{  /* STACK:2000 */
 
     FILE *f;
     int len;
-    char buffer[1024*8];
+    char buffer[1024];
     extern int _immediateInterrupt;
     int savInt;
     char *rslt;
     extern errno;
+    int fd;
 
-    if ((_INST(filePointer) != nil) && (_INST(filePointer) != _writeonly)) {
+    if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
         if (_INST(binary) != true) {
             f = MKFD(_INST(filePointer));
             savInt = _immediateInterrupt;
             _immediateInterrupt = 1;
             buffer[0] = 0;
+
+#ifndef OLD
+            if (_INST(mode) == _readwrite)
+                fseek(f, 0L, 1); /* needed in stdio */
+#endif
+#ifdef OLD
             if (_INST(buffered) == true) {
-		do {
+#endif
+                do {
                     rslt = fgets(buffer, sizeof(buffer), f);
-	        } while ((rslt == NULL) && (errno == EINTR));
+                } while ((rslt == NULL) && (errno == EINTR));
+#ifdef OLD
             } else {
                 rslt = buffer;
+                fd = fileno(f);
                 for (;;) {
-		    do {
-                        len = read(fileno(f), rslt, 1);
-		    } while ((len < 0) && (errno == EINTR));
+                    do {
+                        len = read(fd, rslt, 1);
+                    } while ((len < 0) && (errno == EINTR));
                     if (len <= 0) {
                         if (rslt == buffer) {
                             rslt = NULL;
@@ -1578,6 +1652,7 @@
                     }
                 }
             }
+#endif
             _immediateInterrupt = savInt;
             if (rslt != NULL) {
                 len = strlen(buffer);
@@ -1626,19 +1701,30 @@
 
                 savInt = _immediateInterrupt;
                 _immediateInterrupt = 1;
+#ifdef OLD
                 if (_INST(buffered) == false) {
                     cnt = write(fileno(f), s, len);
-                } else { 
+                } else 
+#endif
+                { 
                     if (_INST(mode) == _readwrite)
                         fseek(f, 0L, 1); /* needed in stdio */
                     cnt = fwrite(s, 1, len, f);
                 }
                 if (cnt == len) {
+#ifdef OLD
                     if (_INST(buffered) == false) {
                         cnt = write(fileno(f), "\n", 1);
-                    } else { 
+                    } else 
+#endif
+                    { 
                         cnt = fwrite("\n", 1, 1, f);
                     }
+#ifndef OLD
+                    if (_INST(buffered) == false) {
+                        fflush(f);
+                    }
+#endif
                     if (cnt == 1) {
                         pos = _INST(position);
                         if (pos != nil) {
@@ -1674,15 +1760,20 @@
     filePointer isNil ifTrue:[^ self errorNotOpen].
     srcFilePointer := aStream filePointer.
     srcFilePointer isNil ifTrue:[^ aStream errorNotOpen].
-%{
+
+%{  /* STACK:2000 */
     FILE *dst, *src;
     char *matchString;
     int matchLen = 0;
-    char buffer[1024*8];
+    char buffer[1024];
     extern int _immediateInterrupt;
     int savInt;
 
-    if (_isSmallInteger(srcFilePointer) && (_INST(buffered) == true)) {
+    if (_isSmallInteger(srcFilePointer) 
+#ifdef OLD
+     && (_INST(buffered) == true)
+#endif
+    ) {
         if ((aStringOrNil == nil) || _isString(aStringOrNil)) {
             if (aStringOrNil != nil) {
                 matchString = (char *) _stringVal(aStringOrNil);
@@ -1695,6 +1786,11 @@
             for (;;) {
                 if (fgets(buffer, sizeof(buffer), src) == NULL) break;
                 if (fputs(buffer, dst) == EOF) break;
+#ifndef OLD
+                    if (_INST(buffered) == false) {
+                        fflush(dst);
+                    }
+#endif
                 if (matchLen) {
                     if (strncmp(matchString, buffer, matchLen) == 0) 
                         break;
@@ -1719,10 +1815,11 @@
     (mode == #writeonly) ifTrue:[^ self errorWriteOnly].
     filePointer isNil ifTrue:[^ self errorNotOpen].
     binary ifTrue:[^ self errorBinary].
-%{
+
+%{  /* STACK: 2000 */
     FILE *f;
     int l;
-    char buffer[1024*8];
+    char buffer[1024];
     char *cp;
     char *matchString;
     int  firstpos, lastpos;
@@ -1736,12 +1833,14 @@
         f = MKFD(_INST(filePointer));
         firstpos = ftell(f);
         for (;;) {
+            if (_INST(mode) == _readwrite)
+                fseek(f, 0L, 1); /* needed in stdio */
             lastpos = ftell(f);
             savInt = _immediateInterrupt;
             _immediateInterrupt = 1;
-	    do {
+            do {
                 cp = fgets(buffer, sizeof(buffer), f);
-	    } while ((cp == NULL) && (errno == EINTR));
+            } while ((cp == NULL) && (errno == EINTR));
             _immediateInterrupt = savInt;
             if (cp == NULL) {
                 fseek(f, firstpos, 0);
@@ -1824,9 +1923,10 @@
     sema := Semaphore new.
 
     [OperatingSystem readCheck:fd] whileFalse:[
-        Processor enableIOSemaphore:sema on:fd.
+        Processor enableSemaphore:sema onInput:fd.
+        Processor currentProcess state:#ioWait.
         sema wait.
-        Processor disableIOSemaphore:sema
+        Processor disableSemaphore:sema
     ]
 !
 
@@ -1842,9 +1942,10 @@
     sema := Semaphore new.
 
     [OperatingSystem writeCheck:fd] whileFalse:[
-        Processor enableIOSemaphore:sema on:fd.
+        Processor enableSemaphore:sema onOutput:fd.
+        Processor currentProcess state:#ioWait.
         sema wait.
-        Processor disableIOSemaphore:sema
+        Processor disableSemaphore:sema
     ]
 ! !
      
@@ -1919,6 +2020,8 @@
                 savInt = _immediateInterrupt;
                 _immediateInterrupt = 1;
                 _INST(position) = nil;
+                if (_INST(mode) == _readwrite)
+                    fseek(f, 0L, 1); /* needed in stdio */
                 for (;;) {
                     c = getc(f);
                     if (c == EOF) {
@@ -1944,10 +2047,10 @@
     "read the next line (characters up to newline) skip only;
      return nil if EOF reached. Not allowed in binary mode."
 
-%{  /* NOCONTEXT */
+%{  /* STACK:2000 */
 
     FILE *f;
-    char buffer[1024*8];
+    char buffer[1024];
     extern int _immediateInterrupt;
     int savInt;
 
@@ -1956,6 +2059,8 @@
             f = MKFD(_INST(filePointer));
             savInt = _immediateInterrupt;
             _immediateInterrupt = 1;
+            if (_INST(mode) == _readwrite)
+                fseek(f, 0L, 1); /* needed in stdio */
             if (fgets(buffer, sizeof(buffer), f) != NULL) {
                 _immediateInterrupt = savInt;
                 RETURN ( self );
@@ -2016,6 +2121,8 @@
     if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
         if (_INST(binary) != true) {
             f = MKFD(_INST(filePointer));
+            if (_INST(mode) == _readwrite)
+                fseek(f, 0L, 1); /* needed in stdio */
             while (1) {
                 if (feof(f)) {
                     RETURN ( nil );
@@ -2066,6 +2173,8 @@
         if (_INST(binary) != true) {
             f = MKFD(_INST(filePointer));
             while (1) {
+                if (_INST(mode) == _readwrite)
+                    fseek(f, 0L, 1); /* needed in stdio */
                 if (feof(f)) {
                     RETURN ( nil );
                 }
@@ -2120,6 +2229,8 @@
     int savInt;
 
     f = MKFD(_INST(filePointer));
+    if (_INST(mode) == _readwrite)
+        fseek(f, 0L, 1); /* needed in stdio */
     /*
      * skip spaces
      */
@@ -2130,9 +2241,9 @@
             _immediateInterrupt = savInt;
             RETURN ( nil );
         }
-	do {
+        do {
             c = getc(f);
-	} while ((c < 0) && (errno == EINTR));
+        } while ((c < 0) && (errno == EINTR));
         switch (c) {
             case ' ':
             case '\t':
@@ -2168,9 +2279,9 @@
             buffer = newBuffer;
             currSize = currSize * 2;
         }
-	do {
+        do {
             c = getc(f);
-	} while (c < 0 && (errno == EINTR));
+        } while (c < 0 && (errno == EINTR));
         if (c == '%') {
             peekC = getc(f);
             ungetc(peekC, f);
--- a/ExternalStream.st	Sat Dec 11 01:42:02 1993 +0100
+++ b/ExternalStream.st	Sat Dec 11 01:46:55 1993 +0100
@@ -11,7 +11,7 @@
 "
 
 ReadWriteStream subclass:#ExternalStream
-       instanceVariableNames:'filePointer mode buffered binary'
+       instanceVariableNames:'filePointer mode buffered binary useCRLF'
        classVariableNames:'lobby'
        poolDictionaries:''
        category:'Streams-External'
@@ -29,7 +29,7 @@
 In text-mode, the elements read/written are characters; while in binary-mode the basic
 elements are bytes which read/write as SmallIntegers in the range 0..255.
 
-$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.5 1993-11-08 02:30:14 claus Exp $
+$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.6 1993-12-11 00:46:55 claus Exp $
 
 written 88 by claus
 '!
@@ -73,7 +73,7 @@
     |newStream|
 
     newStream := self basicNew.
-    newStream text.
+    newStream text; buffered:true; useCRLF:false.
     ^ newStream
 ! !
 
@@ -90,7 +90,15 @@
 
 %{  /* NOCONTEXT */
 
-    fclose(MKFD(_INST(filePointer)));
+    if (_INST(filePointer) != nil) {
+        int savInt;
+        extern int _immediateInterrupt;
+
+        savInt = _immediateInterrupt;
+        _immediateInterrupt = 1;
+        fclose(MKFD(_INST(filePointer)));
+        _immediateInterrupt = savInt;
+    }
 %}
 ! !
 
@@ -214,6 +222,12 @@
     buffered := aBoolean
 !
 
+useCRLF:aBoolean
+    "turn on or off CRLF sending (instead of LF only) - default is off"
+
+    useCRLF := aBoolean
+!
+
 binary
     "switch to binary mode - default is text"
 
@@ -361,9 +375,9 @@
             f = MKFD(_INST(filePointer));
             savInt = _immediateInterrupt;
             _immediateInterrupt = 1;
-	    do {
+            do {
                 ret = ioctl(fileno(f), ioNum);
-	    } while ((ret < 0) && (errno == EINTR));
+            } while ((ret < 0) && (errno == EINTR));
             _immediateInterrupt = savInt;
             if (ret >= 0) {
                 RETURN ( _MKSMALLINT(ret) );
@@ -403,13 +417,13 @@
             ioNum = _intVal(ioctlNumber);
             savInt = _immediateInterrupt;
             _immediateInterrupt = 1;
-	    do {
+            do {
                 if (isStructure == true) {
                     ret = ioctl(fileno(f), ioNum, _ByteArrayInstPtr(arg)->ba_element);
                 } else {
                     ret = ioctl(fileno(f), ioNum, _intVal(arg));
                 }
-	    } while ((ret < 0) && (errno == EINTR));
+            } while ((ret < 0) && (errno == EINTR));
             _immediateInterrupt = savInt;
             if (ret >= 0) {
                 RETURN ( _MKSMALLINT(ret) );
@@ -439,31 +453,29 @@
     extern errno;
     extern int _immediateInterrupt;
 
-    if (_INST(filePointer) != nil) {
-        if (_INST(mode) != _writeonly) {
-            f = MKFD(_INST(filePointer));
-            savInt = _immediateInterrupt;
-            _immediateInterrupt = 1;
-	    do {
-                if (_INST(buffered) == false) {
-                    cnt = read(fileno(f), &byte, 1);
-                } else {
-                    if (_INST(mode) == _readwrite)
-                        fseek(f, 0L, 1); /* needed in stdio */
-                    cnt = fread(&byte, 1, 1, f);
-                }
-	    } while ((cnt < 0) && (errno == EINTR));
-            _immediateInterrupt = savInt;
-            if (cnt == 1) {
-                if (_INST(position) != nil)
-                    _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 1);
-                RETURN ( _MKSMALLINT(byte) );
+    if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
+        f = MKFD(_INST(filePointer));
+        savInt = _immediateInterrupt;
+        _immediateInterrupt = 1;
+        do {
+            if (_INST(buffered) == false) {
+                cnt = read(fileno(f), &byte, 1);
+            } else {
+                if (_INST(mode) == _readwrite)
+                    fseek(f, 0L, 1); /* needed in stdio */
+                cnt = fread(&byte, 1, 1, f);
             }
-            if (cnt < 0) {
-                ErrorNumber = _MKSMALLINT(errno);
-            }
-            RETURN ( nil );
+        } while ((cnt < 0) && (errno == EINTR));
+        _immediateInterrupt = savInt;
+        if (cnt == 1) {
+            if (_INST(position) != nil)
+                _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 1);
+            RETURN ( _MKSMALLINT(byte) );
         }
+        if (cnt < 0) {
+            ErrorNumber = _MKSMALLINT(errno);
+        }
+        RETURN ( nil );
     }
 %}
 .
@@ -497,36 +509,34 @@
     OBJ pos;
     extern int _immediateInterrupt;
 
-    if (_INST(filePointer) != nil) {
-        if (_INST(mode) != _writeonly) {
-            if (_isSmallInteger(count) && _isSmallInteger(start)) {
-                cnt = _intVal(count);
-                offs = _intVal(start) - 1;
-                f = MKFD(_INST(filePointer));
-                objSize = _Size(anObject) - OHDR_SIZE;
-                if ((offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs))) {
-                    cp = (char *)_InstPtr(anObject) + OHDR_SIZE + offs;
-                    savInt = _immediateInterrupt;
-                    _immediateInterrupt = 1;
-		    do {
-                        if (_INST(buffered) == false) {
-                            cnt = read(fileno(f), cp, cnt);
-                        } else {
-                            if (_INST(mode) == _readwrite)
-                                fseek(f, 0L, 1); /* needed in stdio */
-                            cnt = fread(cp, 1, cnt, f);
-                        }
-		    } while ((cnt < 0) && (errno == EINTR));
-                    _immediateInterrupt = savInt;
-                    if (cnt >= 0) {
-                        pos = _INST(position);
-                        if (pos != nil)
-                            _INST(position) = _MKSMALLINT(_intVal(pos) + cnt);
-                        RETURN ( _MKSMALLINT(cnt) );
+    if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
+        if (_isSmallInteger(count) && _isSmallInteger(start)) {
+            cnt = _intVal(count);
+            offs = _intVal(start) - 1;
+            f = MKFD(_INST(filePointer));
+            objSize = _Size(anObject) - OHDR_SIZE;
+            if ((offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs))) {
+                cp = (char *)_InstPtr(anObject) + OHDR_SIZE + offs;
+                savInt = _immediateInterrupt;
+                _immediateInterrupt = 1;
+                do {
+                    if (_INST(buffered) == false) {
+                        cnt = read(fileno(f), cp, cnt);
+                    } else {
+                        if (_INST(mode) == _readwrite)
+                            fseek(f, 0L, 1); /* needed in stdio */
+                        cnt = fread(cp, 1, cnt, f);
                     }
-                    ErrorNumber = _MKSMALLINT(errno);
-                    RETURN ( nil );
+                } while ((cnt < 0) && (errno == EINTR));
+                _immediateInterrupt = savInt;
+                if (cnt >= 0) {
+                    pos = _INST(position);
+                    if (pos != nil)
+                        _INST(position) = _MKSMALLINT(_intVal(pos) + cnt);
+                    RETURN ( _MKSMALLINT(cnt) );
                 }
+                ErrorNumber = _MKSMALLINT(errno);
+                RETURN ( nil );
             }
         }
     }
@@ -550,56 +560,54 @@
     int savInt;
 
     if (_INST(binary) == true) {
-        if (_INST(filePointer) != nil) {
-            if (_INST(mode) != _writeonly) {
-                FILE *f;
-                unsigned char hi, low;
-		int cnt;
+        if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
+            FILE *f;
+            unsigned char hi, low;
+            int cnt;
 
-                savInt = _immediateInterrupt;
-                _immediateInterrupt = 1;
-                f = MKFD(_INST(filePointer));
-		do {
-                    if (_INST(buffered) == false) {
-                        cnt = read(fileno(f), &hi, 1);
-                    } else {
-                        if (_INST(mode) == _readwrite)
-                            fseek(f, 0L, 1); /* needed in stdio */
-                        cnt = fread(&hi, 1, 1, f);
-                    }
-		} while ((cnt < 0) && (errno == EINTR));
-
-		if (cnt < 0) {
-                    _immediateInterrupt = savInt;
-                    RETURN ( nil );
+            savInt = _immediateInterrupt;
+            _immediateInterrupt = 1;
+            f = MKFD(_INST(filePointer));
+            do {
+                if (_INST(buffered) == false) {
+                    cnt = read(fileno(f), &hi, 1);
+                } else {
+                    if (_INST(mode) == _readwrite)
+                        fseek(f, 0L, 1); /* needed in stdio */
+                    cnt = fread(&hi, 1, 1, f);
                 }
-                do {
-                    if (_INST(buffered) == false) {
-                        cnt = read(fileno(f), &low, 1);
-                    } else {
-                        if (_INST(mode) == _readwrite)
-                            fseek(f, 0L, 1); /* needed in stdio */
-                        cnt = fread(&low, 1, 1, f);
-                    }
-                } while ((cnt < 0) && (errno == EINTR));
+            } while ((cnt < 0) && (errno == EINTR));
 
+            if (cnt < 0) {
                 _immediateInterrupt = savInt;
-                if (cnt < 0) {
-                    if (_INST(position) != nil) {
-                        _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 1);
-                    }
-                    RETURN ( _MKSMALLINT(hi & 0xFF) );
+                RETURN ( nil );
+            }
+            do {
+                if (_INST(buffered) == false) {
+                    cnt = read(fileno(f), &low, 1);
+                } else {
+                    if (_INST(mode) == _readwrite)
+                        fseek(f, 0L, 1); /* needed in stdio */
+                    cnt = fread(&low, 1, 1, f);
                 }
+            } while ((cnt < 0) && (errno == EINTR));
+
+            _immediateInterrupt = savInt;
+            if (cnt < 0) {
                 if (_INST(position) != nil) {
-                    _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 2);
+                    _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 1);
                 }
-                RETURN ( _MKSMALLINT(((hi & 0xFF)<<8) | (low & 0xFF)) );
+                RETURN ( _MKSMALLINT(hi & 0xFF) );
             }
+            if (_INST(position) != nil) {
+                _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 2);
+            }
+            RETURN ( _MKSMALLINT(((hi & 0xFF)<<8) | (low & 0xFF)) );
         }
     }
 %}
 .
-%{
+%{  /* STACK: 2000 */
     FILE *f;
     int len;
     char buffer[1024];
@@ -607,54 +615,52 @@
     int cnt = 0;
     extern int _immediateInterrupt;
 
-    if (_INST(filePointer) != nil) {
-        if (_INST(mode) != _writeonly) {
-            f = MKFD(_INST(filePointer));
-            savInt = _immediateInterrupt;
-            _immediateInterrupt = 1;
+    if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
+        f = MKFD(_INST(filePointer));
+        savInt = _immediateInterrupt;
+        _immediateInterrupt = 1;
 
-            /* text-mode */
-            for (;;) {
-                ch = getc(f);
-                cnt++;
+        /* text-mode */
+        for (;;) {
+            ch = getc(f);
+            cnt++;
 
-                if (ch >= ' ') break;
-                if ((ch != ' ') && (ch != '\t') && (ch != '\r')
-                 && (ch != '\n') && (ch != 0x0b)) break;
-            }
-            ungetc(ch, f);
-            cnt--;
+            if (ch >= ' ') break;
+            if ((ch != ' ') && (ch != '\t') && (ch != '\r')
+             && (ch != '\n') && (ch != 0x0b)) break;
+        }
+        ungetc(ch, f);
+        cnt--;
 
-            len = 0;
-            for (;;) {
-                ch = getc(f);
-                if (ch == EOF)
-                    break;
-                ch &= 0xFF;
-                if (! (((ch >= 'a') && (ch <= 'z')) ||
-                       ((ch >= 'A') && (ch <= 'Z')) ||
-                       ((ch >= '0') && (ch <= '9')))) {
-                    ungetc(ch, f);
-                    break;
-                }
-                cnt++;
-                buffer[len++] = ch;
-                if (len >= sizeof(buffer)-1) {
-                    /* emergency */
-                    break;
-                }
+        len = 0;
+        for (;;) {
+            ch = getc(f);
+            if (ch == EOF)
+                break;
+            ch &= 0xFF;
+            if (! (((ch >= 'a') && (ch <= 'z')) ||
+                   ((ch >= 'A') && (ch <= 'Z')) ||
+                   ((ch >= '0') && (ch <= '9')))) {
+                ungetc(ch, f);
+                break;
             }
-            _immediateInterrupt = savInt;
-
-            if (_INST(position) != nil) {
-                _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + cnt);
+            cnt++;
+            buffer[len++] = ch;
+            if (len >= sizeof(buffer)-1) {
+                /* emergency */
+                break;
             }
-            buffer[len] = '\0';
-            if (len != 0) {
-                RETURN ( _MKSTRING(buffer COMMA_CON) );
-            }
-            RETURN ( nil );
+        }
+        _immediateInterrupt = savInt;
+
+        if (_INST(position) != nil) {
+            _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + cnt);
         }
+        buffer[len] = '\0';
+        if (len != 0) {
+            RETURN ( _MKSTRING(buffer COMMA_CON) );
+        }
+        RETURN ( nil );
     }
 %}
 .
@@ -674,35 +680,33 @@
     extern int _immediateInterrupt;
 
     if (_INST(binary) == true) {
-        if (_INST(filePointer) != nil) {
-            if (_INST(mode) != _writeonly) {
-                FILE *f;
-                int first, second;
-                short value;
+        if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
+            FILE *f;
+            int first, second;
+            short value;
 
-                savInt = _immediateInterrupt;
-                _immediateInterrupt = 1;
+            savInt = _immediateInterrupt;
+            _immediateInterrupt = 1;
 
-                f = MKFD(_INST(filePointer));
-                first = getc(f);
-                if (first == EOF) {
-                    _immediateInterrupt = savInt;
-                    RETURN ( nil );
-                }
-                second = getc(f);
+            f = MKFD(_INST(filePointer));
+            first = getc(f);
+            if (first == EOF) {
                 _immediateInterrupt = savInt;
+                RETURN ( nil );
+            }
+            second = getc(f);
+            _immediateInterrupt = savInt;
 
-                if (second == EOF) {
-                    RETURN ( nil );
-                }
-                if (_INST(position) != nil) {
-                    _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 2);
-                }
-                if (msbFlag == true) {
-                    RETURN ( _MKSMALLINT(((first & 0xFF)<<8) | (second & 0xFF)) );
-                }
-                RETURN ( _MKSMALLINT(((second & 0xFF)<<8) | (first & 0xFF)) );
+            if (second == EOF) {
+                RETURN ( nil );
             }
+            if (_INST(position) != nil) {
+                _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 2);
+            }
+            if (msbFlag == true) {
+                RETURN ( _MKSMALLINT(((first & 0xFF)<<8) | (second & 0xFF)) );
+            }
+            RETURN ( _MKSMALLINT(((second & 0xFF)<<8) | (first & 0xFF)) );
         }
     }
 %}
@@ -725,33 +729,31 @@
     extern int _immediateInterrupt;
 
     if (_INST(binary) == true) {
-        if (_INST(filePointer) != nil) {
-            if (_INST(mode) != _writeonly) {
-                FILE *f;
-                int first, second;
+        if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
+            FILE *f;
+            int first, second;
 
-                savInt = _immediateInterrupt;
-                _immediateInterrupt = 1;
-                f = MKFD(_INST(filePointer));
-                first = getc(f);
-                if (first == EOF) {
-                    _immediateInterrupt = savInt;
-                    RETURN ( nil );
-                }
-                second = getc(f);
+            savInt = _immediateInterrupt;
+            _immediateInterrupt = 1;
+            f = MKFD(_INST(filePointer));
+            first = getc(f);
+            if (first == EOF) {
                 _immediateInterrupt = savInt;
+                RETURN ( nil );
+            }
+            second = getc(f);
+            _immediateInterrupt = savInt;
 
-                if (second == EOF) {
-                    RETURN ( nil );
-                }
-                if (_INST(position) != nil) {
-                    _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 2);
-                }
-                if (msbFlag == true) {
-                    RETURN ( _MKSMALLINT(((first & 0xFF)<<8) | (second & 0xFF)) );
-                }
-                RETURN ( _MKSMALLINT(((second & 0xFF)<<8) | (first & 0xFF)) );
+            if (second == EOF) {
+                RETURN ( nil );
             }
+            if (_INST(position) != nil) {
+                _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 2);
+            }
+            if (msbFlag == true) {
+                RETURN ( _MKSMALLINT(((first & 0xFF)<<8) | (second & 0xFF)) );
+            }
+            RETURN ( _MKSMALLINT(((second & 0xFF)<<8) | (first & 0xFF)) );
         }
     }
 %}
@@ -775,54 +777,52 @@
     extern int _immediateInterrupt;
 
     if (_INST(binary) == true) {
-        if (_INST(filePointer) != nil) {
-            if (_INST(mode) != _writeonly) {
-                FILE *f;
-                int first, second, third, fourth;
-                int value;
+        if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
+            FILE *f;
+            int first, second, third, fourth;
+            int value;
 
-                savInt = _immediateInterrupt;
-                _immediateInterrupt = 1;
-                f = MKFD(_INST(filePointer));
-                first = getc(f);
-                if (first == EOF) {
-                    _immediateInterrupt = savInt;
-                    RETURN ( nil );
-                }
-                second = getc(f);
-                if (second == EOF) {
-                    _immediateInterrupt = savInt;
-                    RETURN ( nil );
-                }
-                third = getc(f);
-                if (third == EOF) {
-                    _immediateInterrupt = savInt;
-                    RETURN ( nil );
-                }
-                fourth = getc(f);
+            savInt = _immediateInterrupt;
+            _immediateInterrupt = 1;
+            f = MKFD(_INST(filePointer));
+            first = getc(f);
+            if (first == EOF) {
+                _immediateInterrupt = savInt;
+                RETURN ( nil );
+            }
+            second = getc(f);
+            if (second == EOF) {
+                _immediateInterrupt = savInt;
+                RETURN ( nil );
+            }
+            third = getc(f);
+            if (third == EOF) {
                 _immediateInterrupt = savInt;
-                if (fourth == EOF) {
-                    RETURN ( nil );
-                }
-                if (_INST(position) != nil) {
-                    _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 4);
-                }
-                if (msbFlag == true) {
-                    value = ((first & 0xFF) << 24)
-                            | ((second & 0xFF) << 16)
-                            | ((third & 0xFF) << 8)
-                            | (fourth & 0xFF);
-                } else {
-                    value = ((fourth & 0xFF) << 24)
-                            | ((third & 0xFF) << 16)
-                            | ((second & 0xFF) << 8)
-                            | (first & 0xFF);
-                }
-                if ((value >= _MIN_INT) && (value <= _MAX_INT)) {
-                    RETURN ( _MKSMALLINT(value));
-                }
-                RETURN ( _makeLarge(value) );
+                RETURN ( nil );
+            }
+            fourth = getc(f);
+            _immediateInterrupt = savInt;
+            if (fourth == EOF) {
+                RETURN ( nil );
+            }
+            if (_INST(position) != nil) {
+                _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 4);
             }
+            if (msbFlag == true) {
+                value = ((first & 0xFF) << 24)
+                        | ((second & 0xFF) << 16)
+                        | ((third & 0xFF) << 8)
+                        | (fourth & 0xFF);
+            } else {
+                value = ((fourth & 0xFF) << 24)
+                        | ((third & 0xFF) << 16)
+                        | ((second & 0xFF) << 8)
+                        | (first & 0xFF);
+            }
+            if ((value >= _MIN_INT) && (value <= _MAX_INT)) {
+                RETURN ( _MKSMALLINT(value));
+            }
+            RETURN ( _makeLarge(value) );
         }
     }
 %}
@@ -847,54 +847,52 @@
     extern OBJ _makeULarge();
 
     if (_INST(binary) == true) {
-        if (_INST(filePointer) != nil) {
-            if (_INST(mode) != _writeonly) {
-                FILE *f;
-                int first, second, third, fourth;
-                unsigned int value;
+        if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
+            FILE *f;
+            int first, second, third, fourth;
+            unsigned int value;
 
-                savInt = _immediateInterrupt;
-                _immediateInterrupt = 1;
-                f = MKFD(_INST(filePointer));
-                first = getc(f);
-                if (first == EOF) {
-                    _immediateInterrupt = savInt;
-                    RETURN ( nil );
-                }
-                second = getc(f);
-                if (second == EOF) {
-                    _immediateInterrupt = savInt;
-                    RETURN ( nil );
-                }
-                third = getc(f);
-                if (third == EOF) {
-                    _immediateInterrupt = savInt;
-                    RETURN ( nil );
-                }
-                fourth = getc(f);
+            savInt = _immediateInterrupt;
+            _immediateInterrupt = 1;
+            f = MKFD(_INST(filePointer));
+            first = getc(f);
+            if (first == EOF) {
+                _immediateInterrupt = savInt;
+                RETURN ( nil );
+            }
+            second = getc(f);
+            if (second == EOF) {
+                _immediateInterrupt = savInt;
+                RETURN ( nil );
+            }
+            third = getc(f);
+            if (third == EOF) {
                 _immediateInterrupt = savInt;
-                if (fourth == EOF) {
-                    RETURN ( nil );
-                }
-                if (_INST(position) != nil) {
-                    _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 4);
-                }
-                if (msbFlag == true) {
-                    value = ((first & 0xFF) << 24)
-                            | ((second & 0xFF) << 16)
-                            | ((third & 0xFF) << 8)
-                            | (fourth & 0xFF);
-                } else {
-                    value = ((fourth & 0xFF) << 24)
-                            | ((third & 0xFF) << 16)
-                            | ((second & 0xFF) << 8)
-                            | (first & 0xFF);
-                }
-                if (value <= _MAX_INT) {
-                    RETURN ( _MKSMALLINT(value));
-                }
-                RETURN ( _makeULarge(value) );
+                RETURN ( nil );
+            }
+            fourth = getc(f);
+            _immediateInterrupt = savInt;
+            if (fourth == EOF) {
+                RETURN ( nil );
+            }
+            if (_INST(position) != nil) {
+                _INST(position) = _MKSMALLINT(_intVal(_INST(position)) + 4);
             }
+            if (msbFlag == true) {
+                value = ((first & 0xFF) << 24)
+                        | ((second & 0xFF) << 16)
+                        | ((third & 0xFF) << 8)
+                        | (fourth & 0xFF);
+            } else {
+                value = ((fourth & 0xFF) << 24)
+                        | ((third & 0xFF) << 16)
+                        | ((second & 0xFF) << 8)
+                        | (first & 0xFF);
+            }
+            if (value <= _MAX_INT) {
+                RETURN ( _MKSMALLINT(value));
+            }
+            RETURN ( _makeULarge(value) );
         }
     }
 %}
@@ -929,30 +927,36 @@
     int cnt, savInt;
     extern int _immediateInterrupt;
 
-    if (_INST(filePointer) != nil) {
-        if (_INST(mode) != _readonly) {
-            if (_isSmallInteger(aByteValue)) {
-                c = _intVal(aByteValue);
-                f = MKFD(_INST(filePointer));
-                savInt = _immediateInterrupt;
-                _immediateInterrupt = 1;
+    if ((_INST(filePointer) != nil) && (_INST(mode) != _readonly)) {
+        if (_isSmallInteger(aByteValue)) {
+            c = _intVal(aByteValue);
+            f = MKFD(_INST(filePointer));
+            savInt = _immediateInterrupt;
+            _immediateInterrupt = 1;
+#ifdef OLD
+            if (_INST(buffered) == false) {
+                cnt = write(fileno(f), &c, 1);
+            } else 
+#endif
+            {
+                if (_INST(mode) == _readwrite)
+                    fseek(f, 0L, 1); /* needed in stdio */
+                cnt = fwrite(&c, 1, 1, f);
+#ifndef OLD
                 if (_INST(buffered) == false) {
-                    cnt = write(fileno(f), &c, 1);
-                } else {
-                    if (_INST(mode) == _readwrite)
-                        fseek(f, 0L, 1); /* needed in stdio */
-                    cnt = fwrite(&c, 1, 1, f);
+                    fflush(f);
                 }
-                _immediateInterrupt = savInt;
-                if (cnt == 1) {
-                    pos = _INST(position);
-                    if (pos != nil)
-                        _INST(position) = _MKSMALLINT(_intVal(pos) + 1);
-                    RETURN ( self );
-                }
-                ErrorNumber = _MKSMALLINT(errno);
-                RETURN (nil);
+#endif
             }
+            _immediateInterrupt = savInt;
+            if (cnt == 1) {
+                pos = _INST(position);
+                if (pos != nil)
+                    _INST(position) = _MKSMALLINT(_intVal(pos) + 1);
+                RETURN ( self );
+            }
+            ErrorNumber = _MKSMALLINT(errno);
+            RETURN (nil);
         }
     }
 %}
@@ -999,13 +1003,21 @@
                     cp = (char *)_InstPtr(anObject) + OHDR_SIZE + offs;
                     savInt = _immediateInterrupt;
                     _immediateInterrupt = 1;
+#ifdef OLD
                     if (_INST(buffered) == false) {
                         cnt = write(fileno(f), cp, cnt);
-                    } else {
+                    } else
+#endif
+                    {
                         if (_INST(mode) == _readwrite)
                             fseek(f, 0L, 1); /* needed in stdio */
                         cnt = fwrite(cp, 1, cnt, f);
                     }
+#ifndef OLD
+                    if (_INST(buffered) == false) {
+                        fflush(f);
+                    }
+#endif
                     _immediateInterrupt = savInt;
                     if (cnt >= 0) {
                         pos = _INST(position);
@@ -1067,13 +1079,21 @@
                     f = MKFD(_INST(filePointer));
                     savInt = _immediateInterrupt;
                     _immediateInterrupt = 1;
+#ifdef OLD
                     if (_INST(buffered) == false) {
                         cnt = write(fileno(f), bytes, 2);
-                    } else {
+                    } else 
+#endif
+                    {
                         if (_INST(mode) == _readwrite)
                             fseek(f, 0L, 1); /* needed in stdio */
                         cnt = fwrite(bytes, 1, 2, f);
                     }
+#ifndef OLD
+                    if (_INST(buffered) == false) {
+                        fflush(f);
+                    }
+#endif
                     _immediateInterrupt = savInt;
                     if (cnt == 2) {
                         if (_INST(position) != nil) {
@@ -1131,11 +1151,19 @@
                     f = MKFD(_INST(filePointer));
                     savInt = _immediateInterrupt;
                     _immediateInterrupt = 1;
+#ifdef OLD
                     if (_INST(buffered) == false) {
                         cnt = write(fileno(f), bytes, 4);
-                    } else {
+                    } else 
+#endif
+                    {
                         cnt = fwrite(bytes, 1, 4, f);
                     }
+#ifndef OLD
+                    if (_INST(buffered) == false) {
+                        fflush(f);
+                    }
+#endif
                     _immediateInterrupt = savInt;
                     if (cnt == 4) {
                         if (_INST(position) != nil) {
@@ -1155,7 +1183,7 @@
     (mode == #readonly) ifTrue:[^ self errorReadOnly].
     binary ifFalse:[^ self errorNotBinary].
 
-    (aNumber isKindOf:Integer) ifTrue:[
+    aNumber isInteger ifTrue:[
         (self nextShortPut:(aNumber // 16r10000) MSB:msbFlag) isNil ifTrue:[^ nil].
         ^ self nextShortPut:(aNumber \\ 16r10000) MSB:msbFlag.
     ].
@@ -1179,7 +1207,10 @@
 
     if (_INST(filePointer) != nil) {
         if (_INST(mode) != _writeonly) {
-            if (_INST(buffered) == true) {
+#ifdef OLD
+            if (_INST(buffered) == true) 
+#endif
+            {
                 f = MKFD(_INST(filePointer));
                 savInt = _immediateInterrupt;
                 _immediateInterrupt = 1;
@@ -1223,18 +1254,21 @@
             f = MKFD(_INST(filePointer));
             savInt = _immediateInterrupt;
             _immediateInterrupt = 1;
-	    do {
+            do {
+#ifdef OLD
                 if (_INST(buffered) == false) {
                     if (read(fileno(f), &ch, 1) != 1)
                         c = EOF;
                     else
                         c = ch;
-                } else {
+                } else 
+#endif
+                {
                     if (_INST(mode) == _readwrite)
                         fseek(f, 0L, 1); /* needed in stdio */
                     c = getc(f);
                 }
-	    } while ((c < 0) && (errno == EINTR));
+            } while ((c < 0) && (errno == EINTR));
 
             _immediateInterrupt = savInt;
             if (c != EOF) {
@@ -1283,12 +1317,10 @@
 
 %{  /* NOCONTEXT */
 
-    FILE *f;
-
     if (_INST(filePointer) != nil) {
         if (_INST(mode) != _readonly) {
             if (_INST(buffered) == true) {
-                fflush(f);
+                fflush( MKFD(_INST(filePointer)) );
             }
         }
     }
@@ -1319,13 +1351,21 @@
 
                     savInt = _immediateInterrupt;
                     _immediateInterrupt = 1;
+#ifdef OLD
                     if (_INST(buffered) == false) {
                         cnt = write(fileno(f), &c, 1);
-                    } else { 
+                    } else 
+#endif
+                    { 
                         if (_INST(mode) == _readwrite)
                             fseek(f, 0L, 1); /* needed in stdio */
                         cnt = fwrite(&c, 1, 1, f);
                     }
+#ifndef OLD
+                    if (_INST(buffered) == false) {
+                        fflush(f);
+                    }
+#endif
                     _immediateInterrupt = savInt;
                     if (cnt == 1) {
                         pos = _INST(position);
@@ -1385,13 +1425,21 @@
 
             savInt = _immediateInterrupt;
             _immediateInterrupt = 1;
+#ifdef OLD
             if (_INST(buffered) == false) {
                 cnt = write(fileno(f), cp, len);
-            } else { 
+            } else 
+#endif
+            { 
                 if (_INST(mode) == _readwrite)
                     fseek(f, 0L, 1); /* needed in stdio */
                 cnt = fwrite(cp, 1, len, f);
             }
+#ifndef OLD
+                    if (_INST(buffered) == false) {
+                        fflush(f);
+                    }
+#endif
             _immediateInterrupt = savInt;
             if (cnt == len) {
                 pos = _INST(position);
@@ -1454,13 +1502,21 @@
                 savInt = _immediateInterrupt;
                 _immediateInterrupt = 1;
                 len = index2 - index1 + 1;
+#ifdef OLD
                 if (_INST(buffered) == false) {
                     cnt = write(fileno(f), cp + index1 - 1, len);
-                } else { 
+                } else 
+#endif
+                { 
                     if (_INST(mode) == _readwrite)
                         fseek(f, 0L, 1); /* needed in stdio */
                     cnt = fwrite(cp + index1 - 1, 1, len, f);
                 }
+#ifndef OLD
+                    if (_INST(buffered) == false) {
+                        fflush(f);
+                    }
+#endif
                 _immediateInterrupt = savInt;
                 if (cnt == len) {
                     if (_INST(position) != nil) {
@@ -1500,13 +1556,21 @@
 
             savInt = _immediateInterrupt;
             _immediateInterrupt = 1;
+#ifdef OLD
             if (_INST(buffered) == false) {
                 cnt = write(fileno(f), "\n", 1);
-            } else { 
+            } else 
+#endif
+            { 
                 if (_INST(mode) == _readwrite)
                     fseek(f, 0L, 1); /* needed in stdio */
                 cnt = fwrite("\n", 1, 1, f);
             }
+#ifndef OLD
+                    if (_INST(buffered) == false) {
+                        fflush(f);
+                    }
+#endif
             _immediateInterrupt = savInt;
             if (cnt == 1) {
                 if (_INST(position) != nil) {
@@ -1533,32 +1597,42 @@
      If the previous-to-last character is a cr, this is also removed,
      so its possible to read alien (i.e. ms-dos) text as well."
 
-%{  /* NOCONTEXT */
+%{  /* STACK:2000 */
 
     FILE *f;
     int len;
-    char buffer[1024*8];
+    char buffer[1024];
     extern int _immediateInterrupt;
     int savInt;
     char *rslt;
     extern errno;
+    int fd;
 
-    if ((_INST(filePointer) != nil) && (_INST(filePointer) != _writeonly)) {
+    if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
         if (_INST(binary) != true) {
             f = MKFD(_INST(filePointer));
             savInt = _immediateInterrupt;
             _immediateInterrupt = 1;
             buffer[0] = 0;
+
+#ifndef OLD
+            if (_INST(mode) == _readwrite)
+                fseek(f, 0L, 1); /* needed in stdio */
+#endif
+#ifdef OLD
             if (_INST(buffered) == true) {
-		do {
+#endif
+                do {
                     rslt = fgets(buffer, sizeof(buffer), f);
-	        } while ((rslt == NULL) && (errno == EINTR));
+                } while ((rslt == NULL) && (errno == EINTR));
+#ifdef OLD
             } else {
                 rslt = buffer;
+                fd = fileno(f);
                 for (;;) {
-		    do {
-                        len = read(fileno(f), rslt, 1);
-		    } while ((len < 0) && (errno == EINTR));
+                    do {
+                        len = read(fd, rslt, 1);
+                    } while ((len < 0) && (errno == EINTR));
                     if (len <= 0) {
                         if (rslt == buffer) {
                             rslt = NULL;
@@ -1578,6 +1652,7 @@
                     }
                 }
             }
+#endif
             _immediateInterrupt = savInt;
             if (rslt != NULL) {
                 len = strlen(buffer);
@@ -1626,19 +1701,30 @@
 
                 savInt = _immediateInterrupt;
                 _immediateInterrupt = 1;
+#ifdef OLD
                 if (_INST(buffered) == false) {
                     cnt = write(fileno(f), s, len);
-                } else { 
+                } else 
+#endif
+                { 
                     if (_INST(mode) == _readwrite)
                         fseek(f, 0L, 1); /* needed in stdio */
                     cnt = fwrite(s, 1, len, f);
                 }
                 if (cnt == len) {
+#ifdef OLD
                     if (_INST(buffered) == false) {
                         cnt = write(fileno(f), "\n", 1);
-                    } else { 
+                    } else 
+#endif
+                    { 
                         cnt = fwrite("\n", 1, 1, f);
                     }
+#ifndef OLD
+                    if (_INST(buffered) == false) {
+                        fflush(f);
+                    }
+#endif
                     if (cnt == 1) {
                         pos = _INST(position);
                         if (pos != nil) {
@@ -1674,15 +1760,20 @@
     filePointer isNil ifTrue:[^ self errorNotOpen].
     srcFilePointer := aStream filePointer.
     srcFilePointer isNil ifTrue:[^ aStream errorNotOpen].
-%{
+
+%{  /* STACK:2000 */
     FILE *dst, *src;
     char *matchString;
     int matchLen = 0;
-    char buffer[1024*8];
+    char buffer[1024];
     extern int _immediateInterrupt;
     int savInt;
 
-    if (_isSmallInteger(srcFilePointer) && (_INST(buffered) == true)) {
+    if (_isSmallInteger(srcFilePointer) 
+#ifdef OLD
+     && (_INST(buffered) == true)
+#endif
+    ) {
         if ((aStringOrNil == nil) || _isString(aStringOrNil)) {
             if (aStringOrNil != nil) {
                 matchString = (char *) _stringVal(aStringOrNil);
@@ -1695,6 +1786,11 @@
             for (;;) {
                 if (fgets(buffer, sizeof(buffer), src) == NULL) break;
                 if (fputs(buffer, dst) == EOF) break;
+#ifndef OLD
+                    if (_INST(buffered) == false) {
+                        fflush(dst);
+                    }
+#endif
                 if (matchLen) {
                     if (strncmp(matchString, buffer, matchLen) == 0) 
                         break;
@@ -1719,10 +1815,11 @@
     (mode == #writeonly) ifTrue:[^ self errorWriteOnly].
     filePointer isNil ifTrue:[^ self errorNotOpen].
     binary ifTrue:[^ self errorBinary].
-%{
+
+%{  /* STACK: 2000 */
     FILE *f;
     int l;
-    char buffer[1024*8];
+    char buffer[1024];
     char *cp;
     char *matchString;
     int  firstpos, lastpos;
@@ -1736,12 +1833,14 @@
         f = MKFD(_INST(filePointer));
         firstpos = ftell(f);
         for (;;) {
+            if (_INST(mode) == _readwrite)
+                fseek(f, 0L, 1); /* needed in stdio */
             lastpos = ftell(f);
             savInt = _immediateInterrupt;
             _immediateInterrupt = 1;
-	    do {
+            do {
                 cp = fgets(buffer, sizeof(buffer), f);
-	    } while ((cp == NULL) && (errno == EINTR));
+            } while ((cp == NULL) && (errno == EINTR));
             _immediateInterrupt = savInt;
             if (cp == NULL) {
                 fseek(f, firstpos, 0);
@@ -1824,9 +1923,10 @@
     sema := Semaphore new.
 
     [OperatingSystem readCheck:fd] whileFalse:[
-        Processor enableIOSemaphore:sema on:fd.
+        Processor enableSemaphore:sema onInput:fd.
+        Processor currentProcess state:#ioWait.
         sema wait.
-        Processor disableIOSemaphore:sema
+        Processor disableSemaphore:sema
     ]
 !
 
@@ -1842,9 +1942,10 @@
     sema := Semaphore new.
 
     [OperatingSystem writeCheck:fd] whileFalse:[
-        Processor enableIOSemaphore:sema on:fd.
+        Processor enableSemaphore:sema onOutput:fd.
+        Processor currentProcess state:#ioWait.
         sema wait.
-        Processor disableIOSemaphore:sema
+        Processor disableSemaphore:sema
     ]
 ! !
      
@@ -1919,6 +2020,8 @@
                 savInt = _immediateInterrupt;
                 _immediateInterrupt = 1;
                 _INST(position) = nil;
+                if (_INST(mode) == _readwrite)
+                    fseek(f, 0L, 1); /* needed in stdio */
                 for (;;) {
                     c = getc(f);
                     if (c == EOF) {
@@ -1944,10 +2047,10 @@
     "read the next line (characters up to newline) skip only;
      return nil if EOF reached. Not allowed in binary mode."
 
-%{  /* NOCONTEXT */
+%{  /* STACK:2000 */
 
     FILE *f;
-    char buffer[1024*8];
+    char buffer[1024];
     extern int _immediateInterrupt;
     int savInt;
 
@@ -1956,6 +2059,8 @@
             f = MKFD(_INST(filePointer));
             savInt = _immediateInterrupt;
             _immediateInterrupt = 1;
+            if (_INST(mode) == _readwrite)
+                fseek(f, 0L, 1); /* needed in stdio */
             if (fgets(buffer, sizeof(buffer), f) != NULL) {
                 _immediateInterrupt = savInt;
                 RETURN ( self );
@@ -2016,6 +2121,8 @@
     if ((_INST(filePointer) != nil) && (_INST(mode) != _writeonly)) {
         if (_INST(binary) != true) {
             f = MKFD(_INST(filePointer));
+            if (_INST(mode) == _readwrite)
+                fseek(f, 0L, 1); /* needed in stdio */
             while (1) {
                 if (feof(f)) {
                     RETURN ( nil );
@@ -2066,6 +2173,8 @@
         if (_INST(binary) != true) {
             f = MKFD(_INST(filePointer));
             while (1) {
+                if (_INST(mode) == _readwrite)
+                    fseek(f, 0L, 1); /* needed in stdio */
                 if (feof(f)) {
                     RETURN ( nil );
                 }
@@ -2120,6 +2229,8 @@
     int savInt;
 
     f = MKFD(_INST(filePointer));
+    if (_INST(mode) == _readwrite)
+        fseek(f, 0L, 1); /* needed in stdio */
     /*
      * skip spaces
      */
@@ -2130,9 +2241,9 @@
             _immediateInterrupt = savInt;
             RETURN ( nil );
         }
-	do {
+        do {
             c = getc(f);
-	} while ((c < 0) && (errno == EINTR));
+        } while ((c < 0) && (errno == EINTR));
         switch (c) {
             case ' ':
             case '\t':
@@ -2168,9 +2279,9 @@
             buffer = newBuffer;
             currSize = currSize * 2;
         }
-	do {
+        do {
             c = getc(f);
-	} while (c < 0 && (errno == EINTR));
+        } while (c < 0 && (errno == EINTR));
         if (c == '%') {
             peekC = getc(f);
             ungetc(peekC, f);