blob: c30c71101dbfb00b4fc107d2d0f11a34adf40a7b [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001#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
Elliott Hughesce4783c2013-07-12 17:31:11 -070018#define GRANDPARENTED "Local time zone must be set--see zic manual page"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080019
20/*
21** Defaults for preprocessor symbols.
Elliott Hughes5f564542014-06-19 13:54:10 -070022** You can override these in your C compiler options, e.g. '-DHAVE_ADJTIME=0'.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080023*/
24
25#ifndef HAVE_ADJTIME
Elliott Hughesce4783c2013-07-12 17:31:11 -070026#define HAVE_ADJTIME 1
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080027#endif /* !defined HAVE_ADJTIME */
28
29#ifndef HAVE_GETTEXT
Elliott Hughesce4783c2013-07-12 17:31:11 -070030#define HAVE_GETTEXT 0
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080031#endif /* !defined HAVE_GETTEXT */
32
33#ifndef HAVE_INCOMPATIBLE_CTIME_R
Elliott Hughesce4783c2013-07-12 17:31:11 -070034#define HAVE_INCOMPATIBLE_CTIME_R 0
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080035#endif /* !defined INCOMPATIBLE_CTIME_R */
36
Calin Juravle627d37c2014-02-28 11:46:03 +000037#ifndef HAVE_LINK
38#define HAVE_LINK 1
39#endif /* !defined HAVE_LINK */
40
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041#ifndef HAVE_SETTIMEOFDAY
Elliott Hughesce4783c2013-07-12 17:31:11 -070042#define HAVE_SETTIMEOFDAY 3
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080043#endif /* !defined HAVE_SETTIMEOFDAY */
44
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080045#ifndef HAVE_SYMLINK
Elliott Hughesce4783c2013-07-12 17:31:11 -070046#define HAVE_SYMLINK 1
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080047#endif /* !defined HAVE_SYMLINK */
48
49#ifndef HAVE_SYS_STAT_H
Elliott Hughesce4783c2013-07-12 17:31:11 -070050#define HAVE_SYS_STAT_H 1
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080051#endif /* !defined HAVE_SYS_STAT_H */
52
53#ifndef HAVE_SYS_WAIT_H
Elliott Hughesce4783c2013-07-12 17:31:11 -070054#define HAVE_SYS_WAIT_H 1
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080055#endif /* !defined HAVE_SYS_WAIT_H */
56
57#ifndef HAVE_UNISTD_H
Elliott Hughesce4783c2013-07-12 17:31:11 -070058#define HAVE_UNISTD_H 1
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059#endif /* !defined HAVE_UNISTD_H */
60
61#ifndef HAVE_UTMPX_H
Elliott Hughesce4783c2013-07-12 17:31:11 -070062#define HAVE_UTMPX_H 0
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080063#endif /* !defined HAVE_UTMPX_H */
64
Elliott Hughes5f564542014-06-19 13:54:10 -070065#if !defined(__ANDROID__)
Elliott Hughesce4783c2013-07-12 17:31:11 -070066#ifndef LOCALE_HOME
67#define LOCALE_HOME "/usr/lib/locale"
68#endif /* !defined LOCALE_HOME */
Elliott Hughes5f564542014-06-19 13:54:10 -070069#endif // __ANDROID__
Elliott Hughesce4783c2013-07-12 17:31:11 -070070
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080071#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
Elliott Hughesce4783c2013-07-12 17:31:11 -070080#include "sys/types.h" /* for time_t */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080081#include "stdio.h"
82#include "errno.h"
83#include "string.h"
Elliott Hughesce4783c2013-07-12 17:31:11 -070084#include "limits.h" /* for CHAR_BIT et al. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080085#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
Elliott Hughesce4783c2013-07-12 17:31:11 -070093#include <sys/wait.h> /* for WIFEXITED and WEXITSTATUS */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080094#endif /* HAVE_SYS_WAIT_H */
95
96#ifndef WIFEXITED
Elliott Hughesce4783c2013-07-12 17:31:11 -070097#define WIFEXITED(status) (((status) & 0xff) == 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080098#endif /* !defined WIFEXITED */
99#ifndef WEXITSTATUS
Elliott Hughesce4783c2013-07-12 17:31:11 -0700100#define WEXITSTATUS(status) (((status) >> 8) & 0xff)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800101#endif /* !defined WEXITSTATUS */
102
103#if HAVE_UNISTD_H
Elliott Hughesce4783c2013-07-12 17:31:11 -0700104#include "unistd.h" /* for F_OK, R_OK, and other POSIX goodness */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800105#endif /* HAVE_UNISTD_H */
106
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800107#ifndef F_OK
Elliott Hughesce4783c2013-07-12 17:31:11 -0700108#define F_OK 0
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109#endif /* !defined F_OK */
110#ifndef R_OK
Elliott Hughesce4783c2013-07-12 17:31:11 -0700111#define R_OK 4
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800112#endif /* !defined R_OK */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800113
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 \
Elliott Hughes5f564542014-06-19 13:54:10 -0700125 (199901 <= __STDC_VERSION__ \
126 || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__) \
127 || __CYGWIN__)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800128#endif /* !defined HAVE_STDINT_H */
129
130#if HAVE_STDINT_H
131#include "stdint.h"
132#endif /* !HAVE_STDINT_H */
133
Elliott Hughesce4783c2013-07-12 17:31:11 -0700134#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
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800141#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__
Elliott Hughesce4783c2013-07-12 17:31:11 -0700144typedef 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"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800153#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 */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700158typedef long int_fast64_t;
159# define INT_FAST64_MIN LONG_MIN
160# define INT_FAST64_MAX LONG_MAX
161# define SCNdFAST64 "ld"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800162#endif /* ! (defined LLONG_MAX || defined __LONG_LONG_MAX__) */
163#endif /* !defined INT_FAST64_MAX */
164
Elliott Hughesce4783c2013-07-12 17:31:11 -0700165#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;
Elliott Hughese0d0b152013-09-27 00:04:30 -0700176# define strtoimax strtoll
Elliott Hughesce4783c2013-07-12 17:31:11 -0700177# define PRIdMAX "lld"
Elliott Hughese0d0b152013-09-27 00:04:30 -0700178# 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
Elliott Hughesce4783c2013-07-12 17:31:11 -0700185# else
186typedef long intmax_t;
Elliott Hughese0d0b152013-09-27 00:04:30 -0700187# define strtoimax strtol
Elliott Hughesce4783c2013-07-12 17:31:11 -0700188# define PRIdMAX "ld"
Elliott Hughese0d0b152013-09-27 00:04:30 -0700189# define INTMAX_MAX LONG_MAX
190# define INTMAX_MIN LONG_MIN
Elliott Hughesce4783c2013-07-12 17:31:11 -0700191# 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
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800204#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
Elliott Hughes5f564542014-06-19 13:54:10 -0700211#ifndef SIZE_MAX
212#define SIZE_MAX ((size_t) -1)
213#endif
214
Elliott Hughesce4783c2013-07-12 17:31:11 -0700215#if 2 < __GNUC__ + (96 <= __GNUC_MINOR__)
216# define ATTRIBUTE_CONST __attribute__ ((const))
217# define ATTRIBUTE_PURE __attribute__ ((__pure__))
Elliott Hughese0d0b152013-09-27 00:04:30 -0700218# define ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
Elliott Hughesce4783c2013-07-12 17:31:11 -0700219#else
220# define ATTRIBUTE_CONST /* empty */
221# define ATTRIBUTE_PURE /* empty */
Elliott Hughese0d0b152013-09-27 00:04:30 -0700222# define ATTRIBUTE_FORMAT(spec) /* empty */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700223#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
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800237/*
238** Workarounds for compilers/systems.
239*/
240
241/*
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800242** 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
Elliott Hughesce4783c2013-07-12 17:31:11 -0700248extern 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}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800301#endif
302
303/*
304** Private function declarations.
305*/
306
Elliott Hughesce4783c2013-07-12 17:31:11 -0700307char * icatalloc(char * old, const char * new);
308char * icpyalloc(const char * string);
309const char * scheck(const char * string, const char * format);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800310
311/*
312** Finally, some convenience items.
313*/
314
315#ifndef TRUE
Elliott Hughesce4783c2013-07-12 17:31:11 -0700316#define TRUE 1
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800317#endif /* !defined TRUE */
318
319#ifndef FALSE
Elliott Hughesce4783c2013-07-12 17:31:11 -0700320#define FALSE 0
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800321#endif /* !defined FALSE */
322
323#ifndef TYPE_BIT
Elliott Hughesce4783c2013-07-12 17:31:11 -0700324#define TYPE_BIT(type) (sizeof (type) * CHAR_BIT)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800325#endif /* !defined TYPE_BIT */
326
327#ifndef TYPE_SIGNED
328#define TYPE_SIGNED(type) (((type) -1) < 0)
329#endif /* !defined TYPE_SIGNED */
330
Elliott Hughes713fe642013-08-22 14:13:50 -0700331/* 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
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800341#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) \
Elliott Hughesce4783c2013-07-12 17:31:11 -0700349 ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
350 1 + TYPE_SIGNED(type))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800351#endif /* !defined INT_STRLEN_MAXIMUM */
352
353/*
354** INITIALIZE(x)
355*/
356
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800357#ifdef lint
Elliott Hughes5f564542014-06-19 13:54:10 -0700358# define INITIALIZE(x) ((x) = 0)
359#else
360# define INITIALIZE(x)
361#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800362
363/*
364** For the benefit of GNU folk...
Elliott Hughes5f564542014-06-19 13:54:10 -0700365** '_(MSGID)' uses the current locale's message library string for MSGID.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800366** 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
Elliott Hughes5f564542014-06-19 13:54:10 -0700377#if !defined TZ_DOMAIN && defined TZ_DOMAINDIR
378# define TZ_DOMAIN "tz"
379#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800380
381#if HAVE_INCOMPATIBLE_CTIME_R
382#undef asctime_r
383#undef ctime_r
Elliott Hughesce4783c2013-07-12 17:31:11 -0700384char *asctime_r(struct tm const *, char *);
385char *ctime_r(time_t const *, char *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800386#endif /* HAVE_INCOMPATIBLE_CTIME_R */
387
388#ifndef YEARSPERREPEAT
Elliott Hughesce4783c2013-07-12 17:31:11 -0700389#define YEARSPERREPEAT 400 /* years before a Gregorian repeat */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800390#endif /* !defined YEARSPERREPEAT */
391
392/*
393** The Gregorian year averages 365.2425 days, which is 31556952 seconds.
394*/
395
396#ifndef AVGSECSPERYEAR
Elliott Hughesce4783c2013-07-12 17:31:11 -0700397#define AVGSECSPERYEAR 31556952L
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800398#endif /* !defined AVGSECSPERYEAR */
399
400#ifndef SECSPERREPEAT
Elliott Hughesce4783c2013-07-12 17:31:11 -0700401#define SECSPERREPEAT ((int_fast64_t) YEARSPERREPEAT * (int_fast64_t) AVGSECSPERYEAR)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800402#endif /* !defined SECSPERREPEAT */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700403
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800404#ifndef SECSPERREPEAT_BITS
Elliott Hughesce4783c2013-07-12 17:31:11 -0700405#define SECSPERREPEAT_BITS 34 /* ceil(log2(SECSPERREPEAT)) */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800406#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 */