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