blob: 4eb0ab62215bf39b52119fc6934466388ffe6471 [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.
22** You can override these in your C compiler options, e.g. `-DHAVE_ADJTIME=0'.
23*/
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 Hughesce4783c2013-07-12 17:31:11 -070065#ifndef LOCALE_HOME
66#define LOCALE_HOME "/usr/lib/locale"
67#endif /* !defined LOCALE_HOME */
68
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080069#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
Elliott Hughesce4783c2013-07-12 17:31:11 -070078#include "sys/types.h" /* for time_t */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079#include "stdio.h"
80#include "errno.h"
81#include "string.h"
Elliott Hughesce4783c2013-07-12 17:31:11 -070082#include "limits.h" /* for CHAR_BIT et al. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083#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
Elliott Hughesce4783c2013-07-12 17:31:11 -070091#include <sys/wait.h> /* for WIFEXITED and WEXITSTATUS */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080092#endif /* HAVE_SYS_WAIT_H */
93
94#ifndef WIFEXITED
Elliott Hughesce4783c2013-07-12 17:31:11 -070095#define WIFEXITED(status) (((status) & 0xff) == 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080096#endif /* !defined WIFEXITED */
97#ifndef WEXITSTATUS
Elliott Hughesce4783c2013-07-12 17:31:11 -070098#define WEXITSTATUS(status) (((status) >> 8) & 0xff)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080099#endif /* !defined WEXITSTATUS */
100
101#if HAVE_UNISTD_H
Elliott Hughesce4783c2013-07-12 17:31:11 -0700102#include "unistd.h" /* for F_OK, R_OK, and other POSIX goodness */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800103#endif /* HAVE_UNISTD_H */
104
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800105#ifndef F_OK
Elliott Hughesce4783c2013-07-12 17:31:11 -0700106#define F_OK 0
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800107#endif /* !defined F_OK */
108#ifndef R_OK
Elliott Hughesce4783c2013-07-12 17:31:11 -0700109#define R_OK 4
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800110#endif /* !defined R_OK */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800111
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 \
Elliott Hughesce4783c2013-07-12 17:31:11 -0700123 (199901 <= __STDC_VERSION__ || \
124 2 < (__GLIBC__ + (0 < __GLIBC_MINOR__)))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800125#endif /* !defined HAVE_STDINT_H */
126
127#if HAVE_STDINT_H
128#include "stdint.h"
129#endif /* !HAVE_STDINT_H */
130
Elliott Hughesce4783c2013-07-12 17:31:11 -0700131#ifndef HAVE_INTTYPES_H
132# define HAVE_INTTYPES_H HAVE_STDINT_H
133#endif
134#if HAVE_INTTYPES_H
135# include <inttypes.h>
136#endif
137
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800138#ifndef INT_FAST64_MAX
139/* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX. */
140#if defined LLONG_MAX || defined __LONG_LONG_MAX__
Elliott Hughesce4783c2013-07-12 17:31:11 -0700141typedef long long int_fast64_t;
142# ifdef LLONG_MAX
143# define INT_FAST64_MIN LLONG_MIN
144# define INT_FAST64_MAX LLONG_MAX
145# else
146# define INT_FAST64_MIN __LONG_LONG_MIN__
147# define INT_FAST64_MAX __LONG_LONG_MAX__
148# endif
149# define SCNdFAST64 "lld"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800150#else /* ! (defined LLONG_MAX || defined __LONG_LONG_MAX__) */
151#if (LONG_MAX >> 31) < 0xffffffff
152Please use a compiler that supports a 64-bit integer type (or wider);
153you may need to compile with "-DHAVE_STDINT_H".
154#endif /* (LONG_MAX >> 31) < 0xffffffff */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700155typedef long int_fast64_t;
156# define INT_FAST64_MIN LONG_MIN
157# define INT_FAST64_MAX LONG_MAX
158# define SCNdFAST64 "ld"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800159#endif /* ! (defined LLONG_MAX || defined __LONG_LONG_MAX__) */
160#endif /* !defined INT_FAST64_MAX */
161
Elliott Hughesce4783c2013-07-12 17:31:11 -0700162#ifndef INT_FAST32_MAX
163# if INT_MAX >> 31 == 0
164typedef long int_fast32_t;
165# else
166typedef int int_fast32_t;
167# endif
168#endif
169
170#ifndef INTMAX_MAX
171# if defined LLONG_MAX || defined __LONG_LONG_MAX__
172typedef long long intmax_t;
Elliott Hughese0d0b152013-09-27 00:04:30 -0700173# define strtoimax strtoll
Elliott Hughesce4783c2013-07-12 17:31:11 -0700174# define PRIdMAX "lld"
Elliott Hughese0d0b152013-09-27 00:04:30 -0700175# ifdef LLONG_MAX
176# define INTMAX_MAX LLONG_MAX
177# define INTMAX_MIN LLONG_MIN
178# else
179# define INTMAX_MAX __LONG_LONG_MAX__
180# define INTMAX_MIN __LONG_LONG_MIN__
181# endif
Elliott Hughesce4783c2013-07-12 17:31:11 -0700182# else
183typedef long intmax_t;
Elliott Hughese0d0b152013-09-27 00:04:30 -0700184# define strtoimax strtol
Elliott Hughesce4783c2013-07-12 17:31:11 -0700185# define PRIdMAX "ld"
Elliott Hughese0d0b152013-09-27 00:04:30 -0700186# define INTMAX_MAX LONG_MAX
187# define INTMAX_MIN LONG_MIN
Elliott Hughesce4783c2013-07-12 17:31:11 -0700188# endif
189#endif
190
191#ifndef UINTMAX_MAX
192# if defined ULLONG_MAX || defined __LONG_LONG_MAX__
193typedef unsigned long long uintmax_t;
194# define PRIuMAX "llu"
195# else
196typedef unsigned long uintmax_t;
197# define PRIuMAX "lu"
198# endif
199#endif
200
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800201#ifndef INT32_MAX
202#define INT32_MAX 0x7fffffff
203#endif /* !defined INT32_MAX */
204#ifndef INT32_MIN
205#define INT32_MIN (-1 - INT32_MAX)
206#endif /* !defined INT32_MIN */
207
Elliott Hughesce4783c2013-07-12 17:31:11 -0700208#if 2 < __GNUC__ + (96 <= __GNUC_MINOR__)
209# define ATTRIBUTE_CONST __attribute__ ((const))
210# define ATTRIBUTE_PURE __attribute__ ((__pure__))
Elliott Hughese0d0b152013-09-27 00:04:30 -0700211# define ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
Elliott Hughesce4783c2013-07-12 17:31:11 -0700212#else
213# define ATTRIBUTE_CONST /* empty */
214# define ATTRIBUTE_PURE /* empty */
Elliott Hughese0d0b152013-09-27 00:04:30 -0700215# define ATTRIBUTE_FORMAT(spec) /* empty */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700216#endif
217
218#if !defined _Noreturn && __STDC_VERSION__ < 201112
219# if 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
220# define _Noreturn __attribute__ ((__noreturn__))
221# else
222# define _Noreturn
223# endif
224#endif
225
226#if __STDC_VERSION__ < 199901 && !defined restrict
227# define restrict /* empty */
228#endif
229
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800230/*
231** Workarounds for compilers/systems.
232*/
233
234/*
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800235** Some time.h implementations don't declare asctime_r.
236** Others might define it as a macro.
237** Fix the former without affecting the latter.
238*/
239
240#ifndef asctime_r
Elliott Hughesce4783c2013-07-12 17:31:11 -0700241extern char * asctime_r(struct tm const *, char *);
242#endif
243
244/*
245** Compile with -Dtime_tz=T to build the tz package with a private
246** time_t type equivalent to T rather than the system-supplied time_t.
247** This debugging feature can test unusual design decisions
248** (e.g., time_t wider than 'long', or unsigned time_t) even on
249** typical platforms.
250*/
251#ifdef time_tz
252static time_t sys_time(time_t *x) { return time(x); }
253
254# undef ctime
255# define ctime tz_ctime
256# undef ctime_r
257# define ctime_r tz_ctime_r
258# undef difftime
259# define difftime tz_difftime
260# undef gmtime
261# define gmtime tz_gmtime
262# undef gmtime_r
263# define gmtime_r tz_gmtime_r
264# undef localtime
265# define localtime tz_localtime
266# undef localtime_r
267# define localtime_r tz_localtime_r
268# undef mktime
269# define mktime tz_mktime
270# undef time
271# define time tz_time
272# undef time_t
273# define time_t tz_time_t
274
275typedef time_tz time_t;
276
277char *ctime(time_t const *);
278char *ctime_r(time_t const *, char *);
279double difftime(time_t, time_t);
280struct tm *gmtime(time_t const *);
281struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
282struct tm *localtime(time_t const *);
283struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
284time_t mktime(struct tm *);
285
286static time_t
287time(time_t *p)
288{
289 time_t r = sys_time(0);
290 if (p)
291 *p = r;
292 return r;
293}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800294#endif
295
296/*
297** Private function declarations.
298*/
299
Elliott Hughesce4783c2013-07-12 17:31:11 -0700300char * icatalloc(char * old, const char * new);
301char * icpyalloc(const char * string);
302const char * scheck(const char * string, const char * format);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800303
304/*
305** Finally, some convenience items.
306*/
307
308#ifndef TRUE
Elliott Hughesce4783c2013-07-12 17:31:11 -0700309#define TRUE 1
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800310#endif /* !defined TRUE */
311
312#ifndef FALSE
Elliott Hughesce4783c2013-07-12 17:31:11 -0700313#define FALSE 0
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800314#endif /* !defined FALSE */
315
316#ifndef TYPE_BIT
Elliott Hughesce4783c2013-07-12 17:31:11 -0700317#define TYPE_BIT(type) (sizeof (type) * CHAR_BIT)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800318#endif /* !defined TYPE_BIT */
319
320#ifndef TYPE_SIGNED
321#define TYPE_SIGNED(type) (((type) -1) < 0)
322#endif /* !defined TYPE_SIGNED */
323
Elliott Hughes713fe642013-08-22 14:13:50 -0700324/* The minimum and maximum finite time values. */
325static time_t const time_t_min =
326 (TYPE_SIGNED(time_t)
327 ? (time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1)
328 : 0);
329static time_t const time_t_max =
330 (TYPE_SIGNED(time_t)
331 ? - (~ 0 < 0) - ((time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1))
332 : -1);
333
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800334#ifndef INT_STRLEN_MAXIMUM
335/*
336** 302 / 1000 is log10(2.0) rounded up.
337** Subtract one for the sign bit if the type is signed;
338** add one for integer division truncation;
339** add one more for a minus sign if the type is signed.
340*/
341#define INT_STRLEN_MAXIMUM(type) \
Elliott Hughesce4783c2013-07-12 17:31:11 -0700342 ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
343 1 + TYPE_SIGNED(type))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800344#endif /* !defined INT_STRLEN_MAXIMUM */
345
346/*
347** INITIALIZE(x)
348*/
349
350#ifndef GNUC_or_lint
351#ifdef lint
352#define GNUC_or_lint
353#endif /* defined lint */
354#ifndef lint
355#ifdef __GNUC__
356#define GNUC_or_lint
357#endif /* defined __GNUC__ */
358#endif /* !defined lint */
359#endif /* !defined GNUC_or_lint */
360
361#ifndef INITIALIZE
362#ifdef GNUC_or_lint
Elliott Hughesce4783c2013-07-12 17:31:11 -0700363#define INITIALIZE(x) ((x) = 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800364#endif /* defined GNUC_or_lint */
365#ifndef GNUC_or_lint
366#define INITIALIZE(x)
367#endif /* !defined GNUC_or_lint */
368#endif /* !defined INITIALIZE */
369
370/*
371** For the benefit of GNU folk...
372** `_(MSGID)' uses the current locale's message library string for MSGID.
373** The default is to use gettext if available, and use MSGID otherwise.
374*/
375
376#ifndef _
377#if HAVE_GETTEXT
378#define _(msgid) gettext(msgid)
379#else /* !HAVE_GETTEXT */
380#define _(msgid) msgid
381#endif /* !HAVE_GETTEXT */
382#endif /* !defined _ */
383
384#ifndef TZ_DOMAIN
385#define TZ_DOMAIN "tz"
386#endif /* !defined TZ_DOMAIN */
387
388#if HAVE_INCOMPATIBLE_CTIME_R
389#undef asctime_r
390#undef ctime_r
Elliott Hughesce4783c2013-07-12 17:31:11 -0700391char *asctime_r(struct tm const *, char *);
392char *ctime_r(time_t const *, char *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800393#endif /* HAVE_INCOMPATIBLE_CTIME_R */
394
395#ifndef YEARSPERREPEAT
Elliott Hughesce4783c2013-07-12 17:31:11 -0700396#define YEARSPERREPEAT 400 /* years before a Gregorian repeat */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800397#endif /* !defined YEARSPERREPEAT */
398
399/*
400** The Gregorian year averages 365.2425 days, which is 31556952 seconds.
401*/
402
403#ifndef AVGSECSPERYEAR
Elliott Hughesce4783c2013-07-12 17:31:11 -0700404#define AVGSECSPERYEAR 31556952L
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800405#endif /* !defined AVGSECSPERYEAR */
406
407#ifndef SECSPERREPEAT
Elliott Hughesce4783c2013-07-12 17:31:11 -0700408#define SECSPERREPEAT ((int_fast64_t) YEARSPERREPEAT * (int_fast64_t) AVGSECSPERYEAR)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800409#endif /* !defined SECSPERREPEAT */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700410
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800411#ifndef SECSPERREPEAT_BITS
Elliott Hughesce4783c2013-07-12 17:31:11 -0700412#define SECSPERREPEAT_BITS 34 /* ceil(log2(SECSPERREPEAT)) */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800413#endif /* !defined SECSPERREPEAT_BITS */
414
415/*
416** UNIX was a registered trademark of The Open Group in 2003.
417*/
418
419#endif /* !defined PRIVATE_H */