1a96d7823Smrg/*
2a96d7823Smrg * Copyright 1990 Network Computing Devices;
3a96d7823Smrg * Portions Copyright 1987 by Digital Equipment Corporation
4a96d7823Smrg */
5a96d7823Smrg
6a96d7823Smrg/*
7a96d7823Smrg
8a96d7823SmrgCopyright 1987, 1994, 1998  The Open Group
9a96d7823Smrg
10a96d7823SmrgPermission to use, copy, modify, distribute, and sell this software and its
11a96d7823Smrgdocumentation for any purpose is hereby granted without fee, provided that
12a96d7823Smrgthe above copyright notice appear in all copies and that both that
13a96d7823Smrgcopyright notice and this permission notice appear in supporting
14a96d7823Smrgdocumentation.
15a96d7823Smrg
16a96d7823SmrgThe above copyright notice and this permission notice shall be included
17a96d7823Smrgin all copies or substantial portions of the Software.
18a96d7823Smrg
19a96d7823SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20a96d7823SmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21a96d7823SmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22a96d7823SmrgIN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
23a96d7823SmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24a96d7823SmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25a96d7823SmrgOTHER DEALINGS IN THE SOFTWARE.
26a96d7823Smrg
27a96d7823SmrgExcept as contained in this notice, the name of The Open Group shall
28a96d7823Smrgnot be used in advertising or otherwise to promote the sale, use or
29a96d7823Smrgother dealings in this Software without prior written authorization
30a96d7823Smrgfrom The Open Group.
31a96d7823Smrg
32a96d7823Smrg*/
33a96d7823Smrg
34a96d7823Smrg/*
35a96d7823Smrg * FSlib networking & os include file
36a96d7823Smrg */
37a96d7823Smrg
38a96d7823Smrg#include <X11/Xtrans/Xtrans.h>
39a96d7823Smrg
40a96d7823Smrg#ifndef WIN32
41a96d7823Smrg
42a96d7823Smrg/*
43a96d7823Smrg * makedepend screws up on #undef OPEN_MAX, so we define a new symbol
44a96d7823Smrg */
45a96d7823Smrg
46a96d7823Smrg# ifndef FONT_OPEN_MAX
47a96d7823Smrg
48a96d7823Smrg#  ifdef _POSIX_SOURCE
49a96d7823Smrg#   include <limits.h>
50a96d7823Smrg#  else
51a96d7823Smrg#   define _POSIX_SOURCE
52a96d7823Smrg#   include <limits.h>
53a96d7823Smrg#   undef _POSIX_SOURCE
54a96d7823Smrg#  endif
55a96d7823Smrg#  ifndef SIZE_MAX
56a96d7823Smrg#   ifdef ULONG_MAX
57a96d7823Smrg#    define SIZE_MAX ULONG_MAX
58a96d7823Smrg#   else
59a96d7823Smrg#    define SIZE_MAX UINT_MAX
60a96d7823Smrg#   endif
61a96d7823Smrg#  endif
62a96d7823Smrg#  ifndef OPEN_MAX
63a96d7823Smrg#   if defined(SVR4)
64a96d7823Smrg#    define OPEN_MAX 256
65a96d7823Smrg#   else
66a96d7823Smrg#    include <sys/param.h>
67a96d7823Smrg#    ifndef OPEN_MAX
686a46240fSmrg#     ifdef NOFILE
696a46240fSmrg#      define OPEN_MAX NOFILE
70a96d7823Smrg#     else
716a46240fSmrg#      define OPEN_MAX NOFILES_MAX
72a96d7823Smrg#     endif
73a96d7823Smrg#    endif
74a96d7823Smrg#   endif
75a96d7823Smrg#  endif
76a96d7823Smrg
77a96d7823Smrg#  if OPEN_MAX > 256
78a96d7823Smrg#   define FONT_OPEN_MAX 256
79a96d7823Smrg#  else
80a96d7823Smrg#   define FONT_OPEN_MAX OPEN_MAX
81a96d7823Smrg#  endif
82a96d7823Smrg
83a96d7823Smrg# endif /* FONT_OPEN_MAX */
84a96d7823Smrg
85a96d7823Smrg# ifdef WORD64
86a96d7823Smrg#  define NMSKBITS 64
87a96d7823Smrg# else
88a96d7823Smrg#  define NMSKBITS 32
89a96d7823Smrg# endif
90a96d7823Smrg
91a96d7823Smrg# define MSKCNT ((FONT_OPEN_MAX + NMSKBITS - 1) / NMSKBITS)
92a96d7823Smrg
93a96d7823Smrgtypedef unsigned long FdSet[MSKCNT];
94a96d7823Smrgtypedef FdSet FdSetPtr;
95a96d7823Smrg
96a96d7823Smrg# if (MSKCNT==1)
97a96d7823Smrg#  define BITMASK(i) (1 << (i))
98a96d7823Smrg#  define MASKIDX(i) 0
99a96d7823Smrg# endif
100a96d7823Smrg
101a96d7823Smrg# if (MSKCNT>1)
102a96d7823Smrg#  define BITMASK(i) (1 << ((i) & (NMSKBITS - 1)))
103a96d7823Smrg#  define MASKIDX(i) ((i) / NMSKBITS)
104a96d7823Smrg# endif
105a96d7823Smrg
106a96d7823Smrg# define MASKWORD(buf, i) buf[MASKIDX(i)]
107a96d7823Smrg# define BITSET(buf, i) MASKWORD(buf, i) |= BITMASK(i)
108a96d7823Smrg# define BITCLEAR(buf, i) MASKWORD(buf, i) &= ~BITMASK(i)
109a96d7823Smrg# define GETBIT(buf, i) (MASKWORD(buf, i) & BITMASK(i))
110a96d7823Smrg
111a96d7823Smrg# if (MSKCNT==1)
112a96d7823Smrg#  define COPYBITS(src, dst) dst[0] = src[0]
113a96d7823Smrg#  define CLEARBITS(buf) buf[0] = 0
114a96d7823Smrg#  define MASKANDSETBITS(dst, b1, b2) dst[0] = (b1[0] & b2[0])
115a96d7823Smrg#  define ORBITS(dst, b1, b2) dst[0] = (b1[0] | b2[0])
116a96d7823Smrg#  define UNSETBITS(dst, b1) (dst[0] &= ~b1[0])
117a96d7823Smrg#  define ANYSET(src) (src[0])
118a96d7823Smrg# endif
119a96d7823Smrg
120a96d7823Smrg# if (MSKCNT==2)
121a96d7823Smrg#  define COPYBITS(src, dst) { dst[0] = src[0]; dst[1] = src[1]; }
122a96d7823Smrg#  define CLEARBITS(buf) { buf[0] = 0; buf[1] = 0; }
123a96d7823Smrg#  define MASKANDSETBITS(dst, b1, b2)  {\
124a96d7823Smrg		      dst[0] = (b1[0] & b2[0]);\
125a96d7823Smrg		      dst[1] = (b1[1] & b2[1]); }
126a96d7823Smrg#  define ORBITS(dst, b1, b2)  {\
127a96d7823Smrg		      dst[0] = (b1[0] | b2[0]);\
128a96d7823Smrg		      dst[1] = (b1[1] | b2[1]); }
129a96d7823Smrg#  define UNSETBITS(dst, b1) {\
130a96d7823Smrg                      dst[0] &= ~b1[0]; \
131a96d7823Smrg                      dst[1] &= ~b1[1]; }
132a96d7823Smrg#  define ANYSET(src) (src[0] || src[1])
133a96d7823Smrg# endif
134a96d7823Smrg
135a96d7823Smrg# if (MSKCNT==3)
136a96d7823Smrg#  define COPYBITS(src, dst) { dst[0] = src[0]; dst[1] = src[1]; \
137a96d7823Smrg			     dst[2] = src[2]; }
138a96d7823Smrg#  define CLEARBITS(buf) { buf[0] = 0; buf[1] = 0; buf[2] = 0; }
139a96d7823Smrg#  define MASKANDSETBITS(dst, b1, b2)  {\
140a96d7823Smrg		      dst[0] = (b1[0] & b2[0]);\
141a96d7823Smrg		      dst[1] = (b1[1] & b2[1]);\
142a96d7823Smrg		      dst[2] = (b1[2] & b2[2]); }
143a96d7823Smrg#  define ORBITS(dst, b1, b2)  {\
144a96d7823Smrg		      dst[0] = (b1[0] | b2[0]);\
145a96d7823Smrg		      dst[1] = (b1[1] | b2[1]);\
146a96d7823Smrg		      dst[2] = (b1[2] | b2[2]); }
147a96d7823Smrg#  define UNSETBITS(dst, b1) {\
148a96d7823Smrg                      dst[0] &= ~b1[0]; \
149a96d7823Smrg                      dst[1] &= ~b1[1]; \
150a96d7823Smrg                      dst[2] &= ~b1[2]; }
151a96d7823Smrg#  define ANYSET(src) (src[0] || src[1] || src[2])
152a96d7823Smrg# endif
153a96d7823Smrg
154a96d7823Smrg# if (MSKCNT==4)
155a96d7823Smrg#  define COPYBITS(src, dst) dst[0] = src[0]; dst[1] = src[1]; \
156a96d7823Smrg			   dst[2] = src[2]; dst[3] = src[3]
157a96d7823Smrg#  define CLEARBITS(buf) buf[0] = 0; buf[1] = 0; buf[2] = 0; buf[3] = 0
158a96d7823Smrg#  define MASKANDSETBITS(dst, b1, b2)  \
159a96d7823Smrg                      dst[0] = (b1[0] & b2[0]);\
160a96d7823Smrg                      dst[1] = (b1[1] & b2[1]);\
161a96d7823Smrg                      dst[2] = (b1[2] & b2[2]);\
162a96d7823Smrg                      dst[3] = (b1[3] & b2[3])
163a96d7823Smrg#  define ORBITS(dst, b1, b2)  \
164a96d7823Smrg                      dst[0] = (b1[0] | b2[0]);\
165a96d7823Smrg                      dst[1] = (b1[1] | b2[1]);\
166a96d7823Smrg                      dst[2] = (b1[2] | b2[2]);\
167a96d7823Smrg                      dst[3] = (b1[3] | b2[3])
168a96d7823Smrg#  define UNSETBITS(dst, b1) \
169a96d7823Smrg                      dst[0] &= ~b1[0]; \
170a96d7823Smrg                      dst[1] &= ~b1[1]; \
171a96d7823Smrg                      dst[2] &= ~b1[2]; \
172a96d7823Smrg                      dst[3] &= ~b1[3]
173a96d7823Smrg#  define ANYSET(src) (src[0] || src[1] || src[2] || src[3])
174a96d7823Smrg# endif
175a96d7823Smrg
176a96d7823Smrg# if (MSKCNT>4)
177a96d7823Smrg#  define COPYBITS(src, dst) memmove((caddr_t) dst, (caddr_t) src,\
178a96d7823Smrg				   MSKCNT*sizeof(long))
179a96d7823Smrg#  define CLEARBITS(buf) bzero((caddr_t) buf, MSKCNT*sizeof(long))
180a96d7823Smrg#  define MASKANDSETBITS(dst, b1, b2)  \
181a96d7823Smrg		      { int cri;			\
182a96d7823Smrg			for (cri=MSKCNT; --cri>=0; )	\
183a96d7823Smrg		          dst[cri] = (b1[cri] & b2[cri]); }
184a96d7823Smrg#  define ORBITS(dst, b1, b2)  \
185a96d7823Smrg		      { int cri;			\
186a96d7823Smrg		      for (cri=MSKCNT; --cri>=0; )	\
187a96d7823Smrg		          dst[cri] = (b1[cri] | b2[cri]); }
188a96d7823Smrg#  define UNSETBITS(dst, b1) \
189a96d7823Smrg		      { int cri;			\
190a96d7823Smrg		      for (cri=MSKCNT; --cri>=0; )	\
191a96d7823Smrg		          dst[cri] &= ~b1[cri];  }
192a96d7823Smrg#  if (MSKCNT==8)
193a96d7823Smrg#   define ANYSET(src) (src[0] || src[1] || src[2] || src[3] || \
194a96d7823Smrg		     src[4] || src[5] || src[6] || src[7])
195a96d7823Smrg#  endif
196a96d7823Smrg# endif
197a96d7823Smrg
198a96d7823Smrg#else /* not WIN32 */
199a96d7823Smrg
200a96d7823Smrg# include <X11/Xwinsock.h>
201a96d7823Smrg# include <X11/Xw32defs.h>
202a96d7823Smrg
203a96d7823Smrgtypedef fd_set FdSet;
204a96d7823Smrgtypedef FdSet *FdSetPtr;
205a96d7823Smrg
206a96d7823Smrg# define CLEARBITS(set) FD_ZERO(&set)
207a96d7823Smrg# define BITSET(set,s) FD_SET(s,&set)
208a96d7823Smrg# define BITCLEAR(set,s) FD_CLR(s,&set)
209a96d7823Smrg# define GETBIT(set,s) FD_ISSET(s,&set)
210a96d7823Smrg# define ANYSET(set) set->fd_count
211a96d7823Smrg
212a96d7823Smrg#endif
213