ExternalStream.st
changeset 7887 c5df7cf8d667
parent 7883 1039acd0624b
child 8007 adfabcb563e0
--- a/ExternalStream.st	Fri Feb 06 23:28:14 2004 +0100
+++ b/ExternalStream.st	Fri Feb 06 23:29:37 2004 +0100
@@ -3054,25 +3054,49 @@
 #ifdef HAS_FTRUNCATE
     FILEPOINTER f;
     OBJ fp;
+    off_t truncateSize;
 
     if (((fp = __INST(filePointer)) != nil)
-     && (__INST(mode) != @symbol(readonly))) {
-	if (__isSmallInteger(newSize)) {
-	    f = __FILEVal(fp);
-
-	    if (__INST(buffered) == true) {
-		__READING__(f)
-		FFLUSH(f);
-		OPT_FSEEK(f, 0L, SEEK_END); /* needed in stdio */
-	    }
-	    ftruncate(fileno(f), __intVal(newSize));
-	    RETURN (self);
-	}
+        && (__INST(mode) != @symbol(readonly))) {
+        if (__isSmallInteger(newSize)) {
+            truncateSize = __intVal(newSize);
+            if (truncateSize < 0) {
+                goto getOutOfHere;
+            }
+        } else {
+            truncateSize = __signedLongIntVal(newSize);
+            if (truncateSize < 0) {
+                goto getOutOfHere;
+            }
+            if (truncateSize == 0) {
+                if (sizeof(truncateSize) == 8) {
+                    if (__signedLong64IntVal(newSize, &truncateSize) == 0 || truncateSize < 0) {
+                        goto getOutOfHere;
+                    }
+                } else {
+                    goto getOutOfHere;
+                }
+            }
+        }
+
+        f = __FILEVal(fp);
+
+        if (__INST(buffered) == true) {
+            __READING__(f)
+            FFLUSH(f);
+            OPT_FSEEK(f, 0L, SEEK_END); /* needed in stdio */
+        }
+        ftruncate(fileno(f), truncateSize);
+        RETURN (self);
     }
+getOutOfHere:
 #endif
 %}.
     filePointer isNil ifTrue:[self errorNotOpen. ^ self].
     (mode == #readonly) ifTrue:[self errorReadOnly. ^ self].
+    newSize < 0 ifTrue:[
+        self error:'wrong arg'.
+    ].
     self errorUnsupportedOperation
 
     "
@@ -5857,7 +5881,7 @@
 !ExternalStream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.270 2004-02-06 11:39:18 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.271 2004-02-06 22:29:37 stefan Exp $'
 ! !
 
 ExternalStream initialize!