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