home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume32 / pol / part01 / pol.lib.c < prev    next >
C/C++ Source or Header  |  1992-10-18  |  3KB  |  134 lines

  1. /*
  2.     pol.c    - poll/select() C interface
  3.  
  4.     Bruce Momjian (root%candle.uucp@bts.com
  5.  
  6. */
  7.  
  8. /* tabs = 4 */
  9. /* Include files */
  10. #include <fcntl.h>
  11.  
  12. #include <sys/pol.h>
  13. #include <sys/selec.h>
  14. #include <poll.h>
  15.  
  16. static int pol_fd = -1;
  17.  
  18.  
  19. /*---------------------------------------------------------------------------
  20. **
  21. **    pol()
  22. **
  23. **--------------------------------------------------------------------------*/
  24. pol(fds, nfds, timeout)
  25. struct pollfd *fds;
  26. unsigned long nfds;
  27. int timeout;
  28. {
  29.     struct polfd    pl;
  30.     int i, ret;
  31.     
  32.     if ( pol_fd == -1 && (pol_fd=open("/dev/pol",O_RDONLY)) == -1)
  33.         return -1;
  34.  
  35.     pl.rfds = pl.wfds = 0;
  36.     pl.timeout = timeout;
  37.  
  38.     for (i = 0; i < nfds; i++)
  39.     {
  40.         if ((fds[i].events & POLLIN) != 0)
  41.             pl.rfds |= (1 << fds[i].fd);
  42.         if ((fds[i].events & POLLOUT) != 0)
  43.             pl.wfds |= (1 << fds[i].fd);
  44.     }
  45.     
  46.     if ((ret=ioctl(pol_fd, POL_FDS, &pl)) == -1)
  47.          return -1;
  48.  
  49.     if (ret == 0 || ret == -1)
  50.         pl.rfds = pl.wfds = 0;
  51.     
  52.     for (i=0; i < nfds; i++)
  53.     {
  54.         fds[i].revents = 0;
  55.         if ( (pl.rfds & (1 << fds[i].fd)) != 0)
  56.             fds[i].revents |= POLLIN;
  57.         if ( (pl.wfds & (1 << fds[i].fd)) != 0)
  58.             fds[i].revents |= POLLOUT;
  59.     }
  60.     return ret;
  61. }
  62.     
  63. /*---------------------------------------------------------------------------
  64. **
  65. **    selec()
  66. **
  67. **--------------------------------------------------------------------------*/
  68. selec(nfds, rfds, wfds, expfds, timeout)
  69. unsigned long nfds, *rfds, *wfds, *expfds; /* exceptions not implemented */
  70. struct timeval *timeout;
  71. {
  72.     struct polfd    pl;
  73.     int i, ored, ret;
  74.     
  75.     if ( pol_fd == -1 && (pol_fd=open("/dev/pol",O_RDONLY)) == -1)
  76.         return -1;
  77.  
  78.     pl.rfds = pl.wfds = 0;
  79.     if (timeout == 0L)
  80.         pl.timeout = -1;
  81.     else
  82.         pl.timeout = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
  83.  
  84.     for (i = 0; i < nfds; i++)
  85.     {
  86.         if ( (*rfds & (1 << i)) != 0)
  87.             pl.rfds |= 1 << i;
  88.         if ( (*wfds & (1 << i)) != 0)
  89.             pl.wfds |= 1 << i;
  90.     }
  91.     
  92.     if ((ret=ioctl(pol_fd, POL_FDS, &pl)) == -1)
  93.          return -1;
  94.  
  95.     *rfds = *wfds = *expfds = 0;
  96.  
  97.     if (ret != 0 && ret != -1)
  98.     {
  99.         ret = 0;
  100.         ored = pl.rfds | pl.wfds;
  101.         for (i=0; i < nfds; i++)
  102.         {
  103.             if ( (pl.rfds & (1 << i)) != 0)
  104.                 (*rfds) |= (1 << i);
  105.             if ( (pl.wfds & (1 << i)) != 0)
  106.                 (*wfds) |= (1 << i);
  107.             if ( (ored & (1 << i)) != 0)
  108.                 ret++;
  109.         }
  110.     }
  111.     return ret;
  112. }
  113.     
  114. /*---------------------------------------------------------------------------
  115. **
  116. **    polclose()
  117. **
  118. **--------------------------------------------------------------------------*/
  119. polclose()
  120. {
  121.     if (pol_fd != -1)
  122.         return close(pol_fd);
  123.     return 0;
  124. }
  125. /*---------------------------------------------------------------------------
  126. **
  127. **    selecclose()
  128. **
  129. **--------------------------------------------------------------------------*/
  130. selecclose()
  131. {
  132.     return polclose();
  133. }
  134.