xref: /dragonfly/usr.sbin/zic/zdump.c (revision ce0e08e2)
1 /*
2  * @(#)zdump.c	8.6
3  * $FreeBSD: src/usr.sbin/zic/zdump.c,v 1.7 1999/08/28 01:21:19 peter Exp $
4  * $DragonFly: src/usr.sbin/zic/zdump.c,v 1.5 2008/10/19 20:15:58 swildner Exp $
5  */
6 /*
7 ** This code has been made independent of the rest of the time
8 ** conversion package to increase confidence in the verification it provides.
9 ** You can use this code to help in verifying other implementations.
10 */
11 
12 #include <ctype.h>	/* for isalpha et al. */
13 #include <err.h>
14 #include <float.h>	/* for FLT_MAX and DBL_MAX */
15 #include <stdio.h>	/* for stdout, stderr */
16 #include <stdlib.h>	/* for exit, malloc, atoi */
17 #include <string.h>	/* for strcpy */
18 #include <sys/types.h>	/* for time_t */
19 #include <time.h>	/* for struct tm */
20 #include <unistd.h>
21 
22 #ifndef ZDUMP_LO_YEAR
23 #define ZDUMP_LO_YEAR	(-500)
24 #endif /* !defined ZDUMP_LO_YEAR */
25 
26 #ifndef ZDUMP_HI_YEAR
27 #define ZDUMP_HI_YEAR	2500
28 #endif /* !defined ZDUMP_HI_YEAR */
29 
30 #ifndef MAX_STRING_LENGTH
31 #define MAX_STRING_LENGTH	1024
32 #endif /* !defined MAX_STRING_LENGTH */
33 
34 #ifndef TRUE
35 #define TRUE		1
36 #endif /* !defined TRUE */
37 
38 #ifndef FALSE
39 #define FALSE		0
40 #endif /* !defined FALSE */
41 
42 #ifndef EXIT_SUCCESS
43 #define EXIT_SUCCESS	0
44 #endif /* !defined EXIT_SUCCESS */
45 
46 #ifndef EXIT_FAILURE
47 #define EXIT_FAILURE	1
48 #endif /* !defined EXIT_FAILURE */
49 
50 #ifndef SECSPERMIN
51 #define SECSPERMIN	60
52 #endif /* !defined SECSPERMIN */
53 
54 #ifndef MINSPERHOUR
55 #define MINSPERHOUR	60
56 #endif /* !defined MINSPERHOUR */
57 
58 #ifndef SECSPERHOUR
59 #define SECSPERHOUR	(SECSPERMIN * MINSPERHOUR)
60 #endif /* !defined SECSPERHOUR */
61 
62 #ifndef HOURSPERDAY
63 #define HOURSPERDAY	24
64 #endif /* !defined HOURSPERDAY */
65 
66 #ifndef EPOCH_YEAR
67 #define EPOCH_YEAR	1970
68 #endif /* !defined EPOCH_YEAR */
69 
70 #ifndef TM_YEAR_BASE
71 #define TM_YEAR_BASE	1900
72 #endif /* !defined TM_YEAR_BASE */
73 
74 #ifndef DAYSPERNYEAR
75 #define DAYSPERNYEAR	365
76 #endif /* !defined DAYSPERNYEAR */
77 
78 #ifndef isleap
79 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
80 #endif /* !defined isleap */
81 
82 #ifndef isleap_sum
83 /*
84 ** See tzfile.h for details on isleap_sum.
85 */
86 #define isleap_sum(a, b)	isleap((a) % 400 + (b) % 400)
87 #endif /* !defined isleap_sum */
88 
89 #define SECSPERDAY	((long) SECSPERHOUR * HOURSPERDAY)
90 #define SECSPERNYEAR	(SECSPERDAY * DAYSPERNYEAR)
91 #define SECSPERLYEAR	(SECSPERNYEAR + SECSPERDAY)
92 
93 #ifndef GNUC_or_lint
94 #ifdef lint
95 #define GNUC_or_lint
96 #else /* !defined lint */
97 #ifdef __GNUC__
98 #define GNUC_or_lint
99 #endif /* defined __GNUC__ */
100 #endif /* !defined lint */
101 #endif /* !defined GNUC_or_lint */
102 
103 #ifndef INITIALIZE
104 #ifdef GNUC_or_lint
105 #define INITIALIZE(x)	((x) = 0)
106 #else /* !defined GNUC_or_lint */
107 #define INITIALIZE(x)
108 #endif /* !defined GNUC_or_lint */
109 #endif /* !defined INITIALIZE */
110 
111 /*
112 ** For the benefit of GNU folk...
113 ** `_(MSGID)' uses the current locale's message library string for MSGID.
114 ** The default is to use gettext if available, and use MSGID otherwise.
115 */
116 
117 #ifndef _
118 #define _(msgid) msgid
119 #endif /* !defined _ */
120 
121 #ifndef TZ_DOMAIN
122 #define TZ_DOMAIN "tz"
123 #endif /* !defined TZ_DOMAIN */
124 
125 extern char **	environ;
126 extern char *	tzname[2];
127 
128 static time_t	 absolute_min_time;
129 static time_t	 absolute_max_time;
130 static size_t	 longest;
131 static int	 warned;
132 
133 static char	*abbr(struct tm *tmp);
134 static void	 abbrok(const char *abbrp, const char *zone);
135 static long	 delta(struct tm *newp, struct tm *oldp);
136 static void	 dumptime(const struct tm *tmp);
137 static time_t	 hunt(char *name, time_t lot, time_t hit);
138 static void	 setabsolutes(void);
139 static void	 show(char *zone, time_t t, int v);
140 static const char *tformat(void);
141 static time_t	 yeartot(long y);
142 static void	 usage(void);
143 
144 #ifndef TYPECHECK
145 #define my_localtime	localtime
146 #else /* !defined TYPECHECK */
147 static struct tm *
148 my_localtime(time_t *tp)
149 {
150 	struct tm *	tmp;
151 
152 	tmp = localtime(tp);
153 	if (tp != NULL && tmp != NULL) {
154 		struct tm	tm;
155 		time_t		t;
156 
157 		tm = *tmp;
158 		t = mktime(&tm);
159 		if (t - *tp >= 1 || *tp - t >= 1) {
160 			fflush(stdout);
161 			fprintf(stderr, "\n%s: ", progname);
162 			fprintf(stderr, tformat(), *tp);
163 			fprintf(stderr, " ->");
164 			fprintf(stderr, " year=%d", tmp->tm_year);
165 			fprintf(stderr, " mon=%d", tmp->tm_mon);
166 			fprintf(stderr, " mday=%d", tmp->tm_mday);
167 			fprintf(stderr, " hour=%d", tmp->tm_hour);
168 			fprintf(stderr, " min=%d", tmp->tm_min);
169 			fprintf(stderr, " sec=%d", tmp->tm_sec);
170 			fprintf(stderr, " isdst=%d", tmp->tm_isdst);
171 			fprintf(stderr, " -> ");
172 			fprintf(stderr, tformat(), t);
173 			fprintf(stderr, "\n");
174 		}
175 	}
176 	return tmp;
177 }
178 #endif /* !defined TYPECHECK */
179 
180 static void
181 abbrok(const char * const abbrp, const char * const zone)
182 {
183 	const char *cp;
184 	char *wp;
185 
186 	if (warned)
187 		return;
188 	cp = abbrp;
189 	wp = NULL;
190 	while (isascii((unsigned char) *cp) && isalpha((unsigned char) *cp))
191 		++cp;
192 	if (cp - abbrp == 0)
193 		wp = _("lacks alphabetic at start");
194 	else if (cp - abbrp < 3)
195 		wp = _("has fewer than 3 alphabetics");
196 	else if (cp - abbrp > 6)
197 		wp = _("has more than 6 alphabetics");
198 	if (wp == NULL && (*cp == '+' || *cp == '-')) {
199 		++cp;
200 		if (isascii((unsigned char) *cp) &&
201 			isdigit((unsigned char) *cp))
202 				if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
203 					++cp;
204 		if (*cp != '\0')
205 			wp = _("differs from POSIX standard");
206 	}
207 	if (wp == NULL)
208 		return;
209 	fflush(stdout);
210 	warnx(_("warning: zone \"%s\" abbreviation \"%s\" %s\n"),
211 		zone, abbrp, wp);
212 	warned = TRUE;
213 }
214 
215 int
216 main(int argc, char *argv[])
217 {
218 	int i;
219 	int c;
220 	int vflag;
221 	char *cutarg;
222 	long cutloyear = ZDUMP_LO_YEAR;
223 	long cuthiyear = ZDUMP_HI_YEAR;
224 	time_t cutlotime;
225 	time_t cuthitime;
226 	char **fakeenv;
227 	time_t now;
228 	time_t t;
229 	time_t newt;
230 	struct tm tm;
231 	struct tm newtm;
232 	struct tm *tmp;
233 	struct tm *newtmp;
234 
235 	INITIALIZE(cutlotime);
236 	INITIALIZE(cuthitime);
237 	vflag = 0;
238 	cutarg = NULL;
239 	while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
240 		if (c == 'v')
241 			vflag = 1;
242 		else	cutarg = optarg;
243 	if ((c != EOF && c != -1) ||
244 		(optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
245 			usage();
246 	}
247 	if (vflag) {
248 		if (cutarg != NULL) {
249 			long	lo;
250 			long	hi;
251 			char	dummy;
252 
253 			if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
254 				cuthiyear = hi;
255 			} else if (sscanf(cutarg, "%ld,%ld%c",
256 				&lo, &hi, &dummy) == 2) {
257 					cutloyear = lo;
258 					cuthiyear = hi;
259 			} else {
260 				errx(EXIT_FAILURE,
261 					_("%s: wild -c argument %s\n"),
262 					cutarg);
263 			}
264 		}
265 		setabsolutes();
266 		cutlotime = yeartot(cutloyear);
267 		cuthitime = yeartot(cuthiyear);
268 	}
269 	time(&now);
270 	longest = 0;
271 	for (i = optind; i < argc; ++i)
272 		if (strlen(argv[i]) > longest)
273 			longest = strlen(argv[i]);
274 	{
275 		int from;
276 		int to;
277 
278 		for (i = 0; environ[i] != NULL;  ++i)
279 			continue;
280 		fakeenv = (char **) malloc((size_t) ((i + 2) *
281 			sizeof *fakeenv));
282 		if (fakeenv == NULL ||
283 			(fakeenv[0] = (char *) malloc((size_t) (longest +
284 				4))) == NULL)
285 					errx(EXIT_FAILURE,
286 					     _("malloc() failed"));
287 		to = 0;
288 		strcpy(fakeenv[to++], "TZ=");
289 		for (from = 0; environ[from] != NULL; ++from)
290 			if (strncmp(environ[from], "TZ=", 3) != 0)
291 				fakeenv[to++] = environ[from];
292 		fakeenv[to] = NULL;
293 		environ = fakeenv;
294 	}
295 	for (i = optind; i < argc; ++i) {
296 		static char	buf[MAX_STRING_LENGTH];
297 
298 		strcpy(&fakeenv[0][3], argv[i]);
299 		if (!vflag) {
300 			show(argv[i], now, FALSE);
301 			continue;
302 		}
303 		warned = FALSE;
304 		t = absolute_min_time;
305 		show(argv[i], t, TRUE);
306 		t += SECSPERHOUR * HOURSPERDAY;
307 		show(argv[i], t, TRUE);
308 		if (t < cutlotime)
309 			t = cutlotime;
310 		tmp = my_localtime(&t);
311 		if (tmp != NULL) {
312 			tm = *tmp;
313 			strncpy(buf, abbr(&tm), (sizeof buf) - 1);
314 		}
315 		for ( ; ; ) {
316 			if (t >= cuthitime)
317 				break;
318 			newt = t + SECSPERHOUR * 12;
319 			if (newt >= cuthitime)
320 				break;
321 			if (newt <= t)
322 				break;
323 			newtmp = localtime(&newt);
324 			if (newtmp != NULL)
325 				newtm = *newtmp;
326 			if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
327 				(delta(&newtm, &tm) != (newt - t) ||
328 				newtm.tm_isdst != tm.tm_isdst ||
329 				strcmp(abbr(&newtm), buf) != 0)) {
330 					newt = hunt(argv[i], t, newt);
331 					newtmp = localtime(&newt);
332 					if (newtmp != NULL) {
333 						newtm = *newtmp;
334 						strncpy(buf,
335 							abbr(&newtm),
336 							(sizeof buf) - 1);
337 					}
338 			}
339 			t = newt;
340 			tm = newtm;
341 			tmp = newtmp;
342 		}
343 		t = absolute_max_time;
344 		t -= SECSPERHOUR * HOURSPERDAY;
345 		show(argv[i], t, TRUE);
346 		t += SECSPERHOUR * HOURSPERDAY;
347 		show(argv[i], t, TRUE);
348 	}
349 	if (fflush(stdout) || ferror(stdout))
350 		errx(EXIT_FAILURE, _("error writing standard output"));
351 	exit(EXIT_SUCCESS);
352 	/* If exit fails to exit... */
353 	return EXIT_FAILURE;
354 }
355 
356 static void
357 setabsolutes(void)
358 {
359 	if (0.5 == (time_t) 0.5) {
360 		/*
361 		** time_t is floating.
362 		*/
363 		if (sizeof (time_t) == sizeof (float)) {
364 			absolute_min_time = (time_t) -FLT_MAX;
365 			absolute_max_time = (time_t) FLT_MAX;
366 		} else if (sizeof (time_t) == sizeof (double)) {
367 			absolute_min_time = (time_t) -DBL_MAX;
368 			absolute_max_time = (time_t) DBL_MAX;
369 		} else {
370 			errx(EXIT_FAILURE,
371 _("use of -v on system with floating time_t other than float or double\n"));
372 		}
373 	} else if (0 > (time_t) -1) {
374 		/*
375 		** time_t is signed.  Assume overflow wraps around.
376 		*/
377 		time_t t = 0;
378 		time_t t1 = 1;
379 
380 		while (t < t1) {
381 			t = t1;
382 			t1 = 2 * t1 + 1;
383 		}
384 
385 		absolute_max_time = t;
386 		t = -t;
387 		absolute_min_time = t - 1;
388 		if (t < absolute_min_time)
389 			absolute_min_time = t;
390 	} else {
391 		/*
392 		** time_t is unsigned.
393 		*/
394 		absolute_min_time = 0;
395 		absolute_max_time = absolute_min_time - 1;
396 	}
397 }
398 
399 static void
400 usage(void)
401 {
402 	fprintf(stderr, _("usage: zdump [-v] [-c cutoff] zonename ...\n"));
403 	exit(EXIT_FAILURE);
404 }
405 
406 static time_t
407 yeartot(const long y)
408 {
409 	long	myy;
410 	long	seconds;
411 	time_t	t;
412 
413 	myy = EPOCH_YEAR;
414 	t = 0;
415 	while (myy != y) {
416 		if (myy < y) {
417 			seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
418 			++myy;
419 			if (t > absolute_max_time - seconds) {
420 				t = absolute_max_time;
421 				break;
422 			}
423 			t += seconds;
424 		} else {
425 			--myy;
426 			seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
427 			if (t < absolute_min_time + seconds) {
428 				t = absolute_min_time;
429 				break;
430 			}
431 			t -= seconds;
432 		}
433 	}
434 	return t;
435 }
436 
437 static time_t
438 hunt(char *name, time_t lot, time_t hit)
439 {
440 	time_t		t;
441 	long		diff;
442 	struct tm	lotm;
443 	struct tm *	lotmp;
444 	struct tm	tm;
445 	struct tm *	tmp;
446 	char			loab[MAX_STRING_LENGTH];
447 
448 	lotmp = my_localtime(&lot);
449 	if (lotmp != NULL) {
450 		lotm = *lotmp;
451 		strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
452 	}
453 	for ( ; ; ) {
454 		diff = (long) (hit - lot);
455 		if (diff < 2)
456 			break;
457 		t = lot;
458 		t += diff / 2;
459 		if (t <= lot)
460 			++t;
461 		else if (t >= hit)
462 			--t;
463 		tmp = my_localtime(&t);
464 		if (tmp != NULL)
465 			tm = *tmp;
466 		if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
467 			(delta(&tm, &lotm) == (t - lot) &&
468 			tm.tm_isdst == lotm.tm_isdst &&
469 			strcmp(abbr(&tm), loab) == 0)) {
470 				lot = t;
471 				lotm = tm;
472 				lotmp = tmp;
473 		} else	hit = t;
474 	}
475 	show(name, lot, TRUE);
476 	show(name, hit, TRUE);
477 	return hit;
478 }
479 
480 /*
481 ** Thanks to Paul Eggert for logic used in delta.
482 */
483 
484 static long
485 delta(struct tm *newp, struct tm *oldp)
486 {
487 	long	result;
488 	int	tmy;
489 
490 	if (newp->tm_year < oldp->tm_year)
491 		return -delta(oldp, newp);
492 	result = 0;
493 	for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
494 		result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
495 	result += newp->tm_yday - oldp->tm_yday;
496 	result *= HOURSPERDAY;
497 	result += newp->tm_hour - oldp->tm_hour;
498 	result *= MINSPERHOUR;
499 	result += newp->tm_min - oldp->tm_min;
500 	result *= SECSPERMIN;
501 	result += newp->tm_sec - oldp->tm_sec;
502 	return result;
503 }
504 
505 static void
506 show(char *zone, time_t t, int v)
507 {
508 	struct tm *	tmp;
509 
510 	printf("%-*s  ", (int) longest, zone);
511 	if (v) {
512 		tmp = gmtime(&t);
513 		if (tmp == NULL) {
514 			printf(tformat(), t);
515 		} else {
516 			dumptime(tmp);
517 			printf(" UTC");
518 		}
519 		printf(" = ");
520 	}
521 	tmp = my_localtime(&t);
522 	dumptime(tmp);
523 	if (tmp != NULL) {
524 		if (*abbr(tmp) != '\0')
525 			printf(" %s", abbr(tmp));
526 		if (v) {
527 			printf(" isdst=%d", tmp->tm_isdst);
528 #ifdef TM_GMTOFF
529 			printf(" gmtoff=%ld", tmp->TM_GMTOFF);
530 #endif /* defined TM_GMTOFF */
531 		}
532 	}
533 	printf("\n");
534 	if (tmp != NULL && *abbr(tmp) != '\0')
535 		abbrok(abbr(tmp), zone);
536 }
537 
538 static char *
539 abbr(struct tm *tmp)
540 {
541 	char * result;
542 	static char nada;
543 
544 	if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
545 		return &nada;
546 	result = tzname[tmp->tm_isdst];
547 	return (result == NULL) ? &nada : result;
548 }
549 
550 /*
551 ** The code below can fail on certain theoretical systems;
552 ** it works on all known real-world systems as of 2004-12-30.
553 */
554 
555 static const char *
556 tformat(void)
557 {
558 	if (0.5 == (time_t) 0.5) {	/* floating */
559 		if (sizeof (time_t) > sizeof (double))
560 			return "%Lg";
561 		return "%g";
562 	}
563 	if (0 > (time_t) -1) {		/* signed */
564 		if (sizeof (time_t) > sizeof (long))
565 			return "%lld";
566 		if (sizeof (time_t) > sizeof (int))
567 			return "%ld";
568 		return "%d";
569 	}
570 	if (sizeof (time_t) > sizeof (unsigned long))
571 		return "%llu";
572 	if (sizeof (time_t) > sizeof (unsigned int))
573 		return "%lu";
574 	return "%u";
575 }
576 
577 static void
578 dumptime(const struct tm *timeptr)
579 {
580 	static const char	wday_name[][3] = {
581 		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
582 	};
583 	static const char	mon_name[][3] = {
584 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
585 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
586 	};
587 	const char *	wn;
588 	const char *	mn;
589 	int		lead;
590 	int		trail;
591 
592 	if (timeptr == NULL) {
593 		printf("NULL");
594 		return;
595 	}
596 	/*
597 	** The packaged versions of localtime and gmtime never put out-of-range
598 	** values in tm_wday or tm_mon, but since this code might be compiled
599 	** with other (perhaps experimental) versions, paranoia is in order.
600 	*/
601 	if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
602 		(int) (sizeof wday_name / sizeof wday_name[0]))
603 			wn = "???";
604 	else		wn = wday_name[timeptr->tm_wday];
605 	if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
606 		(int) (sizeof mon_name / sizeof mon_name[0]))
607 			mn = "???";
608 	else		mn = mon_name[timeptr->tm_mon];
609 	printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
610 		wn, mn,
611 		timeptr->tm_mday, timeptr->tm_hour,
612 		timeptr->tm_min, timeptr->tm_sec);
613 #define DIVISOR	10
614 	trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
615 	lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
616 		trail / DIVISOR;
617 	trail %= DIVISOR;
618 	if (trail < 0 && lead > 0) {
619 		trail += DIVISOR;
620 		--lead;
621 	} else if (lead < 0 && trail > 0) {
622 		trail -= DIVISOR;
623 		++lead;
624 	}
625 	if (lead == 0)
626 		printf("%d", trail);
627 	else	printf("%d%d", lead, ((trail < 0) ? -trail : trail));
628 }
629