Unix.st
changeset 3 24d81bf47225
parent 2 6526dde5f3ac
child 5 67342904af11
--- a/Unix.st	Mon Oct 04 11:32:33 1993 +0100
+++ b/Unix.st	Wed Oct 13 01:19:00 1993 +0100
@@ -22,7 +22,7 @@
 COPYRIGHT (c) 1988-93 by Claus Gittinger
              All Rights Reserved
 
-%W% %E%
+$Header: /cvs/stx/stx/libbasic/Attic/Unix.st,v 1.3 1993-10-13 00:18:51 claus Exp $
 
 written 1988 by claus
 '!
@@ -439,6 +439,12 @@
 
 !OperatingSystem class methodsFor:'error messages'!
 
+lastErrorString
+    "return a message string describing the last error"
+
+    ^ self errorTextForNumber:ErrorNumber
+!
+
 errorTextForNumber:errNr
     "return a message string from a unix errorNumber 
      (as returned by a system call). Should be replaced by
@@ -884,42 +890,17 @@
     self primitiveFailed
 !
 
-selectOn:aFileDescriptor withTimeOut:seconds
+selectOn:fd withTimeOut:seconds
     "wait for aFileDesriptor to become ready; timeout after t seconds.
      Return true, if i/o ok, false if timed-out or interrupted.
      With 0 as timeout argument, this can be used to check for availability
      of read-data.
      Experimental."
 
-    |millis|
-
-    millis := (seconds * 1000) rounded.
-%{
-    fd_set rset, wset, eset;
-    int t, fd, i, lX, bX;
-    struct timeval wt;
-
-    if ((aFileDescriptor == nil) || _isSmallInteger(aFileDescriptor)) {
-        if (_isSmallInteger(millis)) {
-            FD_ZERO(&rset);
-            FD_ZERO(&wset);
-            FD_ZERO(&eset);
-            if (aFileDescriptor != nil) {
-                fd = _intVal(aFileDescriptor);
-                if ((fd >= 0) && (fd < FD_SETSIZE))
-                    FD_SET(fd, &rset);
-            } else
-                fd = 0;
-            t = _intVal(millis);
-            wt.tv_sec = t / 1000;
-            wt.tv_usec = (t % 1000) * 1000;
-            RETURN ( (select(fd+1, &rset, &wset, &eset, &wt) == 0) ? false
-                                                                   : true );
-        }
-    }
-%}
-.
-    self primitiveFailed
+    ^ self selectOnAnyReadable:(Array with:fd)
+                      writable:(Array with:fd)
+                         error:nil
+                   withTimeOut:seconds
 !
 
 selectOn:fd1 and:fd2 withTimeOut:seconds
@@ -927,68 +908,58 @@
      Return fd if i/o ok, nil if timed-out or interrupted.
      Experimental."
 
-    |millis|
-
-    millis := (seconds * 1000) rounded.
-%{
-    fd_set rset, wset, eset;
-    int t, f1, f2, i, lX, bX;
-    struct timeval wt;
-    OBJ retFd;
-
-    if (((fd1 == nil) || _isSmallInteger(fd1)) 
-     && ((fd2 == nil) || _isSmallInteger(fd2)))  {
-        if (_isSmallInteger(millis)) {
-            FD_ZERO(&rset);
-            FD_ZERO(&wset);
-            FD_ZERO(&eset);
-            if (fd1 != nil) {
-                f1 = _intVal(fd1);
-                if ((f1 >= 0) && (f1 < FD_SETSIZE))
-                    FD_SET(f1, &rset);
-            } else
-                f1 = 0;
-            if (fd2 != nil) {
-                f2 = _intVal(fd2);
-                if ((f2 >= 0) && (f2 < FD_SETSIZE))
-                    FD_SET(f2, &rset);
-            } else
-                f2 = 0;
-            if (f2 > f1)
-                f1 = f2;
-            t = _intVal(millis);
-            wt.tv_sec = t / 1000;
-            wt.tv_usec = (t % 1000) * 1000;
-            if (select(f1+1, &rset, &wset, &eset, &wt)) {
-                if (FD_ISSET(f1, &rset)) retFd = fd1;
-                else if (FD_ISSET(f1, &wset)) retFd = fd1;
-                else if (FD_ISSET(f1, &eset)) retFd = fd1;
-                else if (FD_ISSET(f2, &rset)) retFd = fd2;
-                else if (FD_ISSET(f2, &wset)) retFd = fd2;
-                else if (FD_ISSET(f2, &eset)) retFd = fd2;
-                RETURN ( retFd );
-            }
-            RETURN ( nil );
-        }
-    }
-%}
-.
-    self primitiveFailed
+    ^ self selectOnAnyReadable:(Array with:fd1 with:fd2)
+                      writable:(Array with:fd1 with:fd2)
+                         error:nil
+                   withTimeOut:seconds
 !
 
 selectOnAnyReadable:fdArray withTimeOut:seconds
+    "wait for any fd in fdArray (an Array of integers) to become ready for reading;
+     timeout after t seconds. An empty set will always wait.
+     Return first ready fd if i/o ok, nil if timed-out or interrupted.
+     Experimental."
+
+    ^ self selectOnAnyReadable:fdArray 
+                      writable:nil 
+                         error:nil
+                   withTimeOut:seconds
+!
+
+selectOnAny:fdArray withTimeOut:seconds
     "wait for any fd in fdArray (an Array of integers) to become ready;
      timeout after t seconds. An empty set will always wait.
      Return first ready fd if i/o ok, nil if timed-out or interrupted.
      Experimental."
 
-    |millis count|
+    ^ self selectOnAnyReadable:fdArray
+                      writable:fdArray
+                         error:nil
+                   withTimeOut:seconds
+! 
+
+selectOnAnyReadable:readFdArray writable:writeFdArray error:errorFdArray withTimeOut:seconds
+    "wait for any fd in readFdArray (an Array of integers) to become ready for reading,
+     writeFdArray to become ready for writing.
+     timeout after t seconds. Empty sets will always wait.
+     Return first ready fd if i/o ok, nil if timed-out or interrupted.
+     Experimental."
+
+    |millis rcount wcount ecount|
 
     millis := (seconds * 1000) rounded asInteger.
-    (fdArray class == Array) ifFalse:[
+    (readFdArray isNil or:[readFdArray class == Array]) ifFalse:[
+        ^ self error:'argument must be an Array'
+    ].
+    (writeFdArray isNil or:[writeFdArray class == Array]) ifFalse:[
         ^ self error:'argument must be an Array'
     ].
-    count := fdArray size.
+    (errorFdArray isNil or:[errorFdArray class == Array]) ifFalse:[
+        ^ self error:'argument must be an Array'
+    ].
+    rcount := readFdArray size.
+    wcount := writeFdArray size.
+    ecount := errorFdArray size.
 %{
     fd_set rset, wset, eset;
     int t, f, maxF, i, lX, bX;
@@ -1001,14 +972,32 @@
         FD_ZERO(&eset);
 
         maxF = 0;
-        for (i=0; i<_intVal(count);i++) {
-            fd = _ArrayInstPtr(fdArray)->a_element[i];
+        for (i=0; i<_intVal(rcount);i++) {
+            fd = _ArrayInstPtr(readFdArray)->a_element[i];
             if (fd != nil) {
                 f = _intVal(fd);
                 if ((f >= 0) && (f < FD_SETSIZE)) {
                     FD_SET(f, &rset);
-/*                   FD_SET(f, &wset);       */
-/*                   FD_SET(f, &eset);       */
+                    if (f > maxF) maxF = f;
+                }
+            }
+        }
+        for (i=0; i<_intVal(wcount);i++) {
+            fd = _ArrayInstPtr(writeFdArray)->a_element[i];
+            if (fd != nil) {
+                f = _intVal(fd);
+                if ((f >= 0) && (f < FD_SETSIZE)) {
+                    FD_SET(f, &wset);       
+                    if (f > maxF) maxF = f;
+                }
+            }
+        }
+        for (i=0; i<_intVal(ecount);i++) {
+            fd = _ArrayInstPtr(errorFdArray)->a_element[i];
+            if (fd != nil) {
+                f = _intVal(fd);
+                if ((f >= 0) && (f < FD_SETSIZE)) {
+                    FD_SET(f, &eset);       
                     if (f > maxF) maxF = f;
                 }
             }
@@ -1019,8 +1008,8 @@
         if (select(maxF+1, &rset, &wset, &eset, &wt)) {
             for (i=0; i <= maxF; i++) {
                 if (FD_ISSET(i, &rset)
-/*                 || FD_ISSET(i, &wset)
-                   || FD_ISSET(i, &eset) */ ) {
+                 || FD_ISSET(i, &wset)
+                 || FD_ISSET(i, &eset)) {
                     RETURN ( _MKSMALLINT(i) );
                 }
             }