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