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