1 /* Copyright (C) 1994, 1996, 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
21 #include <sys/types.h>
25 #include <sys/param.h>
28 /* Poll the file descriptors described by the NFDS structures starting at
29 FDS. If TIMEOUT is nonzero and not -1, allow TIMEOUT milliseconds for
30 an event to occur; if TIMEOUT is -1, block until an event occurs.
31 Returns the number of file descriptors with events, zero if timed out,
35 __poll (fds, nfds, timeout)
37 unsigned long int nfds;
40 static int max_fd_size;
42 fd_set *rset, *wset, *xset;
49 max_fd_size = __getdtablesize ();
51 bytes = howmany (max_fd_size, __NFDBITS);
52 rset = alloca (bytes);
53 wset = alloca (bytes);
54 xset = alloca (bytes);
56 /* We can't call FD_ZERO, since FD_ZERO only works with sets
57 of exactly __FD_SETSIZE size. */
58 __bzero (rset, bytes);
59 __bzero (wset, bytes);
60 __bzero (xset, bytes);
62 for (f = fds; f < &fds[nfds]; ++f)
65 if (f->events & POLLIN)
67 if (f->events & POLLOUT)
69 if (f->events & POLLPRI)
71 if (f->fd > maxfd && (f->events & (POLLIN|POLLOUT|POLLPRI)))
75 tv.tv_sec = timeout / 1000;
76 tv.tv_usec = (timeout % 1000) * 1000;
78 ready = __select (maxfd + 1, rset, wset, xset, timeout == -1 ? NULL : &tv);
80 for (f = fds; f < &fds[nfds]; ++f)
85 if (FD_ISSET (f->fd, rset))
87 if (FD_ISSET (f->fd, wset))
88 f->revents |= POLLOUT;
89 if (FD_ISSET (f->fd, xset))
90 f->revents |= POLLPRI;
97 weak_alias (__poll, poll)