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