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