x68/select.c


DEFINITIONS

This source file includes following functions.
  1. select


   1  /*
   2   * PROJECT C Library, X68000 PROGRAMMING INTERFACE DEFINITION
   3   * --------------------------------------------------------------------
   4   * This file is written by the Project C Library Group,  and completely
   5   * in public domain. You can freely use, copy, modify, and redistribute
   6   * the whole contents, without this notice.
   7   * --------------------------------------------------------------------
   8   * $Id: select.c,v 1.1.1.2 1999/01/20 04:59:39 matz Exp $
   9   */
  10  
  11  #ifndef __IOCS_INLINE__
  12  #define __IOCS_INLINE__
  13  #define __DOS_INLINE__
  14  #define __DOS_DOSCALL__
  15  #endif
  16  
  17  /* System headers */
  18  #include <errno.h>
  19  #include <fcntl.h>
  20  #include <string.h>
  21  #include <sys/dos.h>
  22  #include <sys/iocs.h>
  23  #include <sys/time.h>
  24  #include <sys/types.h>
  25  #if 0
  26  #include <sys/select.h>
  27  #include <sys/xsocket.h>
  28  #endif
  29  #include <sys/xunistd.h>
  30  
  31  /* Macros */
  32  #define XFD_ISSET(fd,fds) ((fds) && FD_ISSET ((fd), (fds)))
  33  #define isreadable(mode)  ((mode) == O_RDONLY || (mode) == O_RDWR)
  34  #define iswritable(mode)  ((mode) == O_WRONLY || (mode) == O_RDWR)
  35  #ifndef _POSIX_FD_SETSIZE
  36  #define _POSIX_FD_SETSIZE OPEN_MAX
  37  #endif
  38  
  39  /* Functions */
  40  int
  41  select (int fds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
  42  {
  43    fd_set oread, owrite, oexcept;
  44    int ticks, start;
  45    int nfds;
  46  
  47    if (fds > _POSIX_FD_SETSIZE)
  48      {
  49        errno = EINVAL;
  50        return -1;
  51      }
  52  
  53    FD_ZERO (&oread);
  54    FD_ZERO (&owrite);
  55    FD_ZERO (&oexcept);
  56  
  57    nfds = 0;
  58    ticks = -1;
  59  
  60    if (timeout)
  61      {
  62        ticks = timeout->tv_sec * 100 + timeout->tv_usec / 10000;
  63        if (ticks < 0)
  64          {
  65            errno = EINVAL;
  66            return -1;
  67          }
  68      }
  69  
  70    start = _iocs_ontime ();
  71    for (;;)
  72      {
  73        {
  74          int fd;
  75          
  76          for (fd = 0; fd < fds; fd++)
  77            {
  78              int accmode;
  79              
  80              if (_fddb[fd].inuse == _FD_NOTUSED)
  81                continue;
  82              
  83              accmode = _fddb[fd].oflag & O_ACCMODE;
  84              
  85              if (isatty (fd))
  86                {
  87                  if (XFD_ISSET (fd, rfds) && isreadable (accmode) && _dos_k_keysns ())
  88                    {
  89                      FD_SET (fd, &oread);
  90                      nfds++;
  91                    }
  92                  
  93                  if (XFD_ISSET (fd, wfds) && iswritable (accmode))
  94                    {
  95                      FD_SET (fd, &owrite);
  96                      nfds++;
  97                    }
  98                }
  99  #if 0
 100              else if (_fddb[fd].sockno >= 0)
 101                {
 102                  if (XFD_ISSET (fd, rfds) && _socklen (_fddb[fd].sockno, 0))
 103                    {
 104                      FD_SET (fd, &oread);
 105                      nfds++;
 106                    }
 107  
 108                  if (XFD_ISSET (fd, wfds) /* && _socklen (_fddb[fd].sockno, 1) == 0 */)
 109                    {
 110                      FD_SET (fd, &owrite);
 111                      nfds++;
 112                    }
 113                }
 114  #endif
 115              else
 116                {
 117                  if (XFD_ISSET (fd, rfds) && isreadable (accmode) && _dos_ioctrlis (fd))
 118                    {
 119                      FD_SET (fd, &oread);
 120                      nfds++;
 121                    }
 122                  
 123                  if (XFD_ISSET (fd, wfds) && iswritable (accmode) && _dos_ioctrlos (fd))
 124                    {
 125                      FD_SET (fd, &owrite);
 126                      nfds++;
 127                    }
 128                }
 129            }
 130        }
 131  
 132        {
 133          int rest;
 134          
 135          if ((rest = (_iocs_ontime () - start) % 8640000) < 0)
 136            rest += 8640000;
 137          
 138          if (nfds != 0)
 139            {
 140              if (ticks >= 0)
 141                {
 142                  int left;
 143                  
 144                  if ((left = ticks - rest) < 0)
 145                    left = 0;
 146                  
 147                  timeout->tv_sec = left / 100;
 148                  timeout->tv_usec = (left % 100) * 10000;
 149                }
 150              
 151              if (rfds)
 152                *rfds = oread;
 153              if (wfds)
 154                *wfds = owrite;
 155              if (efds)
 156                *efds = oexcept;
 157              
 158              return nfds;
 159            }
 160          
 161          if (ticks >= 0 && rest > ticks)
 162            return 0;
 163        }
 164  
 165        _dos_change_pr ();
 166      }
 167  }