compat.h 8.5 KB

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