1
6
7
12
13
14
15
20
21
22
23
24
25
26
27
31
32
33
34
35
36
37
38
39
43
44
49
50
55
56
61
62
63
64
65
85
86
87
/* ... */
#include <stddef.h>
#include <sys/poll.h>
#include <sys/select.h>
#include <sys/errno.h>
#include <sys/param.h>5 includes
int poll(struct pollfd *fds, nfds_t nfds, int timeout)
{
struct timeval tv = {
.tv_sec = timeout / 1000,
.tv_usec = (timeout % 1000) * 1000,
}{...};
int max_fd = -1;
fd_set readfds;
fd_set writefds;
fd_set errorfds;
struct _reent* r = __getreent();
int ret = 0;
if (fds == NULL) {
__errno_r(r) = ENOENT;
return -1;
}{...}
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&errorfds);
for (unsigned int i = 0; i < nfds; ++i) {
fds[i].revents = 0;
if (fds[i].fd < 0) {
continue;
}{...}
if (fds[i].fd >= FD_SETSIZE) {
fds[i].revents |= POLLNVAL;
++ret;
continue;
}{...}
if (fds[i].events & (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
FD_SET(fds[i].fd, &readfds);
FD_SET(fds[i].fd, &errorfds);
max_fd = MAX(max_fd, fds[i].fd);
}{...}
if (fds[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
FD_SET(fds[i].fd, &writefds);
FD_SET(fds[i].fd, &errorfds);
max_fd = MAX(max_fd, fds[i].fd);
}{...}
}{...}
const int select_ret = select(max_fd + 1, &readfds, &writefds, &errorfds, timeout < 0 ? NULL : &tv);
if (select_ret > 0) {
ret += select_ret;
for (unsigned int i = 0; i < nfds; ++i) {
if (FD_ISSET(fds[i].fd, &readfds)) {
fds[i].revents |= POLLIN;
}{...}
if (FD_ISSET(fds[i].fd, &writefds)) {
fds[i].revents |= POLLOUT;
}{...}
if (FD_ISSET(fds[i].fd, &errorfds)) {
fds[i].revents |= POLLERR;
}{...}
}{...}
}{...} else {
ret = select_ret;
}{...}
return ret;
}{ ... }