Libinfo-173.tar.gz
[apple/libinfo.git] / dns.subproj / portability.h
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* portability.h - include or define things that aren't present on all systems
26 * vixie@decwrl 26dec92 [new]
27 *
28 * $Id: portability.h,v 1.2 1999/10/14 21:56:45 wsanchez Exp $
29 */
30
31 /*
32 * ++Copyright++
33 * -
34 * Copyright (c)
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 * -
65 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
66 *
67 * Permission to use, copy, modify, and distribute this software for any
68 * purpose with or without fee is hereby granted, provided that the above
69 * copyright notice and this permission notice appear in all copies, and that
70 * the name of Digital Equipment Corporation not be used in advertising or
71 * publicity pertaining to distribution of the document or software without
72 * specific, written prior permission.
73 *
74 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
75 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
76 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
77 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
78 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
79 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
80 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
81 * SOFTWARE.
82 * -
83 * --Copyright--
84 */
85
86 /* XXX: this file has become a hopeless morass, and will be redone someday. */
87
88 #include <string.h>
89 #include <sys/types.h>
90 #include <sys/param.h>
91 #ifndef TIME_H_INCLUDED
92 # include <sys/time.h>
93 # define TIME_H_INCLUDED
94 #endif
95
96 #ifdef ISC
97 # ifndef _POSIX_SOURCE
98 # define _POSIX_SOURCE
99 # endif
100 # define SYSV
101 # define SVR3
102 # define _SYSV3
103 # define NEED_STRTOUL
104 # define NEED_FTRUNCATE
105 # define USE_POSIX
106 # include <sys/bsdtypes.h>
107 # include <sys/sioctl.h>
108 # include <sys/stream.h>
109 # include <net/errno.h>
110 #endif
111
112 #if defined(__convex__)
113 # if !defined(_POSIX_SOURCE)
114 # define _POSIX_SOURCE
115 # endif
116 # define USE_UTIME
117 # define NEED_PUTENV
118 #endif
119
120 #if defined(_CRAY)
121 # if !defined(_POSIX_SOURCE)
122 # define _POSIX_SOURCE
123 # endif
124 # define writev(a,b,c) __writev(a,b,c)
125 # define setitimer(a,b,c) __setitimer(a,b,c)
126 #endif
127
128 /* This is defined in the Makefile for ISC compiles. */
129 #if defined(ISC)
130 # define ftruncate(a,b) __ftruncate(a,b)
131 # define USE_MEMCPY
132 # define USE_UTIME
133 # define HAVE_FCHMOD 0
134 #endif
135
136 /* SCO UNIX defines only this unique symbol, apparently. */
137 #if defined(M_UNIX)
138 /* XXX - why is this POSIX_SOURCE instead of _POSIX_SOURCE? */
139 # undef POSIX_SOURCE
140 # define POSIX_SIGNALS
141 # define HAVE_FCHMOD 0
142 # define writev(a,b,c) __writev(a,b,c)
143 # define ftruncate(a,b) __ftruncate(a,b)
144 #endif
145
146 #if defined(__APPLE__) && BSD < 199506
147 # define NEED_PUTENV
148 # define NEED_SETENV
149 #endif
150
151 #if defined(__sgi)
152 # define BSD 43
153 # define vfork fork
154 #endif
155
156 #if defined(SUNOS4)
157 # define BSD 43
158 #endif
159
160 #if defined(_AUX_SOURCE)
161 # define vfork fork
162 # define NEED_STRERROR
163 # define NEED_STRTOUL
164 # define SIG_FN void
165 # define USE_MEMCPY
166 #endif
167
168
169 #if defined(SVR4) && !defined(SYSV)
170 # define SYSV
171 #endif
172
173 #if defined(_POSIX_SOURCE) || defined(__sgi) || defined(__ultrix) || \
174 defined(__hpux) || (defined(BSD) && (BSD >= 199103)) || \
175 (defined(sun) && defined(SYSV))
176 # define USE_POSIX
177 #endif
178
179 #if defined(__ultrix) && !defined(BSD)
180 # define BSD 42
181 #endif
182
183 #if defined(host_mips) && defined(SYSTYPE_BSD43)
184 # define RISCOS_BSD
185 #endif
186
187 #if defined(SYSV) || defined(__ultrix) || defined(__osf__) \
188 || (defined(BSD) && BSD >= 199306) || defined(linux)
189 # define USE_UTIME
190 # define HAVE_SETVBUF
191 #endif
192
193 #if defined(SYSV) && !defined(SVR4)
194 # define vfork fork
195 #endif
196
197 #if defined(sun) || defined(SVR4)
198 # define NETREAD_BROKEN
199 #endif
200
201 #if defined(BSD) && BSD >= 199006 && !defined(i386) && !defined(RISCOS_BSD)
202 # define HAVE_DAEMON
203 #endif
204
205 #if !defined(BSD) || (BSD <= 199006)
206 # if !defined(__APPLE__)
207 # define NEED_INETADDR
208 # endif
209 # define NEED_INETATON
210 #endif
211
212 #if defined(__hpux)
213 # if defined(__STDC__)
214 # define select(a,b,c,d,e) select(a, (int *)b, (int *)c, (int *)d, e)
215 # define ctime(x) ctime((const time_t *)x)
216 # endif /*__STDC__*/
217 # if !defined(SYSV)
218 # define USE_UTIME
219 # define setlinebuf(x) setvbuf(x, NULL, _IOLBF, BUFSIZ)
220 # if !defined(SIGWINCH) /*pre 9.0*/
221 # define SIGWINCH SIGWINDOW
222 # endif
223 # endif /*SYSV*/
224 /* XXX: better autodetection of the need for "struct linger" would be nice */
225 # if 0
226 struct linger {
227 int l_onoff; /* option on/off */
228 int l_linger; /* linger time */
229 };
230 # endif
231 #endif /*__hpux*/
232
233 #if defined(_SEQUENT_)
234 # include <netinet/in_systm.h>
235 # define USE_UTIME
236 # define USE_POSIX
237 # define NEED_GETTIMEOFDAY
238 # define _TIMEZONE timezoneBSD
239 struct timezoneBSD {
240 int tz_minuteswest;
241 int tz_dsttime;
242 };
243 #endif
244
245 #ifndef __P
246 # if defined(__STDC__) || defined(__GNUC__)
247 # define __P(x) x
248 # else
249 # define __P(x) ()
250 # endif
251 #endif
252
253 #ifndef _TIMEZONE
254 # define _TIMEZONE timezone
255 #endif
256
257 #if defined(USE_POSIX)
258 # include <stdlib.h>
259 # include <unistd.h>
260 # include <limits.h>
261
262 #else
263
264 # define NEED_STRTOUL
265
266 # define STDIN_FILENO 0
267 # define STDOUT_FILENO 1
268 # define STDERR_FILENO 2
269 # ifndef __APPLE__
270 extern char *getenv __P((char *));
271 # else
272 extern char *getenv __P((const char *));
273 # endif
274 extern int errno;
275
276 # if !defined(DMALLOC) && !defined(__APPLE__)
277 extern char *malloc(), *realloc(), *calloc();
278 # if defined(sun)
279 extern int free();
280 # else
281 extern void free();
282 # endif
283 # endif
284
285 extern int getdtablesize __P((void));
286 # ifdef SHORT_FNAMES
287 extern long pathconf __P((const char *path, int name));
288 # endif
289
290 #endif /*USE_POSIX*/
291
292 #ifndef UINT_MAX
293 # ifdef __STDC__
294 # define UINT_MAX 4294967295u /* max value of an "u_int" */
295 # else
296 # define UINT_MAX ((unsigned)4294967295) /* max value of an "u_int" */
297 # endif
298 # define ULONG_MAX UINT_MAX /* max decimal value of a "u_long" */
299 #endif
300
301 #ifndef INT_MAX
302 # define INT_MAX 2147483647 /* max decimal value of an "int" */
303 #endif
304
305 #ifndef RAND_MAX
306 # define RAND_MAX 0x7fffffff
307 #endif
308
309 #ifndef IN_LOOPBACKNET
310 # define IN_LOOPBACKNET 127
311 #endif
312
313 #ifndef INADDR_NONE
314 # define INADDR_NONE 0xffffffff
315 #endif
316
317 #if defined(apollo)
318 /* Defined in /usr/include/netinet/in.h but doesn't work */
319 #undef IP_OPTIONS
320 #endif
321
322 #if !defined(__STDC__) && !defined(const)
323 # define const /*constant*/
324 #endif
325
326 #if !defined(__convex__) && (!defined(BSD) || (BSD < 199103))
327 int strcasecmp __P((const char *, const char *));
328 #endif
329
330 /* is USE_POSIX the right thing to use here? */
331 #if (!defined(BSD) || (BSD <= 43)) && \
332 !defined(NeXT) && \
333 !defined(__convex__) && \
334 !defined(USE_POSIX)
335 # if !defined(NCR)
336 extern void syslog();
337 # endif
338 extern char *ctime __P((const time_t *clock));
339 extern int close(), setitimer(), recv(), sendto(), sigsetmask(),
340 atoi(), getpid(), fork(), read(), ioctl(),
341 setsockopt(), socket(), bind();
342 #endif
343
344 #if !defined(bcopy) /* some machines have their own macros for this */
345 # if defined(USE_POSIX) || \
346 (defined(__STDC__) && !defined(sun) && !defined(sequent))
347 /* use ANSI C3.159-1989 (``ANSI C'') functions if possible;
348 * ideally we would change the code to use them and then
349 * define them in terms of bcopy et al if !defined(__STDC__)
350 * but that's more work.
351 */
352 #if defined(USE_MEMCPY)
353 # define bcopy(a,b,c) memcpy(b,a,c)
354 #else
355 # define bcopy(a,b,c) memmove(b,a,c)
356 #endif
357 # define bzero(a,b) memset(a,0,b)
358 # define bcmp(a,b,c) memcmp(a,b,c)
359 # else
360 extern void bcopy();
361 extern void bzero();
362 extern int bcmp();
363 # endif /* BSD */
364 #endif /* bcopy */
365
366 #if (!defined(BSD) || (BSD < 43) || defined(RISCOS_BSD)) \
367 && !defined(USE_POSIX) && !defined(apollo) && !defined(sequent) \
368 && !defined(M_UNIX)
369 # define NEED_STRERROR
370 #if !defined(ultrix) && !defined(NCR)
371 # define NEED_PUTENV
372 #endif
373 #endif
374
375 #if defined(SUNOS4)
376 # define NEED_STRERROR
377 # if defined(sun386)
378 # define pid_t int
379 # define NEED_STRCASECMP
380 # endif
381 #endif
382
383 #if (!defined(BSD) || (BSD < 43))
384 # define NEED_MKSTEMP
385 # if !defined(__ultrix) && !defined(apollo)
386 # define NEED_STRCASECMP
387 # define NEED_MKTEMP
388 # if !defined(SVR4)
389 # define NEED_STRPBRK
390 # endif
391 # endif
392 #endif
393
394 #if defined(USE_POSIX)
395 # define POSIX_SIGNALS
396 #endif
397
398 /*
399 * Attempt to configure for type of function returned by signal-catching
400 * functions (which signal and sigvec.sv_handler take a pointer to).
401 * This can guess for BSD; otherwise, define SIG_FN externally.
402 */
403 #ifndef SIG_FN
404 # ifdef BSD
405 # if (BSD >= 199006) || defined(__APPLE__) || defined(__osf__) || defined(sun) \
406 || defined(__ultrix) || defined(apollo) || defined(POSIX_SIGNALS)
407 # define SIG_FN void /* signal-catching functions return void */
408 # else
409 # define SIG_FN int /* signal-catching functions return int */
410 # endif
411 # else /*BSD*/
412 # define SIG_FN void /* signal-catching functions return void */
413 # endif /*BSD*/
414 #endif
415
416 #if !defined(ntohl) && !defined(htonl) && defined(BSD) && (BSD <= 43)
417 /* if these aren't null macros in netinet/in.h, extern them here. */
418 extern u_short htons(), ntohs();
419 extern u_long htonl(), ntohl();
420 #endif
421
422 #if defined(USE_POSIX) && !defined(sun) && !defined(__sgi) \
423 && !defined(__convex__) && !defined(__ultrix) && !defined(_AUX_SOURCE)
424 # define PORT_NONBLOCK O_NONBLOCK
425 # define PORT_WOULDBLK EAGAIN
426 #else
427 # define PORT_NONBLOCK O_NDELAY
428 # define PORT_WOULDBLK EWOULDBLOCK
429 #endif
430
431 #if defined(USE_POSIX)
432 # define USE_SETSID
433 #endif
434
435 #if defined(USE_POSIX) || !defined(SYSV)
436 #define USE_WAITPID
437 #endif
438
439 #if !defined(USE_POSIX)
440 #define waitpid(x,y,z) (wait3(y,z,(struct rusage *)NULL))
441 #endif
442
443 #if defined(__APPLE__) || defined(_AIX) || defined(sun386)
444 # undef WIFEXITED
445 # undef WEXITSTATUS
446 # undef WIFSIGNALED
447 # undef WTERMSIG
448 #endif /* NeXT */
449
450 #if defined(sequent)
451 #define WEXITSTATUS(x) ((x).w_retcode)
452 #define WTERMSIG(x) ((x).w_termsig)
453 #endif /* sequent */
454
455 #if !defined(WIFEXITED)
456 # define WIFEXITED(x) (!(x & 0177))
457 #endif
458 #if !defined(WEXITSTATUS)
459 # define WEXITSTATUS(x) (x >> 8)
460 #endif
461 #if !defined(WIFSIGNALED)
462 # define WIFSIGNALED(x) ((x & 0177) && ((x & 0377) != 0177))
463 #endif
464 #if !defined(WTERMSIG)
465 # define WTERMSIG(x) (x & 0177)
466 #endif
467
468 #ifndef S_ISDIR
469 # ifndef S_IFMT
470 # define S_IFMT 0170000
471 # endif
472 # ifndef S_IFDIR
473 # define S_IFDIR 0040000
474 # endif
475 # define S_ISDIR(m) ((m & S_IFMT) == S_IFDIR)
476 #endif
477
478 #ifndef S_ISREG
479 # ifndef S_IFMT
480 # define S_IFMT 0170000
481 # endif
482 # ifndef S_IFREG
483 # define S_IFREG 0100000
484 # endif
485 # define S_ISREG(m) ((m & S_IFMT) == S_IFREG)
486 #endif
487
488 #ifndef S_ISFIFO
489 # ifndef S_IFMT
490 # define S_IFMT 0170000
491 # endif
492 # ifndef S_IFIFO
493 # define S_IFIFO 0010000
494 # endif
495 # define S_ISFIFO(m) ((m & S_IFMT) == S_IFIFO)
496 #endif
497
498 #if defined(NEED_STRTOUL) && \
499 (defined(__ultrix) || defined(__osf__) || defined(NeXT))
500 # undef NEED_STRTOUL
501 #endif
502
503 #if defined(__ultrix) || defined(__osf__)
504 # define MAYBE_HESIOD
505 #endif
506
507 #ifndef FD_SET
508 #define NFDBITS 32
509 #define FD_SETSIZE 32
510 #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
511 #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
512 #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
513 #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p)))
514 #endif
515
516 #ifndef MIN
517 # define MIN(x, y) ((x > y) ?y :x)
518 #endif
519 #ifndef MAX
520 # define MAX(x, y) ((x > y) ?x :y)
521 #endif
522
523 #if !defined(PATH_MAX)
524 # if defined(_POSIX_PATH_MAX)
525 # define PATH_MAX _POSIX_PATH_MAX
526 # else
527 # if defined(MAXPATHLEN)
528 # define PATH_MAX MAXPATHLEN
529 # endif
530 # endif
531 #endif
532
533 #if defined(BSD) || defined(__osf__) || defined(__convex__)
534 # define HAVE_GETRUSAGE
535 #endif
536
537 /* May be set in the Makefile. */
538 #if defined(HAVE_GETRUSAGE)
539 # include <sys/resource.h>
540 #endif
541
542 /*
543 * Because Convex has true library function feof() which is
544 * patently wrong (it test bit _IOREAD) we need feof() as
545 * a macro.
546 */
547 #if defined(__convex__) && !defined(feof)
548 # define feof(p) ((p)->_flag&_IOEOF)
549 #endif
550
551 #if defined(M_UNIX) || defined(linux)
552 # define SPURIOUS_ECONNREFUSED
553 #endif
554
555 /*
556 * Assume that a system has fchmod() unless something above says otherwise.
557 */
558 #if !defined(HAVE_FCHMOD)
559 # define HAVE_FCHMOD 1
560 #endif
561
562 /*
563 * Prototype the functions we'll be supplying.
564 */
565 #ifdef NEED_PUTENV
566 extern int putenv __P((char *));
567 #endif
568
569 #ifdef NEED_GETTIMEOFDAY
570 extern int gettimeofday __P((struct timeval *, struct _TIMEZONE *));
571 #endif