Back to index

glibc  2.9
Defines | Functions | Variables
zdump.c File Reference
#include "stdio.h"
#include "string.h"
#include "sys/types.h"
#include "time.h"
#include "stdlib.h"
#include "float.h"
#include "ctype.h"

Go to the source code of this file.

Defines

#define isascii(x)   1
#define ZDUMP_LO_YEAR   (-500)
#define ZDUMP_HI_YEAR   2500
#define MAX_STRING_LENGTH   1024
#define TRUE   1
#define FALSE   0
#define EXIT_SUCCESS   0
#define EXIT_FAILURE   1
#define SECSPERMIN   60
#define MINSPERHOUR   60
#define SECSPERHOUR   (SECSPERMIN * MINSPERHOUR)
#define HOURSPERDAY   24
#define EPOCH_YEAR   1970
#define TM_YEAR_BASE   1900
#define DAYSPERNYEAR   365
#define isleap(y)   (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
#define isleap_sum(a, b)   isleap((a) % 400 + (b) % 400)
#define SECSPERDAY   ((long) SECSPERHOUR * HOURSPERDAY)
#define SECSPERNYEAR   (SECSPERDAY * DAYSPERNYEAR)
#define SECSPERLYEAR   (SECSPERNYEAR + SECSPERDAY)
#define HAVE_GETTEXT   0
#define INITIALIZE(x)
#define _(msgid)   msgid
#define TZ_DOMAIN   "tz"
#define my_localtime   localtime
#define DIVISOR   10

Functions

int getopt (int argc, char *const argv[], const char *options)
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)
static void abbrok (char *const abbrp, const char *const zone) const
int main (int argc, argv)

Variables

static char elsieid [] = "@(#)zdump.c 8.6"
char ** environ
char * optarg
int optind
char * tzname [2]
static time_t absolute_min_time
static time_t absolute_max_time
static size_t longest
static char * progname
static int warned

Define Documentation

#define _ (   msgid)    msgid

Definition at line 127 of file zdump.c.

#define DAYSPERNYEAR   365

Definition at line 73 of file zdump.c.

#define DIVISOR   10
#define EPOCH_YEAR   1970

Definition at line 65 of file zdump.c.

#define EXIT_FAILURE   1

Definition at line 45 of file zdump.c.

#define EXIT_SUCCESS   0

Definition at line 41 of file zdump.c.

#define FALSE   0

Definition at line 37 of file zdump.c.

#define HAVE_GETTEXT   0

Definition at line 92 of file zdump.c.

#define HOURSPERDAY   24

Definition at line 61 of file zdump.c.

#define INITIALIZE (   x)

Definition at line 113 of file zdump.c.

#define isascii (   x)    1

Definition at line 17 of file zdump.c.

#define isleap (   y)    (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))

Definition at line 77 of file zdump.c.

#define isleap_sum (   a,
  b 
)    isleap((a) % 400 + (b) % 400)

Definition at line 84 of file zdump.c.

#define MAX_STRING_LENGTH   1024

Definition at line 29 of file zdump.c.

#define MINSPERHOUR   60

Definition at line 53 of file zdump.c.

#define my_localtime   localtime

Definition at line 159 of file zdump.c.

#define SECSPERDAY   ((long) SECSPERHOUR * HOURSPERDAY)

Definition at line 87 of file zdump.c.

Definition at line 57 of file zdump.c.

Definition at line 89 of file zdump.c.

#define SECSPERMIN   60

Definition at line 49 of file zdump.c.

Definition at line 88 of file zdump.c.

#define TM_YEAR_BASE   1900

Definition at line 69 of file zdump.c.

#define TRUE   1

Definition at line 33 of file zdump.c.

#define TZ_DOMAIN   "tz"

Definition at line 132 of file zdump.c.

#define ZDUMP_HI_YEAR   2500

Definition at line 25 of file zdump.c.

#define ZDUMP_LO_YEAR   (-500)

Definition at line 21 of file zdump.c.


Function Documentation

static char * abbr ( struct tm tmp) [static]

Definition at line 576 of file zdump.c.

{
       register char *      result;
       static char   nada;

       if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
              return &nada;
       result = tzname[tmp->tm_isdst];
       return (result == NULL) ? &nada : result;
}

Here is the caller graph for this function:

static void abbrok ( const char *  abbrp,
const char *  zone 
) [static]

Here is the caller graph for this function:

static void abbrok ( char * const  abbrp,
const char * const  zone 
) const [static]

Definition at line 196 of file zdump.c.

{
       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;
}

Here is the call graph for this function:

static long delta ( struct tm newp,
struct tm oldp 
) [static]

Definition at line 520 of file zdump.c.

{
       register long result;
       register int  tmy;

       if (newp->tm_year < oldp->tm_year)
              return -delta(oldp, newp);
       result = 0;
       for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
              result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
       result += newp->tm_yday - oldp->tm_yday;
       result *= HOURSPERDAY;
       result += newp->tm_hour - oldp->tm_hour;
       result *= MINSPERHOUR;
       result += newp->tm_min - oldp->tm_min;
       result *= SECSPERMIN;
       result += newp->tm_sec - oldp->tm_sec;
       return result;
}

Here is the caller graph for this function:

static void dumptime ( const struct tm tmp) [static]

Definition at line 616 of file zdump.c.

{
       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));
}

Here is the call graph for this function:

Here is the caller graph for this function:

int getopt ( int  argc,
char *const  argv[],
const char *  options 
)
static time_t hunt ( char *  name,
time_t  lot,
time_t  hit 
) [static]

Definition at line 473 of file zdump.c.

{
       time_t               t;
       long                 diff;
       struct tm            lotm;
       register struct tm * lotmp;
       struct tm            tm;
       register struct tm * tmp;
       char                 loab[MAX_STRING_LENGTH];

       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;
              if (t <= lot)
                     ++t;
              else if (t >= hit)
                     --t;
              tmp = my_localtime(&t);
              if (tmp != NULL)
                     tm = *tmp;
              if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
                     (delta(&tm, &lotm) == (t - lot) &&
                     tm.tm_isdst == lotm.tm_isdst &&
                     strcmp(abbr(&tm), loab) == 0)) {
                            lot = t;
                            lotm = tm;
                            lotmp = tmp;
              } else hit = t;
       }
       show(name, lot, TRUE);
       show(name, hit, TRUE);
       return hit;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  argc,
argv   
)

Definition at line 234 of file zdump.c.

{
       register int         i;
       register int         c;
       register int         vflag;
       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;
       time_t               now;
       time_t               t;
       time_t               newt;
       struct tm            tm;
       struct tm            newtm;
       register struct tm * tmp;
       register struct tm * newtmp;

       INITIALIZE(cutlotime);
       INITIALIZE(cuthitime);
#if HAVE_GETTEXT
       (void) setlocale(LC_ALL, "");
#ifdef TZ_DOMAINDIR
       (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
#endif /* defined TEXTDOMAINDIR */
       (void) textdomain(TZ_DOMAIN);
#endif /* HAVE_GETTEXT */
       progname = argv[0];
       for (i = 1; i < argc; ++i)
              if (strcmp(argv[i], "--version") == 0) {
                     (void) printf("%s\n", elsieid);
                     exit(EXIT_SUCCESS);
              }
       vflag = 0;
       cutarg = NULL;
       while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
              if (c == 'v')
                     vflag = 1;
              else   cutarg = optarg;
       if ((c != EOF && c != -1) ||
              (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
                     (void) fprintf(stderr,
_("%s: usage is %s [ --version ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n"),
                            progname, progname);
                     exit(EXIT_FAILURE);
       }
       if (vflag) {
              if (cutarg != NULL) {
                     long   lo;
                     long   hi;
                     char   dummy;

                     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);
       }
       (void) time(&now);
       longest = 0;
       for (i = optind; i < argc; ++i)
              if (strlen(argv[i]) > longest)
                     longest = strlen(argv[i]);
       {
              register int  from;
              register int  to;

              for (i = 0; environ[i] != NULL; ++i)
                     continue;
              fakeenv = (char **) malloc((size_t) ((i + 2) *
                     sizeof *fakeenv));
              if (fakeenv == NULL ||
                     (fakeenv[0] = (char *) malloc(longest + 4)) == NULL) {
                                   (void) perror(progname);
                                   exit(EXIT_FAILURE);
              }
              to = 0;
              (void) strcpy(fakeenv[to++], "TZ=");
              for (from = 0; environ[from] != NULL; ++from)
                     if (strncmp(environ[from], "TZ=", 3) != 0)
                            fakeenv[to++] = environ[from];
              fakeenv[to] = NULL;
              environ = fakeenv;
       }
       for (i = optind; i < argc; ++i) {
              static char   buf[MAX_STRING_LENGTH];

              (void) strcpy(&fakeenv[0][3], argv[i]);
              if (!vflag) {
                     show(argv[i], now, FALSE);
                     continue;
              }
              warned = FALSE;
              t = absolute_min_time;
              show(argv[i], t, TRUE);
              t += SECSPERHOUR * HOURSPERDAY;
              show(argv[i], t, TRUE);
              if (t < cutlotime)
                     t = cutlotime;
              tmp = my_localtime(&t);
              if (tmp != NULL) {
                     tm = *tmp;
                     (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
              }
              for ( ; ; ) {
                     if (t >= cuthitime)
                            break;
                     newt = t + SECSPERHOUR * 12;
                     if (newt >= cuthitime)
                            break;
                     if (newt <= t)
                            break;
                     newtmp = localtime(&newt);
                     if (newtmp != NULL)
                            newtm = *newtmp;
                     if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
                            (delta(&newtm, &tm) != (newt - t) ||
                            newtm.tm_isdst != tm.tm_isdst ||
                            strcmp(abbr(&newtm), buf) != 0)) {
                                   newt = hunt(argv[i], t, newt);
                                   newtmp = localtime(&newt);
                                   if (newtmp != NULL) {
                                          newtm = *newtmp;
                                          (void) strncpy(buf,
                                                 abbr(&newtm),
                                                 (sizeof buf) - 1);
                                   }
                     }
                     t = newt;
                     tm = newtm;
                     tmp = newtmp;
              }
              t = absolute_max_time;
              t -= SECSPERHOUR * HOURSPERDAY;
              show(argv[i], t, TRUE);
              t += SECSPERHOUR * HOURSPERDAY;
              show(argv[i], t, TRUE);
       }
       if (fflush(stdout) || ferror(stdout)) {
              (void) fprintf(stderr, "%s: ", progname);
              (void) perror(_("Error writing to standard output"));
              exit(EXIT_FAILURE);
       }
       exit(EXIT_SUCCESS);
       /* If exit fails to exit... */
       return EXIT_FAILURE;
}

Here is the call graph for this function:

static void setabsolutes ( void  ) [static]

Definition at line 396 of file zdump.c.

{
       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;
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void show ( char *  zone,
time_t  t,
int  v 
) [static]

Definition at line 543 of file zdump.c.

{
       register struct tm * tmp;

       (void) printf("%-*s  ", (int) longest, zone);
       if (v) {
              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);
#ifdef TM_GMTOFF
                     (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
#endif /* defined TM_GMTOFF */
              }
       }
       (void) printf("\n");
       if (tmp != NULL && *abbr(tmp) != '\0')
              abbrok(abbr(tmp), zone);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static const char * tformat ( void  ) [static]

Definition at line 594 of file zdump.c.

{
       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";
}

Here is the caller graph for this function:

static time_t yeartot ( long  y) [static]

Definition at line 441 of file zdump.c.

{
       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;
}

Here is the caller graph for this function:


Variable Documentation

Definition at line 143 of file zdump.c.

Definition at line 142 of file zdump.c.

char elsieid[] = "@(#)zdump.c 8.6" [static]

Definition at line 1 of file zdump.c.

char** environ
size_t longest [static]

Definition at line 144 of file zdump.c.

char* optarg

Definition at line 107 of file getopt.c.

Definition at line 122 of file getopt.c.

char* progname [static]

Definition at line 145 of file zdump.c.

char* tzname[2]
int warned [static]

Definition at line 146 of file zdump.c.