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