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