1285612Sdelphij/*  -*- Mode: C -*-
2181834Sroberto *
3285612Sdelphij *  compat.h is free software.
4285612Sdelphij *  This file is part of AutoGen and AutoOpts.
5181834Sroberto *
6285612Sdelphij *  AutoGen Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
7285612Sdelphij *
8285612Sdelphij *  AutoOpts is available under any one of two licenses.  The license
9285612Sdelphij *  in use must be one of these two and the choice is under the control
10285612Sdelphij *  of the user of the license.
11285612Sdelphij *
12285612Sdelphij *   The GNU Lesser General Public License, version 3 or later
13285612Sdelphij *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
14285612Sdelphij *
15285612Sdelphij *   The Modified Berkeley Software Distribution License
16285612Sdelphij *      See the file "COPYING.mbsd"
17285612Sdelphij *
18285612Sdelphij *  These files have the following sha256 sums:
19285612Sdelphij *
20285612Sdelphij *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
21285612Sdelphij *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
22285612Sdelphij *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
23181834Sroberto */
24285612Sdelphij
25285612Sdelphij/**
26285612Sdelphij * \file compat.h
27285612Sdelphij *  fake the preprocessor into handlng stuff portability
28285612Sdelphij */
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
45285612Sdelphij# ifndef HAVE_RAW_DECL_STRSIGNAL
46285612Sdelphij   char * strsignal(int signo);
47285612Sdelphij# 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>
67285612Sdelphij#ifdef HAVE_SYS_WAIT_H
68285612Sdelphij#  include <sys/wait.h>
69285612Sdelphij#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
166285612Sdelphij#if defined(HAVE_STDINT_H)
167181834Sroberto#  include <stdint.h>
168285612Sdelphij
169285612Sdelphij#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
185285612Sdelphij#ifdef HAVE_STDBOOL_H
186285612Sdelphij#  include <stdbool.h>
187285612Sdelphij#else
188285612Sdelphij   typedef enum { false = 0, true = 1 } _Bool;
189285612Sdelphij#  define bool _Bool
190285612Sdelphij
191285612Sdelphij   /* The other macros must be usable in preprocessor directives.  */
192285612Sdelphij#  define false 0
193285612Sdelphij#  define true 1
194285612Sdelphij#endif
195285612Sdelphij
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)
240285612Sdelphij#  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
256285612Sdelphij#  define SHORT_MAX     ~(1 << (8*sizeof(short) - 1))
257181834Sroberto#else
258181834Sroberto#  define USHORT_MAX    ~(OUS)
259181834Sroberto#endif
260181834Sroberto
261181834Sroberto#ifndef HAVE_INT8_T
262285612Sdelphij  typedef signed char           int8_t;
263285612Sdelphij# define  HAVE_INT8_T           1
264181834Sroberto#endif
265181834Sroberto#ifndef HAVE_UINT8_T
266285612Sdelphij  typedef unsigned char         uint8_t;
267285612Sdelphij# define  HAVE_UINT8_T          1
268181834Sroberto#endif
269181834Sroberto#ifndef HAVE_INT16_T
270285612Sdelphij  typedef signed short          int16_t;
271285612Sdelphij# define  HAVE_INT16_T          1
272181834Sroberto#endif
273181834Sroberto#ifndef HAVE_UINT16_T
274285612Sdelphij  typedef unsigned short        uint16_t;
275285612Sdelphij# define  HAVE_UINT16_T         1
276181834Sroberto#endif
277181834Sroberto
278181834Sroberto#ifndef HAVE_INT32_T
279285612Sdelphij# if SIZEOF_INT ==              4
280285612Sdelphij    typedef signed int          int32_t;
281285612Sdelphij# elif SIZEOF_LONG ==           4
282285612Sdelphij    typedef signed long         int32_t;
283181834Sroberto# endif
284285612Sdelphij# define  HAVE_INT32_T          1
285181834Sroberto#endif
286181834Sroberto
287181834Sroberto#ifndef HAVE_UINT32_T
288285612Sdelphij# if SIZEOF_INT ==              4
289285612Sdelphij    typedef unsigned int        uint32_t;
290285612Sdelphij# elif SIZEOF_LONG ==           4
291285612Sdelphij    typedef unsigned long       uint32_t;
292181834Sroberto# else
293181834Sroberto#   error Cannot create a uint32_t type.
294181834Sroberto    Choke Me.
295181834Sroberto# endif
296285612Sdelphij# define  HAVE_UINT32_T         1
297181834Sroberto#endif
298181834Sroberto
299181834Sroberto#ifndef HAVE_INTPTR_T
300285612Sdelphij# if SIZEOF_CHARP == SIZEOF_LONG
301285612Sdelphij    typedef signed long         intptr_t;
302285612Sdelphij# else
303285612Sdelphij    typedef signed int          intptr_t;
304285612Sdelphij# endif
305285612Sdelphij# define  HAVE_INTPTR_T         1
306181834Sroberto#endif
307285612Sdelphij
308181834Sroberto#ifndef HAVE_UINTPTR_T
309285612Sdelphij# if SIZEOF_CHARP == SIZEOF_LONG
310285612Sdelphij    typedef unsigned long       intptr_t;
311285612Sdelphij# else
312285612Sdelphij    typedef unsigned int        intptr_t;
313285612Sdelphij# endif
314285612Sdelphij# define  HAVE_INTPTR_T         1
315181834Sroberto#endif
316181834Sroberto
317285612Sdelphij#ifndef HAVE_UINT_T
318285612Sdelphij  typedef unsigned int          uint_t;
319285612Sdelphij# define  HAVE_UINT_T           1
320285612Sdelphij#endif
321285612Sdelphij
322285612Sdelphij#ifndef HAVE_SIZE_T
323285612Sdelphij  typedef unsigned int          size_t;
324285612Sdelphij# define  HAVE_SIZE_T           1
325285612Sdelphij#endif
326285612Sdelphij#ifndef HAVE_WINT_T
327285612Sdelphij  typedef unsigned int          wint_t;
328285612Sdelphij# define  HAVE_WINT_T           1
329285612Sdelphij#endif
330285612Sdelphij#ifndef HAVE_PID_T
331285612Sdelphij  typedef signed int            pid_t;
332285612Sdelphij# define  HAVE_PID_T            1
333285612Sdelphij#endif
334285612Sdelphij
335181834Sroberto/* redefine these for BSD style string libraries */
336181834Sroberto#ifndef HAVE_STRCHR
337285612Sdelphij#  define strchr            index
338285612Sdelphij#  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