(TLS_MULTIPLE_THREADS_IN_TCB): Define.
[glibc/history.git] / timezone / zdump.c
blobcc27d758fbd2140137f8695b5de827ad9b616728
1 #ifndef lint
2 #ifndef NOID
3 static char elsieid[] = "@(#)zdump.c 7.29";
4 #endif /* !defined NOID */
5 #endif /* !defined lint */
7 /*
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 */
23 #ifndef TRUE
24 #define TRUE 1
25 #endif /* !defined TRUE */
27 #ifndef FALSE
28 #define FALSE 0
29 #endif /* !defined FALSE */
31 #ifndef EXIT_SUCCESS
32 #define EXIT_SUCCESS 0
33 #endif /* !defined EXIT_SUCCESS */
35 #ifndef EXIT_FAILURE
36 #define EXIT_FAILURE 1
37 #endif /* !defined EXIT_FAILURE */
39 #ifndef SECSPERMIN
40 #define SECSPERMIN 60
41 #endif /* !defined SECSPERMIN */
43 #ifndef MINSPERHOUR
44 #define MINSPERHOUR 60
45 #endif /* !defined MINSPERHOUR */
47 #ifndef SECSPERHOUR
48 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
49 #endif /* !defined SECSPERHOUR */
51 #ifndef HOURSPERDAY
52 #define HOURSPERDAY 24
53 #endif /* !defined HOURSPERDAY */
55 #ifndef EPOCH_YEAR
56 #define EPOCH_YEAR 1970
57 #endif /* !defined EPOCH_YEAR */
59 #ifndef TM_YEAR_BASE
60 #define TM_YEAR_BASE 1900
61 #endif /* !defined TM_YEAR_BASE */
63 #ifndef DAYSPERNYEAR
64 #define DAYSPERNYEAR 365
65 #endif /* !defined DAYSPERNYEAR */
67 #ifndef isleap
68 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
69 #endif /* !defined isleap */
71 #if HAVE_GETTEXT - 0
72 #include "locale.h" /* for setlocale */
73 #include "libintl.h"
74 #endif /* HAVE_GETTEXT - 0 */
76 #ifndef GNUC_or_lint
77 #ifdef lint
78 #define GNUC_or_lint
79 #endif /* defined lint */
80 #ifndef lint
81 #ifdef __GNUC__
82 #define GNUC_or_lint
83 #endif /* defined __GNUC__ */
84 #endif /* !defined lint */
85 #endif /* !defined GNUC_or_lint */
87 #ifndef INITIALIZE
88 #ifdef GNUC_or_lint
89 #define INITIALIZE(x) ((x) = 0)
90 #endif /* defined GNUC_or_lint */
91 #ifndef GNUC_or_lint
92 #define INITIALIZE(x)
93 #endif /* !defined GNUC_or_lint */
94 #endif /* !defined INITIALIZE */
97 ** For the benefit of GNU folk...
98 ** `_(MSGID)' uses the current locale's message library string for MSGID.
99 ** The default is to use gettext if available, and use MSGID otherwise.
102 #ifndef _
103 #if HAVE_GETTEXT - 0
104 #define _(msgid) gettext(msgid)
105 #else /* !(HAVE_GETTEXT - 0) */
106 #define _(msgid) msgid
107 #endif /* !(HAVE_GETTEXT - 0) */
108 #endif /* !defined _ */
110 #ifndef TZ_DOMAIN
111 #define TZ_DOMAIN "tz"
112 #endif /* !defined TZ_DOMAIN */
114 #ifndef P
115 #ifdef __STDC__
116 #define P(x) x
117 #endif /* defined __STDC__ */
118 #ifndef __STDC__
119 #define P(x) ()
120 #endif /* !defined __STDC__ */
121 #endif /* !defined P */
123 extern char ** environ;
124 extern int getopt P((int argc, char * const argv[],
125 const char * options));
126 extern char * optarg;
127 extern int optind;
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 char * progname;
135 static void show P((char * zone, time_t t, int v));
138 main(argc, argv)
139 int argc;
140 char * argv[];
142 register int i;
143 register int c;
144 register int vflag;
145 register char * cutoff;
146 register int cutyear;
147 register long cuttime;
148 char ** fakeenv;
149 time_t now;
150 time_t t;
151 time_t newt;
152 time_t hibit;
153 struct tm tm;
154 struct tm newtm;
156 INITIALIZE(cuttime);
157 #if HAVE_GETTEXT - 0
158 (void) setlocale(LC_CTYPE, "");
159 (void) setlocale(LC_MESSAGES, "");
160 #ifdef TZ_DOMAINDIR
161 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
162 #endif /* defined(TEXTDOMAINDIR) */
163 (void) textdomain(TZ_DOMAIN);
164 #endif /* HAVE_GETTEXT - 0 */
165 progname = argv[0];
166 vflag = 0;
167 cutoff = NULL;
168 while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
169 if (c == 'v')
170 vflag = 1;
171 else cutoff = optarg;
172 if ((c != EOF && c != -1) ||
173 (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
174 (void) fprintf(stderr,
175 _("%s: usage is %s [ -v ] [ -c cutoff ] zonename ...\n"),
176 argv[0], argv[0]);
177 (void) exit(EXIT_FAILURE);
179 if (cutoff != NULL) {
180 int y;
182 cutyear = atoi(cutoff);
183 cuttime = 0;
184 for (y = EPOCH_YEAR; y < cutyear; ++y)
185 cuttime += DAYSPERNYEAR + isleap(y);
186 cuttime *= SECSPERHOUR * HOURSPERDAY;
188 (void) time(&now);
189 longest = 0;
190 for (i = optind; i < argc; ++i)
191 if (strlen(argv[i]) > longest)
192 longest = strlen(argv[i]);
193 for (hibit = 1; (hibit << 1) != 0; hibit <<= 1)
194 continue;
196 register int from;
197 register int to;
199 for (i = 0; environ[i] != NULL; ++i)
200 continue;
201 fakeenv = (char **) malloc((size_t) ((i + 2) *
202 sizeof *fakeenv));
203 if (fakeenv == NULL ||
204 (fakeenv[0] = (char *) malloc(longest + 4)) == NULL) {
205 (void) perror(progname);
206 (void) exit(EXIT_FAILURE);
208 to = 0;
209 (void) strcpy(fakeenv[to++], "TZ=");
210 for (from = 0; environ[from] != NULL; ++from)
211 if (strncmp(environ[from], "TZ=", 3) != 0)
212 fakeenv[to++] = environ[from];
213 fakeenv[to] = NULL;
214 environ = fakeenv;
216 for (i = optind; i < argc; ++i) {
217 static char buf[MAX_STRING_LENGTH];
219 (void) strcpy(&fakeenv[0][3], argv[i]);
220 if (!vflag) {
221 show(argv[i], now, FALSE);
222 continue;
225 ** Get lowest value of t.
227 t = hibit;
228 if (t > 0) /* time_t is unsigned */
229 t = 0;
230 show(argv[i], t, TRUE);
231 t += SECSPERHOUR * HOURSPERDAY;
232 show(argv[i], t, TRUE);
233 tm = *localtime(&t);
234 (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
235 for ( ; ; ) {
236 if (cutoff != NULL && t >= cuttime)
237 break;
238 newt = t + SECSPERHOUR * 12;
239 if (cutoff != NULL && newt >= cuttime)
240 break;
241 if (newt <= t)
242 break;
243 newtm = *localtime(&newt);
244 if (delta(&newtm, &tm) != (newt - t) ||
245 newtm.tm_isdst != tm.tm_isdst ||
246 strcmp(abbr(&newtm), buf) != 0) {
247 newt = hunt(argv[i], t, newt);
248 newtm = *localtime(&newt);
249 (void) strncpy(buf, abbr(&newtm),
250 (sizeof buf) - 1);
252 t = newt;
253 tm = newtm;
256 ** Get highest value of t.
258 t = ~((time_t) 0);
259 if (t < 0) /* time_t is signed */
260 t &= ~hibit;
261 t -= SECSPERHOUR * HOURSPERDAY;
262 show(argv[i], t, TRUE);
263 t += SECSPERHOUR * HOURSPERDAY;
264 show(argv[i], t, TRUE);
266 if (fflush(stdout) || ferror(stdout)) {
267 (void) fprintf(stderr, "%s: ", argv[0]);
268 (void) perror(_("Error writing standard output"));
269 (void) exit(EXIT_FAILURE);
271 exit(EXIT_SUCCESS);
273 /* gcc -Wall pacifier */
274 for ( ; ; )
275 continue;
278 static time_t
279 hunt(name, lot, hit)
280 char * name;
281 time_t lot;
282 time_t hit;
284 time_t t;
285 struct tm lotm;
286 struct tm tm;
287 static char loab[MAX_STRING_LENGTH];
289 lotm = *localtime(&lot);
290 (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
291 while ((hit - lot) >= 2) {
292 t = lot / 2 + hit / 2;
293 if (t <= lot)
294 ++t;
295 else if (t >= hit)
296 --t;
297 tm = *localtime(&t);
298 if (delta(&tm, &lotm) == (t - lot) &&
299 tm.tm_isdst == lotm.tm_isdst &&
300 strcmp(abbr(&tm), loab) == 0) {
301 lot = t;
302 lotm = tm;
303 } else hit = t;
305 show(name, lot, TRUE);
306 show(name, hit, TRUE);
307 return hit;
311 ** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta.
314 static long
315 delta(newp, oldp)
316 struct tm * newp;
317 struct tm * oldp;
319 long result;
320 int tmy;
322 if (newp->tm_year < oldp->tm_year)
323 return -delta(oldp, newp);
324 result = 0;
325 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
326 result += DAYSPERNYEAR + isleap(tmy + TM_YEAR_BASE);
327 result += newp->tm_yday - oldp->tm_yday;
328 result *= HOURSPERDAY;
329 result += newp->tm_hour - oldp->tm_hour;
330 result *= MINSPERHOUR;
331 result += newp->tm_min - oldp->tm_min;
332 result *= SECSPERMIN;
333 result += newp->tm_sec - oldp->tm_sec;
334 return result;
337 static void
338 show(zone, t, v)
339 char * zone;
340 time_t t;
341 int v;
343 struct tm * tmp;
345 (void) printf("%-*s ", (int) longest, zone);
346 if (v)
347 (void) printf("%.24s UTC = ", asctime(gmtime(&t)));
348 tmp = localtime(&t);
349 (void) printf("%.24s", asctime(tmp));
350 if (*abbr(tmp) != '\0')
351 (void) printf(" %s", abbr(tmp));
352 if (v) {
353 (void) printf(" isdst=%d", tmp->tm_isdst);
354 #ifdef TM_GMTOFF
355 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
356 #endif /* defined TM_GMTOFF */
358 (void) printf("\n");
361 static char *
362 abbr(tmp)
363 struct tm * tmp;
365 register char * result;
366 static char nada;
368 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
369 return &nada;
370 result = tzname[tmp->tm_isdst];
371 return (result == NULL) ? &nada : result;