Home | History | Annotate | Line # | Download | only in time
private.h revision 1.37
      1 /*	$NetBSD: private.h,v 1.37 2014/10/07 22:20:33 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 "errno.h"
    121 #include "string.h"
    122 #include "limits.h"	/* for CHAR_BIT et al. */
    123 #include "stdlib.h"
    124 
    125 #if HAVE_GETTEXT
    126 #include "libintl.h"
    127 #endif /* HAVE_GETTEXT */
    128 
    129 #if HAVE_SYS_WAIT_H
    130 #include <sys/wait.h>	/* for WIFEXITED and WEXITSTATUS */
    131 #endif /* HAVE_SYS_WAIT_H */
    132 
    133 #ifndef WIFEXITED
    134 #define WIFEXITED(status)	(((status) & 0xff) == 0)
    135 #endif /* !defined WIFEXITED */
    136 #ifndef WEXITSTATUS
    137 #define WEXITSTATUS(status)	(((status) >> 8) & 0xff)
    138 #endif /* !defined WEXITSTATUS */
    139 
    140 #if HAVE_UNISTD_H
    141 #include "unistd.h"	/* for F_OK, R_OK, and other POSIX goodness */
    142 #endif /* HAVE_UNISTD_H */
    143 
    144 #ifndef HAVE_STRFTIME_L
    145 # if _POSIX_VERSION < 200809
    146 #  define HAVE_STRFTIME_L 0
    147 # else
    148 #  define HAVE_STRFTIME_L 1
    149 # endif
    150 #endif
    151 
    152 #ifndef F_OK
    153 #define F_OK	0
    154 #endif /* !defined F_OK */
    155 #ifndef R_OK
    156 #define R_OK	4
    157 #endif /* !defined R_OK */
    158 
    159 /* Unlike <ctype.h>'s isdigit, this also works if c < 0 | c > UCHAR_MAX. */
    160 #define is_digit(c) ((unsigned)(c) - '0' <= 9)
    161 
    162 /*
    163 ** Define HAVE_STDINT_H's default value here, rather than at the
    164 ** start, since __GLIBC__'s value depends on previously-included
    165 ** files.
    166 ** (glibc 2.1 and later have stdint.h, even with pre-C99 compilers.)
    167 */
    168 #ifndef HAVE_STDINT_H
    169 #define HAVE_STDINT_H \
    170    (199901 <= __STDC_VERSION__ \
    171     || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__)	\
    172     || __CYGWIN__)
    173 #endif /* !defined HAVE_STDINT_H */
    174 
    175 #if HAVE_STDINT_H
    176 #include "stdint.h"
    177 #endif /* !HAVE_STDINT_H */
    178 
    179 #ifndef HAVE_INTTYPES_H
    180 # define HAVE_INTTYPES_H HAVE_STDINT_H
    181 #endif
    182 #if HAVE_INTTYPES_H
    183 # include <inttypes.h>
    184 #endif
    185 
    186 /* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX.  */
    187 #ifdef __LONG_LONG_MAX__
    188 # ifndef LLONG_MAX
    189 #  define LLONG_MAX __LONG_LONG_MAX__
    190 # endif
    191 # ifndef LLONG_MIN
    192 #  define LLONG_MIN (-1 - LLONG_MAX)
    193 # endif
    194 #endif
    195 
    196 #ifndef INT_FAST64_MAX
    197 # ifdef LLONG_MAX
    198 typedef long long	int_fast64_t;
    199 #  define INT_FAST64_MIN LLONG_MIN
    200 #  define INT_FAST64_MAX LLONG_MAX
    201 # else
    202 #  if (LONG_MAX >> 31) < 0xffffffff
    203 Please use a compiler that supports a 64-bit integer type (or wider);
    204 you may need to compile with "-DHAVE_STDINT_H".
    205 #  endif
    206 typedef long		int_fast64_t;
    207 #  define INT_FAST64_MIN LONG_MIN
    208 #  define INT_FAST64_MAX LONG_MAX
    209 # endif
    210 #endif
    211 
    212 #ifndef SCNdFAST64
    213 # if INT_FAST64_MAX == LLONG_MAX
    214 #  define SCNdFAST64 "lld"
    215 # else
    216 #  define SCNdFAST64 "ld"
    217 # endif
    218 #endif
    219 
    220 #ifndef INT_FAST32_MAX
    221 # if INT_MAX >> 31 == 0
    222 typedef long int_fast32_t;
    223 #  define INT_FAST32_MAX LONG_MAX
    224 #  define INT_FAST32_MIN LONG_MIN
    225 # else
    226 typedef int int_fast32_t;
    227 #  define INT_FAST32_MAX INT_MAX
    228 #  define INT_FAST32_MIN INT_MIN
    229 # endif
    230 #endif
    231 
    232 #ifndef INTMAX_MAX
    233 # ifdef LLONG_MAX
    234 typedef long long intmax_t;
    235 #  define strtoimax strtoll
    236 #  define INTMAX_MAX LLONG_MAX
    237 #  define INTMAX_MIN LLONG_MIN
    238 # else
    239 typedef long intmax_t;
    240 #  define strtoimax strtol
    241 #  define INTMAX_MAX LONG_MAX
    242 #  define INTMAX_MIN LONG_MIN
    243 # endif
    244 #endif
    245 
    246 #ifndef PRIdMAX
    247 # if INTMAX_MAX == LLONG_MAX
    248 #  define PRIdMAX "lld"
    249 # else
    250 #  define PRIdMAX "ld"
    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 # undef  ctime
    320 # define ctime tz_ctime
    321 # undef  ctime_r
    322 # define ctime_r tz_ctime_r
    323 # undef  difftime
    324 # define difftime tz_difftime
    325 # undef  gmtime
    326 # define gmtime tz_gmtime
    327 # undef  gmtime_r
    328 # define gmtime_r tz_gmtime_r
    329 # undef  localtime
    330 # define localtime tz_localtime
    331 # undef  localtime_r
    332 # define localtime_r tz_localtime_r
    333 # undef  mktime
    334 # define mktime tz_mktime
    335 # undef  offtime
    336 # define offtime tz_offtime
    337 # undef  posix2time
    338 # define posix2time tz_posix2time
    339 # undef  time
    340 # define time tz_time
    341 # undef  time2posix
    342 # define time2posix tz_time2posix
    343 # undef  time_t
    344 # define time_t tz_time_t
    345 # undef  timegm
    346 # define timegm tz_timegm
    347 # undef  timelocal
    348 # define timelocal tz_timelocal
    349 # undef  timeoff
    350 # define timeoff tz_timeoff
    351 
    352 typedef time_tz time_t;
    353 
    354 char *ctime(time_t const *);
    355 char *ctime_r(time_t const *, char *);
    356 double difftime(time_t, time_t);
    357 struct tm *gmtime(time_t const *);
    358 struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
    359 struct tm *localtime(time_t const *);
    360 struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
    361 time_t mktime(struct tm *);
    362 time_t time(time_t *);
    363 #endif
    364 
    365 /*
    366 ** Some time.h implementations don't declare asctime_r.
    367 ** Others might define it as a macro.
    368 ** Fix the former without affecting the latter.
    369 */
    370 
    371 #ifndef asctime_r
    372 extern char *	asctime_r(struct tm const *restrict, char *restrict);
    373 #endif
    374 
    375 /*
    376 ** The STD_INSPIRED functions are similar, but most also need
    377 ** declarations if time_tz is defined.
    378 */
    379 
    380 #ifdef STD_INSPIRED
    381 # if !defined tzsetwall
    382 void tzsetwall(void);
    383 # endif
    384 # if !defined offtime || defined time_tz
    385 struct tm *offtime(time_t const *, long);
    386 # endif
    387 # if !defined timegm || defined time_tz
    388 time_t timegm(struct tm *);
    389 # endif
    390 # if !defined timelocal || defined time_tz
    391 time_t timelocal(struct tm *);
    392 # endif
    393 # if !defined timeoff || defined time_tz
    394 time_t timeoff(struct tm *, long);
    395 # endif
    396 # if !defined time2posix || defined time_tz
    397 time_t time2posix(time_t);
    398 # endif
    399 # if !defined posix2time || defined time_tz
    400 time_t posix2time(time_t);
    401 # endif
    402 #endif
    403 
    404 /* Infer TM_ZONE on systems where this information is known, but suppress
    405    guessing if NO_TM_ZONE is defined.  Similarly for TM_GMTOFF.  */
    406 #if (defined __GLIBC__ \
    407      || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
    408      || (defined __APPLE__ && defined __MACH__))
    409 # if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
    410 #  define TM_GMTOFF tm_gmtoff
    411 # endif
    412 # if !defined TM_ZONE && !defined NO_TM_ZONE
    413 #  define TM_ZONE tm_zone
    414 # endif
    415 #endif
    416 
    417 /*
    418 ** Define functions that are ABI compatible with NetBSD but have
    419 ** better prototypes.  NetBSD 6.1.4 defines a pointer type timezone_t
    420 ** and labors under the misconception that 'const timezone_t' is a
    421 ** pointer to a constant.  This use of 'const' is ineffective, so it
    422 ** is not done here.  What we call 'struct state' NetBSD calls
    423 ** 'struct __state', but this is a private name so it doesn't matter.
    424 */
    425 #ifndef __NetBSD__
    426 #if NETBSD_INSPIRED
    427 typedef struct state *timezone_t;
    428 struct tm *localtime_rz(timezone_t restrict, time_t const *restrict,
    429 			struct tm *restrict);
    430 time_t mktime_z(timezone_t restrict, struct tm *restrict);
    431 timezone_t tzalloc(char const *);
    432 void tzfree(timezone_t);
    433 # ifdef STD_INSPIRED
    434 #  if !defined posix2time_z || defined time_tz
    435 time_t posix2time_z(timezone_t __restrict, time_t) ATTRIBUTE_PURE;
    436 #  endif
    437 #  if !defined time2posix_z || defined time_tz
    438 time_t time2posix_z(timezone_t __restrict, time_t) ATTRIBUTE_PURE;
    439 #  endif
    440 # endif
    441 #endif
    442 #endif
    443 
    444 /*
    445 ** Private function declarations.
    446 */
    447 
    448 char *		icatalloc(char * old, const char * new);
    449 char *		icpyalloc(const char * string);
    450 const char *	scheck(const char * string, const char * format);
    451 
    452 /*
    453 ** Finally, some convenience items.
    454 */
    455 
    456 #if __STDC_VERSION__ < 199901
    457 # define true 1
    458 # define false 0
    459 # define bool int
    460 #else
    461 # include <stdbool.h>
    462 #endif
    463 
    464 #ifndef TYPE_BIT
    465 #define TYPE_BIT(type)	(sizeof (type) * CHAR_BIT)
    466 #endif /* !defined TYPE_BIT */
    467 
    468 #ifndef TYPE_SIGNED
    469 #define TYPE_SIGNED(type) (/*CONSTCOND*/((type) -1) < 0)
    470 #endif /* !defined TYPE_SIGNED */
    471 
    472 #ifdef LOCALTIME_IMPLEMENTATION
    473 /* The minimum and maximum finite time values.  */
    474 static time_t const time_t_min =
    475   (TYPE_SIGNED(time_t)
    476    ? (time_t) -1 << (int)(CHAR_BIT * sizeof (time_t) - 1)
    477    : 0);
    478 static time_t const time_t_max =
    479   (TYPE_SIGNED(time_t)
    480    ? - (~ 0 < 0) - ((time_t) -1 << (int)(CHAR_BIT * sizeof (time_t) - 1))
    481    : -1);
    482 #endif
    483 
    484 #ifndef INT_STRLEN_MAXIMUM
    485 /*
    486 ** 302 / 1000 is log10(2.0) rounded up.
    487 ** Subtract one for the sign bit if the type is signed;
    488 ** add one for integer division truncation;
    489 ** add one more for a minus sign if the type is signed.
    490 */
    491 #define INT_STRLEN_MAXIMUM(type) \
    492 	((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
    493 	1 + TYPE_SIGNED(type))
    494 #endif /* !defined INT_STRLEN_MAXIMUM */
    495 
    496 /*
    497 ** INITIALIZE(x)
    498 */
    499 
    500 #if defined(__GNUC__) || defined(__lint__)
    501 # define INITIALIZE(x)	((x) = 0)
    502 #else
    503 # define INITIALIZE(x)
    504 #endif
    505 
    506 #ifndef UNINIT_TRAP
    507 # define UNINIT_TRAP 0
    508 #endif
    509 
    510 /*
    511 ** For the benefit of GNU folk...
    512 ** '_(MSGID)' uses the current locale's message library string for MSGID.
    513 ** The default is to use gettext if available, and use MSGID otherwise.
    514 */
    515 
    516 #ifndef _
    517 #if HAVE_GETTEXT
    518 #define _(msgid) gettext(msgid)
    519 #else /* !HAVE_GETTEXT */
    520 #define _(msgid) msgid
    521 #endif /* !HAVE_GETTEXT */
    522 #endif /* !defined _ */
    523 
    524 #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
    525 # define TZ_DOMAIN "tz"
    526 #endif
    527 
    528 #if HAVE_INCOMPATIBLE_CTIME_R
    529 #undef asctime_r
    530 #undef ctime_r
    531 char *asctime_r(struct tm const *, char *);
    532 char *ctime_r(time_t const *, char *);
    533 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
    534 
    535 #ifndef YEARSPERREPEAT
    536 #define YEARSPERREPEAT		400	/* years before a Gregorian repeat */
    537 #endif /* !defined YEARSPERREPEAT */
    538 
    539 /*
    540 ** The Gregorian year averages 365.2425 days, which is 31556952 seconds.
    541 */
    542 
    543 #ifndef AVGSECSPERYEAR
    544 #define AVGSECSPERYEAR		31556952L
    545 #endif /* !defined AVGSECSPERYEAR */
    546 
    547 #ifndef SECSPERREPEAT
    548 #define SECSPERREPEAT		((int_fast64_t) YEARSPERREPEAT * (int_fast64_t) AVGSECSPERYEAR)
    549 #endif /* !defined SECSPERREPEAT */
    550 
    551 #ifndef SECSPERREPEAT_BITS
    552 #define SECSPERREPEAT_BITS	34	/* ceil(log2(SECSPERREPEAT)) */
    553 #endif /* !defined SECSPERREPEAT_BITS */
    554 
    555 #endif /* !defined PRIVATE_H */
    556