private.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. #ifndef PRIVATE_H
  2. #define PRIVATE_H
  3. /*
  4. ** This file is in the public domain, so clarified as of
  5. ** 1996-06-05 by Arthur David Olson.
  6. */
  7. /*
  8. ** This header is for use ONLY with the time conversion code.
  9. ** There is no guarantee that it will remain unchanged,
  10. ** or that it will remain at all.
  11. ** Do NOT copy it to any system include directory.
  12. ** Thank you!
  13. */
  14. #define GRANDPARENTED "Local time zone must be set--see zic manual page"
  15. /*
  16. ** Defaults for preprocessor symbols.
  17. ** You can override these in your C compiler options, e.g. `-DHAVE_ADJTIME=0'.
  18. */
  19. #ifndef HAVE_ADJTIME
  20. #define HAVE_ADJTIME 1
  21. #endif /* !defined HAVE_ADJTIME */
  22. #ifndef HAVE_GETTEXT
  23. #define HAVE_GETTEXT 0
  24. #endif /* !defined HAVE_GETTEXT */
  25. #ifndef HAVE_INCOMPATIBLE_CTIME_R
  26. #define HAVE_INCOMPATIBLE_CTIME_R 0
  27. #endif /* !defined INCOMPATIBLE_CTIME_R */
  28. #ifndef HAVE_LINK
  29. #define HAVE_LINK 1
  30. #endif /* !defined HAVE_LINK */
  31. #ifndef HAVE_SETTIMEOFDAY
  32. #define HAVE_SETTIMEOFDAY 3
  33. #endif /* !defined HAVE_SETTIMEOFDAY */
  34. #ifndef HAVE_SYMLINK
  35. #define HAVE_SYMLINK 1
  36. #endif /* !defined HAVE_SYMLINK */
  37. #ifndef HAVE_SYS_STAT_H
  38. #define HAVE_SYS_STAT_H 1
  39. #endif /* !defined HAVE_SYS_STAT_H */
  40. #ifndef HAVE_SYS_WAIT_H
  41. #define HAVE_SYS_WAIT_H 1
  42. #endif /* !defined HAVE_SYS_WAIT_H */
  43. #ifndef HAVE_UNISTD_H
  44. #define HAVE_UNISTD_H 1
  45. #endif /* !defined HAVE_UNISTD_H */
  46. #ifndef HAVE_UTMPX_H
  47. #define HAVE_UTMPX_H 0
  48. #endif /* !defined HAVE_UTMPX_H */
  49. #ifndef LOCALE_HOME
  50. #define LOCALE_HOME "/usr/lib/locale"
  51. #endif /* !defined LOCALE_HOME */
  52. #if HAVE_INCOMPATIBLE_CTIME_R
  53. #define asctime_r _incompatible_asctime_r
  54. #define ctime_r _incompatible_ctime_r
  55. #endif /* HAVE_INCOMPATIBLE_CTIME_R */
  56. /*
  57. ** Nested includes
  58. */
  59. #include "sys/types.h" /* for time_t */
  60. #include "stdio.h"
  61. #include "errno.h"
  62. #include "string.h"
  63. #include "limits.h" /* for CHAR_BIT et al. */
  64. #include "time.h"
  65. #include "stdlib.h"
  66. #if HAVE_GETTEXT
  67. #include "libintl.h"
  68. #endif /* HAVE_GETTEXT */
  69. #if HAVE_SYS_WAIT_H
  70. #include <sys/wait.h> /* for WIFEXITED and WEXITSTATUS */
  71. #endif /* HAVE_SYS_WAIT_H */
  72. #ifndef WIFEXITED
  73. #define WIFEXITED(status) (((status) & 0xff) == 0)
  74. #endif /* !defined WIFEXITED */
  75. #ifndef WEXITSTATUS
  76. #define WEXITSTATUS(status) (((status) >> 8) & 0xff)
  77. #endif /* !defined WEXITSTATUS */
  78. #if HAVE_UNISTD_H
  79. #include "unistd.h" /* for F_OK, R_OK, and other POSIX goodness */
  80. #endif /* HAVE_UNISTD_H */
  81. #ifndef F_OK
  82. #define F_OK 0
  83. #endif /* !defined F_OK */
  84. #ifndef R_OK
  85. #define R_OK 4
  86. #endif /* !defined R_OK */
  87. /* Unlike <ctype.h>'s isdigit, this also works if c < 0 | c > UCHAR_MAX. */
  88. #define is_digit(c) ((unsigned)(c) - '0' <= 9)
  89. /*
  90. ** Define HAVE_STDINT_H's default value here, rather than at the
  91. ** start, since __GLIBC__'s value depends on previously-included
  92. ** files.
  93. ** (glibc 2.1 and later have stdint.h, even with pre-C99 compilers.)
  94. */
  95. #ifndef HAVE_STDINT_H
  96. #define HAVE_STDINT_H \
  97. (199901 <= __STDC_VERSION__ || \
  98. 2 < (__GLIBC__ + (0 < __GLIBC_MINOR__)))
  99. #endif /* !defined HAVE_STDINT_H */
  100. #if HAVE_STDINT_H
  101. #include "stdint.h"
  102. #endif /* !HAVE_STDINT_H */
  103. #ifndef HAVE_INTTYPES_H
  104. # define HAVE_INTTYPES_H HAVE_STDINT_H
  105. #endif
  106. #if HAVE_INTTYPES_H
  107. # include <inttypes.h>
  108. #endif
  109. #ifndef INT_FAST64_MAX
  110. /* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX. */
  111. #if defined LLONG_MAX || defined __LONG_LONG_MAX__
  112. typedef long long int_fast64_t;
  113. # ifdef LLONG_MAX
  114. # define INT_FAST64_MIN LLONG_MIN
  115. # define INT_FAST64_MAX LLONG_MAX
  116. # else
  117. # define INT_FAST64_MIN __LONG_LONG_MIN__
  118. # define INT_FAST64_MAX __LONG_LONG_MAX__
  119. # endif
  120. # define SCNdFAST64 "lld"
  121. #else /* ! (defined LLONG_MAX || defined __LONG_LONG_MAX__) */
  122. #if (LONG_MAX >> 31) < 0xffffffff
  123. Please use a compiler that supports a 64-bit integer type (or wider);
  124. you may need to compile with "-DHAVE_STDINT_H".
  125. #endif /* (LONG_MAX >> 31) < 0xffffffff */
  126. typedef long int_fast64_t;
  127. # define INT_FAST64_MIN LONG_MIN
  128. # define INT_FAST64_MAX LONG_MAX
  129. # define SCNdFAST64 "ld"
  130. #endif /* ! (defined LLONG_MAX || defined __LONG_LONG_MAX__) */
  131. #endif /* !defined INT_FAST64_MAX */
  132. #ifndef INT_FAST32_MAX
  133. # if INT_MAX >> 31 == 0
  134. typedef long int_fast32_t;
  135. # else
  136. typedef int int_fast32_t;
  137. # endif
  138. #endif
  139. #ifndef INTMAX_MAX
  140. # if defined LLONG_MAX || defined __LONG_LONG_MAX__
  141. typedef long long intmax_t;
  142. # define strtoimax strtoll
  143. # define PRIdMAX "lld"
  144. # ifdef LLONG_MAX
  145. # define INTMAX_MAX LLONG_MAX
  146. # define INTMAX_MIN LLONG_MIN
  147. # else
  148. # define INTMAX_MAX __LONG_LONG_MAX__
  149. # define INTMAX_MIN __LONG_LONG_MIN__
  150. # endif
  151. # else
  152. typedef long intmax_t;
  153. # define strtoimax strtol
  154. # define PRIdMAX "ld"
  155. # define INTMAX_MAX LONG_MAX
  156. # define INTMAX_MIN LONG_MIN
  157. # endif
  158. #endif
  159. #ifndef UINTMAX_MAX
  160. # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
  161. typedef unsigned long long uintmax_t;
  162. # define PRIuMAX "llu"
  163. # else
  164. typedef unsigned long uintmax_t;
  165. # define PRIuMAX "lu"
  166. # endif
  167. #endif
  168. #ifndef INT32_MAX
  169. #define INT32_MAX 0x7fffffff
  170. #endif /* !defined INT32_MAX */
  171. #ifndef INT32_MIN
  172. #define INT32_MIN (-1 - INT32_MAX)
  173. #endif /* !defined INT32_MIN */
  174. #ifndef SIZE_MAX
  175. #define SIZE_MAX ((size_t) -1)
  176. #endif
  177. #if 2 < __GNUC__ + (96 <= __GNUC_MINOR__)
  178. # define ATTRIBUTE_CONST __attribute__ ((const))
  179. # define ATTRIBUTE_PURE __attribute__ ((__pure__))
  180. # define ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
  181. #else
  182. # define ATTRIBUTE_CONST /* empty */
  183. # define ATTRIBUTE_PURE /* empty */
  184. # define ATTRIBUTE_FORMAT(spec) /* empty */
  185. #endif
  186. #if !defined _Noreturn && __STDC_VERSION__ < 201112
  187. # if 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
  188. # define _Noreturn __attribute__ ((__noreturn__))
  189. # else
  190. # define _Noreturn
  191. # endif
  192. #endif
  193. #if __STDC_VERSION__ < 199901 && !defined restrict
  194. # define restrict /* empty */
  195. #endif
  196. /*
  197. ** Workarounds for compilers/systems.
  198. */
  199. /*
  200. ** Some time.h implementations don't declare asctime_r.
  201. ** Others might define it as a macro.
  202. ** Fix the former without affecting the latter.
  203. */
  204. #ifndef asctime_r
  205. extern char * asctime_r(struct tm const *, char *);
  206. #endif
  207. /*
  208. ** Compile with -Dtime_tz=T to build the tz package with a private
  209. ** time_t type equivalent to T rather than the system-supplied time_t.
  210. ** This debugging feature can test unusual design decisions
  211. ** (e.g., time_t wider than 'long', or unsigned time_t) even on
  212. ** typical platforms.
  213. */
  214. #ifdef time_tz
  215. static time_t sys_time(time_t *x) { return time(x); }
  216. # undef ctime
  217. # define ctime tz_ctime
  218. # undef ctime_r
  219. # define ctime_r tz_ctime_r
  220. # undef difftime
  221. # define difftime tz_difftime
  222. # undef gmtime
  223. # define gmtime tz_gmtime
  224. # undef gmtime_r
  225. # define gmtime_r tz_gmtime_r
  226. # undef localtime
  227. # define localtime tz_localtime
  228. # undef localtime_r
  229. # define localtime_r tz_localtime_r
  230. # undef mktime
  231. # define mktime tz_mktime
  232. # undef time
  233. # define time tz_time
  234. # undef time_t
  235. # define time_t tz_time_t
  236. typedef time_tz time_t;
  237. char *ctime(time_t const *);
  238. char *ctime_r(time_t const *, char *);
  239. double difftime(time_t, time_t);
  240. struct tm *gmtime(time_t const *);
  241. struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
  242. struct tm *localtime(time_t const *);
  243. struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
  244. time_t mktime(struct tm *);
  245. static time_t
  246. time(time_t *p)
  247. {
  248. time_t r = sys_time(0);
  249. if (p)
  250. *p = r;
  251. return r;
  252. }
  253. #endif
  254. /*
  255. ** Private function declarations.
  256. */
  257. char * icatalloc(char * old, const char * new);
  258. char * icpyalloc(const char * string);
  259. const char * scheck(const char * string, const char * format);
  260. /*
  261. ** Finally, some convenience items.
  262. */
  263. #ifndef TRUE
  264. #define TRUE 1
  265. #endif /* !defined TRUE */
  266. #ifndef FALSE
  267. #define FALSE 0
  268. #endif /* !defined FALSE */
  269. #ifndef TYPE_BIT
  270. #define TYPE_BIT(type) (sizeof (type) * CHAR_BIT)
  271. #endif /* !defined TYPE_BIT */
  272. #ifndef TYPE_SIGNED
  273. #define TYPE_SIGNED(type) (((type) -1) < 0)
  274. #endif /* !defined TYPE_SIGNED */
  275. /* The minimum and maximum finite time values. */
  276. static time_t const time_t_min =
  277. (TYPE_SIGNED(time_t)
  278. ? (time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1)
  279. : 0);
  280. static time_t const time_t_max =
  281. (TYPE_SIGNED(time_t)
  282. ? - (~ 0 < 0) - ((time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1))
  283. : -1);
  284. #ifndef INT_STRLEN_MAXIMUM
  285. /*
  286. ** 302 / 1000 is log10(2.0) rounded up.
  287. ** Subtract one for the sign bit if the type is signed;
  288. ** add one for integer division truncation;
  289. ** add one more for a minus sign if the type is signed.
  290. */
  291. #define INT_STRLEN_MAXIMUM(type) \
  292. ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
  293. 1 + TYPE_SIGNED(type))
  294. #endif /* !defined INT_STRLEN_MAXIMUM */
  295. /*
  296. ** INITIALIZE(x)
  297. */
  298. #ifndef GNUC_or_lint
  299. #ifdef lint
  300. #define GNUC_or_lint
  301. #endif /* defined lint */
  302. #ifndef lint
  303. #ifdef __GNUC__
  304. #define GNUC_or_lint
  305. #endif /* defined __GNUC__ */
  306. #endif /* !defined lint */
  307. #endif /* !defined GNUC_or_lint */
  308. #ifndef INITIALIZE
  309. #ifdef GNUC_or_lint
  310. #define INITIALIZE(x) ((x) = 0)
  311. #endif /* defined GNUC_or_lint */
  312. #ifndef GNUC_or_lint
  313. #define INITIALIZE(x)
  314. #endif /* !defined GNUC_or_lint */
  315. #endif /* !defined INITIALIZE */
  316. /*
  317. ** For the benefit of GNU folk...
  318. ** `_(MSGID)' uses the current locale's message library string for MSGID.
  319. ** The default is to use gettext if available, and use MSGID otherwise.
  320. */
  321. #ifndef _
  322. #if HAVE_GETTEXT
  323. #define _(msgid) gettext(msgid)
  324. #else /* !HAVE_GETTEXT */
  325. #define _(msgid) msgid
  326. #endif /* !HAVE_GETTEXT */
  327. #endif /* !defined _ */
  328. #ifndef TZ_DOMAIN
  329. #define TZ_DOMAIN "tz"
  330. #endif /* !defined TZ_DOMAIN */
  331. #if HAVE_INCOMPATIBLE_CTIME_R
  332. #undef asctime_r
  333. #undef ctime_r
  334. char *asctime_r(struct tm const *, char *);
  335. char *ctime_r(time_t const *, char *);
  336. #endif /* HAVE_INCOMPATIBLE_CTIME_R */
  337. #ifndef YEARSPERREPEAT
  338. #define YEARSPERREPEAT 400 /* years before a Gregorian repeat */
  339. #endif /* !defined YEARSPERREPEAT */
  340. /*
  341. ** The Gregorian year averages 365.2425 days, which is 31556952 seconds.
  342. */
  343. #ifndef AVGSECSPERYEAR
  344. #define AVGSECSPERYEAR 31556952L
  345. #endif /* !defined AVGSECSPERYEAR */
  346. #ifndef SECSPERREPEAT
  347. #define SECSPERREPEAT ((int_fast64_t) YEARSPERREPEAT * (int_fast64_t) AVGSECSPERYEAR)
  348. #endif /* !defined SECSPERREPEAT */
  349. #ifndef SECSPERREPEAT_BITS
  350. #define SECSPERREPEAT_BITS 34 /* ceil(log2(SECSPERREPEAT)) */
  351. #endif /* !defined SECSPERREPEAT_BITS */
  352. /*
  353. ** UNIX was a registered trademark of The Open Group in 2003.
  354. */
  355. #endif /* !defined PRIVATE_H */