3 static char elsieid
[] = "@(#)zdump.c 7.12";
4 #endif /* !defined NOID */
5 #endif /* !defined lint */
8 ** This code has been made independent of the rest of the time
9 ** conversion package to increase confidence in the verification it provides.
10 ** You can use this code to help in verifying other implementations.
13 #include "stdio.h" /* for stdout, stderr */
14 #include "string.h" /* for strcpy */
15 #include "sys/types.h" /* for time_t */
16 #include "time.h" /* for struct tm */
18 #ifndef MAX_STRING_LENGTH
19 #define MAX_STRING_LENGTH 1024
20 #endif /* !defined MAX_STRING_LENGTH */
24 #endif /* !defined TRUE */
28 #endif /* !defined FALSE */
31 #define EXIT_SUCCESS 0
32 #endif /* !defined EXIT_SUCCESS */
35 #define EXIT_FAILURE 1
36 #endif /* !defined EXIT_FAILURE */
40 #endif /* !defined SECSPERMIN */
43 #define MINSPERHOUR 60
44 #endif /* !defined MINSPERHOUR */
47 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
48 #endif /* !defined SECSPERHOUR */
51 #define HOURSPERDAY 24
52 #endif /* !defined HOURSPERDAY */
55 #define EPOCH_YEAR 1970
56 #endif /* !defined EPOCH_YEAR */
59 #define TM_YEAR_BASE 1900
60 #endif /* !defined TM_YEAR_BASE */
63 #define DAYSPERNYEAR 365
64 #endif /* !defined DAYSPERNYEAR */
67 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
68 #endif /* !defined isleap */
73 #endif /* defined lint */
76 #endif /* defined __GNUC__ */
77 #endif /* !defined GNUC_or_lint */
81 #define INITIALIZE(x) ((x) = 0)
82 #endif /* defined GNUC_or_lint */
85 #endif /* !defined GNUC_or_lint */
86 #endif /* !defined INITIALIZE */
88 extern char ** environ
;
93 extern char * tzname
[2];
98 #endif /* defined USG */
100 static char * abbr();
102 static time_t hunt();
104 static char * progname
;
114 register char * cutoff
;
115 register int cutyear
;
116 register long cuttime
;
127 while ((c
= getopt(argc
, argv
, "c:v")) == 'c' || c
== 'v')
130 else cutoff
= optarg
;
132 (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)) {
133 (void) fprintf(stderr
,
134 "%s: usage is %s [ -v ] [ -c cutoff ] zonename ...\n",
136 (void) exit(EXIT_FAILURE
);
138 if (cutoff
!= NULL
) {
141 cutyear
= atoi(cutoff
);
143 for (y
= EPOCH_YEAR
; y
< cutyear
; ++y
)
144 cuttime
+= DAYSPERNYEAR
+ isleap(y
);
145 cuttime
*= SECSPERHOUR
* HOURSPERDAY
;
149 for (i
= optind
; i
< argc
; ++i
)
150 if (strlen(argv
[i
]) > longest
)
151 longest
= strlen(argv
[i
]);
152 for (hibit
= 1; (hibit
<< 1) != 0; hibit
<<= 1)
155 register int from
, to
;
157 for (i
= 0; environ
[i
] != NULL
; ++i
)
159 fakeenv
= (char **) malloc((i
+ 2) * sizeof *fakeenv
);
160 if (fakeenv
== NULL
||
161 (fakeenv
[0] = (char *) malloc(longest
+ 4)) == NULL
) {
162 (void) perror(progname
);
163 (void) exit(EXIT_FAILURE
);
166 (void) strcpy(fakeenv
[to
++], "TZ=");
167 for (from
= 0; environ
[from
] != NULL
; ++from
)
168 if (strncmp(environ
[from
], "TZ=", 3) != 0)
169 fakeenv
[to
++] = environ
[from
];
173 for (i
= optind
; i
< argc
; ++i
) {
174 static char buf
[MAX_STRING_LENGTH
];
176 (void) strcpy(&fakeenv
[0][3], argv
[i
]);
177 show(argv
[i
], now
, FALSE
);
181 ** Get lowest value of t.
184 if (t
> 0) /* time_t is unsigned */
186 show(argv
[i
], t
, TRUE
);
187 t
+= SECSPERHOUR
* HOURSPERDAY
;
188 show(argv
[i
], t
, TRUE
);
190 (void) strncpy(buf
, abbr(&tm
), (sizeof buf
) - 1);
192 if (cutoff
!= NULL
&& t
>= cuttime
)
194 newt
= t
+ SECSPERHOUR
* 12;
195 if (cutoff
!= NULL
&& newt
>= cuttime
)
199 newtm
= *localtime(&newt
);
200 if (delta(&newtm
, &tm
) != (newt
- t
) ||
201 newtm
.tm_isdst
!= tm
.tm_isdst
||
202 strcmp(abbr(&newtm
), buf
) != 0) {
203 newt
= hunt(argv
[i
], t
, newt
);
204 newtm
= *localtime(&newt
);
205 (void) strncpy(buf
, abbr(&newtm
),
212 ** Get highest value of t.
215 if (t
< 0) /* time_t is signed */
217 t
-= SECSPERHOUR
* HOURSPERDAY
;
218 show(argv
[i
], t
, TRUE
);
219 t
+= SECSPERHOUR
* HOURSPERDAY
;
220 show(argv
[i
], t
, TRUE
);
222 if (fflush(stdout
) || ferror(stdout
)) {
223 (void) fprintf(stderr
, "%s: Error writing standard output ",
225 (void) perror("standard output");
226 (void) exit(EXIT_FAILURE
);
230 /* gcc -Wall pacifier */
244 static char loab
[MAX_STRING_LENGTH
];
246 lotm
= *localtime(&lot
);
247 (void) strncpy(loab
, abbr(&lotm
), (sizeof loab
) - 1);
248 while ((hit
- lot
) >= 2) {
249 t
= lot
/ 2 + hit
/ 2;
255 if (delta(&tm
, &lotm
) == (t
- lot
) &&
256 tm
.tm_isdst
== lotm
.tm_isdst
&&
257 strcmp(abbr(&tm
), loab
) == 0) {
262 show(name
, lot
, TRUE
);
263 show(name
, hit
, TRUE
);
268 ** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta.
279 if (newp
->tm_year
< oldp
->tm_year
)
280 return -delta(oldp
, newp
);
282 for (tmy
= oldp
->tm_year
; tmy
< newp
->tm_year
; ++tmy
)
283 result
+= DAYSPERNYEAR
+ isleap(tmy
+ TM_YEAR_BASE
);
284 result
+= newp
->tm_yday
- oldp
->tm_yday
;
285 result
*= HOURSPERDAY
;
286 result
+= newp
->tm_hour
- oldp
->tm_hour
;
287 result
*= MINSPERHOUR
;
288 result
+= newp
->tm_min
- oldp
->tm_min
;
289 result
*= SECSPERMIN
;
290 result
+= newp
->tm_sec
- oldp
->tm_sec
;
294 extern struct tm
* localtime();
304 (void) printf("%-*s ", longest
, zone
);
306 (void) printf("%.24s GMT = ", asctime(gmtime(&t
)));
308 (void) printf("%.24s", asctime(tmp
));
309 if (*abbr(tmp
) != '\0')
310 (void) printf(" %s", abbr(tmp
));
312 (void) printf(" isdst=%d", tmp
->tm_isdst
);
314 (void) printf(" gmtoff=%ld", tmp
->TM_GMTOFF
);
315 #endif /* defined TM_GMTOFF */
324 register char * result
;
327 if (tmp
->tm_isdst
!= 0 && tmp
->tm_isdst
!= 1)
329 result
= tzname
[tmp
->tm_isdst
];
330 return (result
== NULL
) ? &nada
: result
;