compat.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* -*- Mode: C -*- */
  2. /* compat.h --- fake the preprocessor into handlng portability
  3. *
  4. * Time-stamp: "2008-06-14 09:36:25 bkorb"
  5. *
  6. * $Id: compat.h,v 4.20 2008/06/14 22:24:22 bkorb Exp $
  7. *
  8. * compat.h is free software.
  9. * This file is part of AutoGen.
  10. *
  11. * AutoGen copyright (c) 1992-2008 Bruce Korb - all rights reserved
  12. * AutoGen copyright (c) 1992-2008 Bruce Korb - all rights reserved
  13. *
  14. * AutoGen is free software: you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the
  16. * Free Software Foundation, either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * AutoGen is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  22. * See the GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License along
  25. * with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. * As a special exception, Bruce Korb gives permission for additional
  28. * uses of the text contained in the release of compat.h.
  29. *
  30. * The exception is that, if you link the compat.h library with other
  31. * files to produce an executable, this does not by itself cause the
  32. * resulting executable to be covered by the GNU General Public License.
  33. * Your use of that executable is in no way restricted on account of
  34. * linking the compat.h library code into it.
  35. *
  36. * This exception does not however invalidate any other reasons why
  37. * the executable file might be covered by the GNU General Public License.
  38. *
  39. * This exception applies only to the code released by Bruce Korb under
  40. * the name compat.h. If you copy code from other sources under the
  41. * General Public License into a copy of compat.h, as the General Public
  42. * License permits, the exception does not apply to the code that you add
  43. * in this way. To avoid misleading anyone as to the status of such
  44. * modified files, you must delete this exception notice from them.
  45. *
  46. * If you write modifications of your own for compat.h, it is your choice
  47. * whether to permit this exception to apply to your modifications.
  48. * If you do not wish that, delete this exception notice.
  49. */
  50. #ifndef COMPAT_H_GUARD
  51. #define COMPAT_H_GUARD 1
  52. #if defined(HAVE_CONFIG_H)
  53. # include <config.h>
  54. #elif defined(_WIN32) && !defined(__CYGWIN__)
  55. # include "windows-config.h"
  56. #else
  57. # error "compat.h" requires "config.h"
  58. choke me.
  59. #endif
  60. #ifndef HAVE_STRSIGNAL
  61. char * strsignal( int signo );
  62. #endif
  63. #define _GNU_SOURCE 1 /* for strsignal in GNU's libc */
  64. #define __USE_GNU 1 /* exact same thing as above */
  65. #define __EXTENSIONS__ 1 /* and another way to call for it */
  66. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  67. *
  68. * SYSTEM HEADERS:
  69. */
  70. #include <sys/types.h>
  71. #ifdef HAVE_SYS_MMAN_H
  72. # include <sys/mman.h>
  73. #endif
  74. #include <sys/param.h>
  75. #if HAVE_SYS_PROCSET_H
  76. # include <sys/procset.h>
  77. #endif
  78. #include <sys/stat.h>
  79. #include <sys/wait.h>
  80. #if defined( HAVE_SOLARIS_SYSINFO )
  81. # include <sys/systeminfo.h>
  82. #elif defined( HAVE_UNAME_SYSCALL )
  83. # include <sys/utsname.h>
  84. #endif
  85. #ifdef DAEMON_ENABLED
  86. # if HAVE_SYS_STROPTS_H
  87. # include <sys/stropts.h>
  88. # endif
  89. # if HAVE_SYS_SOCKET_H
  90. # include <sys/socket.h>
  91. # endif
  92. # if ! defined(HAVE_SYS_POLL_H) && ! defined(HAVE_SYS_SELECT_H)
  93. # error This system cannot support daemon processing
  94. Choke Me.
  95. # endif
  96. # if HAVE_SYS_POLL_H
  97. # include <sys/poll.h>
  98. # endif
  99. # if HAVE_SYS_SELECT_H
  100. # include <sys/select.h>
  101. # endif
  102. # if HAVE_NETINET_IN_H
  103. # include <netinet/in.h>
  104. # endif
  105. # if HAVE_SYS_UN_H
  106. # include <sys/un.h>
  107. # endif
  108. #endif
  109. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  110. *
  111. * USER HEADERS:
  112. */
  113. #include <stdio.h>
  114. #include <assert.h>
  115. #include <ctype.h>
  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. # if defined (HAVE_DIRENT_H)
  127. # include <dirent.h>
  128. # define D_NAMLEN(dirent) strlen((dirent)->d_name)
  129. # else /* !HAVE_DIRENT_H */
  130. # define dirent direct
  131. # define D_NAMLEN(dirent) (dirent)->d_namlen
  132. # if defined (HAVE_SYS_NDIR_H)
  133. # include <sys/ndir.h>
  134. # endif /* HAVE_SYS_NDIR_H */
  135. # if defined (HAVE_SYS_DIR_H)
  136. # include <sys/dir.h>
  137. # endif /* HAVE_SYS_DIR_H */
  138. # if defined (HAVE_NDIR_H)
  139. # include <ndir.h>
  140. # endif /* HAVE_NDIR_H */
  141. # endif /* !HAVE_DIRENT_H */
  142. #include <errno.h>
  143. #ifdef HAVE_FCNTL_H
  144. # include <fcntl.h>
  145. #endif
  146. #ifndef O_NONBLOCK
  147. # define O_NONBLOCK FNDELAY
  148. #endif
  149. #if defined(HAVE_LIBGEN) && defined(HAVE_LIBGEN_H)
  150. # include <libgen.h>
  151. #endif
  152. #if defined(HAVE_LIMITS_H) /* this is also in options.h */
  153. # include <limits.h>
  154. #elif defined(HAVE_SYS_LIMITS_H)
  155. # include <sys/limits.h>
  156. #endif /* HAVE_LIMITS/SYS_LIMITS_H */
  157. #include <memory.h>
  158. #include <setjmp.h>
  159. #include <signal.h>
  160. #if defined( HAVE_STDINT_H )
  161. # include <stdint.h>
  162. #elif defined( HAVE_INTTYPES_H )
  163. # include <inttypes.h>
  164. #endif
  165. #include <stdlib.h>
  166. #include <string.h>
  167. #include <time.h>
  168. #ifdef HAVE_UTIME_H
  169. # include <utime.h>
  170. #endif
  171. #ifdef HAVE_UNISTD_H
  172. # include <unistd.h>
  173. #endif
  174. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  175. *
  176. * FIXUPS and CONVIENCE STUFF:
  177. */
  178. #ifdef __cplusplus
  179. # define EXTERN extern "C"
  180. #else
  181. # define EXTERN extern
  182. #endif
  183. /* some systems #def errno! and others do not declare it!! */
  184. #ifndef errno
  185. extern int errno;
  186. #endif
  187. /* Some machines forget this! */
  188. # ifndef EXIT_FAILURE
  189. # define EXIT_SUCCESS 0
  190. # define EXIT_FAILURE 1
  191. # endif
  192. #ifndef NUL
  193. # define NUL '\0'
  194. #endif
  195. #ifndef NULL
  196. # define NULL 0
  197. #endif
  198. #if !defined (MAXPATHLEN) && defined (HAVE_SYS_PARAM_H)
  199. # include <sys/param.h>
  200. #endif /* !MAXPATHLEN && HAVE_SYS_PARAM_H */
  201. #if !defined (MAXPATHLEN) && defined (PATH_MAX)
  202. # define MAXPATHLEN PATH_MAX
  203. #endif /* !MAXPATHLEN && PATH_MAX */
  204. #if !defined (MAXPATHLEN) && defined(_MAX_PATH)
  205. # define PATH_MAX _MAX_PATH
  206. # define MAXPATHLEN _MAX_PATH
  207. #endif
  208. #if !defined (MAXPATHLEN)
  209. # define MAXPATHLEN ((size_t)4096)
  210. #endif /* MAXPATHLEN */
  211. #define AG_PATH_MAX ((size_t)MAXPATHLEN)
  212. #ifndef LONG_MAX
  213. # define LONG_MAX ~(1L << (8*sizeof(long) -1))
  214. # define INT_MAX ~(1 << (8*sizeof(int) -1))
  215. #endif
  216. #ifndef ULONG_MAX
  217. # define ULONG_MAX ~(OUL)
  218. # define UINT_MAX ~(OU)
  219. #endif
  220. #ifndef SHORT_MAX
  221. # define SHORT_MAX ~(1 << (8*sizeof(short) -1))
  222. #else
  223. # define USHORT_MAX ~(OUS)
  224. #endif
  225. #ifndef HAVE_INT8_T
  226. typedef signed char int8_t;
  227. #endif
  228. #ifndef HAVE_UINT8_T
  229. typedef unsigned char uint8_t;
  230. #endif
  231. #ifndef HAVE_INT16_T
  232. typedef signed short int16_t;
  233. #endif
  234. #ifndef HAVE_UINT16_T
  235. typedef unsigned short uint16_t;
  236. #endif
  237. #ifndef HAVE_UINT_T
  238. typedef unsigned int uint_t;
  239. #endif
  240. #ifndef HAVE_INT32_T
  241. # if SIZEOF_INT == 4
  242. typedef signed int int32_t;
  243. # elif SIZEOF_LONG == 4
  244. typedef signed long int32_t;
  245. # endif
  246. #endif
  247. #ifndef HAVE_UINT32_T
  248. # if SIZEOF_INT == 4
  249. typedef unsigned int uint32_t;
  250. # elif SIZEOF_LONG == 4
  251. typedef unsigned long uint32_t;
  252. # else
  253. # error Cannot create a uint32_t type.
  254. Choke Me.
  255. # endif
  256. #endif
  257. #ifndef HAVE_INTPTR_T
  258. typedef signed long intptr_t;
  259. #endif
  260. #ifndef HAVE_UINTPTR_T
  261. typedef unsigned long uintptr_t;
  262. #endif
  263. #ifndef HAVE_SIZE_T
  264. typedef unsigned int size_t;
  265. #endif
  266. #ifndef HAVE_WINT_T
  267. typedef unsigned int wint_t;
  268. #endif
  269. #ifndef HAVE_PID_T
  270. typedef signed int pid_t;
  271. #endif
  272. /* redefine these for BSD style string libraries */
  273. #ifndef HAVE_STRCHR
  274. # define strchr index
  275. # define strrchr rindex
  276. #endif
  277. #ifdef USE_FOPEN_BINARY
  278. # ifndef FOPEN_BINARY_FLAG
  279. # define FOPEN_BINARY_FLAG "b"
  280. # endif
  281. # ifndef FOPEN_TEXT_FLAG
  282. # define FOPEN_TEXT_FLAG "t"
  283. # endif
  284. #else
  285. # ifndef FOPEN_BINARY_FLAG
  286. # define FOPEN_BINARY_FLAG
  287. # endif
  288. # ifndef FOPEN_TEXT_FLAG
  289. # define FOPEN_TEXT_FLAG
  290. # endif
  291. #endif
  292. #ifndef STR
  293. # define _STR(s) #s
  294. # define STR(s) _STR(s)
  295. #endif
  296. /* ##### Pointer sized word ##### */
  297. /* FIXME: the MAX stuff in here is broken! */
  298. #if SIZEOF_CHARP > SIZEOF_INT
  299. typedef long t_word;
  300. #define WORD_MAX LONG_MAX
  301. #define WORD_MIN LONG_MIN
  302. #else /* SIZEOF_CHARP <= SIZEOF_INT */
  303. typedef int t_word;
  304. #define WORD_MAX INT_MAX
  305. #define WORD_MIN INT_MIN
  306. #endif
  307. #endif /* COMPAT_H_GUARD */
  308. /*
  309. * Local Variables:
  310. * mode: C
  311. * c-file-style: "stroustrup"
  312. * indent-tabs-mode: nil
  313. * End:
  314. * end of compat/compat.h */