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