Home | History | Annotate | Line # | Download | only in time
zdump.c revision 1.34
      1  1.34  christos /*	$NetBSD: zdump.c,v 1.34 2014/08/15 11:04:07 christos Exp $	*/
      2  1.17   mlelstv /*
      3  1.17   mlelstv ** This file is in the public domain, so clarified as of
      4  1.17   mlelstv ** 2009-05-17 by Arthur David Olson.
      5  1.17   mlelstv */
      6   1.2       jtc 
      7   1.6  christos #include <sys/cdefs.h>
      8   1.1       jtc #ifndef lint
      9  1.34  christos __RCSID("$NetBSD: zdump.c,v 1.34 2014/08/15 11:04:07 christos Exp $");
     10   1.1       jtc #endif /* !defined lint */
     11   1.1       jtc 
     12  1.25  christos #include "version.h"
     13   1.1       jtc /*
     14   1.1       jtc ** This code has been made independent of the rest of the time
     15   1.1       jtc ** conversion package to increase confidence in the verification it provides.
     16   1.1       jtc ** You can use this code to help in verifying other implementations.
     17  1.29  christos **
     18  1.29  christos ** However, include private.h when debugging, so that it overrides
     19  1.29  christos ** time_t consistently with the rest of the package.
     20   1.1       jtc */
     21   1.1       jtc 
     22  1.29  christos #include "private.h"
     23  1.29  christos 
     24  1.15  christos #include "stdio.h"	/* for stdout, stderr */
     25   1.1       jtc #include "string.h"	/* for strcpy */
     26   1.1       jtc #include "sys/types.h"	/* for time_t */
     27   1.1       jtc #include "time.h"	/* for struct tm */
     28   1.1       jtc #include "stdlib.h"	/* for exit, malloc, atoi */
     29  1.15  christos #include <err.h>
     30  1.17   mlelstv 
     31  1.29  christos /*
     32  1.29  christos ** Substitutes for pre-C99 compilers.
     33  1.29  christos ** Much of this section of code is stolen from private.h.
     34  1.29  christos */
     35  1.29  christos 
     36  1.29  christos #ifndef HAVE_STDINT_H
     37  1.29  christos # define HAVE_STDINT_H \
     38  1.33  christos     (199901 <= __STDC_VERSION__ \
     39  1.33  christos      || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__)	\
     40  1.33  christos      || __CYGWIN__)
     41  1.29  christos #endif
     42  1.29  christos #if HAVE_STDINT_H
     43  1.29  christos # include "stdint.h"
     44  1.29  christos #endif
     45  1.29  christos #ifndef HAVE_INTTYPES_H
     46  1.29  christos # define HAVE_INTTYPES_H HAVE_STDINT_H
     47  1.29  christos #endif
     48  1.29  christos #if HAVE_INTTYPES_H
     49  1.29  christos # include <inttypes.h>
     50  1.29  christos #endif
     51  1.29  christos 
     52  1.29  christos #ifndef INT_FAST32_MAX
     53  1.29  christos # if INT_MAX >> 31 == 0
     54  1.29  christos typedef long int_fast32_t;
     55  1.29  christos # else
     56  1.29  christos typedef int int_fast32_t;
     57  1.29  christos # endif
     58  1.29  christos #endif
     59  1.29  christos 
     60  1.29  christos #ifndef INTMAX_MAX
     61  1.29  christos # if defined LLONG_MAX || defined __LONG_LONG_MAX__
     62  1.29  christos typedef long long intmax_t;
     63  1.32  christos #  define strtoimax strtoll
     64  1.29  christos #  define PRIdMAX "lld"
     65  1.31  christos #  ifdef LLONG_MAX
     66  1.31  christos #   define INTMAX_MAX LLONG_MAX
     67  1.31  christos #  else
     68  1.31  christos #   define INTMAX_MAX __LONG_LONG_MAX__
     69  1.31  christos #  endif
     70  1.29  christos # else
     71  1.29  christos typedef long intmax_t;
     72  1.32  christos #  define strtoimax strtol
     73  1.29  christos #  define PRIdMAX "ld"
     74  1.31  christos #  define INTMAX_MAX LONG_MAX
     75  1.29  christos # endif
     76  1.29  christos #endif
     77  1.29  christos 
     78  1.27  christos 
     79  1.17   mlelstv #ifndef ZDUMP_LO_YEAR
     80  1.17   mlelstv #define ZDUMP_LO_YEAR	(-500)
     81  1.17   mlelstv #endif /* !defined ZDUMP_LO_YEAR */
     82  1.17   mlelstv 
     83  1.17   mlelstv #ifndef ZDUMP_HI_YEAR
     84  1.17   mlelstv #define ZDUMP_HI_YEAR	2500
     85  1.17   mlelstv #endif /* !defined ZDUMP_HI_YEAR */
     86   1.1       jtc 
     87   1.1       jtc #ifndef MAX_STRING_LENGTH
     88   1.1       jtc #define MAX_STRING_LENGTH	1024
     89   1.1       jtc #endif /* !defined MAX_STRING_LENGTH */
     90   1.1       jtc 
     91   1.1       jtc #ifndef TRUE
     92   1.1       jtc #define TRUE		1
     93   1.1       jtc #endif /* !defined TRUE */
     94   1.1       jtc 
     95   1.1       jtc #ifndef FALSE
     96   1.1       jtc #define FALSE		0
     97   1.1       jtc #endif /* !defined FALSE */
     98   1.1       jtc 
     99   1.1       jtc #ifndef EXIT_SUCCESS
    100   1.1       jtc #define EXIT_SUCCESS	0
    101   1.1       jtc #endif /* !defined EXIT_SUCCESS */
    102   1.1       jtc 
    103   1.1       jtc #ifndef EXIT_FAILURE
    104   1.1       jtc #define EXIT_FAILURE	1
    105   1.1       jtc #endif /* !defined EXIT_FAILURE */
    106   1.1       jtc 
    107   1.1       jtc #ifndef SECSPERMIN
    108   1.1       jtc #define SECSPERMIN	60
    109   1.1       jtc #endif /* !defined SECSPERMIN */
    110   1.1       jtc 
    111   1.1       jtc #ifndef MINSPERHOUR
    112   1.1       jtc #define MINSPERHOUR	60
    113   1.1       jtc #endif /* !defined MINSPERHOUR */
    114   1.1       jtc 
    115   1.1       jtc #ifndef SECSPERHOUR
    116   1.1       jtc #define SECSPERHOUR	(SECSPERMIN * MINSPERHOUR)
    117   1.1       jtc #endif /* !defined SECSPERHOUR */
    118   1.1       jtc 
    119   1.1       jtc #ifndef HOURSPERDAY
    120   1.1       jtc #define HOURSPERDAY	24
    121   1.1       jtc #endif /* !defined HOURSPERDAY */
    122   1.1       jtc 
    123   1.1       jtc #ifndef EPOCH_YEAR
    124   1.1       jtc #define EPOCH_YEAR	1970
    125   1.1       jtc #endif /* !defined EPOCH_YEAR */
    126   1.1       jtc 
    127   1.1       jtc #ifndef TM_YEAR_BASE
    128   1.1       jtc #define TM_YEAR_BASE	1900
    129   1.1       jtc #endif /* !defined TM_YEAR_BASE */
    130   1.1       jtc 
    131   1.1       jtc #ifndef DAYSPERNYEAR
    132   1.1       jtc #define DAYSPERNYEAR	365
    133   1.1       jtc #endif /* !defined DAYSPERNYEAR */
    134   1.1       jtc 
    135   1.1       jtc #ifndef isleap
    136  1.17   mlelstv #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
    137   1.1       jtc #endif /* !defined isleap */
    138   1.1       jtc 
    139  1.17   mlelstv #ifndef isleap_sum
    140  1.17   mlelstv /*
    141  1.17   mlelstv ** See tzfile.h for details on isleap_sum.
    142  1.17   mlelstv */
    143  1.17   mlelstv #define isleap_sum(a, b)	isleap((a) % 400 + (b) % 400)
    144  1.17   mlelstv #endif /* !defined isleap_sum */
    145  1.17   mlelstv 
    146  1.29  christos #define SECSPERDAY	((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
    147  1.17   mlelstv #define SECSPERNYEAR	(SECSPERDAY * DAYSPERNYEAR)
    148  1.17   mlelstv #define SECSPERLYEAR	(SECSPERNYEAR + SECSPERDAY)
    149  1.31  christos #define SECSPER400YEARS	(SECSPERNYEAR * (intmax_t) (300 + 3)	\
    150  1.31  christos 			 + SECSPERLYEAR * (intmax_t) (100 - 3))
    151  1.31  christos 
    152  1.31  christos /*
    153  1.31  christos ** True if SECSPER400YEARS is known to be representable as an
    154  1.31  christos ** intmax_t.  It's OK that SECSPER400YEARS_FITS can in theory be false
    155  1.31  christos ** even if SECSPER400YEARS is representable, because when that happens
    156  1.31  christos ** the code merely runs a bit more slowly, and this slowness doesn't
    157  1.31  christos ** occur on any practical platform.
    158  1.31  christos */
    159  1.31  christos enum { SECSPER400YEARS_FITS = SECSPERLYEAR <= INTMAX_MAX / 400 };
    160  1.17   mlelstv 
    161  1.17   mlelstv #ifndef HAVE_GETTEXT
    162  1.17   mlelstv #define HAVE_GETTEXT 0
    163  1.17   mlelstv #endif
    164  1.17   mlelstv #if HAVE_GETTEXT
    165   1.3       jtc #include "locale.h"	/* for setlocale */
    166   1.3       jtc #include "libintl.h"
    167  1.17   mlelstv #endif /* HAVE_GETTEXT */
    168   1.3       jtc 
    169  1.29  christos #ifndef ATTRIBUTE_PURE
    170  1.26  christos #if 2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__)
    171  1.29  christos # define ATTRIBUTE_PURE __attribute__ ((ATTRIBUTE_PURE__))
    172  1.26  christos #else
    173  1.29  christos # define ATTRIBUTE_PURE /* empty */
    174  1.26  christos #endif
    175  1.26  christos #endif
    176  1.26  christos 
    177   1.1       jtc #ifndef INITIALIZE
    178   1.1       jtc #ifdef GNUC_or_lint
    179   1.1       jtc #define INITIALIZE(x)	((x) = 0)
    180  1.17   mlelstv #else /* !defined GNUC_or_lint */
    181   1.1       jtc #define INITIALIZE(x)
    182   1.1       jtc #endif /* !defined GNUC_or_lint */
    183   1.1       jtc #endif /* !defined INITIALIZE */
    184   1.1       jtc 
    185   1.3       jtc /*
    186   1.3       jtc ** For the benefit of GNU folk...
    187  1.34  christos ** '_(MSGID)' uses the current locale's message library string for MSGID.
    188   1.3       jtc ** The default is to use gettext if available, and use MSGID otherwise.
    189   1.3       jtc */
    190   1.3       jtc 
    191   1.3       jtc #ifndef _
    192  1.17   mlelstv #if HAVE_GETTEXT
    193   1.3       jtc #define _(msgid) gettext(msgid)
    194  1.17   mlelstv #else /* !HAVE_GETTEXT */
    195  1.21  christos #define _(msgid) msgid
    196  1.17   mlelstv #endif /* !HAVE_GETTEXT */
    197   1.3       jtc #endif /* !defined _ */
    198   1.3       jtc 
    199  1.34  christos #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
    200  1.34  christos # define TZ_DOMAIN "tz"
    201  1.34  christos #endif
    202   1.3       jtc 
    203   1.1       jtc extern char **	environ;
    204  1.17   mlelstv extern int	getopt(int argc, char * const argv[],
    205  1.17   mlelstv 			const char * options);
    206   1.1       jtc extern char *	optarg;
    207   1.1       jtc extern int	optind;
    208   1.1       jtc 
    209  1.29  christos /* The minimum and maximum finite time values.  */
    210  1.29  christos static time_t	absolute_min_time =
    211  1.31  christos   ((time_t) -1 < 0
    212  1.29  christos     ? (time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1)
    213  1.29  christos     : 0);
    214  1.29  christos static time_t	absolute_max_time =
    215  1.31  christos   ((time_t) -1 < 0
    216  1.31  christos     ? - (~ 0 < 0) - ((time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1))
    217  1.29  christos    : -1);
    218   1.5       jtc static size_t	longest;
    219   1.1       jtc static char *	progname;
    220  1.17   mlelstv static int	warned;
    221  1.17   mlelstv 
    222  1.17   mlelstv static const char *	abbr(struct tm * tmp);
    223  1.17   mlelstv static void	abbrok(const char * abbrp, const char * zone);
    224  1.29  christos static intmax_t	delta(struct tm * newp, struct tm * oldp) ATTRIBUTE_PURE;
    225  1.17   mlelstv static void	dumptime(const struct tm * tmp);
    226  1.17   mlelstv static time_t	hunt(char * name, time_t lot, time_t	hit);
    227  1.17   mlelstv static void	show(char * zone, time_t t, int v);
    228  1.17   mlelstv static const char *	tformat(void);
    229  1.29  christos static time_t	yeartot(long y) ATTRIBUTE_PURE;
    230  1.17   mlelstv 
    231  1.34  christos /* Is A an alphabetic character in the C locale?  */
    232  1.34  christos static int
    233  1.34  christos is_alpha(char a)
    234  1.34  christos {
    235  1.34  christos 	switch (a) {
    236  1.34  christos 	  default:
    237  1.34  christos 		return 0;
    238  1.34  christos 	  case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
    239  1.34  christos 	  case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
    240  1.34  christos 	  case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
    241  1.34  christos 	  case 'V': case 'W': case 'X': case 'Y': case 'Z':
    242  1.34  christos 	  case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
    243  1.34  christos 	  case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
    244  1.34  christos 	  case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
    245  1.34  christos 	  case 'v': case 'w': case 'x': case 'y': case 'z':
    246  1.34  christos 	  	return 1;
    247  1.34  christos 	}
    248  1.34  christos }
    249  1.34  christos 
    250  1.17   mlelstv #ifndef TYPECHECK
    251  1.17   mlelstv #define my_localtime	localtime
    252  1.17   mlelstv #else /* !defined TYPECHECK */
    253  1.17   mlelstv static struct tm *
    254  1.26  christos my_localtime(time_t *tp)
    255  1.17   mlelstv {
    256  1.26  christos 	struct tm *tmp;
    257  1.17   mlelstv 
    258  1.17   mlelstv 	tmp = localtime(tp);
    259  1.17   mlelstv 	if (tp != NULL && tmp != NULL) {
    260  1.17   mlelstv 		struct tm	tm;
    261  1.26  christos 		time_t	t;
    262  1.17   mlelstv 
    263  1.17   mlelstv 		tm = *tmp;
    264  1.17   mlelstv 		t = mktime(&tm);
    265  1.31  christos 		if (t != *tp) {
    266  1.17   mlelstv 			(void) fflush(stdout);
    267  1.17   mlelstv 			(void) fprintf(stderr, "\n%s: ", progname);
    268  1.17   mlelstv 			(void) fprintf(stderr, tformat(), *tp);
    269  1.17   mlelstv 			(void) fprintf(stderr, " ->");
    270  1.17   mlelstv 			(void) fprintf(stderr, " year=%d", tmp->tm_year);
    271  1.17   mlelstv 			(void) fprintf(stderr, " mon=%d", tmp->tm_mon);
    272  1.17   mlelstv 			(void) fprintf(stderr, " mday=%d", tmp->tm_mday);
    273  1.17   mlelstv 			(void) fprintf(stderr, " hour=%d", tmp->tm_hour);
    274  1.17   mlelstv 			(void) fprintf(stderr, " min=%d", tmp->tm_min);
    275  1.17   mlelstv 			(void) fprintf(stderr, " sec=%d", tmp->tm_sec);
    276  1.17   mlelstv 			(void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
    277  1.17   mlelstv 			(void) fprintf(stderr, " -> ");
    278  1.17   mlelstv 			(void) fprintf(stderr, tformat(), t);
    279  1.17   mlelstv 			(void) fprintf(stderr, "\n");
    280  1.17   mlelstv 		}
    281  1.17   mlelstv 	}
    282  1.17   mlelstv 	return tmp;
    283  1.17   mlelstv }
    284  1.17   mlelstv #endif /* !defined TYPECHECK */
    285  1.17   mlelstv 
    286  1.17   mlelstv static void
    287  1.26  christos abbrok(const char *const abbrp, const char *const zone)
    288  1.17   mlelstv {
    289  1.26  christos 	const char *cp;
    290  1.26  christos 	const char *wp;
    291  1.17   mlelstv 
    292  1.17   mlelstv 	if (warned)
    293  1.17   mlelstv 		return;
    294  1.17   mlelstv 	cp = abbrp;
    295  1.17   mlelstv 	wp = NULL;
    296  1.34  christos 	while (is_alpha(*cp))
    297  1.17   mlelstv 		++cp;
    298  1.17   mlelstv 	if (cp - abbrp == 0)
    299  1.17   mlelstv 		wp = _("lacks alphabetic at start");
    300  1.17   mlelstv 	else if (cp - abbrp < 3)
    301  1.17   mlelstv 		wp = _("has fewer than 3 alphabetics");
    302  1.17   mlelstv 	else if (cp - abbrp > 6)
    303  1.17   mlelstv 		wp = _("has more than 6 alphabetics");
    304  1.17   mlelstv 	if (wp == NULL && (*cp == '+' || *cp == '-')) {
    305  1.17   mlelstv 		++cp;
    306  1.34  christos 		if ('0' <= *cp && *cp <= '9')
    307  1.34  christos 			if (*cp++ == '1' && '0' <= *cp && *cp <= '4')
    308  1.34  christos 				cp++;
    309  1.17   mlelstv 		if (*cp != '\0')
    310  1.17   mlelstv 			wp = _("differs from POSIX standard");
    311  1.17   mlelstv 	}
    312  1.17   mlelstv 	if (wp == NULL)
    313  1.17   mlelstv 		return;
    314  1.17   mlelstv 	(void) fflush(stdout);
    315  1.17   mlelstv 	(void) fprintf(stderr,
    316  1.17   mlelstv 		_("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
    317  1.17   mlelstv 		progname, zone, abbrp, wp);
    318  1.17   mlelstv 	warned = TRUE;
    319  1.17   mlelstv }
    320  1.17   mlelstv 
    321  1.24     joerg __dead static void
    322  1.26  christos usage(FILE *const stream, const int status)
    323  1.17   mlelstv {
    324  1.17   mlelstv 	(void) fprintf(stream,
    325  1.29  christos _("%s: usage: %s [--version] [--help] [-{vV}] [-{ct} [lo,]hi] zonename ...\n"
    326  1.29  christos   "\n"
    327  1.29  christos   "Report bugs to %s.\n"),
    328  1.28  christos 		       progname, progname, REPORT_BUGS_TO);
    329  1.17   mlelstv 	exit(status);
    330  1.17   mlelstv }
    331   1.1       jtc 
    332   1.1       jtc int
    333  1.26  christos main(int argc, char *argv[])
    334  1.26  christos {
    335  1.26  christos 	int		i;
    336  1.26  christos 	int		vflag;
    337  1.29  christos 	int		Vflag;
    338  1.26  christos 	char *		cutarg;
    339  1.29  christos 	char *		cuttimes;
    340  1.26  christos 	time_t		cutlotime;
    341  1.26  christos 	time_t		cuthitime;
    342  1.26  christos 	char **		fakeenv;
    343  1.26  christos 	time_t		now;
    344  1.26  christos 	time_t		t;
    345  1.26  christos 	time_t		newt;
    346  1.26  christos 	struct tm	tm;
    347  1.26  christos 	struct tm	newtm;
    348  1.26  christos 	struct tm *	tmp;
    349  1.26  christos 	struct tm *	newtmp;
    350   1.1       jtc 
    351  1.29  christos 	cutlotime = absolute_min_time;
    352  1.29  christos 	cuthitime = absolute_max_time;
    353  1.17   mlelstv #if HAVE_GETTEXT
    354  1.17   mlelstv 	(void) setlocale(LC_ALL, "");
    355   1.3       jtc #ifdef TZ_DOMAINDIR
    356   1.3       jtc 	(void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
    357  1.17   mlelstv #endif /* defined TEXTDOMAINDIR */
    358   1.3       jtc 	(void) textdomain(TZ_DOMAIN);
    359  1.17   mlelstv #endif /* HAVE_GETTEXT */
    360   1.1       jtc 	progname = argv[0];
    361  1.14    kleink 	for (i = 1; i < argc; ++i)
    362  1.14    kleink 		if (strcmp(argv[i], "--version") == 0) {
    363  1.28  christos 			(void) printf("zdump %s%s\n", PKGVERSION, TZVERSION);
    364  1.17   mlelstv 			exit(EXIT_SUCCESS);
    365  1.17   mlelstv 		} else if (strcmp(argv[i], "--help") == 0) {
    366  1.22  christos 			usage(stdout, EXIT_SUCCESS);
    367  1.14    kleink 		}
    368  1.29  christos 	vflag = Vflag = 0;
    369  1.29  christos 	cutarg = cuttimes = NULL;
    370  1.29  christos 	for (;;)
    371  1.29  christos 	  switch (getopt(argc, argv, "c:t:vV")) {
    372  1.29  christos 	  case 'c': cutarg = optarg; break;
    373  1.29  christos 	  case 't': cuttimes = optarg; break;
    374  1.29  christos 	  case 'v': vflag = 1; break;
    375  1.29  christos 	  case 'V': Vflag = 1; break;
    376  1.29  christos 	  case -1:
    377  1.29  christos 	    if (! (optind == argc - 1 && strcmp(argv[optind], "=") == 0))
    378  1.29  christos 	      goto arg_processing_done;
    379  1.29  christos 	    /* Fall through.  */
    380  1.29  christos 	  default:
    381  1.29  christos 	    usage(stderr, EXIT_FAILURE);
    382  1.29  christos 	  }
    383  1.29  christos  arg_processing_done:;
    384  1.29  christos 
    385  1.29  christos 	if (vflag | Vflag) {
    386  1.29  christos 		intmax_t	lo;
    387  1.29  christos 		intmax_t	hi;
    388  1.32  christos 		char *loend, *hiend;
    389  1.30  christos 		intmax_t cutloyear = ZDUMP_LO_YEAR;
    390  1.30  christos 		intmax_t cuthiyear = ZDUMP_HI_YEAR;
    391  1.17   mlelstv 		if (cutarg != NULL) {
    392  1.32  christos 			lo = strtoimax(cutarg, &loend, 10);
    393  1.32  christos 			if (cutarg != loend && !*loend) {
    394  1.32  christos 				hi = lo;
    395  1.32  christos 				cuthiyear = hi;
    396  1.32  christos 			} else if (cutarg != loend && *loend == ','
    397  1.32  christos 				   && (hi = strtoimax(loend + 1, &hiend, 10),
    398  1.32  christos 				       loend + 1 != hiend && !*hiend)) {
    399  1.32  christos 				cutloyear = lo;
    400  1.17   mlelstv 				cuthiyear = hi;
    401  1.17   mlelstv 			} else {
    402  1.17   mlelstv (void) fprintf(stderr, _("%s: wild -c argument %s\n"),
    403  1.17   mlelstv 					progname, cutarg);
    404  1.17   mlelstv 				exit(EXIT_FAILURE);
    405  1.17   mlelstv 			}
    406  1.17   mlelstv 		}
    407  1.29  christos 		if (cutarg != NULL || cuttimes == NULL) {
    408  1.29  christos 			cutlotime = yeartot(cutloyear);
    409  1.29  christos 			cuthitime = yeartot(cuthiyear);
    410  1.29  christos 		}
    411  1.29  christos 		if (cuttimes != NULL) {
    412  1.32  christos 			lo = strtoimax(cuttimes, &loend, 10);
    413  1.32  christos 			if (cuttimes != loend && !*loend) {
    414  1.32  christos 				hi = lo;
    415  1.29  christos 				if (hi < cuthitime) {
    416  1.29  christos 					if (hi < absolute_min_time)
    417  1.29  christos 						hi = absolute_min_time;
    418  1.29  christos 					cuthitime = hi;
    419  1.29  christos 				}
    420  1.32  christos 			} else if (cuttimes != loend && *loend == ','
    421  1.32  christos 				   && (hi = strtoimax(loend + 1, &hiend, 10),
    422  1.32  christos 				       loend + 1 != hiend && !*hiend)) {
    423  1.29  christos 				if (cutlotime < lo) {
    424  1.29  christos 					if (absolute_max_time < lo)
    425  1.29  christos 						lo = absolute_max_time;
    426  1.29  christos 					cutlotime = lo;
    427  1.29  christos 				}
    428  1.29  christos 				if (hi < cuthitime) {
    429  1.29  christos 					if (hi < absolute_min_time)
    430  1.29  christos 						hi = absolute_min_time;
    431  1.29  christos 					cuthitime = hi;
    432  1.29  christos 				}
    433  1.29  christos 			} else {
    434  1.29  christos 				(void) fprintf(stderr,
    435  1.29  christos 					_("%s: wild -t argument %s\n"),
    436  1.29  christos 					progname, cuttimes);
    437  1.29  christos 				exit(EXIT_FAILURE);
    438  1.29  christos 			}
    439  1.29  christos 		}
    440   1.1       jtc 	}
    441   1.1       jtc 	(void) time(&now);
    442   1.1       jtc 	longest = 0;
    443   1.1       jtc 	for (i = optind; i < argc; ++i)
    444   1.1       jtc 		if (strlen(argv[i]) > longest)
    445   1.1       jtc 			longest = strlen(argv[i]);
    446   1.1       jtc 	{
    447  1.26  christos 		int	from;
    448  1.26  christos 		int	to;
    449   1.1       jtc 
    450  1.17   mlelstv 		for (i = 0; environ[i] != NULL; ++i)
    451   1.1       jtc 			continue;
    452  1.26  christos 		fakeenv = malloc((i + 2) * sizeof *fakeenv);
    453   1.1       jtc 		if (fakeenv == NULL ||
    454  1.26  christos 			(fakeenv[0] = malloc(longest + 4)) == NULL) {
    455  1.15  christos 			err(EXIT_FAILURE, "Can't allocated %zu bytes",
    456  1.15  christos 			    longest + 4);
    457   1.1       jtc 		}
    458   1.1       jtc 		to = 0;
    459   1.4       mrg 		(void)strcpy(fakeenv[to++], "TZ=");	/* XXX strcpy is safe */
    460   1.1       jtc 		for (from = 0; environ[from] != NULL; ++from)
    461   1.1       jtc 			if (strncmp(environ[from], "TZ=", 3) != 0)
    462   1.1       jtc 				fakeenv[to++] = environ[from];
    463   1.1       jtc 		fakeenv[to] = NULL;
    464   1.1       jtc 		environ = fakeenv;
    465   1.1       jtc 	}
    466   1.1       jtc 	for (i = optind; i < argc; ++i) {
    467   1.1       jtc 		static char	buf[MAX_STRING_LENGTH];
    468   1.1       jtc 
    469   1.4       mrg 		(void) strcpy(&fakeenv[0][3], argv[i]);	/* XXX strcpy is safe */
    470  1.29  christos 		if (! (vflag | Vflag)) {
    471   1.3       jtc 			show(argv[i], now, FALSE);
    472   1.1       jtc 			continue;
    473   1.3       jtc 		}
    474  1.17   mlelstv 		warned = FALSE;
    475  1.17   mlelstv 		t = absolute_min_time;
    476  1.29  christos 		if (!Vflag) {
    477  1.29  christos 			show(argv[i], t, TRUE);
    478  1.31  christos 			t += SECSPERDAY;
    479  1.29  christos 			show(argv[i], t, TRUE);
    480  1.29  christos 		}
    481  1.17   mlelstv 		if (t < cutlotime)
    482  1.17   mlelstv 			t = cutlotime;
    483  1.17   mlelstv 		tmp = my_localtime(&t);
    484  1.17   mlelstv 		if (tmp != NULL) {
    485  1.17   mlelstv 			tm = *tmp;
    486  1.17   mlelstv 			(void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
    487  1.17   mlelstv 		}
    488   1.1       jtc 		for ( ; ; ) {
    489  1.31  christos 			newt = (t < absolute_max_time - SECSPERDAY / 2
    490  1.31  christos 				? t + SECSPERDAY / 2
    491  1.31  christos 				: absolute_max_time);
    492  1.31  christos 			if (cuthitime <= newt)
    493   1.1       jtc 				break;
    494  1.17   mlelstv 			newtmp = localtime(&newt);
    495  1.17   mlelstv 			if (newtmp != NULL)
    496  1.17   mlelstv 				newtm = *newtmp;
    497  1.17   mlelstv 			if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
    498  1.17   mlelstv 				(delta(&newtm, &tm) != (newt - t) ||
    499   1.1       jtc 				newtm.tm_isdst != tm.tm_isdst ||
    500  1.17   mlelstv 				strcmp(abbr(&newtm), buf) != 0)) {
    501   1.1       jtc 					newt = hunt(argv[i], t, newt);
    502  1.17   mlelstv 					newtmp = localtime(&newt);
    503  1.17   mlelstv 					if (newtmp != NULL) {
    504  1.17   mlelstv 						newtm = *newtmp;
    505  1.17   mlelstv 						(void) strncpy(buf,
    506  1.17   mlelstv 							abbr(&newtm),
    507  1.17   mlelstv 							(sizeof buf) - 1);
    508  1.17   mlelstv 					}
    509   1.1       jtc 			}
    510   1.1       jtc 			t = newt;
    511   1.1       jtc 			tm = newtm;
    512  1.17   mlelstv 			tmp = newtmp;
    513   1.1       jtc 		}
    514  1.29  christos 		if (!Vflag) {
    515  1.29  christos 			t = absolute_max_time;
    516  1.31  christos 			t -= SECSPERDAY;
    517  1.29  christos 			show(argv[i], t, TRUE);
    518  1.31  christos 			t += SECSPERDAY;
    519  1.29  christos 			show(argv[i], t, TRUE);
    520  1.29  christos 		}
    521   1.1       jtc 	}
    522   1.1       jtc 	if (fflush(stdout) || ferror(stdout)) {
    523  1.16    kleink 		err(EXIT_FAILURE, _("Error writing standard output"));
    524   1.1       jtc 	}
    525   1.1       jtc 	exit(EXIT_SUCCESS);
    526  1.17   mlelstv 	/* If exit fails to exit... */
    527  1.17   mlelstv 	return EXIT_FAILURE;
    528  1.17   mlelstv }
    529   1.1       jtc 
    530   1.1       jtc static time_t
    531  1.26  christos yeartot(const long y)
    532  1.17   mlelstv {
    533  1.31  christos 	intmax_t	myy, seconds, years;
    534  1.29  christos 	time_t		t;
    535  1.17   mlelstv 
    536  1.17   mlelstv 	myy = EPOCH_YEAR;
    537  1.17   mlelstv 	t = 0;
    538  1.31  christos 	while (myy < y) {
    539  1.31  christos 		if (SECSPER400YEARS_FITS && 400 <= y - myy) {
    540  1.31  christos 			intmax_t diff400 = (y - myy) / 400;
    541  1.31  christos 			if (INTMAX_MAX / SECSPER400YEARS < diff400)
    542  1.31  christos 				return absolute_max_time;
    543  1.31  christos 			seconds = diff400 * SECSPER400YEARS;
    544  1.31  christos 			years = diff400 * 400;
    545  1.31  christos                 } else {
    546  1.17   mlelstv 			seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
    547  1.31  christos 			years = 1;
    548  1.31  christos 		}
    549  1.31  christos 		myy += years;
    550  1.31  christos 		if (t > absolute_max_time - seconds)
    551  1.31  christos 			return absolute_max_time;
    552  1.31  christos 		t += seconds;
    553  1.31  christos 	}
    554  1.31  christos 	while (y < myy) {
    555  1.31  christos 		if (SECSPER400YEARS_FITS && y + 400 <= myy && myy < 0) {
    556  1.31  christos 			intmax_t diff400 = (myy - y) / 400;
    557  1.31  christos 			if (INTMAX_MAX / SECSPER400YEARS < diff400)
    558  1.31  christos 				return absolute_min_time;
    559  1.31  christos 			seconds = diff400 * SECSPER400YEARS;
    560  1.31  christos 			years = diff400 * 400;
    561  1.17   mlelstv 		} else {
    562  1.31  christos 			seconds = isleap(myy - 1) ? SECSPERLYEAR : SECSPERNYEAR;
    563  1.31  christos 			years = 1;
    564  1.17   mlelstv 		}
    565  1.31  christos 		myy -= years;
    566  1.31  christos 		if (t < absolute_min_time + seconds)
    567  1.31  christos 			return absolute_min_time;
    568  1.31  christos 		t -= seconds;
    569  1.17   mlelstv 	}
    570  1.17   mlelstv 	return t;
    571  1.17   mlelstv }
    572  1.17   mlelstv 
    573  1.17   mlelstv static time_t
    574  1.17   mlelstv hunt(char *name, time_t lot, time_t hit)
    575  1.17   mlelstv {
    576  1.17   mlelstv 	time_t			t;
    577  1.17   mlelstv 	struct tm		lotm;
    578  1.26  christos 	struct tm *	lotmp;
    579  1.17   mlelstv 	struct tm		tm;
    580  1.26  christos 	struct tm *	tmp;
    581  1.17   mlelstv 	char			loab[MAX_STRING_LENGTH];
    582  1.17   mlelstv 
    583  1.17   mlelstv 	lotmp = my_localtime(&lot);
    584  1.17   mlelstv 	if (lotmp != NULL) {
    585  1.17   mlelstv 		lotm = *lotmp;
    586  1.17   mlelstv 		(void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
    587  1.17   mlelstv 	}
    588  1.17   mlelstv 	for ( ; ; ) {
    589  1.29  christos 		time_t diff = hit - lot;
    590  1.17   mlelstv 		if (diff < 2)
    591  1.17   mlelstv 			break;
    592  1.17   mlelstv 		t = lot;
    593  1.17   mlelstv 		t += diff / 2;
    594   1.1       jtc 		if (t <= lot)
    595   1.1       jtc 			++t;
    596   1.1       jtc 		else if (t >= hit)
    597   1.1       jtc 			--t;
    598  1.17   mlelstv 		tmp = my_localtime(&t);
    599  1.17   mlelstv 		if (tmp != NULL)
    600  1.17   mlelstv 			tm = *tmp;
    601  1.17   mlelstv 		if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
    602  1.17   mlelstv 			(delta(&tm, &lotm) == (t - lot) &&
    603   1.1       jtc 			tm.tm_isdst == lotm.tm_isdst &&
    604  1.17   mlelstv 			strcmp(abbr(&tm), loab) == 0)) {
    605   1.1       jtc 				lot = t;
    606   1.1       jtc 				lotm = tm;
    607  1.17   mlelstv 				lotmp = tmp;
    608   1.1       jtc 		} else	hit = t;
    609   1.1       jtc 	}
    610   1.1       jtc 	show(name, lot, TRUE);
    611   1.1       jtc 	show(name, hit, TRUE);
    612   1.1       jtc 	return hit;
    613   1.1       jtc }
    614   1.1       jtc 
    615   1.1       jtc /*
    616  1.17   mlelstv ** Thanks to Paul Eggert for logic used in delta.
    617   1.1       jtc */
    618   1.1       jtc 
    619  1.29  christos static intmax_t
    620  1.26  christos delta(struct tm *newp, struct tm *oldp)
    621   1.1       jtc {
    622  1.29  christos 	intmax_t	result;
    623  1.29  christos 	int		tmy;
    624   1.1       jtc 
    625   1.1       jtc 	if (newp->tm_year < oldp->tm_year)
    626   1.1       jtc 		return -delta(oldp, newp);
    627   1.1       jtc 	result = 0;
    628   1.1       jtc 	for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
    629  1.17   mlelstv 		result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
    630   1.1       jtc 	result += newp->tm_yday - oldp->tm_yday;
    631   1.1       jtc 	result *= HOURSPERDAY;
    632   1.1       jtc 	result += newp->tm_hour - oldp->tm_hour;
    633   1.1       jtc 	result *= MINSPERHOUR;
    634   1.1       jtc 	result += newp->tm_min - oldp->tm_min;
    635   1.1       jtc 	result *= SECSPERMIN;
    636   1.1       jtc 	result += newp->tm_sec - oldp->tm_sec;
    637   1.1       jtc 	return result;
    638   1.1       jtc }
    639   1.1       jtc 
    640   1.1       jtc static void
    641  1.17   mlelstv show(char *zone, time_t t, int v)
    642   1.1       jtc {
    643  1.26  christos 	struct tm *	tmp;
    644   1.1       jtc 
    645   1.5       jtc 	(void) printf("%-*s  ", (int) longest, zone);
    646   1.1       jtc 	if (v) {
    647  1.17   mlelstv 		tmp = gmtime(&t);
    648  1.17   mlelstv 		if (tmp == NULL) {
    649  1.17   mlelstv 			(void) printf(tformat(), t);
    650  1.17   mlelstv 		} else {
    651  1.17   mlelstv 			dumptime(tmp);
    652  1.31  christos 			(void) printf(" UT");
    653  1.17   mlelstv 		}
    654  1.17   mlelstv 		(void) printf(" = ");
    655  1.17   mlelstv 	}
    656  1.17   mlelstv 	tmp = my_localtime(&t);
    657  1.17   mlelstv 	dumptime(tmp);
    658  1.17   mlelstv 	if (tmp != NULL) {
    659  1.17   mlelstv 		if (*abbr(tmp) != '\0')
    660  1.17   mlelstv 			(void) printf(" %s", abbr(tmp));
    661  1.17   mlelstv 		if (v) {
    662  1.17   mlelstv 			(void) printf(" isdst=%d", tmp->tm_isdst);
    663   1.1       jtc #ifdef TM_GMTOFF
    664  1.17   mlelstv 			(void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
    665   1.1       jtc #endif /* defined TM_GMTOFF */
    666  1.17   mlelstv 		}
    667   1.1       jtc 	}
    668   1.1       jtc 	(void) printf("\n");
    669  1.17   mlelstv 	if (tmp != NULL && *abbr(tmp) != '\0')
    670  1.17   mlelstv 		abbrok(abbr(tmp), zone);
    671   1.1       jtc }
    672   1.1       jtc 
    673   1.9   mycroft static const char *
    674  1.26  christos abbr(struct tm *tmp)
    675   1.1       jtc {
    676  1.26  christos 	const char *	result;
    677   1.9   mycroft 	static const char	nada;
    678   1.1       jtc 
    679   1.1       jtc 	if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
    680   1.1       jtc 		return &nada;
    681   1.1       jtc 	result = tzname[tmp->tm_isdst];
    682   1.1       jtc 	return (result == NULL) ? &nada : result;
    683   1.1       jtc }
    684  1.17   mlelstv 
    685  1.17   mlelstv /*
    686  1.17   mlelstv ** The code below can fail on certain theoretical systems;
    687  1.17   mlelstv ** it works on all known real-world systems as of 2004-12-30.
    688  1.17   mlelstv */
    689  1.17   mlelstv 
    690  1.17   mlelstv static const char *
    691  1.17   mlelstv tformat(void)
    692  1.17   mlelstv {
    693  1.17   mlelstv 	if (0 > (time_t) -1) {		/* signed */
    694  1.29  christos 		if (sizeof (time_t) == sizeof (intmax_t))
    695  1.29  christos 			return "%"PRIdMAX;
    696  1.17   mlelstv 		if (sizeof (time_t) > sizeof (long))
    697  1.17   mlelstv 			return "%lld";
    698  1.17   mlelstv 		if (sizeof (time_t) > sizeof (int))
    699  1.17   mlelstv 			return "%ld";
    700  1.17   mlelstv 		return "%d";
    701  1.17   mlelstv 	}
    702  1.29  christos #ifdef PRIuMAX
    703  1.29  christos 	if (sizeof (time_t) == sizeof (uintmax_t))
    704  1.29  christos 		return "%"PRIuMAX;
    705  1.29  christos #endif
    706  1.17   mlelstv 	if (sizeof (time_t) > sizeof (unsigned long))
    707  1.17   mlelstv 		return "%llu";
    708  1.17   mlelstv 	if (sizeof (time_t) > sizeof (unsigned int))
    709  1.17   mlelstv 		return "%lu";
    710  1.17   mlelstv 	return "%u";
    711  1.17   mlelstv }
    712  1.17   mlelstv 
    713  1.17   mlelstv static void
    714  1.26  christos dumptime(const struct tm *timeptr)
    715  1.17   mlelstv {
    716  1.17   mlelstv 	static const char	wday_name[][3] = {
    717  1.17   mlelstv 		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
    718  1.17   mlelstv 	};
    719  1.17   mlelstv 	static const char	mon_name[][3] = {
    720  1.17   mlelstv 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
    721  1.17   mlelstv 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    722  1.17   mlelstv 	};
    723  1.26  christos 	const char *	wn;
    724  1.26  christos 	const char *	mn;
    725  1.26  christos 	int		lead;
    726  1.26  christos 	int		trail;
    727  1.17   mlelstv 
    728  1.17   mlelstv 	if (timeptr == NULL) {
    729  1.17   mlelstv 		(void) printf("NULL");
    730  1.17   mlelstv 		return;
    731  1.17   mlelstv 	}
    732  1.17   mlelstv 	/*
    733  1.17   mlelstv 	** The packaged versions of localtime and gmtime never put out-of-range
    734  1.17   mlelstv 	** values in tm_wday or tm_mon, but since this code might be compiled
    735  1.17   mlelstv 	** with other (perhaps experimental) versions, paranoia is in order.
    736  1.17   mlelstv 	*/
    737  1.17   mlelstv 	if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
    738  1.17   mlelstv 		(int) (sizeof wday_name / sizeof wday_name[0]))
    739  1.17   mlelstv 			wn = "???";
    740  1.17   mlelstv 	else		wn = wday_name[timeptr->tm_wday];
    741  1.17   mlelstv 	if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
    742  1.17   mlelstv 		(int) (sizeof mon_name / sizeof mon_name[0]))
    743  1.17   mlelstv 			mn = "???";
    744  1.17   mlelstv 	else		mn = mon_name[timeptr->tm_mon];
    745  1.17   mlelstv 	(void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
    746  1.17   mlelstv 		wn, mn,
    747  1.17   mlelstv 		timeptr->tm_mday, timeptr->tm_hour,
    748  1.17   mlelstv 		timeptr->tm_min, timeptr->tm_sec);
    749  1.17   mlelstv #define DIVISOR	10
    750  1.17   mlelstv 	trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
    751  1.17   mlelstv 	lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
    752  1.17   mlelstv 		trail / DIVISOR;
    753  1.17   mlelstv 	trail %= DIVISOR;
    754  1.17   mlelstv 	if (trail < 0 && lead > 0) {
    755  1.17   mlelstv 		trail += DIVISOR;
    756  1.17   mlelstv 		--lead;
    757  1.17   mlelstv 	} else if (lead < 0 && trail > 0) {
    758  1.17   mlelstv 		trail -= DIVISOR;
    759  1.17   mlelstv 		++lead;
    760  1.17   mlelstv 	}
    761  1.17   mlelstv 	if (lead == 0)
    762  1.17   mlelstv 		(void) printf("%d", trail);
    763  1.17   mlelstv 	else	(void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));
    764  1.17   mlelstv }
    765