private.h revision 1.42 1 /* $NetBSD: private.h,v 1.42 2015/06/22 17:43:23 christos Exp $ */
2
3 #ifndef PRIVATE_H
4 #define PRIVATE_H
5
6 /* NetBSD defaults */
7 #define TM_GMTOFF tm_gmtoff
8 #define TM_ZONE tm_zone
9 #define STD_INSPIRED 1
10 #define HAVE_LONG_DOUBLE 1
11
12 /* For when we build zic as a host tool. */
13 #if HAVE_NBTOOL_CONFIG_H
14 #include "nbtool_config.h"
15 #endif
16
17 /*
18 ** This file is in the public domain, so clarified as of
19 ** 1996-06-05 by Arthur David Olson.
20 */
21
22 /*
23 ** This header is for use ONLY with the time conversion code.
24 ** There is no guarantee that it will remain unchanged,
25 ** or that it will remain at all.
26 ** Do NOT copy it to any system include directory.
27 ** Thank you!
28 */
29
30 #define GRANDPARENTED "Local time zone must be set--see zic manual page"
31
32 /*
33 ** Defaults for preprocessor symbols.
34 ** You can override these in your C compiler options, e.g. '-DHAVE_ADJTIME=0'.
35 */
36
37 #ifndef HAVE_ADJTIME
38 #define HAVE_ADJTIME 1
39 #endif /* !defined HAVE_ADJTIME */
40
41 #ifndef HAVE_GETTEXT
42 #define HAVE_GETTEXT 0
43 #endif /* !defined HAVE_GETTEXT */
44
45 #ifndef HAVE_INCOMPATIBLE_CTIME_R
46 #define HAVE_INCOMPATIBLE_CTIME_R 0
47 #endif /* !defined INCOMPATIBLE_CTIME_R */
48
49 #ifndef HAVE_LINK
50 #define HAVE_LINK 1
51 #endif /* !defined HAVE_LINK */
52
53 #ifndef HAVE_SETTIMEOFDAY
54 #define HAVE_SETTIMEOFDAY 3
55 #endif /* !defined HAVE_SETTIMEOFDAY */
56
57 #ifndef HAVE_STRDUP
58 #define HAVE_STRDUP 1
59 #endif
60
61 #ifndef HAVE_SYMLINK
62 #define HAVE_SYMLINK 1
63 #endif /* !defined HAVE_SYMLINK */
64
65 #ifndef HAVE_SYS_STAT_H
66 #define HAVE_SYS_STAT_H 1
67 #endif /* !defined HAVE_SYS_STAT_H */
68
69 #ifndef HAVE_SYS_WAIT_H
70 #define HAVE_SYS_WAIT_H 1
71 #endif /* !defined HAVE_SYS_WAIT_H */
72
73 #ifndef HAVE_UNISTD_H
74 #define HAVE_UNISTD_H 1
75 #endif /* !defined HAVE_UNISTD_H */
76
77 #ifndef HAVE_UTMPX_H
78 #define HAVE_UTMPX_H 0
79 #endif /* !defined HAVE_UTMPX_H */
80
81 #ifndef NETBSD_INSPIRED
82 # define NETBSD_INSPIRED 1
83 #endif
84
85 #if HAVE_INCOMPATIBLE_CTIME_R
86 #define asctime_r _incompatible_asctime_r
87 #define ctime_r _incompatible_ctime_r
88 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
89
90 /* Enable tm_gmtoff and tm_zone on GNUish systems. */
91 #define _GNU_SOURCE 1
92 /* Fix asctime_r on Solaris 10. */
93 #define _POSIX_PTHREAD_SEMANTICS 1
94 /* Enable strtoimax on Solaris 10. */
95 #define __EXTENSIONS__ 1
96
97 /*
98 ** Nested includes
99 */
100
101 #ifndef __NetBSD__
102 /* Avoid clashes with NetBSD by renaming NetBSD's declarations. */
103 #define localtime_rz sys_localtime_rz
104 #define mktime_z sys_mktime_z
105 #define posix2time_z sys_posix2time_z
106 #define time2posix_z sys_time2posix_z
107 #define timezone_t sys_timezone_t
108 #define tzalloc sys_tzalloc
109 #define tzfree sys_tzfree
110 #include <time.h>
111 #undef localtime_rz
112 #undef mktime_z
113 #undef posix2time_z
114 #undef time2posix_z
115 #undef timezone_t
116 #undef tzalloc
117 #undef tzfree
118 #else
119 #include "time.h"
120 #endif
121
122 #include "sys/types.h" /* for time_t */
123 #include "stdio.h"
124 #include "string.h"
125 #include "limits.h" /* for CHAR_BIT et al. */
126 #include "stdlib.h"
127
128 #include "errno.h"
129
130 #ifndef ENAMETOOLONG
131 # define ENAMETOOLONG EINVAL
132 #endif
133 #ifndef EOVERFLOW
134 # define EOVERFLOW EINVAL
135 #endif
136
137 #if HAVE_GETTEXT
138 #include "libintl.h"
139 #endif /* HAVE_GETTEXT */
140
141 #if HAVE_SYS_WAIT_H
142 #include <sys/wait.h> /* for WIFEXITED and WEXITSTATUS */
143 #endif /* HAVE_SYS_WAIT_H */
144
145 #ifndef WIFEXITED
146 #define WIFEXITED(status) (((status) & 0xff) == 0)
147 #endif /* !defined WIFEXITED */
148 #ifndef WEXITSTATUS
149 #define WEXITSTATUS(status) (((status) >> 8) & 0xff)
150 #endif /* !defined WEXITSTATUS */
151
152 #if HAVE_UNISTD_H
153 #include "unistd.h" /* for F_OK, R_OK, and other POSIX goodness */
154 #endif /* HAVE_UNISTD_H */
155
156 #ifndef HAVE_STRFTIME_L
157 # if _POSIX_VERSION < 200809
158 # define HAVE_STRFTIME_L 0
159 # else
160 # define HAVE_STRFTIME_L 1
161 # endif
162 #endif
163
164 #ifndef F_OK
165 #define F_OK 0
166 #endif /* !defined F_OK */
167 #ifndef R_OK
168 #define R_OK 4
169 #endif /* !defined R_OK */
170
171 /* Unlike <ctype.h>'s isdigit, this also works if c < 0 | c > UCHAR_MAX. */
172 #define is_digit(c) ((unsigned)(c) - '0' <= 9)
173
174 /*
175 ** Define HAVE_STDINT_H's default value here, rather than at the
176 ** start, since __GLIBC__'s value depends on previously-included
177 ** files.
178 ** (glibc 2.1 and later have stdint.h, even with pre-C99 compilers.)
179 */
180 #ifndef HAVE_STDINT_H
181 #define HAVE_STDINT_H \
182 (199901 <= __STDC_VERSION__ \
183 || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__) \
184 || __CYGWIN__)
185 #endif /* !defined HAVE_STDINT_H */
186
187 #if HAVE_STDINT_H
188 #include "stdint.h"
189 #endif /* !HAVE_STDINT_H */
190
191 #ifndef HAVE_INTTYPES_H
192 # define HAVE_INTTYPES_H HAVE_STDINT_H
193 #endif
194 #if HAVE_INTTYPES_H
195 # include <inttypes.h>
196 #endif
197
198 /* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX. */
199 #ifdef __LONG_LONG_MAX__
200 # ifndef LLONG_MAX
201 # define LLONG_MAX __LONG_LONG_MAX__
202 # endif
203 # ifndef LLONG_MIN
204 # define LLONG_MIN (-1 - LLONG_MAX)
205 # endif
206 #endif
207
208 #ifndef INT_FAST64_MAX
209 # ifdef LLONG_MAX
210 typedef long long int_fast64_t;
211 # define INT_FAST64_MIN LLONG_MIN
212 # define INT_FAST64_MAX LLONG_MAX
213 # else
214 # if (LONG_MAX >> 31) < 0xffffffff
215 Please use a compiler that supports a 64-bit integer type (or wider);
216 you may need to compile with "-DHAVE_STDINT_H".
217 # endif
218 typedef long int_fast64_t;
219 # define INT_FAST64_MIN LONG_MIN
220 # define INT_FAST64_MAX LONG_MAX
221 # endif
222 #endif
223
224 #ifndef SCNdFAST64
225 # if INT_FAST64_MAX == LLONG_MAX
226 # define SCNdFAST64 "lld"
227 # else
228 # define SCNdFAST64 "ld"
229 # endif
230 #endif
231
232 #ifndef INT_FAST32_MAX
233 # if INT_MAX >> 31 == 0
234 typedef long int_fast32_t;
235 # define INT_FAST32_MAX LONG_MAX
236 # define INT_FAST32_MIN LONG_MIN
237 # else
238 typedef int int_fast32_t;
239 # define INT_FAST32_MAX INT_MAX
240 # define INT_FAST32_MIN INT_MIN
241 # endif
242 #endif
243
244 #ifndef INTMAX_MAX
245 # ifdef LLONG_MAX
246 typedef long long intmax_t;
247 # define strtoimax strtoll
248 # define INTMAX_MAX LLONG_MAX
249 # define INTMAX_MIN LLONG_MIN
250 # else
251 typedef long intmax_t;
252 # define strtoimax strtol
253 # define INTMAX_MAX LONG_MAX
254 # define INTMAX_MIN LONG_MIN
255 # endif
256 #endif
257
258 #ifndef PRIdMAX
259 # if INTMAX_MAX == LLONG_MAX
260 # define PRIdMAX "lld"
261 # else
262 # define PRIdMAX "ld"
263 # endif
264 #endif
265
266 #ifndef UINTMAX_MAX
267 # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
268 typedef unsigned long long uintmax_t;
269 # else
270 typedef unsigned long uintmax_t;
271 # endif
272 #endif
273
274 #ifndef PRIuMAX
275 # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
276 # define PRIuMAX "llu"
277 # else
278 # define PRIuMAX "lu"
279 # endif
280 #endif
281
282 #ifndef INT32_MAX
283 #define INT32_MAX 0x7fffffff
284 #endif /* !defined INT32_MAX */
285 #ifndef INT32_MIN
286 #define INT32_MIN (-1 - INT32_MAX)
287 #endif /* !defined INT32_MIN */
288
289 #ifndef SIZE_MAX
290 #define SIZE_MAX ((size_t) -1)
291 #endif
292
293 #if 2 < __GNUC__ + (96 <= __GNUC_MINOR__)
294 # define ATTRIBUTE_CONST __attribute__ ((__const__))
295 # define ATTRIBUTE_PURE __attribute__ ((__pure__))
296 # define ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
297 #else
298 # define ATTRIBUTE_CONST /* empty */
299 # define ATTRIBUTE_PURE /* empty */
300 # define ATTRIBUTE_FORMAT(spec) /* empty */
301 #endif
302
303 #if !defined _Noreturn && __STDC_VERSION__ < 201112
304 # if 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
305 # define _Noreturn __attribute__ ((__noreturn__))
306 # else
307 # define _Noreturn
308 # endif
309 #endif
310
311 #if __STDC_VERSION__ < 199901 && !defined restrict
312 # define restrict /* empty */
313 #endif
314
315 /*
316 ** Workarounds for compilers/systems.
317 */
318
319 /*
320 ** Compile with -Dtime_tz=T to build the tz package with a private
321 ** time_t type equivalent to T rather than the system-supplied time_t.
322 ** This debugging feature can test unusual design decisions
323 ** (e.g., time_t wider than 'long', or unsigned time_t) even on
324 ** typical platforms.
325 */
326 #ifdef time_tz
327 # ifdef LOCALTIME_IMPLEMENTATION
328 static time_t sys_time(time_t *x) { return time(x); }
329 # endif
330
331 typedef time_tz tz_time_t;
332
333 # undef ctime
334 # define ctime tz_ctime
335 # undef ctime_r
336 # define ctime_r tz_ctime_r
337 # undef difftime
338 # define difftime tz_difftime
339 # undef gmtime
340 # define gmtime tz_gmtime
341 # undef gmtime_r
342 # define gmtime_r tz_gmtime_r
343 # undef localtime
344 # define localtime tz_localtime
345 # undef localtime_r
346 # define localtime_r tz_localtime_r
347 # undef localtime_rz
348 # define localtime_rz tz_localtime_rz
349 # undef mktime
350 # define mktime tz_mktime
351 # undef mktime_z
352 # define mktime_z tz_mktime_z
353 # undef offtime
354 # define offtime tz_offtime
355 # undef posix2time
356 # define posix2time tz_posix2time
357 # undef posix2time_z
358 # define posix2time_z tz_posix2time_z
359 # undef time
360 # define time tz_time
361 # undef time2posix
362 # define time2posix tz_time2posix
363 # undef time2posix_z
364 # define time2posix_z tz_time2posix_z
365 # undef time_t
366 # define time_t tz_time_t
367 # undef timegm
368 # define timegm tz_timegm
369 # undef timelocal
370 # define timelocal tz_timelocal
371 # undef timeoff
372 # define timeoff tz_timeoff
373 # undef tzalloc
374 # define tzalloc tz_tzalloc
375 # undef tzfree
376 # define tzfree tz_tzfree
377 # undef tzset
378 # define tzset tz_tzset
379 # undef tzsetwall
380 # define tzsetwall tz_tzsetwall
381
382 char *ctime(time_t const *);
383 char *ctime_r(time_t const *, char *);
384 double difftime(time_t, time_t);
385 struct tm *gmtime(time_t const *);
386 struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
387 struct tm *localtime(time_t const *);
388 struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
389 time_t mktime(struct tm *);
390 time_t time(time_t *);
391 void tzset(void);
392 #endif
393
394 /*
395 ** Some time.h implementations don't declare asctime_r.
396 ** Others might define it as a macro.
397 ** Fix the former without affecting the latter.
398 */
399
400 #ifndef asctime_r
401 extern char * asctime_r(struct tm const *restrict, char *restrict);
402 #endif
403
404 /*
405 ** The STD_INSPIRED functions are similar, but most also need
406 ** declarations if time_tz is defined.
407 */
408
409 #ifdef STD_INSPIRED
410 # if !defined tzsetwall || defined time_tz
411 void tzsetwall(void);
412 # endif
413 # if !defined offtime || defined time_tz
414 struct tm *offtime(time_t const *, long);
415 # endif
416 # if !defined timegm || defined time_tz
417 time_t timegm(struct tm *);
418 # endif
419 # if !defined timelocal || defined time_tz
420 time_t timelocal(struct tm *);
421 # endif
422 # if !defined timeoff || defined time_tz
423 time_t timeoff(struct tm *, long);
424 # endif
425 # if !defined time2posix || defined time_tz
426 time_t time2posix(time_t);
427 # endif
428 # if !defined posix2time || defined time_tz
429 time_t posix2time(time_t);
430 # endif
431 #endif
432
433 /* Infer TM_ZONE on systems where this information is known, but suppress
434 guessing if NO_TM_ZONE is defined. Similarly for TM_GMTOFF. */
435 #if (defined __GLIBC__ \
436 || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
437 || (defined __APPLE__ && defined __MACH__))
438 # if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
439 # define TM_GMTOFF tm_gmtoff
440 # endif
441 # if !defined TM_ZONE && !defined NO_TM_ZONE
442 # define TM_ZONE tm_zone
443 # endif
444 #endif
445
446 /*
447 ** Define functions that are ABI compatible with NetBSD but have
448 ** better prototypes. NetBSD 6.1.4 defines a pointer type timezone_t
449 ** and labors under the misconception that 'const timezone_t' is a
450 ** pointer to a constant. This use of 'const' is ineffective, so it
451 ** is not done here. What we call 'struct state' NetBSD calls
452 ** 'struct __state', but this is a private name so it doesn't matter.
453 */
454 #ifndef __NetBSD__
455 #if NETBSD_INSPIRED
456 typedef struct state *timezone_t;
457 struct tm *localtime_rz(timezone_t restrict, time_t const *restrict,
458 struct tm *restrict);
459 time_t mktime_z(timezone_t restrict, struct tm *restrict);
460 timezone_t tzalloc(char const *);
461 void tzfree(timezone_t);
462 # ifdef STD_INSPIRED
463 # if !defined posix2time_z || defined time_tz
464 time_t posix2time_z(timezone_t __restrict, time_t) ATTRIBUTE_PURE;
465 # endif
466 # if !defined time2posix_z || defined time_tz
467 time_t time2posix_z(timezone_t __restrict, time_t) ATTRIBUTE_PURE;
468 # endif
469 # endif
470 #endif
471 #endif
472
473 /*
474 ** Finally, some convenience items.
475 */
476
477 #if __STDC_VERSION__ < 199901
478 # define true 1
479 # define false 0
480 # define bool int
481 #else
482 # include <stdbool.h>
483 #endif
484
485 #ifndef TYPE_BIT
486 #define TYPE_BIT(type) (sizeof (type) * CHAR_BIT)
487 #endif /* !defined TYPE_BIT */
488
489 #ifndef TYPE_SIGNED
490 #define TYPE_SIGNED(type) (/*CONSTCOND*/((type) -1) < 0)
491 #endif /* !defined TYPE_SIGNED */
492
493 #define TWOS_COMPLEMENT(t) (/*CONSTCOND*/(t) ~ (t) 0 < 0)
494
495 /* Max and min values of the integer type T, of which only the bottom
496 B bits are used, and where the highest-order used bit is considered
497 to be a sign bit if T is signed. */
498 #define MAXVAL(t, b) /*LINTED*/ \
499 ((t) (((t) 1 << ((b) - 1 - TYPE_SIGNED(t))) \
500 - 1 + ((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))))
501 #define MINVAL(t, b) \
502 ((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
503
504 #ifdef LOCALTIME_IMPLEMENTATION
505 /* The minimum and maximum finite time values. This assumes no padding. */
506 static time_t const time_t_min = MINVAL(time_t, TYPE_BIT(time_t));
507 static time_t const time_t_max = MAXVAL(time_t, TYPE_BIT(time_t));
508 #endif
509
510 #ifndef INT_STRLEN_MAXIMUM
511 /*
512 ** 302 / 1000 is log10(2.0) rounded up.
513 ** Subtract one for the sign bit if the type is signed;
514 ** add one for integer division truncation;
515 ** add one more for a minus sign if the type is signed.
516 */
517 #define INT_STRLEN_MAXIMUM(type) \
518 ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
519 1 + TYPE_SIGNED(type))
520 #endif /* !defined INT_STRLEN_MAXIMUM */
521
522 /*
523 ** INITIALIZE(x)
524 */
525
526 #if defined(__GNUC__) || defined(__lint__)
527 # define INITIALIZE(x) ((x) = 0)
528 #else
529 # define INITIALIZE(x)
530 #endif
531
532 #ifndef UNINIT_TRAP
533 # define UNINIT_TRAP 0
534 #endif
535
536 /*
537 ** For the benefit of GNU folk...
538 ** '_(MSGID)' uses the current locale's message library string for MSGID.
539 ** The default is to use gettext if available, and use MSGID otherwise.
540 */
541
542 #ifndef _
543 #if HAVE_GETTEXT
544 #define _(msgid) gettext(msgid)
545 #else /* !HAVE_GETTEXT */
546 #define _(msgid) msgid
547 #endif /* !HAVE_GETTEXT */
548 #endif /* !defined _ */
549
550 #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
551 # define TZ_DOMAIN "tz"
552 #endif
553
554 #if HAVE_INCOMPATIBLE_CTIME_R
555 #undef asctime_r
556 #undef ctime_r
557 char *asctime_r(struct tm const *, char *);
558 char *ctime_r(time_t const *, char *);
559 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
560
561 #ifndef YEARSPERREPEAT
562 #define YEARSPERREPEAT 400 /* years before a Gregorian repeat */
563 #endif /* !defined YEARSPERREPEAT */
564
565 /*
566 ** The Gregorian year averages 365.2425 days, which is 31556952 seconds.
567 */
568
569 #ifndef AVGSECSPERYEAR
570 #define AVGSECSPERYEAR 31556952L
571 #endif /* !defined AVGSECSPERYEAR */
572
573 #ifndef SECSPERREPEAT
574 #define SECSPERREPEAT ((int_fast64_t) YEARSPERREPEAT * (int_fast64_t) AVGSECSPERYEAR)
575 #endif /* !defined SECSPERREPEAT */
576
577 #ifndef SECSPERREPEAT_BITS
578 #define SECSPERREPEAT_BITS 34 /* ceil(log2(SECSPERREPEAT)) */
579 #endif /* !defined SECSPERREPEAT_BITS */
580
581 #endif /* !defined PRIVATE_H */
582