original netbsd's tr(1)
[minix.git] / commands / zoneinfo / zdump.c
blobfdf622dbfef0eb471eee1cba2d591c3cb889aa5d
1 static char elsieid[] = "@(#)zdump.c 7.77";
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 #if HAVE_GETTEXT
92 #include "locale.h" /* for setlocale */
93 #include "libintl.h"
94 #endif /* HAVE_GETTEXT */
96 #ifndef GNUC_or_lint
97 #ifdef lint
98 #define GNUC_or_lint
99 #else /* !defined lint */
100 #ifdef __GNUC__
101 #define GNUC_or_lint
102 #endif /* defined __GNUC__ */
103 #endif /* !defined lint */
104 #endif /* !defined GNUC_or_lint */
106 #ifndef INITIALIZE
107 #ifdef GNUC_or_lint
108 #define INITIALIZE(x) ((x) = 0)
109 #else /* !defined GNUC_or_lint */
110 #define INITIALIZE(x)
111 #endif /* !defined GNUC_or_lint */
112 #endif /* !defined INITIALIZE */
115 ** For the benefit of GNU folk...
116 ** `_(MSGID)' uses the current locale's message library string for MSGID.
117 ** The default is to use gettext if available, and use MSGID otherwise.
120 #ifndef _
121 #if HAVE_GETTEXT
122 #define _(msgid) gettext(msgid)
123 #else /* !HAVE_GETTEXT */
124 #define _(msgid) msgid
125 #endif /* !HAVE_GETTEXT */
126 #endif /* !defined _ */
128 #ifndef TZ_DOMAIN
129 #define TZ_DOMAIN "tz"
130 #endif /* !defined TZ_DOMAIN */
132 #ifndef P
133 #if __STDC__
134 #define P(x) x
135 #else /* !__STDC__ */
136 #define P(x) ()
137 #endif /* !__STDC__ */
138 #endif /* !defined P */
140 extern char ** environ;
141 extern int getopt P((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 P((struct tm * tmp));
154 static void abbrok P((const char * abbrp, const char * zone));
155 static long delta P((struct tm * newp, struct tm * oldp));
156 static void dumptime P((const struct tm * tmp));
157 static time_t hunt P((char * name, time_t lot, time_t hit));
158 static void setabsolutes P((void));
159 static void show P((char * zone, time_t t, int v));
160 static const char * tformat P((void));
161 static time_t yeartot P((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;
239 main( int argc, char * argv[])
241 register int i;
242 register int c;
243 register int vflag;
244 register char * cutarg;
245 register long cutloyear = ZDUMP_LO_YEAR;
246 register long cuthiyear = ZDUMP_HI_YEAR;
247 register time_t cutlotime;
248 register time_t cuthitime;
249 register char ** fakeenv;
250 time_t now;
251 time_t t;
252 time_t newt;
253 struct tm tm;
254 struct tm newtm;
255 register struct tm * tmp;
256 register struct tm * newtmp;
258 INITIALIZE(cutlotime);
259 INITIALIZE(cuthitime);
260 #if HAVE_GETTEXT
261 (void) setlocale(LC_ALL, "");
262 #ifdef TZ_DOMAINDIR
263 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
264 #endif /* defined TEXTDOMAINDIR */
265 (void) textdomain(TZ_DOMAIN);
266 #endif /* HAVE_GETTEXT */
267 progname = argv[0];
268 for (i = 1; i < argc; ++i)
269 if (strcmp(argv[i], "--version") == 0) {
270 (void) printf("%s\n", elsieid);
271 exit(EXIT_SUCCESS);
273 vflag = 0;
274 cutarg = NULL;
275 while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
276 if (c == 'v')
277 vflag = 1;
278 else cutarg = optarg;
279 if ((c != EOF && c != -1) ||
280 (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
281 (void) fprintf(stderr,
282 _("%s: usage is %s [ --version ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n"),
283 progname, progname);
284 exit(EXIT_FAILURE);
286 if (vflag) {
287 if (cutarg != NULL) {
288 long lo;
289 long hi;
290 char dummy;
292 if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
293 cuthiyear = hi;
294 } else if (sscanf(cutarg, "%ld,%ld%c",
295 &lo, &hi, &dummy) == 2) {
296 cutloyear = lo;
297 cuthiyear = hi;
298 } else {
299 (void) fprintf(stderr, _("%s: wild -c argument %s\n"),
300 progname, cutarg);
301 exit(EXIT_FAILURE);
304 setabsolutes();
305 cutlotime = yeartot(cutloyear);
306 cuthitime = yeartot(cuthiyear);
308 (void) time(&now);
309 longest = 0;
310 for (i = optind; i < argc; ++i)
311 if (strlen(argv[i]) > longest)
312 longest = strlen(argv[i]);
314 register int from;
315 register int to;
317 for (i = 0; environ[i] != NULL; ++i)
318 continue;
319 fakeenv = (char **) malloc((size_t) ((i + 2) *
320 sizeof *fakeenv));
321 if (fakeenv == NULL ||
322 (fakeenv[0] = (char *) malloc(longest + 4)) == NULL) {
323 (void) perror(progname);
324 exit(EXIT_FAILURE);
326 to = 0;
327 (void) strcpy(fakeenv[to++], "TZ=");
328 for (from = 0; environ[from] != NULL; ++from)
329 if (strncmp(environ[from], "TZ=", 3) != 0)
330 fakeenv[to++] = environ[from];
331 fakeenv[to] = NULL;
332 environ = fakeenv;
334 for (i = optind; i < argc; ++i) {
335 static char buf[MAX_STRING_LENGTH];
337 (void) strcpy(&fakeenv[0][3], argv[i]);
338 if (!vflag) {
339 show(argv[i], now, FALSE);
340 continue;
342 warned = FALSE;
343 t = absolute_min_time;
344 show(argv[i], t, TRUE);
345 t += SECSPERHOUR * HOURSPERDAY;
346 show(argv[i], t, TRUE);
347 if (t < cutlotime)
348 t = cutlotime;
349 tmp = my_localtime(&t);
350 if (tmp != NULL) {
351 tm = *tmp;
352 (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
354 for ( ; ; ) {
355 if (t >= cuthitime)
356 break;
357 newt = t + SECSPERHOUR * 12;
358 if (newt >= cuthitime)
359 break;
360 if (newt <= t)
361 break;
362 newtmp = localtime(&newt);
363 if (newtmp != NULL)
364 newtm = *newtmp;
365 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
366 (delta(&newtm, &tm) != (newt - t) ||
367 newtm.tm_isdst != tm.tm_isdst ||
368 strcmp(abbr(&newtm), buf) != 0)) {
369 newt = hunt(argv[i], t, newt);
370 newtmp = localtime(&newt);
371 if (newtmp != NULL) {
372 newtm = *newtmp;
373 (void) strncpy(buf,
374 abbr(&newtm),
375 (sizeof buf) - 1);
378 t = newt;
379 tm = newtm;
380 tmp = newtmp;
382 t = absolute_max_time;
383 t -= SECSPERHOUR * HOURSPERDAY;
384 show(argv[i], t, TRUE);
385 t += SECSPERHOUR * HOURSPERDAY;
386 show(argv[i], t, TRUE);
388 if (fflush(stdout) || ferror(stdout)) {
389 (void) fprintf(stderr, "%s: ", progname);
390 (void) perror(_("Error writing standard output"));
391 exit(EXIT_FAILURE);
393 exit(EXIT_SUCCESS);
394 /* If exit fails to exit... */
395 return EXIT_FAILURE;
398 static void
399 setabsolutes()
401 #if 0
402 if (0.5 == (time_t) 0.5) {
404 ** time_t is floating.
406 if (sizeof (time_t) == sizeof (float)) {
407 absolute_min_time = (time_t) -FLT_MAX;
408 absolute_max_time = (time_t) FLT_MAX;
409 } else if (sizeof (time_t) == sizeof (double)) {
410 absolute_min_time = (time_t) -DBL_MAX;
411 absolute_max_time = (time_t) DBL_MAX;
412 } else {
413 (void) fprintf(stderr,
414 _("%s: use of -v on system with floating time_t other than float or double\n"),
415 progname);
416 exit(EXIT_FAILURE);
418 } else
419 #endif
420 if (0 > (time_t) -1) {
422 ** time_t is signed. Assume overflow wraps around.
424 time_t t = 0;
425 time_t t1 = 1;
427 while (t < t1) {
428 t = t1;
429 t1 = 2 * t1 + 1;
432 absolute_max_time = t;
433 t = -t;
434 absolute_min_time = t - 1;
435 if (t < absolute_min_time)
436 absolute_min_time = t;
437 } else {
439 ** time_t is unsigned.
441 absolute_min_time = 0;
442 absolute_max_time = absolute_min_time - 1;
446 static time_t
447 yeartot(y)
448 const long y;
450 register long myy;
451 register long seconds;
452 register time_t t;
454 myy = EPOCH_YEAR;
455 t = 0;
456 while (myy != y) {
457 if (myy < y) {
458 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
459 ++myy;
460 if (t > absolute_max_time - seconds) {
461 t = absolute_max_time;
462 break;
464 t += seconds;
465 } else {
466 --myy;
467 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
468 if (t < absolute_min_time + seconds) {
469 t = absolute_min_time;
470 break;
472 t -= seconds;
475 return t;
478 static time_t
479 #if __STDC__
480 hunt(char *name, time_t lot, time_t hit)
481 #else /* !__STDC__ */
482 hunt(name, lot, hit)
483 char * name;
484 time_t lot;
485 time_t hit;
486 #endif /* !__STDC__ */
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 #if __STDC__
557 show(char *zone, time_t t, int v)
558 #else /* !__STDC__ */
559 show(zone, t, v)
560 char * zone;
561 time_t t;
562 int v;
563 #endif /* !__STDC__ */
565 register struct tm * tmp;
567 (void) printf("%-*s ", (int) longest, zone);
568 if (v) {
569 tmp = gmtime(&t);
570 if (tmp == NULL) {
571 (void) printf(tformat(), t);
572 } else {
573 dumptime(tmp);
574 (void) printf(" UTC");
576 (void) printf(" = ");
578 tmp = my_localtime(&t);
579 dumptime(tmp);
580 if (tmp != NULL) {
581 if (*abbr(tmp) != '\0')
582 (void) printf(" %s", abbr(tmp));
583 if (v) {
584 (void) printf(" isdst=%d", tmp->tm_isdst);
585 #ifdef TM_GMTOFF
586 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
587 #endif /* defined TM_GMTOFF */
590 (void) printf("\n");
591 if (tmp != NULL && *abbr(tmp) != '\0')
592 abbrok(abbr(tmp), zone);
595 static char *
596 abbr(tmp)
597 struct tm * tmp;
599 register char * result;
600 static char nada;
602 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
603 return &nada;
604 result = tzname[tmp->tm_isdst];
605 return (result == NULL) ? &nada : result;
609 ** The code below can fail on certain theoretical systems;
610 ** it works on all known real-world systems as of 2004-12-30.
613 static const char *
614 tformat()
616 if (0.5 == (time_t) 0.5) { /* floating */
617 if (sizeof (time_t) > sizeof (double))
618 return "%Lg";
619 return "%g";
621 if (0 > (time_t) -1) { /* signed */
622 if (sizeof (time_t) > sizeof (long))
623 return "%lld";
624 if (sizeof (time_t) > sizeof (int))
625 return "%ld";
626 return "%d";
628 if (sizeof (time_t) > sizeof (unsigned long))
629 return "%llu";
630 if (sizeof (time_t) > sizeof (unsigned int))
631 return "%lu";
632 return "%u";
635 static void
636 dumptime(timeptr)
637 register const struct tm * timeptr;
639 static const char wday_name[][3] = {
640 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
642 static const char mon_name[][3] = {
643 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
644 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
646 register const char * wn;
647 register const char * mn;
648 register int lead;
649 register int trail;
651 if (timeptr == NULL) {
652 (void) printf("NULL");
653 return;
656 ** The packaged versions of localtime and gmtime never put out-of-range
657 ** values in tm_wday or tm_mon, but since this code might be compiled
658 ** with other (perhaps experimental) versions, paranoia is in order.
660 if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
661 (int) (sizeof wday_name / sizeof wday_name[0]))
662 wn = "???";
663 else wn = wday_name[timeptr->tm_wday];
664 if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
665 (int) (sizeof mon_name / sizeof mon_name[0]))
666 mn = "???";
667 else mn = mon_name[timeptr->tm_mon];
668 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
669 wn, mn,
670 timeptr->tm_mday, timeptr->tm_hour,
671 timeptr->tm_min, timeptr->tm_sec);
672 #define DIVISOR 10
673 trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
674 lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
675 trail / DIVISOR;
676 trail %= DIVISOR;
677 if (trail < 0 && lead > 0) {
678 trail += DIVISOR;
679 --lead;
680 } else if (lead < 0 && trail > 0) {
681 trail -= DIVISOR;
682 ++lead;
684 if (lead == 0)
685 (void) printf("%d", trail);
686 else (void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));