6 #ifdef HAVE_SYS_SELECT_H
7 #include <sys/select.h>
10 #include "subst_poll.h"
12 /* This function pulled from Markus Gutschke's "wy60" package */
14 /* $Id: poll.c,v 1.4 2003/06/09 15:57:35 dnelson Exp $ */
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;
27 FD_ZERO(&exceptionFds);
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);
43 timeoutStruct.tv_sec = timeout/1000;
44 timeoutStruct.tv_usec = (timeout%1000) * 1000;
45 timeoutPtr = &timeoutStruct;
47 i = select(fd + 1, &readFds, &writeFds, &exceptionFds,
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;