compat.h 8.2 KB

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