Update.
[platform/upstream/glibc.git] / sysdeps / unix / bsd / poll.c
1 /* Copyright (C) 1994, 1996, 1997, 1998 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
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.
8
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.
13
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.  */
18
19 #include <alloca.h>
20 #include <sys/poll.h>
21 #include <sys/types.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <sys/time.h>
25 #include <sys/param.h>
26 #include <unistd.h>
27
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,
32    or -1 for errors.  */
33
34 int
35 __poll (fds, nfds, timeout)
36      struct pollfd *fds;
37      unsigned long int nfds;
38      int timeout;
39 {
40   static int max_fd_size;
41   struct timeval tv;
42   fd_set *rset, *wset, *xset;
43   struct pollfd *f;
44   int ready;
45   int maxfd = 0;
46   int bytes;
47
48   if (!max_fd_size)
49     max_fd_size = __getdtablesize ();
50
51   bytes = howmany (max_fd_size, __NFDBITS);
52   rset = alloca (bytes);
53   wset = alloca (bytes);
54   xset = alloca (bytes);
55
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);
61
62   for (f = fds; f < &fds[nfds]; ++f)
63     if (f->fd >= 0)
64       {
65         if (f->events & POLLIN)
66           FD_SET (f->fd, rset);
67         if (f->events & POLLOUT)
68           FD_SET (f->fd, wset);
69         if (f->events & POLLPRI)
70           FD_SET (f->fd, xset);
71         if (f->fd > maxfd && (f->events & (POLLIN|POLLOUT|POLLPRI)))
72           maxfd = f->fd;
73       }
74
75   tv.tv_sec = timeout / 1000;
76   tv.tv_usec = (timeout % 1000) * 1000;
77
78   ready = __select (maxfd + 1, rset, wset, xset, timeout == -1 ? NULL : &tv);
79   if (ready > 0)
80     for (f = fds; f < &fds[nfds]; ++f)
81       {
82         f->revents = 0;
83         if (f->fd >= 0)
84           {
85             if (FD_ISSET (f->fd, rset))
86               f->revents |= POLLIN;
87             if (FD_ISSET (f->fd, wset))
88               f->revents |= POLLOUT;
89             if (FD_ISSET (f->fd, xset))
90               f->revents |= POLLPRI;
91           }
92       }
93
94   return ready;
95 }
96 #ifndef __poll
97 weak_alias (__poll, poll)
98 #endif