1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 1986-2002 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate /* 28*0Sstevel@tonic-gate * zdump 7.24 29*0Sstevel@tonic-gate * Taken from elsie.nci.nih.gov to replace the existing Solaris zdump, 30*0Sstevel@tonic-gate * which was based on an earlier version of the elsie code. 31*0Sstevel@tonic-gate * 32*0Sstevel@tonic-gate * For zdump 7.24, the following changes were made to the elsie code: 33*0Sstevel@tonic-gate * locale/textdomain/messages to match existing Solaris style. 34*0Sstevel@tonic-gate * Solaris verbose mode is documented to display the current time first. 35*0Sstevel@tonic-gate * cstyle cleaned code. 36*0Sstevel@tonic-gate * removed old locale/textdomain code. 37*0Sstevel@tonic-gate */ 38*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gate /* static char elsieid[] = "@(#)zdump.c 7.28"; */ 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gate /* 43*0Sstevel@tonic-gate * This code has been made independent of the rest of the time 44*0Sstevel@tonic-gate * conversion package to increase confidence in the verification it provides. 45*0Sstevel@tonic-gate * You can use this code to help in verifying other implementations. 46*0Sstevel@tonic-gate */ 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate #include "stdio.h" /* for stdout, stderr, perror */ 49*0Sstevel@tonic-gate #include "string.h" /* for strcpy */ 50*0Sstevel@tonic-gate #include "sys/types.h" /* for time_t */ 51*0Sstevel@tonic-gate #include "time.h" /* for struct tm */ 52*0Sstevel@tonic-gate #include "stdlib.h" /* for exit, malloc, atoi */ 53*0Sstevel@tonic-gate #include "locale.h" /* for setlocale, textdomain */ 54*0Sstevel@tonic-gate #include "libintl.h" 55*0Sstevel@tonic-gate #include "tzfile.h" /* for defines */ 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate #ifndef MAX_STRING_LENGTH 58*0Sstevel@tonic-gate #define MAX_STRING_LENGTH 1024 59*0Sstevel@tonic-gate #endif /* !defined MAX_STRING_LENGTH */ 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate #ifndef TRUE 62*0Sstevel@tonic-gate #define TRUE 1 63*0Sstevel@tonic-gate #endif /* !defined TRUE */ 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate #ifndef FALSE 66*0Sstevel@tonic-gate #define FALSE 0 67*0Sstevel@tonic-gate #endif /* !defined FALSE */ 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate #ifndef INITIALIZE 70*0Sstevel@tonic-gate #ifdef lint 71*0Sstevel@tonic-gate #define INITIALIZE(x) ((x) = 0) 72*0Sstevel@tonic-gate #endif /* defined lint */ 73*0Sstevel@tonic-gate #ifndef lint 74*0Sstevel@tonic-gate #define INITIALIZE(x) 75*0Sstevel@tonic-gate #endif /* !defined lint */ 76*0Sstevel@tonic-gate #endif /* !defined INITIALIZE */ 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gate extern char ** environ; 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate static char * abbr(struct tm *); 81*0Sstevel@tonic-gate static long delta(struct tm *, struct tm *); 82*0Sstevel@tonic-gate static time_t hunt(char *, time_t, time_t); 83*0Sstevel@tonic-gate static size_t longest; 84*0Sstevel@tonic-gate static char * progname; 85*0Sstevel@tonic-gate static void show(char *, time_t, int); 86*0Sstevel@tonic-gate static void usage(char *); 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate int 89*0Sstevel@tonic-gate main(argc, argv) 90*0Sstevel@tonic-gate int argc; 91*0Sstevel@tonic-gate char * argv[]; 92*0Sstevel@tonic-gate { 93*0Sstevel@tonic-gate register int i; 94*0Sstevel@tonic-gate register int c; 95*0Sstevel@tonic-gate register int vflag; 96*0Sstevel@tonic-gate register char * cutoff; 97*0Sstevel@tonic-gate register int cutyear; 98*0Sstevel@tonic-gate register long cuttime; 99*0Sstevel@tonic-gate char ** fakeenv; 100*0Sstevel@tonic-gate time_t now; 101*0Sstevel@tonic-gate time_t t; 102*0Sstevel@tonic-gate time_t newt; 103*0Sstevel@tonic-gate time_t hibit; 104*0Sstevel@tonic-gate struct tm tm; 105*0Sstevel@tonic-gate struct tm newtm; 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate INITIALIZE(cuttime); 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 110*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 111*0Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 112*0Sstevel@tonic-gate #endif 113*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate progname = argv[0]; 116*0Sstevel@tonic-gate vflag = 0; 117*0Sstevel@tonic-gate cutoff = NULL; 118*0Sstevel@tonic-gate while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v') 119*0Sstevel@tonic-gate if (c == 'v') 120*0Sstevel@tonic-gate vflag = 1; 121*0Sstevel@tonic-gate else cutoff = optarg; 122*0Sstevel@tonic-gate if (c != EOF || 123*0Sstevel@tonic-gate (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) { 124*0Sstevel@tonic-gate usage(argv[0]); 125*0Sstevel@tonic-gate /* NOTREACHED */ 126*0Sstevel@tonic-gate } 127*0Sstevel@tonic-gate if (cutoff != NULL) { 128*0Sstevel@tonic-gate int y; 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gate cutyear = atoi(cutoff); 131*0Sstevel@tonic-gate cuttime = 0; 132*0Sstevel@tonic-gate for (y = EPOCH_YEAR; y < cutyear; ++y) 133*0Sstevel@tonic-gate cuttime += DAYSPERNYEAR + isleap(y); 134*0Sstevel@tonic-gate cuttime *= SECSPERHOUR * HOURSPERDAY; 135*0Sstevel@tonic-gate } 136*0Sstevel@tonic-gate (void) time(&now); 137*0Sstevel@tonic-gate longest = 0; 138*0Sstevel@tonic-gate for (i = optind; i < argc; ++i) 139*0Sstevel@tonic-gate if (strlen(argv[i]) > longest) 140*0Sstevel@tonic-gate longest = strlen(argv[i]); 141*0Sstevel@tonic-gate for (hibit = 1; (hibit << 1) != 0; hibit <<= 1) 142*0Sstevel@tonic-gate continue; 143*0Sstevel@tonic-gate { 144*0Sstevel@tonic-gate register int from; 145*0Sstevel@tonic-gate register int to; 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate for (i = 0; environ[i] != NULL; ++i) 148*0Sstevel@tonic-gate continue; 149*0Sstevel@tonic-gate fakeenv = (char **) malloc((size_t) ((i + 2) * 150*0Sstevel@tonic-gate sizeof (*fakeenv))); 151*0Sstevel@tonic-gate if (fakeenv == NULL || 152*0Sstevel@tonic-gate (fakeenv[0] = (char *) malloc(longest + 4)) == NULL) { 153*0Sstevel@tonic-gate (void) perror(progname); 154*0Sstevel@tonic-gate (void) exit(EXIT_FAILURE); 155*0Sstevel@tonic-gate } 156*0Sstevel@tonic-gate to = 0; 157*0Sstevel@tonic-gate (void) strcpy(fakeenv[to++], "TZ="); 158*0Sstevel@tonic-gate for (from = 0; environ[from] != NULL; ++from) 159*0Sstevel@tonic-gate if (strncmp(environ[from], "TZ=", 3) != 0) 160*0Sstevel@tonic-gate fakeenv[to++] = environ[from]; 161*0Sstevel@tonic-gate fakeenv[to] = NULL; 162*0Sstevel@tonic-gate environ = fakeenv; 163*0Sstevel@tonic-gate } 164*0Sstevel@tonic-gate for (i = optind; i < argc; ++i) { 165*0Sstevel@tonic-gate static char buf[MAX_STRING_LENGTH]; 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate (void) strcpy(&fakeenv[0][3], argv[i]); 168*0Sstevel@tonic-gate if (!vflag) { 169*0Sstevel@tonic-gate show(argv[i], now, FALSE); 170*0Sstevel@tonic-gate continue; 171*0Sstevel@tonic-gate } 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate #if defined(sun) 174*0Sstevel@tonic-gate /* 175*0Sstevel@tonic-gate * We show the current time first, probably because we froze 176*0Sstevel@tonic-gate * the behavior of zdump some time ago and then it got 177*0Sstevel@tonic-gate * changed. 178*0Sstevel@tonic-gate */ 179*0Sstevel@tonic-gate show(argv[i], now, TRUE); 180*0Sstevel@tonic-gate #endif 181*0Sstevel@tonic-gate 182*0Sstevel@tonic-gate /* 183*0Sstevel@tonic-gate * Get lowest value of t. 184*0Sstevel@tonic-gate */ 185*0Sstevel@tonic-gate t = hibit; 186*0Sstevel@tonic-gate if (t > 0) /* time_t is unsigned */ 187*0Sstevel@tonic-gate t = 0; 188*0Sstevel@tonic-gate show(argv[i], t, TRUE); 189*0Sstevel@tonic-gate t += SECSPERHOUR * HOURSPERDAY; 190*0Sstevel@tonic-gate show(argv[i], t, TRUE); 191*0Sstevel@tonic-gate tm = *localtime(&t); 192*0Sstevel@tonic-gate (void) strncpy(buf, abbr(&tm), (sizeof (buf)) - 1); 193*0Sstevel@tonic-gate for (;;) { 194*0Sstevel@tonic-gate if (cutoff != NULL && t >= cuttime) 195*0Sstevel@tonic-gate break; 196*0Sstevel@tonic-gate newt = t + SECSPERHOUR * 12; 197*0Sstevel@tonic-gate if (cutoff != NULL && newt >= cuttime) 198*0Sstevel@tonic-gate break; 199*0Sstevel@tonic-gate if (newt <= t) 200*0Sstevel@tonic-gate break; 201*0Sstevel@tonic-gate newtm = *localtime(&newt); 202*0Sstevel@tonic-gate if (delta(&newtm, &tm) != (newt - t) || 203*0Sstevel@tonic-gate newtm.tm_isdst != tm.tm_isdst || 204*0Sstevel@tonic-gate strcmp(abbr(&newtm), buf) != 0) { 205*0Sstevel@tonic-gate newt = hunt(argv[i], t, newt); 206*0Sstevel@tonic-gate newtm = *localtime(&newt); 207*0Sstevel@tonic-gate (void) strncpy(buf, abbr(&newtm), 208*0Sstevel@tonic-gate (sizeof (buf)) - 1); 209*0Sstevel@tonic-gate } 210*0Sstevel@tonic-gate t = newt; 211*0Sstevel@tonic-gate tm = newtm; 212*0Sstevel@tonic-gate } 213*0Sstevel@tonic-gate /* 214*0Sstevel@tonic-gate * Get highest value of t. 215*0Sstevel@tonic-gate */ 216*0Sstevel@tonic-gate t = ~((time_t) 0); 217*0Sstevel@tonic-gate if (t < 0) /* time_t is signed */ 218*0Sstevel@tonic-gate t &= ~hibit; 219*0Sstevel@tonic-gate #if defined(sun) 220*0Sstevel@tonic-gate show(argv[i], t, TRUE); 221*0Sstevel@tonic-gate t -= SECSPERHOUR * HOURSPERDAY; 222*0Sstevel@tonic-gate show(argv[i], t, TRUE); 223*0Sstevel@tonic-gate #else /* !defined(sun) */ 224*0Sstevel@tonic-gate t -= SECSPERHOUR * HOURSPERDAY; 225*0Sstevel@tonic-gate show(argv[i], t, TRUE); 226*0Sstevel@tonic-gate t += SECSPERHOUR * HOURSPERDAY; 227*0Sstevel@tonic-gate show(argv[i], t, TRUE); 228*0Sstevel@tonic-gate #endif /* !defined(sun) */ 229*0Sstevel@tonic-gate } 230*0Sstevel@tonic-gate if (fflush(stdout) || ferror(stdout)) { 231*0Sstevel@tonic-gate (void) fprintf(stderr, gettext( 232*0Sstevel@tonic-gate "%s: Error writing standard output "), argv[0]); 233*0Sstevel@tonic-gate (void) perror(gettext("standard output")); 234*0Sstevel@tonic-gate usage(argv[0]); 235*0Sstevel@tonic-gate } 236*0Sstevel@tonic-gate return (EXIT_SUCCESS); 237*0Sstevel@tonic-gate } 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gate static time_t 240*0Sstevel@tonic-gate hunt(name, lot, hit) 241*0Sstevel@tonic-gate char * name; 242*0Sstevel@tonic-gate time_t lot; 243*0Sstevel@tonic-gate time_t hit; 244*0Sstevel@tonic-gate { 245*0Sstevel@tonic-gate time_t t; 246*0Sstevel@tonic-gate struct tm lotm; 247*0Sstevel@tonic-gate struct tm tm; 248*0Sstevel@tonic-gate static char loab[MAX_STRING_LENGTH]; 249*0Sstevel@tonic-gate 250*0Sstevel@tonic-gate lotm = *localtime(&lot); 251*0Sstevel@tonic-gate (void) strncpy(loab, abbr(&lotm), (sizeof (loab)) - 1); 252*0Sstevel@tonic-gate while ((hit - lot) >= 2) { 253*0Sstevel@tonic-gate t = lot / 2 + hit / 2; 254*0Sstevel@tonic-gate if (t <= lot) 255*0Sstevel@tonic-gate ++t; 256*0Sstevel@tonic-gate else if (t >= hit) 257*0Sstevel@tonic-gate --t; 258*0Sstevel@tonic-gate tm = *localtime(&t); 259*0Sstevel@tonic-gate if (delta(&tm, &lotm) == (t - lot) && 260*0Sstevel@tonic-gate tm.tm_isdst == lotm.tm_isdst && 261*0Sstevel@tonic-gate strcmp(abbr(&tm), loab) == 0) { 262*0Sstevel@tonic-gate lot = t; 263*0Sstevel@tonic-gate lotm = tm; 264*0Sstevel@tonic-gate } else hit = t; 265*0Sstevel@tonic-gate } 266*0Sstevel@tonic-gate show(name, lot, TRUE); 267*0Sstevel@tonic-gate show(name, hit, TRUE); 268*0Sstevel@tonic-gate return (hit); 269*0Sstevel@tonic-gate } 270*0Sstevel@tonic-gate 271*0Sstevel@tonic-gate /* 272*0Sstevel@tonic-gate * Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta. 273*0Sstevel@tonic-gate */ 274*0Sstevel@tonic-gate 275*0Sstevel@tonic-gate static long 276*0Sstevel@tonic-gate delta(newp, oldp) 277*0Sstevel@tonic-gate struct tm * newp; 278*0Sstevel@tonic-gate struct tm * oldp; 279*0Sstevel@tonic-gate { 280*0Sstevel@tonic-gate long result; 281*0Sstevel@tonic-gate int tmy; 282*0Sstevel@tonic-gate 283*0Sstevel@tonic-gate if (newp->tm_year < oldp->tm_year) 284*0Sstevel@tonic-gate return (-delta(oldp, newp)); 285*0Sstevel@tonic-gate result = 0; 286*0Sstevel@tonic-gate for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy) 287*0Sstevel@tonic-gate result += DAYSPERNYEAR + isleap(tmy + TM_YEAR_BASE); 288*0Sstevel@tonic-gate result += newp->tm_yday - oldp->tm_yday; 289*0Sstevel@tonic-gate result *= HOURSPERDAY; 290*0Sstevel@tonic-gate result += newp->tm_hour - oldp->tm_hour; 291*0Sstevel@tonic-gate result *= MINSPERHOUR; 292*0Sstevel@tonic-gate result += newp->tm_min - oldp->tm_min; 293*0Sstevel@tonic-gate result *= SECSPERMIN; 294*0Sstevel@tonic-gate result += newp->tm_sec - oldp->tm_sec; 295*0Sstevel@tonic-gate return (result); 296*0Sstevel@tonic-gate } 297*0Sstevel@tonic-gate 298*0Sstevel@tonic-gate static void 299*0Sstevel@tonic-gate show(zone, t, v) 300*0Sstevel@tonic-gate char * zone; 301*0Sstevel@tonic-gate time_t t; 302*0Sstevel@tonic-gate int v; 303*0Sstevel@tonic-gate { 304*0Sstevel@tonic-gate struct tm * tmp; 305*0Sstevel@tonic-gate 306*0Sstevel@tonic-gate (void) printf("%-*s ", (int)longest, zone); 307*0Sstevel@tonic-gate if (v) 308*0Sstevel@tonic-gate (void) printf("%.24s UTC = ", asctime(gmtime(&t))); 309*0Sstevel@tonic-gate tmp = localtime(&t); 310*0Sstevel@tonic-gate (void) printf("%.24s", asctime(tmp)); 311*0Sstevel@tonic-gate if (*abbr(tmp) != '\0') 312*0Sstevel@tonic-gate (void) printf(" %s", abbr(tmp)); 313*0Sstevel@tonic-gate if (v) { 314*0Sstevel@tonic-gate (void) printf(" isdst=%d", tmp->tm_isdst); 315*0Sstevel@tonic-gate } 316*0Sstevel@tonic-gate (void) printf("\n"); 317*0Sstevel@tonic-gate } 318*0Sstevel@tonic-gate 319*0Sstevel@tonic-gate static char * 320*0Sstevel@tonic-gate abbr(tmp) 321*0Sstevel@tonic-gate struct tm * tmp; 322*0Sstevel@tonic-gate { 323*0Sstevel@tonic-gate register char * result; 324*0Sstevel@tonic-gate static char nada; 325*0Sstevel@tonic-gate 326*0Sstevel@tonic-gate if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1) 327*0Sstevel@tonic-gate return (&nada); 328*0Sstevel@tonic-gate result = tzname[tmp->tm_isdst]; 329*0Sstevel@tonic-gate return ((result == NULL) ? &nada : result); 330*0Sstevel@tonic-gate } 331*0Sstevel@tonic-gate 332*0Sstevel@tonic-gate static void 333*0Sstevel@tonic-gate usage(char *progname) 334*0Sstevel@tonic-gate { 335*0Sstevel@tonic-gate (void) fprintf(stderr, gettext( 336*0Sstevel@tonic-gate "%s: [ -v ] [ -c cutoffyear ] [ zonename ... ]\n"), progname); 337*0Sstevel@tonic-gate (void) exit(EXIT_FAILURE); 338*0Sstevel@tonic-gate /* NOTREACHED */ 339*0Sstevel@tonic-gate } 340