blob: 3a1930571db5e178ab27833cb549d2fd83ec567c [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
37#ifndef HAVE_SETTIMEOFDAY
Elliott Hughesce4783c2013-07-12 17:31:11 -070038#define HAVE_SETTIMEOFDAY 3
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080039#endif /* !defined HAVE_SETTIMEOFDAY */
40
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041#ifndef HAVE_SYMLINK
Elliott Hughesce4783c2013-07-12 17:31:11 -070042#define HAVE_SYMLINK 1
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080043#endif /* !defined HAVE_SYMLINK */
44
45#ifndef HAVE_SYS_STAT_H
Elliott Hughesce4783c2013-07-12 17:31:11 -070046#define HAVE_SYS_STAT_H 1
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080047#endif /* !defined HAVE_SYS_STAT_H */
48
49#ifndef HAVE_SYS_WAIT_H
Elliott Hughesce4783c2013-07-12 17:31:11 -070050#define HAVE_SYS_WAIT_H 1
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080051#endif /* !defined HAVE_SYS_WAIT_H */
52
53#ifndef HAVE_UNISTD_H
Elliott Hughesce4783c2013-07-12 17:31:11 -070054#define HAVE_UNISTD_H 1
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080055#endif /* !defined HAVE_UNISTD_H */
56
57#ifndef HAVE_UTMPX_H
Elliott Hughesce4783c2013-07-12 17:31:11 -070058#define HAVE_UTMPX_H 0
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059#endif /* !defined HAVE_UTMPX_H */
60
Elliott Hughesce4783c2013-07-12 17:31:11 -070061#ifndef LOCALE_HOME
62#define LOCALE_HOME "/usr/lib/locale"
63#endif /* !defined LOCALE_HOME */
64
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080065#if HAVE_INCOMPATIBLE_CTIME_R
66#define asctime_r _incompatible_asctime_r
67#define ctime_r _incompatible_ctime_r
68#endif /* HAVE_INCOMPATIBLE_CTIME_R */
69
70/*
71** Nested includes
72*/
73
Elliott Hughesce4783c2013-07-12 17:31:11 -070074#include "sys/types.h" /* for time_t */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080075#include "stdio.h"
76#include "errno.h"
77#include "string.h"
Elliott Hughesce4783c2013-07-12 17:31:11 -070078#include "limits.h" /* for CHAR_BIT et al. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079#include "time.h"
80#include "stdlib.h"
81
82#if HAVE_GETTEXT
83#include "libintl.h"
84#endif /* HAVE_GETTEXT */
85
86#if HAVE_SYS_WAIT_H
Elliott Hughesce4783c2013-07-12 17:31:11 -070087#include <sys/wait.h> /* for WIFEXITED and WEXITSTATUS */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080088#endif /* HAVE_SYS_WAIT_H */
89
90#ifndef WIFEXITED
Elliott Hughesce4783c2013-07-12 17:31:11 -070091#define WIFEXITED(status) (((status) & 0xff) == 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080092#endif /* !defined WIFEXITED */
93#ifndef WEXITSTATUS
Elliott Hughesce4783c2013-07-12 17:31:11 -070094#define WEXITSTATUS(status) (((status) >> 8) & 0xff)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095#endif /* !defined WEXITSTATUS */
96
97#if HAVE_UNISTD_H
Elliott Hughesce4783c2013-07-12 17:31:11 -070098#include "unistd.h" /* for F_OK, R_OK, and other POSIX goodness */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080099#endif /* HAVE_UNISTD_H */
100
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800101#ifndef F_OK
Elliott Hughesce4783c2013-07-12 17:31:11 -0700102#define F_OK 0
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800103#endif /* !defined F_OK */
104#ifndef R_OK
Elliott Hughesce4783c2013-07-12 17:31:11 -0700105#define R_OK 4
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800106#endif /* !defined R_OK */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800107
108/* Unlike <ctype.h>'s isdigit, this also works if c < 0 | c > UCHAR_MAX. */
109#define is_digit(c) ((unsigned)(c) - '0' <= 9)
110
111/*
112** Define HAVE_STDINT_H's default value here, rather than at the
113** start, since __GLIBC__'s value depends on previously-included
114** files.
115** (glibc 2.1 and later have stdint.h, even with pre-C99 compilers.)
116*/
117#ifndef HAVE_STDINT_H
118#define HAVE_STDINT_H \
Elliott Hughesce4783c2013-07-12 17:31:11 -0700119 (199901 <= __STDC_VERSION__ || \
120 2 < (__GLIBC__ + (0 < __GLIBC_MINOR__)))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800121#endif /* !defined HAVE_STDINT_H */
122
123#if HAVE_STDINT_H
124#include "stdint.h"
125#endif /* !HAVE_STDINT_H */
126
Elliott Hughesce4783c2013-07-12 17:31:11 -0700127#ifndef HAVE_INTTYPES_H
128# define HAVE_INTTYPES_H HAVE_STDINT_H
129#endif
130#if HAVE_INTTYPES_H
131# include <inttypes.h>
132#endif
133
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800134#ifndef INT_FAST64_MAX
135/* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX. */
136#if defined LLONG_MAX || defined __LONG_LONG_MAX__
Elliott Hughesce4783c2013-07-12 17:31:11 -0700137typedef long long int_fast64_t;
138# ifdef LLONG_MAX
139# define INT_FAST64_MIN LLONG_MIN
140# define INT_FAST64_MAX LLONG_MAX
141# else
142# define INT_FAST64_MIN __LONG_LONG_MIN__
143# define INT_FAST64_MAX __LONG_LONG_MAX__
144# endif
145# define SCNdFAST64 "lld"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800146#else /* ! (defined LLONG_MAX || defined __LONG_LONG_MAX__) */
147#if (LONG_MAX >> 31) < 0xffffffff
148Please use a compiler that supports a 64-bit integer type (or wider);
149you may need to compile with "-DHAVE_STDINT_H".
150#endif /* (LONG_MAX >> 31) < 0xffffffff */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700151typedef long int_fast64_t;
152# define INT_FAST64_MIN LONG_MIN
153# define INT_FAST64_MAX LONG_MAX
154# define SCNdFAST64 "ld"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800155#endif /* ! (defined LLONG_MAX || defined __LONG_LONG_MAX__) */
156#endif /* !defined INT_FAST64_MAX */
157
Elliott Hughesce4783c2013-07-12 17:31:11 -0700158#ifndef INT_FAST32_MAX
159# if INT_MAX >> 31 == 0
160typedef long int_fast32_t;
161# else
162typedef int int_fast32_t;
163# endif
164#endif
165
166#ifndef INTMAX_MAX
167# if defined LLONG_MAX || defined __LONG_LONG_MAX__
168typedef long long intmax_t;
Elliott Hughese0d0b152013-09-27 00:04:30 -0700169# define strtoimax strtoll
Elliott Hughesce4783c2013-07-12 17:31:11 -0700170# define PRIdMAX "lld"
Elliott Hughese0d0b152013-09-27 00:04:30 -0700171# ifdef LLONG_MAX
172# define INTMAX_MAX LLONG_MAX
173# define INTMAX_MIN LLONG_MIN
174# else
175# define INTMAX_MAX __LONG_LONG_MAX__
176# define INTMAX_MIN __LONG_LONG_MIN__
177# endif
Elliott Hughesce4783c2013-07-12 17:31:11 -0700178# else
179typedef long intmax_t;
Elliott Hughese0d0b152013-09-27 00:04:30 -0700180# define strtoimax strtol
Elliott Hughesce4783c2013-07-12 17:31:11 -0700181# define PRIdMAX "ld"
Elliott Hughese0d0b152013-09-27 00:04:30 -0700182# define INTMAX_MAX LONG_MAX
183# define INTMAX_MIN LONG_MIN
Elliott Hughesce4783c2013-07-12 17:31:11 -0700184# endif
185#endif
186
187#ifndef UINTMAX_MAX
188# if defined ULLONG_MAX || defined __LONG_LONG_MAX__
189typedef unsigned long long uintmax_t;
190# define PRIuMAX "llu"
191# else
192typedef unsigned long uintmax_t;
193# define PRIuMAX "lu"
194# endif
195#endif
196
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800197#ifndef INT32_MAX
198#define INT32_MAX 0x7fffffff
199#endif /* !defined INT32_MAX */
200#ifndef INT32_MIN
201#define INT32_MIN (-1 - INT32_MAX)
202#endif /* !defined INT32_MIN */
203
Elliott Hughesce4783c2013-07-12 17:31:11 -0700204#if 2 < __GNUC__ + (96 <= __GNUC_MINOR__)
205# define ATTRIBUTE_CONST __attribute__ ((const))
206# define ATTRIBUTE_PURE __attribute__ ((__pure__))
Elliott Hughese0d0b152013-09-27 00:04:30 -0700207# define ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
Elliott Hughesce4783c2013-07-12 17:31:11 -0700208#else
209# define ATTRIBUTE_CONST /* empty */
210# define ATTRIBUTE_PURE /* empty */
Elliott Hughese0d0b152013-09-27 00:04:30 -0700211# define ATTRIBUTE_FORMAT(spec) /* empty */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700212#endif
213
214#if !defined _Noreturn && __STDC_VERSION__ < 201112
215# if 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
216# define _Noreturn __attribute__ ((__noreturn__))
217# else
218# define _Noreturn
219# endif
220#endif
221
222#if __STDC_VERSION__ < 199901 && !defined restrict
223# define restrict /* empty */
224#endif
225
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800226/*
227** Workarounds for compilers/systems.
228*/
229
230/*
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800231** Some time.h implementations don't declare asctime_r.
232** Others might define it as a macro.
233** Fix the former without affecting the latter.
234*/
235
236#ifndef asctime_r
Elliott Hughesce4783c2013-07-12 17:31:11 -0700237extern char * asctime_r(struct tm const *, char *);
238#endif
239
240/*
241** Compile with -Dtime_tz=T to build the tz package with a private
242** time_t type equivalent to T rather than the system-supplied time_t.
243** This debugging feature can test unusual design decisions
244** (e.g., time_t wider than 'long', or unsigned time_t) even on
245** typical platforms.
246*/
247#ifdef time_tz
248static time_t sys_time(time_t *x) { return time(x); }
249
250# undef ctime
251# define ctime tz_ctime
252# undef ctime_r
253# define ctime_r tz_ctime_r
254# undef difftime
255# define difftime tz_difftime
256# undef gmtime
257# define gmtime tz_gmtime
258# undef gmtime_r
259# define gmtime_r tz_gmtime_r
260# undef localtime
261# define localtime tz_localtime
262# undef localtime_r
263# define localtime_r tz_localtime_r
264# undef mktime
265# define mktime tz_mktime
266# undef time
267# define time tz_time
268# undef time_t
269# define time_t tz_time_t
270
271typedef time_tz time_t;
272
273char *ctime(time_t const *);
274char *ctime_r(time_t const *, char *);
275double difftime(time_t, time_t);
276struct tm *gmtime(time_t const *);
277struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
278struct tm *localtime(time_t const *);
279struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
280time_t mktime(struct tm *);
281
282static time_t
283time(time_t *p)
284{
285 time_t r = sys_time(0);
286 if (p)
287 *p = r;
288 return r;
289}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800290#endif
291
292/*
293** Private function declarations.
294*/
295
Elliott Hughesce4783c2013-07-12 17:31:11 -0700296char * icatalloc(char * old, const char * new);
297char * icpyalloc(const char * string);
298const char * scheck(const char * string, const char * format);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800299
300/*
301** Finally, some convenience items.
302*/
303
304#ifndef TRUE
Elliott Hughesce4783c2013-07-12 17:31:11 -0700305#define TRUE 1
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800306#endif /* !defined TRUE */
307
308#ifndef FALSE
Elliott Hughesce4783c2013-07-12 17:31:11 -0700309#define FALSE 0
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800310#endif /* !defined FALSE */
311
312#ifndef TYPE_BIT
Elliott Hughesce4783c2013-07-12 17:31:11 -0700313#define TYPE_BIT(type) (sizeof (type) * CHAR_BIT)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800314#endif /* !defined TYPE_BIT */
315
316#ifndef TYPE_SIGNED
317#define TYPE_SIGNED(type) (((type) -1) < 0)
318#endif /* !defined TYPE_SIGNED */
319
Elliott Hughes713fe642013-08-22 14:13:50 -0700320/* The minimum and maximum finite time values. */
321static time_t const time_t_min =
322 (TYPE_SIGNED(time_t)
323 ? (time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1)
324 : 0);
325static time_t const time_t_max =
326 (TYPE_SIGNED(time_t)
327 ? - (~ 0 < 0) - ((time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1))
328 : -1);
329
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800330#ifndef INT_STRLEN_MAXIMUM
331/*
332** 302 / 1000 is log10(2.0) rounded up.
333** Subtract one for the sign bit if the type is signed;
334** add one for integer division truncation;
335** add one more for a minus sign if the type is signed.
336*/
337#define INT_STRLEN_MAXIMUM(type) \
Elliott Hughesce4783c2013-07-12 17:31:11 -0700338 ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
339 1 + TYPE_SIGNED(type))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800340#endif /* !defined INT_STRLEN_MAXIMUM */
341
342/*
343** INITIALIZE(x)
344*/
345
346#ifndef GNUC_or_lint
347#ifdef lint
348#define GNUC_or_lint
349#endif /* defined lint */
350#ifndef lint
351#ifdef __GNUC__
352#define GNUC_or_lint
353#endif /* defined __GNUC__ */
354#endif /* !defined lint */
355#endif /* !defined GNUC_or_lint */
356
357#ifndef INITIALIZE
358#ifdef GNUC_or_lint
Elliott Hughesce4783c2013-07-12 17:31:11 -0700359#define INITIALIZE(x) ((x) = 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800360#endif /* defined GNUC_or_lint */
361#ifndef GNUC_or_lint
362#define INITIALIZE(x)
363#endif /* !defined GNUC_or_lint */
364#endif /* !defined INITIALIZE */
365
366/*
367** For the benefit of GNU folk...
368** `_(MSGID)' uses the current locale's message library string for MSGID.
369** The default is to use gettext if available, and use MSGID otherwise.
370*/
371
372#ifndef _
373#if HAVE_GETTEXT
374#define _(msgid) gettext(msgid)
375#else /* !HAVE_GETTEXT */
376#define _(msgid) msgid
377#endif /* !HAVE_GETTEXT */
378#endif /* !defined _ */
379
380#ifndef TZ_DOMAIN
381#define TZ_DOMAIN "tz"
382#endif /* !defined TZ_DOMAIN */
383
384#if HAVE_INCOMPATIBLE_CTIME_R
385#undef asctime_r
386#undef ctime_r
Elliott Hughesce4783c2013-07-12 17:31:11 -0700387char *asctime_r(struct tm const *, char *);
388char *ctime_r(time_t const *, char *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800389#endif /* HAVE_INCOMPATIBLE_CTIME_R */
390
391#ifndef YEARSPERREPEAT
Elliott Hughesce4783c2013-07-12 17:31:11 -0700392#define YEARSPERREPEAT 400 /* years before a Gregorian repeat */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800393#endif /* !defined YEARSPERREPEAT */
394
395/*
396** The Gregorian year averages 365.2425 days, which is 31556952 seconds.
397*/
398
399#ifndef AVGSECSPERYEAR
Elliott Hughesce4783c2013-07-12 17:31:11 -0700400#define AVGSECSPERYEAR 31556952L
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800401#endif /* !defined AVGSECSPERYEAR */
402
403#ifndef SECSPERREPEAT
Elliott Hughesce4783c2013-07-12 17:31:11 -0700404#define SECSPERREPEAT ((int_fast64_t) YEARSPERREPEAT * (int_fast64_t) AVGSECSPERYEAR)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800405#endif /* !defined SECSPERREPEAT */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700406
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800407#ifndef SECSPERREPEAT_BITS
Elliott Hughesce4783c2013-07-12 17:31:11 -0700408#define SECSPERREPEAT_BITS 34 /* ceil(log2(SECSPERREPEAT)) */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800409#endif /* !defined SECSPERREPEAT_BITS */
410
411/*
412** UNIX was a registered trademark of The Open Group in 2003.
413*/
414
415#endif /* !defined PRIVATE_H */