1 /*
2 Copyright (c) 1982, 1986, 1993
3 The Regents of the University of California.  All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. Neither the name of the University nor the names of its contributors
14 may be used to endorse or promote products derived from this software
15 without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 SUCH DAMAGE.
28  */
29 
30 #ifndef _SYS_SELECT_H
31 #define _SYS_SELECT_H
32 
33 /* We don't define fd_set and friends if we are compiling POSIX
34    source, or if we have included (or may include as indicated
35    by __USE_W32_SOCKETS) the W32api winsock[2].h header which
36    defines Windows versions of them.   Note that a program which
37    includes the W32api winsock[2].h header must know what it is doing;
38    it must not call the Cygwin select function.
39 */
40 # if !(defined (_WINSOCK_H) || defined (_WINSOCKAPI_) || defined (__USE_W32_SOCKETS))
41 
42 #include <sys/cdefs.h>
43 #include <sys/_sigset.h>
44 #include <sys/_timeval.h>
45 #include <sys/timespec.h>
46 
47 #if !defined(_SIGSET_T_DECLARED)
48 #define	_SIGSET_T_DECLARED
49 typedef	__sigset_t	sigset_t;
50 #endif
51 
52 #  define _SYS_TYPES_FD_SET
53 /*
54  * Select uses bit masks of file descriptors in longs.
55  * These macros manipulate such bit fields (the filesystem macros use chars).
56  * FD_SETSIZE may be defined by the user, but the default here
57  * should be enough for most uses.
58  */
59 #ifndef FD_SETSIZE
60 # ifdef __CYGWIN__
61 #  define FD_SETSIZE	1024
62 # else
63 #  define FD_SETSIZE	64
64 # endif
65 #endif
66 
67 typedef unsigned long	__fd_mask;
68 #if __BSD_VISIBLE
69 typedef __fd_mask	fd_mask;
70 #endif
71 
72 #define _NFDBITS	((int)sizeof(__fd_mask) * 8) /* bits per mask */
73 #if __BSD_VISIBLE
74 #define NFDBITS		_NFDBITS
75 #endif
76 
77 #ifndef	_howmany
78 #define	_howmany(x,y)	(((x) + ((y) - 1)) / (y))
79 #endif
80 
81 typedef	struct fd_set {
82 	__fd_mask	__fds_bits[_howmany(FD_SETSIZE, _NFDBITS)];
83 } fd_set;
84 #if __BSD_VISIBLE
85 #define fds_bits	__fds_bits
86 #endif
87 
88 #define __fdset_mask(n)	((__fd_mask)1 << ((n) % _NFDBITS))
89 #define FD_CLR(n, p)	((p)->__fds_bits[(n)/_NFDBITS] &= ~__fdset_mask(n))
90 #if __BSD_VISIBLE
91 #define FD_COPY(f, t)	(void)(*(t) = *(f))
92 #endif
93 #define FD_ISSET(n, p)	(((p)->__fds_bits[(n)/_NFDBITS] & __fdset_mask(n)) != 0)
94 #define FD_SET(n, p)	((p)->__fds_bits[(n)/_NFDBITS] |= __fdset_mask(n))
95 #define FD_ZERO(p) do {					\
96         fd_set *_p;					\
97         __size_t _n;					\
98 							\
99         _p = (p);					\
100         _n = _howmany(FD_SETSIZE, _NFDBITS);		\
101         while (_n > 0)					\
102                 _p->__fds_bits[--_n] = 0;		\
103 } while (0)
104 
105 #if !defined (__INSIDE_CYGWIN_NET__)
106 
107 __BEGIN_DECLS
108 
109 int select __P ((int __n, fd_set *__readfds, fd_set *__writefds,
110 		 fd_set *__exceptfds, struct timeval *__timeout));
111 #if __POSIX_VISIBLE >= 200112
112 int pselect __P ((int __n, fd_set *__readfds, fd_set *__writefds,
113 		  fd_set *__exceptfds, const struct timespec *__timeout,
114 		  const sigset_t *__set));
115 #endif
116 
117 __END_DECLS
118 
119 #endif /* !__INSIDE_CYGWIN_NET__ */
120 
121 #endif /* !(_WINSOCK_H || _WINSOCKAPI_ || __USE_W32_SOCKETS) */
122 
123 #endif /* sys/select.h */
124