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