1 static char elsieid
[] = "@(#)zdump.c 7.31";
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.
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 */
15 #ifndef MAX_STRING_LENGTH
16 #define MAX_STRING_LENGTH 1024
17 #endif /* !defined MAX_STRING_LENGTH */
21 #endif /* !defined TRUE */
25 #endif /* !defined FALSE */
28 #define EXIT_SUCCESS 0
29 #endif /* !defined EXIT_SUCCESS */
32 #define EXIT_FAILURE 1
33 #endif /* !defined EXIT_FAILURE */
37 #endif /* !defined SECSPERMIN */
40 #define MINSPERHOUR 60
41 #endif /* !defined MINSPERHOUR */
44 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
45 #endif /* !defined SECSPERHOUR */
48 #define HOURSPERDAY 24
49 #endif /* !defined HOURSPERDAY */
52 #define EPOCH_YEAR 1970
53 #endif /* !defined EPOCH_YEAR */
56 #define TM_YEAR_BASE 1900
57 #endif /* !defined TM_YEAR_BASE */
60 #define DAYSPERNYEAR 365
61 #endif /* !defined DAYSPERNYEAR */
64 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
65 #endif /* !defined isleap */
68 #include "locale.h" /* for setlocale */
70 #endif /* HAVE_GETTEXT - 0 */
75 #endif /* defined lint */
79 #endif /* defined __GNUC__ */
80 #endif /* !defined lint */
81 #endif /* !defined GNUC_or_lint */
85 #define INITIALIZE(x) ((x) = 0)
86 #endif /* defined GNUC_or_lint */
89 #endif /* !defined GNUC_or_lint */
90 #endif /* !defined INITIALIZE */
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.
100 #define _(msgid) gettext(msgid)
101 #else /* !(HAVE_GETTEXT - 0) */
102 #define _(msgid) msgid
103 #endif /* !(HAVE_GETTEXT - 0) */
104 #endif /* !defined _ */
107 #define TZ_DOMAIN "tz"
108 #endif /* !defined TZ_DOMAIN */
113 #endif /* defined __STDC__ */
116 #endif /* !defined __STDC__ */
117 #endif /* !defined P */
119 extern char ** environ
;
120 extern int getopt
P((int argc
, char * const argv
[],
121 const char * options
));
122 extern char * optarg
;
124 extern char * tzname
[2];
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
));
141 register char * cutoff
;
142 register int cutyear
;
143 register long cuttime
;
154 (void) setlocale(LC_CTYPE
, "");
155 (void) setlocale(LC_MESSAGES
, "");
157 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
158 #endif /* defined(TEXTDOMAINDIR) */
159 (void) textdomain(TZ_DOMAIN
);
160 #endif /* HAVE_GETTEXT - 0 */
162 for (i
= 1; i
< argc
; ++i
)
163 if (strcmp(argv
[i
], "--version") == 0) {
164 (void) printf("%s\n", elsieid
);
165 (void) exit(EXIT_SUCCESS
);
169 while ((c
= getopt(argc
, argv
, "c:v")) == 'c' || c
== 'v')
172 else cutoff
= optarg
;
173 if ((c
!= EOF
&& c
!= -1) ||
174 (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)) {
175 (void) fprintf(stderr
,
176 _("%s: usage is %s [ --version ] [ -v ] [ -c cutoff ] zonename ...\n"),
178 (void) exit(EXIT_FAILURE
);
180 if (cutoff
!= NULL
) {
183 cutyear
= atoi(cutoff
);
185 for (y
= EPOCH_YEAR
; y
< cutyear
; ++y
)
186 cuttime
+= DAYSPERNYEAR
+ isleap(y
);
187 cuttime
*= SECSPERHOUR
* HOURSPERDAY
;
191 for (i
= optind
; i
< argc
; ++i
)
192 if (strlen(argv
[i
]) > longest
)
193 longest
= strlen(argv
[i
]);
194 for (hibit
= 1; (hibit
<< 1) != 0; hibit
<<= 1)
200 for (i
= 0; environ
[i
] != NULL
; ++i
)
202 fakeenv
= (char **) malloc((size_t) ((i
+ 2) *
204 if (fakeenv
== NULL
||
205 (fakeenv
[0] = (char *) malloc(longest
+ 4)) == NULL
) {
206 (void) perror(progname
);
207 (void) exit(EXIT_FAILURE
);
210 (void) strcpy(fakeenv
[to
++], "TZ=");
211 for (from
= 0; environ
[from
] != NULL
; ++from
)
212 if (strncmp(environ
[from
], "TZ=", 3) != 0)
213 fakeenv
[to
++] = environ
[from
];
217 for (i
= optind
; i
< argc
; ++i
) {
218 static char buf
[MAX_STRING_LENGTH
];
220 (void) strcpy(&fakeenv
[0][3], argv
[i
]);
222 show(argv
[i
], now
, FALSE
);
226 ** Get lowest value of t.
229 if (t
> 0) /* time_t is unsigned */
231 show(argv
[i
], t
, TRUE
);
232 t
+= SECSPERHOUR
* HOURSPERDAY
;
233 show(argv
[i
], t
, TRUE
);
235 (void) strncpy(buf
, abbr(&tm
), (sizeof buf
) - 1);
237 if (cutoff
!= NULL
&& t
>= cuttime
)
239 newt
= t
+ SECSPERHOUR
* 12;
240 if (cutoff
!= NULL
&& newt
>= cuttime
)
244 newtm
= *localtime(&newt
);
245 if (delta(&newtm
, &tm
) != (newt
- t
) ||
246 newtm
.tm_isdst
!= tm
.tm_isdst
||
247 strcmp(abbr(&newtm
), buf
) != 0) {
248 newt
= hunt(argv
[i
], t
, newt
);
249 newtm
= *localtime(&newt
);
250 (void) strncpy(buf
, abbr(&newtm
),
257 ** Get highest value of t.
260 if (t
< 0) /* time_t is signed */
262 t
-= SECSPERHOUR
* HOURSPERDAY
;
263 show(argv
[i
], t
, TRUE
);
264 t
+= SECSPERHOUR
* HOURSPERDAY
;
265 show(argv
[i
], t
, TRUE
);
267 if (fflush(stdout
) || ferror(stdout
)) {
268 (void) fprintf(stderr
, "%s: ", argv
[0]);
269 (void) perror(_("Error writing standard output"));
270 (void) exit(EXIT_FAILURE
);
274 /* gcc -Wall pacifier */
288 static char loab
[MAX_STRING_LENGTH
];
290 lotm
= *localtime(&lot
);
291 (void) strncpy(loab
, abbr(&lotm
), (sizeof loab
) - 1);
292 while ((hit
- lot
) >= 2) {
293 t
= lot
/ 2 + hit
/ 2;
299 if (delta(&tm
, &lotm
) == (t
- lot
) &&
300 tm
.tm_isdst
== lotm
.tm_isdst
&&
301 strcmp(abbr(&tm
), loab
) == 0) {
306 show(name
, lot
, TRUE
);
307 show(name
, hit
, TRUE
);
312 ** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta.
323 if (newp
->tm_year
< oldp
->tm_year
)
324 return -delta(oldp
, newp
);
326 for (tmy
= oldp
->tm_year
; tmy
< newp
->tm_year
; ++tmy
)
327 result
+= DAYSPERNYEAR
+ isleap(tmy
+ TM_YEAR_BASE
);
328 result
+= newp
->tm_yday
- oldp
->tm_yday
;
329 result
*= HOURSPERDAY
;
330 result
+= newp
->tm_hour
- oldp
->tm_hour
;
331 result
*= MINSPERHOUR
;
332 result
+= newp
->tm_min
- oldp
->tm_min
;
333 result
*= SECSPERMIN
;
334 result
+= newp
->tm_sec
- oldp
->tm_sec
;
346 (void) printf("%-*s ", (int) longest
, zone
);
348 (void) printf("%.24s UTC = ", asctime(gmtime(&t
)));
350 (void) printf("%.24s", asctime(tmp
));
351 if (*abbr(tmp
) != '\0')
352 (void) printf(" %s", abbr(tmp
));
354 (void) printf(" isdst=%d", tmp
->tm_isdst
);
356 (void) printf(" gmtoff=%ld", tmp
->TM_GMTOFF
);
357 #endif /* defined TM_GMTOFF */
366 register char * result
;
369 if (tmp
->tm_isdst
!= 0 && tmp
->tm_isdst
!= 1)
371 result
= tzname
[tmp
->tm_isdst
];
372 return (result
== NULL
) ? &nada
: result
;