3 static char elsieid[] = "@(#)zdump.c 7.20";
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, perror */
14 #include "string.h" /* for strcpy */
15 #include "sys/types.h" /* for time_t */
16 #include "time.h" /* for struct tm */
17 #include "stdlib.h" /* for exit, malloc, atoi */
19 #ifndef MAX_STRING_LENGTH
20 #define MAX_STRING_LENGTH 1024
21 #endif /* !defined MAX_STRING_LENGTH */
25 #endif /* !defined TRUE */
29 #endif /* !defined FALSE */
32 #define EXIT_SUCCESS 0
33 #endif /* !defined EXIT_SUCCESS */
36 #define EXIT_FAILURE 1
37 #endif /* !defined EXIT_FAILURE */
41 #endif /* !defined SECSPERMIN */
44 #define MINSPERHOUR 60
45 #endif /* !defined MINSPERHOUR */
48 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
49 #endif /* !defined SECSPERHOUR */
52 #define HOURSPERDAY 24
53 #endif /* !defined HOURSPERDAY */
56 #define EPOCH_YEAR 1970
57 #endif /* !defined EPOCH_YEAR */
60 #define TM_YEAR_BASE 1900
61 #endif /* !defined TM_YEAR_BASE */
64 #define DAYSPERNYEAR 365
65 #endif /* !defined DAYSPERNYEAR */
68 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
69 #endif /* !defined isleap */
74 #endif /* defined lint */
78 #endif /* defined __GNUC__ */
79 #endif /* !defined lint */
80 #endif /* !defined GNUC_or_lint */
84 #define INITIALIZE(x) ((x) = 0)
85 #endif /* defined GNUC_or_lint */
88 #endif /* !defined GNUC_or_lint */
89 #endif /* !defined INITIALIZE */
91 extern char ** environ;
96 extern char * tzname[2];
100 static time_t hunt();
102 static char * progname;
113 register char * cutoff;
114 register int cutyear;
115 register long cuttime;
128 while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
131 else cutoff = optarg;
133 (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
134 (void) fprintf(stderr,
135 "%s: usage is %s [ -v ] [ -c cutoff ] zonename ...\n",
137 (void) exit(EXIT_FAILURE);
139 if (cutoff != NULL) {
142 cutyear = atoi(cutoff);
144 for (y = EPOCH_YEAR; y < cutyear; ++y)
145 cuttime += DAYSPERNYEAR + isleap(y);
146 cuttime *= SECSPERHOUR * HOURSPERDAY;
150 for (i = optind; i < argc; ++i)
151 if (strlen(argv[i]) > longest)
152 longest = strlen(argv[i]);
153 for (hibit = 1; (hibit << 1) != 0; hibit <<= 1)
159 for (i = 0; environ[i] != NULL; ++i)
161 fakeenv = (char **) malloc((size_t) ((i + 2) *
163 if (fakeenv == NULL ||
164 (fakeenv[0] = (char *) malloc((size_t) (longest +
166 (void) perror(progname);
167 (void) exit(EXIT_FAILURE);
170 (void) strcpy(fakeenv[to++], "TZ=");
171 for (from = 0; environ[from] != NULL; ++from)
172 if (strncmp(environ[from], "TZ=", 3) != 0)
173 fakeenv[to++] = environ[from];
177 for (i = optind; i < argc; ++i) {
178 static char buf[MAX_STRING_LENGTH];
180 (void) strcpy(&fakeenv[0][3], argv[i]);
181 show(argv[i], now, FALSE);
185 ** Get lowest value of t.
188 if (t > 0) /* time_t is unsigned */
190 show(argv[i], t, TRUE);
191 t += SECSPERHOUR * HOURSPERDAY;
192 show(argv[i], t, TRUE);
194 (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
196 if (cutoff != NULL && t >= cuttime)
198 newt = t + SECSPERHOUR * 12;
199 if (cutoff != NULL && newt >= cuttime)
203 newtm = *localtime(&newt);
204 if (delta(&newtm, &tm) != (newt - t) ||
205 newtm.tm_isdst != tm.tm_isdst ||
206 strcmp(abbr(&newtm), buf) != 0) {
207 newt = hunt(argv[i], t, newt);
208 newtm = *localtime(&newt);
209 (void) strncpy(buf, abbr(&newtm),
216 ** Get highest value of t.
219 if (t < 0) /* time_t is signed */
221 t -= SECSPERHOUR * HOURSPERDAY;
222 show(argv[i], t, TRUE);
223 t += SECSPERHOUR * HOURSPERDAY;
224 show(argv[i], t, TRUE);
226 if (fflush(stdout) || ferror(stdout)) {
227 (void) fprintf(stderr, "%s: Error writing standard output ",
229 (void) perror("standard output");
230 (void) exit(EXIT_FAILURE);
234 /* gcc -Wall pacifier */
248 static char loab[MAX_STRING_LENGTH];
250 lotm = *localtime(&lot);
251 (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
252 while ((hit - lot) >= 2) {
253 t = lot / 2 + hit / 2;
259 if (delta(&tm, &lotm) == (t - lot) &&
260 tm.tm_isdst == lotm.tm_isdst &&
261 strcmp(abbr(&tm), loab) == 0) {
266 show(name, lot, TRUE);
267 show(name, hit, TRUE);
272 ** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta.
283 if (newp->tm_year < oldp->tm_year)
284 return -delta(oldp, newp);
286 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
287 result += DAYSPERNYEAR + isleap(tmy + TM_YEAR_BASE);
288 result += newp->tm_yday - oldp->tm_yday;
289 result *= HOURSPERDAY;
290 result += newp->tm_hour - oldp->tm_hour;
291 result *= MINSPERHOUR;
292 result += newp->tm_min - oldp->tm_min;
293 result *= SECSPERMIN;
294 result += newp->tm_sec - oldp->tm_sec;
298 extern struct tm * localtime();
308 (void) printf("%-*s ", longest, zone);
310 (void) printf("%.24s GMT = ", asctime(gmtime(&t)));
312 (void) printf("%.24s", asctime(tmp));
313 if (*abbr(tmp) != '\0')
314 (void) printf(" %s", abbr(tmp));
316 (void) printf(" isdst=%d", tmp->tm_isdst);
318 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
319 #endif /* defined TM_GMTOFF */
328 register char * result;
331 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
333 result = tzname[tmp->tm_isdst];
334 return (result == NULL) ? &nada : result;