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