Always check dtv before freeing dtv[-1]
[glibc.git] / timezone / zdump.c
blob67bed06bc3986e54799031b180cf739823470252
1 /*
2 ** This file is in the public domain, so clarified as of
3 ** 2009-05-17 by Arthur David Olson.
4 */
6 static char elsieid[] = "@(#)zdump.c 8.10";
8 /*
9 ** This code has been made independent of the rest of the time
10 ** conversion package to increase confidence in the verification it provides.
11 ** You can use this code to help in verifying other implementations.
14 #include "stdio.h" /* for stdout, stderr, perror */
15 #include "string.h" /* for strcpy */
16 #include "sys/types.h" /* for time_t */
17 #include "time.h" /* for struct tm */
18 #include "stdlib.h" /* for exit, malloc, atoi */
19 #include "float.h" /* for FLT_MAX and DBL_MAX */
20 #include "ctype.h" /* for isalpha et al. */
21 #ifndef isascii
22 #define isascii(x) 1
23 #endif /* !defined isascii */
25 #ifndef ZDUMP_LO_YEAR
26 #define ZDUMP_LO_YEAR (-500)
27 #endif /* !defined ZDUMP_LO_YEAR */
29 #ifndef ZDUMP_HI_YEAR
30 #define ZDUMP_HI_YEAR 2500
31 #endif /* !defined ZDUMP_HI_YEAR */
33 #ifndef MAX_STRING_LENGTH
34 #define MAX_STRING_LENGTH 1024
35 #endif /* !defined MAX_STRING_LENGTH */
37 #ifndef TRUE
38 #define TRUE 1
39 #endif /* !defined TRUE */
41 #ifndef FALSE
42 #define FALSE 0
43 #endif /* !defined FALSE */
45 #ifndef EXIT_SUCCESS
46 #define EXIT_SUCCESS 0
47 #endif /* !defined EXIT_SUCCESS */
49 #ifndef EXIT_FAILURE
50 #define EXIT_FAILURE 1
51 #endif /* !defined EXIT_FAILURE */
53 #ifndef SECSPERMIN
54 #define SECSPERMIN 60
55 #endif /* !defined SECSPERMIN */
57 #ifndef MINSPERHOUR
58 #define MINSPERHOUR 60
59 #endif /* !defined MINSPERHOUR */
61 #ifndef SECSPERHOUR
62 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
63 #endif /* !defined SECSPERHOUR */
65 #ifndef HOURSPERDAY
66 #define HOURSPERDAY 24
67 #endif /* !defined HOURSPERDAY */
69 #ifndef EPOCH_YEAR
70 #define EPOCH_YEAR 1970
71 #endif /* !defined EPOCH_YEAR */
73 #ifndef TM_YEAR_BASE
74 #define TM_YEAR_BASE 1900
75 #endif /* !defined TM_YEAR_BASE */
77 #ifndef DAYSPERNYEAR
78 #define DAYSPERNYEAR 365
79 #endif /* !defined DAYSPERNYEAR */
81 #ifndef isleap
82 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
83 #endif /* !defined isleap */
85 #ifndef isleap_sum
87 ** See tzfile.h for details on isleap_sum.
89 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
90 #endif /* !defined isleap_sum */
92 #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
93 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
94 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
96 #ifndef HAVE_GETTEXT
97 #define HAVE_GETTEXT 0
98 #endif
99 #if HAVE_GETTEXT
100 #include "locale.h" /* for setlocale */
101 #include "libintl.h"
102 #endif /* HAVE_GETTEXT */
104 #ifndef GNUC_or_lint
105 #ifdef lint
106 #define GNUC_or_lint
107 #else /* !defined lint */
108 #ifdef __GNUC__
109 #define GNUC_or_lint
110 #endif /* defined __GNUC__ */
111 #endif /* !defined lint */
112 #endif /* !defined GNUC_or_lint */
114 #ifndef INITIALIZE
115 #ifdef GNUC_or_lint
116 #define INITIALIZE(x) ((x) = 0)
117 #else /* !defined GNUC_or_lint */
118 #define INITIALIZE(x)
119 #endif /* !defined GNUC_or_lint */
120 #endif /* !defined INITIALIZE */
123 ** For the benefit of GNU folk...
124 ** `_(MSGID)' uses the current locale's message library string for MSGID.
125 ** The default is to use gettext if available, and use MSGID otherwise.
128 #ifndef _
129 #if HAVE_GETTEXT
130 #define _(msgid) gettext(msgid)
131 #else /* !HAVE_GETTEXT */
132 #define _(msgid) msgid
133 #endif /* !HAVE_GETTEXT */
134 #endif /* !defined _ */
136 #ifndef TZ_DOMAIN
137 #define TZ_DOMAIN "tz"
138 #endif /* !defined TZ_DOMAIN */
140 extern char ** environ;
141 extern int getopt(int argc, char * const argv[],
142 const char * options);
143 extern char * optarg;
144 extern int optind;
145 extern char * tzname[2];
147 static time_t absolute_min_time;
148 static time_t absolute_max_time;
149 static size_t longest;
150 static char * progname;
151 static int warned;
153 static char * abbr(struct tm * tmp);
154 static void abbrok(const char * abbrp, const char * zone);
155 static long delta(struct tm * newp, struct tm * oldp);
156 static void dumptime(const struct tm * tmp);
157 static time_t hunt(char * name, time_t lot, time_t hit);
158 static void setabsolutes(void);
159 static void show(char * zone, time_t t, int v);
160 static const char * tformat(void);
161 static time_t yeartot(long y);
163 #ifndef TYPECHECK
164 #define my_localtime localtime
165 #else /* !defined TYPECHECK */
166 static struct tm *
167 my_localtime(tp)
168 time_t * tp;
170 register struct tm * tmp;
172 tmp = localtime(tp);
173 if (tp != NULL && tmp != NULL) {
174 struct tm tm;
175 register time_t t;
177 tm = *tmp;
178 t = mktime(&tm);
179 if (t - *tp >= 1 || *tp - t >= 1) {
180 (void) fflush(stdout);
181 (void) fprintf(stderr, "\n%s: ", progname);
182 (void) fprintf(stderr, tformat(), *tp);
183 (void) fprintf(stderr, " ->");
184 (void) fprintf(stderr, " year=%d", tmp->tm_year);
185 (void) fprintf(stderr, " mon=%d", tmp->tm_mon);
186 (void) fprintf(stderr, " mday=%d", tmp->tm_mday);
187 (void) fprintf(stderr, " hour=%d", tmp->tm_hour);
188 (void) fprintf(stderr, " min=%d", tmp->tm_min);
189 (void) fprintf(stderr, " sec=%d", tmp->tm_sec);
190 (void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
191 (void) fprintf(stderr, " -> ");
192 (void) fprintf(stderr, tformat(), t);
193 (void) fprintf(stderr, "\n");
196 return tmp;
198 #endif /* !defined TYPECHECK */
200 static void
201 abbrok(abbrp, zone)
202 const char * const abbrp;
203 const char * const zone;
205 register const char * cp;
206 register char * wp;
208 if (warned)
209 return;
210 cp = abbrp;
211 wp = NULL;
212 while (isascii((unsigned char) *cp) && isalpha((unsigned char) *cp))
213 ++cp;
214 if (cp - abbrp == 0)
215 wp = _("lacks alphabetic at start");
216 else if (cp - abbrp < 3)
217 wp = _("has fewer than 3 alphabetics");
218 else if (cp - abbrp > 6)
219 wp = _("has more than 6 alphabetics");
220 if (wp == NULL && (*cp == '+' || *cp == '-')) {
221 ++cp;
222 if (isascii((unsigned char) *cp) &&
223 isdigit((unsigned char) *cp))
224 if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
225 ++cp;
226 if (*cp != '\0')
227 wp = _("differs from POSIX standard");
229 if (wp == NULL)
230 return;
231 (void) fflush(stdout);
232 (void) fprintf(stderr,
233 _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
234 progname, zone, abbrp, wp);
235 warned = TRUE;
238 static void
239 usage(stream, status)
240 FILE * const stream;
241 const int status;
243 (void) fprintf(stream,
244 _("%s: usage is %s [ --version ] [ --help ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n\
246 Report bugs to tz@elsie.nci.nih.gov.\n"),
247 progname, progname);
248 exit(status);
252 main(argc, argv)
253 int argc;
254 char * argv[];
256 register int i;
257 register int c;
258 register int vflag;
259 register char * cutarg;
260 register long cutloyear = ZDUMP_LO_YEAR;
261 register long cuthiyear = ZDUMP_HI_YEAR;
262 register time_t cutlotime;
263 register time_t cuthitime;
264 register char ** fakeenv;
265 time_t now;
266 time_t t;
267 time_t newt;
268 struct tm tm;
269 struct tm newtm;
270 register struct tm * tmp;
271 register struct tm * newtmp;
273 INITIALIZE(cutlotime);
274 INITIALIZE(cuthitime);
275 #if HAVE_GETTEXT
276 (void) setlocale(LC_ALL, "");
277 #ifdef TZ_DOMAINDIR
278 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
279 #endif /* defined TEXTDOMAINDIR */
280 (void) textdomain(TZ_DOMAIN);
281 #endif /* HAVE_GETTEXT */
282 progname = argv[0];
283 for (i = 1; i < argc; ++i)
284 if (strcmp(argv[i], "--version") == 0) {
285 (void) printf("%s\n", elsieid);
286 exit(EXIT_SUCCESS);
287 } else if (strcmp(argv[i], "--help") == 0) {
288 usage(stdout, EXIT_SUCCESS);
290 vflag = 0;
291 cutarg = NULL;
292 while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
293 if (c == 'v')
294 vflag = 1;
295 else cutarg = optarg;
296 if ((c != EOF && c != -1) ||
297 (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
298 usage(stderr, EXIT_FAILURE);
300 if (vflag) {
301 if (cutarg != NULL) {
302 long lo;
303 long hi;
304 char dummy;
306 if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
307 cuthiyear = hi;
308 } else if (sscanf(cutarg, "%ld,%ld%c",
309 &lo, &hi, &dummy) == 2) {
310 cutloyear = lo;
311 cuthiyear = hi;
312 } else {
313 (void) fprintf(stderr, _("%s: wild -c argument %s\n"),
314 progname, cutarg);
315 exit(EXIT_FAILURE);
318 setabsolutes();
319 cutlotime = yeartot(cutloyear);
320 cuthitime = yeartot(cuthiyear);
322 (void) time(&now);
323 longest = 0;
324 for (i = optind; i < argc; ++i)
325 if (strlen(argv[i]) > longest)
326 longest = strlen(argv[i]);
328 register int from;
329 register int to;
331 for (i = 0; environ[i] != NULL; ++i)
332 continue;
333 fakeenv = (char **) malloc((size_t) ((i + 2) *
334 sizeof *fakeenv));
335 if (fakeenv == NULL ||
336 (fakeenv[0] = (char *) malloc(longest + 4)) == NULL) {
337 (void) perror(progname);
338 exit(EXIT_FAILURE);
340 to = 0;
341 (void) strcpy(fakeenv[to++], "TZ=");
342 for (from = 0; environ[from] != NULL; ++from)
343 if (strncmp(environ[from], "TZ=", 3) != 0)
344 fakeenv[to++] = environ[from];
345 fakeenv[to] = NULL;
346 environ = fakeenv;
348 for (i = optind; i < argc; ++i) {
349 static char buf[MAX_STRING_LENGTH];
351 (void) strcpy(&fakeenv[0][3], argv[i]);
352 if (!vflag) {
353 show(argv[i], now, FALSE);
354 continue;
356 warned = FALSE;
357 t = absolute_min_time;
358 show(argv[i], t, TRUE);
359 t += SECSPERHOUR * HOURSPERDAY;
360 show(argv[i], t, TRUE);
361 if (t < cutlotime)
362 t = cutlotime;
363 tmp = my_localtime(&t);
364 if (tmp != NULL) {
365 tm = *tmp;
366 (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
368 for ( ; ; ) {
369 if (t >= cuthitime || t >= cuthitime - SECSPERHOUR * 12)
370 break;
371 newt = t + SECSPERHOUR * 12;
372 newtmp = localtime(&newt);
373 if (newtmp != NULL)
374 newtm = *newtmp;
375 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
376 (delta(&newtm, &tm) != (newt - t) ||
377 newtm.tm_isdst != tm.tm_isdst ||
378 strcmp(abbr(&newtm), buf) != 0)) {
379 newt = hunt(argv[i], t, newt);
380 newtmp = localtime(&newt);
381 if (newtmp != NULL) {
382 newtm = *newtmp;
383 (void) strncpy(buf,
384 abbr(&newtm),
385 (sizeof buf) - 1);
388 t = newt;
389 tm = newtm;
390 tmp = newtmp;
392 t = absolute_max_time;
393 t -= SECSPERHOUR * HOURSPERDAY;
394 show(argv[i], t, TRUE);
395 t += SECSPERHOUR * HOURSPERDAY;
396 show(argv[i], t, TRUE);
398 if (fflush(stdout) || ferror(stdout)) {
399 (void) fprintf(stderr, "%s: ", progname);
400 (void) perror(_("Error writing to standard output"));
401 exit(EXIT_FAILURE);
403 exit(EXIT_SUCCESS);
404 /* If exit fails to exit... */
405 return EXIT_FAILURE;
408 static void
409 setabsolutes(void)
411 if (0.5 == (time_t) 0.5) {
413 ** time_t is floating.
415 if (sizeof (time_t) == sizeof (float)) {
416 absolute_min_time = (time_t) -FLT_MAX;
417 absolute_max_time = (time_t) FLT_MAX;
418 } else if (sizeof (time_t) == sizeof (double)) {
419 absolute_min_time = (time_t) -DBL_MAX;
420 absolute_max_time = (time_t) DBL_MAX;
421 } else {
422 (void) fprintf(stderr,
423 _("%s: use of -v on system with floating time_t other than float or double\n"),
424 progname);
425 exit(EXIT_FAILURE);
427 } else if (0 > (time_t) -1) {
429 ** time_t is signed. Assume overflow wraps around.
431 time_t t = 0;
432 time_t t1 = 1;
434 while (t < t1) {
435 t = t1;
436 t1 = 2 * t1 + 1;
439 absolute_max_time = t;
440 t = -t;
441 absolute_min_time = t - 1;
442 if (t < absolute_min_time)
443 absolute_min_time = t;
444 } else {
446 ** time_t is unsigned.
448 absolute_min_time = 0;
449 absolute_max_time = absolute_min_time - 1;
453 static time_t
454 yeartot(y)
455 const long y;
457 register long myy;
458 register long seconds;
459 register time_t t;
461 myy = EPOCH_YEAR;
462 t = 0;
463 while (myy != y) {
464 if (myy < y) {
465 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
466 ++myy;
467 if (t > absolute_max_time - seconds) {
468 t = absolute_max_time;
469 break;
471 t += seconds;
472 } else {
473 --myy;
474 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
475 if (t < absolute_min_time + seconds) {
476 t = absolute_min_time;
477 break;
479 t -= seconds;
482 return t;
485 static time_t
486 hunt(char *name, time_t lot, time_t hit)
488 time_t t;
489 long diff;
490 struct tm lotm;
491 register struct tm * lotmp;
492 struct tm tm;
493 register struct tm * tmp;
494 char loab[MAX_STRING_LENGTH];
496 lotmp = my_localtime(&lot);
497 if (lotmp != NULL) {
498 lotm = *lotmp;
499 (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
501 for ( ; ; ) {
502 diff = (long) (hit - lot);
503 if (diff < 2)
504 break;
505 t = lot;
506 t += diff / 2;
507 if (t <= lot)
508 ++t;
509 else if (t >= hit)
510 --t;
511 tmp = my_localtime(&t);
512 if (tmp != NULL)
513 tm = *tmp;
514 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
515 (delta(&tm, &lotm) == (t - lot) &&
516 tm.tm_isdst == lotm.tm_isdst &&
517 strcmp(abbr(&tm), loab) == 0)) {
518 lot = t;
519 lotm = tm;
520 lotmp = tmp;
521 } else hit = t;
523 show(name, lot, TRUE);
524 show(name, hit, TRUE);
525 return hit;
529 ** Thanks to Paul Eggert for logic used in delta.
532 static long
533 delta(newp, oldp)
534 struct tm * newp;
535 struct tm * oldp;
537 register long result;
538 register int tmy;
540 if (newp->tm_year < oldp->tm_year)
541 return -delta(oldp, newp);
542 result = 0;
543 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
544 result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
545 result += newp->tm_yday - oldp->tm_yday;
546 result *= HOURSPERDAY;
547 result += newp->tm_hour - oldp->tm_hour;
548 result *= MINSPERHOUR;
549 result += newp->tm_min - oldp->tm_min;
550 result *= SECSPERMIN;
551 result += newp->tm_sec - oldp->tm_sec;
552 return result;
555 static void
556 show(char *zone, time_t t, int v)
558 register struct tm * tmp;
560 (void) printf("%-*s ", (int) longest, zone);
561 if (v) {
562 tmp = gmtime(&t);
563 if (tmp == NULL) {
564 (void) printf(tformat(), t);
565 } else {
566 dumptime(tmp);
567 (void) printf(" UTC");
569 (void) printf(" = ");
571 tmp = my_localtime(&t);
572 dumptime(tmp);
573 if (tmp != NULL) {
574 if (*abbr(tmp) != '\0')
575 (void) printf(" %s", abbr(tmp));
576 if (v) {
577 (void) printf(" isdst=%d", tmp->tm_isdst);
578 #ifdef TM_GMTOFF
579 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
580 #endif /* defined TM_GMTOFF */
583 (void) printf("\n");
584 if (tmp != NULL && *abbr(tmp) != '\0')
585 abbrok(abbr(tmp), zone);
588 static char *
589 abbr(tmp)
590 struct tm * tmp;
592 register char * result;
593 static char nada;
595 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
596 return &nada;
597 result = tzname[tmp->tm_isdst];
598 return (result == NULL) ? &nada : result;
602 ** The code below can fail on certain theoretical systems;
603 ** it works on all known real-world systems as of 2004-12-30.
606 static const char *
607 tformat(void)
609 if (0.5 == (time_t) 0.5) { /* floating */
610 if (sizeof (time_t) > sizeof (double))
611 return "%Lg";
612 return "%g";
614 if (0 > (time_t) -1) { /* signed */
615 if (sizeof (time_t) > sizeof (long))
616 return "%lld";
617 if (sizeof (time_t) > sizeof (int))
618 return "%ld";
619 return "%d";
621 if (sizeof (time_t) > sizeof (unsigned long))
622 return "%llu";
623 if (sizeof (time_t) > sizeof (unsigned int))
624 return "%lu";
625 return "%u";
628 static void
629 dumptime(timeptr)
630 register const struct tm * timeptr;
632 static const char wday_name[][3] = {
633 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
635 static const char mon_name[][3] = {
636 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
637 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
639 register const char * wn;
640 register const char * mn;
641 register int lead;
642 register int trail;
644 if (timeptr == NULL) {
645 (void) printf("NULL");
646 return;
649 ** The packaged versions of localtime and gmtime never put out-of-range
650 ** values in tm_wday or tm_mon, but since this code might be compiled
651 ** with other (perhaps experimental) versions, paranoia is in order.
653 if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
654 (int) (sizeof wday_name / sizeof wday_name[0]))
655 wn = "???";
656 else wn = wday_name[timeptr->tm_wday];
657 if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
658 (int) (sizeof mon_name / sizeof mon_name[0]))
659 mn = "???";
660 else mn = mon_name[timeptr->tm_mon];
661 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
662 wn, mn,
663 timeptr->tm_mday, timeptr->tm_hour,
664 timeptr->tm_min, timeptr->tm_sec);
665 #define DIVISOR 10
666 trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
667 lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
668 trail / DIVISOR;
669 trail %= DIVISOR;
670 if (trail < 0 && lead > 0) {
671 trail += DIVISOR;
672 --lead;
673 } else if (lead < 0 && trail > 0) {
674 trail -= DIVISOR;
675 ++lead;
677 if (lead == 0)
678 (void) printf("%d", trail);
679 else (void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));