xref: /netbsd-src/lib/libc/time/zdump.c (revision fdecd6a253f999ae92b139670d9e15cc9df4497c)
1 /*	$NetBSD: zdump.c,v 1.5 1997/06/18 01:13:00 jtc Exp $	*/
2 
3 #ifndef lint
4 #ifndef NOID
5 static char	elsieid[] = "@(#)zdump.c	7.26";
6 #endif /* !defined NOID */
7 #endif /* !defined lint */
8 
9 /*
10 ** This code has been made independent of the rest of the time
11 ** conversion package to increase confidence in the verification it provides.
12 ** You can use this code to help in verifying other implementations.
13 */
14 
15 #include "stdio.h"	/* for stdout, stderr, perror */
16 #include "string.h"	/* for strcpy */
17 #include "sys/types.h"	/* for time_t */
18 #include "time.h"	/* for struct tm */
19 #include "stdlib.h"	/* for exit, malloc, atoi */
20 
21 #ifndef MAX_STRING_LENGTH
22 #define MAX_STRING_LENGTH	1024
23 #endif /* !defined MAX_STRING_LENGTH */
24 
25 #ifndef TRUE
26 #define TRUE		1
27 #endif /* !defined TRUE */
28 
29 #ifndef FALSE
30 #define FALSE		0
31 #endif /* !defined FALSE */
32 
33 #ifndef EXIT_SUCCESS
34 #define EXIT_SUCCESS	0
35 #endif /* !defined EXIT_SUCCESS */
36 
37 #ifndef EXIT_FAILURE
38 #define EXIT_FAILURE	1
39 #endif /* !defined EXIT_FAILURE */
40 
41 #ifndef SECSPERMIN
42 #define SECSPERMIN	60
43 #endif /* !defined SECSPERMIN */
44 
45 #ifndef MINSPERHOUR
46 #define MINSPERHOUR	60
47 #endif /* !defined MINSPERHOUR */
48 
49 #ifndef SECSPERHOUR
50 #define SECSPERHOUR	(SECSPERMIN * MINSPERHOUR)
51 #endif /* !defined SECSPERHOUR */
52 
53 #ifndef HOURSPERDAY
54 #define HOURSPERDAY	24
55 #endif /* !defined HOURSPERDAY */
56 
57 #ifndef EPOCH_YEAR
58 #define EPOCH_YEAR	1970
59 #endif /* !defined EPOCH_YEAR */
60 
61 #ifndef TM_YEAR_BASE
62 #define TM_YEAR_BASE	1900
63 #endif /* !defined TM_YEAR_BASE */
64 
65 #ifndef DAYSPERNYEAR
66 #define DAYSPERNYEAR	365
67 #endif /* !defined DAYSPERNYEAR */
68 
69 #ifndef isleap
70 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
71 #endif /* !defined isleap */
72 
73 #if HAVE_GETTEXT - 0
74 #include "locale.h"	/* for setlocale */
75 #include "libintl.h"
76 #endif /* HAVE_GETTEXT - 0 */
77 
78 #ifndef GNUC_or_lint
79 #ifdef lint
80 #define GNUC_or_lint
81 #endif /* defined lint */
82 #ifndef lint
83 #ifdef __GNUC__
84 #define GNUC_or_lint
85 #endif /* defined __GNUC__ */
86 #endif /* !defined lint */
87 #endif /* !defined GNUC_or_lint */
88 
89 #ifndef INITIALIZE
90 #ifdef GNUC_or_lint
91 #define INITIALIZE(x)	((x) = 0)
92 #endif /* defined GNUC_or_lint */
93 #ifndef GNUC_or_lint
94 #define INITIALIZE(x)
95 #endif /* !defined GNUC_or_lint */
96 #endif /* !defined INITIALIZE */
97 
98 /*
99 ** For the benefit of GNU folk...
100 ** `_(MSGID)' uses the current locale's message library string for MSGID.
101 ** The default is to use gettext if available, and use MSGID otherwise.
102 */
103 
104 #ifndef _
105 #if HAVE_GETTEXT - 0
106 #define _(msgid) gettext(msgid)
107 #else /* !(HAVE_GETTEXT - 0) */
108 #define _(msgid) msgid
109 #endif /* !(HAVE_GETTEXT - 0) */
110 #endif /* !defined _ */
111 
112 #ifndef TZ_DOMAIN
113 #define TZ_DOMAIN "tz"
114 #endif /* !defined TZ_DOMAIN */
115 
116 #ifndef P
117 #ifdef __STDC__
118 #define P(x)	x
119 #endif /* defined __STDC__ */
120 #ifndef __STDC__
121 #define P(x)	()
122 #endif /* !defined __STDC__ */
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 extern char *	tzname[2];
131 
132 static char *	abbr P((struct tm * tmp));
133 static long	delta P((struct tm * newp, struct tm * oldp));
134 static time_t	hunt P((char * name, time_t lot, time_t	hit));
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) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
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) strncpy(buf, abbr(&newtm),
251 						(sizeof buf) - 1);
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: Error writing standard output "),
269 			argv[0]);
270 		(void) perror(_("standard output"));
271 		(void) exit(EXIT_FAILURE);
272 	}
273 	exit(EXIT_SUCCESS);
274 
275 	/* gcc -Wall pacifier */
276 	for ( ; ; )
277 		continue;
278 }
279 
280 static time_t
281 hunt(name, lot, hit)
282 char *	name;
283 time_t	lot;
284 time_t	hit;
285 {
286 	time_t		t;
287 	struct tm	lotm;
288 	struct tm	tm;
289 	static char	loab[MAX_STRING_LENGTH];
290 
291 	lotm = *localtime(&lot);
292 	(void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
293 	while ((hit - lot) >= 2) {
294 		t = lot / 2 + hit / 2;
295 		if (t <= lot)
296 			++t;
297 		else if (t >= hit)
298 			--t;
299 		tm = *localtime(&t);
300 		if (delta(&tm, &lotm) == (t - lot) &&
301 			tm.tm_isdst == lotm.tm_isdst &&
302 			strcmp(abbr(&tm), loab) == 0) {
303 				lot = t;
304 				lotm = tm;
305 		} else	hit = t;
306 	}
307 	show(name, lot, TRUE);
308 	show(name, hit, TRUE);
309 	return hit;
310 }
311 
312 /*
313 ** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta.
314 */
315 
316 static long
317 delta(newp, oldp)
318 struct tm *	newp;
319 struct tm *	oldp;
320 {
321 	long	result;
322 	int	tmy;
323 
324 	if (newp->tm_year < oldp->tm_year)
325 		return -delta(oldp, newp);
326 	result = 0;
327 	for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
328 		result += DAYSPERNYEAR + isleap(tmy + TM_YEAR_BASE);
329 	result += newp->tm_yday - oldp->tm_yday;
330 	result *= HOURSPERDAY;
331 	result += newp->tm_hour - oldp->tm_hour;
332 	result *= MINSPERHOUR;
333 	result += newp->tm_min - oldp->tm_min;
334 	result *= SECSPERMIN;
335 	result += newp->tm_sec - oldp->tm_sec;
336 	return result;
337 }
338 
339 static void
340 show(zone, t, v)
341 char *	zone;
342 time_t	t;
343 int	v;
344 {
345 	struct tm *	tmp;
346 
347 	(void) printf("%-*s  ", (int) longest, zone);
348 	if (v)
349 		(void) printf("%.24s GMT = ", asctime(gmtime(&t)));
350 	tmp = localtime(&t);
351 	(void) printf("%.24s", asctime(tmp));
352 	if (*abbr(tmp) != '\0')
353 		(void) printf(" %s", abbr(tmp));
354 	if (v) {
355 		(void) printf(" isdst=%d", tmp->tm_isdst);
356 #ifdef TM_GMTOFF
357 		(void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
358 #endif /* defined TM_GMTOFF */
359 	}
360 	(void) printf("\n");
361 }
362 
363 static char *
364 abbr(tmp)
365 struct tm *	tmp;
366 {
367 	register char *	result;
368 	static char	nada;
369 
370 	if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
371 		return &nada;
372 	result = tzname[tmp->tm_isdst];
373 	return (result == NULL) ? &nada : result;
374 }
375