Add strstr with unaligned loads. Fixes bug 12100.
[glibc.git] / timezone / zdump.c
blob9255affc164ca319071d369b3e36a8f50a38f1ee
1 /*
2 ** This file is in the public domain, so clarified as of
3 ** 2009-05-17 by Arthur David Olson.
4 */
6 #include "version.h"
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 "limits.h" /* for CHAR_BIT, LLONG_MAX */
21 #include "ctype.h" /* for isalpha et al. */
22 #ifndef isascii
23 #define isascii(x) 1
24 #endif /* !defined isascii */
26 #ifndef ZDUMP_LO_YEAR
27 #define ZDUMP_LO_YEAR (-500)
28 #endif /* !defined ZDUMP_LO_YEAR */
30 #ifndef ZDUMP_HI_YEAR
31 #define ZDUMP_HI_YEAR 2500
32 #endif /* !defined ZDUMP_HI_YEAR */
34 #ifndef MAX_STRING_LENGTH
35 #define MAX_STRING_LENGTH 1024
36 #endif /* !defined MAX_STRING_LENGTH */
38 #ifndef TRUE
39 #define TRUE 1
40 #endif /* !defined TRUE */
42 #ifndef FALSE
43 #define FALSE 0
44 #endif /* !defined FALSE */
46 #ifndef EXIT_SUCCESS
47 #define EXIT_SUCCESS 0
48 #endif /* !defined EXIT_SUCCESS */
50 #ifndef EXIT_FAILURE
51 #define EXIT_FAILURE 1
52 #endif /* !defined EXIT_FAILURE */
54 #ifndef SECSPERMIN
55 #define SECSPERMIN 60
56 #endif /* !defined SECSPERMIN */
58 #ifndef MINSPERHOUR
59 #define MINSPERHOUR 60
60 #endif /* !defined MINSPERHOUR */
62 #ifndef SECSPERHOUR
63 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
64 #endif /* !defined SECSPERHOUR */
66 #ifndef HOURSPERDAY
67 #define HOURSPERDAY 24
68 #endif /* !defined HOURSPERDAY */
70 #ifndef EPOCH_YEAR
71 #define EPOCH_YEAR 1970
72 #endif /* !defined EPOCH_YEAR */
74 #ifndef TM_YEAR_BASE
75 #define TM_YEAR_BASE 1900
76 #endif /* !defined TM_YEAR_BASE */
78 #ifndef DAYSPERNYEAR
79 #define DAYSPERNYEAR 365
80 #endif /* !defined DAYSPERNYEAR */
82 #ifndef isleap
83 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
84 #endif /* !defined isleap */
86 #ifndef isleap_sum
88 ** See tzfile.h for details on isleap_sum.
90 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
91 #endif /* !defined isleap_sum */
93 #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
94 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
95 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
97 #ifndef HAVE_GETTEXT
98 #define HAVE_GETTEXT 0
99 #endif
100 #if HAVE_GETTEXT
101 #include "locale.h" /* for setlocale */
102 #include "libintl.h"
103 #endif /* HAVE_GETTEXT */
105 #ifndef GNUC_or_lint
106 #ifdef lint
107 #define GNUC_or_lint
108 #else /* !defined lint */
109 #ifdef __GNUC__
110 #define GNUC_or_lint
111 #endif /* defined __GNUC__ */
112 #endif /* !defined lint */
113 #endif /* !defined GNUC_or_lint */
115 #ifndef INITIALIZE
116 #ifdef GNUC_or_lint
117 #define INITIALIZE(x) ((x) = 0)
118 #else /* !defined GNUC_or_lint */
119 #define INITIALIZE(x)
120 #endif /* !defined GNUC_or_lint */
121 #endif /* !defined INITIALIZE */
123 #if 2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__)
124 # define ATTRIBUTE_PURE __attribute__ ((__pure__))
125 #else
126 # define ATTRIBUTE_PURE /* empty */
127 #endif
130 ** For the benefit of GNU folk...
131 ** `_(MSGID)' uses the current locale's message library string for MSGID.
132 ** The default is to use gettext if available, and use MSGID otherwise.
135 #ifndef _
136 #if HAVE_GETTEXT
137 #define _(msgid) gettext(msgid)
138 #else /* !HAVE_GETTEXT */
139 #define _(msgid) msgid
140 #endif /* !HAVE_GETTEXT */
141 #endif /* !defined _ */
143 #ifndef TZ_DOMAIN
144 #define TZ_DOMAIN "tz"
145 #endif /* !defined TZ_DOMAIN */
147 extern char ** environ;
148 extern int getopt(int argc, char * const argv[],
149 const char * options);
150 extern char * optarg;
151 extern int optind;
152 extern char * tzname[2];
154 /* The minimum and maximum finite time values. Shift 'long long' or
155 'long' instead of 'time_t'; this avoids compile-time errors when
156 time_t is floating-point. In practice, 'long long' is wide enough. */
157 static time_t const absolute_min_time =
158 ((time_t) 0.5 == 0.5
159 ? (sizeof (time_t) == sizeof (float) ? (time_t) -FLT_MAX
160 : sizeof (time_t) == sizeof (double) ? (time_t) -DBL_MAX
161 : sizeof (time_t) == sizeof (long double) ? (time_t) -LDBL_MAX
162 : 0)
163 : (time_t) -1 < 0
164 #ifdef LLONG_MAX
165 ? (time_t) ((long long) -1 << (CHAR_BIT * sizeof (time_t) - 1))
166 #else
167 ? (time_t) ((long) -1 << (CHAR_BIT * sizeof (time_t) - 1))
168 #endif
169 : 0);
170 static time_t const absolute_max_time =
171 ((time_t) 0.5 == 0.5
172 ? (sizeof (time_t) == sizeof (float) ? (time_t) FLT_MAX
173 : sizeof (time_t) == sizeof (double) ? (time_t) DBL_MAX
174 : sizeof (time_t) == sizeof (long double) ? (time_t) LDBL_MAX
175 : -1)
176 : (time_t) -1 < 0
177 #ifdef LLONG_MAX
178 ? (time_t) (- (~ 0 < 0) - ((long long) -1 << (CHAR_BIT * sizeof (time_t) - 1)))
179 #else
180 ? (time_t) (- (~ 0 < 0) - ((long) -1 << (CHAR_BIT * sizeof (time_t) - 1)))
181 #endif
182 : (time_t) -1);
183 static size_t longest;
184 static char * progname;
185 static int warned;
187 static char * abbr(struct tm * tmp);
188 static void abbrok(const char * abbrp, const char * zone);
189 static long delta(struct tm * newp, struct tm * oldp) ATTRIBUTE_PURE;
190 static void dumptime(const struct tm * tmp);
191 static time_t hunt(char * name, time_t lot, time_t hit);
192 static void checkabsolutes(void);
193 static void show(char * zone, time_t t, int v);
194 static const char * tformat(void);
195 static time_t yeartot(long y) ATTRIBUTE_PURE;
197 #ifndef TYPECHECK
198 #define my_localtime localtime
199 #else /* !defined TYPECHECK */
200 static struct tm *
201 my_localtime(time_t *tp)
203 register struct tm * tmp;
205 tmp = localtime(tp);
206 if (tp != NULL && tmp != NULL) {
207 struct tm tm;
208 register time_t t;
210 tm = *tmp;
211 t = mktime(&tm);
212 if (t - *tp >= 1 || *tp - t >= 1) {
213 (void) fflush(stdout);
214 (void) fprintf(stderr, "\n%s: ", progname);
215 (void) fprintf(stderr, tformat(), *tp);
216 (void) fprintf(stderr, " ->");
217 (void) fprintf(stderr, " year=%d", tmp->tm_year);
218 (void) fprintf(stderr, " mon=%d", tmp->tm_mon);
219 (void) fprintf(stderr, " mday=%d", tmp->tm_mday);
220 (void) fprintf(stderr, " hour=%d", tmp->tm_hour);
221 (void) fprintf(stderr, " min=%d", tmp->tm_min);
222 (void) fprintf(stderr, " sec=%d", tmp->tm_sec);
223 (void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
224 (void) fprintf(stderr, " -> ");
225 (void) fprintf(stderr, tformat(), t);
226 (void) fprintf(stderr, "\n");
229 return tmp;
231 #endif /* !defined TYPECHECK */
233 static void
234 abbrok(const char *const abbrp, const char *const zone)
236 register const char * cp;
237 register const char * wp;
239 if (warned)
240 return;
241 cp = abbrp;
242 wp = NULL;
243 while (isascii((unsigned char) *cp) && isalpha((unsigned char) *cp))
244 ++cp;
245 if (cp - abbrp == 0)
246 wp = _("lacks alphabetic at start");
247 else if (cp - abbrp < 3)
248 wp = _("has fewer than 3 alphabetics");
249 else if (cp - abbrp > 6)
250 wp = _("has more than 6 alphabetics");
251 if (wp == NULL && (*cp == '+' || *cp == '-')) {
252 ++cp;
253 if (isascii((unsigned char) *cp) &&
254 isdigit((unsigned char) *cp))
255 if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
256 ++cp;
257 if (*cp != '\0')
258 wp = _("differs from POSIX standard");
260 if (wp == NULL)
261 return;
262 (void) fflush(stdout);
263 (void) fprintf(stderr,
264 _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
265 progname, zone, abbrp, wp);
266 warned = TRUE;
269 static void
270 usage(FILE * const stream, const int status)
272 (void) fprintf(stream,
273 _("%s: usage is %s [ --version ] [ --help ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n\
275 Report bugs to %s.\n"),
276 progname, progname, REPORT_BUGS_TO);
277 exit(status);
281 main(int argc, char *argv[])
283 register int i;
284 register int c;
285 register int vflag;
286 register char * cutarg;
287 register long cutloyear = ZDUMP_LO_YEAR;
288 register long cuthiyear = ZDUMP_HI_YEAR;
289 register time_t cutlotime;
290 register time_t cuthitime;
291 register char ** fakeenv;
292 time_t now;
293 time_t t;
294 time_t newt;
295 struct tm tm;
296 struct tm newtm;
297 register struct tm * tmp;
298 register struct tm * newtmp;
300 INITIALIZE(cutlotime);
301 INITIALIZE(cuthitime);
302 #if HAVE_GETTEXT
303 (void) setlocale(LC_ALL, "");
304 #ifdef TZ_DOMAINDIR
305 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
306 #endif /* defined TEXTDOMAINDIR */
307 (void) textdomain(TZ_DOMAIN);
308 #endif /* HAVE_GETTEXT */
309 progname = argv[0];
310 for (i = 1; i < argc; ++i)
311 if (strcmp(argv[i], "--version") == 0) {
312 (void) printf("zdump %s%s\n", PKGVERSION, TZVERSION);
313 exit(EXIT_SUCCESS);
314 } else if (strcmp(argv[i], "--help") == 0) {
315 usage(stdout, EXIT_SUCCESS);
317 vflag = 0;
318 cutarg = NULL;
319 while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
320 if (c == 'v')
321 vflag = 1;
322 else cutarg = optarg;
323 if ((c != EOF && c != -1) ||
324 (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
325 usage(stderr, EXIT_FAILURE);
327 if (vflag) {
328 if (cutarg != NULL) {
329 long lo;
330 long hi;
331 char dummy;
333 if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
334 cuthiyear = hi;
335 } else if (sscanf(cutarg, "%ld,%ld%c",
336 &lo, &hi, &dummy) == 2) {
337 cutloyear = lo;
338 cuthiyear = hi;
339 } else {
340 (void) fprintf(stderr, _("%s: wild -c argument %s\n"),
341 progname, cutarg);
342 exit(EXIT_FAILURE);
345 checkabsolutes();
346 cutlotime = yeartot(cutloyear);
347 cuthitime = yeartot(cuthiyear);
349 (void) time(&now);
350 longest = 0;
351 for (i = optind; i < argc; ++i)
352 if (strlen(argv[i]) > longest)
353 longest = strlen(argv[i]);
355 register int from;
356 register int to;
358 for (i = 0; environ[i] != NULL; ++i)
359 continue;
360 fakeenv = malloc((i + 2) * sizeof *fakeenv);
361 if (fakeenv == NULL
362 || (fakeenv[0] = malloc(longest + 4)) == NULL) {
363 (void) perror(progname);
364 exit(EXIT_FAILURE);
366 to = 0;
367 (void) strcpy(fakeenv[to++], "TZ=");
368 for (from = 0; environ[from] != NULL; ++from)
369 if (strncmp(environ[from], "TZ=", 3) != 0)
370 fakeenv[to++] = environ[from];
371 fakeenv[to] = NULL;
372 environ = fakeenv;
374 for (i = optind; i < argc; ++i) {
375 static char buf[MAX_STRING_LENGTH];
377 (void) strcpy(&fakeenv[0][3], argv[i]);
378 if (!vflag) {
379 show(argv[i], now, FALSE);
380 continue;
382 warned = FALSE;
383 t = absolute_min_time;
384 show(argv[i], t, TRUE);
385 t += SECSPERHOUR * HOURSPERDAY;
386 show(argv[i], t, TRUE);
387 if (t < cutlotime)
388 t = cutlotime;
389 tmp = my_localtime(&t);
390 if (tmp != NULL) {
391 tm = *tmp;
392 (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
394 for ( ; ; ) {
395 if (t >= cuthitime || t >= cuthitime - SECSPERHOUR * 12)
396 break;
397 newt = t + SECSPERHOUR * 12;
398 newtmp = localtime(&newt);
399 if (newtmp != NULL)
400 newtm = *newtmp;
401 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
402 (delta(&newtm, &tm) != (newt - t) ||
403 newtm.tm_isdst != tm.tm_isdst ||
404 strcmp(abbr(&newtm), buf) != 0)) {
405 newt = hunt(argv[i], t, newt);
406 newtmp = localtime(&newt);
407 if (newtmp != NULL) {
408 newtm = *newtmp;
409 (void) strncpy(buf,
410 abbr(&newtm),
411 (sizeof buf) - 1);
414 t = newt;
415 tm = newtm;
416 tmp = newtmp;
418 t = absolute_max_time;
419 t -= SECSPERHOUR * HOURSPERDAY;
420 show(argv[i], t, TRUE);
421 t += SECSPERHOUR * HOURSPERDAY;
422 show(argv[i], t, TRUE);
424 if (fflush(stdout) || ferror(stdout)) {
425 (void) fprintf(stderr, "%s: ", progname);
426 (void) perror(_("Error writing to standard output"));
427 exit(EXIT_FAILURE);
429 exit(EXIT_SUCCESS);
430 /* If exit fails to exit... */
431 return EXIT_FAILURE;
434 static void
435 checkabsolutes(void)
437 if (absolute_max_time < absolute_min_time) {
438 (void) fprintf(stderr,
439 _("%s: use of -v on system with floating time_t other than float or double\n"),
440 progname);
441 exit(EXIT_FAILURE);
445 static time_t
446 yeartot(const long y)
448 register long myy;
449 register long seconds;
450 register time_t t;
452 myy = EPOCH_YEAR;
453 t = 0;
454 while (myy != y) {
455 if (myy < y) {
456 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
457 ++myy;
458 if (t > absolute_max_time - seconds) {
459 t = absolute_max_time;
460 break;
462 t += seconds;
463 } else {
464 --myy;
465 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
466 if (t < absolute_min_time + seconds) {
467 t = absolute_min_time;
468 break;
470 t -= seconds;
473 return t;
476 static time_t
477 hunt(char *name, time_t lot, time_t hit)
479 time_t t;
480 long diff;
481 struct tm lotm;
482 register struct tm * lotmp;
483 struct tm tm;
484 register struct tm * tmp;
485 char loab[MAX_STRING_LENGTH];
487 lotmp = my_localtime(&lot);
488 if (lotmp != NULL) {
489 lotm = *lotmp;
490 (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
492 for ( ; ; ) {
493 diff = (long) (hit - lot);
494 if (diff < 2)
495 break;
496 t = lot;
497 t += diff / 2;
498 if (t <= lot)
499 ++t;
500 else if (t >= hit)
501 --t;
502 tmp = my_localtime(&t);
503 if (tmp != NULL)
504 tm = *tmp;
505 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
506 (delta(&tm, &lotm) == (t - lot) &&
507 tm.tm_isdst == lotm.tm_isdst &&
508 strcmp(abbr(&tm), loab) == 0)) {
509 lot = t;
510 lotm = tm;
511 lotmp = tmp;
512 } else hit = t;
514 show(name, lot, TRUE);
515 show(name, hit, TRUE);
516 return hit;
520 ** Thanks to Paul Eggert for logic used in delta.
523 static long
524 delta(struct tm * newp, struct tm *oldp)
526 register long result;
527 register int tmy;
529 if (newp->tm_year < oldp->tm_year)
530 return -delta(oldp, newp);
531 result = 0;
532 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
533 result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
534 result += newp->tm_yday - oldp->tm_yday;
535 result *= HOURSPERDAY;
536 result += newp->tm_hour - oldp->tm_hour;
537 result *= MINSPERHOUR;
538 result += newp->tm_min - oldp->tm_min;
539 result *= SECSPERMIN;
540 result += newp->tm_sec - oldp->tm_sec;
541 return result;
544 static void
545 show(char *zone, time_t t, int v)
547 register struct tm * tmp;
549 (void) printf("%-*s ", (int) longest, zone);
550 if (v) {
551 tmp = gmtime(&t);
552 if (tmp == NULL) {
553 (void) printf(tformat(), t);
554 } else {
555 dumptime(tmp);
556 (void) printf(" UTC");
558 (void) printf(" = ");
560 tmp = my_localtime(&t);
561 dumptime(tmp);
562 if (tmp != NULL) {
563 if (*abbr(tmp) != '\0')
564 (void) printf(" %s", abbr(tmp));
565 if (v) {
566 (void) printf(" isdst=%d", tmp->tm_isdst);
567 #ifdef TM_GMTOFF
568 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
569 #endif /* defined TM_GMTOFF */
572 (void) printf("\n");
573 if (tmp != NULL && *abbr(tmp) != '\0')
574 abbrok(abbr(tmp), zone);
577 static char *
578 abbr(struct tm *tmp)
580 register char * result;
581 static char nada;
583 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
584 return &nada;
585 result = tzname[tmp->tm_isdst];
586 return (result == NULL) ? &nada : result;
590 ** The code below can fail on certain theoretical systems;
591 ** it works on all known real-world systems as of 2004-12-30.
594 static const char *
595 tformat(void)
597 if (0.5 == (time_t) 0.5) { /* floating */
598 if (sizeof (time_t) > sizeof (double))
599 return "%Lg";
600 return "%g";
602 if (0 > (time_t) -1) { /* signed */
603 if (sizeof (time_t) > sizeof (long))
604 return "%lld";
605 if (sizeof (time_t) > sizeof (int))
606 return "%ld";
607 return "%d";
609 if (sizeof (time_t) > sizeof (unsigned long))
610 return "%llu";
611 if (sizeof (time_t) > sizeof (unsigned int))
612 return "%lu";
613 return "%u";
616 static void
617 dumptime(register const struct tm *timeptr)
619 static const char wday_name[][3] = {
620 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
622 static const char mon_name[][3] = {
623 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
624 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
626 register const char * wn;
627 register const char * mn;
628 register int lead;
629 register int trail;
631 if (timeptr == NULL) {
632 (void) printf("NULL");
633 return;
636 ** The packaged versions of localtime and gmtime never put out-of-range
637 ** values in tm_wday or tm_mon, but since this code might be compiled
638 ** with other (perhaps experimental) versions, paranoia is in order.
640 if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
641 (int) (sizeof wday_name / sizeof wday_name[0]))
642 wn = "???";
643 else wn = wday_name[timeptr->tm_wday];
644 if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
645 (int) (sizeof mon_name / sizeof mon_name[0]))
646 mn = "???";
647 else mn = mon_name[timeptr->tm_mon];
648 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
649 wn, mn,
650 timeptr->tm_mday, timeptr->tm_hour,
651 timeptr->tm_min, timeptr->tm_sec);
652 #define DIVISOR 10
653 trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
654 lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
655 trail / DIVISOR;
656 trail %= DIVISOR;
657 if (trail < 0 && lead > 0) {
658 trail += DIVISOR;
659 --lead;
660 } else if (lead < 0 && trail > 0) {
661 trail -= DIVISOR;
662 ++lead;
664 if (lead == 0)
665 (void) printf("%d", trail);
666 else (void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));