Deleted Added
full compact
1,2d0
< static const char elsieid[] = "@(#)zdump.c 7.31";
<
5c3,4
< "$FreeBSD: head/usr.sbin/zic/zdump.c 176407 2008-02-19 07:09:19Z ru $";
---
> "$FreeBSD: head/usr.sbin/zic/zdump.c 192625 2009-05-23 06:31:50Z edwin $";
> static char elsieid[] = "@(#)zdump.c 8.8";
20a20,24
> #include <float.h> /* for FLT_MAX and DBL_MAX */
> #include <ctype.h> /* for isalpha et al. */
> #ifndef isascii
> #define isascii(x) 1
> #endif /* !defined isascii */
21a26,33
> #ifndef ZDUMP_LO_YEAR
> #define ZDUMP_LO_YEAR (-500)
> #endif /* !defined ZDUMP_LO_YEAR */
>
> #ifndef ZDUMP_HI_YEAR
> #define ZDUMP_HI_YEAR 2500
> #endif /* !defined ZDUMP_HI_YEAR */
>
71c83
< #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
---
> #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
74c86,100
< #if HAVE_GETTEXT - 0
---
> #ifndef isleap_sum
> /*
> ** See tzfile.h for details on isleap_sum.
> */
> #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
> #endif /* !defined isleap_sum */
>
> #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
> #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
> #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
>
> #ifndef HAVE_GETTEXT
> #define HAVE_GETTEXT 0
> #endif
> #if HAVE_GETTEXT
77c103
< #endif /* HAVE_GETTEXT - 0 */
---
> #endif /* HAVE_GETTEXT */
82,83c108
< #endif /* defined lint */
< #ifndef lint
---
> #else /* !defined lint */
93,94c118
< #endif /* defined GNUC_or_lint */
< #ifndef GNUC_or_lint
---
> #else /* !defined GNUC_or_lint */
106c130
< #if HAVE_GETTEXT - 0
---
> #if HAVE_GETTEXT
108c132
< #else /* !(HAVE_GETTEXT - 0) */
---
> #else /* !(HAVE_GETTEXT) */
110c134
< #endif /* !(HAVE_GETTEXT - 0) */
---
> #endif /* !(HAVE_GETTEXT) */
117,125d140
< #ifndef P
< #ifdef __STDC__
< #define P(x) x
< #endif /* defined __STDC__ */
< #ifndef __STDC__
< #define P(x) ()
< #endif /* !defined __STDC__ */
< #endif /* !defined P */
<
129,131c144,145
< static char * abbr P((struct tm * tmp));
< static long delta P((struct tm * newp, struct tm * oldp));
< static time_t hunt P((char * name, time_t lot, time_t hit));
---
> static time_t absolute_min_time;
> static time_t absolute_max_time;
133,134c147,148
< static void show P((char * zone, time_t t, int v));
< static void usage(void);
---
> static char * progname;
> static int warned;
135a150,235
> static void usage(const char *progname, FILE *stream, int status);
> static char * abbr(struct tm * tmp);
> static void abbrok(const char * abbrp, const char * zone);
> static long delta(struct tm * newp, struct tm * oldp);
> static void dumptime(const struct tm * tmp);
> static time_t hunt(char * name, time_t lot, time_t hit);
> static void setabsolutes(void);
> static void show(char * zone, time_t t, int v);
> static const char * tformat(void);
> static time_t yeartot(long y);
>
> #ifndef TYPECHECK
> #define my_localtime localtime
> #else /* !defined TYPECHECK */
> static struct tm *
> my_localtime(tp)
> time_t * tp;
> {
> register struct tm * tmp;
>
> tmp = localtime(tp);
> if (tp != NULL && tmp != NULL) {
> struct tm tm;
> register time_t t;
>
> tm = *tmp;
> t = mktime(&tm);
> if (t - *tp >= 1 || *tp - t >= 1) {
> (void) fflush(stdout);
> (void) fprintf(stderr, "\n%s: ", progname);
> (void) fprintf(stderr, tformat(), *tp);
> (void) fprintf(stderr, " ->");
> (void) fprintf(stderr, " year=%d", tmp->tm_year);
> (void) fprintf(stderr, " mon=%d", tmp->tm_mon);
> (void) fprintf(stderr, " mday=%d", tmp->tm_mday);
> (void) fprintf(stderr, " hour=%d", tmp->tm_hour);
> (void) fprintf(stderr, " min=%d", tmp->tm_min);
> (void) fprintf(stderr, " sec=%d", tmp->tm_sec);
> (void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
> (void) fprintf(stderr, " -> ");
> (void) fprintf(stderr, tformat(), t);
> (void) fprintf(stderr, "\n");
> }
> }
> return tmp;
> }
> #endif /* !defined TYPECHECK */
>
> static void
> abbrok(abbrp, zone)
> const char * const abbrp;
> const char * const zone;
> {
> register const char * cp;
> register char * wp;
>
> if (warned)
> return;
> cp = abbrp;
> wp = NULL;
> while (isascii((unsigned char) *cp) && isalpha((unsigned char) *cp))
> ++cp;
> if (cp - abbrp == 0)
> wp = _("lacks alphabetic at start");
> else if (cp - abbrp < 3)
> wp = _("has fewer than 3 alphabetics");
> else if (cp - abbrp > 6)
> wp = _("has more than 6 alphabetics");
> if (wp == NULL && (*cp == '+' || *cp == '-')) {
> ++cp;
> if (isascii((unsigned char) *cp) &&
> isdigit((unsigned char) *cp))
> if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
> ++cp;
> if (*cp != '\0')
> wp = _("differs from POSIX standard");
> }
> if (wp == NULL)
> return;
> (void) fflush(stdout);
> (void) fprintf(stderr,
> _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
> progname, zone, abbrp, wp);
> warned = TRUE;
> }
>
144,147c244,249
< register char * cutoff;
< register int cutyear;
< register long cuttime;
< char ** fakeenv;
---
> register char * cutarg;
> register long cutloyear = ZDUMP_LO_YEAR;
> register long cuthiyear = ZDUMP_HI_YEAR;
> register time_t cutlotime;
> register time_t cuthitime;
> register char ** fakeenv;
151d252
< time_t hibit;
153a255,256
> register struct tm * tmp;
> register struct tm * newtmp;
155,156c258,260
< INITIALIZE(cuttime);
< #if HAVE_GETTEXT - 0
---
> INITIALIZE(cutlotime);
> INITIALIZE(cuthitime);
> #if HAVE_GETTEXT
160c264
< #endif /* defined(TEXTDOMAINDIR) */
---
> #endif /* TEXTDOMAINDIR */
162c266
< #endif /* HAVE_GETTEXT - 0 */
---
> #endif /* HAVE_GETTEXT */
165a270,271
> } else if (strcmp(argv[i], "--help") == 0) {
> usage(progname, stdout, EXIT_SUCCESS);
168c274
< cutoff = NULL;
---
> cutarg = NULL;
172c278
< else cutoff = optarg;
---
> else cutarg = optarg;
175c281
< usage();
---
> usage(progname, stderr, EXIT_FAILURE);
177,178c283,287
< if (cutoff != NULL) {
< int y;
---
> if (vflag) {
> if (cutarg != NULL) {
> long lo;
> long hi;
> char dummy;
180,184c289,303
< cutyear = atoi(cutoff);
< cuttime = 0;
< for (y = EPOCH_YEAR; y < cutyear; ++y)
< cuttime += DAYSPERNYEAR + isleap(y);
< cuttime *= SECSPERHOUR * HOURSPERDAY;
---
> if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
> cuthiyear = hi;
> } else if (sscanf(cutarg, "%ld,%ld%c",
> &lo, &hi, &dummy) == 2) {
> cutloyear = lo;
> cuthiyear = hi;
> } else {
> (void) fprintf(stderr, _("%s: wild -c argument %s\n"),
> progname, cutarg);
> exit(EXIT_FAILURE);
> }
> }
> setabsolutes();
> cutlotime = yeartot(cutloyear);
> cuthitime = yeartot(cuthiyear);
191,192d309
< for (hibit = 1; (hibit << 1) != 0; hibit <<= 1)
< continue;
197c314
< for (i = 0; environ[i] != NULL; ++i)
---
> for (i = 0; environ[i] != NULL; ++i)
222,227c339,340
< /*
< ** Get lowest value of t.
< */
< t = hibit;
< if (t > 0) /* time_t is unsigned */
< t = 0;
---
> warned = FALSE;
> t = absolute_min_time;
231,232c344,350
< tm = *localtime(&t);
< (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
---
> if (t < cutlotime)
> t = cutlotime;
> tmp = my_localtime(&t);
> if (tmp != NULL) {
> tm = *tmp;
> (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
> }
234c352
< if (cutoff != NULL && t >= cuttime)
---
> if (t >= cuthitime || t >= cuthitime - SECSPERHOUR * 12)
237,242c355,359
< if (cutoff != NULL && newt >= cuttime)
< break;
< if (newt <= t)
< break;
< newtm = *localtime(&newt);
< if (delta(&newtm, &tm) != (newt - t) ||
---
> newtmp = localtime(&newt);
> if (newtmp != NULL)
> newtm = *newtmp;
> if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
> (delta(&newtm, &tm) != (newt - t) ||
244c361
< strcmp(abbr(&newtm), buf) != 0) {
---
> strcmp(abbr(&newtm), buf) != 0)) {
246,248c363,369
< newtm = *localtime(&newt);
< (void) strncpy(buf, abbr(&newtm),
< (sizeof buf) - 1);
---
> newtmp = localtime(&newt);
> if (newtmp != NULL) {
> newtm = *newtmp;
> (void) strncpy(buf,
> abbr(&newtm),
> (sizeof buf) - 1);
> }
251a373
> tmp = newtmp;
253,258c375
< /*
< ** Get highest value of t.
< */
< t = ~((time_t) 0);
< if (t < 0) /* time_t is signed */
< t &= ~hibit;
---
> t = absolute_max_time;
266a384,386
> /* If exit fails to exit... */
> return(EXIT_FAILURE);
> }
268,270c388,430
< /* gcc -Wall pacifier */
< for ( ; ; )
< continue;
---
> static void
> setabsolutes(void)
> {
> if (0.5 == (time_t) 0.5) {
> /*
> ** time_t is floating.
> */
> if (sizeof (time_t) == sizeof (float)) {
> absolute_min_time = (time_t) -FLT_MAX;
> absolute_max_time = (time_t) FLT_MAX;
> } else if (sizeof (time_t) == sizeof (double)) {
> absolute_min_time = (time_t) -DBL_MAX;
> absolute_max_time = (time_t) DBL_MAX;
> } else {
> (void) fprintf(stderr,
> _("%s: use of -v on system with floating time_t other than float or double\n"),
> progname);
> exit(EXIT_FAILURE);
> }
> } else if (0 > (time_t) -1) {
> /*
> ** time_t is signed. Assume overflow wraps around.
> */
> time_t t = 0;
> time_t t1 = 1;
>
> while (t < t1) {
> t = t1;
> t1 = 2 * t1 + 1;
> }
>
> absolute_max_time = t;
> t = -t;
> absolute_min_time = t - 1;
> if (t < absolute_min_time)
> absolute_min_time = t;
> } else {
> /*
> ** time_t is unsigned.
> */
> absolute_min_time = 0;
> absolute_max_time = absolute_min_time - 1;
> }
272a433,464
> static time_t
> yeartot(y)
> const long y;
> {
> register long myy;
> register long seconds;
> register time_t t;
>
> myy = EPOCH_YEAR;
> t = 0;
> while (myy != y) {
> if (myy < y) {
> seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
> ++myy;
> if (t > absolute_max_time - seconds) {
> t = absolute_max_time;
> break;
> }
> t += seconds;
> } else {
> --myy;
> seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
> if (t < absolute_min_time + seconds) {
> t = absolute_min_time;
> break;
> }
> t -= seconds;
> }
> }
> return t;
> }
>
274c466
< usage(void)
---
> usage(const char *progname, FILE *stream, int status)
276,278c468,472
< fprintf(stderr,
< _("usage: zdump [--version] [-v] [-c cutoff] zonename ...\n"));
< exit(EXIT_FAILURE);
---
> fprintf(stream,
> _("usage: %s [--version] [-v] [--help] [-c [loyear,]hiyear] zonename ...\n\
> \n\
> Report bugs to tz@elsie.nci.nih.gov.\n"), progname);
> exit(status);
282,285c476
< hunt(name, lot, hit)
< char * name;
< time_t lot;
< time_t hit;
---
> hunt(char *name, time_t lot, time_t hit)
287,290c478,484
< time_t t;
< struct tm lotm;
< struct tm tm;
< static char loab[MAX_STRING_LENGTH];
---
> time_t t;
> long diff;
> struct tm lotm;
> register struct tm * lotmp;
> struct tm tm;
> register struct tm * tmp;
> char loab[MAX_STRING_LENGTH];
292,295c486,496
< lotm = *localtime(&lot);
< (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
< while ((hit - lot) >= 2) {
< t = lot / 2 + hit / 2;
---
> lotmp = my_localtime(&lot);
> if (lotmp != NULL) {
> lotm = *lotmp;
> (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
> }
> for ( ; ; ) {
> diff = (long) (hit - lot);
> if (diff < 2)
> break;
> t = lot;
> t += diff / 2;
300,301c501,505
< tm = *localtime(&t);
< if (delta(&tm, &lotm) == (t - lot) &&
---
> tmp = my_localtime(&t);
> if (tmp != NULL)
> tm = *tmp;
> if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
> (delta(&tm, &lotm) == (t - lot) &&
303c507
< strcmp(abbr(&tm), loab) == 0) {
---
> strcmp(abbr(&tm), loab) == 0)) {
305a510
> lotmp = tmp;
314c519
< ** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta.
---
> ** Thanks to Paul Eggert for logic used in delta.
322,323c527,528
< long result;
< int tmy;
---
> register long result;
> register int tmy;
329c534
< result += DAYSPERNYEAR + isleap(tmy + TM_YEAR_BASE);
---
> result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
341,344c546
< show(zone, t, v)
< char * zone;
< time_t t;
< int v;
---
> show(char *zone, time_t t, int v)
346c548
< struct tm * tmp;
---
> register struct tm * tmp;
349,354d550
< if (v)
< (void) printf("%.24s UTC = ", asctime(gmtime(&t)));
< tmp = localtime(&t);
< (void) printf("%.24s", asctime(tmp));
< if (*abbr(tmp) != '\0')
< (void) printf(" %s", abbr(tmp));
356c552,567
< (void) printf(" isdst=%d", tmp->tm_isdst);
---
> tmp = gmtime(&t);
> if (tmp == NULL) {
> (void) printf(tformat(), t);
> } else {
> dumptime(tmp);
> (void) printf(" UTC");
> }
> (void) printf(" = ");
> }
> tmp = my_localtime(&t);
> dumptime(tmp);
> if (tmp != NULL) {
> if (*abbr(tmp) != '\0')
> (void) printf(" %s", abbr(tmp));
> if (v) {
> (void) printf(" isdst=%d", tmp->tm_isdst);
358c569
< (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
---
> (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
359a571
> }
361a574,575
> if (tmp != NULL && *abbr(tmp) != '\0')
> abbrok(abbr(tmp), zone);
375a590,670
>
> /*
> ** The code below can fail on certain theoretical systems;
> ** it works on all known real-world systems as of 2004-12-30.
> */
>
> static const char *
> tformat(void)
> {
> if (0.5 == (time_t) 0.5) { /* floating */
> if (sizeof (time_t) > sizeof (double))
> return "%Lg";
> return "%g";
> }
> if (0 > (time_t) -1) { /* signed */
> if (sizeof (time_t) > sizeof (long))
> return "%lld";
> if (sizeof (time_t) > sizeof (int))
> return "%ld";
> return "%d";
> }
> if (sizeof (time_t) > sizeof (unsigned long))
> return "%llu";
> if (sizeof (time_t) > sizeof (unsigned int))
> return "%lu";
> return "%u";
> }
>
> static void
> dumptime(timeptr)
> register const struct tm * timeptr;
> {
> static const char wday_name[][3] = {
> "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
> };
> static const char mon_name[][3] = {
> "Jan", "Feb", "Mar", "Apr", "May", "Jun",
> "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
> };
> register const char * wn;
> register const char * mn;
> register int lead;
> register int trail;
>
> if (timeptr == NULL) {
> (void) printf("NULL");
> return;
> }
> /*
> ** The packaged versions of localtime and gmtime never put out-of-range
> ** values in tm_wday or tm_mon, but since this code might be compiled
> ** with other (perhaps experimental) versions, paranoia is in order.
> */
> if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
> (int) (sizeof wday_name / sizeof wday_name[0]))
> wn = "???";
> else wn = wday_name[timeptr->tm_wday];
> if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
> (int) (sizeof mon_name / sizeof mon_name[0]))
> mn = "???";
> else mn = mon_name[timeptr->tm_mon];
> (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
> wn, mn,
> timeptr->tm_mday, timeptr->tm_hour,
> timeptr->tm_min, timeptr->tm_sec);
> #define DIVISOR 10
> trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
> lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
> trail / DIVISOR;
> trail %= DIVISOR;
> if (trail < 0 && lead > 0) {
> trail += DIVISOR;
> --lead;
> } else if (lead < 0 && trail > 0) {
> trail -= DIVISOR;
> ++lead;
> }
> if (lead == 0)
> (void) printf("%d", trail);
> else (void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));
> }