Home | History | Annotate | Line # | Download | only in time
private.h revision 1.34
      1  1.34  christos /*	$NetBSD: private.h,v 1.34 2014/08/15 11:04:07 christos Exp $	*/
      2   1.2       jtc 
      3   1.1       jtc #ifndef PRIVATE_H
      4   1.1       jtc #define PRIVATE_H
      5   1.3       jtc 
      6   1.3       jtc /* NetBSD defaults */
      7   1.3       jtc #define TM_GMTOFF	tm_gmtoff
      8   1.3       jtc #define TM_ZONE		tm_zone
      9   1.3       jtc #define STD_INSPIRED	1
     10   1.3       jtc #define HAVE_LONG_DOUBLE 1
     11  1.21     bjh21 
     12  1.21     bjh21 /* For when we build zic as a host tool. */
     13  1.23     lukem #if HAVE_NBTOOL_CONFIG_H
     14  1.23     lukem #include "nbtool_config.h"
     15  1.21     bjh21 #endif
     16   1.1       jtc 
     17   1.1       jtc /*
     18   1.6       jtc ** This file is in the public domain, so clarified as of
     19  1.25   mlelstv ** 1996-06-05 by Arthur David Olson.
     20   1.6       jtc */
     21   1.6       jtc 
     22   1.6       jtc /*
     23   1.1       jtc ** This header is for use ONLY with the time conversion code.
     24   1.1       jtc ** There is no guarantee that it will remain unchanged,
     25   1.1       jtc ** or that it will remain at all.
     26   1.1       jtc ** Do NOT copy it to any system include directory.
     27   1.1       jtc ** Thank you!
     28   1.1       jtc */
     29   1.1       jtc 
     30  1.25   mlelstv #define GRANDPARENTED	"Local time zone must be set--see zic manual page"
     31  1.25   mlelstv 
     32   1.1       jtc /*
     33   1.1       jtc ** Defaults for preprocessor symbols.
     34  1.34  christos ** You can override these in your C compiler options, e.g. '-DHAVE_ADJTIME=0'.
     35   1.1       jtc */
     36   1.1       jtc 
     37   1.1       jtc #ifndef HAVE_ADJTIME
     38   1.1       jtc #define HAVE_ADJTIME		1
     39   1.1       jtc #endif /* !defined HAVE_ADJTIME */
     40   1.1       jtc 
     41   1.6       jtc #ifndef HAVE_GETTEXT
     42   1.6       jtc #define HAVE_GETTEXT		0
     43   1.6       jtc #endif /* !defined HAVE_GETTEXT */
     44   1.6       jtc 
     45  1.17    kleink #ifndef HAVE_INCOMPATIBLE_CTIME_R
     46  1.17    kleink #define HAVE_INCOMPATIBLE_CTIME_R	0
     47  1.17    kleink #endif /* !defined INCOMPATIBLE_CTIME_R */
     48  1.17    kleink 
     49  1.32  christos #ifndef HAVE_LINK
     50  1.32  christos #define HAVE_LINK		1
     51  1.32  christos #endif /* !defined HAVE_LINK */
     52  1.32  christos 
     53   1.1       jtc #ifndef HAVE_SETTIMEOFDAY
     54   1.1       jtc #define HAVE_SETTIMEOFDAY	3
     55   1.1       jtc #endif /* !defined HAVE_SETTIMEOFDAY */
     56   1.1       jtc 
     57  1.12    kleink #ifndef HAVE_SYMLINK
     58  1.12    kleink #define HAVE_SYMLINK		1
     59  1.12    kleink #endif /* !defined HAVE_SYMLINK */
     60   1.6       jtc 
     61  1.20    kleink #ifndef HAVE_SYS_STAT_H
     62  1.20    kleink #define HAVE_SYS_STAT_H		1
     63  1.20    kleink #endif /* !defined HAVE_SYS_STAT_H */
     64  1.20    kleink 
     65  1.14    kleink #ifndef HAVE_SYS_WAIT_H
     66  1.14    kleink #define HAVE_SYS_WAIT_H		1
     67  1.14    kleink #endif /* !defined HAVE_SYS_WAIT_H */
     68  1.14    kleink 
     69   1.1       jtc #ifndef HAVE_UNISTD_H
     70   1.1       jtc #define HAVE_UNISTD_H		1
     71   1.1       jtc #endif /* !defined HAVE_UNISTD_H */
     72   1.1       jtc 
     73   1.5       jtc #ifndef HAVE_UTMPX_H
     74   1.5       jtc #define HAVE_UTMPX_H		0
     75   1.5       jtc #endif /* !defined HAVE_UTMPX_H */
     76   1.5       jtc 
     77  1.18    kleink #ifdef LOCALE_HOME
     78  1.19       cgd #undef LOCALE_HOME		/* not to be handled by tzcode itself */
     79  1.18    kleink #endif /* defined LOCALE_HOME */
     80   1.1       jtc 
     81  1.17    kleink #if HAVE_INCOMPATIBLE_CTIME_R
     82  1.17    kleink #define asctime_r _incompatible_asctime_r
     83  1.17    kleink #define ctime_r _incompatible_ctime_r
     84  1.17    kleink #endif /* HAVE_INCOMPATIBLE_CTIME_R */
     85  1.17    kleink 
     86   1.1       jtc /*
     87   1.1       jtc ** Nested includes
     88   1.1       jtc */
     89   1.1       jtc 
     90   1.1       jtc #include "sys/types.h"	/* for time_t */
     91   1.1       jtc #include "stdio.h"
     92   1.1       jtc #include "errno.h"
     93   1.1       jtc #include "string.h"
     94  1.25   mlelstv #include "limits.h"	/* for CHAR_BIT et al. */
     95   1.1       jtc #include "time.h"
     96   1.1       jtc #include "stdlib.h"
     97   1.1       jtc 
     98  1.25   mlelstv #if HAVE_GETTEXT
     99   1.6       jtc #include "libintl.h"
    100  1.25   mlelstv #endif /* HAVE_GETTEXT */
    101  1.14    kleink 
    102  1.25   mlelstv #if HAVE_SYS_WAIT_H
    103  1.14    kleink #include <sys/wait.h>	/* for WIFEXITED and WEXITSTATUS */
    104  1.25   mlelstv #endif /* HAVE_SYS_WAIT_H */
    105  1.14    kleink 
    106  1.14    kleink #ifndef WIFEXITED
    107  1.14    kleink #define WIFEXITED(status)	(((status) & 0xff) == 0)
    108  1.14    kleink #endif /* !defined WIFEXITED */
    109  1.14    kleink #ifndef WEXITSTATUS
    110  1.14    kleink #define WEXITSTATUS(status)	(((status) >> 8) & 0xff)
    111  1.14    kleink #endif /* !defined WEXITSTATUS */
    112   1.6       jtc 
    113  1.25   mlelstv #if HAVE_UNISTD_H
    114  1.25   mlelstv #include "unistd.h"	/* for F_OK, R_OK, and other POSIX goodness */
    115  1.25   mlelstv #endif /* HAVE_UNISTD_H */
    116   1.1       jtc 
    117   1.1       jtc #ifndef F_OK
    118   1.1       jtc #define F_OK	0
    119   1.1       jtc #endif /* !defined F_OK */
    120   1.1       jtc #ifndef R_OK
    121   1.1       jtc #define R_OK	4
    122   1.1       jtc #endif /* !defined R_OK */
    123   1.1       jtc 
    124  1.25   mlelstv /* Unlike <ctype.h>'s isdigit, this also works if c < 0 | c > UCHAR_MAX. */
    125   1.4       jtc #define is_digit(c) ((unsigned)(c) - '0' <= 9)
    126   1.4       jtc 
    127   1.1       jtc /*
    128  1.25   mlelstv ** Define HAVE_STDINT_H's default value here, rather than at the
    129  1.25   mlelstv ** start, since __GLIBC__'s value depends on previously-included
    130  1.25   mlelstv ** files.
    131  1.25   mlelstv ** (glibc 2.1 and later have stdint.h, even with pre-C99 compilers.)
    132  1.25   mlelstv */
    133  1.25   mlelstv #ifndef HAVE_STDINT_H
    134  1.25   mlelstv #define HAVE_STDINT_H \
    135  1.33  christos    (199901 <= __STDC_VERSION__ \
    136  1.33  christos     || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__)	\
    137  1.33  christos     || __CYGWIN__)
    138  1.25   mlelstv #endif /* !defined HAVE_STDINT_H */
    139  1.25   mlelstv 
    140  1.25   mlelstv #if HAVE_STDINT_H
    141  1.25   mlelstv #include "stdint.h"
    142  1.25   mlelstv #endif /* !HAVE_STDINT_H */
    143  1.25   mlelstv 
    144  1.29  christos #ifndef HAVE_INTTYPES_H
    145  1.29  christos # define HAVE_INTTYPES_H HAVE_STDINT_H
    146  1.29  christos #endif
    147  1.29  christos #if HAVE_INTTYPES_H
    148  1.29  christos # include <inttypes.h>
    149  1.29  christos #endif
    150  1.29  christos 
    151  1.25   mlelstv #ifndef INT_FAST64_MAX
    152  1.25   mlelstv /* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX.  */
    153  1.25   mlelstv #if defined LLONG_MAX || defined __LONG_LONG_MAX__
    154  1.25   mlelstv typedef long long	int_fast64_t;
    155  1.29  christos # ifdef LLONG_MAX
    156  1.29  christos #  define INT_FAST64_MIN LLONG_MIN
    157  1.29  christos #  define INT_FAST64_MAX LLONG_MAX
    158  1.29  christos # else
    159  1.29  christos #  define INT_FAST64_MIN __LONG_LONG_MIN__
    160  1.29  christos #  define INT_FAST64_MAX __LONG_LONG_MAX__
    161  1.29  christos # endif
    162  1.29  christos # define SCNdFAST64 "lld"
    163  1.25   mlelstv #else /* ! (defined LLONG_MAX || defined __LONG_LONG_MAX__) */
    164  1.25   mlelstv #if (LONG_MAX >> 31) < 0xffffffff
    165  1.25   mlelstv Please use a compiler that supports a 64-bit integer type (or wider);
    166  1.25   mlelstv you may need to compile with "-DHAVE_STDINT_H".
    167  1.25   mlelstv #endif /* (LONG_MAX >> 31) < 0xffffffff */
    168  1.25   mlelstv typedef long		int_fast64_t;
    169  1.29  christos # define INT_FAST64_MIN LONG_MIN
    170  1.29  christos # define INT_FAST64_MAX LONG_MAX
    171  1.29  christos # define SCNdFAST64 "ld"
    172  1.25   mlelstv #endif /* ! (defined LLONG_MAX || defined __LONG_LONG_MAX__) */
    173  1.25   mlelstv #endif /* !defined INT_FAST64_MAX */
    174  1.25   mlelstv 
    175  1.29  christos #ifndef INT_FAST32_MAX
    176  1.29  christos # if INT_MAX >> 31 == 0
    177  1.29  christos typedef long int_fast32_t;
    178  1.29  christos # else
    179  1.29  christos typedef int int_fast32_t;
    180  1.29  christos # endif
    181  1.29  christos #endif
    182  1.29  christos 
    183  1.29  christos #ifndef INTMAX_MAX
    184  1.29  christos # if defined LLONG_MAX || defined __LONG_LONG_MAX__
    185  1.29  christos typedef long long intmax_t;
    186  1.30  christos #  define strtoimax strtoll
    187  1.29  christos #  define PRIdMAX "lld"
    188  1.30  christos #  ifdef LLONG_MAX
    189  1.30  christos #   define INTMAX_MAX LLONG_MAX
    190  1.30  christos #   define INTMAX_MIN LLONG_MIN
    191  1.30  christos #  else
    192  1.30  christos #   define INTMAX_MAX __LONG_LONG_MAX__
    193  1.30  christos #   define INTMAX_MIN __LONG_LONG_MIN__
    194  1.30  christos #  endif
    195  1.29  christos # else
    196  1.29  christos typedef long intmax_t;
    197  1.30  christos #  define strtoimax strtol
    198  1.29  christos #  define PRIdMAX "ld"
    199  1.30  christos #  define INTMAX_MAX LONG_MAX
    200  1.30  christos #  define INTMAX_MIN LONG_MIN
    201  1.29  christos # endif
    202  1.29  christos #endif
    203  1.29  christos 
    204  1.29  christos #ifndef UINTMAX_MAX
    205  1.29  christos # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
    206  1.29  christos typedef unsigned long long uintmax_t;
    207  1.29  christos #  define PRIuMAX "llu"
    208  1.29  christos # else
    209  1.29  christos typedef unsigned long uintmax_t;
    210  1.29  christos #  define PRIuMAX "lu"
    211  1.29  christos # endif
    212  1.29  christos #endif
    213  1.29  christos 
    214  1.25   mlelstv #ifndef INT32_MAX
    215  1.25   mlelstv #define INT32_MAX 0x7fffffff
    216  1.25   mlelstv #endif /* !defined INT32_MAX */
    217  1.25   mlelstv #ifndef INT32_MIN
    218  1.25   mlelstv #define INT32_MIN (-1 - INT32_MAX)
    219  1.25   mlelstv #endif /* !defined INT32_MIN */
    220   1.1       jtc 
    221  1.33  christos #ifndef SIZE_MAX
    222  1.33  christos #define SIZE_MAX ((size_t) -1)
    223  1.33  christos #endif
    224  1.33  christos 
    225  1.29  christos #if 2 < __GNUC__ + (96 <= __GNUC_MINOR__)
    226  1.29  christos # define ATTRIBUTE_CONST __attribute__ ((__const__))
    227  1.29  christos # define ATTRIBUTE_PURE __attribute__ ((__pure__))
    228  1.30  christos # define ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
    229  1.27  christos #else
    230  1.29  christos # define ATTRIBUTE_CONST /* empty */
    231  1.29  christos # define ATTRIBUTE_PURE /* empty */
    232  1.30  christos # define ATTRIBUTE_FORMAT(spec) /* empty */
    233  1.27  christos #endif
    234  1.29  christos 
    235  1.29  christos #if !defined _Noreturn && __STDC_VERSION__ < 201112
    236  1.29  christos # if 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
    237  1.29  christos #  define _Noreturn __attribute__ ((__noreturn__))
    238  1.29  christos # else
    239  1.29  christos #  define _Noreturn
    240  1.29  christos # endif
    241  1.29  christos #endif
    242  1.29  christos 
    243  1.29  christos #if __STDC_VERSION__ < 199901 && !defined restrict
    244  1.29  christos # define restrict /* empty */
    245  1.27  christos #endif
    246  1.27  christos 
    247   1.1       jtc /*
    248  1.25   mlelstv ** Workarounds for compilers/systems.
    249   1.1       jtc */
    250   1.1       jtc 
    251   1.1       jtc /*
    252  1.25   mlelstv ** Some time.h implementations don't declare asctime_r.
    253  1.25   mlelstv ** Others might define it as a macro.
    254  1.25   mlelstv ** Fix the former without affecting the latter.
    255   1.1       jtc */
    256   1.1       jtc 
    257  1.25   mlelstv #ifndef asctime_r
    258  1.25   mlelstv extern char *	asctime_r(struct tm const *, char *);
    259  1.16  christos #endif
    260   1.1       jtc 
    261   1.1       jtc /*
    262  1.29  christos ** Compile with -Dtime_tz=T to build the tz package with a private
    263  1.29  christos ** time_t type equivalent to T rather than the system-supplied time_t.
    264  1.29  christos ** This debugging feature can test unusual design decisions
    265  1.29  christos ** (e.g., time_t wider than 'long', or unsigned time_t) even on
    266  1.29  christos ** typical platforms.
    267  1.29  christos */
    268  1.29  christos #ifdef time_tz
    269  1.29  christos static time_t sys_time(time_t *x) { return time(x); }
    270  1.29  christos 
    271  1.29  christos # undef  ctime
    272  1.29  christos # define ctime tz_ctime
    273  1.29  christos # undef  ctime_r
    274  1.29  christos # define ctime_r tz_ctime_r
    275  1.29  christos # undef  difftime
    276  1.29  christos # define difftime tz_difftime
    277  1.29  christos # undef  gmtime
    278  1.29  christos # define gmtime tz_gmtime
    279  1.29  christos # undef  gmtime_r
    280  1.29  christos # define gmtime_r tz_gmtime_r
    281  1.29  christos # undef  localtime
    282  1.29  christos # define localtime tz_localtime
    283  1.29  christos # undef  localtime_r
    284  1.29  christos # define localtime_r tz_localtime_r
    285  1.29  christos # undef  mktime
    286  1.29  christos # define mktime tz_mktime
    287  1.29  christos # undef  time
    288  1.29  christos # define time tz_time
    289  1.29  christos # undef  time_t
    290  1.29  christos # define time_t tz_time_t
    291  1.29  christos 
    292  1.29  christos typedef time_tz time_t;
    293  1.29  christos 
    294  1.29  christos char *ctime(time_t const *);
    295  1.29  christos char *ctime_r(time_t const *, char *);
    296  1.29  christos double difftime(time_t, time_t);
    297  1.29  christos struct tm *gmtime(time_t const *);
    298  1.29  christos struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
    299  1.29  christos struct tm *localtime(time_t const *);
    300  1.29  christos struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
    301  1.29  christos time_t mktime(struct tm *);
    302  1.29  christos 
    303  1.29  christos static time_t
    304  1.29  christos time(time_t *p)
    305  1.29  christos {
    306  1.29  christos 	time_t r = sys_time(0);
    307  1.29  christos 	if (p)
    308  1.29  christos 		*p = r;
    309  1.29  christos 	return r;
    310  1.29  christos }
    311  1.29  christos #endif
    312  1.29  christos 
    313  1.29  christos /*
    314   1.8       jtc ** Private function declarations.
    315   1.8       jtc */
    316   1.8       jtc 
    317  1.25   mlelstv char *		icatalloc(char * old, const char * new);
    318  1.25   mlelstv char *		icpyalloc(const char * string);
    319  1.25   mlelstv const char *	scheck(const char * string, const char * format);
    320   1.6       jtc 
    321   1.6       jtc /*
    322   1.1       jtc ** Finally, some convenience items.
    323   1.1       jtc */
    324   1.1       jtc 
    325   1.1       jtc #ifndef TRUE
    326   1.1       jtc #define TRUE	1
    327   1.1       jtc #endif /* !defined TRUE */
    328   1.1       jtc 
    329   1.1       jtc #ifndef FALSE
    330   1.1       jtc #define FALSE	0
    331   1.1       jtc #endif /* !defined FALSE */
    332   1.1       jtc 
    333   1.5       jtc #ifndef TYPE_BIT
    334   1.5       jtc #define TYPE_BIT(type)	(sizeof (type) * CHAR_BIT)
    335   1.5       jtc #endif /* !defined TYPE_BIT */
    336   1.5       jtc 
    337   1.5       jtc #ifndef TYPE_SIGNED
    338  1.28  christos #define TYPE_SIGNED(type) (/*CONSTCOND*/((type) -1) < 0)
    339   1.5       jtc #endif /* !defined TYPE_SIGNED */
    340   1.5       jtc 
    341   1.1       jtc #ifndef INT_STRLEN_MAXIMUM
    342   1.1       jtc /*
    343   1.1       jtc ** 302 / 1000 is log10(2.0) rounded up.
    344   1.5       jtc ** Subtract one for the sign bit if the type is signed;
    345   1.5       jtc ** add one for integer division truncation;
    346   1.5       jtc ** add one more for a minus sign if the type is signed.
    347   1.1       jtc */
    348   1.1       jtc #define INT_STRLEN_MAXIMUM(type) \
    349  1.25   mlelstv 	((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
    350  1.25   mlelstv 	1 + TYPE_SIGNED(type))
    351   1.1       jtc #endif /* !defined INT_STRLEN_MAXIMUM */
    352   1.1       jtc 
    353   1.1       jtc /*
    354   1.1       jtc ** INITIALIZE(x)
    355   1.1       jtc */
    356   1.1       jtc 
    357   1.1       jtc #ifdef lint
    358  1.34  christos # define INITIALIZE(x)	((x) = 0)
    359  1.34  christos #else
    360  1.34  christos # define INITIALIZE(x)
    361  1.34  christos #endif
    362   1.6       jtc 
    363   1.6       jtc /*
    364   1.6       jtc ** For the benefit of GNU folk...
    365  1.34  christos ** '_(MSGID)' uses the current locale's message library string for MSGID.
    366   1.6       jtc ** The default is to use gettext if available, and use MSGID otherwise.
    367   1.6       jtc */
    368   1.6       jtc 
    369   1.6       jtc #ifndef _
    370  1.25   mlelstv #if HAVE_GETTEXT
    371   1.6       jtc #define _(msgid) gettext(msgid)
    372  1.25   mlelstv #else /* !HAVE_GETTEXT */
    373   1.6       jtc #define _(msgid) msgid
    374  1.25   mlelstv #endif /* !HAVE_GETTEXT */
    375   1.6       jtc #endif /* !defined _ */
    376   1.6       jtc 
    377  1.34  christos #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
    378  1.34  christos # define TZ_DOMAIN "tz"
    379  1.34  christos #endif
    380  1.17    kleink 
    381  1.17    kleink #if HAVE_INCOMPATIBLE_CTIME_R
    382  1.17    kleink #undef asctime_r
    383  1.17    kleink #undef ctime_r
    384  1.25   mlelstv char *asctime_r(struct tm const *, char *);
    385  1.25   mlelstv char *ctime_r(time_t const *, char *);
    386  1.17    kleink #endif /* HAVE_INCOMPATIBLE_CTIME_R */
    387   1.1       jtc 
    388  1.25   mlelstv #ifndef YEARSPERREPEAT
    389  1.25   mlelstv #define YEARSPERREPEAT		400	/* years before a Gregorian repeat */
    390  1.25   mlelstv #endif /* !defined YEARSPERREPEAT */
    391  1.25   mlelstv 
    392  1.25   mlelstv /*
    393  1.25   mlelstv ** The Gregorian year averages 365.2425 days, which is 31556952 seconds.
    394  1.25   mlelstv */
    395  1.25   mlelstv 
    396  1.25   mlelstv #ifndef AVGSECSPERYEAR
    397  1.25   mlelstv #define AVGSECSPERYEAR		31556952L
    398  1.25   mlelstv #endif /* !defined AVGSECSPERYEAR */
    399  1.25   mlelstv 
    400  1.25   mlelstv #ifndef SECSPERREPEAT
    401  1.25   mlelstv #define SECSPERREPEAT		((int_fast64_t) YEARSPERREPEAT * (int_fast64_t) AVGSECSPERYEAR)
    402  1.25   mlelstv #endif /* !defined SECSPERREPEAT */
    403  1.25   mlelstv 
    404  1.25   mlelstv #ifndef SECSPERREPEAT_BITS
    405  1.25   mlelstv #define SECSPERREPEAT_BITS	34	/* ceil(log2(SECSPERREPEAT)) */
    406  1.25   mlelstv #endif /* !defined SECSPERREPEAT_BITS */
    407  1.25   mlelstv 
    408   1.1       jtc #endif /* !defined PRIVATE_H */
    409