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