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