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