FSlibos.h revision da1f2d5d
1/*
2 * Copyright 1990 Network Computing Devices;
3 * Portions Copyright 1987 by Digital Equipment Corporation
4 *
5 * Permission to use, copy, modify, distribute, and sell this software
6 * and its documentation for any purpose is hereby granted without fee,
7 * provided that the above copyright notice appear in all copies and
8 * that both that copyright notice and this permission notice appear
9 * in supporting documentation, and that the names of Network Computing
10 * Devices or Digital not be used in advertising or publicity pertaining
11 * to distribution of the software without specific, written prior
12 * permission. Network Computing Devices or Digital make no representations
13 * about the suitability of this software for any purpose.  It is provided
14 * "as is" without express or implied warranty.
15 *
16 * NETWORK COMPUTING DEVICES AND  DIGITAL DISCLAIM ALL WARRANTIES WITH
17 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES
19 * OR DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES
20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
22 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
23 * SOFTWARE.
24 */
25
26/*
27
28Copyright 1987, 1994, 1998  The Open Group
29
30Permission to use, copy, modify, distribute, and sell this software and its
31documentation for any purpose is hereby granted without fee, provided that
32the above copyright notice appear in all copies and that both that
33copyright notice and this permission notice appear in supporting
34documentation.
35
36The above copyright notice and this permission notice shall be included in
37all copies or substantial portions of the Software.
38
39THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
42OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
43AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
44CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45
46Except as contained in this notice, the name of The Open Group shall not be
47used in advertising or otherwise to promote the sale, use or other dealings
48in this Software without prior written authorization from The Open Group.
49
50*/
51
52/*
53 * FSlib networking & os include file
54 */
55
56#include <X11/Xfuncs.h>
57#include <X11/Xfuncproto.h>
58#include <X11/Xosdefs.h>
59
60#ifndef WIN32
61
62# if defined(__SCO__) || defined(__UNIXWARE__)
63#  include <stdint.h>	/* For SIZE_MAX */
64# endif
65
66/*
67 * makedepend screws up on #undef OPEN_MAX, so we define a new symbol
68 */
69
70# ifndef FS_OPEN_MAX
71
72#  ifdef _POSIX_SOURCE
73#   include <limits.h>
74#  else
75#   define _POSIX_SOURCE
76#   include <limits.h>
77#   undef _POSIX_SOURCE
78#  endif
79#  ifndef SIZE_MAX
80#   ifdef ULONG_MAX
81#    define SIZE_MAX ULONG_MAX
82#   else
83#    define SIZE_MAX UINT_MAX
84#   endif
85#  endif
86#  ifndef OPEN_MAX
87#   ifdef SVR4
88#    define OPEN_MAX 256
89#   else
90#    include <sys/param.h>
91#    ifndef OPEN_MAX
92#     ifdef __OSF1__
93#      define OPEN_MAX 256
94#     else
95#      ifdef NOFILE
96#       define OPEN_MAX NOFILE
97#      else
98#       if !defined(__UNIXOS2__) && !defined(__QNX__)
99#        ifdef __GNU__
100#         define OPEN_MAX (sysconf(_SC_OPEN_MAX))
101#        else /* !__GNU__ */
102#         define OPEN_MAX NOFILES_MAX
103#        endif /* __GNU__ */
104#       else /* !__UNIXOS2__ && !__QNX__ */
105#        define OPEN_MAX 256
106#       endif /* __UNIXOS2__ */
107#      endif
108#     endif
109#    endif
110#   endif
111#  endif
112
113#  ifdef __GNU__
114#   define FS_OPEN_MAX 256
115#  else /*!__GNU__*/
116#   if OPEN_MAX > 256
117#    define FS_OPEN_MAX 256
118#   else
119#    define FS_OPEN_MAX OPEN_MAX
120#   endif
121#  endif /*__GNU__*/
122
123# endif /* FS_OPEN_MAX */
124
125/* Utek leaves kernel macros around in include files (bleah) */
126
127# ifdef dirty
128#  undef dirty
129# endif
130
131# define NMSKBITS 32
132# define MSKCNT ((FS_OPEN_MAX + NMSKBITS - 1) / NMSKBITS)
133
134# ifdef LONG64
135typedef unsigned int FdSet[MSKCNT];
136# else
137typedef unsigned long FdSet[MSKCNT];
138# endif
139
140# if (MSKCNT==1)
141#  define BITMASK(i) (1 << (i))
142#  define MASKIDX(i) 0
143# endif
144
145# if (MSKCNT>1)
146#  define BITMASK(i) (1 << ((i) & (NMSKBITS - 1)))
147#  define MASKIDX(i) ((i) / NMSKBITS)
148# endif
149
150# define MASKWORD(buf, i) buf[MASKIDX(i)]
151# define BITSET(buf, i) MASKWORD(buf, i) |= BITMASK(i)
152# define BITCLEAR(buf, i) MASKWORD(buf, i) &= ~BITMASK(i)
153# define GETBIT(buf, i) (MASKWORD(buf, i) & BITMASK(i))
154
155# if (MSKCNT==1)
156#  define COPYBITS(src, dst) dst[0] = src[0]
157#  define CLEARBITS(buf) buf[0] = 0
158#  define MASKANDSETBITS(dst, b1, b2) dst[0] = (b1[0] & b2[0])
159#  define ORBITS(dst, b1, b2) dst[0] = (b1[0] | b2[0])
160#  define UNSETBITS(dst, b1) (dst[0] &= ~b1[0])
161#  define _FSANYSET(src) (src[0])
162# endif
163
164# if (MSKCNT==2)
165#  define COPYBITS(src, dst) { dst[0] = src[0]; dst[1] = src[1]; }
166#  define CLEARBITS(buf) { buf[0] = 0; buf[1] = 0; }
167#  define MASKANDSETBITS(dst, b1, b2)  {\
168		      dst[0] = (b1[0] & b2[0]);\
169		      dst[1] = (b1[1] & b2[1]); }
170#  define ORBITS(dst, b1, b2)  {\
171		      dst[0] = (b1[0] | b2[0]);\
172		      dst[1] = (b1[1] | b2[1]); }
173#  define UNSETBITS(dst, b1) {\
174                      dst[0] &= ~b1[0]; \
175                      dst[1] &= ~b1[1]; }
176#  define _FSANYSET(src) (src[0] || src[1])
177# endif
178
179# if (MSKCNT==3)
180#  define COPYBITS(src, dst) { dst[0] = src[0]; dst[1] = src[1]; \
181			     dst[2] = src[2]; }
182#  define CLEARBITS(buf) { buf[0] = 0; buf[1] = 0; buf[2] = 0; }
183#  define MASKANDSETBITS(dst, b1, b2)  {\
184		      dst[0] = (b1[0] & b2[0]);\
185		      dst[1] = (b1[1] & b2[1]);\
186		      dst[2] = (b1[2] & b2[2]); }
187#  define ORBITS(dst, b1, b2)  {\
188		      dst[0] = (b1[0] | b2[0]);\
189		      dst[1] = (b1[1] | b2[1]);\
190		      dst[2] = (b1[2] | b2[2]); }
191#  define UNSETBITS(dst, b1) {\
192                      dst[0] &= ~b1[0]; \
193                      dst[1] &= ~b1[1]; \
194                      dst[2] &= ~b1[2]; }
195#  define _FSANYSET(src) (src[0] || src[1] || src[2])
196# endif
197
198# if (MSKCNT==4)
199#  define COPYBITS(src, dst) dst[0] = src[0]; dst[1] = src[1]; \
200			   dst[2] = src[2]; dst[3] = src[3]
201#  define CLEARBITS(buf) buf[0] = 0; buf[1] = 0; buf[2] = 0; buf[3] = 0
202#  define MASKANDSETBITS(dst, b1, b2)  \
203                      dst[0] = (b1[0] & b2[0]);\
204                      dst[1] = (b1[1] & b2[1]);\
205                      dst[2] = (b1[2] & b2[2]);\
206                      dst[3] = (b1[3] & b2[3])
207#  define ORBITS(dst, b1, b2)  \
208                      dst[0] = (b1[0] | b2[0]);\
209                      dst[1] = (b1[1] | b2[1]);\
210                      dst[2] = (b1[2] | b2[2]);\
211                      dst[3] = (b1[3] | b2[3])
212#  define UNSETBITS(dst, b1) \
213                      dst[0] &= ~b1[0]; \
214                      dst[1] &= ~b1[1]; \
215                      dst[2] &= ~b1[2]; \
216                      dst[3] &= ~b1[3]
217#  define _FSANYSET(src) (src[0] || src[1] || src[2] || src[3])
218# endif
219
220# if (MSKCNT>4)
221#  define COPYBITS(src, dst) memmove((caddr_t) dst, (caddr_t) src, sizeof(FdSet))
222#  define CLEARBITS(buf) bzero((caddr_t) buf, sizeof(FdSet))
223#  define MASKANDSETBITS(dst, b1, b2)  \
224		      { int cri;			\
225			for (cri=0; cri<MSKCNT; cri++)	\
226		          dst[cri] = (b1[cri] & b2[cri]) }
227#  define ORBITS(dst, b1, b2)  \
228		      { int cri;			\
229		      for (cri=0; cri<MSKCNT; cri++)	\
230		          dst[cri] = (b1[cri] | b2[cri]) }
231#  define UNSETBITS(dst, b1) \
232		      { int cri;			\
233		      for (cri=0; cri<MSKCNT; cri++)	\
234		          dst[cri] &= ~b1[cri];  }
235#  if (MSKCNT==8)
236#   define _FSANYSET(src) (src[0] || src[1] || src[2] || src[3] || \
237			src[4] || src[5] || src[6] || src[7])
238#  endif
239/*
240 * If MSKCNT>4 and not 8, then _FSANYSET is a routine defined in FSlibInt.c.
241 *
242 * #define _FSANYSET(src) (src[0] || src[1] || src[2] || src[3] || src[4] ...)
243 */
244# endif
245
246
247#else
248
249# include <X11/Xwinsock.h>
250# include <X11/Xw32defs.h>
251
252typedef fd_set FdSet;
253
254# define CLEARBITS(set) FD_ZERO(&set)
255# define BITSET(set,s) FD_SET(s,&set)
256# define _FSANYSET(set) set.fd_count
257
258#endif
259
260#include <X11/Xtrans/Xtrans.h>
261#include <stdlib.h>
262#include <string.h>
263
264/*
265 * The following definitions can be used for locking requests in multi-threaded
266 * address spaces.
267 */
268#define LockDisplay(dis)
269#define LockMutex(mutex)
270#define UnlockMutex(mutex)
271#define UnlockDisplay(dis)
272#define FSfree(ptr) free((ptr))
273
274#ifndef HAVE_REALLOCARRAY
275extern _X_HIDDEN void *fsreallocarray(void *optr, size_t nmemb, size_t size);
276# define reallocarray(ptr, n, size) \
277    fsreallocarray((ptr), (size_t)(n), (size_t)(size))
278#endif
279
280/*
281 * Note that some machines do not return a valid pointer for malloc(0), in
282 * which case we provide an alternate under the control of the
283 * define MALLOC_0_RETURNS_NULL.  This is necessary because some
284 * FSlib code expects malloc(0) to return a valid pointer to storage.
285 */
286
287#if defined(MALLOC_0_RETURNS_NULL) || defined(__clang_analyzer__)
288# define FSmalloc(size) malloc(((size) > 0 ? (size) : 1))
289# define FSrealloc(ptr, size) realloc((ptr), ((size) > 0 ? (size) : 1))
290# define FScalloc(nelem, elsize) calloc(((nelem) > 0 ? (nelem) : 1), (elsize))
291# define FSreallocarray(ptr, n, size) \
292    reallocarray((ptr), ((n) == 0 ? 1 : (n)), size)
293
294#else
295
296# define FSmalloc(size) malloc((size))
297# define FSrealloc(ptr, size) realloc((ptr), (size))
298# define FScalloc(nelem, elsize) calloc((nelem), (elsize))
299# define FSreallocarray(ptr, n, size) reallocarray((ptr), (n), (size))
300
301#endif
302
303#define FSmallocarray(n, size) FSreallocarray(NULL, (n), (size))
304
305#define SearchString(string, char) index((string), (char))
306