]>
git.saurik.com Git - apple/system_cmds.git/blob - zdump.tproj/zdump.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
24 #if defined(LIBC_SCCS) && !defined(lint)
26 static char elsieid
[] = "@(#)zdump.c 7.24";
28 static char rcsid
[] = "$OpenBSD: zdump.c,v 1.5 1997/01/21 04:52:45 millert Exp $";
30 #endif /* LIBC_SCCS and not lint */
33 ** This code has been made independent of the rest of the time
34 ** conversion package to increase confidence in the verification it provides.
35 ** You can use this code to help in verifying other implementations.
38 #include "stdio.h" /* for stdout, stderr, perror */
39 #include "string.h" /* for strcpy */
40 #include "sys/types.h" /* for time_t */
41 #include "time.h" /* for struct tm */
42 #include "stdlib.h" /* for exit, malloc, atoi */
44 #ifndef MAX_STRING_LENGTH
45 #define MAX_STRING_LENGTH 1024
46 #endif /* !defined MAX_STRING_LENGTH */
50 #endif /* !defined TRUE */
54 #endif /* !defined FALSE */
57 #define EXIT_SUCCESS 0
58 #endif /* !defined EXIT_SUCCESS */
61 #define EXIT_FAILURE 1
62 #endif /* !defined EXIT_FAILURE */
66 #endif /* !defined SECSPERMIN */
69 #define MINSPERHOUR 60
70 #endif /* !defined MINSPERHOUR */
73 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
74 #endif /* !defined SECSPERHOUR */
77 #define HOURSPERDAY 24
78 #endif /* !defined HOURSPERDAY */
81 #define EPOCH_YEAR 1970
82 #endif /* !defined EPOCH_YEAR */
85 #define TM_YEAR_BASE 1900
86 #endif /* !defined TM_YEAR_BASE */
89 #define DAYSPERNYEAR 365
90 #endif /* !defined DAYSPERNYEAR */
93 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
94 #endif /* !defined isleap */
97 #include "locale.h" /* for setlocale */
99 #endif /* HAVE_GETTEXT - 0 */
104 #endif /* defined lint */
108 #endif /* defined __GNUC__ */
109 #endif /* !defined lint */
110 #endif /* !defined GNUC_or_lint */
114 #define INITIALIZE(x) ((x) = 0)
115 #endif /* defined GNUC_or_lint */
117 #define INITIALIZE(x)
118 #endif /* !defined GNUC_or_lint */
119 #endif /* !defined INITIALIZE */
122 ** For the benefit of GNU folk...
123 ** `_(MSGID)' uses the current locale's message library string for MSGID.
124 ** The default is to use gettext if available, and use MSGID otherwise.
129 #define _(msgid) gettext(msgid)
130 #else /* !(HAVE_GETTEXT - 0) */
131 #define _(msgid) msgid
132 #endif /* !(HAVE_GETTEXT - 0) */
133 #endif /* !defined _ */
136 #define TZ_DOMAIN "tz"
137 #endif /* !defined TZ_DOMAIN */
139 extern char ** environ
;
141 extern char * optarg
;
143 extern time_t time();
144 extern char * tzname
[2];
146 static char * abbr();
148 static time_t hunt();
150 static char * progname
;
161 register char * cutoff
;
162 register int cutyear
;
163 register long cuttime
;
174 (void) setlocale(LC_MESSAGES
, "");
176 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
177 #endif /* defined(TEXTDOMAINDIR) */
178 (void) textdomain(TZ_DOMAIN
);
179 #endif /* HAVE_GETTEXT - 0 */
183 while ((c
= getopt(argc
, argv
, "c:v")) == 'c' || c
== 'v')
186 else cutoff
= optarg
;
188 (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)) {
189 (void) fprintf(stderr
,
190 _("%s: usage is %s [ -v ] [ -c cutoff ] zonename ...\n"),
192 (void) exit(EXIT_FAILURE
);
194 if (cutoff
!= NULL
) {
197 cutyear
= atoi(cutoff
);
199 for (y
= EPOCH_YEAR
; y
< cutyear
; ++y
)
200 cuttime
+= DAYSPERNYEAR
+ isleap(y
);
201 cuttime
*= SECSPERHOUR
* HOURSPERDAY
;
205 for (i
= optind
; i
< argc
; ++i
)
206 if (strlen(argv
[i
]) > longest
)
207 longest
= strlen(argv
[i
]);
208 for (hibit
= 1; (hibit
<< 1) != 0; hibit
<<= 1)
214 for (i
= 0; environ
[i
] != NULL
; ++i
)
216 fakeenv
= (char **) malloc((size_t) ((i
+ 2) *
218 if (fakeenv
== NULL
||
219 (fakeenv
[0] = (char *) malloc((size_t) (longest
+
221 (void) perror(progname
);
222 (void) exit(EXIT_FAILURE
);
225 (void) strcpy(fakeenv
[to
++], "TZ=");
226 for (from
= 0; environ
[from
] != NULL
; ++from
)
227 if (strncmp(environ
[from
], "TZ=", 3) != 0)
228 fakeenv
[to
++] = environ
[from
];
232 for (i
= optind
; i
< argc
; ++i
) {
233 static char buf
[MAX_STRING_LENGTH
];
235 (void) strcpy(&fakeenv
[0][3], argv
[i
]);
236 show(argv
[i
], now
, FALSE
);
240 ** Get lowest value of t.
243 if (t
> 0) /* time_t is unsigned */
245 show(argv
[i
], t
, TRUE
);
246 t
+= SECSPERHOUR
* HOURSPERDAY
;
247 show(argv
[i
], t
, TRUE
);
249 (void) strncpy(buf
, abbr(&tm
), (sizeof buf
) - 1);
251 if (cutoff
!= NULL
&& t
>= cuttime
)
253 newt
= t
+ SECSPERHOUR
* 12;
254 if (cutoff
!= NULL
&& newt
>= cuttime
)
258 newtm
= *localtime(&newt
);
259 if (delta(&newtm
, &tm
) != (newt
- t
) ||
260 newtm
.tm_isdst
!= tm
.tm_isdst
||
261 strcmp(abbr(&newtm
), buf
) != 0) {
262 newt
= hunt(argv
[i
], t
, newt
);
263 newtm
= *localtime(&newt
);
264 (void) strncpy(buf
, abbr(&newtm
),
271 ** Get highest value of t.
274 if (t
< 0) /* time_t is signed */
276 t
-= SECSPERHOUR
* HOURSPERDAY
;
277 show(argv
[i
], t
, TRUE
);
278 t
+= SECSPERHOUR
* HOURSPERDAY
;
279 show(argv
[i
], t
, TRUE
);
281 if (fflush(stdout
) || ferror(stdout
)) {
282 (void) fprintf(stderr
, _("%s: Error writing standard output "),
284 (void) perror(_("standard output"));
285 (void) exit(EXIT_FAILURE
);
289 /* gcc -Wall pacifier */
303 static char loab
[MAX_STRING_LENGTH
];
305 lotm
= *localtime(&lot
);
306 (void) strncpy(loab
, abbr(&lotm
), (sizeof loab
) - 1);
307 loab
[(sizeof loab
) - 1] = '\0';
308 while ((hit
- lot
) >= 2) {
309 t
= lot
/ 2 + hit
/ 2;
315 if (delta(&tm
, &lotm
) == (t
- lot
) &&
316 tm
.tm_isdst
== lotm
.tm_isdst
&&
317 strcmp(abbr(&tm
), loab
) == 0) {
322 show(name
, lot
, TRUE
);
323 show(name
, hit
, TRUE
);
328 ** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta.
339 if (newp
->tm_year
< oldp
->tm_year
)
340 return -delta(oldp
, newp
);
342 for (tmy
= oldp
->tm_year
; tmy
< newp
->tm_year
; ++tmy
)
343 result
+= DAYSPERNYEAR
+ isleap(tmy
+ TM_YEAR_BASE
);
344 result
+= newp
->tm_yday
- oldp
->tm_yday
;
345 result
*= HOURSPERDAY
;
346 result
+= newp
->tm_hour
- oldp
->tm_hour
;
347 result
*= MINSPERHOUR
;
348 result
+= newp
->tm_min
- oldp
->tm_min
;
349 result
*= SECSPERMIN
;
350 result
+= newp
->tm_sec
- oldp
->tm_sec
;
354 extern struct tm
* localtime();
364 (void) printf("%-*s ", longest
, zone
);
366 (void) printf("%.24s GMT = ", asctime(gmtime(&t
)));
368 (void) printf("%.24s", asctime(tmp
));
369 if (*abbr(tmp
) != '\0')
370 (void) printf(" %s", abbr(tmp
));
372 (void) printf(" isdst=%d", tmp
->tm_isdst
);
374 (void) printf(" gmtoff=%ld", tmp
->TM_GMTOFF
);
375 #endif /* defined TM_GMTOFF */
384 register char * result
;
387 if (tmp
->tm_isdst
!= 0 && tmp
->tm_isdst
!= 1)
389 result
= tzname
[tmp
->tm_isdst
];
390 return (result
== NULL
) ? &nada
: result
;