Home | History | Annotate | Line # | Download | only in time
zdump.c revision 1.10.12.4
      1 /*	$NetBSD: zdump.c,v 1.10.12.4 2002/06/21 18:18:29 nathanw Exp $	*/
      2 
      3 #include <sys/cdefs.h>
      4 #ifndef lint
      5 #ifndef NOID
      6 #if 0
      7 static char	elsieid[] = "@(#)zdump.c	7.29";
      8 #else
      9 __RCSID("$NetBSD: zdump.c,v 1.10.12.4 2002/06/21 18:18:29 nathanw Exp $");
     10 #endif
     11 #endif /* !defined NOID */
     12 #endif /* !defined lint */
     13 
     14 /*
     15 ** This code has been made independent of the rest of the time
     16 ** conversion package to increase confidence in the verification it provides.
     17 ** You can use this code to help in verifying other implementations.
     18 */
     19 
     20 #include "stdio.h"	/* for stdout, stderr, perror */
     21 #include "string.h"	/* for strcpy */
     22 #include "sys/types.h"	/* for time_t */
     23 #include "time.h"	/* for struct tm */
     24 #include "stdlib.h"	/* for exit, malloc, atoi */
     25 
     26 #ifndef MAX_STRING_LENGTH
     27 #define MAX_STRING_LENGTH	1024
     28 #endif /* !defined MAX_STRING_LENGTH */
     29 
     30 #ifndef TRUE
     31 #define TRUE		1
     32 #endif /* !defined TRUE */
     33 
     34 #ifndef FALSE
     35 #define FALSE		0
     36 #endif /* !defined FALSE */
     37 
     38 #ifndef EXIT_SUCCESS
     39 #define EXIT_SUCCESS	0
     40 #endif /* !defined EXIT_SUCCESS */
     41 
     42 #ifndef EXIT_FAILURE
     43 #define EXIT_FAILURE	1
     44 #endif /* !defined EXIT_FAILURE */
     45 
     46 #ifndef SECSPERMIN
     47 #define SECSPERMIN	60
     48 #endif /* !defined SECSPERMIN */
     49 
     50 #ifndef MINSPERHOUR
     51 #define MINSPERHOUR	60
     52 #endif /* !defined MINSPERHOUR */
     53 
     54 #ifndef SECSPERHOUR
     55 #define SECSPERHOUR	(SECSPERMIN * MINSPERHOUR)
     56 #endif /* !defined SECSPERHOUR */
     57 
     58 #ifndef HOURSPERDAY
     59 #define HOURSPERDAY	24
     60 #endif /* !defined HOURSPERDAY */
     61 
     62 #ifndef EPOCH_YEAR
     63 #define EPOCH_YEAR	1970
     64 #endif /* !defined EPOCH_YEAR */
     65 
     66 #ifndef TM_YEAR_BASE
     67 #define TM_YEAR_BASE	1900
     68 #endif /* !defined TM_YEAR_BASE */
     69 
     70 #ifndef DAYSPERNYEAR
     71 #define DAYSPERNYEAR	365
     72 #endif /* !defined DAYSPERNYEAR */
     73 
     74 #ifndef isleap
     75 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
     76 #endif /* !defined isleap */
     77 
     78 #if HAVE_GETTEXT - 0
     79 #include "locale.h"	/* for setlocale */
     80 #include "libintl.h"
     81 #endif /* HAVE_GETTEXT - 0 */
     82 
     83 #ifndef GNUC_or_lint
     84 #ifdef lint
     85 #define GNUC_or_lint
     86 #endif /* defined lint */
     87 #ifndef lint
     88 #ifdef __GNUC__
     89 #define GNUC_or_lint
     90 #endif /* defined __GNUC__ */
     91 #endif /* !defined lint */
     92 #endif /* !defined GNUC_or_lint */
     93 
     94 #ifndef INITIALIZE
     95 #ifdef GNUC_or_lint
     96 #define INITIALIZE(x)	((x) = 0)
     97 #endif /* defined GNUC_or_lint */
     98 #ifndef GNUC_or_lint
     99 #define INITIALIZE(x)
    100 #endif /* !defined GNUC_or_lint */
    101 #endif /* !defined INITIALIZE */
    102 
    103 /*
    104 ** For the benefit of GNU folk...
    105 ** `_(MSGID)' uses the current locale's message library string for MSGID.
    106 ** The default is to use gettext if available, and use MSGID otherwise.
    107 */
    108 
    109 #ifndef _
    110 #if HAVE_GETTEXT - 0
    111 #define _(msgid) gettext(msgid)
    112 #else /* !(HAVE_GETTEXT - 0) */
    113 #define _(msgid) msgid
    114 #endif /* !(HAVE_GETTEXT - 0) */
    115 #endif /* !defined _ */
    116 
    117 #ifndef TZ_DOMAIN
    118 #define TZ_DOMAIN "tz"
    119 #endif /* !defined TZ_DOMAIN */
    120 
    121 #ifndef P
    122 #define P(x)	x
    123 #endif /* !defined P */
    124 
    125 extern char **	environ;
    126 extern int	getopt P((int argc, char * const argv[],
    127 			  const char * options));
    128 extern char *	optarg;
    129 extern int	optind;
    130 
    131 static const char *	abbr P((struct tm * tmp));
    132 static long	delta P((struct tm * newp, struct tm * oldp));
    133 static time_t	hunt P((char * name, time_t lot, time_t	hit));
    134 int		main P((int, char **));
    135 static size_t	longest;
    136 static char *	progname;
    137 static void	show P((char * zone, time_t t, int v));
    138 
    139 int
    140 main(argc, argv)
    141 int	argc;
    142 char *	argv[];
    143 {
    144 	register int		i;
    145 	register int		c;
    146 	register int		vflag;
    147 	register char *		cutoff;
    148 	register int		cutyear;
    149 	register long		cuttime;
    150 	char **			fakeenv;
    151 	time_t			now;
    152 	time_t			t;
    153 	time_t			newt;
    154 	time_t			hibit;
    155 	struct tm		tm;
    156 	struct tm		newtm;
    157 
    158 	INITIALIZE(cuttime);
    159 #if HAVE_GETTEXT - 0
    160 	(void) setlocale(LC_MESSAGES, "");
    161 #ifdef TZ_DOMAINDIR
    162 	(void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
    163 #endif /* defined(TEXTDOMAINDIR) */
    164 	(void) textdomain(TZ_DOMAIN);
    165 #endif /* HAVE_GETTEXT - 0 */
    166 	progname = argv[0];
    167 	vflag = 0;
    168 	cutoff = NULL;
    169 	while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
    170 		if (c == 'v')
    171 			vflag = 1;
    172 		else	cutoff = optarg;
    173 	if ((c != EOF && c != -1) ||
    174 		(optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
    175 			(void) fprintf(stderr,
    176 _("%s: usage is %s [ -v ] [ -c cutoff ] zonename ...\n"),
    177 				argv[0], argv[0]);
    178 			(void) exit(EXIT_FAILURE);
    179 	}
    180 	if (cutoff != NULL) {
    181 		int	y;
    182 
    183 		cutyear = atoi(cutoff);
    184 		cuttime = 0;
    185 		for (y = EPOCH_YEAR; y < cutyear; ++y)
    186 			cuttime += DAYSPERNYEAR + isleap(y);
    187 		cuttime *= SECSPERHOUR * HOURSPERDAY;
    188 	}
    189 	(void) time(&now);
    190 	longest = 0;
    191 	for (i = optind; i < argc; ++i)
    192 		if (strlen(argv[i]) > longest)
    193 			longest = strlen(argv[i]);
    194 	for (hibit = 1; (hibit << 1) != 0; hibit <<= 1)
    195 		continue;
    196 	{
    197 		register int	from;
    198 		register int	to;
    199 
    200 		for (i = 0;  environ[i] != NULL;  ++i)
    201 			continue;
    202 		fakeenv = (char **) malloc((size_t) ((i + 2) *
    203 			sizeof *fakeenv));
    204 		if (fakeenv == NULL ||
    205 			(fakeenv[0] = (char *) malloc(longest + 4)) == NULL) {
    206 					(void) perror(progname);
    207 					(void) exit(EXIT_FAILURE);
    208 		}
    209 		to = 0;
    210 		(void)strcpy(fakeenv[to++], "TZ=");	/* XXX strcpy is safe */
    211 		for (from = 0; environ[from] != NULL; ++from)
    212 			if (strncmp(environ[from], "TZ=", 3) != 0)
    213 				fakeenv[to++] = environ[from];
    214 		fakeenv[to] = NULL;
    215 		environ = fakeenv;
    216 	}
    217 	for (i = optind; i < argc; ++i) {
    218 		static char	buf[MAX_STRING_LENGTH];
    219 
    220 		(void) strcpy(&fakeenv[0][3], argv[i]);	/* XXX strcpy is safe */
    221 		if (!vflag) {
    222 			show(argv[i], now, FALSE);
    223 			continue;
    224 		}
    225 		/*
    226 		** Get lowest value of t.
    227 		*/
    228 		t = hibit;
    229 		if (t > 0)		/* time_t is unsigned */
    230 			t = 0;
    231 		show(argv[i], t, TRUE);
    232 		t += SECSPERHOUR * HOURSPERDAY;
    233 		show(argv[i], t, TRUE);
    234 		tm = *localtime(&t);
    235 		(void) strlcpy(buf, abbr(&tm), (sizeof buf));
    236 		for ( ; ; ) {
    237 			if (cutoff != NULL && t >= cuttime)
    238 				break;
    239 			newt = t + SECSPERHOUR * 12;
    240 			if (cutoff != NULL && newt >= cuttime)
    241 				break;
    242 			if (newt <= t)
    243 				break;
    244 			newtm = *localtime(&newt);
    245 			if (delta(&newtm, &tm) != (newt - t) ||
    246 				newtm.tm_isdst != tm.tm_isdst ||
    247 				strcmp(abbr(&newtm), buf) != 0) {
    248 					newt = hunt(argv[i], t, newt);
    249 					newtm = *localtime(&newt);
    250 					(void) strlcpy(buf, abbr(&newtm),
    251 						(sizeof buf));
    252 			}
    253 			t = newt;
    254 			tm = newtm;
    255 		}
    256 		/*
    257 		** Get highest value of t.
    258 		*/
    259 		t = ~((time_t) 0);
    260 		if (t < 0)		/* time_t is signed */
    261 			t &= ~hibit;
    262 		t -= SECSPERHOUR * HOURSPERDAY;
    263 		show(argv[i], t, TRUE);
    264 		t += SECSPERHOUR * HOURSPERDAY;
    265 		show(argv[i], t, TRUE);
    266 	}
    267 	if (fflush(stdout) || ferror(stdout)) {
    268 		(void) fprintf(stderr, "%s: ", argv[0]);
    269 		(void) perror(_("Error writing standard output"));
    270 		(void) exit(EXIT_FAILURE);
    271 	}
    272 	exit(EXIT_SUCCESS);
    273 
    274 	/* gcc -Wall pacifier */
    275 	for ( ; ; )
    276 		continue;
    277 }
    278 
    279 static time_t
    280 hunt(name, lot, hit)
    281 char *	name;
    282 time_t	lot;
    283 time_t	hit;
    284 {
    285 	time_t		t;
    286 	struct tm	lotm;
    287 	struct tm	tm;
    288 	static char	loab[MAX_STRING_LENGTH];
    289 
    290 	lotm = *localtime(&lot);
    291 	(void) strlcpy(loab, abbr(&lotm), (sizeof loab));
    292 	while ((hit - lot) >= 2) {
    293 		t = lot / 2 + hit / 2;
    294 		if (t <= lot)
    295 			++t;
    296 		else if (t >= hit)
    297 			--t;
    298 		tm = *localtime(&t);
    299 		if (delta(&tm, &lotm) == (t - lot) &&
    300 			tm.tm_isdst == lotm.tm_isdst &&
    301 			strcmp(abbr(&tm), loab) == 0) {
    302 				lot = t;
    303 				lotm = tm;
    304 		} else	hit = t;
    305 	}
    306 	show(name, lot, TRUE);
    307 	show(name, hit, TRUE);
    308 	return hit;
    309 }
    310 
    311 /*
    312 ** Thanks to Paul Eggert (eggert (at) twinsun.com) for logic used in delta.
    313 */
    314 
    315 static long
    316 delta(newp, oldp)
    317 struct tm *	newp;
    318 struct tm *	oldp;
    319 {
    320 	long	result;
    321 	int	tmy;
    322 
    323 	if (newp->tm_year < oldp->tm_year)
    324 		return -delta(oldp, newp);
    325 	result = 0;
    326 	for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
    327 		result += DAYSPERNYEAR + isleap(tmy + TM_YEAR_BASE);
    328 	result += newp->tm_yday - oldp->tm_yday;
    329 	result *= HOURSPERDAY;
    330 	result += newp->tm_hour - oldp->tm_hour;
    331 	result *= MINSPERHOUR;
    332 	result += newp->tm_min - oldp->tm_min;
    333 	result *= SECSPERMIN;
    334 	result += newp->tm_sec - oldp->tm_sec;
    335 	return result;
    336 }
    337 
    338 static void
    339 show(zone, t, v)
    340 char *	zone;
    341 time_t	t;
    342 int	v;
    343 {
    344 	struct tm *	tmp;
    345 
    346 	(void) printf("%-*s  ", (int) longest, zone);
    347 	if (v)
    348 		(void) printf("%.24s UTC = ", asctime(gmtime(&t)));
    349 	tmp = localtime(&t);
    350 	(void) printf("%.24s", asctime(tmp));
    351 	if (*abbr(tmp) != '\0')
    352 		(void) printf(" %s", abbr(tmp));
    353 	if (v) {
    354 		(void) printf(" isdst=%d", tmp->tm_isdst);
    355 #ifdef TM_GMTOFF
    356 		(void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
    357 #endif /* defined TM_GMTOFF */
    358 	}
    359 	(void) printf("\n");
    360 }
    361 
    362 static const char *
    363 abbr(tmp)
    364 struct tm *	tmp;
    365 {
    366 	register const char *	result;
    367 	static const char	nada;
    368 
    369 	if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
    370 		return &nada;
    371 	result = tzname[tmp->tm_isdst];
    372 	return (result == NULL) ? &nada : result;
    373 }
    374