1 /*
2 ** This file is in the public domain, so clarified as of
3 ** 2009-05-17 by Arthur David Olson.
4 */
5
6 #include "version.h"
7
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.
12 **
13 ** However, include private.h when debugging, so that it overrides
14 ** time_t consistently with the rest of the package.
15 */
16
17 #ifdef time_tz
18 # include "private.h"
19 #endif
20
21 #include <stdbool.h>
22
23 #include "stdio.h" /* for stdout, stderr, perror */
24 #include "string.h" /* for strcpy */
25 #include "sys/types.h" /* for time_t */
26 #include "time.h" /* for struct tm */
27 #include "stdlib.h" /* for exit, malloc, atoi */
28 #include "limits.h" /* for CHAR_BIT, LLONG_MAX */
29 #include "ctype.h" /* for isalpha et al. */
30
31 /* Enable extensions and modifications for ICU. */
32 #define ICU
33
34 #ifdef ICU
35 #include "dirent.h"
36 #include "sys/stat.h"
37 #endif
38
39 #ifndef isascii
40 #define isascii(x) 1
41 #endif /* !defined isascii */
42
43 /*
44 ** Substitutes for pre-C99 compilers.
45 ** Much of this section of code is stolen from private.h.
46 */
47
48 #ifndef HAVE_STDINT_H
49 # define HAVE_STDINT_H \
50 (199901 <= __STDC_VERSION__ || 2 < (__GLIBC__ + (0 < __GLIBC_MINOR__)))
51 #endif
52 #if HAVE_STDINT_H
53 # include "stdint.h"
54 #endif
55 #ifndef HAVE_INTTYPES_H
56 # define HAVE_INTTYPES_H HAVE_STDINT_H
57 #endif
58 #if HAVE_INTTYPES_H
59 # include <inttypes.h>
60 #endif
61
62 #ifndef INT_FAST32_MAX
63 # if INT_MAX >> 31 == 0
64 typedef long int_fast32_t;
65 # else
66 typedef int int_fast32_t;
67 # endif
68 #endif
69
70 #ifndef INTMAX_MAX
71 # if defined LLONG_MAX || defined __LONG_LONG_MAX__
72 typedef long long intmax_t;
73 # define strtoimax strtoll
74 # define PRIdMAX "lld"
75 # ifdef LLONG_MAX
76 # define INTMAX_MAX LLONG_MAX
77 # else
78 # define INTMAX_MAX __LONG_LONG_MAX__
79 # endif
80 # else
81 typedef long intmax_t;
82 # define strtoimax strtol
83 # define PRIdMAX "ld"
84 # define INTMAX_MAX LONG_MAX
85 # endif
86 #endif
87
88
89 #ifndef ZDUMP_LO_YEAR
90 #define ZDUMP_LO_YEAR (-500)
91 #endif /* !defined ZDUMP_LO_YEAR */
92
93 #ifndef ZDUMP_HI_YEAR
94 #define ZDUMP_HI_YEAR 2500
95 #endif /* !defined ZDUMP_HI_YEAR */
96
97 #ifndef MAX_STRING_LENGTH
98 #define MAX_STRING_LENGTH 1024
99 #endif /* !defined MAX_STRING_LENGTH */
100
101 #ifndef EXIT_SUCCESS
102 #define EXIT_SUCCESS 0
103 #endif /* !defined EXIT_SUCCESS */
104
105 #ifndef EXIT_FAILURE
106 #define EXIT_FAILURE 1
107 #endif /* !defined EXIT_FAILURE */
108
109 #ifndef SECSPERMIN
110 #define SECSPERMIN 60
111 #endif /* !defined SECSPERMIN */
112
113 #ifndef MINSPERHOUR
114 #define MINSPERHOUR 60
115 #endif /* !defined MINSPERHOUR */
116
117 #ifndef SECSPERHOUR
118 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
119 #endif /* !defined SECSPERHOUR */
120
121 #ifndef HOURSPERDAY
122 #define HOURSPERDAY 24
123 #endif /* !defined HOURSPERDAY */
124
125 #ifndef EPOCH_YEAR
126 #define EPOCH_YEAR 1970
127 #endif /* !defined EPOCH_YEAR */
128
129 #ifndef TM_YEAR_BASE
130 #define TM_YEAR_BASE 1900
131 #endif /* !defined TM_YEAR_BASE */
132
133 #ifndef DAYSPERNYEAR
134 #define DAYSPERNYEAR 365
135 #endif /* !defined DAYSPERNYEAR */
136
137 #ifndef isleap
138 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
139 #endif /* !defined isleap */
140
141 #ifndef isleap_sum
142 /*
143 ** See tzfile.h for details on isleap_sum.
144 */
145 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
146 #endif /* !defined isleap_sum */
147
148 #define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
149 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
150 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
151 #define SECSPER400YEARS (SECSPERNYEAR * (intmax_t) (300 + 3) \
152 + SECSPERLYEAR * (intmax_t) (100 - 3))
153
154 /*
155 ** True if SECSPER400YEARS is known to be representable as an
156 ** intmax_t. It's OK that SECSPER400YEARS_FITS can in theory be false
157 ** even if SECSPER400YEARS is representable, because when that happens
158 ** the code merely runs a bit more slowly, and this slowness doesn't
159 ** occur on any practical platform.
160 */
161 enum { SECSPER400YEARS_FITS = SECSPERLYEAR <= INTMAX_MAX / 400 };
162
163 #ifndef HAVE_GETTEXT
164 #define HAVE_GETTEXT 0
165 #endif
166 #if HAVE_GETTEXT
167 #include "locale.h" /* for setlocale */
168 #include "libintl.h"
169 #endif /* HAVE_GETTEXT */
170
171 #ifndef GNUC_or_lint
172 #ifdef lint
173 #define GNUC_or_lint
174 #else /* !defined lint */
175 #ifdef __GNUC__
176 #define GNUC_or_lint
177 #endif /* defined __GNUC__ */
178 #endif /* !defined lint */
179 #endif /* !defined GNUC_or_lint */
180
181 #if 2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__)
182 # define ATTRIBUTE_PURE __attribute__ ((__pure__))
183 #else
184 # define ATTRIBUTE_PURE /* empty */
185 #endif
186
187 /*
188 ** For the benefit of GNU folk...
189 ** `_(MSGID)' uses the current locale's message library string for MSGID.
190 ** The default is to use gettext if available, and use MSGID otherwise.
191 */
192
193 #ifndef _
194 #if HAVE_GETTEXT
195 #define _(msgid) gettext(msgid)
196 #else /* !HAVE_GETTEXT */
197 #define _(msgid) msgid
198 #endif /* !HAVE_GETTEXT */
199 #endif /* !defined _ */
200
201 #ifndef TZ_DOMAIN
202 #define TZ_DOMAIN "tz"
203 #endif /* !defined TZ_DOMAIN */
204
205 extern char ** environ;
206 extern int getopt(int argc, char * const argv[],
207 const char * options);
208 extern char * optarg;
209 extern int optind;
210 extern char * tzname[2];
211
212 /* The minimum and maximum finite time values. */
213 static time_t const absolute_min_time =
214 ((time_t) -1 < 0
215 ? (time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1)
216 : 0);
217 static time_t const absolute_max_time =
218 ((time_t) -1 < 0
219 ? - (~ 0 < 0) - ((time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1))
220 : -1);
221 static size_t longest;
222 static char * progname;
223 static int warned;
224
225 static char * abbr(struct tm * tmp);
226 static void abbrok(const char * abbrp, const char * zone);
227 static intmax_t delta(struct tm * newp, struct tm * oldp) ATTRIBUTE_PURE;
228 static void dumptime(const struct tm * tmp);
229 static time_t hunt(char * name, time_t lot, time_t hit);
230 static void show(char * zone, time_t t, int v);
231 static const char * tformat(void);
232 static time_t yeartot(intmax_t y) ATTRIBUTE_PURE;
233 #ifdef ICU
234 typedef struct listentry {
235 char * name;
236 struct listentry * next;
237 } listentry;
238
239 static time_t huntICU(char * name, time_t lot, time_t hit, FILE *fp);
240 static void dumptimeICU(FILE * fp, time_t t);
241 static void showICU(FILE * fp, char * zone, time_t t1, time_t t2);
242 static int getall(struct listentry ** namelist);
243 static void getzones(char * basedir, char * subdir, struct listentry ** last, int * count);
244 #endif
245
246 #ifndef TYPECHECK
247 #define my_localtime localtime
248 #else /* !defined TYPECHECK */
249 static struct tm *
my_localtime(time_t * tp)250 my_localtime(time_t *tp)
251 {
252 register struct tm * tmp;
253
254 tmp = localtime(tp);
255 if (tp != NULL && tmp != NULL) {
256 struct tm tm;
257 register time_t t;
258
259 tm = *tmp;
260 t = mktime(&tm);
261 if (t != *tp) {
262 (void) fflush(stdout);
263 (void) fprintf(stderr, "\n%s: ", progname);
264 (void) fprintf(stderr, tformat(), *tp);
265 (void) fprintf(stderr, " ->");
266 (void) fprintf(stderr, " year=%d", tmp->tm_year);
267 (void) fprintf(stderr, " mon=%d", tmp->tm_mon);
268 (void) fprintf(stderr, " mday=%d", tmp->tm_mday);
269 (void) fprintf(stderr, " hour=%d", tmp->tm_hour);
270 (void) fprintf(stderr, " min=%d", tmp->tm_min);
271 (void) fprintf(stderr, " sec=%d", tmp->tm_sec);
272 (void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
273 (void) fprintf(stderr, " -> ");
274 (void) fprintf(stderr, tformat(), t);
275 (void) fprintf(stderr, "\n");
276 }
277 }
278 return tmp;
279 }
280 #endif /* !defined TYPECHECK */
281
282 static void
abbrok(const char * const abbrp,const char * const zone)283 abbrok(const char *const abbrp, const char *const zone)
284 {
285 register const char * cp;
286 register const char * wp;
287
288 if (warned)
289 return;
290 cp = abbrp;
291 wp = NULL;
292 while (isascii((unsigned char) *cp) && isalpha((unsigned char) *cp))
293 ++cp;
294 if (cp - abbrp == 0)
295 wp = _("lacks alphabetic at start");
296 else if (cp - abbrp < 3)
297 wp = _("has fewer than 3 alphabetics");
298 else if (cp - abbrp > 6)
299 wp = _("has more than 6 alphabetics");
300 if (wp == NULL && (*cp == '+' || *cp == '-')) {
301 ++cp;
302 if (isascii((unsigned char) *cp) &&
303 isdigit((unsigned char) *cp))
304 if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
305 ++cp;
306 if (*cp != '\0')
307 wp = _("differs from POSIX standard");
308 }
309 if (wp == NULL)
310 return;
311 (void) fflush(stdout);
312 (void) fprintf(stderr,
313 _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
314 progname, zone, abbrp, wp);
315 warned = true;
316 }
317
318 static void
usage(FILE * const stream,const int status)319 usage(FILE * const stream, const int status)
320 {
321 (void) fprintf(stream,
322 _("%s: usage: %s [--version] [--help] [-{vV}] [-{ct} [lo,]hi] zonename ...\n"
323 "\n"
324 "Report bugs to %s.\n"),
325 progname, progname, REPORT_BUGS_TO);
326 exit(status);
327 }
328
329 int
main(int argc,char * argv[])330 main(int argc, char *argv[])
331 {
332 register int i;
333 register int vflag;
334 register int Vflag;
335 register char * cutarg;
336 register char * cuttimes;
337 register time_t cutlotime;
338 register time_t cuthitime;
339 register char ** fakeenv;
340 time_t now;
341 time_t t;
342 time_t newt;
343 struct tm tm;
344 struct tm newtm;
345 register struct tm * tmp;
346 register struct tm * newtmp;
347 #ifdef ICU
348 int nextopt;
349 char * dirarg;
350 int aflag;
351 int iflag;
352 listentry * namelist = NULL;
353 FILE * fp = stdout;
354 #endif
355
356 cutlotime = absolute_min_time;
357 cuthitime = absolute_max_time;
358 #if HAVE_GETTEXT
359 (void) setlocale(LC_ALL, "");
360 #ifdef TZ_DOMAINDIR
361 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
362 #endif /* defined TEXTDOMAINDIR */
363 (void) textdomain(TZ_DOMAIN);
364 #endif /* HAVE_GETTEXT */
365 progname = argv[0];
366 for (i = 1; i < argc; ++i)
367 if (strcmp(argv[i], "--version") == 0) {
368 (void) printf("zdump %s%s\n", PKGVERSION, TZVERSION);
369 exit(EXIT_SUCCESS);
370 } else if (strcmp(argv[i], "--help") == 0) {
371 usage(stdout, EXIT_SUCCESS);
372 }
373 vflag = Vflag = 0;
374 cutarg = cuttimes = NULL;
375 #ifdef ICU
376 aflag = 0;
377 iflag = 0;
378 dirarg = NULL;
379 for (;;)
380 switch(getopt(argc, argv, "ac:d:it:vV")) {
381 case 'a': aflag = 1; break;
382 case 'c': cutarg = optarg; break;
383 case 'd': dirarg = optarg; break;
384 case 'i': iflag = 1; break;
385 case 't': cuttimes = optarg; break;
386 case 'v': vflag = 1; break;
387 case 'V': Vflag = 1; break;
388 case -1:
389 if (! (optind == argc - 1 && strcmp(argv[optind], "=") == 0))
390 goto arg_processing_done;
391 /* Fall through. */
392 default:
393 (void) fprintf(stderr,
394 _("%s: usage is %s [ --version ] [ -a ] [ -v ] [ -V ] [ -i ] [ -c [loyear,]hiyear ] [ -t [lotime,]hitime] ][ -d dir ] [ zonename ... ]\n"),
395 progname, progname);
396 exit(EXIT_FAILURE);
397 }
398 #else
399 for (;;)
400 switch (getopt(argc, argv, "c:t:vV")) {
401 case 'c': cutarg = optarg; break;
402 case 't': cuttimes = optarg; break;
403 case 'v': vflag = 1; break;
404 case 'V': Vflag = 1; break;
405 case -1:
406 if (! (optind == argc - 1 && strcmp(argv[optind], "=") == 0))
407 goto arg_processing_done;
408 /* Fall through. */
409 default:
410 usage(stderr, EXIT_FAILURE);
411 }
412 #endif
413 arg_processing_done:;
414
415 #ifdef ICU
416 if (dirarg != NULL) {
417 DIR * dp;
418 /* create the output directory */
419 mkdir(dirarg, 0777);
420 if ((dp = opendir(dirarg)) == NULL) {
421 fprintf(stderr, "cannot create the target directory");
422 exit(EXIT_FAILURE);
423 }
424 closedir(dp);
425 }
426 #endif
427
428 if (vflag | Vflag) {
429 intmax_t lo;
430 intmax_t hi;
431 char *loend, *hiend;
432 register intmax_t cutloyear = ZDUMP_LO_YEAR;
433 register intmax_t cuthiyear = ZDUMP_HI_YEAR;
434 if (cutarg != NULL) {
435 lo = strtoimax(cutarg, &loend, 10);
436 if (cutarg != loend && !*loend) {
437 hi = lo;
438 cuthiyear = hi;
439 } else if (cutarg != loend && *loend == ','
440 && (hi = strtoimax(loend + 1, &hiend, 10),
441 loend + 1 != hiend && !*hiend)) {
442 cutloyear = lo;
443 cuthiyear = hi;
444 } else {
445 (void) fprintf(stderr, _("%s: wild -c argument %s\n"),
446 progname, cutarg);
447 exit(EXIT_FAILURE);
448 }
449 }
450 if (cutarg != NULL || cuttimes == NULL) {
451 cutlotime = yeartot(cutloyear);
452 cuthitime = yeartot(cuthiyear);
453 }
454 if (cuttimes != NULL) {
455 lo = strtoimax(cuttimes, &loend, 10);
456 if (cuttimes != loend && !*loend) {
457 hi = lo;
458 if (hi < cuthitime) {
459 if (hi < absolute_min_time)
460 hi = absolute_min_time;
461 cuthitime = hi;
462 }
463 } else if (cuttimes != loend && *loend == ','
464 && (hi = strtoimax(loend + 1, &hiend, 10),
465 loend + 1 != hiend && !*hiend)) {
466 if (cutlotime < lo) {
467 if (absolute_max_time < lo)
468 lo = absolute_max_time;
469 cutlotime = lo;
470 }
471 if (hi < cuthitime) {
472 if (hi < absolute_min_time)
473 hi = absolute_min_time;
474 cuthitime = hi;
475 }
476 } else {
477 (void) fprintf(stderr,
478 _("%s: wild -t argument %s\n"),
479 progname, cuttimes);
480 exit(EXIT_FAILURE);
481 }
482 }
483 }
484
485 #ifdef ICU
486 if (aflag) {
487 /* get all available zones */
488 char ** fakeargv;
489 int i;
490 int count;
491
492 count = getall(&namelist);
493 fakeargv = (char **) malloc((size_t) (argc + count) * sizeof *argv);
494 /*
495 if ((fakeargv = (char **) malloc((size_t) (argc + count) * sizeof *argv)) == NULL) {
496 exit(EXIT_FAILURE);
497 }
498 */
499 for (i = 0; i < argc; i++) {
500 fakeargv[i] = argv[i];
501 }
502 for (i = 0; i < count; i++) {
503 fakeargv[i + argc] = namelist->name;
504 namelist = namelist->next;
505 }
506 argv = fakeargv;
507 argc += count;
508 }
509 #endif
510 (void) time(&now);
511 longest = 0;
512 for (i = optind; i < argc; ++i)
513 if (strlen(argv[i]) > longest)
514 longest = strlen(argv[i]);
515 {
516 register int from;
517 register int to;
518
519 for (i = 0; environ[i] != NULL; ++i)
520 continue;
521 fakeenv = malloc((i + 2) * sizeof *fakeenv);
522 if (fakeenv == NULL
523 || (fakeenv[0] = malloc(longest + 4)) == NULL) {
524 (void) perror(progname);
525 exit(EXIT_FAILURE);
526 }
527 to = 0;
528 (void) strcpy(fakeenv[to++], "TZ=");
529 for (from = 0; environ[from] != NULL; ++from)
530 if (strncmp(environ[from], "TZ=", 3) != 0)
531 fakeenv[to++] = environ[from];
532 fakeenv[to] = NULL;
533 environ = fakeenv;
534 }
535 for (i = optind; i < argc; ++i) {
536 static char buf[MAX_STRING_LENGTH];
537
538 (void) strcpy(&fakeenv[0][3], argv[i]);
539 if (! (vflag | Vflag)) {
540 show(argv[i], now, false);
541 continue;
542 }
543 #ifdef ICU
544 fp = NULL;
545 if (iflag) {
546 if (dirarg == NULL) {
547 /* we want to display a zone name here */
548 if (i != optind) {
549 printf("\n");
550 }
551 printf("ZONE: %s\n", argv[i]);
552 } else {
553 int zstart;
554 char path[FILENAME_MAX + 1];
555 strcpy(path, dirarg);
556 strcat(path, "/");
557 zstart = strlen(path);
558 strcat(path, argv[i]);
559 /* replace '/' with '-' */
560 while(path[++zstart] != 0) {
561 if (path[zstart] == '/') {
562 path[zstart] = '-';
563 }
564 }
565 if ((fp = fopen(path, "w")) == NULL) {
566 fprintf(stderr, "cannot create output file %s\n", path);
567 exit(EXIT_FAILURE);
568 }
569 }
570 }
571 #endif
572 warned = false;
573 t = absolute_min_time;
574 #ifdef ICU
575 /* skip displaying info for the lowest time, which is actually not
576 * a transition when -i option is set */
577 if (!iflag) {
578 #endif
579 if (!Vflag) {
580 show(argv[i], t, true);
581 t += SECSPERDAY;
582 show(argv[i], t, true);
583 }
584 #ifdef ICU
585 }
586 #endif
587 if (t < cutlotime)
588 t = cutlotime;
589 tmp = my_localtime(&t);
590 if (tmp != NULL) {
591 tm = *tmp;
592 (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
593 }
594 for ( ; ; ) {
595 newt = (t < absolute_max_time - SECSPERDAY / 2
596 ? t + SECSPERDAY / 2
597 : absolute_max_time);
598 if (cuthitime <= newt)
599 break;
600 newtmp = localtime(&newt);
601 if (newtmp != NULL)
602 newtm = *newtmp;
603 #ifdef ICU
604 if (iflag) {
605 /* We do not want to capture transitions just for
606 * abbreviated zone name changes */
607 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
608 (delta(&newtm, &tm) != (newt - t) ||
609 newtm.tm_isdst != tm.tm_isdst)) {
610 newt = huntICU(argv[i], t, newt, fp);
611 newtmp = localtime(&newt);
612 if (newtmp != NULL) {
613 newtm = *newtmp;
614 (void) strncpy(buf,
615 abbr(&newtm),
616 (sizeof buf) - 1);
617 }
618 }
619 } else {
620 #endif
621 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
622 (delta(&newtm, &tm) != (newt - t) ||
623 newtm.tm_isdst != tm.tm_isdst ||
624 strcmp(abbr(&newtm), buf) != 0)) {
625 newt = hunt(argv[i], t, newt);
626 newtmp = localtime(&newt);
627 if (newtmp != NULL) {
628 newtm = *newtmp;
629 (void) strncpy(buf,
630 abbr(&newtm),
631 (sizeof buf) - 1);
632 }
633 }
634 #ifdef ICU
635 }
636 #endif
637 t = newt;
638 tm = newtm;
639 tmp = newtmp;
640 }
641 #ifdef ICU
642 if (!iflag) {
643 /* skip displaying info for the highest time, which is actually not
644 * a transition when -i option is used*/
645 #endif
646 if (!Vflag) {
647 t = absolute_max_time;
648 t -= SECSPERDAY;
649 show(argv[i], t, true);
650 t += SECSPERDAY;
651 show(argv[i], t, true);
652 }
653 #ifdef ICU
654 }
655 /* close file */
656 if (fp != NULL) {
657 fclose(fp);
658 }
659 #endif
660 }
661 if (fflush(stdout) || ferror(stdout)) {
662 (void) fprintf(stderr, "%s: ", progname);
663 (void) perror(_("Error writing to standard output"));
664 exit(EXIT_FAILURE);
665 }
666 #ifdef ICU
667 if (aflag) {
668 struct listentry * entry = namelist;
669 struct listentry * next;
670 while (entry != NULL) {
671 free(entry->name);
672 next = entry->next;
673 free(entry);
674 entry = next;
675 }
676 }
677 #endif
678 exit(EXIT_SUCCESS);
679 /* If exit fails to exit... */
680 return EXIT_FAILURE;
681 }
682
683 static time_t
yeartot(const intmax_t y)684 yeartot(const intmax_t y)
685 {
686 register intmax_t myy, seconds, years;
687 register time_t t;
688
689 myy = EPOCH_YEAR;
690 t = 0;
691 while (myy < y) {
692 if (SECSPER400YEARS_FITS && 400 <= y - myy) {
693 intmax_t diff400 = (y - myy) / 400;
694 if (INTMAX_MAX / SECSPER400YEARS < diff400)
695 return absolute_max_time;
696 seconds = diff400 * SECSPER400YEARS;
697 years = diff400 * 400;
698 } else {
699 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
700 years = 1;
701 }
702 myy += years;
703 if (t > absolute_max_time - seconds)
704 return absolute_max_time;
705 t += seconds;
706 }
707 while (y < myy) {
708 if (SECSPER400YEARS_FITS && y + 400 <= myy && myy < 0) {
709 intmax_t diff400 = (myy - y) / 400;
710 if (INTMAX_MAX / SECSPER400YEARS < diff400)
711 return absolute_min_time;
712 seconds = diff400 * SECSPER400YEARS;
713 years = diff400 * 400;
714 } else {
715 seconds = isleap(myy - 1) ? SECSPERLYEAR : SECSPERNYEAR;
716 years = 1;
717 }
718 myy -= years;
719 if (t < absolute_min_time + seconds)
720 return absolute_min_time;
721 t -= seconds;
722 }
723 return t;
724 }
725
726 static time_t
hunt(char * name,time_t lot,time_t hit)727 hunt(char *name, time_t lot, time_t hit)
728 {
729 time_t t;
730 struct tm lotm;
731 register struct tm * lotmp;
732 struct tm tm;
733 register struct tm * tmp;
734 char loab[MAX_STRING_LENGTH];
735
736 lotmp = my_localtime(&lot);
737 if (lotmp != NULL) {
738 lotm = *lotmp;
739 (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
740 }
741 for ( ; ; ) {
742 time_t diff = hit - lot;
743 if (diff < 2)
744 break;
745 t = lot;
746 t += diff / 2;
747 if (t <= lot)
748 ++t;
749 else if (t >= hit)
750 --t;
751 tmp = my_localtime(&t);
752 if (tmp != NULL)
753 tm = *tmp;
754 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
755 (delta(&tm, &lotm) == (t - lot) &&
756 tm.tm_isdst == lotm.tm_isdst &&
757 strcmp(abbr(&tm), loab) == 0)) {
758 lot = t;
759 lotm = tm;
760 lotmp = tmp;
761 } else hit = t;
762 }
763 show(name, lot, true);
764 show(name, hit, true);
765 return hit;
766 }
767
768 /*
769 ** Thanks to Paul Eggert for logic used in delta.
770 */
771
772 static intmax_t
delta(struct tm * newp,struct tm * oldp)773 delta(struct tm * newp, struct tm *oldp)
774 {
775 register intmax_t result;
776 register int tmy;
777
778 if (newp->tm_year < oldp->tm_year)
779 return -delta(oldp, newp);
780 result = 0;
781 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
782 result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
783 result += newp->tm_yday - oldp->tm_yday;
784 result *= HOURSPERDAY;
785 result += newp->tm_hour - oldp->tm_hour;
786 result *= MINSPERHOUR;
787 result += newp->tm_min - oldp->tm_min;
788 result *= SECSPERMIN;
789 result += newp->tm_sec - oldp->tm_sec;
790 return result;
791 }
792
793 static void
show(char * zone,time_t t,int v)794 show(char *zone, time_t t, int v)
795 {
796 register struct tm * tmp;
797
798 (void) printf("%-*s ", (int) longest, zone);
799 if (v) {
800 tmp = gmtime(&t);
801 if (tmp == NULL) {
802 (void) printf(tformat(), t);
803 } else {
804 dumptime(tmp);
805 (void) printf(" UT");
806 }
807 (void) printf(" = ");
808 }
809 tmp = my_localtime(&t);
810 dumptime(tmp);
811 if (tmp != NULL) {
812 if (*abbr(tmp) != '\0')
813 (void) printf(" %s", abbr(tmp));
814 if (v) {
815 (void) printf(" isdst=%d", tmp->tm_isdst);
816 #ifdef TM_GMTOFF
817 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
818 #endif /* defined TM_GMTOFF */
819 }
820 }
821 (void) printf("\n");
822 if (tmp != NULL && *abbr(tmp) != '\0')
823 abbrok(abbr(tmp), zone);
824 }
825
826 static char *
abbr(struct tm * tmp)827 abbr(struct tm *tmp)
828 {
829 register char * result;
830 static char nada;
831
832 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
833 return &nada;
834 result = tzname[tmp->tm_isdst];
835 return (result == NULL) ? &nada : result;
836 }
837
838 /*
839 ** The code below can fail on certain theoretical systems;
840 ** it works on all known real-world systems as of 2004-12-30.
841 */
842
843 static const char *
tformat(void)844 tformat(void)
845 {
846 if (0 > (time_t) -1) { /* signed */
847 if (sizeof (time_t) == sizeof (intmax_t))
848 return "%"PRIdMAX;
849 if (sizeof (time_t) > sizeof (long))
850 return "%lld";
851 if (sizeof (time_t) > sizeof (int))
852 return "%ld";
853 return "%d";
854 }
855 #ifdef PRIuMAX
856 if (sizeof (time_t) == sizeof (uintmax_t))
857 return "%"PRIuMAX;
858 #endif
859 if (sizeof (time_t) > sizeof (unsigned long))
860 return "%llu";
861 if (sizeof (time_t) > sizeof (unsigned int))
862 return "%lu";
863 return "%u";
864 }
865
866 static void
dumptime(register const struct tm * timeptr)867 dumptime(register const struct tm *timeptr)
868 {
869 static const char wday_name[][3] = {
870 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
871 };
872 static const char mon_name[][3] = {
873 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
874 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
875 };
876 register const char * wn;
877 register const char * mn;
878 register int lead;
879 register int trail;
880
881 if (timeptr == NULL) {
882 (void) printf("NULL");
883 return;
884 }
885 /*
886 ** The packaged versions of localtime and gmtime never put out-of-range
887 ** values in tm_wday or tm_mon, but since this code might be compiled
888 ** with other (perhaps experimental) versions, paranoia is in order.
889 */
890 if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
891 (int) (sizeof wday_name / sizeof wday_name[0]))
892 wn = "???";
893 else wn = wday_name[timeptr->tm_wday];
894 if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
895 (int) (sizeof mon_name / sizeof mon_name[0]))
896 mn = "???";
897 else mn = mon_name[timeptr->tm_mon];
898 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
899 wn, mn,
900 timeptr->tm_mday, timeptr->tm_hour,
901 timeptr->tm_min, timeptr->tm_sec);
902 #define DIVISOR 10
903 trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
904 lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
905 trail / DIVISOR;
906 trail %= DIVISOR;
907 if (trail < 0 && lead > 0) {
908 trail += DIVISOR;
909 --lead;
910 } else if (lead < 0 && trail > 0) {
911 trail -= DIVISOR;
912 ++lead;
913 }
914 if (lead == 0)
915 (void) printf("%d", trail);
916 else (void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));
917 }
918
919 #ifdef ICU
920 static time_t
huntICU(char * name,time_t lot,time_t hit,FILE * fp)921 huntICU(char *name, time_t lot, time_t hit, FILE * fp)
922 {
923 time_t t;
924 long diff;
925 struct tm lotm;
926 register struct tm * lotmp;
927 struct tm tm;
928 register struct tm * tmp;
929 char loab[MAX_STRING_LENGTH];
930
931 lotmp = my_localtime(&lot);
932 if (lotmp != NULL) {
933 lotm = *lotmp;
934 (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
935 }
936 for ( ; ; ) {
937 diff = (long) (hit - lot);
938 if (diff < 2)
939 break;
940 t = lot;
941 t += diff / 2;
942 if (t <= lot)
943 ++t;
944 else if (t >= hit)
945 --t;
946 tmp = my_localtime(&t);
947 if (tmp != NULL)
948 tm = *tmp;
949 /* We do not want to capture transitions just for
950 * abbreviated zone name changes */
951 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
952 (delta(&tm, &lotm) == (t - lot) &&
953 tm.tm_isdst == lotm.tm_isdst)) {
954 lot = t;
955 lotm = tm;
956 lotmp = tmp;
957 } else hit = t;
958 }
959 showICU(fp, name, lot, hit);
960 return hit;
961 }
962
showICU(FILE * fp,char * zone,time_t t1,time_t t2)963 static void showICU(FILE * fp, char *zone, time_t t1, time_t t2)
964 {
965 if (fp == NULL) {
966 fp = stdout;
967 }
968 dumptimeICU(fp, t1);
969 fprintf(fp, " > ");
970 dumptimeICU(fp, t2);
971 fprintf(fp, "\n");
972 }
973
dumptimeICU(FILE * fp,time_t t)974 static void dumptimeICU(FILE * fp, time_t t)
975 {
976 static const char wday_name[][3] = {
977 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
978 };
979 struct tm gmt;
980 struct tm loc;
981 register int lead;
982 register int trail;
983 long offset;
984 long hour, min, sec;
985
986 loc = *my_localtime(&t);
987
988 trail = loc.tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
989 lead = loc.tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR + trail / DIVISOR;
990 trail %= DIVISOR;
991 if (trail < 0 && lead > 0) {
992 trail += DIVISOR;
993 --lead;
994 } else if (lead < 0 && trail > 0) {
995 trail -= DIVISOR;
996 ++lead;
997 }
998
999 fprintf(fp, "%04d-%02d-%02d", lead * DIVISOR + trail, loc.tm_mon + 1, loc.tm_mday);
1000 fprintf(fp, " %.3s ", wday_name[loc.tm_wday]);
1001 fprintf(fp, "%02d:%02d:%02d", loc.tm_hour, loc.tm_min, loc.tm_sec);
1002
1003 gmt = *gmtime(&t);
1004 offset = delta(&loc, &gmt);
1005 if (offset < 0) {
1006 offset = -offset;
1007 fprintf(fp, "-");
1008 } else {
1009 fprintf(fp, "+");
1010 }
1011
1012 sec = offset % 60;
1013 offset = (offset - sec) / 60;
1014 min = offset % 60;
1015 hour = offset / 60;
1016
1017 fprintf(fp, "%02ld", hour);
1018 fprintf(fp, "%02ld", min);
1019 fprintf(fp, "%02ld", sec);
1020 fprintf(fp, "[DST=%d]", loc.tm_isdst);
1021 }
1022
getall(struct listentry ** namelist)1023 static int getall(struct listentry ** namelist) {
1024 int count = 0;
1025 struct listentry dummyentry;
1026 struct listentry * last = &dummyentry;
1027
1028 getzones(TZDIR, NULL, &last, &count);
1029 if (count > 0) {
1030 *namelist = dummyentry.next;
1031 }
1032
1033 return count;
1034 }
1035
getzones(char * basedir,char * relpath,struct listentry ** last,int * count)1036 static void getzones(char * basedir, char * relpath, struct listentry ** last, int * count) {
1037 char path[FILENAME_MAX + 1];
1038 struct dirent * dir;
1039 DIR * dp;
1040
1041 strcpy(path, basedir);
1042 if (relpath != NULL) {
1043 strcat(path, "/");
1044 strcat(path, relpath);
1045 }
1046
1047 if ((dp = opendir(path)) == NULL) {
1048 /* file */
1049 if (strstr(relpath, ".tab") == NULL && strcmp(relpath, "Etc/Unknown") != 0) {
1050 char * pzonename;
1051 listentry * pentry;
1052
1053 if ((pzonename = malloc(strlen(relpath) + 1)) == NULL) {
1054 exit(EXIT_FAILURE);
1055 }
1056 strcpy(pzonename, relpath);
1057
1058 if ((pentry = malloc(sizeof(listentry))) == NULL) {
1059 exit(EXIT_FAILURE);
1060 }
1061
1062 pentry->name = pzonename;
1063 pentry->next = NULL;
1064 (*last)->next = pentry;
1065 *last = pentry;
1066 (*count)++;
1067 }
1068 } else {
1069 /* directory */
1070 while ((dir = readdir(dp)) != NULL) {
1071 char subpath[FILENAME_MAX + 1];
1072
1073 if (strcmp(dir->d_name, ".") == 0
1074 || strcmp(dir->d_name, "..") == 0) {
1075 continue;
1076 }
1077 if (relpath != NULL) {
1078 strcpy(subpath, relpath);
1079 strcat(subpath, "/");
1080 strcat(subpath, dir->d_name);
1081 } else {
1082 strcpy(subpath, dir->d_name);
1083 }
1084 getzones(basedir, subpath, last, count);
1085 }
1086 closedir(dp);
1087 }
1088 }
1089 #endif
1090