FSlibos.h revision 2d8abe4f
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/Xosdefs.h>
58
59#ifndef WIN32
60
61#if defined(__SCO__) || defined(__UNIXWARE__)
62#include <stdint.h>	/* For SIZE_MAX */
63#endif
64
65/*
66 * makedepend screws up on #undef OPEN_MAX, so we define a new symbol
67 */
68
69#ifndef FS_OPEN_MAX
70
71#ifndef X_NOT_POSIX
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#endif
80#ifndef SIZE_MAX
81# ifdef ULONG_MAX
82#  define SIZE_MAX ULONG_MAX
83# else
84#  define SIZE_MAX UINT_MAX
85# endif
86#endif
87#ifndef OPEN_MAX
88#ifdef SVR4
89#define OPEN_MAX 256
90#else
91#include <sys/param.h>
92#ifndef OPEN_MAX
93#ifdef __OSF1__
94#define OPEN_MAX 256
95#else
96#ifdef NOFILE
97#define OPEN_MAX NOFILE
98#else
99#if !defined(__UNIXOS2__) && !defined(__QNX__)
100#ifdef __GNU__
101#define OPEN_MAX (sysconf(_SC_OPEN_MAX))
102#else /* !__GNU__ */
103#define OPEN_MAX NOFILES_MAX
104#endif /* __GNU__ */
105#else /* !__UNIXOS2__ && !__QNX__ */
106#define OPEN_MAX 256
107#endif /* __UNIXOS2__ */
108#endif
109#endif
110#endif
111#endif
112#endif
113
114#ifdef __GNU__
115#define FS_OPEN_MAX 256
116#else /*!__GNU__*/
117#if OPEN_MAX > 256
118#define FS_OPEN_MAX 256
119#else
120#define FS_OPEN_MAX OPEN_MAX
121#endif
122#endif /*__GNU__*/
123
124#endif /* FS_OPEN_MAX */
125
126/* Utek leaves kernel macros around in include files (bleah) */
127
128#ifdef dirty
129#undef dirty
130#endif
131
132#ifdef WORD64
133#define NMSKBITS 64
134#else
135#define NMSKBITS 32
136#endif
137
138#define MSKCNT ((FS_OPEN_MAX + NMSKBITS - 1) / NMSKBITS)
139
140#ifdef LONG64
141typedef unsigned int FdSet[MSKCNT];
142#else
143typedef unsigned long FdSet[MSKCNT];
144#endif
145
146#if (MSKCNT==1)
147#define BITMASK(i) (1 << (i))
148#define MASKIDX(i) 0
149#endif
150
151#if (MSKCNT>1)
152#define BITMASK(i) (1 << ((i) & (NMSKBITS - 1)))
153#define MASKIDX(i) ((i) / NMSKBITS)
154#endif
155
156#define MASKWORD(buf, i) buf[MASKIDX(i)]
157#define BITSET(buf, i) MASKWORD(buf, i) |= BITMASK(i)
158#define BITCLEAR(buf, i) MASKWORD(buf, i) &= ~BITMASK(i)
159#define GETBIT(buf, i) (MASKWORD(buf, i) & BITMASK(i))
160
161#if (MSKCNT==1)
162#define COPYBITS(src, dst) dst[0] = src[0]
163#define CLEARBITS(buf) buf[0] = 0
164#define MASKANDSETBITS(dst, b1, b2) dst[0] = (b1[0] & b2[0])
165#define ORBITS(dst, b1, b2) dst[0] = (b1[0] | b2[0])
166#define UNSETBITS(dst, b1) (dst[0] &= ~b1[0])
167#define _FSANYSET(src) (src[0])
168#endif
169
170#if (MSKCNT==2)
171#define COPYBITS(src, dst) { dst[0] = src[0]; dst[1] = src[1]; }
172#define CLEARBITS(buf) { buf[0] = 0; buf[1] = 0; }
173#define MASKANDSETBITS(dst, b1, b2)  {\
174		      dst[0] = (b1[0] & b2[0]);\
175		      dst[1] = (b1[1] & b2[1]); }
176#define ORBITS(dst, b1, b2)  {\
177		      dst[0] = (b1[0] | b2[0]);\
178		      dst[1] = (b1[1] | b2[1]); }
179#define UNSETBITS(dst, b1) {\
180                      dst[0] &= ~b1[0]; \
181                      dst[1] &= ~b1[1]; }
182#define _FSANYSET(src) (src[0] || src[1])
183#endif
184
185#if (MSKCNT==3)
186#define COPYBITS(src, dst) { dst[0] = src[0]; dst[1] = src[1]; \
187			     dst[2] = src[2]; }
188#define CLEARBITS(buf) { buf[0] = 0; buf[1] = 0; buf[2] = 0; }
189#define MASKANDSETBITS(dst, b1, b2)  {\
190		      dst[0] = (b1[0] & b2[0]);\
191		      dst[1] = (b1[1] & b2[1]);\
192		      dst[2] = (b1[2] & b2[2]); }
193#define ORBITS(dst, b1, b2)  {\
194		      dst[0] = (b1[0] | b2[0]);\
195		      dst[1] = (b1[1] | b2[1]);\
196		      dst[2] = (b1[2] | b2[2]); }
197#define UNSETBITS(dst, b1) {\
198                      dst[0] &= ~b1[0]; \
199                      dst[1] &= ~b1[1]; \
200                      dst[2] &= ~b1[2]; }
201#define _FSANYSET(src) (src[0] || src[1] || src[2])
202#endif
203
204#if (MSKCNT==4)
205#define COPYBITS(src, dst) dst[0] = src[0]; dst[1] = src[1]; \
206			   dst[2] = src[2]; dst[3] = src[3]
207#define CLEARBITS(buf) buf[0] = 0; buf[1] = 0; buf[2] = 0; buf[3] = 0
208#define MASKANDSETBITS(dst, b1, b2)  \
209                      dst[0] = (b1[0] & b2[0]);\
210                      dst[1] = (b1[1] & b2[1]);\
211                      dst[2] = (b1[2] & b2[2]);\
212                      dst[3] = (b1[3] & b2[3])
213#define ORBITS(dst, b1, b2)  \
214                      dst[0] = (b1[0] | b2[0]);\
215                      dst[1] = (b1[1] | b2[1]);\
216                      dst[2] = (b1[2] | b2[2]);\
217                      dst[3] = (b1[3] | b2[3])
218#define UNSETBITS(dst, b1) \
219                      dst[0] &= ~b1[0]; \
220                      dst[1] &= ~b1[1]; \
221                      dst[2] &= ~b1[2]; \
222                      dst[3] &= ~b1[3]
223#define _FSANYSET(src) (src[0] || src[1] || src[2] || src[3])
224#endif
225
226#if (MSKCNT>4)
227#define COPYBITS(src, dst) memmove((caddr_t) dst, (caddr_t) src, sizeof(FdSet))
228#define CLEARBITS(buf) bzero((caddr_t) buf, sizeof(FdSet))
229#define MASKANDSETBITS(dst, b1, b2)  \
230		      { int cri;			\
231			for (cri=0; cri<MSKCNT; cri++)	\
232		          dst[cri] = (b1[cri] & b2[cri]) }
233#define ORBITS(dst, b1, b2)  \
234		      { int cri;			\
235		      for (cri=0; cri<MSKCNT; cri++)	\
236		          dst[cri] = (b1[cri] | b2[cri]) }
237#define UNSETBITS(dst, b1) \
238		      { int cri;			\
239		      for (cri=0; cri<MSKCNT; cri++)	\
240		          dst[cri] &= ~b1[cri];  }
241#if (MSKCNT==8)
242#define _FSANYSET(src) (src[0] || src[1] || src[2] || src[3] || \
243			src[4] || src[5] || src[6] || src[7])
244#endif
245/*
246 * If MSKCNT>4 and not 8, then _FSANYSET is a routine defined in FSlibInt.c.
247 *
248 * #define _FSANYSET(src) (src[0] || src[1] || src[2] || src[3] || src[4] ...)
249 */
250#endif
251
252
253#else
254
255#include <X11/Xwinsock.h>
256#include <X11/Xw32defs.h>
257
258typedef fd_set FdSet;
259
260#define CLEARBITS(set) FD_ZERO(&set)
261#define BITSET(set,s) FD_SET(s,&set)
262#define _FSANYSET(set) set.fd_count
263
264#endif
265
266#include <X11/Xtrans/Xtrans.h>
267#include <stdlib.h>
268#include <string.h>
269
270/*
271 * The following definitions can be used for locking requests in multi-threaded
272 * address spaces.
273 */
274#define LockDisplay(dis)
275#define LockMutex(mutex)
276#define UnlockMutex(mutex)
277#define UnlockDisplay(dis)
278#define FSfree(ptr) free((ptr))
279
280
281/*
282 * Note that some machines do not return a valid pointer for malloc(0), in
283 * which case we provide an alternate under the control of the
284 * define MALLOC_0_RETURNS_NULL.  This is necessary because some
285 * FSlib code expects malloc(0) to return a valid pointer to storage.
286 */
287
288#ifdef MALLOC_0_RETURNS_NULL
289#define FSmalloc(size) malloc(((size) > 0 ? (size) : 1))
290#define FSrealloc(ptr, size) realloc((ptr), ((size) > 0 ? (size) : 1))
291#define FScalloc(nelem, elsize) calloc(((nelem) > 0 ? (nelem) : 1), (elsize))
292
293#else
294
295#define FSmalloc(size) malloc((size))
296#define FSrealloc(ptr, size) realloc((ptr), (size))
297#define FScalloc(nelem, elsize) calloc((nelem), (elsize))
298#endif
299
300#define SearchString(string, char) index((string), (char))
301