]>
git.saurik.com Git - apple/system_cmds.git/blob - zdump.tproj/zdump.c
1 static const char elsieid
[] = "@(#)zdump.c 7.31";
4 static const char rcsid
[] =
5 "$FreeBSD: src/usr.sbin/zic/zdump.c,v 1.10 2008/02/19 07:09:19 ru Exp $";
9 ** This code has been made independent of the rest of the time
10 ** conversion package to increase confidence in the verification it provides.
11 ** You can use this code to help in verifying other implementations.
15 #include <stdio.h> /* for stdout, stderr */
16 #include <stdlib.h> /* for exit, malloc, atoi */
17 #include <string.h> /* for strcpy */
18 #include <sys/types.h> /* for time_t */
19 #include <time.h> /* for struct tm */
23 #ifndef MAX_STRING_LENGTH
24 #define MAX_STRING_LENGTH 1024
25 #endif /* !defined MAX_STRING_LENGTH */
29 #endif /* !defined TRUE */
33 #endif /* !defined FALSE */
36 #define EXIT_SUCCESS 0
37 #endif /* !defined EXIT_SUCCESS */
40 #define EXIT_FAILURE 1
41 #endif /* !defined EXIT_FAILURE */
45 #endif /* !defined SECSPERMIN */
48 #define MINSPERHOUR 60
49 #endif /* !defined MINSPERHOUR */
52 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
53 #endif /* !defined SECSPERHOUR */
56 #define HOURSPERDAY 24
57 #endif /* !defined HOURSPERDAY */
60 #define EPOCH_YEAR 1970
61 #endif /* !defined EPOCH_YEAR */
64 #define TM_YEAR_BASE 1900
65 #endif /* !defined TM_YEAR_BASE */
68 #define DAYSPERNYEAR 365
69 #endif /* !defined DAYSPERNYEAR */
72 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
73 #endif /* !defined isleap */
76 #include "locale.h" /* for setlocale */
78 #endif /* HAVE_GETTEXT - 0 */
83 #endif /* defined lint */
87 #endif /* defined __GNUC__ */
88 #endif /* !defined lint */
89 #endif /* !defined GNUC_or_lint */
93 #define INITIALIZE(x) ((x) = 0)
94 #endif /* defined GNUC_or_lint */
97 #endif /* !defined GNUC_or_lint */
98 #endif /* !defined INITIALIZE */
101 ** For the benefit of GNU folk...
102 ** `_(MSGID)' uses the current locale's message library string for MSGID.
103 ** The default is to use gettext if available, and use MSGID otherwise.
108 #define _(msgid) gettext(msgid)
109 #else /* !(HAVE_GETTEXT - 0) */
110 #define _(msgid) msgid
111 #endif /* !(HAVE_GETTEXT - 0) */
112 #endif /* !defined _ */
115 #define TZ_DOMAIN "tz"
116 #endif /* !defined TZ_DOMAIN */
121 #endif /* defined __STDC__ */
124 #endif /* !defined __STDC__ */
125 #endif /* !defined P */
127 extern char ** environ
;
128 extern char * tzname
[2];
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 void show
P((char * zone
, time_t t
, int v
));
135 static void usage(void);
145 register char * cutoff
;
146 register int cutyear
;
147 register long cuttime
;
153 // <rdar://problem/6013740>
154 // The approach of walking through every day from the minimum
155 // possible time_t value to the maximum possible time_t value
156 // falls apart with 64-bit time_t (takes too long to iterate,
157 // and causes gmtime(3) and localtime(3) to return EOVERFLOW
158 // which this code does not anticipate). Limiting the time_t
159 // range to [INT_MIN:INT_MAX] even on LP64.
167 (void) setlocale(LC_MESSAGES
, "");
169 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
170 #endif /* defined(TEXTDOMAINDIR) */
171 (void) textdomain(TZ_DOMAIN
);
172 #endif /* HAVE_GETTEXT - 0 */
173 for (i
= 1; i
< argc
; ++i
)
174 if (strcmp(argv
[i
], "--version") == 0) {
175 errx(EXIT_SUCCESS
, "%s", elsieid
);
179 while ((c
= getopt(argc
, argv
, "c:v")) == 'c' || c
== 'v')
182 else cutoff
= optarg
;
184 (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)) {
187 if (cutoff
!= NULL
) {
190 cutyear
= atoi(cutoff
);
192 for (y
= EPOCH_YEAR
; y
< cutyear
; ++y
)
193 cuttime
+= DAYSPERNYEAR
+ isleap(y
);
194 cuttime
*= SECSPERHOUR
* HOURSPERDAY
;
198 for (i
= optind
; i
< argc
; ++i
)
199 if (strlen(argv
[i
]) > longest
)
200 longest
= strlen(argv
[i
]);
202 for (hibit
= 1; (hibit
<< 1) != 0; hibit
<<= 1)
209 for (i
= 0; environ
[i
] != NULL
; ++i
)
211 fakeenv
= (char **) malloc((size_t) ((i
+ 2) *
213 if (fakeenv
== NULL
||
214 (fakeenv
[0] = (char *) malloc((size_t) (longest
+
217 _("malloc() failed"));
219 (void) strcpy(fakeenv
[to
++], "TZ=");
220 for (from
= 0; environ
[from
] != NULL
; ++from
)
221 if (strncmp(environ
[from
], "TZ=", 3) != 0)
222 fakeenv
[to
++] = environ
[from
];
226 for (i
= optind
; i
< argc
; ++i
) {
227 static char buf
[MAX_STRING_LENGTH
];
229 (void) strcpy(&fakeenv
[0][3], argv
[i
]);
231 show(argv
[i
], now
, FALSE
);
235 ** Get lowest value of t.
241 if (t
> 0) /* time_t is unsigned */
244 show(argv
[i
], t
, TRUE
);
245 t
+= SECSPERHOUR
* HOURSPERDAY
;
246 show(argv
[i
], t
, TRUE
);
248 (void) strncpy(buf
, abbr(&tm
), (sizeof buf
) - 1);
250 if (cutoff
!= NULL
&& t
>= cuttime
)
252 newt
= t
+ SECSPERHOUR
* 12;
253 if (cutoff
!= NULL
&& newt
>= cuttime
)
262 newtm
= *localtime(&newt
);
263 if (delta(&newtm
, &tm
) != (newt
- t
) ||
264 newtm
.tm_isdst
!= tm
.tm_isdst
||
265 strcmp(abbr(&newtm
), buf
) != 0) {
266 newt
= hunt(argv
[i
], t
, newt
);
267 newtm
= *localtime(&newt
);
268 (void) strncpy(buf
, abbr(&newtm
),
275 ** Get highest value of t.
281 if (t
< 0) /* time_t is signed */
284 t
-= SECSPERHOUR
* HOURSPERDAY
;
285 show(argv
[i
], t
, TRUE
);
286 t
+= SECSPERHOUR
* HOURSPERDAY
;
287 show(argv
[i
], t
, TRUE
);
289 if (fflush(stdout
) || ferror(stdout
))
290 errx(EXIT_FAILURE
, _("error writing standard output"));
293 /* gcc -Wall pacifier */
302 _("usage: zdump [--version] [-v] [-c cutoff] zonename ...\n"));
315 static char loab
[MAX_STRING_LENGTH
];
317 lotm
= *localtime(&lot
);
318 (void) strncpy(loab
, abbr(&lotm
), (sizeof loab
) - 1);
319 while ((hit
- lot
) >= 2) {
320 t
= lot
/ 2 + hit
/ 2;
326 if (delta(&tm
, &lotm
) == (t
- lot
) &&
327 tm
.tm_isdst
== lotm
.tm_isdst
&&
328 strcmp(abbr(&tm
), loab
) == 0) {
333 show(name
, lot
, TRUE
);
334 show(name
, hit
, TRUE
);
339 ** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta.
350 if (newp
->tm_year
< oldp
->tm_year
)
351 return -delta(oldp
, newp
);
353 for (tmy
= oldp
->tm_year
; tmy
< newp
->tm_year
; ++tmy
)
354 result
+= DAYSPERNYEAR
+ isleap(tmy
+ TM_YEAR_BASE
);
355 result
+= newp
->tm_yday
- oldp
->tm_yday
;
356 result
*= HOURSPERDAY
;
357 result
+= newp
->tm_hour
- oldp
->tm_hour
;
358 result
*= MINSPERHOUR
;
359 result
+= newp
->tm_min
- oldp
->tm_min
;
360 result
*= SECSPERMIN
;
361 result
+= newp
->tm_sec
- oldp
->tm_sec
;
373 (void) printf("%-*s ", (int) longest
, zone
);
375 (void) printf("%.24s UTC = ", asctime(gmtime(&t
)));
377 (void) printf("%.24s", asctime(tmp
));
378 if (*abbr(tmp
) != '\0')
379 (void) printf(" %s", abbr(tmp
));
381 (void) printf(" isdst=%d", tmp
->tm_isdst
);
383 (void) printf(" gmtoff=%ld", tmp
->TM_GMTOFF
);
384 #endif /* defined TM_GMTOFF */
393 register char * result
;
396 if (tmp
->tm_isdst
!= 0 && tmp
->tm_isdst
!= 1)
398 result
= tzname
[tmp
->tm_isdst
];
399 return (result
== NULL
) ? &nada
: result
;