Watt-32 tcp/ip  2.2 dev-rel.10
poll.c
Go to the documentation of this file.
1 
5 /* BSD sockets functionality for Watt-32 TCP/IP
6  *
7  * Copyright (c) 1997-2002 Gisle Vanem <gvanem@yahoo.no>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  * must display the following acknowledgement:
19  * This product includes software developed by Gisle Vanem
20  * Bergen, Norway.
21  *
22  * THIS SOFTWARE IS PROVIDED BY ME (Gisle Vanem) AND CONTRIBUTORS ``AS IS''
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL I OR CONTRIBUTORS BE LIABLE FOR ANY
26  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Version
34  *
35  * 1.0 : Sep 3, 1999 : G. Vanem
36  *
37  * This function is not in BSD, but Linux-specific (to aid
38  * porting Linux applications to Watt-32).
39  *
40  * poll() taken from HTTP-tunnel:
41  *
42  * Copyright (C) 1999 Lars Brinkhoff <lars@nocrew.org>.
43  *
44  */
45 
46 #include <sys/poll.h>
47 #include "socket.h"
48 
49 #if defined(USE_BSD_API)
50 
51 #undef read
52 #undef write
53 #undef except
54 
55 #ifdef __HIGHC__ /* set warning for stack-usage */
56 #pragma stack_size_warn (16000) /* ~3*MAX_SOCKETS */
57 #endif
58 
59 int poll (struct pollfd *p, int num, int timeout)
60 {
61  struct timeval tv;
62  fd_set read [NUM_SOCK_FDSETS];
63  fd_set write [NUM_SOCK_FDSETS];
64  fd_set except[NUM_SOCK_FDSETS];
65  int i, n, ret;
66 
67  memset (read, 0, sizeof(read));
68  memset (write, 0, sizeof(write));
69  memset (except, 0, sizeof(except));
70 
71  SOCK_DEBUGF (("\npoll: num %d, to %d", num, timeout));
72  SOCK_ENTER_SCOPE();
73 
74  if (num > MAX_SOCKETS)
75  {
76  SOCK_DEBUGF ((", EINVAL"));
77  SOCK_ERRNO (EINVAL);
78  return (-1);
79  }
80 
81  n = -1;
82  for (i = 0; i < num; i++)
83  {
84  if (p[i].fd < 0)
85  continue;
86  if (p[i].events & POLLIN)
87  FD_SET (p[i].fd, &read[0]);
88  if (p[i].events & POLLOUT)
89  FD_SET (p[i].fd, &write[0]);
90  if (p[i].events & POLLERR)
91  FD_SET (p[i].fd, &except[0]);
92  if (p[i].fd > n)
93  n = p[i].fd;
94  }
95 
96  if (n == -1)
97  ret = 0;
98 
99  else if (timeout < 0)
100  ret = select_s (n+1, read, write, except, NULL);
101 
102  else
103  {
104  tv.tv_sec = timeout / 1000;
105  tv.tv_usec = 1000 * (timeout % 1000);
106  ret = select_s (n+1, read, write, except, &tv);
107  }
108 
109  for (i = 0; ret >= 0 && i < num; i++)
110  {
111  p[i].revents = 0;
112  if (FD_ISSET (p[i].fd, &read[0]))
113  p[i].revents |= POLLIN;
114  if (FD_ISSET (p[i].fd, &write[0]))
115  p[i].revents |= POLLOUT;
116  if (FD_ISSET (p[i].fd, &except[0]))
117  p[i].revents |= POLLERR;
118  }
119 
120  SOCK_DEBUGF (("\nrc %d", ret));
121  SOCK_LEAVE_SCOPE();
122  return (ret);
123 }
124 #endif /* USE_BSD_API */
Definition: wtypes.h:197
int W32_CALL select_s(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
The select_s() function.
Definition: select.c:112
Definition: wtime.h:38
Definition: poll.h:14