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