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