libport: Replacement for poll().
[wine/wine64.git] / libs / port / poll.c
blobb633a0d90ef74607bfde52aeb52b3d51edae2a40
1 /*
2 * poll function
4 * Copyright 2008 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #ifndef HAVE_POLL
26 #ifdef HAVE_WINSOCK2_H
27 #include <winsock2.h>
28 #endif
29 #ifdef HAVE_SYS_TIME_H
30 #include <sys/time.h>
31 #endif
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
36 int poll( struct pollfd *fds, unsigned int count, int timeout )
38 fd_set read_set, write_set, except_set;
39 unsigned int i;
40 int maxfd = -1, ret;
42 FD_ZERO( &read_set );
43 FD_ZERO( &write_set );
44 FD_ZERO( &except_set );
46 for (i = 0; i < count; i++)
48 if (fds[i].fd == -1) continue;
49 if (fds[i].events & (POLLIN|POLLPRI)) FD_SET( fds[i].fd, &read_set );
50 if (fds[i].events & POLLOUT) FD_SET( fds[i].fd, &write_set );
51 FD_SET( fds[i].fd, &except_set ); /* POLLERR etc. are always selected */
52 if (fds[i].fd > maxfd) maxfd = fds[i].fd;
54 if (timeout != -1)
56 struct timeval tv;
57 tv.tv_sec = timeout / 1000;
58 tv.tv_usec = timeout % 1000;
59 ret = select( maxfd + 1, &read_set, &write_set, &except_set, &tv );
61 else ret = select( maxfd + 1, &read_set, &write_set, &except_set, NULL );
63 if (ret >= 0)
65 for (i = 0; i < count; i++)
67 fds[i].revents = 0;
68 if (fds[i].fd == -1) continue;
69 if (FD_ISSET( fds[i].fd, &read_set )) fds[i].revents |= POLLIN;
70 if (FD_ISSET( fds[i].fd, &write_set )) fds[i].revents |= POLLOUT;
71 if (FD_ISSET( fds[i].fd, &except_set )) fds[i].revents |= POLLERR;
74 return ret;
77 #endif /* HAVE_POLL */