zdump.c revision 130819
1static const char elsieid[] = "@(#)zdump.c 7.31"; 2 3#ifndef lint 4static const char rcsid[] = 5 "$FreeBSD: head/usr.sbin/zic/zdump.c 130819 2004-06-20 21:41:11Z stefanf $"; 6#endif /* not lint */ 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 14#include <err.h> 15#include <stdio.h> /* for stdout, stderr */ 16#include <stdlib.h> /* for exit, malloc, atoi */ 17#include <string.h> /* for strcpy */ 18#include <sys/types.h> /* for time_t */ 19#include <time.h> /* for struct tm */ 20#include <unistd.h> 21 22#ifndef MAX_STRING_LENGTH 23#define MAX_STRING_LENGTH 1024 24#endif /* !defined MAX_STRING_LENGTH */ 25 26#ifndef TRUE 27#define TRUE 1 28#endif /* !defined TRUE */ 29 30#ifndef FALSE 31#define FALSE 0 32#endif /* !defined FALSE */ 33 34#ifndef EXIT_SUCCESS 35#define EXIT_SUCCESS 0 36#endif /* !defined EXIT_SUCCESS */ 37 38#ifndef EXIT_FAILURE 39#define EXIT_FAILURE 1 40#endif /* !defined EXIT_FAILURE */ 41 42#ifndef SECSPERMIN 43#define SECSPERMIN 60 44#endif /* !defined SECSPERMIN */ 45 46#ifndef MINSPERHOUR 47#define MINSPERHOUR 60 48#endif /* !defined MINSPERHOUR */ 49 50#ifndef SECSPERHOUR 51#define SECSPERHOUR (SECSPERMIN * MINSPERHOUR) 52#endif /* !defined SECSPERHOUR */ 53 54#ifndef HOURSPERDAY 55#define HOURSPERDAY 24 56#endif /* !defined HOURSPERDAY */ 57 58#ifndef EPOCH_YEAR 59#define EPOCH_YEAR 1970 60#endif /* !defined EPOCH_YEAR */ 61 62#ifndef TM_YEAR_BASE 63#define TM_YEAR_BASE 1900 64#endif /* !defined TM_YEAR_BASE */ 65 66#ifndef DAYSPERNYEAR 67#define DAYSPERNYEAR 365 68#endif /* !defined DAYSPERNYEAR */ 69 70#ifndef isleap 71#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0) 72#endif /* !defined isleap */ 73 74#if HAVE_GETTEXT - 0 75#include "locale.h" /* for setlocale */ 76#include "libintl.h" 77#endif /* HAVE_GETTEXT - 0 */ 78 79#ifndef GNUC_or_lint 80#ifdef lint 81#define GNUC_or_lint 82#endif /* defined lint */ 83#ifndef lint 84#ifdef __GNUC__ 85#define GNUC_or_lint 86#endif /* defined __GNUC__ */ 87#endif /* !defined lint */ 88#endif /* !defined GNUC_or_lint */ 89 90#ifndef INITIALIZE 91#ifdef GNUC_or_lint 92#define INITIALIZE(x) ((x) = 0) 93#endif /* defined GNUC_or_lint */ 94#ifndef GNUC_or_lint 95#define INITIALIZE(x) 96#endif /* !defined GNUC_or_lint */ 97#endif /* !defined INITIALIZE */ 98 99/* 100** For the benefit of GNU folk... 101** `_(MSGID)' uses the current locale's message library string for MSGID. 102** The default is to use gettext if available, and use MSGID otherwise. 103*/ 104 105#ifndef _ 106#if HAVE_GETTEXT - 0 107#define _(msgid) gettext(msgid) 108#else /* !(HAVE_GETTEXT - 0) */ 109#define _(msgid) msgid 110#endif /* !(HAVE_GETTEXT - 0) */ 111#endif /* !defined _ */ 112 113#ifndef TZ_DOMAIN 114#define TZ_DOMAIN "tz" 115#endif /* !defined TZ_DOMAIN */ 116 117#ifndef P 118#ifdef __STDC__ 119#define P(x) x 120#endif /* defined __STDC__ */ 121#ifndef __STDC__ 122#define P(x) () 123#endif /* !defined __STDC__ */ 124#endif /* !defined P */ 125 126extern char ** environ; 127extern char * tzname[2]; 128 129static char * abbr P((struct tm * tmp)); 130static long delta P((struct tm * newp, struct tm * oldp)); 131static time_t hunt P((char * name, time_t lot, time_t hit)); 132static size_t longest; 133static void show P((char * zone, time_t t, int v)); 134static void usage(void); 135 136int 137main(argc, argv) 138int argc; 139char * argv[]; 140{ 141 register int i; 142 register int c; 143 register int vflag; 144 register char * cutoff; 145 register int cutyear; 146 register long cuttime; 147 char ** fakeenv; 148 time_t now; 149 time_t t; 150 time_t newt; 151 time_t hibit; 152 struct tm tm; 153 struct tm newtm; 154 155 INITIALIZE(cuttime); 156#if HAVE_GETTEXT - 0 157 (void) setlocale(LC_MESSAGES, ""); 158#ifdef TZ_DOMAINDIR 159 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR); 160#endif /* defined(TEXTDOMAINDIR) */ 161 (void) textdomain(TZ_DOMAIN); 162#endif /* HAVE_GETTEXT - 0 */ 163 for (i = 1; i < argc; ++i) 164 if (strcmp(argv[i], "--version") == 0) { 165 errx(EXIT_SUCCESS, "%s", elsieid); 166 } 167 vflag = 0; 168 cutoff = NULL; 169 while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v') 170 if (c == 'v') 171 vflag = 1; 172 else cutoff = optarg; 173 if ((c != EOF && c != -1) || 174 (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) { 175 usage(); 176 } 177 if (cutoff != NULL) { 178 int y; 179 180 cutyear = atoi(cutoff); 181 cuttime = 0; 182 for (y = EPOCH_YEAR; y < cutyear; ++y) 183 cuttime += DAYSPERNYEAR + isleap(y); 184 cuttime *= SECSPERHOUR * HOURSPERDAY; 185 } 186 (void) time(&now); 187 longest = 0; 188 for (i = optind; i < argc; ++i) 189 if (strlen(argv[i]) > longest) 190 longest = strlen(argv[i]); 191 for (hibit = 1; (hibit << 1) != 0; hibit <<= 1) 192 continue; 193 { 194 register int from; 195 register int to; 196 197 for (i = 0; environ[i] != NULL; ++i) 198 continue; 199 fakeenv = (char **) malloc((size_t) ((i + 2) * 200 sizeof *fakeenv)); 201 if (fakeenv == NULL || 202 (fakeenv[0] = (char *) malloc((size_t) (longest + 203 4))) == NULL) 204 errx(EXIT_FAILURE, 205 _("malloc() failed")); 206 to = 0; 207 (void) strcpy(fakeenv[to++], "TZ="); 208 for (from = 0; environ[from] != NULL; ++from) 209 if (strncmp(environ[from], "TZ=", 3) != 0) 210 fakeenv[to++] = environ[from]; 211 fakeenv[to] = NULL; 212 environ = fakeenv; 213 } 214 for (i = optind; i < argc; ++i) { 215 static char buf[MAX_STRING_LENGTH]; 216 217 (void) strcpy(&fakeenv[0][3], argv[i]); 218 if (!vflag) { 219 show(argv[i], now, FALSE); 220 continue; 221 } 222 /* 223 ** Get lowest value of t. 224 */ 225 t = hibit; 226 if (t > 0) /* time_t is unsigned */ 227 t = 0; 228 show(argv[i], t, TRUE); 229 t += SECSPERHOUR * HOURSPERDAY; 230 show(argv[i], t, TRUE); 231 tm = *localtime(&t); 232 (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1); 233 for ( ; ; ) { 234 if (cutoff != NULL && t >= cuttime) 235 break; 236 newt = t + SECSPERHOUR * 12; 237 if (cutoff != NULL && newt >= cuttime) 238 break; 239 if (newt <= t) 240 break; 241 newtm = *localtime(&newt); 242 if (delta(&newtm, &tm) != (newt - t) || 243 newtm.tm_isdst != tm.tm_isdst || 244 strcmp(abbr(&newtm), buf) != 0) { 245 newt = hunt(argv[i], t, newt); 246 newtm = *localtime(&newt); 247 (void) strncpy(buf, abbr(&newtm), 248 (sizeof buf) - 1); 249 } 250 t = newt; 251 tm = newtm; 252 } 253 /* 254 ** Get highest value of t. 255 */ 256 t = ~((time_t) 0); 257 if (t < 0) /* time_t is signed */ 258 t &= ~hibit; 259 t -= SECSPERHOUR * HOURSPERDAY; 260 show(argv[i], t, TRUE); 261 t += SECSPERHOUR * HOURSPERDAY; 262 show(argv[i], t, TRUE); 263 } 264 if (fflush(stdout) || ferror(stdout)) 265 errx(EXIT_FAILURE, _("error writing standard output")); 266 exit(EXIT_SUCCESS); 267 268 /* gcc -Wall pacifier */ 269 for ( ; ; ) 270 continue; 271} 272 273static void 274usage(void) 275{ 276 fprintf(stderr, 277_("usage: zdump [--version] [-v] [-c cutoff] zonename ...\n")); 278 exit(EXIT_FAILURE); 279} 280 281static time_t 282hunt(name, lot, hit) 283char * name; 284time_t lot; 285time_t hit; 286{ 287 time_t t; 288 struct tm lotm; 289 struct tm tm; 290 static char loab[MAX_STRING_LENGTH]; 291 292 lotm = *localtime(&lot); 293 (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1); 294 while ((hit - lot) >= 2) { 295 t = lot / 2 + hit / 2; 296 if (t <= lot) 297 ++t; 298 else if (t >= hit) 299 --t; 300 tm = *localtime(&t); 301 if (delta(&tm, &lotm) == (t - lot) && 302 tm.tm_isdst == lotm.tm_isdst && 303 strcmp(abbr(&tm), loab) == 0) { 304 lot = t; 305 lotm = tm; 306 } else hit = t; 307 } 308 show(name, lot, TRUE); 309 show(name, hit, TRUE); 310 return hit; 311} 312 313/* 314** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta. 315*/ 316 317static long 318delta(newp, oldp) 319struct tm * newp; 320struct tm * oldp; 321{ 322 long result; 323 int tmy; 324 325 if (newp->tm_year < oldp->tm_year) 326 return -delta(oldp, newp); 327 result = 0; 328 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy) 329 result += DAYSPERNYEAR + isleap(tmy + TM_YEAR_BASE); 330 result += newp->tm_yday - oldp->tm_yday; 331 result *= HOURSPERDAY; 332 result += newp->tm_hour - oldp->tm_hour; 333 result *= MINSPERHOUR; 334 result += newp->tm_min - oldp->tm_min; 335 result *= SECSPERMIN; 336 result += newp->tm_sec - oldp->tm_sec; 337 return result; 338} 339 340static void 341show(zone, t, v) 342char * zone; 343time_t t; 344int v; 345{ 346 struct tm * tmp; 347 348 (void) printf("%-*s ", (int) longest, zone); 349 if (v) 350 (void) printf("%.24s UTC = ", asctime(gmtime(&t))); 351 tmp = localtime(&t); 352 (void) printf("%.24s", asctime(tmp)); 353 if (*abbr(tmp) != '\0') 354 (void) printf(" %s", abbr(tmp)); 355 if (v) { 356 (void) printf(" isdst=%d", tmp->tm_isdst); 357#ifdef TM_GMTOFF 358 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF); 359#endif /* defined TM_GMTOFF */ 360 } 361 (void) printf("\n"); 362} 363 364static char * 365abbr(tmp) 366struct tm * tmp; 367{ 368 register char * result; 369 static char nada; 370 371 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1) 372 return &nada; 373 result = tzname[tmp->tm_isdst]; 374 return (result == NULL) ? &nada : result; 375} 376