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(fds, nfds, timeout)
44 fd_set rfd,wfd,efd,ifd;
45 struct timeval timebuf;
46 struct timeval *tbuf = (struct timeval *)0;
58 for(i = 0 ; i < nfds ; i++) {
59 int events = fds[i].events;
64 if(fd < 0 || FD_ISSET(fd, &ifd))
70 if(events & POLL_CAN_READ)
73 if(events & POLL_CAN_WRITE)
76 if(events & POLL_HAS_EXCP)
81 timebuf.tv_sec = timeout / 1000;
82 timebuf.tv_usec = (timeout % 1000) * 1000;
86 err = select(n+1,&rfd,&wfd,&efd,tbuf);
91 for(i = 0 ; i < nfds ; i++) {
93 if((fstat(fds[i].fd,&buf) < 0) && (errno == EBADF)) {
94 FD_SET(fds[i].fd, &ifd);
99 #endif /* HAS_FSTAT */
105 for(i = 0 ; i < nfds ; i++) {
106 int revents = (fds[i].events & POLL_EVENTS_MASK);
112 if(FD_ISSET(fd, &ifd))
115 if(!FD_ISSET(fd, &rfd))
116 revents &= ~POLL_CAN_READ;
118 if(!FD_ISSET(fd, &wfd))
119 revents &= ~POLL_CAN_WRITE;
121 if(!FD_ISSET(fd, &efd))
122 revents &= ~POLL_HAS_EXCP;
125 if((fds[i].revents = revents) != 0)
132 #endif /* EMULATE_POLL_WITH_SELECT */