]>
Commit | Line | Data |
---|---|---|
28f540f4 RM |
1 | #ifndef lint |
2 | #ifndef NOID | |
1ef32c3d | 3 | static char elsieid[] = "@(#)zic.c 7.81"; |
28f540f4 RM |
4 | #endif /* !defined NOID */ |
5 | #endif /* !defined lint */ | |
6 | ||
7 | #include "private.h" | |
92777700 | 8 | #include "locale.h" |
28f540f4 | 9 | #include "tzfile.h" |
6c2f0507 RM |
10 | #ifdef unix |
11 | #include "sys/stat.h" /* for umask manifest constants */ | |
12 | #endif /* defined unix */ | |
28f540f4 | 13 | |
f2e235b9 RM |
14 | /* |
15 | ** On some ancient hosts, predicates like `isspace(C)' are defined | |
16 | ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard, | |
17 | ** which says they are defined only if C == ((unsigned char) C) || C == EOF. | |
18 | ** Neither the C Standard nor Posix require that `isascii' exist. | |
19 | ** For portability, we check both ancient and modern requirements. | |
20 | ** If isascii is not defined, the isascii check succeeds trivially. | |
21 | */ | |
22 | #include "ctype.h" | |
23 | #ifndef isascii | |
24 | #define isascii(x) 1 | |
25 | #endif | |
26 | ||
28f540f4 RM |
27 | struct rule { |
28 | const char * r_filename; | |
29 | int r_linenum; | |
30 | const char * r_name; | |
31 | ||
32 | int r_loyear; /* for example, 1986 */ | |
33 | int r_hiyear; /* for example, 1986 */ | |
34 | const char * r_yrtype; | |
35 | ||
36 | int r_month; /* 0..11 */ | |
37 | ||
38 | int r_dycode; /* see below */ | |
39 | int r_dayofmonth; | |
40 | int r_wday; | |
41 | ||
42 | long r_tod; /* time from midnight */ | |
43 | int r_todisstd; /* above is standard time if TRUE */ | |
44 | /* or wall clock time if FALSE */ | |
6c2f0507 | 45 | int r_todisgmt; /* above is GMT if TRUE */ |
28f540f4 RM |
46 | /* or local time if FALSE */ |
47 | long r_stdoff; /* offset from standard time */ | |
48 | const char * r_abbrvar; /* variable part of abbreviation */ | |
49 | ||
50 | int r_todo; /* a rule to do (used in outzone) */ | |
51 | time_t r_temp; /* used in outzone */ | |
52 | }; | |
53 | ||
54 | /* | |
55 | ** r_dycode r_dayofmonth r_wday | |
56 | */ | |
57 | ||
58 | #define DC_DOM 0 /* 1..31 */ /* unused */ | |
59 | #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */ | |
60 | #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */ | |
61 | ||
62 | struct zone { | |
63 | const char * z_filename; | |
64 | int z_linenum; | |
65 | ||
66 | const char * z_name; | |
67 | long z_gmtoff; | |
68 | const char * z_rule; | |
69 | const char * z_format; | |
70 | ||
71 | long z_stdoff; | |
72 | ||
73 | struct rule * z_rules; | |
74 | int z_nrules; | |
75 | ||
76 | struct rule z_untilrule; | |
77 | time_t z_untiltime; | |
78 | }; | |
79 | ||
6c2f0507 RM |
80 | extern int getopt P((int argc, char * const argv[], |
81 | const char * options)); | |
28f540f4 RM |
82 | extern char * icatalloc P((char * old, const char * new)); |
83 | extern char * icpyalloc P((const char * string)); | |
84 | extern void ifree P((char * p)); | |
85 | extern char * imalloc P((int n)); | |
86 | extern void * irealloc P((void * old, int n)); | |
87 | extern int link P((const char * fromname, const char * toname)); | |
88 | extern char * optarg; | |
89 | extern int optind; | |
90 | extern char * scheck P((const char * string, const char * format)); | |
91 | ||
92 | static void addtt P((time_t starttime, int type)); | |
93 | static int addtype P((long gmtoff, const char * abbr, int isdst, | |
6c2f0507 | 94 | int ttisstd, int ttisgmt)); |
28f540f4 RM |
95 | static void leapadd P((time_t t, int positive, int rolling, int count)); |
96 | static void adjleap P((void)); | |
97 | static void associate P((void)); | |
98 | static int ciequal P((const char * ap, const char * bp)); | |
99 | static void convert P((long val, char * buf)); | |
100 | static void dolink P((const char * fromfile, const char * tofile)); | |
6c2f0507 RM |
101 | static void doabbr P((char * abbr, const char * format, |
102 | const char * letters, int isdst)); | |
28f540f4 RM |
103 | static void eat P((const char * name, int num)); |
104 | static void eats P((const char * name, int num, | |
105 | const char * rname, int rnum)); | |
106 | static long eitol P((int i)); | |
107 | static void error P((const char * message)); | |
108 | static char ** getfields P((char * buf)); | |
109 | static long gethms P((const char * string, const char * errstrng, | |
110 | int signable)); | |
111 | static void infile P((const char * filename)); | |
112 | static void inleap P((char ** fields, int nfields)); | |
113 | static void inlink P((char ** fields, int nfields)); | |
114 | static void inrule P((char ** fields, int nfields)); | |
115 | static int inzcont P((char ** fields, int nfields)); | |
116 | static int inzone P((char ** fields, int nfields)); | |
117 | static int inzsub P((char ** fields, int nfields, int iscont)); | |
118 | static int itsabbr P((const char * abbr, const char * word)); | |
119 | static int itsdir P((const char * name)); | |
120 | static int lowerit P((int c)); | |
121 | static char * memcheck P((char * tocheck)); | |
122 | static int mkdirs P((char * filename)); | |
123 | static void newabbr P((const char * abbr)); | |
124 | static long oadd P((long t1, long t2)); | |
125 | static void outzone P((const struct zone * zp, int ntzones)); | |
126 | static void puttzcode P((long code, FILE * fp)); | |
6c2f0507 | 127 | static int rcomp P((const void * leftp, const void * rightp)); |
28f540f4 RM |
128 | static time_t rpytime P((const struct rule * rp, int wantedy)); |
129 | static void rulesub P((struct rule * rp, | |
130 | const char * loyearp, const char * hiyearp, | |
131 | const char * typep, const char * monthp, | |
132 | const char * dayp, const char * timep)); | |
133 | static void setboundaries P((void)); | |
134 | static time_t tadd P((time_t t1, long t2)); | |
135 | static void usage P((void)); | |
136 | static void writezone P((const char * name)); | |
137 | static int yearistype P((int year, const char * type)); | |
138 | ||
569c558c | 139 | #if !(HAVE_STRERROR - 0) |
92777700 | 140 | static char * strerror P((int)); |
569c558c | 141 | #endif /* !(HAVE_STRERROR - 0) */ |
92777700 | 142 | |
28f540f4 RM |
143 | static int charcnt; |
144 | static int errors; | |
145 | static const char * filename; | |
146 | static int leapcnt; | |
147 | static int linenum; | |
28f540f4 RM |
148 | static time_t max_time; |
149 | static int max_year; | |
28f540f4 RM |
150 | static time_t min_time; |
151 | static int min_year; | |
152 | static int noise; | |
153 | static const char * rfilename; | |
154 | static int rlinenum; | |
155 | static const char * progname; | |
156 | static int timecnt; | |
157 | static int typecnt; | |
28f540f4 RM |
158 | |
159 | /* | |
160 | ** Line codes. | |
161 | */ | |
162 | ||
163 | #define LC_RULE 0 | |
164 | #define LC_ZONE 1 | |
165 | #define LC_LINK 2 | |
166 | #define LC_LEAP 3 | |
167 | ||
168 | /* | |
169 | ** Which fields are which on a Zone line. | |
170 | */ | |
171 | ||
172 | #define ZF_NAME 1 | |
173 | #define ZF_GMTOFF 2 | |
174 | #define ZF_RULE 3 | |
175 | #define ZF_FORMAT 4 | |
176 | #define ZF_TILYEAR 5 | |
177 | #define ZF_TILMONTH 6 | |
178 | #define ZF_TILDAY 7 | |
179 | #define ZF_TILTIME 8 | |
180 | #define ZONE_MINFIELDS 5 | |
181 | #define ZONE_MAXFIELDS 9 | |
182 | ||
183 | /* | |
184 | ** Which fields are which on a Zone continuation line. | |
185 | */ | |
186 | ||
187 | #define ZFC_GMTOFF 0 | |
188 | #define ZFC_RULE 1 | |
189 | #define ZFC_FORMAT 2 | |
190 | #define ZFC_TILYEAR 3 | |
191 | #define ZFC_TILMONTH 4 | |
192 | #define ZFC_TILDAY 5 | |
193 | #define ZFC_TILTIME 6 | |
194 | #define ZONEC_MINFIELDS 3 | |
195 | #define ZONEC_MAXFIELDS 7 | |
196 | ||
197 | /* | |
198 | ** Which files are which on a Rule line. | |
199 | */ | |
200 | ||
201 | #define RF_NAME 1 | |
202 | #define RF_LOYEAR 2 | |
203 | #define RF_HIYEAR 3 | |
204 | #define RF_COMMAND 4 | |
205 | #define RF_MONTH 5 | |
206 | #define RF_DAY 6 | |
207 | #define RF_TOD 7 | |
208 | #define RF_STDOFF 8 | |
209 | #define RF_ABBRVAR 9 | |
210 | #define RULE_FIELDS 10 | |
211 | ||
212 | /* | |
213 | ** Which fields are which on a Link line. | |
214 | */ | |
215 | ||
216 | #define LF_FROM 1 | |
217 | #define LF_TO 2 | |
218 | #define LINK_FIELDS 3 | |
219 | ||
220 | /* | |
221 | ** Which fields are which on a Leap line. | |
222 | */ | |
223 | ||
224 | #define LP_YEAR 1 | |
225 | #define LP_MONTH 2 | |
226 | #define LP_DAY 3 | |
227 | #define LP_TIME 4 | |
228 | #define LP_CORR 5 | |
229 | #define LP_ROLL 6 | |
230 | #define LEAP_FIELDS 7 | |
231 | ||
232 | /* | |
233 | ** Year synonyms. | |
234 | */ | |
235 | ||
236 | #define YR_MINIMUM 0 | |
237 | #define YR_MAXIMUM 1 | |
238 | #define YR_ONLY 2 | |
239 | ||
240 | static struct rule * rules; | |
241 | static int nrules; /* number of rules */ | |
242 | ||
243 | static struct zone * zones; | |
244 | static int nzones; /* number of zones */ | |
245 | ||
246 | struct link { | |
247 | const char * l_filename; | |
248 | int l_linenum; | |
249 | const char * l_from; | |
250 | const char * l_to; | |
251 | }; | |
252 | ||
253 | static struct link * links; | |
254 | static int nlinks; | |
255 | ||
256 | struct lookup { | |
257 | const char * l_word; | |
258 | const int l_value; | |
259 | }; | |
260 | ||
261 | static struct lookup const * byword P((const char * string, | |
262 | const struct lookup * lp)); | |
263 | ||
264 | static struct lookup const line_codes[] = { | |
265 | { "Rule", LC_RULE }, | |
266 | { "Zone", LC_ZONE }, | |
267 | { "Link", LC_LINK }, | |
268 | { "Leap", LC_LEAP }, | |
269 | { NULL, 0} | |
270 | }; | |
271 | ||
272 | static struct lookup const mon_names[] = { | |
273 | { "January", TM_JANUARY }, | |
274 | { "February", TM_FEBRUARY }, | |
275 | { "March", TM_MARCH }, | |
276 | { "April", TM_APRIL }, | |
277 | { "May", TM_MAY }, | |
278 | { "June", TM_JUNE }, | |
279 | { "July", TM_JULY }, | |
280 | { "August", TM_AUGUST }, | |
281 | { "September", TM_SEPTEMBER }, | |
282 | { "October", TM_OCTOBER }, | |
283 | { "November", TM_NOVEMBER }, | |
284 | { "December", TM_DECEMBER }, | |
285 | { NULL, 0 } | |
286 | }; | |
287 | ||
288 | static struct lookup const wday_names[] = { | |
289 | { "Sunday", TM_SUNDAY }, | |
290 | { "Monday", TM_MONDAY }, | |
291 | { "Tuesday", TM_TUESDAY }, | |
292 | { "Wednesday", TM_WEDNESDAY }, | |
293 | { "Thursday", TM_THURSDAY }, | |
294 | { "Friday", TM_FRIDAY }, | |
295 | { "Saturday", TM_SATURDAY }, | |
296 | { NULL, 0 } | |
297 | }; | |
298 | ||
299 | static struct lookup const lasts[] = { | |
300 | { "last-Sunday", TM_SUNDAY }, | |
301 | { "last-Monday", TM_MONDAY }, | |
302 | { "last-Tuesday", TM_TUESDAY }, | |
303 | { "last-Wednesday", TM_WEDNESDAY }, | |
304 | { "last-Thursday", TM_THURSDAY }, | |
305 | { "last-Friday", TM_FRIDAY }, | |
306 | { "last-Saturday", TM_SATURDAY }, | |
307 | { NULL, 0 } | |
308 | }; | |
309 | ||
310 | static struct lookup const begin_years[] = { | |
311 | { "minimum", YR_MINIMUM }, | |
312 | { "maximum", YR_MAXIMUM }, | |
313 | { NULL, 0 } | |
314 | }; | |
315 | ||
316 | static struct lookup const end_years[] = { | |
317 | { "minimum", YR_MINIMUM }, | |
318 | { "maximum", YR_MAXIMUM }, | |
319 | { "only", YR_ONLY }, | |
320 | { NULL, 0 } | |
321 | }; | |
322 | ||
323 | static struct lookup const leap_types[] = { | |
324 | { "Rolling", TRUE }, | |
325 | { "Stationary", FALSE }, | |
326 | { NULL, 0 } | |
327 | }; | |
328 | ||
329 | static const int len_months[2][MONSPERYEAR] = { | |
330 | { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, | |
331 | { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } | |
332 | }; | |
333 | ||
334 | static const int len_years[2] = { | |
335 | DAYSPERNYEAR, DAYSPERLYEAR | |
336 | }; | |
337 | ||
dfe1754a RM |
338 | static struct attype { |
339 | time_t at; | |
340 | unsigned char type; | |
341 | } attypes[TZ_MAX_TIMES]; | |
28f540f4 RM |
342 | static long gmtoffs[TZ_MAX_TYPES]; |
343 | static char isdsts[TZ_MAX_TYPES]; | |
344 | static unsigned char abbrinds[TZ_MAX_TYPES]; | |
345 | static char ttisstds[TZ_MAX_TYPES]; | |
6c2f0507 | 346 | static char ttisgmts[TZ_MAX_TYPES]; |
28f540f4 RM |
347 | static char chars[TZ_MAX_CHARS]; |
348 | static time_t trans[TZ_MAX_LEAPS]; | |
349 | static long corr[TZ_MAX_LEAPS]; | |
350 | static char roll[TZ_MAX_LEAPS]; | |
351 | ||
352 | /* | |
353 | ** Memory allocation. | |
354 | */ | |
355 | ||
356 | static char * | |
357 | memcheck(ptr) | |
358 | char * const ptr; | |
359 | { | |
360 | if (ptr == NULL) { | |
92777700 | 361 | const char *e = strerror(errno); |
6d52618b | 362 | |
92777700 RM |
363 | (void) fprintf(stderr, _("%s: Memory exhausted: %s\n"), |
364 | progname, e); | |
28f540f4 RM |
365 | (void) exit(EXIT_FAILURE); |
366 | } | |
367 | return ptr; | |
368 | } | |
369 | ||
370 | #define emalloc(size) memcheck(imalloc(size)) | |
371 | #define erealloc(ptr, size) memcheck(irealloc((ptr), (size))) | |
372 | #define ecpyalloc(ptr) memcheck(icpyalloc(ptr)) | |
373 | #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp))) | |
374 | ||
375 | /* | |
376 | ** Error handling. | |
377 | */ | |
378 | ||
569c558c | 379 | #if !(HAVE_STRERROR - 0) |
92777700 RM |
380 | static char * |
381 | strerror(errnum) | |
382 | int errnum; | |
383 | { | |
569c558c UD |
384 | extern char * sys_errlist[]; |
385 | extern int sys_nerr; | |
92777700 | 386 | |
569c558c UD |
387 | return (errnum > 0 && errnum <= sys_nerr) ? |
388 | sys_errlist[errnum] : "Unknown system error"; | |
92777700 | 389 | } |
569c558c | 390 | #endif /* !(HAVE_STRERROR - 0) */ |
92777700 | 391 | |
28f540f4 RM |
392 | static void |
393 | eats(name, num, rname, rnum) | |
394 | const char * const name; | |
395 | const int num; | |
396 | const char * const rname; | |
397 | const int rnum; | |
398 | { | |
399 | filename = name; | |
400 | linenum = num; | |
401 | rfilename = rname; | |
402 | rlinenum = rnum; | |
403 | } | |
404 | ||
405 | static void | |
406 | eat(name, num) | |
407 | const char * const name; | |
408 | const int num; | |
409 | { | |
410 | eats(name, num, (char *) NULL, -1); | |
411 | } | |
412 | ||
413 | static void | |
414 | error(string) | |
415 | const char * const string; | |
416 | { | |
417 | /* | |
418 | ** Match the format of "cc" to allow sh users to | |
6c2f0507 | 419 | ** zic ... 2>&1 | error -t "*" -v |
28f540f4 RM |
420 | ** on BSD systems. |
421 | */ | |
cbd3dceb | 422 | (void) fprintf(stderr, _("\"%s\", line %d: %s"), |
28f540f4 RM |
423 | filename, linenum, string); |
424 | if (rfilename != NULL) | |
cbd3dceb | 425 | (void) fprintf(stderr, _(" (rule from \"%s\", line %d)"), |
28f540f4 RM |
426 | rfilename, rlinenum); |
427 | (void) fprintf(stderr, "\n"); | |
428 | ++errors; | |
429 | } | |
430 | ||
dfe1754a RM |
431 | static void |
432 | warning(string) | |
433 | const char * const string; | |
434 | { | |
435 | char * cp; | |
436 | ||
437 | cp = ecpyalloc("warning: "); | |
438 | cp = ecatalloc(cp, string); | |
439 | error(string); | |
440 | ifree(cp); | |
441 | --errors; | |
442 | } | |
443 | ||
28f540f4 RM |
444 | static void |
445 | usage P((void)) | |
446 | { | |
dfe1754a | 447 | (void) fprintf(stderr, _("%s: usage is %s [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] [ -d directory ]\n\t[ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"), |
28f540f4 RM |
448 | progname, progname); |
449 | (void) exit(EXIT_FAILURE); | |
450 | } | |
451 | ||
452 | static const char * psxrules; | |
453 | static const char * lcltime; | |
454 | static const char * directory; | |
455 | static const char * leapsec; | |
456 | static const char * yitcommand; | |
457 | static int sflag = FALSE; | |
458 | ||
459 | int | |
460 | main(argc, argv) | |
461 | int argc; | |
462 | char * argv[]; | |
463 | { | |
6c2f0507 RM |
464 | register int i; |
465 | register int j; | |
28f540f4 RM |
466 | register int c; |
467 | ||
468 | #ifdef unix | |
6c2f0507 | 469 | (void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH)); |
28f540f4 | 470 | #endif /* defined unix */ |
92777700 RM |
471 | #if HAVE_GETTEXT - 0 |
472 | (void) setlocale(LC_MESSAGES, ""); | |
473 | #ifdef TZ_DOMAINDIR | |
474 | (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR); | |
475 | #endif /* defined TEXTDOMAINDIR */ | |
476 | (void) textdomain(TZ_DOMAIN); | |
477 | #endif /* HAVE_GETTEXT - 0 */ | |
28f540f4 | 478 | progname = argv[0]; |
1ef32c3d | 479 | while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF && c != -1) |
28f540f4 RM |
480 | switch (c) { |
481 | default: | |
482 | usage(); | |
483 | case 'd': | |
484 | if (directory == NULL) | |
485 | directory = optarg; | |
486 | else { | |
487 | (void) fprintf(stderr, | |
cbd3dceb | 488 | _("%s: More than one -d option specified\n"), |
28f540f4 RM |
489 | progname); |
490 | (void) exit(EXIT_FAILURE); | |
491 | } | |
492 | break; | |
493 | case 'l': | |
494 | if (lcltime == NULL) | |
495 | lcltime = optarg; | |
496 | else { | |
497 | (void) fprintf(stderr, | |
cbd3dceb | 498 | _("%s: More than one -l option specified\n"), |
28f540f4 RM |
499 | progname); |
500 | (void) exit(EXIT_FAILURE); | |
501 | } | |
502 | break; | |
503 | case 'p': | |
504 | if (psxrules == NULL) | |
505 | psxrules = optarg; | |
506 | else { | |
507 | (void) fprintf(stderr, | |
cbd3dceb | 508 | _("%s: More than one -p option specified\n"), |
28f540f4 RM |
509 | progname); |
510 | (void) exit(EXIT_FAILURE); | |
511 | } | |
512 | break; | |
513 | case 'y': | |
514 | if (yitcommand == NULL) | |
515 | yitcommand = optarg; | |
516 | else { | |
517 | (void) fprintf(stderr, | |
cbd3dceb | 518 | _("%s: More than one -y option specified\n"), |
28f540f4 RM |
519 | progname); |
520 | (void) exit(EXIT_FAILURE); | |
521 | } | |
522 | break; | |
523 | case 'L': | |
524 | if (leapsec == NULL) | |
525 | leapsec = optarg; | |
526 | else { | |
527 | (void) fprintf(stderr, | |
cbd3dceb | 528 | _("%s: More than one -L option specified\n"), |
28f540f4 RM |
529 | progname); |
530 | (void) exit(EXIT_FAILURE); | |
531 | } | |
532 | break; | |
533 | case 'v': | |
534 | noise = TRUE; | |
535 | break; | |
536 | case 's': | |
537 | sflag = TRUE; | |
538 | break; | |
539 | } | |
540 | if (optind == argc - 1 && strcmp(argv[optind], "=") == 0) | |
541 | usage(); /* usage message by request */ | |
542 | if (directory == NULL) | |
543 | directory = TZDIR; | |
544 | if (yitcommand == NULL) | |
545 | yitcommand = "yearistype"; | |
546 | ||
547 | setboundaries(); | |
548 | ||
549 | if (optind < argc && leapsec != NULL) { | |
550 | infile(leapsec); | |
551 | adjleap(); | |
552 | } | |
553 | ||
554 | for (i = optind; i < argc; ++i) | |
555 | infile(argv[i]); | |
556 | if (errors) | |
557 | (void) exit(EXIT_FAILURE); | |
558 | associate(); | |
559 | for (i = 0; i < nzones; i = j) { | |
560 | /* | |
561 | ** Find the next non-continuation zone entry. | |
562 | */ | |
563 | for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j) | |
564 | continue; | |
565 | outzone(&zones[i], j - i); | |
566 | } | |
567 | /* | |
568 | ** Make links. | |
569 | */ | |
570 | for (i = 0; i < nlinks; ++i) | |
571 | dolink(links[i].l_from, links[i].l_to); | |
572 | if (lcltime != NULL) | |
573 | dolink(lcltime, TZDEFAULT); | |
574 | if (psxrules != NULL) | |
575 | dolink(psxrules, TZDEFRULES); | |
576 | return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE; | |
577 | } | |
578 | ||
579 | static void | |
580 | dolink(fromfile, tofile) | |
581 | const char * const fromfile; | |
582 | const char * const tofile; | |
583 | { | |
584 | register char * fromname; | |
585 | register char * toname; | |
586 | ||
587 | if (fromfile[0] == '/') | |
588 | fromname = ecpyalloc(fromfile); | |
589 | else { | |
590 | fromname = ecpyalloc(directory); | |
591 | fromname = ecatalloc(fromname, "/"); | |
592 | fromname = ecatalloc(fromname, fromfile); | |
593 | } | |
594 | if (tofile[0] == '/') | |
595 | toname = ecpyalloc(tofile); | |
596 | else { | |
597 | toname = ecpyalloc(directory); | |
598 | toname = ecatalloc(toname, "/"); | |
599 | toname = ecatalloc(toname, tofile); | |
600 | } | |
601 | /* | |
602 | ** We get to be careful here since | |
603 | ** there's a fair chance of root running us. | |
604 | */ | |
605 | if (!itsdir(toname)) | |
606 | (void) remove(toname); | |
607 | if (link(fromname, toname) != 0) { | |
608 | if (mkdirs(toname) != 0) | |
609 | (void) exit(EXIT_FAILURE); | |
610 | if (link(fromname, toname) != 0) { | |
92777700 | 611 | const char *e = strerror(errno); |
6d52618b | 612 | |
92777700 RM |
613 | (void) fprintf(stderr, |
614 | _("%s: Can't link from %s to %s: %s\n"), | |
615 | progname, fromname, toname, e); | |
28f540f4 RM |
616 | (void) exit(EXIT_FAILURE); |
617 | } | |
618 | } | |
619 | ifree(fromname); | |
620 | ifree(toname); | |
621 | } | |
622 | ||
f2e235b9 RM |
623 | #ifndef INT_MAX |
624 | #define INT_MAX ((int) (((unsigned)~0)>>1)) | |
625 | #endif /* !defined INT_MAX */ | |
626 | ||
627 | #ifndef INT_MIN | |
628 | #define INT_MIN ((int) ~(((unsigned)~0)>>1)) | |
629 | #endif /* !defined INT_MIN */ | |
630 | ||
631 | /* | |
632 | ** The tz file format currently allows at most 32-bit quantities. | |
633 | ** This restriction should be removed before signed 32-bit values | |
634 | ** wrap around in 2038, but unfortunately this will require a | |
635 | ** change to the tz file format. | |
636 | */ | |
637 | ||
638 | #define MAX_BITS_IN_FILE 32 | |
dfe1754a | 639 | #define TIME_T_BITS_IN_FILE ((TYPE_BIT(time_t) < MAX_BITS_IN_FILE) ? TYPE_BIT(time_t) : MAX_BITS_IN_FILE) |
f2e235b9 | 640 | |
28f540f4 RM |
641 | static void |
642 | setboundaries P((void)) | |
643 | { | |
f2e235b9 RM |
644 | if (TYPE_SIGNED(time_t)) { |
645 | min_time = ~ (time_t) 0; | |
646 | min_time <<= TIME_T_BITS_IN_FILE - 1; | |
647 | max_time = ~ (time_t) 0 - min_time; | |
28f540f4 RM |
648 | if (sflag) |
649 | min_time = 0; | |
f2e235b9 RM |
650 | } else { |
651 | min_time = 0; | |
652 | max_time = 2 - sflag; | |
653 | max_time <<= TIME_T_BITS_IN_FILE - 1; | |
654 | --max_time; | |
28f540f4 RM |
655 | } |
656 | min_year = TM_YEAR_BASE + gmtime(&min_time)->tm_year; | |
657 | max_year = TM_YEAR_BASE + gmtime(&max_time)->tm_year; | |
28f540f4 RM |
658 | } |
659 | ||
660 | static int | |
661 | itsdir(name) | |
662 | const char * const name; | |
663 | { | |
664 | register char * myname; | |
665 | register int accres; | |
666 | ||
667 | myname = ecpyalloc(name); | |
668 | myname = ecatalloc(myname, "/."); | |
6c2f0507 | 669 | accres = access(myname, F_OK); |
28f540f4 RM |
670 | ifree(myname); |
671 | return accres == 0; | |
672 | } | |
673 | ||
674 | /* | |
675 | ** Associate sets of rules with zones. | |
676 | */ | |
677 | ||
678 | /* | |
679 | ** Sort by rule name. | |
680 | */ | |
681 | ||
682 | static int | |
683 | rcomp(cp1, cp2) | |
6c2f0507 RM |
684 | const void * cp1; |
685 | const void * cp2; | |
28f540f4 | 686 | { |
6c2f0507 RM |
687 | return strcmp(((const struct rule *) cp1)->r_name, |
688 | ((const struct rule *) cp2)->r_name); | |
28f540f4 RM |
689 | } |
690 | ||
691 | static void | |
692 | associate P((void)) | |
693 | { | |
694 | register struct zone * zp; | |
695 | register struct rule * rp; | |
696 | register int base, out; | |
dfe1754a | 697 | register int i, j; |
28f540f4 | 698 | |
dfe1754a | 699 | if (nrules != 0) { |
6c2f0507 RM |
700 | (void) qsort((void *) rules, (size_t) nrules, |
701 | (size_t) sizeof *rules, rcomp); | |
dfe1754a RM |
702 | for (i = 0; i < nrules - 1; ++i) { |
703 | if (strcmp(rules[i].r_name, | |
704 | rules[i + 1].r_name) != 0) | |
705 | continue; | |
706 | if (strcmp(rules[i].r_filename, | |
707 | rules[i + 1].r_filename) == 0) | |
708 | continue; | |
709 | eat(rules[i].r_filename, rules[i].r_linenum); | |
710 | warning(_("same rule name in multiple files")); | |
711 | eat(rules[i + 1].r_filename, rules[i + 1].r_linenum); | |
712 | warning(_("same rule name in multiple files")); | |
713 | for (j = i + 2; j < nrules; ++j) { | |
714 | if (strcmp(rules[i].r_name, | |
715 | rules[j].r_name) != 0) | |
716 | break; | |
717 | if (strcmp(rules[i].r_filename, | |
718 | rules[j].r_filename) == 0) | |
719 | continue; | |
720 | if (strcmp(rules[i + 1].r_filename, | |
721 | rules[j].r_filename) == 0) | |
722 | continue; | |
723 | break; | |
724 | } | |
725 | i = j - 1; | |
726 | } | |
727 | } | |
28f540f4 RM |
728 | for (i = 0; i < nzones; ++i) { |
729 | zp = &zones[i]; | |
730 | zp->z_rules = NULL; | |
731 | zp->z_nrules = 0; | |
732 | } | |
733 | for (base = 0; base < nrules; base = out) { | |
734 | rp = &rules[base]; | |
735 | for (out = base + 1; out < nrules; ++out) | |
736 | if (strcmp(rp->r_name, rules[out].r_name) != 0) | |
737 | break; | |
738 | for (i = 0; i < nzones; ++i) { | |
739 | zp = &zones[i]; | |
740 | if (strcmp(zp->z_rule, rp->r_name) != 0) | |
741 | continue; | |
742 | zp->z_rules = rp; | |
743 | zp->z_nrules = out - base; | |
744 | } | |
745 | } | |
746 | for (i = 0; i < nzones; ++i) { | |
747 | zp = &zones[i]; | |
748 | if (zp->z_nrules == 0) { | |
749 | /* | |
750 | ** Maybe we have a local standard time offset. | |
751 | */ | |
752 | eat(zp->z_filename, zp->z_linenum); | |
92777700 RM |
753 | zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"), |
754 | TRUE); | |
28f540f4 RM |
755 | /* |
756 | ** Note, though, that if there's no rule, | |
757 | ** a '%s' in the format is a bad thing. | |
758 | */ | |
759 | if (strchr(zp->z_format, '%') != 0) | |
cbd3dceb | 760 | error(_("%s in ruleless zone")); |
28f540f4 RM |
761 | } |
762 | } | |
763 | if (errors) | |
764 | (void) exit(EXIT_FAILURE); | |
765 | } | |
766 | ||
767 | static void | |
768 | infile(name) | |
769 | const char * name; | |
770 | { | |
771 | register FILE * fp; | |
772 | register char ** fields; | |
773 | register char * cp; | |
774 | register const struct lookup * lp; | |
775 | register int nfields; | |
776 | register int wantcont; | |
777 | register int num; | |
778 | char buf[BUFSIZ]; | |
779 | ||
780 | if (strcmp(name, "-") == 0) { | |
cbd3dceb | 781 | name = _("standard input"); |
28f540f4 RM |
782 | fp = stdin; |
783 | } else if ((fp = fopen(name, "r")) == NULL) { | |
92777700 | 784 | const char *e = strerror(errno); |
6d52618b | 785 | |
92777700 RM |
786 | (void) fprintf(stderr, _("%s: Can't open %s: %s\n"), |
787 | progname, name, e); | |
28f540f4 RM |
788 | (void) exit(EXIT_FAILURE); |
789 | } | |
790 | wantcont = FALSE; | |
791 | for (num = 1; ; ++num) { | |
792 | eat(name, num); | |
793 | if (fgets(buf, (int) sizeof buf, fp) != buf) | |
794 | break; | |
795 | cp = strchr(buf, '\n'); | |
796 | if (cp == NULL) { | |
cbd3dceb | 797 | error(_("line too long")); |
28f540f4 RM |
798 | (void) exit(EXIT_FAILURE); |
799 | } | |
800 | *cp = '\0'; | |
801 | fields = getfields(buf); | |
802 | nfields = 0; | |
803 | while (fields[nfields] != NULL) { | |
804 | static char nada; | |
805 | ||
f2e235b9 | 806 | if (strcmp(fields[nfields], "-") == 0) |
28f540f4 RM |
807 | fields[nfields] = &nada; |
808 | ++nfields; | |
809 | } | |
810 | if (nfields == 0) { | |
811 | /* nothing to do */ | |
812 | } else if (wantcont) { | |
813 | wantcont = inzcont(fields, nfields); | |
814 | } else { | |
815 | lp = byword(fields[0], line_codes); | |
816 | if (lp == NULL) | |
cbd3dceb | 817 | error(_("input line of unknown type")); |
28f540f4 RM |
818 | else switch ((int) (lp->l_value)) { |
819 | case LC_RULE: | |
820 | inrule(fields, nfields); | |
821 | wantcont = FALSE; | |
822 | break; | |
823 | case LC_ZONE: | |
824 | wantcont = inzone(fields, nfields); | |
825 | break; | |
826 | case LC_LINK: | |
827 | inlink(fields, nfields); | |
828 | wantcont = FALSE; | |
829 | break; | |
830 | case LC_LEAP: | |
831 | if (name != leapsec) | |
832 | (void) fprintf(stderr, | |
cbd3dceb | 833 | _("%s: Leap line in non leap seconds file %s\n"), |
28f540f4 RM |
834 | progname, name); |
835 | else inleap(fields, nfields); | |
836 | wantcont = FALSE; | |
837 | break; | |
838 | default: /* "cannot happen" */ | |
839 | (void) fprintf(stderr, | |
cbd3dceb | 840 | _("%s: panic: Invalid l_value %d\n"), |
28f540f4 RM |
841 | progname, lp->l_value); |
842 | (void) exit(EXIT_FAILURE); | |
843 | } | |
844 | } | |
845 | ifree((char *) fields); | |
846 | } | |
847 | if (ferror(fp)) { | |
92777700 RM |
848 | (void) fprintf(stderr, _("%s: Error reading %s\n"), |
849 | progname, filename); | |
28f540f4 RM |
850 | (void) exit(EXIT_FAILURE); |
851 | } | |
852 | if (fp != stdin && fclose(fp)) { | |
92777700 | 853 | const char *e = strerror(errno); |
6d52618b | 854 | |
92777700 RM |
855 | (void) fprintf(stderr, _("%s: Error closing %s: %s\n"), |
856 | progname, filename, e); | |
28f540f4 RM |
857 | (void) exit(EXIT_FAILURE); |
858 | } | |
859 | if (wantcont) | |
cbd3dceb | 860 | error(_("expected continuation line not found")); |
28f540f4 RM |
861 | } |
862 | ||
863 | /* | |
864 | ** Convert a string of one of the forms | |
6c2f0507 | 865 | ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss |
28f540f4 RM |
866 | ** into a number of seconds. |
867 | ** A null string maps to zero. | |
868 | ** Call error with errstring and return zero on errors. | |
869 | */ | |
870 | ||
871 | static long | |
872 | gethms(string, errstring, signable) | |
873 | const char * string; | |
874 | const char * const errstring; | |
875 | const int signable; | |
876 | { | |
877 | int hh, mm, ss, sign; | |
878 | ||
879 | if (string == NULL || *string == '\0') | |
880 | return 0; | |
881 | if (!signable) | |
882 | sign = 1; | |
883 | else if (*string == '-') { | |
884 | sign = -1; | |
885 | ++string; | |
886 | } else sign = 1; | |
887 | if (sscanf(string, scheck(string, "%d"), &hh) == 1) | |
888 | mm = ss = 0; | |
889 | else if (sscanf(string, scheck(string, "%d:%d"), &hh, &mm) == 2) | |
890 | ss = 0; | |
891 | else if (sscanf(string, scheck(string, "%d:%d:%d"), | |
892 | &hh, &mm, &ss) != 3) { | |
893 | error(errstring); | |
894 | return 0; | |
895 | } | |
896 | if (hh < 0 || hh >= HOURSPERDAY || | |
897 | mm < 0 || mm >= MINSPERHOUR || | |
898 | ss < 0 || ss > SECSPERMIN) { | |
899 | error(errstring); | |
900 | return 0; | |
901 | } | |
902 | return eitol(sign) * | |
903 | (eitol(hh * MINSPERHOUR + mm) * | |
904 | eitol(SECSPERMIN) + eitol(ss)); | |
905 | } | |
906 | ||
907 | static void | |
908 | inrule(fields, nfields) | |
909 | register char ** const fields; | |
910 | const int nfields; | |
911 | { | |
912 | static struct rule r; | |
913 | ||
914 | if (nfields != RULE_FIELDS) { | |
cbd3dceb | 915 | error(_("wrong number of fields on Rule line")); |
28f540f4 RM |
916 | return; |
917 | } | |
918 | if (*fields[RF_NAME] == '\0') { | |
cbd3dceb | 919 | error(_("nameless rule")); |
28f540f4 RM |
920 | return; |
921 | } | |
922 | r.r_filename = filename; | |
923 | r.r_linenum = linenum; | |
cbd3dceb | 924 | r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE); |
28f540f4 RM |
925 | rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND], |
926 | fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]); | |
927 | r.r_name = ecpyalloc(fields[RF_NAME]); | |
928 | r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]); | |
929 | rules = (struct rule *) (void *) erealloc((char *) rules, | |
930 | (int) ((nrules + 1) * sizeof *rules)); | |
931 | rules[nrules++] = r; | |
932 | } | |
933 | ||
934 | static int | |
935 | inzone(fields, nfields) | |
936 | register char ** const fields; | |
937 | const int nfields; | |
938 | { | |
939 | register int i; | |
940 | static char * buf; | |
941 | ||
942 | if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) { | |
cbd3dceb | 943 | error(_("wrong number of fields on Zone line")); |
28f540f4 RM |
944 | return FALSE; |
945 | } | |
946 | if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) { | |
947 | buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT))); | |
948 | (void) sprintf(buf, | |
cbd3dceb | 949 | _("\"Zone %s\" line and -l option are mutually exclusive"), |
28f540f4 RM |
950 | TZDEFAULT); |
951 | error(buf); | |
952 | return FALSE; | |
953 | } | |
954 | if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) { | |
955 | buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES))); | |
956 | (void) sprintf(buf, | |
cbd3dceb | 957 | _("\"Zone %s\" line and -p option are mutually exclusive"), |
28f540f4 RM |
958 | TZDEFRULES); |
959 | error(buf); | |
960 | return FALSE; | |
961 | } | |
962 | for (i = 0; i < nzones; ++i) | |
963 | if (zones[i].z_name != NULL && | |
964 | strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) { | |
965 | buf = erealloc(buf, (int) (132 + | |
966 | strlen(fields[ZF_NAME]) + | |
967 | strlen(zones[i].z_filename))); | |
968 | (void) sprintf(buf, | |
cbd3dceb | 969 | _("duplicate zone name %s (file \"%s\", line %d)"), |
28f540f4 RM |
970 | fields[ZF_NAME], |
971 | zones[i].z_filename, | |
972 | zones[i].z_linenum); | |
973 | error(buf); | |
974 | return FALSE; | |
975 | } | |
976 | return inzsub(fields, nfields, FALSE); | |
977 | } | |
978 | ||
979 | static int | |
980 | inzcont(fields, nfields) | |
981 | register char ** const fields; | |
982 | const int nfields; | |
983 | { | |
984 | if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) { | |
cbd3dceb | 985 | error(_("wrong number of fields on Zone continuation line")); |
28f540f4 RM |
986 | return FALSE; |
987 | } | |
988 | return inzsub(fields, nfields, TRUE); | |
989 | } | |
990 | ||
991 | static int | |
992 | inzsub(fields, nfields, iscont) | |
993 | register char ** const fields; | |
994 | const int nfields; | |
995 | const int iscont; | |
996 | { | |
997 | register char * cp; | |
998 | static struct zone z; | |
999 | register int i_gmtoff, i_rule, i_format; | |
1000 | register int i_untilyear, i_untilmonth; | |
1001 | register int i_untilday, i_untiltime; | |
1002 | register int hasuntil; | |
1003 | ||
1004 | if (iscont) { | |
1005 | i_gmtoff = ZFC_GMTOFF; | |
1006 | i_rule = ZFC_RULE; | |
1007 | i_format = ZFC_FORMAT; | |
1008 | i_untilyear = ZFC_TILYEAR; | |
1009 | i_untilmonth = ZFC_TILMONTH; | |
1010 | i_untilday = ZFC_TILDAY; | |
1011 | i_untiltime = ZFC_TILTIME; | |
1012 | z.z_name = NULL; | |
1013 | } else { | |
1014 | i_gmtoff = ZF_GMTOFF; | |
1015 | i_rule = ZF_RULE; | |
1016 | i_format = ZF_FORMAT; | |
1017 | i_untilyear = ZF_TILYEAR; | |
1018 | i_untilmonth = ZF_TILMONTH; | |
1019 | i_untilday = ZF_TILDAY; | |
1020 | i_untiltime = ZF_TILTIME; | |
1021 | z.z_name = ecpyalloc(fields[ZF_NAME]); | |
1022 | } | |
1023 | z.z_filename = filename; | |
1024 | z.z_linenum = linenum; | |
cbd3dceb | 1025 | z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid GMT offset"), TRUE); |
28f540f4 RM |
1026 | if ((cp = strchr(fields[i_format], '%')) != 0) { |
1027 | if (*++cp != 's' || strchr(cp, '%') != 0) { | |
cbd3dceb | 1028 | error(_("invalid abbreviation format")); |
28f540f4 RM |
1029 | return FALSE; |
1030 | } | |
1031 | } | |
1032 | z.z_rule = ecpyalloc(fields[i_rule]); | |
1033 | z.z_format = ecpyalloc(fields[i_format]); | |
1034 | hasuntil = nfields > i_untilyear; | |
1035 | if (hasuntil) { | |
1036 | z.z_untilrule.r_filename = filename; | |
1037 | z.z_untilrule.r_linenum = linenum; | |
1038 | rulesub(&z.z_untilrule, | |
1039 | fields[i_untilyear], | |
1040 | "only", | |
1041 | "", | |
1042 | (nfields > i_untilmonth) ? | |
1043 | fields[i_untilmonth] : "Jan", | |
1044 | (nfields > i_untilday) ? fields[i_untilday] : "1", | |
1045 | (nfields > i_untiltime) ? fields[i_untiltime] : "0"); | |
1046 | z.z_untiltime = rpytime(&z.z_untilrule, | |
1047 | z.z_untilrule.r_loyear); | |
1048 | if (iscont && nzones > 0 && | |
1049 | z.z_untiltime > min_time && | |
1050 | z.z_untiltime < max_time && | |
1051 | zones[nzones - 1].z_untiltime > min_time && | |
1052 | zones[nzones - 1].z_untiltime < max_time && | |
1053 | zones[nzones - 1].z_untiltime >= z.z_untiltime) { | |
dfe1754a | 1054 | error(_("Zone continuation line end time is not after end time of previous line")); |
28f540f4 RM |
1055 | return FALSE; |
1056 | } | |
1057 | } | |
1058 | zones = (struct zone *) (void *) erealloc((char *) zones, | |
1059 | (int) ((nzones + 1) * sizeof *zones)); | |
1060 | zones[nzones++] = z; | |
1061 | /* | |
1062 | ** If there was an UNTIL field on this line, | |
1063 | ** there's more information about the zone on the next line. | |
1064 | */ | |
1065 | return hasuntil; | |
1066 | } | |
1067 | ||
1068 | static void | |
1069 | inleap(fields, nfields) | |
1070 | register char ** const fields; | |
1071 | const int nfields; | |
1072 | { | |
1073 | register const char * cp; | |
1074 | register const struct lookup * lp; | |
1075 | register int i, j; | |
1076 | int year, month, day; | |
1077 | long dayoff, tod; | |
1078 | time_t t; | |
1079 | ||
1080 | if (nfields != LEAP_FIELDS) { | |
cbd3dceb | 1081 | error(_("wrong number of fields on Leap line")); |
28f540f4 RM |
1082 | return; |
1083 | } | |
1084 | dayoff = 0; | |
1085 | cp = fields[LP_YEAR]; | |
1086 | if (sscanf(cp, scheck(cp, "%d"), &year) != 1) { | |
1087 | /* | |
1088 | * Leapin' Lizards! | |
1089 | */ | |
cbd3dceb | 1090 | error(_("invalid leaping year")); |
28f540f4 RM |
1091 | return; |
1092 | } | |
1093 | j = EPOCH_YEAR; | |
1094 | while (j != year) { | |
1095 | if (year > j) { | |
1096 | i = len_years[isleap(j)]; | |
1097 | ++j; | |
1098 | } else { | |
1099 | --j; | |
1100 | i = -len_years[isleap(j)]; | |
1101 | } | |
1102 | dayoff = oadd(dayoff, eitol(i)); | |
1103 | } | |
1104 | if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) { | |
cbd3dceb | 1105 | error(_("invalid month name")); |
28f540f4 RM |
1106 | return; |
1107 | } | |
1108 | month = lp->l_value; | |
1109 | j = TM_JANUARY; | |
1110 | while (j != month) { | |
1111 | i = len_months[isleap(year)][j]; | |
1112 | dayoff = oadd(dayoff, eitol(i)); | |
1113 | ++j; | |
1114 | } | |
1115 | cp = fields[LP_DAY]; | |
1116 | if (sscanf(cp, scheck(cp, "%d"), &day) != 1 || | |
1117 | day <= 0 || day > len_months[isleap(year)][month]) { | |
cbd3dceb | 1118 | error(_("invalid day of month")); |
28f540f4 RM |
1119 | return; |
1120 | } | |
1121 | dayoff = oadd(dayoff, eitol(day - 1)); | |
f2e235b9 | 1122 | if (dayoff < 0 && !TYPE_SIGNED(time_t)) { |
92777700 | 1123 | error(_("time before zero")); |
28f540f4 RM |
1124 | return; |
1125 | } | |
1126 | t = (time_t) dayoff * SECSPERDAY; | |
1127 | /* | |
1128 | ** Cheap overflow check. | |
1129 | */ | |
1130 | if (t / SECSPERDAY != dayoff) { | |
cbd3dceb | 1131 | error(_("time overflow")); |
28f540f4 RM |
1132 | return; |
1133 | } | |
92777700 | 1134 | tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE); |
28f540f4 RM |
1135 | cp = fields[LP_CORR]; |
1136 | { | |
1137 | register int positive; | |
1138 | int count; | |
1139 | ||
1140 | if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */ | |
1141 | positive = FALSE; | |
1142 | count = 1; | |
1143 | } else if (strcmp(cp, "--") == 0) { | |
1144 | positive = FALSE; | |
1145 | count = 2; | |
1146 | } else if (strcmp(cp, "+") == 0) { | |
1147 | positive = TRUE; | |
1148 | count = 1; | |
1149 | } else if (strcmp(cp, "++") == 0) { | |
1150 | positive = TRUE; | |
1151 | count = 2; | |
1152 | } else { | |
cbd3dceb | 1153 | error(_("illegal CORRECTION field on Leap line")); |
28f540f4 RM |
1154 | return; |
1155 | } | |
1156 | if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) { | |
cbd3dceb | 1157 | error(_("illegal Rolling/Stationary field on Leap line")); |
28f540f4 RM |
1158 | return; |
1159 | } | |
1160 | leapadd(tadd(t, tod), positive, lp->l_value, count); | |
1161 | } | |
1162 | } | |
1163 | ||
1164 | static void | |
1165 | inlink(fields, nfields) | |
1166 | register char ** const fields; | |
1167 | const int nfields; | |
1168 | { | |
1169 | struct link l; | |
1170 | ||
1171 | if (nfields != LINK_FIELDS) { | |
cbd3dceb | 1172 | error(_("wrong number of fields on Link line")); |
28f540f4 RM |
1173 | return; |
1174 | } | |
1175 | if (*fields[LF_FROM] == '\0') { | |
cbd3dceb | 1176 | error(_("blank FROM field on Link line")); |
28f540f4 RM |
1177 | return; |
1178 | } | |
1179 | if (*fields[LF_TO] == '\0') { | |
cbd3dceb | 1180 | error(_("blank TO field on Link line")); |
28f540f4 RM |
1181 | return; |
1182 | } | |
1183 | l.l_filename = filename; | |
1184 | l.l_linenum = linenum; | |
1185 | l.l_from = ecpyalloc(fields[LF_FROM]); | |
1186 | l.l_to = ecpyalloc(fields[LF_TO]); | |
1187 | links = (struct link *) (void *) erealloc((char *) links, | |
1188 | (int) ((nlinks + 1) * sizeof *links)); | |
1189 | links[nlinks++] = l; | |
1190 | } | |
1191 | ||
1192 | static void | |
1193 | rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep) | |
1194 | register struct rule * const rp; | |
1195 | const char * const loyearp; | |
1196 | const char * const hiyearp; | |
1197 | const char * const typep; | |
1198 | const char * const monthp; | |
1199 | const char * const dayp; | |
1200 | const char * const timep; | |
1201 | { | |
1202 | register const struct lookup * lp; | |
1203 | register const char * cp; | |
1204 | register char * dp; | |
1205 | register char * ep; | |
1206 | ||
1207 | if ((lp = byword(monthp, mon_names)) == NULL) { | |
cbd3dceb | 1208 | error(_("invalid month name")); |
28f540f4 RM |
1209 | return; |
1210 | } | |
1211 | rp->r_month = lp->l_value; | |
1212 | rp->r_todisstd = FALSE; | |
6c2f0507 | 1213 | rp->r_todisgmt = FALSE; |
28f540f4 RM |
1214 | dp = ecpyalloc(timep); |
1215 | if (*dp != '\0') { | |
1216 | ep = dp + strlen(dp) - 1; | |
1217 | switch (lowerit(*ep)) { | |
1218 | case 's': /* Standard */ | |
1219 | rp->r_todisstd = TRUE; | |
6c2f0507 | 1220 | rp->r_todisgmt = FALSE; |
28f540f4 RM |
1221 | *ep = '\0'; |
1222 | break; | |
1223 | case 'w': /* Wall */ | |
1224 | rp->r_todisstd = FALSE; | |
6c2f0507 | 1225 | rp->r_todisgmt = FALSE; |
28f540f4 RM |
1226 | *ep = '\0'; |
1227 | case 'g': /* Greenwich */ | |
1228 | case 'u': /* Universal */ | |
1229 | case 'z': /* Zulu */ | |
1230 | rp->r_todisstd = TRUE; | |
6c2f0507 | 1231 | rp->r_todisgmt = TRUE; |
28f540f4 RM |
1232 | *ep = '\0'; |
1233 | break; | |
1234 | } | |
1235 | } | |
92777700 | 1236 | rp->r_tod = gethms(dp, _("invalid time of day"), FALSE); |
28f540f4 RM |
1237 | ifree(dp); |
1238 | /* | |
1239 | ** Year work. | |
1240 | */ | |
1241 | cp = loyearp; | |
6c2f0507 RM |
1242 | lp = byword(cp, begin_years); |
1243 | if (lp != NULL) switch ((int) lp->l_value) { | |
28f540f4 | 1244 | case YR_MINIMUM: |
f2e235b9 | 1245 | rp->r_loyear = INT_MIN; |
28f540f4 RM |
1246 | break; |
1247 | case YR_MAXIMUM: | |
f2e235b9 | 1248 | rp->r_loyear = INT_MAX; |
28f540f4 RM |
1249 | break; |
1250 | default: /* "cannot happen" */ | |
1251 | (void) fprintf(stderr, | |
cbd3dceb | 1252 | _("%s: panic: Invalid l_value %d\n"), |
28f540f4 RM |
1253 | progname, lp->l_value); |
1254 | (void) exit(EXIT_FAILURE); | |
1255 | } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) { | |
cbd3dceb | 1256 | error(_("invalid starting year")); |
28f540f4 RM |
1257 | return; |
1258 | } | |
1259 | cp = hiyearp; | |
1260 | if ((lp = byword(cp, end_years)) != NULL) switch ((int) lp->l_value) { | |
1261 | case YR_MINIMUM: | |
f2e235b9 | 1262 | rp->r_hiyear = INT_MIN; |
28f540f4 RM |
1263 | break; |
1264 | case YR_MAXIMUM: | |
f2e235b9 | 1265 | rp->r_hiyear = INT_MAX; |
28f540f4 RM |
1266 | break; |
1267 | case YR_ONLY: | |
1268 | rp->r_hiyear = rp->r_loyear; | |
1269 | break; | |
1270 | default: /* "cannot happen" */ | |
1271 | (void) fprintf(stderr, | |
cbd3dceb | 1272 | _("%s: panic: Invalid l_value %d\n"), |
28f540f4 RM |
1273 | progname, lp->l_value); |
1274 | (void) exit(EXIT_FAILURE); | |
1275 | } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) { | |
cbd3dceb | 1276 | error(_("invalid ending year")); |
28f540f4 RM |
1277 | return; |
1278 | } | |
1279 | if (rp->r_loyear > rp->r_hiyear) { | |
cbd3dceb | 1280 | error(_("starting year greater than ending year")); |
28f540f4 RM |
1281 | return; |
1282 | } | |
1283 | if (*typep == '\0') | |
1284 | rp->r_yrtype = NULL; | |
1285 | else { | |
1286 | if (rp->r_loyear == rp->r_hiyear) { | |
cbd3dceb | 1287 | error(_("typed single year")); |
28f540f4 RM |
1288 | return; |
1289 | } | |
1290 | rp->r_yrtype = ecpyalloc(typep); | |
1291 | } | |
1292 | /* | |
1293 | ** Day work. | |
1294 | ** Accept things such as: | |
1295 | ** 1 | |
1296 | ** last-Sunday | |
1297 | ** Sun<=20 | |
1298 | ** Sun>=7 | |
1299 | */ | |
1300 | dp = ecpyalloc(dayp); | |
1301 | if ((lp = byword(dp, lasts)) != NULL) { | |
1302 | rp->r_dycode = DC_DOWLEQ; | |
1303 | rp->r_wday = lp->l_value; | |
1304 | rp->r_dayofmonth = len_months[1][rp->r_month]; | |
1305 | } else { | |
1306 | if ((ep = strchr(dp, '<')) != 0) | |
1307 | rp->r_dycode = DC_DOWLEQ; | |
1308 | else if ((ep = strchr(dp, '>')) != 0) | |
1309 | rp->r_dycode = DC_DOWGEQ; | |
1310 | else { | |
1311 | ep = dp; | |
1312 | rp->r_dycode = DC_DOM; | |
1313 | } | |
1314 | if (rp->r_dycode != DC_DOM) { | |
1315 | *ep++ = 0; | |
1316 | if (*ep++ != '=') { | |
cbd3dceb | 1317 | error(_("invalid day of month")); |
28f540f4 RM |
1318 | ifree(dp); |
1319 | return; | |
1320 | } | |
1321 | if ((lp = byword(dp, wday_names)) == NULL) { | |
cbd3dceb | 1322 | error(_("invalid weekday name")); |
28f540f4 RM |
1323 | ifree(dp); |
1324 | return; | |
1325 | } | |
1326 | rp->r_wday = lp->l_value; | |
1327 | } | |
1328 | if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 || | |
1329 | rp->r_dayofmonth <= 0 || | |
1330 | (rp->r_dayofmonth > len_months[1][rp->r_month])) { | |
cbd3dceb | 1331 | error(_("invalid day of month")); |
28f540f4 RM |
1332 | ifree(dp); |
1333 | return; | |
1334 | } | |
1335 | } | |
1336 | ifree(dp); | |
1337 | } | |
1338 | ||
1339 | static void | |
1340 | convert(val, buf) | |
1341 | const long val; | |
1342 | char * const buf; | |
1343 | { | |
1344 | register int i; | |
1345 | register long shift; | |
1346 | ||
1347 | for (i = 0, shift = 24; i < 4; ++i, shift -= 8) | |
1348 | buf[i] = val >> shift; | |
1349 | } | |
1350 | ||
1351 | static void | |
1352 | puttzcode(val, fp) | |
1353 | const long val; | |
1354 | FILE * const fp; | |
1355 | { | |
1356 | char buf[4]; | |
1357 | ||
1358 | convert(val, buf); | |
6c2f0507 | 1359 | (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp); |
28f540f4 RM |
1360 | } |
1361 | ||
dfe1754a RM |
1362 | static int |
1363 | atcomp(avp, bvp) | |
1364 | void * avp; | |
1365 | void * bvp; | |
1366 | { | |
1367 | if (((struct attype *) avp)->at < ((struct attype *) bvp)->at) | |
1368 | return -1; | |
1369 | else if (((struct attype *) avp)->at > ((struct attype *) bvp)->at) | |
1370 | return 1; | |
1371 | else return 0; | |
1372 | } | |
1373 | ||
28f540f4 RM |
1374 | static void |
1375 | writezone(name) | |
1376 | const char * const name; | |
1377 | { | |
1378 | register FILE * fp; | |
1379 | register int i, j; | |
1380 | static char * fullname; | |
1381 | static struct tzhead tzh; | |
dfe1754a RM |
1382 | time_t ats[TZ_MAX_TIMES]; |
1383 | unsigned char types[TZ_MAX_TIMES]; | |
28f540f4 | 1384 | |
dfe1754a RM |
1385 | /* |
1386 | ** Sort. | |
1387 | */ | |
1388 | if (timecnt > 1) | |
1389 | (void) qsort((void *) attypes, (size_t) timecnt, | |
1390 | (size_t) sizeof *attypes, atcomp); | |
1391 | /* | |
1392 | ** Optimize. | |
1393 | */ | |
1394 | { | |
1395 | int fromi; | |
1396 | int toi; | |
1397 | ||
1398 | toi = 0; | |
1399 | fromi = 0; | |
1400 | if (isdsts[0] == 0) | |
1401 | while (attypes[fromi].type == 0) | |
1402 | ++fromi; /* handled by default rule */ | |
1403 | for ( ; fromi < timecnt; ++fromi) { | |
1404 | if (toi != 0 | |
1405 | && ((attypes[fromi].at | |
1406 | + gmtoffs[attypes[toi - 1].type]) | |
1407 | <= (attypes[toi - 1].at | |
1408 | + gmtoffs[toi == 1 ? 0 | |
1409 | : attypes[toi - 2].type]))) { | |
1410 | attypes[toi - 1].type = attypes[fromi].type; | |
1411 | continue; | |
1412 | } | |
1413 | if (toi == 0 || | |
1414 | attypes[toi - 1].type != attypes[fromi].type) | |
1415 | attypes[toi++] = attypes[fromi]; | |
1416 | } | |
1417 | timecnt = toi; | |
1418 | } | |
1419 | /* | |
1420 | ** Transfer. | |
1421 | */ | |
1422 | for (i = 0; i < timecnt; ++i) { | |
1423 | ats[i] = attypes[i].at; | |
1424 | types[i] = attypes[i].type; | |
1425 | } | |
28f540f4 RM |
1426 | fullname = erealloc(fullname, |
1427 | (int) (strlen(directory) + 1 + strlen(name) + 1)); | |
1428 | (void) sprintf(fullname, "%s/%s", directory, name); | |
6d52618b UD |
1429 | /* |
1430 | ** Remove old file, if any, to snap links. | |
1431 | */ | |
1432 | if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT) { | |
1433 | const char *e = strerror(errno); | |
1434 | ||
1435 | (void) fprintf(stderr, _("%s: Can't remove %s: %s\n"), | |
1436 | progname, fullname, e); | |
1437 | (void) exit(EXIT_FAILURE); | |
1438 | } | |
28f540f4 RM |
1439 | if ((fp = fopen(fullname, "wb")) == NULL) { |
1440 | if (mkdirs(fullname) != 0) | |
1441 | (void) exit(EXIT_FAILURE); | |
1442 | if ((fp = fopen(fullname, "wb")) == NULL) { | |
92777700 | 1443 | const char *e = strerror(errno); |
6d52618b | 1444 | |
92777700 RM |
1445 | (void) fprintf(stderr, _("%s: Can't create %s: %s\n"), |
1446 | progname, fullname, e); | |
28f540f4 RM |
1447 | (void) exit(EXIT_FAILURE); |
1448 | } | |
1449 | } | |
6c2f0507 | 1450 | convert(eitol(typecnt), tzh.tzh_ttisgmtcnt); |
28f540f4 RM |
1451 | convert(eitol(typecnt), tzh.tzh_ttisstdcnt); |
1452 | convert(eitol(leapcnt), tzh.tzh_leapcnt); | |
1453 | convert(eitol(timecnt), tzh.tzh_timecnt); | |
1454 | convert(eitol(typecnt), tzh.tzh_typecnt); | |
1455 | convert(eitol(charcnt), tzh.tzh_charcnt); | |
dfe1754a | 1456 | #define DO(field) (void) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp) |
6c2f0507 RM |
1457 | DO(tzh_reserved); |
1458 | DO(tzh_ttisgmtcnt); | |
1459 | DO(tzh_ttisstdcnt); | |
1460 | DO(tzh_leapcnt); | |
1461 | DO(tzh_timecnt); | |
1462 | DO(tzh_typecnt); | |
1463 | DO(tzh_charcnt); | |
1464 | #undef DO | |
28f540f4 RM |
1465 | for (i = 0; i < timecnt; ++i) { |
1466 | j = leapcnt; | |
1467 | while (--j >= 0) | |
1468 | if (ats[i] >= trans[j]) { | |
1469 | ats[i] = tadd(ats[i], corr[j]); | |
1470 | break; | |
1471 | } | |
1472 | puttzcode((long) ats[i], fp); | |
1473 | } | |
1474 | if (timecnt > 0) | |
6c2f0507 RM |
1475 | (void) fwrite((void *) types, (size_t) sizeof types[0], |
1476 | (size_t) timecnt, fp); | |
28f540f4 RM |
1477 | for (i = 0; i < typecnt; ++i) { |
1478 | puttzcode((long) gmtoffs[i], fp); | |
1479 | (void) putc(isdsts[i], fp); | |
1480 | (void) putc(abbrinds[i], fp); | |
1481 | } | |
1482 | if (charcnt != 0) | |
6c2f0507 RM |
1483 | (void) fwrite((void *) chars, (size_t) sizeof chars[0], |
1484 | (size_t) charcnt, fp); | |
28f540f4 RM |
1485 | for (i = 0; i < leapcnt; ++i) { |
1486 | if (roll[i]) { | |
1487 | if (timecnt == 0 || trans[i] < ats[0]) { | |
1488 | j = 0; | |
1489 | while (isdsts[j]) | |
1490 | if (++j >= typecnt) { | |
1491 | j = 0; | |
1492 | break; | |
1493 | } | |
1494 | } else { | |
1495 | j = 1; | |
1496 | while (j < timecnt && trans[i] >= ats[j]) | |
1497 | ++j; | |
1498 | j = types[j - 1]; | |
1499 | } | |
1500 | puttzcode((long) tadd(trans[i], -gmtoffs[j]), fp); | |
1501 | } else puttzcode((long) trans[i], fp); | |
1502 | puttzcode((long) corr[i], fp); | |
1503 | } | |
1504 | for (i = 0; i < typecnt; ++i) | |
1505 | (void) putc(ttisstds[i], fp); | |
6c2f0507 RM |
1506 | for (i = 0; i < typecnt; ++i) |
1507 | (void) putc(ttisgmts[i], fp); | |
28f540f4 | 1508 | if (ferror(fp) || fclose(fp)) { |
92777700 RM |
1509 | (void) fprintf(stderr, _("%s: Error writing %s\n"), |
1510 | progname, fullname); | |
28f540f4 RM |
1511 | (void) exit(EXIT_FAILURE); |
1512 | } | |
1513 | } | |
1514 | ||
6c2f0507 RM |
1515 | static void |
1516 | doabbr(abbr, format, letters, isdst) | |
1517 | char * const abbr; | |
1518 | const char * const format; | |
1519 | const char * const letters; | |
1520 | const int isdst; | |
1521 | { | |
1522 | if (strchr(format, '/') == NULL) { | |
1523 | if (letters == NULL) | |
1524 | (void) strcpy(abbr, format); | |
1525 | else (void) sprintf(abbr, format, letters); | |
1526 | } else if (isdst) | |
1527 | (void) strcpy(abbr, strchr(format, '/') + 1); | |
1528 | else { | |
1529 | (void) strcpy(abbr, format); | |
1530 | *strchr(abbr, '/') = '\0'; | |
1531 | } | |
1532 | } | |
1533 | ||
28f540f4 RM |
1534 | static void |
1535 | outzone(zpfirst, zonecount) | |
1536 | const struct zone * const zpfirst; | |
1537 | const int zonecount; | |
1538 | { | |
1539 | register const struct zone * zp; | |
1540 | register struct rule * rp; | |
1541 | register int i, j; | |
1542 | register int usestart, useuntil; | |
1543 | register time_t starttime, untiltime; | |
1544 | register long gmtoff; | |
1545 | register long stdoff; | |
1546 | register int year; | |
1547 | register long startoff; | |
28f540f4 | 1548 | register int startttisstd; |
6c2f0507 | 1549 | register int startttisgmt; |
28f540f4 RM |
1550 | register int type; |
1551 | char startbuf[BUFSIZ]; | |
1552 | ||
1553 | INITIALIZE(untiltime); | |
1554 | INITIALIZE(starttime); | |
28f540f4 RM |
1555 | /* |
1556 | ** Now. . .finally. . .generate some useful data! | |
1557 | */ | |
1558 | timecnt = 0; | |
1559 | typecnt = 0; | |
1560 | charcnt = 0; | |
1561 | /* | |
1562 | ** A guess that may well be corrected later. | |
1563 | */ | |
1564 | stdoff = 0; | |
1565 | /* | |
1566 | ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au) | |
1567 | ** for noting the need to unconditionally initialize startttisstd. | |
1568 | */ | |
1569 | startttisstd = FALSE; | |
6c2f0507 | 1570 | startttisgmt = FALSE; |
28f540f4 RM |
1571 | for (i = 0; i < zonecount; ++i) { |
1572 | zp = &zpfirst[i]; | |
1573 | usestart = i > 0 && (zp - 1)->z_untiltime > min_time; | |
1574 | useuntil = i < (zonecount - 1); | |
1575 | if (useuntil && zp->z_untiltime <= min_time) | |
1576 | continue; | |
1577 | gmtoff = zp->z_gmtoff; | |
1578 | eat(zp->z_filename, zp->z_linenum); | |
dfe1754a RM |
1579 | *startbuf = '\0'; |
1580 | startoff = zp->z_gmtoff; | |
28f540f4 RM |
1581 | if (zp->z_nrules == 0) { |
1582 | stdoff = zp->z_stdoff; | |
6c2f0507 RM |
1583 | doabbr(startbuf, zp->z_format, |
1584 | (char *) NULL, stdoff != 0); | |
28f540f4 | 1585 | type = addtype(oadd(zp->z_gmtoff, stdoff), |
6c2f0507 RM |
1586 | startbuf, stdoff != 0, startttisstd, |
1587 | startttisgmt); | |
dfe1754a | 1588 | if (usestart) { |
28f540f4 | 1589 | addtt(starttime, type); |
dfe1754a RM |
1590 | usestart = FALSE; |
1591 | } | |
28f540f4 RM |
1592 | else if (stdoff != 0) |
1593 | addtt(min_time, type); | |
1594 | } else for (year = min_year; year <= max_year; ++year) { | |
1595 | if (useuntil && year > zp->z_untilrule.r_hiyear) | |
1596 | break; | |
1597 | /* | |
1598 | ** Mark which rules to do in the current year. | |
1599 | ** For those to do, calculate rpytime(rp, year); | |
1600 | */ | |
1601 | for (j = 0; j < zp->z_nrules; ++j) { | |
1602 | rp = &zp->z_rules[j]; | |
1603 | eats(zp->z_filename, zp->z_linenum, | |
1604 | rp->r_filename, rp->r_linenum); | |
1605 | rp->r_todo = year >= rp->r_loyear && | |
1606 | year <= rp->r_hiyear && | |
1607 | yearistype(year, rp->r_yrtype); | |
1608 | if (rp->r_todo) | |
1609 | rp->r_temp = rpytime(rp, year); | |
1610 | } | |
1611 | for ( ; ; ) { | |
1612 | register int k; | |
1613 | register time_t jtime, ktime; | |
1614 | register long offset; | |
1615 | char buf[BUFSIZ]; | |
1616 | ||
1617 | INITIALIZE(ktime); | |
1618 | if (useuntil) { | |
1619 | /* | |
1620 | ** Turn untiltime into GMT | |
1621 | ** assuming the current gmtoff and | |
1622 | ** stdoff values. | |
1623 | */ | |
1624 | untiltime = zp->z_untiltime; | |
6c2f0507 | 1625 | if (!zp->z_untilrule.r_todisgmt) |
28f540f4 RM |
1626 | untiltime = tadd(untiltime, |
1627 | -gmtoff); | |
1628 | if (!zp->z_untilrule.r_todisstd) | |
1629 | untiltime = tadd(untiltime, | |
1630 | -stdoff); | |
1631 | } | |
1632 | /* | |
1633 | ** Find the rule (of those to do, if any) | |
1634 | ** that takes effect earliest in the year. | |
1635 | */ | |
1636 | k = -1; | |
28f540f4 RM |
1637 | for (j = 0; j < zp->z_nrules; ++j) { |
1638 | rp = &zp->z_rules[j]; | |
1639 | if (!rp->r_todo) | |
1640 | continue; | |
1641 | eats(zp->z_filename, zp->z_linenum, | |
1642 | rp->r_filename, rp->r_linenum); | |
6c2f0507 | 1643 | offset = rp->r_todisgmt ? 0 : gmtoff; |
28f540f4 RM |
1644 | if (!rp->r_todisstd) |
1645 | offset = oadd(offset, stdoff); | |
1646 | jtime = rp->r_temp; | |
1647 | if (jtime == min_time || | |
1648 | jtime == max_time) | |
1649 | continue; | |
1650 | jtime = tadd(jtime, -offset); | |
1651 | if (k < 0 || jtime < ktime) { | |
1652 | k = j; | |
1653 | ktime = jtime; | |
1654 | } | |
1655 | } | |
1656 | if (k < 0) | |
1657 | break; /* go on to next year */ | |
1658 | rp = &zp->z_rules[k]; | |
1659 | rp->r_todo = FALSE; | |
1660 | if (useuntil && ktime >= untiltime) | |
1661 | break; | |
dfe1754a RM |
1662 | stdoff = rp->r_stdoff; |
1663 | if (usestart && ktime == starttime) | |
1664 | usestart = FALSE; | |
28f540f4 | 1665 | if (usestart) { |
dfe1754a RM |
1666 | if (ktime < starttime) { |
1667 | startoff = oadd(zp->z_gmtoff, | |
1668 | stdoff); | |
1669 | doabbr(startbuf, zp->z_format, | |
1670 | rp->r_abbrvar, | |
1671 | rp->r_stdoff != 0); | |
1672 | continue; | |
1673 | } | |
1674 | if (*startbuf == '\0' && | |
1675 | startoff == oadd(zp->z_gmtoff, | |
1676 | stdoff)) { | |
1677 | doabbr(startbuf, zp->z_format, | |
1678 | rp->r_abbrvar, | |
1679 | rp->r_stdoff != 0); | |
28f540f4 | 1680 | } |
28f540f4 RM |
1681 | } |
1682 | eats(zp->z_filename, zp->z_linenum, | |
1683 | rp->r_filename, rp->r_linenum); | |
6c2f0507 RM |
1684 | doabbr(buf, zp->z_format, rp->r_abbrvar, |
1685 | rp->r_stdoff != 0); | |
28f540f4 RM |
1686 | offset = oadd(zp->z_gmtoff, rp->r_stdoff); |
1687 | type = addtype(offset, buf, rp->r_stdoff != 0, | |
6c2f0507 | 1688 | rp->r_todisstd, rp->r_todisgmt); |
28f540f4 | 1689 | addtt(ktime, type); |
28f540f4 RM |
1690 | } |
1691 | } | |
dfe1754a RM |
1692 | if (usestart) { |
1693 | if (*startbuf == '\0' && | |
1694 | zp->z_format != NULL && | |
1695 | strchr(zp->z_format, '%') == NULL && | |
1696 | strchr(zp->z_format, '/') == NULL) | |
1697 | (void) strcpy(startbuf, zp->z_format); | |
1698 | eat(zp->z_filename, zp->z_linenum); | |
1699 | if (*startbuf == '\0') | |
6bc31da0 | 1700 | error(_("can't determine time zone abbreviation to use just after until time")); |
dfe1754a RM |
1701 | else addtt(starttime, |
1702 | addtype(startoff, startbuf, | |
1703 | startoff != zp->z_gmtoff, | |
1704 | startttisstd, | |
1705 | startttisgmt)); | |
1706 | } | |
28f540f4 RM |
1707 | /* |
1708 | ** Now we may get to set starttime for the next zone line. | |
1709 | */ | |
1710 | if (useuntil) { | |
28f540f4 | 1711 | startttisstd = zp->z_untilrule.r_todisstd; |
6c2f0507 | 1712 | startttisgmt = zp->z_untilrule.r_todisgmt; |
dfe1754a | 1713 | starttime = zp->z_untiltime; |
28f540f4 RM |
1714 | if (!startttisstd) |
1715 | starttime = tadd(starttime, -stdoff); | |
dfe1754a RM |
1716 | if (!startttisgmt) |
1717 | starttime = tadd(starttime, -gmtoff); | |
28f540f4 RM |
1718 | } |
1719 | } | |
1720 | writezone(zpfirst->z_name); | |
1721 | } | |
1722 | ||
1723 | static void | |
1724 | addtt(starttime, type) | |
1725 | const time_t starttime; | |
1726 | const int type; | |
1727 | { | |
28f540f4 | 1728 | if (timecnt >= TZ_MAX_TIMES) { |
cbd3dceb | 1729 | error(_("too many transitions?!")); |
28f540f4 RM |
1730 | (void) exit(EXIT_FAILURE); |
1731 | } | |
dfe1754a RM |
1732 | attypes[timecnt].at = starttime; |
1733 | attypes[timecnt].type = type; | |
28f540f4 RM |
1734 | ++timecnt; |
1735 | } | |
1736 | ||
1737 | static int | |
6c2f0507 | 1738 | addtype(gmtoff, abbr, isdst, ttisstd, ttisgmt) |
28f540f4 RM |
1739 | const long gmtoff; |
1740 | const char * const abbr; | |
1741 | const int isdst; | |
1742 | const int ttisstd; | |
6c2f0507 | 1743 | const int ttisgmt; |
28f540f4 RM |
1744 | { |
1745 | register int i, j; | |
1746 | ||
dfe1754a RM |
1747 | if (isdst != TRUE && isdst != FALSE) { |
1748 | error(_("internal error - addtype called with bad isdst")); | |
1749 | (void) exit(EXIT_FAILURE); | |
1750 | } | |
1751 | if (ttisstd != TRUE && ttisstd != FALSE) { | |
1752 | error(_("internal error - addtype called with bad ttisstd")); | |
1753 | (void) exit(EXIT_FAILURE); | |
1754 | } | |
1755 | if (ttisgmt != TRUE && ttisgmt != FALSE) { | |
1756 | error(_("internal error - addtype called with bad ttisgmt")); | |
1757 | (void) exit(EXIT_FAILURE); | |
1758 | } | |
28f540f4 RM |
1759 | /* |
1760 | ** See if there's already an entry for this zone type. | |
1761 | ** If so, just return its index. | |
1762 | */ | |
1763 | for (i = 0; i < typecnt; ++i) { | |
1764 | if (gmtoff == gmtoffs[i] && isdst == isdsts[i] && | |
1765 | strcmp(abbr, &chars[abbrinds[i]]) == 0 && | |
6c2f0507 RM |
1766 | ttisstd == ttisstds[i] && |
1767 | ttisgmt == ttisgmts[i]) | |
28f540f4 RM |
1768 | return i; |
1769 | } | |
1770 | /* | |
1771 | ** There isn't one; add a new one, unless there are already too | |
1772 | ** many. | |
1773 | */ | |
1774 | if (typecnt >= TZ_MAX_TYPES) { | |
cbd3dceb | 1775 | error(_("too many local time types")); |
28f540f4 RM |
1776 | (void) exit(EXIT_FAILURE); |
1777 | } | |
1778 | gmtoffs[i] = gmtoff; | |
1779 | isdsts[i] = isdst; | |
1780 | ttisstds[i] = ttisstd; | |
6c2f0507 | 1781 | ttisgmts[i] = ttisgmt; |
28f540f4 RM |
1782 | |
1783 | for (j = 0; j < charcnt; ++j) | |
1784 | if (strcmp(&chars[j], abbr) == 0) | |
1785 | break; | |
1786 | if (j == charcnt) | |
1787 | newabbr(abbr); | |
1788 | abbrinds[i] = j; | |
1789 | ++typecnt; | |
1790 | return i; | |
1791 | } | |
1792 | ||
1793 | static void | |
1794 | leapadd(t, positive, rolling, count) | |
1795 | const time_t t; | |
1796 | const int positive; | |
1797 | const int rolling; | |
1798 | int count; | |
1799 | { | |
1800 | register int i, j; | |
1801 | ||
1802 | if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) { | |
cbd3dceb | 1803 | error(_("too many leap seconds")); |
28f540f4 RM |
1804 | (void) exit(EXIT_FAILURE); |
1805 | } | |
1806 | for (i = 0; i < leapcnt; ++i) | |
1807 | if (t <= trans[i]) { | |
1808 | if (t == trans[i]) { | |
cbd3dceb | 1809 | error(_("repeated leap second moment")); |
28f540f4 RM |
1810 | (void) exit(EXIT_FAILURE); |
1811 | } | |
1812 | break; | |
1813 | } | |
1814 | do { | |
1815 | for (j = leapcnt; j > i; --j) { | |
1816 | trans[j] = trans[j - 1]; | |
1817 | corr[j] = corr[j - 1]; | |
1818 | roll[j] = roll[j - 1]; | |
1819 | } | |
1820 | trans[i] = t; | |
1821 | corr[i] = positive ? 1L : eitol(-count); | |
1822 | roll[i] = rolling; | |
1823 | ++leapcnt; | |
1824 | } while (positive && --count != 0); | |
1825 | } | |
1826 | ||
1827 | static void | |
1828 | adjleap P((void)) | |
1829 | { | |
1830 | register int i; | |
1831 | register long last = 0; | |
1832 | ||
1833 | /* | |
1834 | ** propagate leap seconds forward | |
1835 | */ | |
1836 | for (i = 0; i < leapcnt; ++i) { | |
1837 | trans[i] = tadd(trans[i], last); | |
1838 | last = corr[i] += last; | |
1839 | } | |
1840 | } | |
1841 | ||
1842 | static int | |
1843 | yearistype(year, type) | |
1844 | const int year; | |
1845 | const char * const type; | |
1846 | { | |
1847 | static char * buf; | |
1848 | int result; | |
1849 | ||
1850 | if (type == NULL || *type == '\0') | |
1851 | return TRUE; | |
1852 | buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type))); | |
1853 | (void) sprintf(buf, "%s %d %s", yitcommand, year, type); | |
1854 | result = system(buf); | |
1855 | if (result == 0) | |
1856 | return TRUE; | |
1857 | if (result == (1 << 8)) | |
1858 | return FALSE; | |
cbd3dceb RM |
1859 | error(_("Wild result from command execution")); |
1860 | (void) fprintf(stderr, _("%s: command was '%s', result was %d\n"), | |
28f540f4 RM |
1861 | progname, buf, result); |
1862 | for ( ; ; ) | |
1863 | (void) exit(EXIT_FAILURE); | |
1864 | } | |
1865 | ||
1866 | static int | |
1867 | lowerit(a) | |
f2e235b9 | 1868 | int a; |
28f540f4 | 1869 | { |
f2e235b9 | 1870 | a = (unsigned char) a; |
28f540f4 RM |
1871 | return (isascii(a) && isupper(a)) ? tolower(a) : a; |
1872 | } | |
1873 | ||
1874 | static int | |
1875 | ciequal(ap, bp) /* case-insensitive equality */ | |
1876 | register const char * ap; | |
1877 | register const char * bp; | |
1878 | { | |
1879 | while (lowerit(*ap) == lowerit(*bp++)) | |
1880 | if (*ap++ == '\0') | |
1881 | return TRUE; | |
1882 | return FALSE; | |
1883 | } | |
1884 | ||
1885 | static int | |
1886 | itsabbr(abbr, word) | |
1887 | register const char * abbr; | |
1888 | register const char * word; | |
1889 | { | |
1890 | if (lowerit(*abbr) != lowerit(*word)) | |
1891 | return FALSE; | |
1892 | ++word; | |
1893 | while (*++abbr != '\0') | |
f2e235b9 RM |
1894 | do { |
1895 | if (*word == '\0') | |
1896 | return FALSE; | |
1897 | } while (lowerit(*word++) != lowerit(*abbr)); | |
28f540f4 RM |
1898 | return TRUE; |
1899 | } | |
1900 | ||
1901 | static const struct lookup * | |
1902 | byword(word, table) | |
1903 | register const char * const word; | |
1904 | register const struct lookup * const table; | |
1905 | { | |
1906 | register const struct lookup * foundlp; | |
1907 | register const struct lookup * lp; | |
1908 | ||
1909 | if (word == NULL || table == NULL) | |
1910 | return NULL; | |
1911 | /* | |
1912 | ** Look for exact match. | |
1913 | */ | |
1914 | for (lp = table; lp->l_word != NULL; ++lp) | |
1915 | if (ciequal(word, lp->l_word)) | |
1916 | return lp; | |
1917 | /* | |
1918 | ** Look for inexact match. | |
1919 | */ | |
1920 | foundlp = NULL; | |
1921 | for (lp = table; lp->l_word != NULL; ++lp) | |
1922 | if (itsabbr(word, lp->l_word)) | |
1923 | if (foundlp == NULL) | |
1924 | foundlp = lp; | |
1925 | else return NULL; /* multiple inexact matches */ | |
1926 | return foundlp; | |
1927 | } | |
1928 | ||
1929 | static char ** | |
1930 | getfields(cp) | |
1931 | register char * cp; | |
1932 | { | |
1933 | register char * dp; | |
1934 | register char ** array; | |
1935 | register int nsubs; | |
1936 | ||
1937 | if (cp == NULL) | |
1938 | return NULL; | |
1939 | array = (char **) (void *) | |
1940 | emalloc((int) ((strlen(cp) + 1) * sizeof *array)); | |
1941 | nsubs = 0; | |
1942 | for ( ; ; ) { | |
f2e235b9 | 1943 | while (isascii(*cp) && isspace((unsigned char) *cp)) |
28f540f4 RM |
1944 | ++cp; |
1945 | if (*cp == '\0' || *cp == '#') | |
1946 | break; | |
1947 | array[nsubs++] = dp = cp; | |
1948 | do { | |
1949 | if ((*dp = *cp++) != '"') | |
1950 | ++dp; | |
1951 | else while ((*dp = *cp++) != '"') | |
1952 | if (*dp != '\0') | |
1953 | ++dp; | |
cbd3dceb | 1954 | else error(_("Odd number of quotation marks")); |
28f540f4 | 1955 | } while (*cp != '\0' && *cp != '#' && |
f2e235b9 RM |
1956 | (!isascii(*cp) || !isspace((unsigned char) *cp))); |
1957 | if (isascii(*cp) && isspace((unsigned char) *cp)) | |
28f540f4 RM |
1958 | ++cp; |
1959 | *dp = '\0'; | |
1960 | } | |
1961 | array[nsubs] = NULL; | |
1962 | return array; | |
1963 | } | |
1964 | ||
1965 | static long | |
1966 | oadd(t1, t2) | |
1967 | const long t1; | |
1968 | const long t2; | |
1969 | { | |
1970 | register long t; | |
1971 | ||
1972 | t = t1 + t2; | |
1973 | if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) { | |
cbd3dceb | 1974 | error(_("time overflow")); |
28f540f4 RM |
1975 | (void) exit(EXIT_FAILURE); |
1976 | } | |
1977 | return t; | |
1978 | } | |
1979 | ||
1980 | static time_t | |
1981 | tadd(t1, t2) | |
1982 | const time_t t1; | |
1983 | const long t2; | |
1984 | { | |
1985 | register time_t t; | |
1986 | ||
1987 | if (t1 == max_time && t2 > 0) | |
1988 | return max_time; | |
1989 | if (t1 == min_time && t2 < 0) | |
1990 | return min_time; | |
1991 | t = t1 + t2; | |
1992 | if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) { | |
cbd3dceb | 1993 | error(_("time overflow")); |
28f540f4 RM |
1994 | (void) exit(EXIT_FAILURE); |
1995 | } | |
1996 | return t; | |
1997 | } | |
1998 | ||
1999 | /* | |
2000 | ** Given a rule, and a year, compute the date - in seconds since January 1, | |
2001 | ** 1970, 00:00 LOCAL time - in that year that the rule refers to. | |
2002 | */ | |
2003 | ||
2004 | static time_t | |
2005 | rpytime(rp, wantedy) | |
2006 | register const struct rule * const rp; | |
2007 | register const int wantedy; | |
2008 | { | |
2009 | register int y, m, i; | |
2010 | register long dayoff; /* with a nod to Margaret O. */ | |
2011 | register time_t t; | |
2012 | ||
f2e235b9 | 2013 | if (wantedy == INT_MIN) |
28f540f4 | 2014 | return min_time; |
f2e235b9 | 2015 | if (wantedy == INT_MAX) |
28f540f4 RM |
2016 | return max_time; |
2017 | dayoff = 0; | |
2018 | m = TM_JANUARY; | |
2019 | y = EPOCH_YEAR; | |
2020 | while (wantedy != y) { | |
2021 | if (wantedy > y) { | |
2022 | i = len_years[isleap(y)]; | |
2023 | ++y; | |
2024 | } else { | |
2025 | --y; | |
2026 | i = -len_years[isleap(y)]; | |
2027 | } | |
2028 | dayoff = oadd(dayoff, eitol(i)); | |
2029 | } | |
2030 | while (m != rp->r_month) { | |
2031 | i = len_months[isleap(y)][m]; | |
2032 | dayoff = oadd(dayoff, eitol(i)); | |
2033 | ++m; | |
2034 | } | |
2035 | i = rp->r_dayofmonth; | |
2036 | if (m == TM_FEBRUARY && i == 29 && !isleap(y)) { | |
2037 | if (rp->r_dycode == DC_DOWLEQ) | |
2038 | --i; | |
2039 | else { | |
cbd3dceb | 2040 | error(_("use of 2/29 in non leap-year")); |
28f540f4 RM |
2041 | (void) exit(EXIT_FAILURE); |
2042 | } | |
2043 | } | |
2044 | --i; | |
2045 | dayoff = oadd(dayoff, eitol(i)); | |
2046 | if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) { | |
2047 | register long wday; | |
2048 | ||
2049 | #define LDAYSPERWEEK ((long) DAYSPERWEEK) | |
2050 | wday = eitol(EPOCH_WDAY); | |
2051 | /* | |
2052 | ** Don't trust mod of negative numbers. | |
2053 | */ | |
2054 | if (dayoff >= 0) | |
2055 | wday = (wday + dayoff) % LDAYSPERWEEK; | |
2056 | else { | |
2057 | wday -= ((-dayoff) % LDAYSPERWEEK); | |
2058 | if (wday < 0) | |
2059 | wday += LDAYSPERWEEK; | |
2060 | } | |
2061 | while (wday != eitol(rp->r_wday)) | |
2062 | if (rp->r_dycode == DC_DOWGEQ) { | |
2063 | dayoff = oadd(dayoff, (long) 1); | |
2064 | if (++wday >= LDAYSPERWEEK) | |
2065 | wday = 0; | |
2066 | ++i; | |
2067 | } else { | |
2068 | dayoff = oadd(dayoff, (long) -1); | |
2069 | if (--wday < 0) | |
2070 | wday = LDAYSPERWEEK - 1; | |
2071 | --i; | |
2072 | } | |
2073 | if (i < 0 || i >= len_months[isleap(y)][m]) { | |
cbd3dceb | 2074 | error(_("no day in month matches rule")); |
28f540f4 RM |
2075 | (void) exit(EXIT_FAILURE); |
2076 | } | |
2077 | } | |
f2e235b9 | 2078 | if (dayoff < 0 && !TYPE_SIGNED(time_t)) |
28f540f4 RM |
2079 | return min_time; |
2080 | t = (time_t) dayoff * SECSPERDAY; | |
2081 | /* | |
2082 | ** Cheap overflow check. | |
2083 | */ | |
2084 | if (t / SECSPERDAY != dayoff) | |
2085 | return (dayoff > 0) ? max_time : min_time; | |
2086 | return tadd(t, rp->r_tod); | |
2087 | } | |
2088 | ||
2089 | static void | |
2090 | newabbr(string) | |
2091 | const char * const string; | |
2092 | { | |
2093 | register int i; | |
2094 | ||
2095 | i = strlen(string) + 1; | |
2096 | if (charcnt + i > TZ_MAX_CHARS) { | |
cbd3dceb | 2097 | error(_("too many, or too long, time zone abbreviations")); |
28f540f4 RM |
2098 | (void) exit(EXIT_FAILURE); |
2099 | } | |
2100 | (void) strcpy(&chars[charcnt], string); | |
2101 | charcnt += eitol(i); | |
2102 | } | |
2103 | ||
2104 | static int | |
2105 | mkdirs(argname) | |
2106 | char * const argname; | |
2107 | { | |
2108 | register char * name; | |
2109 | register char * cp; | |
2110 | ||
2111 | if (argname == NULL || *argname == '\0') | |
2112 | return 0; | |
2113 | cp = name = ecpyalloc(argname); | |
2114 | while ((cp = strchr(cp + 1, '/')) != 0) { | |
2115 | *cp = '\0'; | |
2116 | #ifndef unix | |
2117 | /* | |
6c2f0507 | 2118 | ** DOS drive specifier? |
28f540f4 | 2119 | */ |
f2e235b9 RM |
2120 | if (isalpha((unsigned char) name[0]) && |
2121 | name[1] == ':' && name[2] == '\0') { | |
28f540f4 RM |
2122 | *cp = '/'; |
2123 | continue; | |
2124 | } | |
2125 | #endif /* !defined unix */ | |
2126 | if (!itsdir(name)) { | |
2127 | /* | |
2128 | ** It doesn't seem to exist, so we try to create it. | |
2129 | */ | |
6c2f0507 | 2130 | if (mkdir(name, 0755) != 0) { |
92777700 | 2131 | const char *e = strerror(errno); |
6d52618b | 2132 | |
28f540f4 | 2133 | (void) fprintf(stderr, |
92777700 RM |
2134 | _("%s: Can't create directory %s: %s\n"), |
2135 | progname, name, e); | |
28f540f4 RM |
2136 | ifree(name); |
2137 | return -1; | |
2138 | } | |
2139 | } | |
2140 | *cp = '/'; | |
2141 | } | |
2142 | ifree(name); | |
2143 | return 0; | |
2144 | } | |
2145 | ||
2146 | static long | |
2147 | eitol(i) | |
2148 | const int i; | |
2149 | { | |
2150 | long l; | |
2151 | ||
2152 | l = i; | |
2153 | if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0)) { | |
2154 | (void) fprintf(stderr, | |
cbd3dceb | 2155 | _("%s: %d did not sign extend correctly\n"), |
28f540f4 RM |
2156 | progname, i); |
2157 | (void) exit(EXIT_FAILURE); | |
2158 | } | |
2159 | return l; | |
2160 | } | |
2161 | ||
2162 | /* | |
2163 | ** UNIX was a registered trademark of UNIX System Laboratories in 1993. | |
2164 | */ |