UnixOperatingSystem.st
changeset 6306 0e4cc1ce9245
parent 6301 f95d23e6da4d
child 6334 cbb0e8c8c1a5
--- a/UnixOperatingSystem.st	Tue Dec 11 21:18:16 2001 +0100
+++ b/UnixOperatingSystem.st	Tue Dec 11 21:21:45 2001 +0100
@@ -3384,12 +3384,12 @@
 
 createFileForReadAppend:pathName
 
-    self open:pathName attributes:#(O_RDWR O_APPEND O_CREAT) mode:nil
+    ^ self open:pathName attributes:#(O_RDWR O_APPEND O_CREAT) mode:nil
 !
 
 createFileForReadWrite:pathName
 
-    self open:pathName attributes:#(O_RDWR O_CREAT)  mode:nil
+    ^ self open:pathName attributes:#(O_RDWR O_CREAT O_TRUNC) mode:nil
 !
 
 createHardLinkFrom:oldPath to:newPath
@@ -3480,7 +3480,7 @@
     |error fileDescriptor|
 
 %{
-    OBJ attribute;
+    OBJ *ap;
     int nAttributes;
     int fd;
     int mode, openFlags = 0;
@@ -3504,9 +3504,11 @@
     }
 
     nAttributes = __arraySize(attributes);
-    for (n = 0, attribute = __ArrayInstPtr(attributes)->a_element[n]; 
+    for (n = 0, ap = __ArrayInstPtr(attributes)->a_element; 
          n < nAttributes; 
          n++) {
+        OBJ attribute = ap[n];
+
         if (attribute == @symbol(O_RDONLY)) {
             openFlags |= O_RDONLY;
         } else if (attribute == @symbol(O_RDWR)) {
@@ -3571,32 +3573,33 @@
         self open:'/tmp/xxzz' attributes:#(O_RDWR O_CREAT) mode:8r611
         self open:'/etc/passwd' attributes:#(O_RDWR) mode:nil
         self open:'/no one knows this file' attributes:#(O_RDONLY) mode:nil
+        self open:'foo/bar/baz' attributes:#(O_RDWR O_CREAT) mode:nil
     "
 !
 
 openFileForAppend:pathName
 
-    self open:pathName attributes:#(O_RDWR O_APPEND) mode:nil
+    ^ self open:pathName attributes:#(O_RDWR O_APPEND) mode:nil
 !
 
 openFileForRead:pathName
 
-    self open:pathName attributes:#(O_RDONLY) mode:nil
+    ^ self open:pathName attributes:#(O_RDONLY) mode:nil
 !
 
 openFileForReadAppend:pathName
 
-    self open:pathName attributes:#(O_RDWR O_APPEND) mode:nil
+    ^ self open:pathName attributes:#(O_RDWR O_APPEND) mode:nil
 !
 
 openFileForReadWrite:pathName
 
-    self open:pathName attributes:#(O_RDWR)  mode:nil
+    ^ self open:pathName attributes:#(O_RDWR)  mode:nil
 !
 
 openFileForWrite:pathName
 
-    self open:pathName attributes:#(O_WRONLY)  mode:nil
+    ^ self open:pathName attributes:#(O_WRONLY)  mode:nil
 !
 
 recursiveCopyDirectory:sourcePathName to:destination
@@ -9011,22 +9014,35 @@
 
 readBytes:count into:aByteBuffer startingAt:firstIndex
     "read count bytes into a byte-buffer;
-     Return the number of bytes read (negative on error)"
+     Return the number of bytes read.
+     The read is non-blocking. If the operation would block
+     either an incomplete count or nil will be returned.
+
+     An exception is raised on any other error"
+
+    |error|
 
 %{
     unsigned char *extPtr;
     int nRead = -1;
-    INT fd = (INT)(__externalAddressVal(self));
+    INT fd;
     INT cnt, offs;
     int nInstBytes, objSize;
 
-    if (! __bothSmallInteger(count, firstIndex)) {
+    if (! __isSmallInteger(__INST(fd))) {
+        error = @symbol(errorNotOpen);
         goto bad;
     }
+    if (! __bothSmallInteger(count, firstIndex)) {
+        error = @symbol(badArgument);
+        goto bad;
+    }
+    fd = __smallIntegerVal(__INST(fd));
     cnt = __smallIntegerVal(count);
     offs = __smallIntegerVal(firstIndex) - 1;
 
     if (fd < 0) {
+        error = @symbol(internalError);
         goto bad;
     }
     if (__isExternalBytes(aByteBuffer)) {
@@ -9055,6 +9071,7 @@
             case DOUBLEARRAY:
                 break;
             default:
+                error = @symbol(badArgument1);
                 goto bad;
         }
         extPtr = (char *)0;
@@ -9063,8 +9080,8 @@
         objSize = __Size(aByteBuffer) - OHDR_SIZE - nInstBytes;
     }
     if ((offs >= 0)
-     && (cnt >= 0)
-     && ((objSize == -1) || (objSize >= (cnt + offs)))) {
+        && (cnt >= 0)
+        && ((objSize == -1) || (objSize >= (cnt + offs)))) {
         nRead = 0;
 
         do {
@@ -9087,13 +9104,24 @@
                 cnt -= n;
                 offs += n;
                 nRead += n;
-            } else {
-                if (n < 0) {
-                    if (errno == EINTR) {
-                        continue;
-                    }
-                    break;
+            } else if (n == 0) {
+                break;
+            } else if (n < 0) {
+                if (0
+#ifdef EWOULDBLOCK
+                    || errno == EWOULDBLOCK
+#endif
+#ifdef EAGAIN
+                    || errno == EAGAIN
+#endif
+                ) {
+                     RETURN(nil);
                 }
+                if (errno != EINTR) {
+                     error = __mkSmallInteger(errno);
+                     goto bad;
+                }
+                __HANDLE_INTERRUPTS__;
             }
         } while (cnt > 0);
 
@@ -9101,38 +9129,54 @@
     }
 bad: ;   
 %}.
-    ^ self primitiveFailed
+    ^ self error:error.
 
     "
      |h buff n|
 
-     h := self basicNew.
-     h setFileDescriptor:0.
-     buff := ByteArray new:10. buff inspect.
-     n := h readBytes:10 into:buff startingAt:1.
-     Transcript show:n; space; showCR:buff.
-    "
-
+     h := OperatingSystem openFileForRead:'/etc/hosts'.
+     buff := ByteArray new:1000. buff inspect.
+     n := h readBytes:1000 into:buff startingAt:1.
+     Transcript show:n; space; showCR:buff asString.
+    "
+
+    "
+     |h buff n|
+
+     h := OperatingSystem openFileForRead:'/dev/cua0'.
+     buff := ByteArray new:1000. buff inspect.
+     n := h readBytes:1000 into:buff startingAt:1.
+     Transcript show:n printString; space; showCR:buff asString.
+    "
 !
 
 writeBytes:count from:aByteBuffer startingAt:firstIndex
     "write count bytes from a byte-buffer;
      Return the number of bytes written (negative on error)"
 
+    |error|
+
 %{
     unsigned char *extPtr;
     int nWritten = -1;
-    INT fd = (INT)(__externalAddressVal(self));
+    INT fd;
     INT cnt, offs;
     int nInstBytes, objSize;
 
-    if (! __bothSmallInteger(count, firstIndex)) {
+    if (! __isSmallInteger(__INST(fd))) {
+        error = @symbol(errorNotOpen);
         goto bad;
     }
+    if (! __bothSmallInteger(count, firstIndex)) {
+        error = @symbol(badArgument);
+        goto bad;
+    }
+    fd = __smallIntegerVal(__INST(fd));
     cnt = __smallIntegerVal(count);
     offs = __smallIntegerVal(firstIndex) - 1;
 
     if (fd < 0) {
+        error = @symbol(internalError);
         goto bad;
     }
     if (__isExternalBytes(aByteBuffer)) {
@@ -9161,6 +9205,7 @@
             case DOUBLEARRAY:
                 break;
             default:
+                error = @symbol(badArgument1);
                 goto bad;
         }
         extPtr = (char *)0;
@@ -9169,8 +9214,8 @@
         objSize = __Size(aByteBuffer) - OHDR_SIZE - nInstBytes;
     }
     if ((offs >= 0)
-     && (cnt >= 0)
-     && ((objSize == -1) || (objSize >= (cnt + offs)))) {
+        && (cnt >= 0)
+        && ((objSize == -1) || (objSize >= (cnt + offs)))) {
         nWritten = 0;
 
         do {
@@ -9193,13 +9238,24 @@
                 cnt -= n;
                 offs += n;
                 nWritten += n;
-            } else {
-                if (n < 0) {
-                    if (errno == EINTR) {
-                        continue;
-                    }
-                    break;
+            } else if (n == 0) {
+                break;
+            } else if (n < 0) {
+                if (0
+#ifdef EWOULDBLOCK
+                    || errno == EWOULDBLOCK
+#endif
+#ifdef EAGAIN
+                    || errno == EAGAIN
+#endif
+                ) {
+                     RETURN(nil);
                 }
+                if (errno != EINTR) {
+                     error = __mkSmallInteger(errno);
+                     goto bad;
+                }
+                __HANDLE_INTERRUPTS__;
             }
         } while (cnt > 0);
 
@@ -9207,18 +9263,23 @@
     }
 bad: ;   
 %}.
-    ^ self primitiveFailed
+    ^ self error:error
 
     "
      |h buff n|
 
-     h := self basicNew.
-     h setFileDescriptor:1.
+     h := OperatingSystem createFileForReadWrite:'/tmp/xx'.
      buff := '12345678901234567890'.
      n := h writeBytes:10 from:buff startingAt:1.
     "
 
-
+    "
+     |h buff n|
+
+     h := OperatingSystem createFileForReadWrite:'/dev/cua0'.
+     buff := '12345678901234567890'.
+     n := h writeBytes:10 from:buff startingAt:1.
+    "
 ! !
 
 !UnixOperatingSystem::FileDescriptorHandle methodsFor:'misc functions'!
@@ -9448,7 +9509,7 @@
 !UnixOperatingSystem::FilePointerHandle class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/UnixOperatingSystem.st,v 1.103 2001-12-11 18:55:07 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/UnixOperatingSystem.st,v 1.104 2001-12-11 20:21:45 stefan Exp $'
 ! !
 
 !UnixOperatingSystem::FilePointerHandle methodsFor:'release'!
@@ -9760,6 +9821,6 @@
 !UnixOperatingSystem class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/UnixOperatingSystem.st,v 1.103 2001-12-11 18:55:07 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/UnixOperatingSystem.st,v 1.104 2001-12-11 20:21:45 stefan Exp $'
 ! !
 UnixOperatingSystem initialize!