dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / zdump / zdump.c
blob0e5c2feabe16576b4561fbae41171fe1ecd08706
1 /*
2 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
6 /*
7 * zdump 7.24
8 * Taken from elsie.nci.nih.gov to replace the existing Solaris zdump,
9 * which was based on an earlier version of the elsie code.
11 * For zdump 7.24, the following changes were made to the elsie code:
12 * locale/textdomain/messages to match existing Solaris style.
13 * Solaris verbose mode is documented to display the current time first.
14 * cstyle cleaned code.
15 * removed old locale/textdomain code.
18 static char elsieid[] = "@(#)zdump.c 7.74";
21 * This code has been made independent of the rest of the time
22 * conversion package to increase confidence in the verification it provides.
23 * You can use this code to help in verifying other implementations.
26 #include "stdio.h" /* for stdout, stderr, perror */
27 #include "string.h" /* for strcpy */
28 #include "sys/types.h" /* for time_t */
29 #include "time.h" /* for struct tm */
30 #include "stdlib.h" /* for exit, malloc, atoi */
31 #include "locale.h" /* for setlocale, textdomain */
32 #include "libintl.h"
33 #include <ctype.h>
34 #include "tzfile.h" /* for defines */
35 #include <limits.h>
37 #ifndef ZDUMP_LO_YEAR
38 #define ZDUMP_LO_YEAR (-500)
39 #endif /* !defined ZDUMP_LO_YEAR */
41 #ifndef ZDUMP_HI_YEAR
42 #define ZDUMP_HI_YEAR 2500
43 #endif /* !defined ZDUMP_HI_YEAR */
45 #ifndef MAX_STRING_LENGTH
46 #define MAX_STRING_LENGTH 1024
47 #endif /* !defined MAX_STRING_LENGTH */
49 #ifndef TRUE
50 #define TRUE 1
51 #endif /* !defined TRUE */
53 #ifndef FALSE
54 #define FALSE 0
55 #endif /* !defined FALSE */
57 #ifndef isleap_sum
59 * See tzfile.h for details on isleap_sum.
61 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
62 #endif /* !defined isleap_sum */
64 #ifndef SECSPERDAY
65 #define SECSPERDAY ((long)SECSPERHOUR * HOURSPERDAY)
66 #endif
67 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
68 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
70 #ifndef GNUC_or_lint
71 #ifdef __GNUC__
72 #define GNUC_or_lint
73 #endif /* defined __GNUC__ */
74 #endif /* !defined GNUC_or_lint */
76 #ifndef INITIALIZE
77 #ifdef GNUC_or_lint
78 #define INITIALIZE(x) ((x) = 0)
79 #else /* !defined GNUC_or_lint */
80 #define INITIALIZE(x)
81 #endif /* !defined GNUC_or_lint */
82 #endif /* !defined INITIALIZE */
84 static time_t absolute_min_time;
85 static time_t absolute_max_time;
86 static size_t longest;
87 static char *progname;
88 static int warned;
90 static char *abbr(struct tm *);
91 static void abbrok(const char *, const char *);
92 static long delta(struct tm *, struct tm *);
93 static void dumptime(const struct tm *);
94 static time_t hunt(char *, time_t, time_t);
95 static void setabsolutes(void);
96 static void show(char *, time_t, int);
97 static void usage(void);
98 static const char *tformat(void);
99 static time_t yeartot(long y);
101 #ifndef TYPECHECK
102 #define my_localtime localtime
103 #else /* !defined TYPECHECK */
104 static struct tm *
105 my_localtime(tp)
106 time_t *tp;
108 register struct tm *tmp;
110 tmp = localtime(tp);
111 if (tp != NULL && tmp != NULL) {
112 struct tm tm;
113 register time_t t;
115 tm = *tmp;
116 t = mktime(&tm);
117 if (t - *tp >= 1 || *tp - t >= 1) {
118 (void) fflush(stdout);
119 (void) fprintf(stderr, "\n%s: ", progname);
120 (void) fprintf(stderr, tformat(), *tp);
121 (void) fprintf(stderr, " ->");
122 (void) fprintf(stderr, " year=%d", tmp->tm_year);
123 (void) fprintf(stderr, " mon=%d", tmp->tm_mon);
124 (void) fprintf(stderr, " mday=%d", tmp->tm_mday);
125 (void) fprintf(stderr, " hour=%d", tmp->tm_hour);
126 (void) fprintf(stderr, " min=%d", tmp->tm_min);
127 (void) fprintf(stderr, " sec=%d", tmp->tm_sec);
128 (void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
129 (void) fprintf(stderr, " -> ");
130 (void) fprintf(stderr, tformat(), t);
131 (void) fprintf(stderr, "\n");
134 return (tmp);
136 #endif /* !defined TYPECHECK */
138 static void
139 abbrok(const char * const abbrp, const char * const zone)
141 register const char *cp;
142 int error = 0;
144 if (warned)
145 return;
146 cp = abbrp;
147 while (isalpha(*cp) || isdigit(*cp) || *cp == '-' || *cp == '+')
148 ++cp;
149 (void) fflush(stdout);
150 if (cp - abbrp < 3) {
151 (void) fprintf(stderr, gettext("%s: warning: zone \"%s\" "
152 "abbreviation \"%s\" has fewer than 3 alphabetics\n"),
153 progname, zone, abbrp);
154 error = 1;
155 } else if (cp - abbrp > 6) {
156 (void) fprintf(stderr, gettext("%s: warning: zone \"%s\" "
157 "abbreviation \"%s\" has more than 6 characters\n"),
158 progname, zone, abbrp);
159 error = 1;
160 } else if (*cp != '\0') {
161 (void) fprintf(stderr, gettext("%s: warning: zone \"%s\" "
162 "abbreviation \"%s\" has characters other than "
163 "alphanumerics\n"), progname, zone, abbrp);
164 error = 1;
166 if (error)
167 warned = TRUE;
171 main(argc, argv)
172 int argc;
173 char *argv[];
175 register int i;
176 register int c;
177 register int vflag;
178 register char *cutarg;
179 register long cutloyear = ZDUMP_LO_YEAR;
180 register long cuthiyear = ZDUMP_HI_YEAR;
181 register time_t cutlotime;
182 register time_t cuthitime;
183 time_t now;
184 time_t t;
185 time_t newt;
186 struct tm tm;
187 struct tm newtm;
188 register struct tm *tmp;
189 register struct tm *newtmp;
191 INITIALIZE(cutlotime);
192 INITIALIZE(cuthitime);
194 (void) setlocale(LC_ALL, "");
195 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
196 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
197 #endif
198 (void) textdomain(TEXT_DOMAIN);
200 progname = argv[0];
201 for (i = 1; i < argc; ++i)
202 if (strcmp(argv[i], "--version") == 0) {
203 (void) printf("%s\n", elsieid);
204 exit(EXIT_SUCCESS);
206 vflag = 0;
207 cutarg = NULL;
208 while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
209 if (c == 'v')
210 vflag = 1;
211 else cutarg = optarg;
212 if (c != EOF ||
213 (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
214 usage();
215 /* NOTREACHED */
217 if (vflag) {
218 if (cutarg != NULL) {
219 long lo;
220 long hi;
221 char dummy;
223 if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
224 cuthiyear = hi;
225 } else if (sscanf(cutarg, "%ld,%ld%c",
226 &lo, &hi, &dummy) == 2) {
227 cutloyear = lo;
228 cuthiyear = hi;
229 } else {
230 (void) fprintf(stderr, gettext("%s: wild -c argument %s\n"),
231 progname, cutarg);
232 exit(EXIT_FAILURE);
235 setabsolutes();
236 cutlotime = yeartot(cutloyear);
237 cuthitime = yeartot(cuthiyear);
239 (void) time(&now);
240 longest = 0;
241 for (i = optind; i < argc; ++i)
242 if (strlen(argv[i]) > longest)
243 longest = strlen(argv[i]);
245 for (i = optind; i < argc; ++i) {
246 static char buf[MAX_STRING_LENGTH];
247 static char *tzp = NULL;
249 (void) unsetenv("TZ");
250 if (tzp != NULL)
251 free(tzp);
252 if ((tzp = malloc(3 + strlen(argv[i]) + 1)) == NULL) {
253 perror(progname);
254 exit(EXIT_FAILURE);
256 (void) strcpy(tzp, "TZ=");
257 (void) strcat(tzp, argv[i]);
258 if (putenv(tzp) != 0) {
259 perror(progname);
260 exit(EXIT_FAILURE);
262 if (!vflag) {
263 show(argv[i], now, FALSE);
264 continue;
267 #if defined(sun)
269 * We show the current time first, probably because we froze
270 * the behavior of zdump some time ago and then it got
271 * changed.
273 show(argv[i], now, TRUE);
274 #endif
275 warned = FALSE;
276 t = absolute_min_time;
277 show(argv[i], t, TRUE);
278 t += SECSPERHOUR * HOURSPERDAY;
279 show(argv[i], t, TRUE);
280 if (t < cutlotime)
281 t = cutlotime;
282 tmp = my_localtime(&t);
283 if (tmp != NULL) {
284 tm = *tmp;
285 (void) strncpy(buf, abbr(&tm), sizeof (buf) - 1);
287 for (;;) {
288 if (t >= cuthitime)
289 break;
290 /* check if newt will overrun maximum time_t value */
291 if (t > LONG_MAX - (SECSPERHOUR * 12))
292 break;
293 newt = t + SECSPERHOUR * 12;
294 if (newt >= cuthitime)
295 break;
296 newtmp = localtime(&newt);
297 if (newtmp != NULL)
298 newtm = *newtmp;
299 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
300 (delta(&newtm, &tm) != (newt - t) ||
301 newtm.tm_isdst != tm.tm_isdst ||
302 strcmp(abbr(&newtm), buf) != 0)) {
303 newt = hunt(argv[i], t, newt);
304 newtmp = localtime(&newt);
305 if (newtmp != NULL) {
306 newtm = *newtmp;
307 (void) strncpy(buf,
308 abbr(&newtm),
309 sizeof (buf) - 1);
312 t = newt;
313 tm = newtm;
314 tmp = newtmp;
316 t = absolute_max_time;
317 #if defined(sun)
318 show(argv[i], t, TRUE);
319 t -= SECSPERHOUR * HOURSPERDAY;
320 show(argv[i], t, TRUE);
321 #else /* !defined(sun) */
322 t -= SECSPERHOUR * HOURSPERDAY;
323 show(argv[i], t, TRUE);
324 t += SECSPERHOUR * HOURSPERDAY;
325 show(argv[i], t, TRUE);
326 #endif /* !defined(sun) */
328 if (fflush(stdout) || ferror(stdout)) {
329 (void) fprintf(stderr, "%s: ", progname);
330 (void) perror(gettext("Error writing standard output"));
331 exit(EXIT_FAILURE);
333 return (EXIT_SUCCESS);
336 static void
337 setabsolutes()
339 #if defined(sun)
340 absolute_min_time = LONG_MIN;
341 absolute_max_time = LONG_MAX;
342 #else
343 if (0.5 == (time_t)0.5) {
345 * time_t is floating.
347 if (sizeof (time_t) == sizeof (float)) {
348 absolute_min_time = (time_t)-FLT_MAX;
349 absolute_max_time = (time_t)FLT_MAX;
350 } else if (sizeof (time_t) == sizeof (double)) {
351 absolute_min_time = (time_t)-DBL_MAX;
352 absolute_max_time = (time_t)DBL_MAX;
353 } else {
354 (void) fprintf(stderr, gettext("%s: use of -v on "
355 "system with floating time_t other than float "
356 "or double\n"), progname);
357 exit(EXIT_FAILURE);
359 } else
360 /*CONSTANTCONDITION*/
361 if (0 > (time_t)-1) {
363 * time_t is signed.
365 register time_t hibit;
367 for (hibit = 1; (hibit * 2) != 0; hibit *= 2)
368 continue;
369 absolute_min_time = hibit;
370 absolute_max_time = -(hibit + 1);
371 } else {
373 * time_t is unsigned.
375 absolute_min_time = 0;
376 absolute_max_time = absolute_min_time - 1;
378 #endif
381 static time_t
382 yeartot(y)
383 const long y;
385 register long myy;
386 register long seconds;
387 register time_t t;
389 myy = EPOCH_YEAR;
390 t = 0;
391 while (myy != y) {
392 if (myy < y) {
393 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
394 ++myy;
395 if (t > absolute_max_time - seconds) {
396 t = absolute_max_time;
397 break;
399 t += seconds;
400 } else {
401 --myy;
402 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
403 if (t < absolute_min_time + seconds) {
404 t = absolute_min_time;
405 break;
407 t -= seconds;
410 return (t);
413 static time_t
414 hunt(name, lot, hit)
415 char *name;
416 time_t lot;
417 time_t hit;
419 time_t t;
420 long diff;
421 struct tm lotm;
422 register struct tm *lotmp;
423 struct tm tm;
424 register struct tm *tmp;
425 char loab[MAX_STRING_LENGTH];
427 lotmp = my_localtime(&lot);
428 if (lotmp != NULL) {
429 lotm = *lotmp;
430 (void) strncpy(loab, abbr(&lotm), sizeof (loab) - 1);
432 for (;;) {
433 diff = (long)(hit - lot);
434 if (diff < 2)
435 break;
436 t = lot;
437 t += diff / 2;
438 if (t <= lot)
439 ++t;
440 else if (t >= hit)
441 --t;
442 tmp = my_localtime(&t);
443 if (tmp != NULL)
444 tm = *tmp;
445 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
446 (delta(&tm, &lotm) == (t - lot) &&
447 tm.tm_isdst == lotm.tm_isdst &&
448 strcmp(abbr(&tm), loab) == 0)) {
449 lot = t;
450 lotm = tm;
451 lotmp = tmp;
452 } else hit = t;
454 show(name, lot, TRUE);
455 show(name, hit, TRUE);
456 return (hit);
460 * Thanks to Paul Eggert for logic used in delta.
463 static long
464 delta(newp, oldp)
465 struct tm *newp;
466 struct tm *oldp;
468 register long result;
469 register int tmy;
471 if (newp->tm_year < oldp->tm_year)
472 return (-delta(oldp, newp));
473 result = 0;
474 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
475 result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
476 result += newp->tm_yday - oldp->tm_yday;
477 result *= HOURSPERDAY;
478 result += newp->tm_hour - oldp->tm_hour;
479 result *= MINSPERHOUR;
480 result += newp->tm_min - oldp->tm_min;
481 result *= SECSPERMIN;
482 result += newp->tm_sec - oldp->tm_sec;
483 return (result);
486 static void
487 show(zone, t, v)
488 char *zone;
489 time_t t;
490 int v;
492 register struct tm *tmp;
494 (void) printf("%-*s ", (int)longest, zone);
495 if (v) {
496 tmp = gmtime(&t);
497 if (tmp == NULL) {
498 (void) printf(tformat(), t);
499 } else {
500 dumptime(tmp);
501 (void) printf(" UTC");
503 (void) printf(" = ");
505 tmp = my_localtime(&t);
506 dumptime(tmp);
507 if (tmp != NULL) {
508 if (*abbr(tmp) != '\0')
509 (void) printf(" %s", abbr(tmp));
510 if (v) {
511 (void) printf(" isdst=%d", tmp->tm_isdst);
512 #ifdef TM_GMTOFF
513 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
514 #endif /* defined TM_GMTOFF */
517 (void) printf("\n");
518 if (tmp != NULL && *abbr(tmp) != '\0')
519 abbrok(abbr(tmp), zone);
522 static char *
523 abbr(tmp)
524 struct tm *tmp;
526 register char *result;
527 static char nada;
529 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
530 return (&nada);
531 result = tzname[tmp->tm_isdst];
532 return ((result == NULL) ? &nada : result);
536 * The code below can fail on certain theoretical systems;
537 * it works on all known real-world systems as of 2004-12-30.
540 static const char *
541 tformat()
543 #if defined(sun)
544 /* time_t is signed long */
545 return ("%ld");
546 #else
547 /*CONSTANTCONDITION*/
548 if (0.5 == (time_t)0.5) { /* floating */
549 /*CONSTANTCONDITION*/
550 if (sizeof (time_t) > sizeof (double))
551 return ("%Lg");
552 return ("%g");
554 /*CONSTANTCONDITION*/
555 if (0 > (time_t)-1) { /* signed */
556 /*CONSTANTCONDITION*/
557 if (sizeof (time_t) > sizeof (long))
558 return ("%lld");
559 /*CONSTANTCONDITION*/
560 if (sizeof (time_t) > sizeof (int))
561 return ("%ld");
562 return ("%d");
564 /*CONSTANTCONDITION*/
565 if (sizeof (time_t) > sizeof (unsigned long))
566 return ("%llu");
567 /*CONSTANTCONDITION*/
568 if (sizeof (time_t) > sizeof (unsigned int))
569 return ("%lu");
570 return ("%u");
571 #endif
574 static void
575 dumptime(timeptr)
576 register const struct tm *timeptr;
578 static const char wday_name[][3] = {
579 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
581 static const char mon_name[][3] = {
582 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
583 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
585 register const char *wn;
586 register const char *mn;
587 register int lead;
588 register int trail;
590 if (timeptr == NULL) {
591 (void) printf("NULL");
592 return;
595 * The packaged versions of localtime and gmtime never put out-of-range
596 * values in tm_wday or tm_mon, but since this code might be compiled
597 * with other (perhaps experimental) versions, paranoia is in order.
599 if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
600 (int)(sizeof (wday_name) / sizeof (wday_name[0])))
601 wn = "???";
602 else wn = wday_name[timeptr->tm_wday];
603 if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
604 (int)(sizeof (mon_name) / sizeof (mon_name[0])))
605 mn = "???";
606 else mn = mon_name[timeptr->tm_mon];
607 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
608 wn, mn,
609 timeptr->tm_mday, timeptr->tm_hour,
610 timeptr->tm_min, timeptr->tm_sec);
611 #define DIVISOR 10
612 trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
613 lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
614 trail / DIVISOR;
615 trail %= DIVISOR;
616 if (trail < 0 && lead > 0) {
617 trail += DIVISOR;
618 --lead;
619 } else if (lead < 0 && trail > 0) {
620 trail -= DIVISOR;
621 ++lead;
623 if (lead == 0)
624 (void) printf("%d", trail);
625 else
626 (void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));
629 static void
630 usage()
632 (void) fprintf(stderr, gettext(
633 "%s: [ --version ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n"),
634 progname);
635 exit(EXIT_FAILURE);
636 /* NOTREACHED */