4 * Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the same terms as Perl itself.
8 * For systems that do not have the poll() system call (for example Linux
9 * kernels < v2.1.23) try to emulate it as closely as possible using select()
19 # include <sys/time.h>
24 #include <sys/types.h>
25 #if defined(HAS_SOCKET) && !defined(VMS) && !defined(ultrix) /* VMS handles sockets via vmsish.h, ULTRIX dies of socket struct redefinitions */
26 # include <sys/socket.h>
33 #include <sys/select.h>
37 #ifdef EMULATE_POLL_WITH_SELECT
39 # define POLL_CAN_READ (POLLIN | POLLRDNORM )
40 # define POLL_CAN_WRITE (POLLOUT | POLLWRNORM | POLLWRBAND )
41 # define POLL_HAS_EXCP (POLLRDBAND | POLLPRI )
43 # define POLL_EVENTS_MASK (POLL_CAN_READ | POLL_CAN_WRITE | POLL_HAS_EXCP)
46 poll(struct pollfd *fds, unsigned long nfds, int timeout)
49 fd_set rfd,wfd,efd,ifd;
50 struct timeval timebuf;
51 struct timeval *tbuf = (struct timeval *)0;
63 for(i = 0 ; i < (int)nfds ; i++) {
64 int events = fds[i].events;
69 if(fd < 0 || FD_ISSET(fd, &ifd))
75 if(events & POLL_CAN_READ)
78 if(events & POLL_CAN_WRITE)
81 if(events & POLL_HAS_EXCP)
86 timebuf.tv_sec = timeout / 1000;
87 timebuf.tv_usec = (timeout % 1000) * 1000;
91 err = select(n+1,&rfd,&wfd,&efd,tbuf);
96 for(i = 0 ; i < nfds ; i++) {
98 if((fstat(fds[i].fd,&buf) < 0) && (errno == EBADF)) {
99 FD_SET(fds[i].fd, &ifd);
104 #endif /* HAS_FSTAT */
110 for(i = 0 ; i < (int)nfds ; i++) {
111 int revents = (fds[i].events & POLL_EVENTS_MASK);
117 if(FD_ISSET(fd, &ifd))
120 if(!FD_ISSET(fd, &rfd))
121 revents &= ~POLL_CAN_READ;
123 if(!FD_ISSET(fd, &wfd))
124 revents &= ~POLL_CAN_WRITE;
126 if(!FD_ISSET(fd, &efd))
127 revents &= ~POLL_HAS_EXCP;
130 if((fds[i].revents = revents) != 0)
137 #endif /* EMULATE_POLL_WITH_SELECT */