zdump.c revision 1.10.12.2 1 /* $NetBSD: zdump.c,v 1.10.12.2 2002/03/22 20:42:46 nathanw Exp $ */
2
3 #include <sys/cdefs.h>
4 #ifndef lint
5 #ifndef NOID
6 #if 0
7 static char elsieid[] = "@(#)zdump.c 7.29";
8 #else
9 __RCSID("$NetBSD: zdump.c,v 1.10.12.2 2002/03/22 20:42:46 nathanw 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: ", argv[0]);
274 (void) perror(_("Error writing standard output"));
275 (void) exit(EXIT_FAILURE);
276 }
277 exit(EXIT_SUCCESS);
278
279 /* gcc -Wall pacifier */
280 for ( ; ; )
281 continue;
282 }
283
284 static time_t
285 hunt(name, lot, hit)
286 char * name;
287 time_t lot;
288 time_t hit;
289 {
290 time_t t;
291 struct tm lotm;
292 struct tm tm;
293 static char loab[MAX_STRING_LENGTH];
294
295 lotm = *localtime(&lot);
296 (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
297 while ((hit - lot) >= 2) {
298 t = lot / 2 + hit / 2;
299 if (t <= lot)
300 ++t;
301 else if (t >= hit)
302 --t;
303 tm = *localtime(&t);
304 if (delta(&tm, &lotm) == (t - lot) &&
305 tm.tm_isdst == lotm.tm_isdst &&
306 strcmp(abbr(&tm), loab) == 0) {
307 lot = t;
308 lotm = tm;
309 } else hit = t;
310 }
311 show(name, lot, TRUE);
312 show(name, hit, TRUE);
313 return hit;
314 }
315
316 /*
317 ** Thanks to Paul Eggert (eggert (at) twinsun.com) for logic used in delta.
318 */
319
320 static long
321 delta(newp, oldp)
322 struct tm * newp;
323 struct tm * oldp;
324 {
325 long result;
326 int tmy;
327
328 if (newp->tm_year < oldp->tm_year)
329 return -delta(oldp, newp);
330 result = 0;
331 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
332 result += DAYSPERNYEAR + isleap(tmy + TM_YEAR_BASE);
333 result += newp->tm_yday - oldp->tm_yday;
334 result *= HOURSPERDAY;
335 result += newp->tm_hour - oldp->tm_hour;
336 result *= MINSPERHOUR;
337 result += newp->tm_min - oldp->tm_min;
338 result *= SECSPERMIN;
339 result += newp->tm_sec - oldp->tm_sec;
340 return result;
341 }
342
343 static void
344 show(zone, t, v)
345 char * zone;
346 time_t t;
347 int v;
348 {
349 struct tm * tmp;
350
351 (void) printf("%-*s ", (int) longest, zone);
352 if (v)
353 (void) printf("%.24s UTC = ", asctime(gmtime(&t)));
354 tmp = localtime(&t);
355 (void) printf("%.24s", asctime(tmp));
356 if (*abbr(tmp) != '\0')
357 (void) printf(" %s", abbr(tmp));
358 if (v) {
359 (void) printf(" isdst=%d", tmp->tm_isdst);
360 #ifdef TM_GMTOFF
361 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
362 #endif /* defined TM_GMTOFF */
363 }
364 (void) printf("\n");
365 }
366
367 static const char *
368 abbr(tmp)
369 struct tm * tmp;
370 {
371 register const char * result;
372 static const char nada;
373
374 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
375 return &nada;
376 result = tzname[tmp->tm_isdst];
377 return (result == NULL) ? &nada : result;
378 }
379