1280849Scy/*  -*- Mode: C -*-
2181834Sroberto *
3280849Scy *  compat.h is free software.
4280849Scy *  This file is part of AutoGen and AutoOpts.
5181834Sroberto *
6285169Scy *  AutoGen Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
7280849Scy *
8280849Scy *  AutoOpts is available under any one of two licenses.  The license
9280849Scy *  in use must be one of these two and the choice is under the control
10280849Scy *  of the user of the license.
11280849Scy *
12280849Scy *   The GNU Lesser General Public License, version 3 or later
13280849Scy *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
14280849Scy *
15280849Scy *   The Modified Berkeley Software Distribution License
16280849Scy *      See the file "COPYING.mbsd"
17280849Scy *
18280849Scy *  These files have the following sha256 sums:
19280849Scy *
20280849Scy *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
21280849Scy *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
22280849Scy *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
23181834Sroberto */
24280849Scy
25280849Scy/**
26280849Scy * \file compat.h
27280849Scy *  fake the preprocessor into handlng stuff portability
28280849Scy */
29181834Sroberto#ifndef COMPAT_H_GUARD
30181834Sroberto#define COMPAT_H_GUARD 1
31181834Sroberto
32181834Sroberto#if defined(HAVE_CONFIG_H)
33181834Sroberto#  include <config.h>
34181834Sroberto
35181834Sroberto#elif defined(_WIN32) && !defined(__CYGWIN__)
36181834Sroberto#  include "windows-config.h"
37181834Sroberto
38181834Sroberto#else
39181834Sroberto#  error "compat.h" requires "config.h"
40181834Sroberto   choke me.
41181834Sroberto#endif
42181834Sroberto
43181834Sroberto
44181834Sroberto#ifndef HAVE_STRSIGNAL
45280849Scy# ifndef HAVE_RAW_DECL_STRSIGNAL
46280849Scy   char * strsignal(int signo);
47280849Scy# endif
48181834Sroberto#endif
49181834Sroberto
50181834Sroberto#define  _GNU_SOURCE    1 /* for strsignal in GNU's libc */
51181834Sroberto#define  __USE_GNU      1 /* exact same thing as above   */
52181834Sroberto#define  __EXTENSIONS__ 1 /* and another way to call for it */
53181834Sroberto
54181834Sroberto/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
55181834Sroberto *
56181834Sroberto *  SYSTEM HEADERS:
57181834Sroberto */
58181834Sroberto#include <sys/types.h>
59181834Sroberto#ifdef HAVE_SYS_MMAN_H
60181834Sroberto#  include <sys/mman.h>
61181834Sroberto#endif
62181834Sroberto#include <sys/param.h>
63181834Sroberto#if HAVE_SYS_PROCSET_H
64181834Sroberto#  include <sys/procset.h>
65181834Sroberto#endif
66181834Sroberto#include <sys/stat.h>
67280849Scy#ifdef HAVE_SYS_WAIT_H
68280849Scy#  include <sys/wait.h>
69280849Scy#endif
70181834Sroberto
71181834Sroberto#if defined( HAVE_SOLARIS_SYSINFO )
72181834Sroberto#  include <sys/systeminfo.h>
73181834Sroberto#elif defined( HAVE_UNAME_SYSCALL )
74181834Sroberto#  include <sys/utsname.h>
75181834Sroberto#endif
76181834Sroberto
77181834Sroberto#ifdef DAEMON_ENABLED
78181834Sroberto#  if HAVE_SYS_STROPTS_H
79181834Sroberto#  include <sys/stropts.h>
80181834Sroberto#  endif
81181834Sroberto
82181834Sroberto#  if HAVE_SYS_SOCKET_H
83181834Sroberto#  include <sys/socket.h>
84181834Sroberto#  endif
85181834Sroberto
86181834Sroberto#  if ! defined(HAVE_SYS_POLL_H) && ! defined(HAVE_SYS_SELECT_H)
87181834Sroberto#    error This system cannot support daemon processing
88181834Sroberto     Choke Me.
89181834Sroberto#  endif
90181834Sroberto
91181834Sroberto#  if HAVE_SYS_POLL_H
92181834Sroberto#  include <sys/poll.h>
93181834Sroberto#  endif
94181834Sroberto
95181834Sroberto#  if HAVE_SYS_SELECT_H
96181834Sroberto#  include <sys/select.h>
97181834Sroberto#  endif
98181834Sroberto
99181834Sroberto#  if HAVE_NETINET_IN_H
100181834Sroberto#  include <netinet/in.h>
101181834Sroberto#  endif
102181834Sroberto
103181834Sroberto#  if HAVE_SYS_UN_H
104181834Sroberto#  include <sys/un.h>
105181834Sroberto#  endif
106181834Sroberto#endif
107181834Sroberto
108181834Sroberto/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
109181834Sroberto *
110181834Sroberto *  USER HEADERS:
111181834Sroberto */
112181834Sroberto#include <stdio.h>
113181834Sroberto#include <assert.h>
114181834Sroberto#include <ctype.h>
115181834Sroberto
116181834Sroberto/*
117181834Sroberto *  Directory opening stuff:
118181834Sroberto */
119181834Sroberto# if defined (_POSIX_SOURCE)
120181834Sroberto/* Posix does not require that the d_ino field be present, and some
121181834Sroberto   systems do not provide it. */
122181834Sroberto#    define REAL_DIR_ENTRY(dp) 1
123181834Sroberto# else /* !_POSIX_SOURCE */
124181834Sroberto#    define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
125181834Sroberto# endif /* !_POSIX_SOURCE */
126181834Sroberto
127181834Sroberto# if defined (HAVE_DIRENT_H)
128181834Sroberto#   include <dirent.h>
129181834Sroberto#   define D_NAMLEN(dirent) strlen((dirent)->d_name)
130181834Sroberto# else /* !HAVE_DIRENT_H */
131181834Sroberto#   define dirent direct
132181834Sroberto#   define D_NAMLEN(dirent) (dirent)->d_namlen
133181834Sroberto#   if defined (HAVE_SYS_NDIR_H)
134181834Sroberto#     include <sys/ndir.h>
135181834Sroberto#   endif /* HAVE_SYS_NDIR_H */
136181834Sroberto#   if defined (HAVE_SYS_DIR_H)
137181834Sroberto#     include <sys/dir.h>
138181834Sroberto#   endif /* HAVE_SYS_DIR_H */
139181834Sroberto#   if defined (HAVE_NDIR_H)
140181834Sroberto#     include <ndir.h>
141181834Sroberto#   endif /* HAVE_NDIR_H */
142181834Sroberto# endif /* !HAVE_DIRENT_H */
143181834Sroberto
144181834Sroberto#include <errno.h>
145181834Sroberto#ifdef HAVE_FCNTL_H
146181834Sroberto# include <fcntl.h>
147181834Sroberto#endif
148181834Sroberto#ifndef O_NONBLOCK
149181834Sroberto# define O_NONBLOCK FNDELAY
150181834Sroberto#endif
151181834Sroberto
152181834Sroberto#if defined(HAVE_LIBGEN) && defined(HAVE_LIBGEN_H)
153181834Sroberto#  include <libgen.h>
154181834Sroberto#endif
155181834Sroberto
156181834Sroberto#if defined(HAVE_LIMITS_H)  /* this is also in options.h */
157181834Sroberto#  include <limits.h>
158181834Sroberto#elif defined(HAVE_SYS_LIMITS_H)
159181834Sroberto#  include <sys/limits.h>
160181834Sroberto#endif /* HAVE_LIMITS/SYS_LIMITS_H */
161181834Sroberto
162181834Sroberto#include <memory.h>
163181834Sroberto#include <setjmp.h>
164181834Sroberto#include <signal.h>
165181834Sroberto
166280849Scy#if defined(HAVE_STDINT_H)
167181834Sroberto#  include <stdint.h>
168280849Scy
169280849Scy#elif defined(HAVE_INTTYPES_H)
170181834Sroberto#  include <inttypes.h>
171181834Sroberto#endif
172181834Sroberto
173181834Sroberto#include <stdlib.h>
174181834Sroberto#include <string.h>
175181834Sroberto#include <time.h>
176181834Sroberto
177181834Sroberto#ifdef HAVE_UTIME_H
178181834Sroberto#  include <utime.h>
179181834Sroberto#endif
180181834Sroberto
181181834Sroberto#ifdef HAVE_UNISTD_H
182181834Sroberto#  include <unistd.h>
183181834Sroberto#endif
184181834Sroberto
185280849Scy#ifdef HAVE_STDBOOL_H
186280849Scy#  include <stdbool.h>
187280849Scy#else
188280849Scy   typedef enum { false = 0, true = 1 } _Bool;
189280849Scy#  define bool _Bool
190280849Scy
191280849Scy   /* The other macros must be usable in preprocessor directives.  */
192280849Scy#  define false 0
193280849Scy#  define true 1
194280849Scy#endif
195280849Scy
196181834Sroberto/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
197181834Sroberto *
198181834Sroberto *  FIXUPS and CONVIENCE STUFF:
199181834Sroberto */
200181834Sroberto#ifdef __cplusplus
201181834Sroberto#   define EXTERN extern "C"
202181834Sroberto#else
203181834Sroberto#   define EXTERN extern
204181834Sroberto#endif
205181834Sroberto
206181834Sroberto/* some systems #def errno! and others do not declare it!! */
207181834Sroberto#ifndef errno
208181834Sroberto   extern int errno;
209181834Sroberto#endif
210181834Sroberto
211181834Sroberto/* Some machines forget this! */
212181834Sroberto
213181834Sroberto# ifndef EXIT_FAILURE
214181834Sroberto#   define EXIT_SUCCESS 0
215181834Sroberto#   define EXIT_FAILURE 1
216181834Sroberto# endif
217181834Sroberto
218181834Sroberto#ifndef NUL
219181834Sroberto#  define NUL '\0'
220181834Sroberto#endif
221181834Sroberto
222181834Sroberto#ifndef NULL
223181834Sroberto#  define NULL 0
224181834Sroberto#endif
225181834Sroberto
226181834Sroberto#if !defined (MAXPATHLEN) && defined (HAVE_SYS_PARAM_H)
227181834Sroberto#  include <sys/param.h>
228181834Sroberto#endif /* !MAXPATHLEN && HAVE_SYS_PARAM_H */
229181834Sroberto
230181834Sroberto#if !defined (MAXPATHLEN) && defined (PATH_MAX)
231181834Sroberto#  define MAXPATHLEN PATH_MAX
232181834Sroberto#endif /* !MAXPATHLEN && PATH_MAX */
233181834Sroberto
234181834Sroberto#if !defined (MAXPATHLEN) && defined(_MAX_PATH)
235181834Sroberto#  define PATH_MAX _MAX_PATH
236181834Sroberto#  define MAXPATHLEN _MAX_PATH
237181834Sroberto#endif
238181834Sroberto
239181834Sroberto#if !defined (MAXPATHLEN)
240280849Scy#  define MAXPATHLEN 4096
241181834Sroberto#endif /* MAXPATHLEN */
242181834Sroberto
243181834Sroberto#define AG_PATH_MAX  ((size_t)MAXPATHLEN)
244181834Sroberto
245181834Sroberto#ifndef LONG_MAX
246181834Sroberto#  define LONG_MAX      ~(1L << (8*sizeof(long) -1))
247181834Sroberto#  define INT_MAX       ~(1 << (8*sizeof(int) -1))
248181834Sroberto#endif
249181834Sroberto
250181834Sroberto#ifndef ULONG_MAX
251181834Sroberto#  define ULONG_MAX     ~(OUL)
252181834Sroberto#  define UINT_MAX      ~(OU)
253181834Sroberto#endif
254181834Sroberto
255181834Sroberto#ifndef SHORT_MAX
256280849Scy#  define SHORT_MAX     ~(1 << (8*sizeof(short) - 1))
257181834Sroberto#else
258181834Sroberto#  define USHORT_MAX    ~(OUS)
259181834Sroberto#endif
260181834Sroberto
261181834Sroberto#ifndef HAVE_INT8_T
262280849Scy  typedef signed char           int8_t;
263280849Scy# define  HAVE_INT8_T           1
264181834Sroberto#endif
265181834Sroberto#ifndef HAVE_UINT8_T
266280849Scy  typedef unsigned char         uint8_t;
267280849Scy# define  HAVE_UINT8_T          1
268181834Sroberto#endif
269181834Sroberto#ifndef HAVE_INT16_T
270280849Scy  typedef signed short          int16_t;
271280849Scy# define  HAVE_INT16_T          1
272181834Sroberto#endif
273181834Sroberto#ifndef HAVE_UINT16_T
274280849Scy  typedef unsigned short        uint16_t;
275280849Scy# define  HAVE_UINT16_T         1
276181834Sroberto#endif
277181834Sroberto
278181834Sroberto#ifndef HAVE_INT32_T
279280849Scy# if SIZEOF_INT ==              4
280280849Scy    typedef signed int          int32_t;
281280849Scy# elif SIZEOF_LONG ==           4
282280849Scy    typedef signed long         int32_t;
283181834Sroberto# endif
284280849Scy# define  HAVE_INT32_T          1
285181834Sroberto#endif
286181834Sroberto
287181834Sroberto#ifndef HAVE_UINT32_T
288280849Scy# if SIZEOF_INT ==              4
289280849Scy    typedef unsigned int        uint32_t;
290280849Scy# elif SIZEOF_LONG ==           4
291280849Scy    typedef unsigned long       uint32_t;
292181834Sroberto# else
293181834Sroberto#   error Cannot create a uint32_t type.
294181834Sroberto    Choke Me.
295181834Sroberto# endif
296280849Scy# define  HAVE_UINT32_T         1
297181834Sroberto#endif
298181834Sroberto
299181834Sroberto#ifndef HAVE_INTPTR_T
300280849Scy# if SIZEOF_CHARP == SIZEOF_LONG
301280849Scy    typedef signed long         intptr_t;
302280849Scy# else
303280849Scy    typedef signed int          intptr_t;
304280849Scy# endif
305280849Scy# define  HAVE_INTPTR_T         1
306181834Sroberto#endif
307280849Scy
308181834Sroberto#ifndef HAVE_UINTPTR_T
309280849Scy# if SIZEOF_CHARP == SIZEOF_LONG
310280849Scy    typedef unsigned long       intptr_t;
311280849Scy# else
312280849Scy    typedef unsigned int        intptr_t;
313280849Scy# endif
314280849Scy# define  HAVE_INTPTR_T         1
315181834Sroberto#endif
316181834Sroberto
317280849Scy#ifndef HAVE_UINT_T
318280849Scy  typedef unsigned int          uint_t;
319280849Scy# define  HAVE_UINT_T           1
320280849Scy#endif
321280849Scy
322280849Scy#ifndef HAVE_SIZE_T
323280849Scy  typedef unsigned int          size_t;
324280849Scy# define  HAVE_SIZE_T           1
325280849Scy#endif
326280849Scy#ifndef HAVE_WINT_T
327280849Scy  typedef unsigned int          wint_t;
328280849Scy# define  HAVE_WINT_T           1
329280849Scy#endif
330280849Scy#ifndef HAVE_PID_T
331280849Scy  typedef signed int            pid_t;
332280849Scy# define  HAVE_PID_T            1
333280849Scy#endif
334280849Scy
335181834Sroberto/* redefine these for BSD style string libraries */
336181834Sroberto#ifndef HAVE_STRCHR
337280849Scy#  define strchr            index
338280849Scy#  define strrchr           rindex
339181834Sroberto#endif
340181834Sroberto
341181834Sroberto#ifdef USE_FOPEN_BINARY
342181834Sroberto#  ifndef FOPEN_BINARY_FLAG
343181834Sroberto#    define FOPEN_BINARY_FLAG   "b"
344181834Sroberto#  endif
345181834Sroberto#  ifndef FOPEN_TEXT_FLAG
346181834Sroberto#    define FOPEN_TEXT_FLAG     "t"
347181834Sroberto#  endif
348181834Sroberto#else
349181834Sroberto#  ifndef FOPEN_BINARY_FLAG
350181834Sroberto#    define FOPEN_BINARY_FLAG
351181834Sroberto#  endif
352181834Sroberto#  ifndef FOPEN_TEXT_FLAG
353181834Sroberto#    define FOPEN_TEXT_FLAG
354181834Sroberto#  endif
355181834Sroberto#endif
356181834Sroberto
357181834Sroberto#ifndef STR
358181834Sroberto#  define _STR(s) #s
359181834Sroberto#  define STR(s)  _STR(s)
360181834Sroberto#endif
361181834Sroberto
362181834Sroberto/* ##### Pointer sized word ##### */
363181834Sroberto
364181834Sroberto/* FIXME:  the MAX stuff in here is broken! */
365181834Sroberto#if SIZEOF_CHARP > SIZEOF_INT
366181834Sroberto   typedef long t_word;
367181834Sroberto   #define WORD_MAX  LONG_MAX
368181834Sroberto   #define WORD_MIN  LONG_MIN
369181834Sroberto#else /* SIZEOF_CHARP <= SIZEOF_INT */
370181834Sroberto   typedef int t_word;
371181834Sroberto   #define WORD_MAX  INT_MAX
372181834Sroberto   #define WORD_MIN  INT_MIN
373181834Sroberto#endif
374181834Sroberto
375181834Sroberto#endif /* COMPAT_H_GUARD */
376181834Sroberto
377181834Sroberto/*
378181834Sroberto * Local Variables:
379181834Sroberto * mode: C
380181834Sroberto * c-file-style: "stroustrup"
381181834Sroberto * indent-tabs-mode: nil
382181834Sroberto * End:
383181834Sroberto * end of compat/compat.h */
384