xref: /freebsd/contrib/tzcode/private.h (revision f5dc2263ab1be8a35a7e27e82103f9ccd41ae584)
1 /* Private header for tzdb code.  */
2 
3 #ifndef PRIVATE_H
4 
5 #define PRIVATE_H
6 
7 /*
8 ** This file is in the public domain, so clarified as of
9 ** 1996-06-05 by Arthur David Olson.
10 */
11 
12 /*
13 ** This header is for use ONLY with the time conversion code.
14 ** There is no guarantee that it will remain unchanged,
15 ** or that it will remain at all.
16 ** Do NOT copy it to any system include directory.
17 ** Thank you!
18 */
19 
20 /* PORT_TO_C89 means the code should work even if the underlying
21    compiler and library support only C89 plus C99's 'long long'
22    and perhaps a few other extensions to C89.
23 
24    This macro is obsolescent, and the plan is to remove it along with
25    associated code.  A good time to do that might be in the year 2029
26    because RHEL 7 (whose GCC defaults to C89) extended life cycle
27    support (ELS) is scheduled to end on 2028-06-30.  */
28 #ifndef PORT_TO_C89
29 # define PORT_TO_C89 0
30 #endif
31 
32 /* SUPPORT_C89 means the tzcode library should support C89 callers
33    in addition to the usual support for C99-and-later callers.
34    This defaults to 1 as POSIX requires, even though that can trigger
35    latent bugs in callers.  */
36 #ifndef SUPPORT_C89
37 # define SUPPORT_C89 1
38 #endif
39 
40 
41 /* The following feature-test macros should be defined before
42    any #include of a system header.  */
43 
44 /* Enable tm_gmtoff, tm_zone, and environ on GNUish systems.  */
45 #define _GNU_SOURCE 1
46 /* Fix asctime_r on Solaris 11.  */
47 #define _POSIX_PTHREAD_SEMANTICS 1
48 /* Enable strtoimax on pre-C99 Solaris 11.  */
49 #define __EXTENSIONS__ 1
50 /* Cause MS-Windows headers to define POSIX names.  */
51 #define _CRT_DECLARE_NONSTDC_NAMES 1
52 /* Prevent MS-Windows headers from defining min and max.  */
53 #define NOMINMAX 1
54 
55 /* On GNUish systems where time_t might be 32 or 64 bits, use 64.
56    On these platforms _FILE_OFFSET_BITS must also be 64; otherwise
57    setting _TIME_BITS to 64 does not work.  The code does not
58    otherwise rely on _FILE_OFFSET_BITS being 64, since it does not
59    use off_t or functions like 'stat' that depend on off_t.  */
60 #ifndef _TIME_BITS
61 # ifndef _FILE_OFFSET_BITS
62 #  define _FILE_OFFSET_BITS 64
63 # endif
64 # if _FILE_OFFSET_BITS == 64
65 #  define _TIME_BITS 64
66 # endif
67 #endif
68 
69 /* End of feature-test macro definitions.  */
70 
71 
72 #ifndef __STDC_VERSION__
73 # define __STDC_VERSION__ 0
74 #endif
75 
76 /* Define true, false and bool if they don't work out of the box.  */
77 #if PORT_TO_C89 && __STDC_VERSION__ < 199901
78 # define true 1
79 # define false 0
80 # define bool int
81 #elif __STDC_VERSION__ < 202311
82 # include <stdbool.h>
83 #endif
84 
85 /* For pre-C23 compilers, a substitute for static_assert.
86    Some of these compilers may warn if it is used outside the top level.  */
87 #if __STDC_VERSION__ < 202311 && !defined static_assert
88 # define static_assert(cond) extern int static_assert_check[(cond) ? 1 : -1]
89 #endif
90 
91 /*
92 ** zdump has been made independent of the rest of the time
93 ** conversion package to increase confidence in the verification it provides.
94 ** You can use zdump to help in verifying other implementations.
95 ** To do this, compile with -DUSE_LTZ=0 and link without the tz library.
96 */
97 #ifndef USE_LTZ
98 # define USE_LTZ 1
99 #endif
100 
101 /* This string was in the Factory zone through version 2016f.  */
102 #ifndef GRANDPARENTED
103 # define GRANDPARENTED	"Local time zone must be set--see zic manual page"
104 #endif
105 
106 /*
107 ** Defaults for preprocessor symbols.
108 ** You can override these in your C compiler options, e.g. '-DHAVE_GETTEXT=1'.
109 */
110 
111 #if !defined HAVE__GENERIC && defined __has_extension
112 # if !__has_extension(c_generic_selections)
113 #  define HAVE__GENERIC 0
114 # endif
115 #endif
116 /* _Generic is buggy in pre-4.9 GCC.  */
117 #if !defined HAVE__GENERIC && defined __GNUC__ && !defined __STRICT_ANSI__
118 # define HAVE__GENERIC (4 < __GNUC__ + (9 <= __GNUC_MINOR__))
119 #endif
120 #ifndef HAVE__GENERIC
121 # define HAVE__GENERIC (201112 <= __STDC_VERSION__)
122 #endif
123 
124 #ifndef HAVE_GETEUID
125 # define HAVE_GETEUID 1
126 #endif
127 
128 #ifndef HAVE_GETRESUID
129 # define HAVE_GETRESUID 1
130 #endif
131 
132 #if !defined HAVE_GETTEXT && defined __has_include
133 # if __has_include(<libintl.h>)
134 #  define HAVE_GETTEXT 1
135 # endif
136 #endif
137 #ifndef HAVE_GETTEXT
138 # define HAVE_GETTEXT 0
139 #endif
140 
141 #ifndef HAVE_INCOMPATIBLE_CTIME_R
142 # define HAVE_INCOMPATIBLE_CTIME_R 0
143 #endif
144 
145 #ifndef HAVE_LINK
146 # define HAVE_LINK 1
147 #endif /* !defined HAVE_LINK */
148 
149 #ifndef HAVE_MALLOC_ERRNO
150 # define HAVE_MALLOC_ERRNO 1
151 #endif
152 
153 #ifndef HAVE_POSIX_DECLS
154 # define HAVE_POSIX_DECLS 1
155 #endif
156 
157 #ifndef HAVE_SETENV
158 # define HAVE_SETENV 1
159 #endif
160 
161 #ifndef HAVE_STRDUP
162 # define HAVE_STRDUP 1
163 #endif
164 
165 #ifndef HAVE_SYMLINK
166 # define HAVE_SYMLINK 1
167 #endif /* !defined HAVE_SYMLINK */
168 
169 #if !defined HAVE_SYS_STAT_H && defined __has_include
170 # if !__has_include(<sys/stat.h>)
171 #  define HAVE_SYS_STAT_H 0
172 # endif
173 #endif
174 #ifndef HAVE_SYS_STAT_H
175 # define HAVE_SYS_STAT_H 1
176 #endif
177 
178 #if !defined HAVE_UNISTD_H && defined __has_include
179 # if !__has_include(<unistd.h>)
180 #  define HAVE_UNISTD_H 0
181 # endif
182 #endif
183 #ifndef HAVE_UNISTD_H
184 # define HAVE_UNISTD_H 1
185 #endif
186 
187 #ifndef NETBSD_INSPIRED
188 # define NETBSD_INSPIRED 1
189 #endif
190 
191 #if HAVE_INCOMPATIBLE_CTIME_R
192 # define asctime_r _incompatible_asctime_r
193 # define ctime_r _incompatible_ctime_r
194 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
195 
196 #ifndef TZ_RUNTIME_LEAPS
197 # define TZ_RUNTIME_LEAPS 1
198 #endif
199 
200 /*
201 ** Nested includes
202 */
203 
204 #include <stddef.h>
205 
206 /* If defining the 'timezone' variable a la POSIX, avoid clashing with the old
207    'timezone' function of FreeBSD <= 14, by renaming the latter's declaration.
208    This hack can be removed after 2028-11-30, FreeBSD 14's expected EOL.  */
209 #if (defined __FreeBSD__  && __FreeBSD__ < 15 && defined __BSD_VISIBLE \
210      && defined USG_COMPAT  && USG_COMPAT == 2)
211 # define timezone sys_timezone
212 # define timezone_defined
213 #endif
214 
215 #include <time.h>
216 
217 #ifdef timezone_defined
218 # undef timezone
219 # undef timezone_defined
220 #endif
221 
222 #include <string.h>
223 #if defined HAVE_STRNLEN && !HAVE_STRNLEN
224 static size_t
225 strnlen (char const *s, size_t maxlen)
226 {
227   size_t i;
228   for (i = 0; i < maxlen && s[i]; i++)
229     continue;
230   return i;
231 }
232 #endif
233 
234 #if !PORT_TO_C89
235 # include <inttypes.h>
236 #endif
237 #include <limits.h>	/* for CHAR_BIT et al. */
238 #include <stdlib.h>
239 
240 #include <errno.h>
241 
242 #ifndef EINVAL
243 # define EINVAL ERANGE
244 #endif
245 
246 #ifndef EFTYPE
247 # define EFTYPE EINVAL
248 #endif
249 #ifndef ELOOP
250 # define ELOOP EINVAL
251 #endif
252 #ifndef ENAMETOOLONG
253 # define ENAMETOOLONG EINVAL
254 #endif
255 #ifndef ENOMEM
256 # define ENOMEM EINVAL
257 #endif
258 #ifndef ENOTCAPABLE
259 # define ENOTCAPABLE EINVAL
260 #endif
261 #ifndef ENOTSUP
262 # define ENOTSUP EINVAL
263 #endif
264 #ifndef EOVERFLOW
265 # define EOVERFLOW EINVAL
266 #endif
267 
268 #if HAVE_GETTEXT
269 # include <libintl.h>
270 #endif /* HAVE_GETTEXT */
271 
272 #if HAVE_UNISTD_H
273 # include <unistd.h>
274 #else
275 /* Assume getopt.o or equivalent is linked via Makefile configuration.  */
276 int getopt(int, char *const[], char const *);
277 extern char *optarg;
278 extern int optind;
279 #endif /* HAVE_UNISTD_H */
280 
281 /* SUPPORT_POSIX2008 means the tzcode library should support
282    POSIX.1-2017-and-earlier callers in addition to the usual support for
283    POSIX.1-2024-and-later callers; however, this can be
284    incompatible with POSIX.1-2024-and-later callers.
285    This macro is obsolescent, and the plan is to remove it
286    along with any code needed only when it is nonzero.
287    A good time to do that might be in the year 2034.
288    This macro's name is SUPPORT_POSIX2008 because _POSIX_VERSION == 200809
289    in POSIX.1-2017, a minor revision of POSIX.1-2008.  */
290 #ifndef SUPPORT_POSIX2008
291 # if defined _POSIX_VERSION && _POSIX_VERSION <= 200809
292 #  define SUPPORT_POSIX2008 1
293 # else
294 #  define SUPPORT_POSIX2008 0
295 # endif
296 #endif
297 
298 #ifndef HAVE_DECL_ASCTIME_R
299 # if SUPPORT_POSIX2008
300 #  define HAVE_DECL_ASCTIME_R 1
301 # else
302 #  define HAVE_DECL_ASCTIME_R 0
303 # endif
304 #endif
305 
306 #ifndef HAVE_ISSETUGID
307 # if (defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
308       || (defined __linux__ && !defined __GLIBC__) /* Android, musl, etc. */ \
309       || (defined __APPLE__ && defined __MACH__) || defined __sun)
310 #  define HAVE_ISSETUGID 1
311 # else
312 #  define HAVE_ISSETUGID 0
313 # endif
314 #endif
315 
316 #ifndef HAVE_SNPRINTF
317 # define HAVE_SNPRINTF (!PORT_TO_C89 || 199901 <= __STDC_VERSION__)
318 #endif
319 
320 #ifndef HAVE_STRFTIME_L
321 # if _POSIX_VERSION < 200809
322 #  define HAVE_STRFTIME_L 0
323 # else
324 #  define HAVE_STRFTIME_L 1
325 # endif
326 #endif
327 
328 #ifndef USG_COMPAT
329 # ifndef _XOPEN_VERSION
330 #  define USG_COMPAT 0
331 # else
332 #  define USG_COMPAT 1
333 # endif
334 #endif
335 
336 #ifndef HAVE_TZNAME
337 # if _POSIX_VERSION < 198808 && !USG_COMPAT
338 #  define HAVE_TZNAME 0
339 # else
340 #  define HAVE_TZNAME 1
341 # endif
342 #endif
343 
344 #ifndef ALTZONE
345 # if defined __sun || defined _M_XENIX
346 #  define ALTZONE 1
347 # else
348 #  define ALTZONE 0
349 # endif
350 #endif
351 
352 #if PORT_TO_C89
353 
354 /*
355 ** Define HAVE_STDINT_H's default value here, rather than at the
356 ** start, since __GLIBC__ and INTMAX_MAX's values depend on
357 ** previously included files.  glibc 2.1 and Solaris 10 and later have
358 ** stdint.h, even with pre-C99 compilers.
359 */
360 # if !defined HAVE_STDINT_H && defined __has_include
361 #  define HAVE_STDINT_H 1 /* C23 __has_include implies C99 stdint.h.  */
362 # endif
363 # ifndef HAVE_STDINT_H
364 #  define HAVE_STDINT_H \
365     (199901 <= __STDC_VERSION__ \
366      || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__) \
367      || __CYGWIN__ || INTMAX_MAX)
368 # endif /* !defined HAVE_STDINT_H */
369 
370 # if HAVE_STDINT_H
371 #  include <stdint.h>
372 # endif /* !HAVE_STDINT_H */
373 
374 # ifndef HAVE_INTTYPES_H
375 #  define HAVE_INTTYPES_H HAVE_STDINT_H
376 # endif
377 # if HAVE_INTTYPES_H
378 #  include <inttypes.h>
379 # endif
380 
381 /* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX.  */
382 # if defined __LONG_LONG_MAX__ && !defined __STRICT_ANSI__
383 #  ifndef LLONG_MAX
384 #   define LLONG_MAX __LONG_LONG_MAX__
385 #  endif
386 #  ifndef LLONG_MIN
387 #   define LLONG_MIN (-1 - LLONG_MAX)
388 #  endif
389 #  ifndef ULLONG_MAX
390 #   define ULLONG_MAX (LLONG_MAX * 2ull + 1)
391 #  endif
392 # endif
393 
394 # ifndef INT_FAST64_MAX
395 #  if 1 <= LONG_MAX >> 31 >> 31
396 typedef long int_fast64_t;
397 #   define INT_FAST64_MIN LONG_MIN
398 #   define INT_FAST64_MAX LONG_MAX
399 #  else
400 /* If this fails, compile with -DHAVE_STDINT_H or with a better compiler.  */
401 typedef long long int_fast64_t;
402 #   define INT_FAST64_MIN LLONG_MIN
403 #   define INT_FAST64_MAX LLONG_MAX
404 #  endif
405 # endif
406 
407 # ifndef PRIdFAST64
408 #  if INT_FAST64_MAX == LONG_MAX
409 #   define PRIdFAST64 "ld"
410 #  else
411 #   define PRIdFAST64 "lld"
412 #  endif
413 # endif
414 
415 # ifndef SCNdFAST64
416 #  define SCNdFAST64 PRIdFAST64
417 # endif
418 
419 # ifndef INT_FAST32_MAX
420 typedef long int_fast32_t;
421 #  define INT_FAST32_MAX LONG_MAX
422 #  define INT_FAST32_MIN LONG_MIN
423 # endif
424 
425 # ifndef INT_LEAST32_MAX
426 typedef int_fast32_t int_least32_t;
427 # endif
428 
429 # ifndef INTMAX_MAX
430 #  ifdef LLONG_MAX
431 typedef long long intmax_t;
432 #   ifndef HAVE_STRTOLL
433 #    define HAVE_STRTOLL 1
434 #   endif
435 #   if HAVE_STRTOLL
436 #    define strtoimax strtoll
437 #   endif
438 #   define INTMAX_MAX LLONG_MAX
439 #   define INTMAX_MIN LLONG_MIN
440 #  else
441 typedef long intmax_t;
442 #   define INTMAX_MAX LONG_MAX
443 #   define INTMAX_MIN LONG_MIN
444 #  endif
445 #  ifndef strtoimax
446 #   define strtoimax strtol
447 #  endif
448 # endif
449 
450 # ifndef PRIdMAX
451 #  if INTMAX_MAX == LLONG_MAX
452 #   define PRIdMAX "lld"
453 #  else
454 #   define PRIdMAX "ld"
455 #  endif
456 # endif
457 
458 # ifndef PTRDIFF_MAX
459 #  define PTRDIFF_MAX MAXVAL(ptrdiff_t, TYPE_BIT(ptrdiff_t))
460 # endif
461 
462 # ifndef UINT_FAST32_MAX
463 typedef unsigned long uint_fast32_t;
464 # endif
465 
466 # ifndef UINT_FAST64_MAX
467 #  if 3 <= ULONG_MAX >> 31 >> 31
468 typedef unsigned long uint_fast64_t;
469 #   define UINT_FAST64_MAX ULONG_MAX
470 #  else
471 /* If this fails, compile with -DHAVE_STDINT_H or with a better compiler.  */
472 typedef unsigned long long uint_fast64_t;
473 #   define UINT_FAST64_MAX ULLONG_MAX
474 #  endif
475 # endif
476 
477 # ifndef UINTMAX_MAX
478 #  ifdef ULLONG_MAX
479 typedef unsigned long long uintmax_t;
480 #   define UINTMAX_MAX ULLONG_MAX
481 #  else
482 typedef unsigned long uintmax_t;
483 #   define UINTMAX_MAX ULONG_MAX
484 #  endif
485 # endif
486 
487 # ifndef PRIuMAX
488 #  ifdef ULLONG_MAX
489 #   define PRIuMAX "llu"
490 #  else
491 #   define PRIuMAX "lu"
492 #  endif
493 # endif
494 
495 # ifndef SIZE_MAX
496 #  define SIZE_MAX ((size_t) -1)
497 # endif
498 
499 #endif /* PORT_TO_C89 */
500 
501 /* The maximum size of any created object, as a signed integer.
502    Although the C standard does not outright prohibit larger objects,
503    behavior is undefined if the result of pointer subtraction does not
504    fit into ptrdiff_t, and the code assumes in several places that
505    pointer subtraction works.  As a practical matter it's OK to not
506    support objects larger than this.  */
507 #define INDEX_MAX ((ptrdiff_t) min(PTRDIFF_MAX, SIZE_MAX))
508 
509 /* Support ckd_add, ckd_sub, ckd_mul on C23 or recent-enough GCC-like
510    hosts, unless compiled with -DHAVE_STDCKDINT_H=0 or with pre-C23 EDG.  */
511 #if !defined HAVE_STDCKDINT_H && defined __has_include
512 # if __has_include(<stdckdint.h>)
513 #  define HAVE_STDCKDINT_H 1
514 # endif
515 #endif
516 #ifdef HAVE_STDCKDINT_H
517 # if HAVE_STDCKDINT_H
518 #  include <stdckdint.h>
519 # endif
520 #elif defined __EDG__
521 /* Do nothing, to work around EDG bug <https://bugs.gnu.org/53256>.  */
522 #elif defined __has_builtin
523 # if __has_builtin(__builtin_add_overflow)
524 #  define ckd_add(r, a, b) __builtin_add_overflow(a, b, r)
525 # endif
526 # if __has_builtin(__builtin_sub_overflow)
527 #  define ckd_sub(r, a, b) __builtin_sub_overflow(a, b, r)
528 # endif
529 # if __has_builtin(__builtin_mul_overflow)
530 #  define ckd_mul(r, a, b) __builtin_mul_overflow(a, b, r)
531 # endif
532 #elif 7 <= __GNUC__
533 # define ckd_add(r, a, b) __builtin_add_overflow(a, b, r)
534 # define ckd_sub(r, a, b) __builtin_sub_overflow(a, b, r)
535 # define ckd_mul(r, a, b) __builtin_mul_overflow(a, b, r)
536 #endif
537 
538 #if (defined __has_c_attribute \
539      && (202311 <= __STDC_VERSION__ || !defined __STRICT_ANSI__))
540 # define HAVE___HAS_C_ATTRIBUTE true
541 #else
542 # define HAVE___HAS_C_ATTRIBUTE false
543 #endif
544 
545 #ifdef __has_attribute
546 # if __has_attribute (nonstring)
547 #  define ATTRIBUTE_NONSTRING __attribute__((__nonstring__))
548 # endif
549 #endif
550 #ifndef ATTRIBUTE_NONSTRING
551 # define ATTRIBUTE_NONSTRING
552 #endif
553 
554 #if HAVE___HAS_C_ATTRIBUTE
555 # if __has_c_attribute(deprecated)
556 #  define ATTRIBUTE_DEPRECATED [[deprecated]]
557 # endif
558 #endif
559 #ifndef ATTRIBUTE_DEPRECATED
560 # if 3 < __GNUC__ + (2 <= __GNUC_MINOR__)
561 #  define ATTRIBUTE_DEPRECATED __attribute__((deprecated))
562 # else
563 #  define ATTRIBUTE_DEPRECATED /* empty */
564 # endif
565 #endif
566 
567 #if HAVE___HAS_C_ATTRIBUTE
568 # if __has_c_attribute(fallthrough)
569 #  define ATTRIBUTE_FALLTHROUGH [[fallthrough]]
570 # endif
571 #endif
572 #ifndef ATTRIBUTE_FALLTHROUGH
573 # if 7 <= __GNUC__
574 #  define ATTRIBUTE_FALLTHROUGH __attribute__((fallthrough))
575 # else
576 #  define ATTRIBUTE_FALLTHROUGH ((void) 0)
577 # endif
578 #endif
579 
580 #if HAVE___HAS_C_ATTRIBUTE
581 # if __has_c_attribute(maybe_unused)
582 #  define ATTRIBUTE_MAYBE_UNUSED [[maybe_unused]]
583 # endif
584 #endif
585 #ifndef ATTRIBUTE_MAYBE_UNUSED
586 # if 2 < __GNUC__ + (7 <= __GNUC_MINOR__)
587 #  define ATTRIBUTE_MAYBE_UNUSED __attribute__((unused))
588 # else
589 #  define ATTRIBUTE_MAYBE_UNUSED /* empty */
590 # endif
591 #endif
592 
593 #if HAVE___HAS_C_ATTRIBUTE
594 # if __has_c_attribute(noreturn)
595 #  define ATTRIBUTE_NORETURN [[noreturn]]
596 # endif
597 #endif
598 #ifndef ATTRIBUTE_NORETURN
599 # if 201112 <= __STDC_VERSION__
600 #  define ATTRIBUTE_NORETURN _Noreturn
601 # elif 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
602 #  define ATTRIBUTE_NORETURN __attribute__((noreturn))
603 # else
604 #  define ATTRIBUTE_NORETURN /* empty */
605 # endif
606 #endif
607 
608 #if HAVE___HAS_C_ATTRIBUTE
609 # if __has_c_attribute(reproducible)
610 #  define ATTRIBUTE_REPRODUCIBLE [[reproducible]]
611 # endif
612 #endif
613 #ifndef ATTRIBUTE_REPRODUCIBLE
614 # define ATTRIBUTE_REPRODUCIBLE /* empty */
615 #endif
616 
617 #if HAVE___HAS_C_ATTRIBUTE
618 # if __has_c_attribute(unsequenced)
619 #  define ATTRIBUTE_UNSEQUENCED [[unsequenced]]
620 # endif
621 #endif
622 #ifndef ATTRIBUTE_UNSEQUENCED
623 # define ATTRIBUTE_UNSEQUENCED /* empty */
624 #endif
625 
626 /* GNU C attributes that are useful in tzcode.
627    Although neither __attribute__((const)) nor __attribute__((pure)) are
628    stricter than their C23 counterparts [[unsequenced]] and [[reproducible]],
629    the C23 attributes happen to work in each tzcode use of ATTRIBUTE_CONST
630    and ATTRIBUTE_PURE.  (This might not work outside of tzcode!)  */
631 #if __GNUC__ < 3
632 # define ATTRIBUTE_CONST ATTRIBUTE_UNSEQUENCED
633 # define ATTRIBUTE_FORMAT(spec) /* empty */
634 # define ATTRIBUTE_PURE ATTRIBUTE_REPRODUCIBLE
635 #else
636 # define ATTRIBUTE_CONST __attribute__((const))
637 # define ATTRIBUTE_FORMAT(spec) __attribute__((format spec))
638 # define ATTRIBUTE_PURE __attribute__((pure))
639 #endif
640 
641 /* Avoid GCC bug 114833 <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114833>.
642    Remove this macro and its uses when the bug is fixed in a GCC release,
643    because only the latest GCC matters for $(GCC_DEBUG_FLAGS).  */
644 #ifdef GCC_LINT
645 # define ATTRIBUTE_PURE_114833 ATTRIBUTE_PURE
646 #else
647 # define ATTRIBUTE_PURE_114833 /* empty */
648 #endif
649 /* GCC_LINT hack to pacify GCC bug 114833 even though the attribute is
650    not strictly correct, as the function might not return whereas pure
651    functions are supposed to return exactly once.  This hack is not
652    known to generate wrong code for tzcode on any platform.
653    Remove this macro and its uses when the bug is fixed in a GCC release.  */
654 #define ATTRIBUTE_PURE_114833_HACK ATTRIBUTE_PURE_114833
655 
656 #if (__STDC_VERSION__ < 199901 && !defined restrict \
657      && (PORT_TO_C89 || defined _MSC_VER))
658 # define restrict /* empty */
659 #endif
660 
661 /*
662 ** Workarounds for compilers/systems.
663 */
664 
665 #ifndef EPOCH_LOCAL
666 # define EPOCH_LOCAL 0
667 #endif
668 #ifndef EPOCH_OFFSET
669 # define EPOCH_OFFSET 0
670 #endif
671 #ifndef RESERVE_STD_EXT_IDS
672 # define RESERVE_STD_EXT_IDS 0
673 #endif
674 
675 #ifdef time_tz
676 # define defined_time_tz true
677 #else
678 # define defined_time_tz false
679 #endif
680 
681 /* If standard C identifiers with external linkage (e.g., localtime)
682    are reserved and are not already being renamed anyway, rename them
683    as if compiling with '-Dtime_tz=time_t'.  */
684 #if !defined time_tz && RESERVE_STD_EXT_IDS && USE_LTZ
685 # define time_tz time_t
686 #endif
687 
688 /*
689 ** Compile with -Dtime_tz=T to build the tz package with a private
690 ** time_t type equivalent to T rather than the system-supplied time_t.
691 ** This debugging feature can test unusual design decisions
692 ** (e.g., time_t wider than 'long', or unsigned time_t) even on
693 ** typical platforms.
694 */
695 #if defined time_tz || EPOCH_LOCAL || EPOCH_OFFSET != 0
696 # define TZ_TIME_T true
697 #else
698 # define TZ_TIME_T false
699 #endif
700 
701 #if defined LOCALTIME_IMPLEMENTATION && TZ_TIME_T
702 static time_t sys_time(time_t *x) { return time(x); }
703 #endif
704 
705 #if TZ_TIME_T
706 
707 typedef time_tz tz_time_t;
708 
709 # undef  asctime
710 # define asctime tz_asctime
711 # undef  ctime
712 # define ctime tz_ctime
713 # undef  difftime
714 # define difftime tz_difftime
715 # undef  gmtime
716 # define gmtime tz_gmtime
717 # undef  gmtime_r
718 # define gmtime_r tz_gmtime_r
719 # undef  localtime
720 # define localtime tz_localtime
721 # undef  localtime_r
722 # define localtime_r tz_localtime_r
723 # undef  localtime_rz
724 # define localtime_rz tz_localtime_rz
725 # undef  mktime
726 # define mktime tz_mktime
727 # undef  mktime_z
728 # define mktime_z tz_mktime_z
729 # undef  offtime
730 # define offtime tz_offtime
731 # undef  offtime_r
732 # define offtime_r tz_offtime_r
733 # undef  posix2time
734 # define posix2time tz_posix2time
735 # undef  posix2time_z
736 # define posix2time_z tz_posix2time_z
737 # undef  strftime
738 # define strftime tz_strftime
739 # undef  time
740 # define time tz_time
741 # undef  time2posix
742 # define time2posix tz_time2posix
743 # undef  time2posix_z
744 # define time2posix_z tz_time2posix_z
745 # undef  time_t
746 # define time_t tz_time_t
747 # undef  timegm
748 # define timegm tz_timegm
749 # undef  timelocal
750 # define timelocal tz_timelocal
751 # undef  timeoff
752 # define timeoff tz_timeoff
753 # undef  tzalloc
754 # define tzalloc tz_tzalloc
755 # undef  tzfree
756 # define tzfree tz_tzfree
757 # undef  tzset
758 # define tzset tz_tzset
759 # if SUPPORT_POSIX2008
760 #  undef  asctime_r
761 #  define asctime_r tz_asctime_r
762 #  undef  ctime_r
763 #  define ctime_r tz_ctime_r
764 # endif
765 # if HAVE_STRFTIME_L
766 #  undef  strftime_l
767 #  define strftime_l tz_strftime_l
768 # endif
769 # if HAVE_TZNAME
770 #  undef  tzname
771 #  define tzname tz_tzname
772 # endif
773 # if USG_COMPAT
774 #  undef  daylight
775 #  define daylight tz_daylight
776 #  undef  timezone
777 #  define timezone tz_timezone
778 # endif
779 # if ALTZONE
780 #  undef  altzone
781 #  define altzone tz_altzone
782 # endif
783 
784 # if __STDC_VERSION__ < 202311
785 #  define DEPRECATED_IN_C23 /* empty */
786 # else
787 #  define DEPRECATED_IN_C23 ATTRIBUTE_DEPRECATED
788 # endif
789 DEPRECATED_IN_C23 char *asctime(struct tm const *);
790 DEPRECATED_IN_C23 char *ctime(time_t const *);
791 # if SUPPORT_POSIX2008
792 char *asctime_r(struct tm const *restrict, char *restrict);
793 char *ctime_r(time_t const *, char *);
794 # endif
795 ATTRIBUTE_CONST double difftime(time_t, time_t);
796 size_t strftime(char *restrict, size_t, char const *restrict,
797 		struct tm const *restrict);
798 # if HAVE_STRFTIME_L
799 size_t strftime_l(char *restrict, size_t, char const *restrict,
800 		  struct tm const *restrict, locale_t);
801 # endif
802 struct tm *gmtime(time_t const *);
803 struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
804 struct tm *localtime(time_t const *);
805 struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
806 time_t mktime(struct tm *);
807 time_t time(time_t *);
808 time_t timegm(struct tm *);
809 void tzset(void);
810 #endif
811 
812 #ifndef HAVE_DECL_TIMEGM
813 # if (202311 <= __STDC_VERSION__ \
814       || defined __GLIBC__ || defined __tm_zone /* musl */ \
815       || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
816       || (defined __APPLE__ && defined __MACH__))
817 #  define HAVE_DECL_TIMEGM 1
818 # else
819 #  define HAVE_DECL_TIMEGM 0
820 # endif
821 #endif
822 #if !HAVE_DECL_TIMEGM && !defined timegm
823 time_t timegm(struct tm *);
824 #endif
825 
826 #if !HAVE_DECL_ASCTIME_R && !defined asctime_r && SUPPORT_POSIX2008
827 extern char *asctime_r(struct tm const *restrict, char *restrict);
828 #endif
829 
830 #ifndef HAVE_DECL_ENVIRON
831 # if defined environ || defined __USE_GNU
832 #  define HAVE_DECL_ENVIRON 1
833 # else
834 #  define HAVE_DECL_ENVIRON 0
835 # endif
836 #endif
837 
838 #if !HAVE_DECL_ENVIRON
839 extern char **environ;
840 #endif
841 
842 #ifndef HAVE_MEMPCPY
843 # if (defined mempcpy \
844       || defined __FreeBSD__ || defined __NetBSD__ || defined __linux__)
845 #  define HAVE_MEMPCPY 1
846 # else
847 #  define HAVE_MEMPCPY 0
848 # endif
849 #endif
850 #if !HAVE_MEMPCPY
851 static void *
852 mempcpy(void *restrict s1, void const *restrict s2, size_t n)
853 {
854   char *p = memcpy(s1, s2, n);
855   return p + n;
856 }
857 #endif
858 
859 #if 2 <= HAVE_TZNAME + (TZ_TIME_T || !HAVE_POSIX_DECLS)
860 extern char *tzname[];
861 #endif
862 #if 2 <= USG_COMPAT + (TZ_TIME_T || !HAVE_POSIX_DECLS)
863 extern long timezone;
864 extern int daylight;
865 #endif
866 #if 2 <= ALTZONE + (TZ_TIME_T || !HAVE_POSIX_DECLS)
867 extern long altzone;
868 #endif
869 
870 /*
871 ** The STD_INSPIRED functions are similar, but most also need
872 ** declarations if time_tz is defined.
873 */
874 
875 #ifndef STD_INSPIRED
876 # ifdef __NetBSD__
877 #  define STD_INSPIRED 1
878 # else
879 #  define STD_INSPIRED 0
880 # endif
881 #endif
882 #if STD_INSPIRED
883 # if TZ_TIME_T || !defined offtime
884 struct tm *offtime(time_t const *, long);
885 # endif
886 # if TZ_TIME_T || !defined offtime_r
887 struct tm *offtime_r(time_t const *restrict, long, struct tm *restrict);
888 # endif
889 # if TZ_TIME_T || !defined timelocal
890 time_t timelocal(struct tm *);
891 # endif
892 # if TZ_TIME_T || !defined timeoff
893 #  define EXTERN_TIMEOFF
894 # endif
895 # if TZ_TIME_T || !defined time2posix
896 time_t time2posix(time_t);
897 # endif
898 # if TZ_TIME_T || !defined posix2time
899 time_t posix2time(time_t);
900 # endif
901 #endif
902 
903 /* Infer TM_ZONE on systems where this information is known, but suppress
904    guessing if NO_TM_ZONE is defined.  Similarly for TM_GMTOFF.  */
905 #if (200809 < _POSIX_VERSION \
906      || defined __GLIBC__ \
907      || defined __tm_zone /* musl */ \
908      || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
909      || (defined __APPLE__ && defined __MACH__))
910 # if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
911 #  define TM_GMTOFF tm_gmtoff
912 # endif
913 # if !defined TM_ZONE && !defined NO_TM_ZONE
914 #  define TM_ZONE tm_zone
915 # endif
916 #endif
917 
918 /*
919 ** Define functions that are ABI compatible with NetBSD.
920 ** What we call 'struct state' NetBSD calls
921 ** 'struct __state', but this is a private name so it doesn't matter.
922 */
923 #if NETBSD_INSPIRED
924 # ifdef _NETBSD_SOURCE
925 #  define state __state
926 # else
927 typedef struct state *timezone_t;
928 # endif
929 struct tm *localtime_rz(timezone_t restrict, time_t const *restrict,
930 			struct tm *restrict);
931 time_t mktime_z(timezone_t restrict, struct tm *restrict);
932 timezone_t tzalloc(char const *);
933 void tzfree(timezone_t);
934 # if STD_INSPIRED
935 #  if TZ_RUNTIME_LEAPS
936 #   define ATTRIBUTE_POSIX2TIME ATTRIBUTE_PURE
937 #  else
938 #   define ATTRIBUTE_POSIX2TIME ATTRIBUTE_CONST
939 #  endif
940 #  if TZ_TIME_T || !defined posix2time_z
941 ATTRIBUTE_POSIX2TIME time_t posix2time_z(timezone_t, time_t);
942 #  endif
943 #  if TZ_TIME_T || !defined time2posix_z
944 ATTRIBUTE_POSIX2TIME time_t time2posix_z(timezone_t, time_t);
945 #  endif
946 # endif
947 #endif
948 
949 /*
950 ** Finally, some convenience items.
951 */
952 
953 #define TYPE_BIT(type) (CHAR_BIT * (ptrdiff_t) sizeof(type))
954 #define TYPE_SIGNED(type) (((type) -1) < 0)
955 #define TWOS_COMPLEMENT(type) (TYPE_SIGNED (type) && (! ~ (type) -1))
956 
957 /* Yield the value of the arithmetic expression X after integer promotion.
958    This is safer than a cast, which in general would accept even
959    pointers and which might trap or yield a value not equal to X.  */
960 #define INT_PROMOTE(x) (+(x))
961 
962 /* Minimum and maximum of two values.  Use lower case to avoid
963    naming clashes with standard include files.  */
964 #define max(a, b) ((a) > (b) ? (a) : (b))
965 #define min(a, b) ((a) < (b) ? (a) : (b))
966 
967 /* Max and min values of the integer type T, of which only the bottom
968    B bits are used, and where the highest-order used bit is considered
969    to be a sign bit if T is signed.  */
970 #define MAXVAL(t, b)						\
971   ((t) (((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))			\
972 	- 1 + ((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))))
973 #define MINVAL(t, b)						\
974   ((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
975 
976 /* The extreme time values, assuming no padding.  */
977 #define TIME_T_MIN_NO_PADDING MINVAL(time_t, TYPE_BIT(time_t))
978 #define TIME_T_MAX_NO_PADDING MAXVAL(time_t, TYPE_BIT(time_t))
979 
980 /* The extreme time values.  These are macros, not constants, so that
981    any portability problems occur only when compiling .c files that use
982    the macros, which is safer for applications that need only zdump and zic.
983    This implementation assumes no padding if time_t is signed and
984    either the compiler lacks support for _Generic or time_t is not one
985    of the standard signed integer types.  */
986 #if HAVE__GENERIC
987 # define TIME_T_MIN \
988     _Generic((time_t) 0, \
989 	     signed char: SCHAR_MIN, short: SHRT_MIN, \
990 	     int: INT_MIN, long: LONG_MIN, long long: LLONG_MIN, \
991 	     default: TIME_T_MIN_NO_PADDING)
992 # define TIME_T_MAX \
993     (TYPE_SIGNED(time_t) \
994      ? _Generic((time_t) 0, \
995 		signed char: SCHAR_MAX, short: SHRT_MAX, \
996 		int: INT_MAX, long: LONG_MAX, long long: LLONG_MAX, \
997 		default: TIME_T_MAX_NO_PADDING)			    \
998      : (time_t) -1)
999 enum { SIGNED_PADDING_CHECK_NEEDED
1000 	 = _Generic((time_t) 0,
1001 		    signed char: false, short: false,
1002 		    int: false, long: false, long long: false,
1003 		    default: true) };
1004 #else
1005 # define TIME_T_MIN TIME_T_MIN_NO_PADDING
1006 # define TIME_T_MAX TIME_T_MAX_NO_PADDING
1007 enum { SIGNED_PADDING_CHECK_NEEDED = true };
1008 #endif
1009 /* Try to check the padding assumptions.  Although TIME_T_MAX and the
1010    following check can both have undefined behavior on oddball
1011    platforms due to shifts exceeding widths of signed integers, these
1012    platforms' compilers are likely to diagnose these issues in integer
1013    constant expressions, so it shouldn't hurt to check statically.  */
1014 static_assert(! TYPE_SIGNED(time_t) || ! SIGNED_PADDING_CHECK_NEEDED
1015 	      || TIME_T_MAX >> (TYPE_BIT(time_t) - 2) == 1);
1016 
1017 /*
1018 ** 302 / 1000 is log10(2.0) rounded up.
1019 ** Subtract one for the sign bit if the type is signed;
1020 ** add one for integer division truncation;
1021 ** add one more for a minus sign if the type is signed.
1022 */
1023 #define INT_STRLEN_MAXIMUM(type) \
1024 	((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
1025 	1 + TYPE_SIGNED(type))
1026 
1027 /*
1028 ** INITIALIZE(x)
1029 */
1030 
1031 #ifdef GCC_LINT
1032 # define INITIALIZE(x)	((x) = 0)
1033 #else
1034 # define INITIALIZE(x)
1035 #endif
1036 
1037 /* Whether memory access must strictly follow the C standard.
1038    If 0, it's OK to read uninitialized storage so long as the value is
1039    not relied upon.  Defining it to 0 lets mktime access parts of
1040    struct tm that might be uninitialized, as a heuristic when the
1041    standard doesn't say what to return and when tm_gmtoff can help
1042    mktime likely infer a better value.  */
1043 #ifndef UNINIT_TRAP
1044 # define UNINIT_TRAP 0
1045 #endif
1046 
1047 /* strftime.c sometimes needs access to timeoff if it is not already public.
1048    tz_private_timeoff should be used only by localtime.c and strftime.c.  */
1049 #if (!defined EXTERN_TIMEOFF \
1050      && defined TM_GMTOFF && (200809 < _POSIX_VERSION || ! UNINIT_TRAP))
1051 # ifndef timeoff
1052 #  define timeoff tz_private_timeoff
1053 # endif
1054 # define EXTERN_TIMEOFF
1055 #endif
1056 #ifdef EXTERN_TIMEOFF
1057 time_t timeoff(struct tm *, long);
1058 #endif
1059 
1060 #ifdef DEBUG
1061 # undef unreachable
1062 # define unreachable() abort()
1063 #elif !defined unreachable
1064 # ifdef __has_builtin
1065 #  if __has_builtin(__builtin_unreachable)
1066 #   define unreachable() __builtin_unreachable()
1067 #  endif
1068 # elif 4 < __GNUC__ + (5 <= __GNUC_MINOR__)
1069 #  define unreachable() __builtin_unreachable()
1070 # endif
1071 # ifndef unreachable
1072 #  define unreachable() ((void) 0)
1073 # endif
1074 #endif
1075 
1076 /*
1077 ** For the benefit of GNU folk...
1078 ** '_(MSGID)' uses the current locale's message library string for MSGID.
1079 ** The default is to use gettext if available, and use MSGID otherwise.
1080 */
1081 
1082 #ifndef _
1083 # if HAVE_GETTEXT
1084 #  define _(msgid) gettext(msgid)
1085 # else
1086 #  define _(msgid) (msgid)
1087 # endif
1088 #endif
1089 #ifndef N_
1090 # define N_(msgid) (msgid)
1091 #endif
1092 
1093 #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
1094 # define TZ_DOMAIN "tz"
1095 #endif
1096 
1097 #if HAVE_INCOMPATIBLE_CTIME_R
1098 # undef asctime_r
1099 # undef ctime_r
1100 char *asctime_r(struct tm const *restrict, char *restrict);
1101 char *ctime_r(time_t const *, char *);
1102 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
1103 
1104 /* Handy constants that are independent of tzfile implementation.  */
1105 
1106 /* 2**31 - 1 as a signed integer, and usable in #if.  */
1107 #define TWO_31_MINUS_1 2147483647
1108 
1109 enum {
1110   SECSPERMIN = 60,
1111   MINSPERHOUR = 60,
1112   SECSPERHOUR = SECSPERMIN * MINSPERHOUR,
1113   HOURSPERDAY = 24,
1114   DAYSPERWEEK = 7,
1115   DAYSPERNYEAR = 365,
1116   DAYSPERLYEAR = DAYSPERNYEAR + 1,
1117   MONSPERYEAR = 12,
1118   YEARSPERREPEAT = 400	/* years before a Gregorian repeat */
1119 };
1120 
1121 #define SECSPERDAY	((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
1122 
1123 #define DAYSPERREPEAT		((int_fast32_t) 400 * 365 + 100 - 4 + 1)
1124 #define SECSPERREPEAT		((int_fast64_t) DAYSPERREPEAT * SECSPERDAY)
1125 #define AVGSECSPERYEAR		(SECSPERREPEAT / YEARSPERREPEAT)
1126 
1127 /* How many years to generate (in zic.c) or search through (in localtime.c).
1128    This is two years larger than the obvious 400, to avoid edge cases.
1129    E.g., suppose a rule applies from 2012 on with transitions
1130    in March and September, plus one-off transitions in November 2013,
1131    and suppose the rule cannot be expressed as a proleptic TZ string.
1132    If zic looked only at the last 400 years, it would set max_year=2413,
1133    with the intent that the 400 years 2014 through 2413 will be repeated.
1134    The last transition listed in the tzfile would be in 2413-09,
1135    less than 400 years after the last one-off transition in 2013-11.
1136    Two years is not overkill for localtime.c, as a one-year bump
1137    would mishandle 2023d's America/Ciudad_Juarez for November 2422.  */
1138 enum { years_of_observations = YEARSPERREPEAT + 2 };
1139 
1140 enum {
1141   TM_SUNDAY,
1142   TM_MONDAY,
1143   TM_TUESDAY,
1144   TM_WEDNESDAY,
1145   TM_THURSDAY,
1146   TM_FRIDAY,
1147   TM_SATURDAY
1148 };
1149 
1150 enum {
1151   TM_JANUARY,
1152   TM_FEBRUARY,
1153   TM_MARCH,
1154   TM_APRIL,
1155   TM_MAY,
1156   TM_JUNE,
1157   TM_JULY,
1158   TM_AUGUST,
1159   TM_SEPTEMBER,
1160   TM_OCTOBER,
1161   TM_NOVEMBER,
1162   TM_DECEMBER
1163 };
1164 
1165 enum {
1166   TM_YEAR_BASE = 1900,
1167   TM_WDAY_BASE = TM_MONDAY,
1168   EPOCH_YEAR = 1970,
1169   EPOCH_WDAY = TM_THURSDAY
1170 };
1171 
1172 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
1173 
1174 /*
1175 ** Since everything in isleap is modulo 400 (or a factor of 400), we know that
1176 **	isleap(y) == isleap(y % 400)
1177 ** and so
1178 **	isleap(a + b) == isleap((a + b) % 400)
1179 ** or
1180 **	isleap(a + b) == isleap(a % 400 + b % 400)
1181 ** This is true even if % means modulo rather than Fortran remainder
1182 ** (which is allowed by C89 but not by C99 or later).
1183 ** We use this to avoid addition overflow problems.
1184 */
1185 
1186 #define isleap_sum(a, b)	isleap((a) % 400 + (b) % 400)
1187 
1188 #endif /* !defined PRIVATE_H */
1189