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