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