29 #include <sys/types.h> 33 #include <sys/param.h> 43 poll(
struct pollfd* fds,
unsigned long int nfds,
int timeout)
45 static int max_fd_size;
47 fd_set *rset, *wset, *xset;
54 max_fd_size = getdtablesize ();
56 bytes = howmany (max_fd_size, NFDBITS);
57 rset = alloca (bytes);
58 wset = alloca (bytes);
59 xset = alloca (bytes);
67 for (f = fds; f < &fds[nfds]; ++f)
72 if (f->
fd >= max_fd_size)
77 fd_set *nrset, *nwset, *nxset;
80 max_fd_size = roundup (f->
fd, NFDBITS);
81 nbytes = howmany (max_fd_size, NFDBITS);
83 nrset = alloca (nbytes);
84 nwset = alloca (nbytes);
85 nxset = alloca (nbytes);
87 bzero ((
char *) nrset + bytes, nbytes - bytes);
88 bzero ((
char *) nwset + bytes, nbytes - bytes);
89 bzero ((
char *) nxset + bytes, nbytes - bytes);
91 rset = memcpy (nrset, rset, bytes);
92 wset = memcpy (nwset, wset, bytes);
93 xset = memcpy (nxset, xset, bytes);
101 FD_SET (f->
fd, wset);
103 FD_SET (f->
fd, xset);
109 tv.tv_sec = timeout / 1000;
110 tv.tv_usec = (timeout % 1000) * 1000;
114 ready = select (maxfd + 1, rset, wset, xset,
115 timeout == -1 ? NULL : &tv);
119 if (ready == -1 && errno == EBADF)
121 fd_set *sngl_rset = alloca (bytes);
122 fd_set *sngl_wset = alloca (bytes);
123 fd_set *sngl_xset = alloca (bytes);
124 struct timeval sngl_tv;
140 for (f = fds; f < &fds[nfds]; ++f)
146 bzero (sngl_rset, bytes);
147 bzero (sngl_wset, bytes);
148 bzero (sngl_xset, bytes);
151 FD_SET (f->
fd, sngl_rset);
153 FD_SET (f->
fd, sngl_wset);
155 FD_SET (f->
fd, sngl_xset);
157 n = select (f->
fd + 1, sngl_rset, sngl_wset, sngl_xset,
163 FD_SET (f->
fd, rset);
165 FD_SET (f->
fd, wset);
167 FD_SET (f->
fd, xset);
174 else if (errno == EBADF)
185 for (f = fds; f < &fds[nfds]; ++f)
189 if (FD_ISSET (f->
fd, rset))
191 if (FD_ISSET (f->
fd, wset))
193 if (FD_ISSET (f->
fd, xset))
int poll(struct pollfd *fds, unsigned long int nfds, int timeout)