zdump.c revision 1.31 1 1.31 christos /* $NetBSD: zdump.c,v 1.31 2013/09/20 19:06:54 christos Exp $ */
2 1.17 mlelstv /*
3 1.17 mlelstv ** This file is in the public domain, so clarified as of
4 1.17 mlelstv ** 2009-05-17 by Arthur David Olson.
5 1.17 mlelstv */
6 1.2 jtc
7 1.6 christos #include <sys/cdefs.h>
8 1.1 jtc #ifndef lint
9 1.31 christos __RCSID("$NetBSD: zdump.c,v 1.31 2013/09/20 19:06:54 christos Exp $");
10 1.1 jtc #endif /* !defined lint */
11 1.1 jtc
12 1.25 christos #include "version.h"
13 1.1 jtc /*
14 1.1 jtc ** This code has been made independent of the rest of the time
15 1.1 jtc ** conversion package to increase confidence in the verification it provides.
16 1.1 jtc ** You can use this code to help in verifying other implementations.
17 1.29 christos **
18 1.29 christos ** However, include private.h when debugging, so that it overrides
19 1.29 christos ** time_t consistently with the rest of the package.
20 1.1 jtc */
21 1.1 jtc
22 1.29 christos #include "private.h"
23 1.29 christos
24 1.15 christos #include "stdio.h" /* for stdout, stderr */
25 1.1 jtc #include "string.h" /* for strcpy */
26 1.1 jtc #include "sys/types.h" /* for time_t */
27 1.1 jtc #include "time.h" /* for struct tm */
28 1.1 jtc #include "stdlib.h" /* for exit, malloc, atoi */
29 1.15 christos #include <err.h>
30 1.17 mlelstv #include "ctype.h" /* for isalpha et al. */
31 1.17 mlelstv #ifndef isascii
32 1.17 mlelstv #define isascii(x) 1
33 1.17 mlelstv #endif /* !defined isascii */
34 1.17 mlelstv
35 1.29 christos /*
36 1.29 christos ** Substitutes for pre-C99 compilers.
37 1.29 christos ** Much of this section of code is stolen from private.h.
38 1.29 christos */
39 1.29 christos
40 1.29 christos #ifndef HAVE_STDINT_H
41 1.29 christos # define HAVE_STDINT_H \
42 1.29 christos (199901 <= __STDC_VERSION__ || 2 < (__GLIBC__ + (0 < __GLIBC_MINOR__)))
43 1.29 christos #endif
44 1.29 christos #if HAVE_STDINT_H
45 1.29 christos # include "stdint.h"
46 1.29 christos #endif
47 1.29 christos #ifndef HAVE_INTTYPES_H
48 1.29 christos # define HAVE_INTTYPES_H HAVE_STDINT_H
49 1.29 christos #endif
50 1.29 christos #if HAVE_INTTYPES_H
51 1.29 christos # include <inttypes.h>
52 1.29 christos #endif
53 1.29 christos
54 1.29 christos #ifndef INT_FAST32_MAX
55 1.29 christos # if INT_MAX >> 31 == 0
56 1.29 christos typedef long int_fast32_t;
57 1.29 christos # else
58 1.29 christos typedef int int_fast32_t;
59 1.29 christos # endif
60 1.29 christos #endif
61 1.29 christos
62 1.29 christos #ifndef INTMAX_MAX
63 1.29 christos # if defined LLONG_MAX || defined __LONG_LONG_MAX__
64 1.29 christos typedef long long intmax_t;
65 1.29 christos # define PRIdMAX "lld"
66 1.31 christos # ifdef LLONG_MAX
67 1.31 christos # define INTMAX_MAX LLONG_MAX
68 1.31 christos # else
69 1.31 christos # define INTMAX_MAX __LONG_LONG_MAX__
70 1.31 christos # endif
71 1.29 christos # else
72 1.29 christos typedef long intmax_t;
73 1.29 christos # define PRIdMAX "ld"
74 1.31 christos # define INTMAX_MAX LONG_MAX
75 1.29 christos # endif
76 1.29 christos #endif
77 1.29 christos #ifndef SCNdMAX
78 1.29 christos # define SCNdMAX PRIdMAX
79 1.29 christos #endif
80 1.29 christos
81 1.27 christos
82 1.17 mlelstv #ifndef ZDUMP_LO_YEAR
83 1.17 mlelstv #define ZDUMP_LO_YEAR (-500)
84 1.17 mlelstv #endif /* !defined ZDUMP_LO_YEAR */
85 1.17 mlelstv
86 1.17 mlelstv #ifndef ZDUMP_HI_YEAR
87 1.17 mlelstv #define ZDUMP_HI_YEAR 2500
88 1.17 mlelstv #endif /* !defined ZDUMP_HI_YEAR */
89 1.1 jtc
90 1.1 jtc #ifndef MAX_STRING_LENGTH
91 1.1 jtc #define MAX_STRING_LENGTH 1024
92 1.1 jtc #endif /* !defined MAX_STRING_LENGTH */
93 1.1 jtc
94 1.1 jtc #ifndef TRUE
95 1.1 jtc #define TRUE 1
96 1.1 jtc #endif /* !defined TRUE */
97 1.1 jtc
98 1.1 jtc #ifndef FALSE
99 1.1 jtc #define FALSE 0
100 1.1 jtc #endif /* !defined FALSE */
101 1.1 jtc
102 1.1 jtc #ifndef EXIT_SUCCESS
103 1.1 jtc #define EXIT_SUCCESS 0
104 1.1 jtc #endif /* !defined EXIT_SUCCESS */
105 1.1 jtc
106 1.1 jtc #ifndef EXIT_FAILURE
107 1.1 jtc #define EXIT_FAILURE 1
108 1.1 jtc #endif /* !defined EXIT_FAILURE */
109 1.1 jtc
110 1.1 jtc #ifndef SECSPERMIN
111 1.1 jtc #define SECSPERMIN 60
112 1.1 jtc #endif /* !defined SECSPERMIN */
113 1.1 jtc
114 1.1 jtc #ifndef MINSPERHOUR
115 1.1 jtc #define MINSPERHOUR 60
116 1.1 jtc #endif /* !defined MINSPERHOUR */
117 1.1 jtc
118 1.1 jtc #ifndef SECSPERHOUR
119 1.1 jtc #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
120 1.1 jtc #endif /* !defined SECSPERHOUR */
121 1.1 jtc
122 1.1 jtc #ifndef HOURSPERDAY
123 1.1 jtc #define HOURSPERDAY 24
124 1.1 jtc #endif /* !defined HOURSPERDAY */
125 1.1 jtc
126 1.1 jtc #ifndef EPOCH_YEAR
127 1.1 jtc #define EPOCH_YEAR 1970
128 1.1 jtc #endif /* !defined EPOCH_YEAR */
129 1.1 jtc
130 1.1 jtc #ifndef TM_YEAR_BASE
131 1.1 jtc #define TM_YEAR_BASE 1900
132 1.1 jtc #endif /* !defined TM_YEAR_BASE */
133 1.1 jtc
134 1.1 jtc #ifndef DAYSPERNYEAR
135 1.1 jtc #define DAYSPERNYEAR 365
136 1.1 jtc #endif /* !defined DAYSPERNYEAR */
137 1.1 jtc
138 1.1 jtc #ifndef isleap
139 1.17 mlelstv #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
140 1.1 jtc #endif /* !defined isleap */
141 1.1 jtc
142 1.17 mlelstv #ifndef isleap_sum
143 1.17 mlelstv /*
144 1.17 mlelstv ** See tzfile.h for details on isleap_sum.
145 1.17 mlelstv */
146 1.17 mlelstv #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
147 1.17 mlelstv #endif /* !defined isleap_sum */
148 1.17 mlelstv
149 1.29 christos #define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
150 1.17 mlelstv #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
151 1.17 mlelstv #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
152 1.31 christos #define SECSPER400YEARS (SECSPERNYEAR * (intmax_t) (300 + 3) \
153 1.31 christos + SECSPERLYEAR * (intmax_t) (100 - 3))
154 1.31 christos
155 1.31 christos /*
156 1.31 christos ** True if SECSPER400YEARS is known to be representable as an
157 1.31 christos ** intmax_t. It's OK that SECSPER400YEARS_FITS can in theory be false
158 1.31 christos ** even if SECSPER400YEARS is representable, because when that happens
159 1.31 christos ** the code merely runs a bit more slowly, and this slowness doesn't
160 1.31 christos ** occur on any practical platform.
161 1.31 christos */
162 1.31 christos enum { SECSPER400YEARS_FITS = SECSPERLYEAR <= INTMAX_MAX / 400 };
163 1.17 mlelstv
164 1.17 mlelstv #ifndef HAVE_GETTEXT
165 1.17 mlelstv #define HAVE_GETTEXT 0
166 1.17 mlelstv #endif
167 1.17 mlelstv #if HAVE_GETTEXT
168 1.3 jtc #include "locale.h" /* for setlocale */
169 1.3 jtc #include "libintl.h"
170 1.17 mlelstv #endif /* HAVE_GETTEXT */
171 1.3 jtc
172 1.1 jtc #ifndef GNUC_or_lint
173 1.1 jtc #ifdef lint
174 1.1 jtc #define GNUC_or_lint
175 1.17 mlelstv #else /* !defined lint */
176 1.1 jtc #ifdef __GNUC__
177 1.1 jtc #define GNUC_or_lint
178 1.1 jtc #endif /* defined __GNUC__ */
179 1.1 jtc #endif /* !defined lint */
180 1.1 jtc #endif /* !defined GNUC_or_lint */
181 1.1 jtc
182 1.29 christos #ifndef ATTRIBUTE_PURE
183 1.26 christos #if 2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__)
184 1.29 christos # define ATTRIBUTE_PURE __attribute__ ((ATTRIBUTE_PURE__))
185 1.26 christos #else
186 1.29 christos # define ATTRIBUTE_PURE /* empty */
187 1.26 christos #endif
188 1.26 christos #endif
189 1.26 christos
190 1.1 jtc #ifndef INITIALIZE
191 1.1 jtc #ifdef GNUC_or_lint
192 1.1 jtc #define INITIALIZE(x) ((x) = 0)
193 1.17 mlelstv #else /* !defined GNUC_or_lint */
194 1.1 jtc #define INITIALIZE(x)
195 1.1 jtc #endif /* !defined GNUC_or_lint */
196 1.1 jtc #endif /* !defined INITIALIZE */
197 1.1 jtc
198 1.3 jtc /*
199 1.3 jtc ** For the benefit of GNU folk...
200 1.3 jtc ** `_(MSGID)' uses the current locale's message library string for MSGID.
201 1.3 jtc ** The default is to use gettext if available, and use MSGID otherwise.
202 1.3 jtc */
203 1.3 jtc
204 1.3 jtc #ifndef _
205 1.17 mlelstv #if HAVE_GETTEXT
206 1.3 jtc #define _(msgid) gettext(msgid)
207 1.17 mlelstv #else /* !HAVE_GETTEXT */
208 1.21 christos #define _(msgid) msgid
209 1.17 mlelstv #endif /* !HAVE_GETTEXT */
210 1.3 jtc #endif /* !defined _ */
211 1.3 jtc
212 1.3 jtc #ifndef TZ_DOMAIN
213 1.3 jtc #define TZ_DOMAIN "tz"
214 1.3 jtc #endif /* !defined TZ_DOMAIN */
215 1.3 jtc
216 1.1 jtc extern char ** environ;
217 1.17 mlelstv extern int getopt(int argc, char * const argv[],
218 1.17 mlelstv const char * options);
219 1.1 jtc extern char * optarg;
220 1.1 jtc extern int optind;
221 1.1 jtc
222 1.29 christos /* The minimum and maximum finite time values. */
223 1.29 christos static time_t absolute_min_time =
224 1.31 christos ((time_t) -1 < 0
225 1.29 christos ? (time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1)
226 1.29 christos : 0);
227 1.29 christos static time_t absolute_max_time =
228 1.31 christos ((time_t) -1 < 0
229 1.31 christos ? - (~ 0 < 0) - ((time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1))
230 1.29 christos : -1);
231 1.5 jtc static size_t longest;
232 1.1 jtc static char * progname;
233 1.17 mlelstv static int warned;
234 1.17 mlelstv
235 1.17 mlelstv static const char * abbr(struct tm * tmp);
236 1.17 mlelstv static void abbrok(const char * abbrp, const char * zone);
237 1.29 christos static intmax_t delta(struct tm * newp, struct tm * oldp) ATTRIBUTE_PURE;
238 1.17 mlelstv static void dumptime(const struct tm * tmp);
239 1.17 mlelstv static time_t hunt(char * name, time_t lot, time_t hit);
240 1.17 mlelstv static void show(char * zone, time_t t, int v);
241 1.17 mlelstv static const char * tformat(void);
242 1.29 christos static time_t yeartot(long y) ATTRIBUTE_PURE;
243 1.17 mlelstv
244 1.17 mlelstv #ifndef TYPECHECK
245 1.17 mlelstv #define my_localtime localtime
246 1.17 mlelstv #else /* !defined TYPECHECK */
247 1.17 mlelstv static struct tm *
248 1.26 christos my_localtime(time_t *tp)
249 1.17 mlelstv {
250 1.26 christos struct tm *tmp;
251 1.17 mlelstv
252 1.17 mlelstv tmp = localtime(tp);
253 1.17 mlelstv if (tp != NULL && tmp != NULL) {
254 1.17 mlelstv struct tm tm;
255 1.26 christos time_t t;
256 1.17 mlelstv
257 1.17 mlelstv tm = *tmp;
258 1.17 mlelstv t = mktime(&tm);
259 1.31 christos if (t != *tp) {
260 1.17 mlelstv (void) fflush(stdout);
261 1.17 mlelstv (void) fprintf(stderr, "\n%s: ", progname);
262 1.17 mlelstv (void) fprintf(stderr, tformat(), *tp);
263 1.17 mlelstv (void) fprintf(stderr, " ->");
264 1.17 mlelstv (void) fprintf(stderr, " year=%d", tmp->tm_year);
265 1.17 mlelstv (void) fprintf(stderr, " mon=%d", tmp->tm_mon);
266 1.17 mlelstv (void) fprintf(stderr, " mday=%d", tmp->tm_mday);
267 1.17 mlelstv (void) fprintf(stderr, " hour=%d", tmp->tm_hour);
268 1.17 mlelstv (void) fprintf(stderr, " min=%d", tmp->tm_min);
269 1.17 mlelstv (void) fprintf(stderr, " sec=%d", tmp->tm_sec);
270 1.17 mlelstv (void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
271 1.17 mlelstv (void) fprintf(stderr, " -> ");
272 1.17 mlelstv (void) fprintf(stderr, tformat(), t);
273 1.17 mlelstv (void) fprintf(stderr, "\n");
274 1.17 mlelstv }
275 1.17 mlelstv }
276 1.17 mlelstv return tmp;
277 1.17 mlelstv }
278 1.17 mlelstv #endif /* !defined TYPECHECK */
279 1.17 mlelstv
280 1.17 mlelstv static void
281 1.26 christos abbrok(const char *const abbrp, const char *const zone)
282 1.17 mlelstv {
283 1.26 christos const char *cp;
284 1.26 christos const char *wp;
285 1.17 mlelstv
286 1.17 mlelstv if (warned)
287 1.17 mlelstv return;
288 1.17 mlelstv cp = abbrp;
289 1.17 mlelstv wp = NULL;
290 1.17 mlelstv while (isascii((unsigned char) *cp) && isalpha((unsigned char) *cp))
291 1.17 mlelstv ++cp;
292 1.17 mlelstv if (cp - abbrp == 0)
293 1.17 mlelstv wp = _("lacks alphabetic at start");
294 1.17 mlelstv else if (cp - abbrp < 3)
295 1.17 mlelstv wp = _("has fewer than 3 alphabetics");
296 1.17 mlelstv else if (cp - abbrp > 6)
297 1.17 mlelstv wp = _("has more than 6 alphabetics");
298 1.17 mlelstv if (wp == NULL && (*cp == '+' || *cp == '-')) {
299 1.17 mlelstv ++cp;
300 1.17 mlelstv if (isascii((unsigned char) *cp) &&
301 1.17 mlelstv isdigit((unsigned char) *cp))
302 1.17 mlelstv if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
303 1.17 mlelstv ++cp;
304 1.17 mlelstv if (*cp != '\0')
305 1.17 mlelstv wp = _("differs from POSIX standard");
306 1.17 mlelstv }
307 1.17 mlelstv if (wp == NULL)
308 1.17 mlelstv return;
309 1.17 mlelstv (void) fflush(stdout);
310 1.17 mlelstv (void) fprintf(stderr,
311 1.17 mlelstv _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
312 1.17 mlelstv progname, zone, abbrp, wp);
313 1.17 mlelstv warned = TRUE;
314 1.17 mlelstv }
315 1.17 mlelstv
316 1.24 joerg __dead static void
317 1.26 christos usage(FILE *const stream, const int status)
318 1.17 mlelstv {
319 1.17 mlelstv (void) fprintf(stream,
320 1.29 christos _("%s: usage: %s [--version] [--help] [-{vV}] [-{ct} [lo,]hi] zonename ...\n"
321 1.29 christos "\n"
322 1.29 christos "Report bugs to %s.\n"),
323 1.28 christos progname, progname, REPORT_BUGS_TO);
324 1.17 mlelstv exit(status);
325 1.17 mlelstv }
326 1.1 jtc
327 1.1 jtc int
328 1.26 christos main(int argc, char *argv[])
329 1.26 christos {
330 1.26 christos int i;
331 1.26 christos int vflag;
332 1.29 christos int Vflag;
333 1.26 christos char * cutarg;
334 1.29 christos char * cuttimes;
335 1.26 christos time_t cutlotime;
336 1.26 christos time_t cuthitime;
337 1.26 christos char ** fakeenv;
338 1.26 christos time_t now;
339 1.26 christos time_t t;
340 1.26 christos time_t newt;
341 1.26 christos struct tm tm;
342 1.26 christos struct tm newtm;
343 1.26 christos struct tm * tmp;
344 1.26 christos struct tm * newtmp;
345 1.1 jtc
346 1.29 christos cutlotime = absolute_min_time;
347 1.29 christos cuthitime = absolute_max_time;
348 1.17 mlelstv #if HAVE_GETTEXT
349 1.17 mlelstv (void) setlocale(LC_ALL, "");
350 1.3 jtc #ifdef TZ_DOMAINDIR
351 1.3 jtc (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
352 1.17 mlelstv #endif /* defined TEXTDOMAINDIR */
353 1.3 jtc (void) textdomain(TZ_DOMAIN);
354 1.17 mlelstv #endif /* HAVE_GETTEXT */
355 1.1 jtc progname = argv[0];
356 1.14 kleink for (i = 1; i < argc; ++i)
357 1.14 kleink if (strcmp(argv[i], "--version") == 0) {
358 1.28 christos (void) printf("zdump %s%s\n", PKGVERSION, TZVERSION);
359 1.17 mlelstv exit(EXIT_SUCCESS);
360 1.17 mlelstv } else if (strcmp(argv[i], "--help") == 0) {
361 1.22 christos usage(stdout, EXIT_SUCCESS);
362 1.14 kleink }
363 1.29 christos vflag = Vflag = 0;
364 1.29 christos cutarg = cuttimes = NULL;
365 1.29 christos for (;;)
366 1.29 christos switch (getopt(argc, argv, "c:t:vV")) {
367 1.29 christos case 'c': cutarg = optarg; break;
368 1.29 christos case 't': cuttimes = optarg; break;
369 1.29 christos case 'v': vflag = 1; break;
370 1.29 christos case 'V': Vflag = 1; break;
371 1.29 christos case -1:
372 1.29 christos if (! (optind == argc - 1 && strcmp(argv[optind], "=") == 0))
373 1.29 christos goto arg_processing_done;
374 1.29 christos /* Fall through. */
375 1.29 christos default:
376 1.29 christos usage(stderr, EXIT_FAILURE);
377 1.29 christos }
378 1.29 christos arg_processing_done:;
379 1.29 christos
380 1.29 christos if (vflag | Vflag) {
381 1.29 christos intmax_t lo;
382 1.29 christos intmax_t hi;
383 1.29 christos char dummy;
384 1.30 christos intmax_t cutloyear = ZDUMP_LO_YEAR;
385 1.30 christos intmax_t cuthiyear = ZDUMP_HI_YEAR;
386 1.17 mlelstv if (cutarg != NULL) {
387 1.29 christos if (sscanf(cutarg, "%"SCNdMAX"%c", &hi, &dummy) == 1) {
388 1.17 mlelstv cuthiyear = hi;
389 1.29 christos } else if (sscanf(cutarg, "%"SCNdMAX",%"SCNdMAX"%c",
390 1.17 mlelstv &lo, &hi, &dummy) == 2) {
391 1.17 mlelstv cutloyear = lo;
392 1.17 mlelstv cuthiyear = hi;
393 1.17 mlelstv } else {
394 1.17 mlelstv (void) fprintf(stderr, _("%s: wild -c argument %s\n"),
395 1.17 mlelstv progname, cutarg);
396 1.17 mlelstv exit(EXIT_FAILURE);
397 1.17 mlelstv }
398 1.17 mlelstv }
399 1.29 christos if (cutarg != NULL || cuttimes == NULL) {
400 1.29 christos cutlotime = yeartot(cutloyear);
401 1.29 christos cuthitime = yeartot(cuthiyear);
402 1.29 christos }
403 1.29 christos if (cuttimes != NULL) {
404 1.29 christos if (sscanf(cuttimes, "%"SCNdMAX"%c", &hi, &dummy) == 1) {
405 1.29 christos if (hi < cuthitime) {
406 1.29 christos if (hi < absolute_min_time)
407 1.29 christos hi = absolute_min_time;
408 1.29 christos cuthitime = hi;
409 1.29 christos }
410 1.29 christos } else if (sscanf(cuttimes, "%"SCNdMAX",%"SCNdMAX"%c",
411 1.29 christos &lo, &hi, &dummy) == 2) {
412 1.29 christos if (cutlotime < lo) {
413 1.29 christos if (absolute_max_time < lo)
414 1.29 christos lo = absolute_max_time;
415 1.29 christos cutlotime = lo;
416 1.29 christos }
417 1.29 christos if (hi < cuthitime) {
418 1.29 christos if (hi < absolute_min_time)
419 1.29 christos hi = absolute_min_time;
420 1.29 christos cuthitime = hi;
421 1.29 christos }
422 1.29 christos } else {
423 1.29 christos (void) fprintf(stderr,
424 1.29 christos _("%s: wild -t argument %s\n"),
425 1.29 christos progname, cuttimes);
426 1.29 christos exit(EXIT_FAILURE);
427 1.29 christos }
428 1.29 christos }
429 1.1 jtc }
430 1.1 jtc (void) time(&now);
431 1.1 jtc longest = 0;
432 1.1 jtc for (i = optind; i < argc; ++i)
433 1.1 jtc if (strlen(argv[i]) > longest)
434 1.1 jtc longest = strlen(argv[i]);
435 1.1 jtc {
436 1.26 christos int from;
437 1.26 christos int to;
438 1.1 jtc
439 1.17 mlelstv for (i = 0; environ[i] != NULL; ++i)
440 1.1 jtc continue;
441 1.26 christos fakeenv = malloc((i + 2) * sizeof *fakeenv);
442 1.1 jtc if (fakeenv == NULL ||
443 1.26 christos (fakeenv[0] = malloc(longest + 4)) == NULL) {
444 1.15 christos err(EXIT_FAILURE, "Can't allocated %zu bytes",
445 1.15 christos longest + 4);
446 1.1 jtc }
447 1.1 jtc to = 0;
448 1.4 mrg (void)strcpy(fakeenv[to++], "TZ="); /* XXX strcpy is safe */
449 1.1 jtc for (from = 0; environ[from] != NULL; ++from)
450 1.1 jtc if (strncmp(environ[from], "TZ=", 3) != 0)
451 1.1 jtc fakeenv[to++] = environ[from];
452 1.1 jtc fakeenv[to] = NULL;
453 1.1 jtc environ = fakeenv;
454 1.1 jtc }
455 1.1 jtc for (i = optind; i < argc; ++i) {
456 1.1 jtc static char buf[MAX_STRING_LENGTH];
457 1.1 jtc
458 1.4 mrg (void) strcpy(&fakeenv[0][3], argv[i]); /* XXX strcpy is safe */
459 1.29 christos if (! (vflag | Vflag)) {
460 1.3 jtc show(argv[i], now, FALSE);
461 1.1 jtc continue;
462 1.3 jtc }
463 1.17 mlelstv warned = FALSE;
464 1.17 mlelstv t = absolute_min_time;
465 1.29 christos if (!Vflag) {
466 1.29 christos show(argv[i], t, TRUE);
467 1.31 christos t += SECSPERDAY;
468 1.29 christos show(argv[i], t, TRUE);
469 1.29 christos }
470 1.17 mlelstv if (t < cutlotime)
471 1.17 mlelstv t = cutlotime;
472 1.17 mlelstv tmp = my_localtime(&t);
473 1.17 mlelstv if (tmp != NULL) {
474 1.17 mlelstv tm = *tmp;
475 1.17 mlelstv (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
476 1.17 mlelstv }
477 1.1 jtc for ( ; ; ) {
478 1.31 christos newt = (t < absolute_max_time - SECSPERDAY / 2
479 1.31 christos ? t + SECSPERDAY / 2
480 1.31 christos : absolute_max_time);
481 1.31 christos if (cuthitime <= newt)
482 1.1 jtc break;
483 1.17 mlelstv newtmp = localtime(&newt);
484 1.17 mlelstv if (newtmp != NULL)
485 1.17 mlelstv newtm = *newtmp;
486 1.17 mlelstv if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
487 1.17 mlelstv (delta(&newtm, &tm) != (newt - t) ||
488 1.1 jtc newtm.tm_isdst != tm.tm_isdst ||
489 1.17 mlelstv strcmp(abbr(&newtm), buf) != 0)) {
490 1.1 jtc newt = hunt(argv[i], t, newt);
491 1.17 mlelstv newtmp = localtime(&newt);
492 1.17 mlelstv if (newtmp != NULL) {
493 1.17 mlelstv newtm = *newtmp;
494 1.17 mlelstv (void) strncpy(buf,
495 1.17 mlelstv abbr(&newtm),
496 1.17 mlelstv (sizeof buf) - 1);
497 1.17 mlelstv }
498 1.1 jtc }
499 1.1 jtc t = newt;
500 1.1 jtc tm = newtm;
501 1.17 mlelstv tmp = newtmp;
502 1.1 jtc }
503 1.29 christos if (!Vflag) {
504 1.29 christos t = absolute_max_time;
505 1.31 christos t -= SECSPERDAY;
506 1.29 christos show(argv[i], t, TRUE);
507 1.31 christos t += SECSPERDAY;
508 1.29 christos show(argv[i], t, TRUE);
509 1.29 christos }
510 1.1 jtc }
511 1.1 jtc if (fflush(stdout) || ferror(stdout)) {
512 1.16 kleink err(EXIT_FAILURE, _("Error writing standard output"));
513 1.1 jtc }
514 1.1 jtc exit(EXIT_SUCCESS);
515 1.17 mlelstv /* If exit fails to exit... */
516 1.17 mlelstv return EXIT_FAILURE;
517 1.17 mlelstv }
518 1.1 jtc
519 1.1 jtc static time_t
520 1.26 christos yeartot(const long y)
521 1.17 mlelstv {
522 1.31 christos intmax_t myy, seconds, years;
523 1.29 christos time_t t;
524 1.17 mlelstv
525 1.17 mlelstv myy = EPOCH_YEAR;
526 1.17 mlelstv t = 0;
527 1.31 christos while (myy < y) {
528 1.31 christos if (SECSPER400YEARS_FITS && 400 <= y - myy) {
529 1.31 christos intmax_t diff400 = (y - myy) / 400;
530 1.31 christos if (INTMAX_MAX / SECSPER400YEARS < diff400)
531 1.31 christos return absolute_max_time;
532 1.31 christos seconds = diff400 * SECSPER400YEARS;
533 1.31 christos years = diff400 * 400;
534 1.31 christos } else {
535 1.17 mlelstv seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
536 1.31 christos years = 1;
537 1.31 christos }
538 1.31 christos myy += years;
539 1.31 christos if (t > absolute_max_time - seconds)
540 1.31 christos return absolute_max_time;
541 1.31 christos t += seconds;
542 1.31 christos }
543 1.31 christos while (y < myy) {
544 1.31 christos if (SECSPER400YEARS_FITS && y + 400 <= myy && myy < 0) {
545 1.31 christos intmax_t diff400 = (myy - y) / 400;
546 1.31 christos if (INTMAX_MAX / SECSPER400YEARS < diff400)
547 1.31 christos return absolute_min_time;
548 1.31 christos seconds = diff400 * SECSPER400YEARS;
549 1.31 christos years = diff400 * 400;
550 1.17 mlelstv } else {
551 1.31 christos seconds = isleap(myy - 1) ? SECSPERLYEAR : SECSPERNYEAR;
552 1.31 christos years = 1;
553 1.17 mlelstv }
554 1.31 christos myy -= years;
555 1.31 christos if (t < absolute_min_time + seconds)
556 1.31 christos return absolute_min_time;
557 1.31 christos t -= seconds;
558 1.17 mlelstv }
559 1.17 mlelstv return t;
560 1.17 mlelstv }
561 1.17 mlelstv
562 1.17 mlelstv static time_t
563 1.17 mlelstv hunt(char *name, time_t lot, time_t hit)
564 1.17 mlelstv {
565 1.17 mlelstv time_t t;
566 1.17 mlelstv struct tm lotm;
567 1.26 christos struct tm * lotmp;
568 1.17 mlelstv struct tm tm;
569 1.26 christos struct tm * tmp;
570 1.17 mlelstv char loab[MAX_STRING_LENGTH];
571 1.17 mlelstv
572 1.17 mlelstv lotmp = my_localtime(&lot);
573 1.17 mlelstv if (lotmp != NULL) {
574 1.17 mlelstv lotm = *lotmp;
575 1.17 mlelstv (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
576 1.17 mlelstv }
577 1.17 mlelstv for ( ; ; ) {
578 1.29 christos time_t diff = hit - lot;
579 1.17 mlelstv if (diff < 2)
580 1.17 mlelstv break;
581 1.17 mlelstv t = lot;
582 1.17 mlelstv t += diff / 2;
583 1.1 jtc if (t <= lot)
584 1.1 jtc ++t;
585 1.1 jtc else if (t >= hit)
586 1.1 jtc --t;
587 1.17 mlelstv tmp = my_localtime(&t);
588 1.17 mlelstv if (tmp != NULL)
589 1.17 mlelstv tm = *tmp;
590 1.17 mlelstv if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
591 1.17 mlelstv (delta(&tm, &lotm) == (t - lot) &&
592 1.1 jtc tm.tm_isdst == lotm.tm_isdst &&
593 1.17 mlelstv strcmp(abbr(&tm), loab) == 0)) {
594 1.1 jtc lot = t;
595 1.1 jtc lotm = tm;
596 1.17 mlelstv lotmp = tmp;
597 1.1 jtc } else hit = t;
598 1.1 jtc }
599 1.1 jtc show(name, lot, TRUE);
600 1.1 jtc show(name, hit, TRUE);
601 1.1 jtc return hit;
602 1.1 jtc }
603 1.1 jtc
604 1.1 jtc /*
605 1.17 mlelstv ** Thanks to Paul Eggert for logic used in delta.
606 1.1 jtc */
607 1.1 jtc
608 1.29 christos static intmax_t
609 1.26 christos delta(struct tm *newp, struct tm *oldp)
610 1.1 jtc {
611 1.29 christos intmax_t result;
612 1.29 christos int tmy;
613 1.1 jtc
614 1.1 jtc if (newp->tm_year < oldp->tm_year)
615 1.1 jtc return -delta(oldp, newp);
616 1.1 jtc result = 0;
617 1.1 jtc for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
618 1.17 mlelstv result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
619 1.1 jtc result += newp->tm_yday - oldp->tm_yday;
620 1.1 jtc result *= HOURSPERDAY;
621 1.1 jtc result += newp->tm_hour - oldp->tm_hour;
622 1.1 jtc result *= MINSPERHOUR;
623 1.1 jtc result += newp->tm_min - oldp->tm_min;
624 1.1 jtc result *= SECSPERMIN;
625 1.1 jtc result += newp->tm_sec - oldp->tm_sec;
626 1.1 jtc return result;
627 1.1 jtc }
628 1.1 jtc
629 1.1 jtc static void
630 1.17 mlelstv show(char *zone, time_t t, int v)
631 1.1 jtc {
632 1.26 christos struct tm * tmp;
633 1.1 jtc
634 1.5 jtc (void) printf("%-*s ", (int) longest, zone);
635 1.1 jtc if (v) {
636 1.17 mlelstv tmp = gmtime(&t);
637 1.17 mlelstv if (tmp == NULL) {
638 1.17 mlelstv (void) printf(tformat(), t);
639 1.17 mlelstv } else {
640 1.17 mlelstv dumptime(tmp);
641 1.31 christos (void) printf(" UT");
642 1.17 mlelstv }
643 1.17 mlelstv (void) printf(" = ");
644 1.17 mlelstv }
645 1.17 mlelstv tmp = my_localtime(&t);
646 1.17 mlelstv dumptime(tmp);
647 1.17 mlelstv if (tmp != NULL) {
648 1.17 mlelstv if (*abbr(tmp) != '\0')
649 1.17 mlelstv (void) printf(" %s", abbr(tmp));
650 1.17 mlelstv if (v) {
651 1.17 mlelstv (void) printf(" isdst=%d", tmp->tm_isdst);
652 1.1 jtc #ifdef TM_GMTOFF
653 1.17 mlelstv (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
654 1.1 jtc #endif /* defined TM_GMTOFF */
655 1.17 mlelstv }
656 1.1 jtc }
657 1.1 jtc (void) printf("\n");
658 1.17 mlelstv if (tmp != NULL && *abbr(tmp) != '\0')
659 1.17 mlelstv abbrok(abbr(tmp), zone);
660 1.1 jtc }
661 1.1 jtc
662 1.9 mycroft static const char *
663 1.26 christos abbr(struct tm *tmp)
664 1.1 jtc {
665 1.26 christos const char * result;
666 1.9 mycroft static const char nada;
667 1.1 jtc
668 1.1 jtc if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
669 1.1 jtc return &nada;
670 1.1 jtc result = tzname[tmp->tm_isdst];
671 1.1 jtc return (result == NULL) ? &nada : result;
672 1.1 jtc }
673 1.17 mlelstv
674 1.17 mlelstv /*
675 1.17 mlelstv ** The code below can fail on certain theoretical systems;
676 1.17 mlelstv ** it works on all known real-world systems as of 2004-12-30.
677 1.17 mlelstv */
678 1.17 mlelstv
679 1.17 mlelstv static const char *
680 1.17 mlelstv tformat(void)
681 1.17 mlelstv {
682 1.17 mlelstv if (0 > (time_t) -1) { /* signed */
683 1.29 christos if (sizeof (time_t) == sizeof (intmax_t))
684 1.29 christos return "%"PRIdMAX;
685 1.17 mlelstv if (sizeof (time_t) > sizeof (long))
686 1.17 mlelstv return "%lld";
687 1.17 mlelstv if (sizeof (time_t) > sizeof (int))
688 1.17 mlelstv return "%ld";
689 1.17 mlelstv return "%d";
690 1.17 mlelstv }
691 1.29 christos #ifdef PRIuMAX
692 1.29 christos if (sizeof (time_t) == sizeof (uintmax_t))
693 1.29 christos return "%"PRIuMAX;
694 1.29 christos #endif
695 1.17 mlelstv if (sizeof (time_t) > sizeof (unsigned long))
696 1.17 mlelstv return "%llu";
697 1.17 mlelstv if (sizeof (time_t) > sizeof (unsigned int))
698 1.17 mlelstv return "%lu";
699 1.17 mlelstv return "%u";
700 1.17 mlelstv }
701 1.17 mlelstv
702 1.17 mlelstv static void
703 1.26 christos dumptime(const struct tm *timeptr)
704 1.17 mlelstv {
705 1.17 mlelstv static const char wday_name[][3] = {
706 1.17 mlelstv "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
707 1.17 mlelstv };
708 1.17 mlelstv static const char mon_name[][3] = {
709 1.17 mlelstv "Jan", "Feb", "Mar", "Apr", "May", "Jun",
710 1.17 mlelstv "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
711 1.17 mlelstv };
712 1.26 christos const char * wn;
713 1.26 christos const char * mn;
714 1.26 christos int lead;
715 1.26 christos int trail;
716 1.17 mlelstv
717 1.17 mlelstv if (timeptr == NULL) {
718 1.17 mlelstv (void) printf("NULL");
719 1.17 mlelstv return;
720 1.17 mlelstv }
721 1.17 mlelstv /*
722 1.17 mlelstv ** The packaged versions of localtime and gmtime never put out-of-range
723 1.17 mlelstv ** values in tm_wday or tm_mon, but since this code might be compiled
724 1.17 mlelstv ** with other (perhaps experimental) versions, paranoia is in order.
725 1.17 mlelstv */
726 1.17 mlelstv if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
727 1.17 mlelstv (int) (sizeof wday_name / sizeof wday_name[0]))
728 1.17 mlelstv wn = "???";
729 1.17 mlelstv else wn = wday_name[timeptr->tm_wday];
730 1.17 mlelstv if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
731 1.17 mlelstv (int) (sizeof mon_name / sizeof mon_name[0]))
732 1.17 mlelstv mn = "???";
733 1.17 mlelstv else mn = mon_name[timeptr->tm_mon];
734 1.17 mlelstv (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
735 1.17 mlelstv wn, mn,
736 1.17 mlelstv timeptr->tm_mday, timeptr->tm_hour,
737 1.17 mlelstv timeptr->tm_min, timeptr->tm_sec);
738 1.17 mlelstv #define DIVISOR 10
739 1.17 mlelstv trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
740 1.17 mlelstv lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
741 1.17 mlelstv trail / DIVISOR;
742 1.17 mlelstv trail %= DIVISOR;
743 1.17 mlelstv if (trail < 0 && lead > 0) {
744 1.17 mlelstv trail += DIVISOR;
745 1.17 mlelstv --lead;
746 1.17 mlelstv } else if (lead < 0 && trail > 0) {
747 1.17 mlelstv trail -= DIVISOR;
748 1.17 mlelstv ++lead;
749 1.17 mlelstv }
750 1.17 mlelstv if (lead == 0)
751 1.17 mlelstv (void) printf("%d", trail);
752 1.17 mlelstv else (void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));
753 1.17 mlelstv }
754