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