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 * try to emulate it as closely as possible using select()
17 # include <sys/time.h>
22 #include <sys/types.h>
23 #if defined(HAS_SOCKET) && !defined(VMS) /* VMS handles sockets via vmsish.h */
24 # include <sys/socket.h>
29 #ifdef EMULATE_POLL_WITH_SELECT
31 # define POLL_CAN_READ (POLLIN | POLLRDNORM )
32 # define POLL_CAN_WRITE (POLLOUT | POLLWRNORM | POLLWRBAND )
33 # define POLL_HAS_EXCP (POLLRDBAND | POLLPRI )
35 # define POLL_EVENTS_MASK (POLL_CAN_READ | POLL_CAN_WRITE | POLL_HAS_EXCP)
38 poll(struct pollfd *fds, unsigned long nfds, int timeout)
41 fd_set rfd,wfd,efd,ifd;
42 struct timeval timebuf;
43 struct timeval *tbuf = (struct timeval *)0;
55 for(i = 0 ; i < nfds ; i++) {
56 int events = fds[i].events;
61 if(fd < 0 || FD_ISSET(fd, &ifd))
67 if(events & POLL_CAN_READ)
70 if(events & POLL_CAN_WRITE)
73 if(events & POLL_HAS_EXCP)
78 timebuf.tv_sec = timeout / 1000;
79 timebuf.tv_usec = (timeout % 1000) * 1000;
83 err = select(n+1,&rfd,&wfd,&efd,tbuf);
88 for(i = 0 ; i < nfds ; i++) {
90 if((fstat(fds[i].fd,&buf) < 0) && (errno == EBADF)) {
91 FD_SET(fds[i].fd, &ifd);
96 #endif /* HAS_FSTAT */
102 for(i = 0 ; i < nfds ; i++) {
103 int revents = (fds[i].events & POLL_EVENTS_MASK);
109 if(FD_ISSET(fd, &ifd))
112 if(!FD_ISSET(fd, &rfd))
113 revents &= ~POLL_CAN_READ;
115 if(!FD_ISSET(fd, &wfd))
116 revents &= ~POLL_CAN_WRITE;
118 if(!FD_ISSET(fd, &efd))
119 revents &= ~POLL_HAS_EXCP;
122 if((fds[i].revents = revents) != 0)
129 #endif /* EMULATE_POLL_WITH_SELECT */