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