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