]> git.donarmstrong.com Git - deb_pkgs/spamass-milter.git/blob - poll.c
Merge branch 'upstream'
[deb_pkgs/spamass-milter.git] / poll.c
1 #include "config.h"
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <sys/time.h>
6 #ifdef HAVE_SYS_SELECT_H
7 #include <sys/select.h>
8 #endif
9
10 #include "subst_poll.h"
11
12 /* This function pulled from Markus Gutschke's "wy60" package */
13
14 /* $Id: poll.c,v 1.4 2003/06/09 15:57:35 dnelson Exp $ */
15
16 int poll(struct pollfd *fds, unsigned long nfds, int timeout) {
17   // This emulation function is somewhat limited. Most notably, it will never
18   // report POLLERR, POLLHUP, or POLLNVAL. The calling code has to detect
19   // these error conditions by some other means (typically by read() or write()
20   // reporting end-of-file).
21   fd_set         readFds, writeFds, exceptionFds;
22   struct timeval *timeoutPtr, timeoutStruct;
23   int            i, rc, fd;
24
25   FD_ZERO(&readFds);
26   FD_ZERO(&writeFds);
27   FD_ZERO(&exceptionFds);
28   fd                      = -1;
29   for (i = nfds; i--; ) {
30     if (fds[i].events & POLLIN)
31       FD_SET(fds[i].fd, &readFds);
32     if (fds[i].events & POLLOUT)
33       FD_SET(fds[i].fd, &writeFds);
34     if (fds[i].events & POLLPRI)
35       FD_SET(fds[i].fd, &exceptionFds);
36     if (fds[i].fd > fd)
37       fd                  = fds[i].fd;
38     fds[i].revents        = 0;
39   }
40   if (timeout < 0)
41     timeoutPtr            = NULL;
42   else {
43     timeoutStruct.tv_sec  =  timeout/1000;
44     timeoutStruct.tv_usec = (timeout%1000) * 1000;
45     timeoutPtr            = &timeoutStruct;
46   }
47   i                       = select(fd + 1, &readFds, &writeFds, &exceptionFds,
48                                    timeoutPtr);
49   if (i <= 0)
50     rc                    = i;
51   else {
52     rc                    = 0;
53     for (i = nfds; i--; ) {
54       if (FD_ISSET(fds[i].fd, &readFds))
55         fds[i].revents   |= POLLIN;
56       if (FD_ISSET(fds[i].fd, &writeFds))
57         fds[i].revents   |= POLLOUT;
58       if (FD_ISSET(fds[i].fd, &exceptionFds))
59         fds[i].revents   |= POLLPRI;
60       if (fds[i].revents)
61         rc++;
62     }
63   }
64   return(rc);
65 }