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