home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / EMXLIB8F.ZIP / EMX / LIB / IO / SELECT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-02  |  1.5 KB  |  54 lines

  1. /* select.c (emx+gcc) -- Copyright (c) 1992-1993 by Eberhard Mattes */
  2.  
  3. #include <sys/emx.h>
  4. #include <sys/types.h>
  5. #include <sys/time.h>
  6. #include <stdlib.h>
  7. #include <memory.h>
  8. #include <io.h>
  9.  
  10. #define MIN(a,b) (((a) < (b)) ? (a) : (b))
  11.  
  12. int select (int nfds, fd_set *readfds, fd_set *writefds,
  13.             fd_set *exceptfds, struct timeval *timeout)
  14. {
  15.   struct _select args;
  16.   int i, j, n, rc;
  17.   struct timeval tv;
  18.   fd_set tmp;
  19.  
  20.   args.nfds = nfds;
  21.   args.readfds = readfds;
  22.   args.writefds = writefds;
  23.   args.exceptfds = exceptfds;
  24.   args.timeout = timeout;
  25.   n = MIN (FD_SETSIZE, _nfiles);
  26.   if (readfds != NULL)
  27.     {
  28.       FD_ZERO (&tmp);            /* no handles ready due to look ahead */
  29.       j = 0;
  30.       for (i = 0; i < n; ++i)
  31.         if (FD_ISSET (i, readfds) && _lookahead[i] >= 0)
  32.           {
  33.             FD_SET (i, &tmp);    /* handles ready due to look ahead */
  34.             ++j;
  35.           }
  36.       if (j != 0)                /* there are handles with active look ahead */
  37.         {
  38.           tv.tv_sec = 0; tv.tv_usec = 0;      /* immediately return */
  39.           args.timeout = &tv;
  40.           rc = __select (&args);
  41.           if (rc == -1)
  42.             return (-1);
  43.           else if (rc == 0)      /* no handles ready -> use look ahead */
  44.             *readfds = tmp;
  45.           else                   /* merge */
  46.             for (i = 0; i < n; ++i)
  47.               if (FD_ISSET (i, &tmp))
  48.                 FD_SET (i, readfds);
  49.           return (rc);
  50.         }
  51.     }
  52.   return (__select (&args));
  53. }
  54.