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