Home | History | Annotate | Line # | Download | only in time
private.h revision 1.33.2.1
      1 /*	$NetBSD: private.h,v 1.33.2.1 2015/01/25 09:11:03 martin 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 # undef  ctime
    328 # define ctime tz_ctime
    329 # undef  ctime_r
    330 # define ctime_r tz_ctime_r
    331 # undef  difftime
    332 # define difftime tz_difftime
    333 # undef  gmtime
    334 # define gmtime tz_gmtime
    335 # undef  gmtime_r
    336 # define gmtime_r tz_gmtime_r
    337 # undef  localtime
    338 # define localtime tz_localtime
    339 # undef  localtime_r
    340 # define localtime_r tz_localtime_r
    341 # undef  mktime
    342 # define mktime tz_mktime
    343 # undef  offtime
    344 # define offtime tz_offtime
    345 # undef  posix2time
    346 # define posix2time tz_posix2time
    347 # undef  time
    348 # define time tz_time
    349 # undef  time2posix
    350 # define time2posix tz_time2posix
    351 # undef  time_t
    352 # define time_t tz_time_t
    353 # undef  timegm
    354 # define timegm tz_timegm
    355 # undef  timelocal
    356 # define timelocal tz_timelocal
    357 # undef  timeoff
    358 # define timeoff tz_timeoff
    359 
    360 typedef time_tz time_t;
    361 
    362 char *ctime(time_t const *);
    363 char *ctime_r(time_t const *, char *);
    364 double difftime(time_t, time_t);
    365 struct tm *gmtime(time_t const *);
    366 struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
    367 struct tm *localtime(time_t const *);
    368 struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
    369 time_t mktime(struct tm *);
    370 time_t time(time_t *);
    371 #endif
    372 
    373 /*
    374 ** Some time.h implementations don't declare asctime_r.
    375 ** Others might define it as a macro.
    376 ** Fix the former without affecting the latter.
    377 */
    378 
    379 #ifndef asctime_r
    380 extern char *	asctime_r(struct tm const *restrict, char *restrict);
    381 #endif
    382 
    383 /*
    384 ** The STD_INSPIRED functions are similar, but most also need
    385 ** declarations if time_tz is defined.
    386 */
    387 
    388 #ifdef STD_INSPIRED
    389 # if !defined tzsetwall
    390 void tzsetwall(void);
    391 # endif
    392 # if !defined offtime || defined time_tz
    393 struct tm *offtime(time_t const *, long);
    394 # endif
    395 # if !defined timegm || defined time_tz
    396 time_t timegm(struct tm *);
    397 # endif
    398 # if !defined timelocal || defined time_tz
    399 time_t timelocal(struct tm *);
    400 # endif
    401 # if !defined timeoff || defined time_tz
    402 time_t timeoff(struct tm *, long);
    403 # endif
    404 # if !defined time2posix || defined time_tz
    405 time_t time2posix(time_t);
    406 # endif
    407 # if !defined posix2time || defined time_tz
    408 time_t posix2time(time_t);
    409 # endif
    410 #endif
    411 
    412 /* Infer TM_ZONE on systems where this information is known, but suppress
    413    guessing if NO_TM_ZONE is defined.  Similarly for TM_GMTOFF.  */
    414 #if (defined __GLIBC__ \
    415      || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
    416      || (defined __APPLE__ && defined __MACH__))
    417 # if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
    418 #  define TM_GMTOFF tm_gmtoff
    419 # endif
    420 # if !defined TM_ZONE && !defined NO_TM_ZONE
    421 #  define TM_ZONE tm_zone
    422 # endif
    423 #endif
    424 
    425 /*
    426 ** Define functions that are ABI compatible with NetBSD but have
    427 ** better prototypes.  NetBSD 6.1.4 defines a pointer type timezone_t
    428 ** and labors under the misconception that 'const timezone_t' is a
    429 ** pointer to a constant.  This use of 'const' is ineffective, so it
    430 ** is not done here.  What we call 'struct state' NetBSD calls
    431 ** 'struct __state', but this is a private name so it doesn't matter.
    432 */
    433 #ifndef __NetBSD__
    434 #if NETBSD_INSPIRED
    435 typedef struct state *timezone_t;
    436 struct tm *localtime_rz(timezone_t restrict, time_t const *restrict,
    437 			struct tm *restrict);
    438 time_t mktime_z(timezone_t restrict, struct tm *restrict);
    439 timezone_t tzalloc(char const *);
    440 void tzfree(timezone_t);
    441 # ifdef STD_INSPIRED
    442 #  if !defined posix2time_z || defined time_tz
    443 time_t posix2time_z(timezone_t __restrict, time_t) ATTRIBUTE_PURE;
    444 #  endif
    445 #  if !defined time2posix_z || defined time_tz
    446 time_t time2posix_z(timezone_t __restrict, time_t) ATTRIBUTE_PURE;
    447 #  endif
    448 # endif
    449 #endif
    450 #endif
    451 
    452 /*
    453 ** Private function declarations.
    454 */
    455 
    456 char *		icatalloc(char * old, const char * new);
    457 char *		icpyalloc(const char * string);
    458 const char *	scheck(const char * string, const char * format);
    459 
    460 /*
    461 ** Finally, some convenience items.
    462 */
    463 
    464 #if __STDC_VERSION__ < 199901
    465 # define true 1
    466 # define false 0
    467 # define bool int
    468 #else
    469 # include <stdbool.h>
    470 #endif
    471 
    472 #ifndef TYPE_BIT
    473 #define TYPE_BIT(type)	(sizeof (type) * CHAR_BIT)
    474 #endif /* !defined TYPE_BIT */
    475 
    476 #ifndef TYPE_SIGNED
    477 #define TYPE_SIGNED(type) (/*CONSTCOND*/((type) -1) < 0)
    478 #endif /* !defined TYPE_SIGNED */
    479 
    480 #ifdef LOCALTIME_IMPLEMENTATION
    481 /* The minimum and maximum finite time values.  */
    482 static time_t const time_t_min =
    483   (TYPE_SIGNED(time_t)
    484    ? (time_t) -1 << (int)(CHAR_BIT * sizeof (time_t) - 1)
    485    : 0);
    486 static time_t const time_t_max =
    487   (TYPE_SIGNED(time_t)
    488    ? - (~ 0 < 0) - ((time_t) -1 << (int)(CHAR_BIT * sizeof (time_t) - 1))
    489    : -1);
    490 #endif
    491 
    492 #ifndef INT_STRLEN_MAXIMUM
    493 /*
    494 ** 302 / 1000 is log10(2.0) rounded up.
    495 ** Subtract one for the sign bit if the type is signed;
    496 ** add one for integer division truncation;
    497 ** add one more for a minus sign if the type is signed.
    498 */
    499 #define INT_STRLEN_MAXIMUM(type) \
    500 	((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
    501 	1 + TYPE_SIGNED(type))
    502 #endif /* !defined INT_STRLEN_MAXIMUM */
    503 
    504 /*
    505 ** INITIALIZE(x)
    506 */
    507 
    508 #if defined(__GNUC__) || defined(__lint__)
    509 # define INITIALIZE(x)	((x) = 0)
    510 #else
    511 # define INITIALIZE(x)
    512 #endif
    513 
    514 #ifndef UNINIT_TRAP
    515 # define UNINIT_TRAP 0
    516 #endif
    517 
    518 /*
    519 ** For the benefit of GNU folk...
    520 ** '_(MSGID)' uses the current locale's message library string for MSGID.
    521 ** The default is to use gettext if available, and use MSGID otherwise.
    522 */
    523 
    524 #ifndef _
    525 #if HAVE_GETTEXT
    526 #define _(msgid) gettext(msgid)
    527 #else /* !HAVE_GETTEXT */
    528 #define _(msgid) msgid
    529 #endif /* !HAVE_GETTEXT */
    530 #endif /* !defined _ */
    531 
    532 #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
    533 # define TZ_DOMAIN "tz"
    534 #endif
    535 
    536 #if HAVE_INCOMPATIBLE_CTIME_R
    537 #undef asctime_r
    538 #undef ctime_r
    539 char *asctime_r(struct tm const *, char *);
    540 char *ctime_r(time_t const *, char *);
    541 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
    542 
    543 #ifndef YEARSPERREPEAT
    544 #define YEARSPERREPEAT		400	/* years before a Gregorian repeat */
    545 #endif /* !defined YEARSPERREPEAT */
    546 
    547 /*
    548 ** The Gregorian year averages 365.2425 days, which is 31556952 seconds.
    549 */
    550 
    551 #ifndef AVGSECSPERYEAR
    552 #define AVGSECSPERYEAR		31556952L
    553 #endif /* !defined AVGSECSPERYEAR */
    554 
    555 #ifndef SECSPERREPEAT
    556 #define SECSPERREPEAT		((int_fast64_t) YEARSPERREPEAT * (int_fast64_t) AVGSECSPERYEAR)
    557 #endif /* !defined SECSPERREPEAT */
    558 
    559 #ifndef SECSPERREPEAT_BITS
    560 #define SECSPERREPEAT_BITS	34	/* ceil(log2(SECSPERREPEAT)) */
    561 #endif /* !defined SECSPERREPEAT_BITS */
    562 
    563 #endif /* !defined PRIVATE_H */
    564