xref: /netbsd-src/external/bsd/ntp/dist/sntp/libopts/compat/compat.h (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: compat.h,v 1.6 2024/08/18 20:47:25 christos Exp $	*/
2abb0f93cSkardel 
38585484eSchristos /*  -*- Mode: C -*-
4abb0f93cSkardel  *
5abb0f93cSkardel  *  compat.h is free software.
68585484eSchristos  *  This file is part of AutoGen and AutoOpts.
7abb0f93cSkardel  *
8*eabc0478Schristos  *  AutoGen Copyright (C) 1992-2018 by Bruce Korb - all rights reserved
9abb0f93cSkardel  *
108585484eSchristos  *  AutoOpts is available under any one of two licenses.  The license
118585484eSchristos  *  in use must be one of these two and the choice is under the control
128585484eSchristos  *  of the user of the license.
13abb0f93cSkardel  *
148585484eSchristos  *   The GNU Lesser General Public License, version 3 or later
158585484eSchristos  *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
16abb0f93cSkardel  *
178585484eSchristos  *   The Modified Berkeley Software Distribution License
188585484eSchristos  *      See the file "COPYING.mbsd"
19abb0f93cSkardel  *
208585484eSchristos  *  These files have the following sha256 sums:
21abb0f93cSkardel  *
228585484eSchristos  *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
238585484eSchristos  *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
248585484eSchristos  *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
258585484eSchristos  */
268585484eSchristos 
278585484eSchristos /**
288585484eSchristos  * \file compat.h
298585484eSchristos  *  fake the preprocessor into handlng stuff portability
30abb0f93cSkardel  */
31abb0f93cSkardel #ifndef COMPAT_H_GUARD
32abb0f93cSkardel #define COMPAT_H_GUARD 1
33abb0f93cSkardel 
34abb0f93cSkardel #if defined(HAVE_CONFIG_H)
35abb0f93cSkardel #  include <config.h>
36abb0f93cSkardel 
37abb0f93cSkardel #elif defined(_WIN32) && !defined(__CYGWIN__)
38abb0f93cSkardel #  include "windows-config.h"
39abb0f93cSkardel 
40abb0f93cSkardel #else
41abb0f93cSkardel #  error "compat.h" requires "config.h"
42abb0f93cSkardel    choke me.
43abb0f93cSkardel #endif
44abb0f93cSkardel 
45abb0f93cSkardel 
46abb0f93cSkardel #ifndef HAVE_STRSIGNAL
478585484eSchristos # ifndef HAVE_RAW_DECL_STRSIGNAL
48abb0f93cSkardel    char * strsignal(int signo);
49abb0f93cSkardel # endif
508585484eSchristos #endif
51abb0f93cSkardel 
52abb0f93cSkardel #define  _GNU_SOURCE    1 /* for strsignal in GNU's libc */
53abb0f93cSkardel #define  __USE_GNU      1 /* exact same thing as above   */
54abb0f93cSkardel #define  __EXTENSIONS__ 1 /* and another way to call for it */
55abb0f93cSkardel 
56abb0f93cSkardel /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
57abb0f93cSkardel  *
58abb0f93cSkardel  *  SYSTEM HEADERS:
59abb0f93cSkardel  */
60abb0f93cSkardel #include <sys/types.h>
61abb0f93cSkardel #ifdef HAVE_SYS_MMAN_H
62abb0f93cSkardel #  include <sys/mman.h>
63abb0f93cSkardel #endif
64abb0f93cSkardel #include <sys/param.h>
65abb0f93cSkardel #if HAVE_SYS_PROCSET_H
66abb0f93cSkardel #  include <sys/procset.h>
67abb0f93cSkardel #endif
68abb0f93cSkardel #include <sys/stat.h>
698585484eSchristos #ifdef HAVE_SYS_WAIT_H
70abb0f93cSkardel #  include <sys/wait.h>
718585484eSchristos #endif
72abb0f93cSkardel 
73abb0f93cSkardel #if defined( HAVE_SOLARIS_SYSINFO )
74abb0f93cSkardel #  include <sys/systeminfo.h>
75abb0f93cSkardel #elif defined( HAVE_UNAME_SYSCALL )
76abb0f93cSkardel #  include <sys/utsname.h>
77abb0f93cSkardel #endif
78abb0f93cSkardel 
79abb0f93cSkardel #ifdef DAEMON_ENABLED
80abb0f93cSkardel #  if HAVE_SYS_STROPTS_H
81abb0f93cSkardel #  include <sys/stropts.h>
82abb0f93cSkardel #  endif
83abb0f93cSkardel 
84abb0f93cSkardel #  if HAVE_SYS_SOCKET_H
85abb0f93cSkardel #  include <sys/socket.h>
86abb0f93cSkardel #  endif
87abb0f93cSkardel 
88abb0f93cSkardel #  if ! defined(HAVE_SYS_POLL_H) && ! defined(HAVE_SYS_SELECT_H)
89abb0f93cSkardel #    error This system cannot support daemon processing
90abb0f93cSkardel      Choke Me.
91abb0f93cSkardel #  endif
92abb0f93cSkardel 
93abb0f93cSkardel #  if HAVE_SYS_POLL_H
94abb0f93cSkardel #  include <sys/poll.h>
95abb0f93cSkardel #  endif
96abb0f93cSkardel 
97abb0f93cSkardel #  if HAVE_SYS_SELECT_H
98abb0f93cSkardel #  include <sys/select.h>
99abb0f93cSkardel #  endif
100abb0f93cSkardel 
101abb0f93cSkardel #  if HAVE_NETINET_IN_H
102abb0f93cSkardel #  include <netinet/in.h>
103abb0f93cSkardel #  endif
104abb0f93cSkardel 
105abb0f93cSkardel #  if HAVE_SYS_UN_H
106abb0f93cSkardel #  include <sys/un.h>
107abb0f93cSkardel #  endif
108abb0f93cSkardel #endif
109abb0f93cSkardel 
110abb0f93cSkardel /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
111abb0f93cSkardel  *
112abb0f93cSkardel  *  USER HEADERS:
113abb0f93cSkardel  */
114abb0f93cSkardel #include <stdio.h>
115abb0f93cSkardel #include <assert.h>
116abb0f93cSkardel #include <ctype.h>
117abb0f93cSkardel 
118abb0f93cSkardel /*
119abb0f93cSkardel  *  Directory opening stuff:
120abb0f93cSkardel  */
121abb0f93cSkardel # if defined (_POSIX_SOURCE)
122abb0f93cSkardel /* Posix does not require that the d_ino field be present, and some
123abb0f93cSkardel    systems do not provide it. */
124abb0f93cSkardel #    define REAL_DIR_ENTRY(dp) 1
125abb0f93cSkardel # else /* !_POSIX_SOURCE */
126abb0f93cSkardel #    define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
127abb0f93cSkardel # endif /* !_POSIX_SOURCE */
128abb0f93cSkardel 
129abb0f93cSkardel # if defined (HAVE_DIRENT_H)
130abb0f93cSkardel #   include <dirent.h>
131abb0f93cSkardel #   define D_NAMLEN(dirent) strlen((dirent)->d_name)
132abb0f93cSkardel # else /* !HAVE_DIRENT_H */
133abb0f93cSkardel #   define dirent direct
134abb0f93cSkardel #   define D_NAMLEN(dirent) (dirent)->d_namlen
135abb0f93cSkardel #   if defined (HAVE_SYS_NDIR_H)
136abb0f93cSkardel #     include <sys/ndir.h>
137abb0f93cSkardel #   endif /* HAVE_SYS_NDIR_H */
138abb0f93cSkardel #   if defined (HAVE_SYS_DIR_H)
139abb0f93cSkardel #     include <sys/dir.h>
140abb0f93cSkardel #   endif /* HAVE_SYS_DIR_H */
141abb0f93cSkardel #   if defined (HAVE_NDIR_H)
142abb0f93cSkardel #     include <ndir.h>
143abb0f93cSkardel #   endif /* HAVE_NDIR_H */
144abb0f93cSkardel # endif /* !HAVE_DIRENT_H */
145abb0f93cSkardel 
146abb0f93cSkardel #include <errno.h>
147abb0f93cSkardel #ifdef HAVE_FCNTL_H
148abb0f93cSkardel # include <fcntl.h>
149abb0f93cSkardel #endif
150abb0f93cSkardel #ifndef O_NONBLOCK
151abb0f93cSkardel # define O_NONBLOCK FNDELAY
152abb0f93cSkardel #endif
153abb0f93cSkardel 
154abb0f93cSkardel #if defined(HAVE_LIBGEN) && defined(HAVE_LIBGEN_H)
155abb0f93cSkardel #  include <libgen.h>
156abb0f93cSkardel #endif
157abb0f93cSkardel 
158abb0f93cSkardel #if defined(HAVE_LIMITS_H)  /* this is also in options.h */
159abb0f93cSkardel #  include <limits.h>
160abb0f93cSkardel #elif defined(HAVE_SYS_LIMITS_H)
161abb0f93cSkardel #  include <sys/limits.h>
162abb0f93cSkardel #endif /* HAVE_LIMITS/SYS_LIMITS_H */
163abb0f93cSkardel 
164abb0f93cSkardel #include <memory.h>
165abb0f93cSkardel #include <setjmp.h>
166abb0f93cSkardel #include <signal.h>
167abb0f93cSkardel 
168abb0f93cSkardel #if defined(HAVE_STDINT_H)
169abb0f93cSkardel #  include <stdint.h>
1708585484eSchristos 
171abb0f93cSkardel #elif defined(HAVE_INTTYPES_H)
172abb0f93cSkardel #  include <inttypes.h>
173abb0f93cSkardel #endif
174abb0f93cSkardel 
175abb0f93cSkardel #include <stdlib.h>
176abb0f93cSkardel #include <string.h>
177abb0f93cSkardel #include <time.h>
178abb0f93cSkardel 
179abb0f93cSkardel #ifdef HAVE_UTIME_H
180abb0f93cSkardel #  include <utime.h>
181abb0f93cSkardel #endif
182abb0f93cSkardel 
183abb0f93cSkardel #ifdef HAVE_UNISTD_H
184abb0f93cSkardel #  include <unistd.h>
185abb0f93cSkardel #endif
186abb0f93cSkardel 
187*eabc0478Schristos #if defined(HAVE_STDBOOL_H)
1888585484eSchristos #  include <stdbool.h>
189*eabc0478Schristos #elif ! defined(bool)
1908585484eSchristos    typedef enum { false = 0, true = 1 } _Bool;
1918585484eSchristos #  define bool _Bool
1928585484eSchristos 
1938585484eSchristos    /* The other macros must be usable in preprocessor directives.  */
1948585484eSchristos #  define false 0
1958585484eSchristos #  define true 1
1968585484eSchristos #endif
1978585484eSchristos 
198abb0f93cSkardel /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
199abb0f93cSkardel  *
200abb0f93cSkardel  *  FIXUPS and CONVIENCE STUFF:
201abb0f93cSkardel  */
202abb0f93cSkardel #ifdef __cplusplus
203abb0f93cSkardel #   define EXTERN extern "C"
204abb0f93cSkardel #else
205abb0f93cSkardel #   define EXTERN extern
206abb0f93cSkardel #endif
207abb0f93cSkardel 
208abb0f93cSkardel /* some systems #def errno! and others do not declare it!! */
209abb0f93cSkardel #ifndef errno
210abb0f93cSkardel    extern int errno;
211abb0f93cSkardel #endif
212abb0f93cSkardel 
213abb0f93cSkardel /* Some machines forget this! */
214abb0f93cSkardel 
215abb0f93cSkardel # ifndef EXIT_FAILURE
216abb0f93cSkardel #   define EXIT_SUCCESS 0
217abb0f93cSkardel #   define EXIT_FAILURE 1
218abb0f93cSkardel # endif
219abb0f93cSkardel 
220abb0f93cSkardel #ifndef NUL
221abb0f93cSkardel #  define NUL '\0'
222abb0f93cSkardel #endif
223abb0f93cSkardel 
224abb0f93cSkardel #ifndef NULL
225abb0f93cSkardel #  define NULL 0
226abb0f93cSkardel #endif
227abb0f93cSkardel 
228abb0f93cSkardel #if !defined (MAXPATHLEN) && defined (HAVE_SYS_PARAM_H)
229abb0f93cSkardel #  include <sys/param.h>
230abb0f93cSkardel #endif /* !MAXPATHLEN && HAVE_SYS_PARAM_H */
231abb0f93cSkardel 
232abb0f93cSkardel #if !defined (MAXPATHLEN) && defined (PATH_MAX)
233abb0f93cSkardel #  define MAXPATHLEN PATH_MAX
234abb0f93cSkardel #endif /* !MAXPATHLEN && PATH_MAX */
235abb0f93cSkardel 
236abb0f93cSkardel #if !defined (MAXPATHLEN) && defined(_MAX_PATH)
237abb0f93cSkardel #  define PATH_MAX _MAX_PATH
238abb0f93cSkardel #  define MAXPATHLEN _MAX_PATH
239abb0f93cSkardel #endif
240abb0f93cSkardel 
241abb0f93cSkardel #if !defined (MAXPATHLEN)
2428585484eSchristos #  define MAXPATHLEN 4096
243abb0f93cSkardel #endif /* MAXPATHLEN */
244abb0f93cSkardel 
245abb0f93cSkardel #define AG_PATH_MAX  ((size_t)MAXPATHLEN)
246abb0f93cSkardel 
247abb0f93cSkardel #ifndef LONG_MAX
248abb0f93cSkardel #  define LONG_MAX      ~(1L << (8*sizeof(long) -1))
249abb0f93cSkardel #  define INT_MAX       ~(1 << (8*sizeof(int) -1))
250abb0f93cSkardel #endif
251abb0f93cSkardel 
252abb0f93cSkardel #ifndef ULONG_MAX
253abb0f93cSkardel #  define ULONG_MAX     ~(OUL)
254abb0f93cSkardel #  define UINT_MAX      ~(OU)
255abb0f93cSkardel #endif
256abb0f93cSkardel 
257abb0f93cSkardel #ifndef SHORT_MAX
258abb0f93cSkardel #  define SHORT_MAX     ~(1 << (8*sizeof(short) - 1))
259abb0f93cSkardel #else
260abb0f93cSkardel #  define USHORT_MAX    ~(OUS)
261abb0f93cSkardel #endif
262abb0f93cSkardel 
263abb0f93cSkardel #ifndef HAVE_INT8_T
264abb0f93cSkardel   typedef signed char           int8_t;
265abb0f93cSkardel # define  HAVE_INT8_T           1
266abb0f93cSkardel #endif
267abb0f93cSkardel #ifndef HAVE_UINT8_T
268abb0f93cSkardel   typedef unsigned char         uint8_t;
269abb0f93cSkardel # define  HAVE_UINT8_T          1
270abb0f93cSkardel #endif
271abb0f93cSkardel #ifndef HAVE_INT16_T
272abb0f93cSkardel   typedef signed short          int16_t;
273abb0f93cSkardel # define  HAVE_INT16_T          1
274abb0f93cSkardel #endif
275abb0f93cSkardel #ifndef HAVE_UINT16_T
276abb0f93cSkardel   typedef unsigned short        uint16_t;
277abb0f93cSkardel # define  HAVE_UINT16_T         1
278abb0f93cSkardel #endif
279abb0f93cSkardel 
280abb0f93cSkardel #ifndef HAVE_INT32_T
281abb0f93cSkardel # if SIZEOF_INT ==              4
282abb0f93cSkardel     typedef signed int          int32_t;
283abb0f93cSkardel # elif SIZEOF_LONG ==           4
284abb0f93cSkardel     typedef signed long         int32_t;
285abb0f93cSkardel # endif
286abb0f93cSkardel # define  HAVE_INT32_T          1
287abb0f93cSkardel #endif
288abb0f93cSkardel 
289abb0f93cSkardel #ifndef HAVE_UINT32_T
290abb0f93cSkardel # if SIZEOF_INT ==              4
291abb0f93cSkardel     typedef unsigned int        uint32_t;
292abb0f93cSkardel # elif SIZEOF_LONG ==           4
293abb0f93cSkardel     typedef unsigned long       uint32_t;
294abb0f93cSkardel # else
295abb0f93cSkardel #   error Cannot create a uint32_t type.
296abb0f93cSkardel     Choke Me.
297abb0f93cSkardel # endif
298abb0f93cSkardel # define  HAVE_UINT32_T         1
299abb0f93cSkardel #endif
300abb0f93cSkardel 
301abb0f93cSkardel #ifndef HAVE_INTPTR_T
302abb0f93cSkardel # if SIZEOF_CHARP == SIZEOF_LONG
303abb0f93cSkardel     typedef signed long         intptr_t;
304abb0f93cSkardel # else
305abb0f93cSkardel     typedef signed int          intptr_t;
306abb0f93cSkardel # endif
307abb0f93cSkardel # define  HAVE_INTPTR_T         1
308abb0f93cSkardel #endif
309abb0f93cSkardel 
310abb0f93cSkardel #ifndef HAVE_UINTPTR_T
311abb0f93cSkardel # if SIZEOF_CHARP == SIZEOF_LONG
312abb0f93cSkardel     typedef unsigned long       intptr_t;
313abb0f93cSkardel # else
314abb0f93cSkardel     typedef unsigned int        intptr_t;
315abb0f93cSkardel # endif
316abb0f93cSkardel # define  HAVE_INTPTR_T         1
317abb0f93cSkardel #endif
318abb0f93cSkardel 
319abb0f93cSkardel #ifndef HAVE_UINT_T
320abb0f93cSkardel   typedef unsigned int          uint_t;
321abb0f93cSkardel # define  HAVE_UINT_T           1
322abb0f93cSkardel #endif
323abb0f93cSkardel 
324abb0f93cSkardel #ifndef HAVE_SIZE_T
325abb0f93cSkardel   typedef unsigned int          size_t;
326abb0f93cSkardel # define  HAVE_SIZE_T           1
327abb0f93cSkardel #endif
328abb0f93cSkardel #ifndef HAVE_WINT_T
329abb0f93cSkardel   typedef unsigned int          wint_t;
330abb0f93cSkardel # define  HAVE_WINT_T           1
331abb0f93cSkardel #endif
332abb0f93cSkardel #ifndef HAVE_PID_T
333abb0f93cSkardel   typedef signed int            pid_t;
334abb0f93cSkardel # define  HAVE_PID_T            1
335abb0f93cSkardel #endif
336abb0f93cSkardel 
337abb0f93cSkardel /* redefine these for BSD style string libraries */
338abb0f93cSkardel #ifndef HAVE_STRCHR
339abb0f93cSkardel #  define strchr            index
340abb0f93cSkardel #  define strrchr           rindex
341abb0f93cSkardel #endif
342abb0f93cSkardel 
343abb0f93cSkardel #ifdef USE_FOPEN_BINARY
344abb0f93cSkardel #  ifndef FOPEN_BINARY_FLAG
345abb0f93cSkardel #    define FOPEN_BINARY_FLAG   "b"
346abb0f93cSkardel #  endif
347abb0f93cSkardel #  ifndef FOPEN_TEXT_FLAG
348abb0f93cSkardel #    define FOPEN_TEXT_FLAG     "t"
349abb0f93cSkardel #  endif
350abb0f93cSkardel #else
351abb0f93cSkardel #  ifndef FOPEN_BINARY_FLAG
352abb0f93cSkardel #    define FOPEN_BINARY_FLAG
353abb0f93cSkardel #  endif
354abb0f93cSkardel #  ifndef FOPEN_TEXT_FLAG
355abb0f93cSkardel #    define FOPEN_TEXT_FLAG
356abb0f93cSkardel #  endif
357abb0f93cSkardel #endif
358abb0f93cSkardel 
359abb0f93cSkardel #ifndef STR
360abb0f93cSkardel #  define _STR(s) #s
361abb0f93cSkardel #  define STR(s)  _STR(s)
362abb0f93cSkardel #endif
363abb0f93cSkardel 
364abb0f93cSkardel /* ##### Pointer sized word ##### */
365abb0f93cSkardel 
366abb0f93cSkardel /* FIXME:  the MAX stuff in here is broken! */
367abb0f93cSkardel #if SIZEOF_CHARP > SIZEOF_INT
368abb0f93cSkardel    typedef long t_word;
369abb0f93cSkardel    #define WORD_MAX  LONG_MAX
370abb0f93cSkardel    #define WORD_MIN  LONG_MIN
371abb0f93cSkardel #else /* SIZEOF_CHARP <= SIZEOF_INT */
372abb0f93cSkardel    typedef int t_word;
373abb0f93cSkardel    #define WORD_MAX  INT_MAX
374abb0f93cSkardel    #define WORD_MIN  INT_MIN
375abb0f93cSkardel #endif
376abb0f93cSkardel 
377abb0f93cSkardel #endif /* COMPAT_H_GUARD */
378abb0f93cSkardel 
379abb0f93cSkardel /*
380abb0f93cSkardel  * Local Variables:
381abb0f93cSkardel  * mode: C
382abb0f93cSkardel  * c-file-style: "stroustrup"
383abb0f93cSkardel  * indent-tabs-mode: nil
384abb0f93cSkardel  * End:
385abb0f93cSkardel  * end of compat/compat.h */
386