FSlibos.h revision 00ca1914
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# ifdef HAVE_STDINT_H 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# include <sys/param.h> 88# ifndef OPEN_MAX 89# ifdef NOFILE 90# define OPEN_MAX NOFILE 91# else 92# ifdef NOFILES_MAX 93# define OPEN_MAX NOFILES_MAX 94# endif 95# endif 96# endif 97# endif 98 99# ifndef OPEN_MAX 100# define FS_OPEN_MAX 256 101# else /* !OPEN_MAX */ 102# if OPEN_MAX > 256 103# define FS_OPEN_MAX 256 104# else 105# define FS_OPEN_MAX OPEN_MAX 106# endif 107# endif /* OPEN_MAX */ 108 109# endif /* FS_OPEN_MAX */ 110 111# define NMSKBITS 32 112# define MSKCNT ((FS_OPEN_MAX + NMSKBITS - 1) / NMSKBITS) 113 114# ifdef LONG64 115typedef unsigned int FdSet[MSKCNT]; 116# else 117typedef unsigned long FdSet[MSKCNT]; 118# endif 119 120# if (MSKCNT==1) 121# define BITMASK(i) (1 << (i)) 122# define MASKIDX(i) 0 123# endif 124 125# if (MSKCNT>1) 126# define BITMASK(i) (1 << ((i) & (NMSKBITS - 1))) 127# define MASKIDX(i) ((i) / NMSKBITS) 128# endif 129 130# define MASKWORD(buf, i) buf[MASKIDX(i)] 131# define BITSET(buf, i) MASKWORD(buf, i) |= BITMASK(i) 132# define BITCLEAR(buf, i) MASKWORD(buf, i) &= ~BITMASK(i) 133# define GETBIT(buf, i) (MASKWORD(buf, i) & BITMASK(i)) 134 135# if (MSKCNT==1) 136# define COPYBITS(src, dst) dst[0] = src[0] 137# define CLEARBITS(buf) buf[0] = 0 138# define MASKANDSETBITS(dst, b1, b2) dst[0] = (b1[0] & b2[0]) 139# define ORBITS(dst, b1, b2) dst[0] = (b1[0] | b2[0]) 140# define UNSETBITS(dst, b1) (dst[0] &= ~b1[0]) 141# define _FSANYSET(src) (src[0]) 142# endif 143 144# if (MSKCNT==2) 145# define COPYBITS(src, dst) { dst[0] = src[0]; dst[1] = src[1]; } 146# define CLEARBITS(buf) { buf[0] = 0; buf[1] = 0; } 147# define MASKANDSETBITS(dst, b1, b2) {\ 148 dst[0] = (b1[0] & b2[0]);\ 149 dst[1] = (b1[1] & b2[1]); } 150# define ORBITS(dst, b1, b2) {\ 151 dst[0] = (b1[0] | b2[0]);\ 152 dst[1] = (b1[1] | b2[1]); } 153# define UNSETBITS(dst, b1) {\ 154 dst[0] &= ~b1[0]; \ 155 dst[1] &= ~b1[1]; } 156# define _FSANYSET(src) (src[0] || src[1]) 157# endif 158 159# if (MSKCNT==3) 160# define COPYBITS(src, dst) { dst[0] = src[0]; dst[1] = src[1]; \ 161 dst[2] = src[2]; } 162# define CLEARBITS(buf) { buf[0] = 0; buf[1] = 0; buf[2] = 0; } 163# define MASKANDSETBITS(dst, b1, b2) {\ 164 dst[0] = (b1[0] & b2[0]);\ 165 dst[1] = (b1[1] & b2[1]);\ 166 dst[2] = (b1[2] & b2[2]); } 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# define UNSETBITS(dst, b1) {\ 172 dst[0] &= ~b1[0]; \ 173 dst[1] &= ~b1[1]; \ 174 dst[2] &= ~b1[2]; } 175# define _FSANYSET(src) (src[0] || src[1] || src[2]) 176# endif 177 178# if (MSKCNT==4) 179# define COPYBITS(src, dst) dst[0] = src[0]; dst[1] = src[1]; \ 180 dst[2] = src[2]; dst[3] = src[3] 181# define CLEARBITS(buf) buf[0] = 0; buf[1] = 0; buf[2] = 0; buf[3] = 0 182# define MASKANDSETBITS(dst, b1, b2) \ 183 dst[0] = (b1[0] & b2[0]);\ 184 dst[1] = (b1[1] & b2[1]);\ 185 dst[2] = (b1[2] & b2[2]);\ 186 dst[3] = (b1[3] & b2[3]) 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 dst[3] = (b1[3] | b2[3]) 192# define UNSETBITS(dst, b1) \ 193 dst[0] &= ~b1[0]; \ 194 dst[1] &= ~b1[1]; \ 195 dst[2] &= ~b1[2]; \ 196 dst[3] &= ~b1[3] 197# define _FSANYSET(src) (src[0] || src[1] || src[2] || src[3]) 198# endif 199 200# if (MSKCNT>4) 201# define COPYBITS(src, dst) memmove((caddr_t) dst, (caddr_t) src, sizeof(FdSet)) 202# define CLEARBITS(buf) bzero((caddr_t) buf, sizeof(FdSet)) 203# define MASKANDSETBITS(dst, b1, b2) \ 204 { int cri; \ 205 for (cri=0; cri<MSKCNT; cri++) \ 206 dst[cri] = (b1[cri] & b2[cri]) } 207# define ORBITS(dst, b1, b2) \ 208 { int cri; \ 209 for (cri=0; cri<MSKCNT; cri++) \ 210 dst[cri] = (b1[cri] | b2[cri]) } 211# define UNSETBITS(dst, b1) \ 212 { int cri; \ 213 for (cri=0; cri<MSKCNT; cri++) \ 214 dst[cri] &= ~b1[cri]; } 215# if (MSKCNT==8) 216# define _FSANYSET(src) (src[0] || src[1] || src[2] || src[3] || \ 217 src[4] || src[5] || src[6] || src[7]) 218# endif 219/* 220 * If MSKCNT>4 and not 8, then _FSANYSET is a routine defined in FSlibInt.c. 221 * 222 * #define _FSANYSET(src) (src[0] || src[1] || src[2] || src[3] || src[4] ...) 223 */ 224# endif 225 226 227#else 228 229# include <X11/Xwinsock.h> 230# include <X11/Xw32defs.h> 231 232typedef fd_set FdSet; 233 234# define CLEARBITS(set) FD_ZERO(&set) 235# define BITSET(set,s) FD_SET(s,&set) 236# define _FSANYSET(set) set.fd_count 237 238#endif 239 240#include <X11/Xtrans/Xtrans.h> 241#include <stdlib.h> 242#include <string.h> 243 244/* 245 * The following definitions can be used for locking requests in multi-threaded 246 * address spaces. 247 */ 248#define LockDisplay(dis) 249#define LockMutex(mutex) 250#define UnlockMutex(mutex) 251#define UnlockDisplay(dis) 252#define FSfree(ptr) free((ptr)) 253 254#ifndef HAVE_REALLOCARRAY 255extern _X_HIDDEN void *fsreallocarray(void *optr, size_t nmemb, size_t size); 256# define reallocarray(ptr, n, size) \ 257 fsreallocarray((ptr), (size_t)(n), (size_t)(size)) 258#endif 259 260/* 261 * Note that some machines do not return a valid pointer for malloc(0), in 262 * which case we provide an alternate under the control of the 263 * define MALLOC_0_RETURNS_NULL. This is necessary because some 264 * FSlib code expects malloc(0) to return a valid pointer to storage. 265 */ 266 267#if defined(MALLOC_0_RETURNS_NULL) || defined(__clang_analyzer__) 268# define FSmalloc(size) malloc(((size) > 0 ? (size) : 1)) 269# define FSrealloc(ptr, size) realloc((ptr), ((size) > 0 ? (size) : 1)) 270# define FScalloc(nelem, elsize) calloc(((nelem) > 0 ? (nelem) : 1), (elsize)) 271# define FSreallocarray(ptr, n, size) \ 272 reallocarray((ptr), ((n) == 0 ? 1 : (n)), size) 273 274#else 275 276# define FSmalloc(size) malloc((size)) 277# define FSrealloc(ptr, size) realloc((ptr), (size)) 278# define FScalloc(nelem, elsize) calloc((nelem), (elsize)) 279# define FSreallocarray(ptr, n, size) reallocarray((ptr), (n), (size)) 280 281#endif 282 283#define FSmallocarray(n, size) FSreallocarray(NULL, (n), (size)) 284 285#define SearchString(string, char) index((string), (char)) 286