2.3.3-74
[glibc.git] / timezone / zic.c
blob9bb8662e55ce34d0cd6036aef02da5ba860fb010
1 static char elsieid[] = "@(#)zic.c 7.116";
3 #include "private.h"
4 #include "locale.h"
5 #include "tzfile.h"
7 #if HAVE_SYS_STAT_H
8 #include "sys/stat.h"
9 #endif
10 #ifdef S_IRUSR
11 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
12 #else
13 #define MKDIR_UMASK 0755
14 #endif
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 [ --version ] [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\t[ -d directory ] [ -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_CTYPE, "");
471 (void) setlocale(LC_MESSAGES, "");
472 #ifdef TZ_DOMAINDIR
473 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
474 #endif /* defined TEXTDOMAINDIR */
475 (void) textdomain(TZ_DOMAIN);
476 #endif /* HAVE_GETTEXT - 0 */
477 progname = argv[0];
478 for (i = 1; i < argc; ++i)
479 if (strcmp(argv[i], "--version") == 0) {
480 (void) printf("%s\n", elsieid);
481 (void) exit(EXIT_SUCCESS);
483 while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF && c != -1)
484 switch (c) {
485 default:
486 usage();
487 case 'd':
488 if (directory == NULL)
489 directory = optarg;
490 else {
491 (void) fprintf(stderr,
492 _("%s: More than one -d option specified\n"),
493 progname);
494 (void) exit(EXIT_FAILURE);
496 break;
497 case 'l':
498 if (lcltime == NULL)
499 lcltime = optarg;
500 else {
501 (void) fprintf(stderr,
502 _("%s: More than one -l option specified\n"),
503 progname);
504 (void) exit(EXIT_FAILURE);
506 break;
507 case 'p':
508 if (psxrules == NULL)
509 psxrules = optarg;
510 else {
511 (void) fprintf(stderr,
512 _("%s: More than one -p option specified\n"),
513 progname);
514 (void) exit(EXIT_FAILURE);
516 break;
517 case 'y':
518 if (yitcommand == NULL)
519 yitcommand = optarg;
520 else {
521 (void) fprintf(stderr,
522 _("%s: More than one -y option specified\n"),
523 progname);
524 (void) exit(EXIT_FAILURE);
526 break;
527 case 'L':
528 if (leapsec == NULL)
529 leapsec = optarg;
530 else {
531 (void) fprintf(stderr,
532 _("%s: More than one -L option specified\n"),
533 progname);
534 (void) exit(EXIT_FAILURE);
536 break;
537 case 'v':
538 noise = TRUE;
539 break;
540 case 's':
541 sflag = TRUE;
542 break;
544 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
545 usage(); /* usage message by request */
546 if (directory == NULL)
547 directory = TZDIR;
548 if (yitcommand == NULL)
549 yitcommand = "yearistype";
551 setboundaries();
553 if (optind < argc && leapsec != NULL) {
554 infile(leapsec);
555 adjleap();
558 for (i = optind; i < argc; ++i)
559 infile(argv[i]);
560 if (errors)
561 (void) exit(EXIT_FAILURE);
562 associate();
563 for (i = 0; i < nzones; i = j) {
565 ** Find the next non-continuation zone entry.
567 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
568 continue;
569 outzone(&zones[i], j - i);
572 ** Make links.
574 for (i = 0; i < nlinks; ++i) {
575 eat(links[i].l_filename, links[i].l_linenum);
576 dolink(links[i].l_from, links[i].l_to);
578 if (lcltime != NULL) {
579 eat("command line", 1);
580 dolink(lcltime, TZDEFAULT);
582 if (psxrules != NULL) {
583 eat("command line", 1);
584 dolink(psxrules, TZDEFRULES);
586 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
589 static void
590 dolink(fromfile, tofile)
591 const char * const fromfile;
592 const char * const tofile;
594 register char * fromname;
595 register char * toname;
597 if (fromfile[0] == '/')
598 fromname = ecpyalloc(fromfile);
599 else {
600 fromname = ecpyalloc(directory);
601 fromname = ecatalloc(fromname, "/");
602 fromname = ecatalloc(fromname, fromfile);
604 if (tofile[0] == '/')
605 toname = ecpyalloc(tofile);
606 else {
607 toname = ecpyalloc(directory);
608 toname = ecatalloc(toname, "/");
609 toname = ecatalloc(toname, tofile);
612 ** We get to be careful here since
613 ** there's a fair chance of root running us.
615 if (!itsdir(toname))
616 (void) remove(toname);
617 if (link(fromname, toname) != 0) {
618 int result;
620 if (mkdirs(toname) != 0)
621 (void) exit(EXIT_FAILURE);
623 result = link(fromname, toname);
624 #if (HAVE_SYMLINK - 0)
625 if (result != 0 &&
626 access(fromname, F_OK) == 0 &&
627 !itsdir(fromname)) {
628 const char *s = tofile;
629 register char * symlinkcontents = NULL;
630 while ((s = strchr(s+1, '/')) != NULL)
631 symlinkcontents = ecatalloc(symlinkcontents, "../");
632 symlinkcontents = ecatalloc(symlinkcontents, fromname);
634 result = unlink(toname);
635 if (result != 0 && errno != ENOENT) {
636 const char *e = strerror(errno);
638 (void) fprintf(stderr,
639 _("%s: Can't unlink %s: %s\n"),
640 progname, toname, e);
641 (void) exit(EXIT_FAILURE);
644 result = symlink(symlinkcontents, toname);
645 if (result == 0)
646 warning(_("hard link failed, symbolic link used"));
647 ifree(symlinkcontents);
649 #endif
650 if (result != 0) {
651 const char *e = strerror(errno);
653 (void) fprintf(stderr,
654 _("%s: Can't link from %s to %s: %s\n"),
655 progname, fromname, toname, e);
656 (void) exit(EXIT_FAILURE);
659 ifree(fromname);
660 ifree(toname);
663 #ifndef INT_MAX
664 #define INT_MAX ((int) (((unsigned)~0)>>1))
665 #endif /* !defined INT_MAX */
667 #ifndef INT_MIN
668 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
669 #endif /* !defined INT_MIN */
672 ** The tz file format currently allows at most 32-bit quantities.
673 ** This restriction should be removed before signed 32-bit values
674 ** wrap around in 2038, but unfortunately this will require a
675 ** change to the tz file format.
678 #define MAX_BITS_IN_FILE 32
679 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(time_t) < MAX_BITS_IN_FILE) ? TYPE_BIT(time_t) : MAX_BITS_IN_FILE)
681 static void
682 setboundaries P((void))
684 if (TYPE_SIGNED(time_t)) {
685 min_time = ~ (time_t) 0;
686 min_time <<= TIME_T_BITS_IN_FILE - 1;
687 max_time = ~ (time_t) 0 - min_time;
688 if (sflag)
689 min_time = 0;
690 } else {
691 min_time = 0;
692 max_time = 2 - sflag;
693 max_time <<= TIME_T_BITS_IN_FILE - 1;
694 --max_time;
696 min_year = TM_YEAR_BASE + gmtime(&min_time)->tm_year;
697 max_year = TM_YEAR_BASE + gmtime(&max_time)->tm_year;
698 min_year_representable = min_year;
699 max_year_representable = max_year;
702 static int
703 itsdir(name)
704 const char * const name;
706 register char * myname;
707 register int accres;
709 myname = ecpyalloc(name);
710 myname = ecatalloc(myname, "/.");
711 accres = access(myname, F_OK);
712 ifree(myname);
713 return accres == 0;
717 ** Associate sets of rules with zones.
721 ** Sort by rule name.
724 static int
725 rcomp(cp1, cp2)
726 const void * cp1;
727 const void * cp2;
729 return strcmp(((const struct rule *) cp1)->r_name,
730 ((const struct rule *) cp2)->r_name);
733 static void
734 associate P((void))
736 register struct zone * zp;
737 register struct rule * rp;
738 register int base, out;
739 register int i, j;
741 if (nrules != 0) {
742 (void) qsort((void *) rules, (size_t) nrules,
743 (size_t) sizeof *rules, rcomp);
744 for (i = 0; i < nrules - 1; ++i) {
745 if (strcmp(rules[i].r_name,
746 rules[i + 1].r_name) != 0)
747 continue;
748 if (strcmp(rules[i].r_filename,
749 rules[i + 1].r_filename) == 0)
750 continue;
751 eat(rules[i].r_filename, rules[i].r_linenum);
752 warning(_("same rule name in multiple files"));
753 eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
754 warning(_("same rule name in multiple files"));
755 for (j = i + 2; j < nrules; ++j) {
756 if (strcmp(rules[i].r_name,
757 rules[j].r_name) != 0)
758 break;
759 if (strcmp(rules[i].r_filename,
760 rules[j].r_filename) == 0)
761 continue;
762 if (strcmp(rules[i + 1].r_filename,
763 rules[j].r_filename) == 0)
764 continue;
765 break;
767 i = j - 1;
770 for (i = 0; i < nzones; ++i) {
771 zp = &zones[i];
772 zp->z_rules = NULL;
773 zp->z_nrules = 0;
775 for (base = 0; base < nrules; base = out) {
776 rp = &rules[base];
777 for (out = base + 1; out < nrules; ++out)
778 if (strcmp(rp->r_name, rules[out].r_name) != 0)
779 break;
780 for (i = 0; i < nzones; ++i) {
781 zp = &zones[i];
782 if (strcmp(zp->z_rule, rp->r_name) != 0)
783 continue;
784 zp->z_rules = rp;
785 zp->z_nrules = out - base;
788 for (i = 0; i < nzones; ++i) {
789 zp = &zones[i];
790 if (zp->z_nrules == 0) {
792 ** Maybe we have a local standard time offset.
794 eat(zp->z_filename, zp->z_linenum);
795 zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
796 TRUE);
798 ** Note, though, that if there's no rule,
799 ** a '%s' in the format is a bad thing.
801 if (strchr(zp->z_format, '%') != 0)
802 error(_("%s in ruleless zone"));
805 if (errors)
806 (void) exit(EXIT_FAILURE);
809 static void
810 infile(name)
811 const char * name;
813 register FILE * fp;
814 register char ** fields;
815 register char * cp;
816 register const struct lookup * lp;
817 register int nfields;
818 register int wantcont;
819 register int num;
820 char buf[BUFSIZ];
822 if (strcmp(name, "-") == 0) {
823 name = _("standard input");
824 fp = stdin;
825 } else if ((fp = fopen(name, "r")) == NULL) {
826 const char *e = strerror(errno);
828 (void) fprintf(stderr, _("%s: Can't open %s: %s\n"),
829 progname, name, e);
830 (void) exit(EXIT_FAILURE);
832 wantcont = FALSE;
833 for (num = 1; ; ++num) {
834 eat(name, num);
835 if (fgets(buf, (int) sizeof buf, fp) != buf)
836 break;
837 cp = strchr(buf, '\n');
838 if (cp == NULL) {
839 error(_("line too long"));
840 (void) exit(EXIT_FAILURE);
842 *cp = '\0';
843 fields = getfields(buf);
844 nfields = 0;
845 while (fields[nfields] != NULL) {
846 static char nada;
848 if (strcmp(fields[nfields], "-") == 0)
849 fields[nfields] = &nada;
850 ++nfields;
852 if (nfields == 0) {
853 /* nothing to do */
854 } else if (wantcont) {
855 wantcont = inzcont(fields, nfields);
856 } else {
857 lp = byword(fields[0], line_codes);
858 if (lp == NULL)
859 error(_("input line of unknown type"));
860 else switch ((int) (lp->l_value)) {
861 case LC_RULE:
862 inrule(fields, nfields);
863 wantcont = FALSE;
864 break;
865 case LC_ZONE:
866 wantcont = inzone(fields, nfields);
867 break;
868 case LC_LINK:
869 inlink(fields, nfields);
870 wantcont = FALSE;
871 break;
872 case LC_LEAP:
873 if (name != leapsec)
874 (void) fprintf(stderr,
875 _("%s: Leap line in non leap seconds file %s\n"),
876 progname, name);
877 else inleap(fields, nfields);
878 wantcont = FALSE;
879 break;
880 default: /* "cannot happen" */
881 (void) fprintf(stderr,
882 _("%s: panic: Invalid l_value %d\n"),
883 progname, lp->l_value);
884 (void) exit(EXIT_FAILURE);
887 ifree((char *) fields);
889 if (ferror(fp)) {
890 (void) fprintf(stderr, _("%s: Error reading %s\n"),
891 progname, filename);
892 (void) exit(EXIT_FAILURE);
894 if (fp != stdin && fclose(fp)) {
895 const char *e = strerror(errno);
897 (void) fprintf(stderr, _("%s: Error closing %s: %s\n"),
898 progname, filename, e);
899 (void) exit(EXIT_FAILURE);
901 if (wantcont)
902 error(_("expected continuation line not found"));
906 ** Convert a string of one of the forms
907 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
908 ** into a number of seconds.
909 ** A null string maps to zero.
910 ** Call error with errstring and return zero on errors.
913 static long
914 gethms(string, errstring, signable)
915 const char * string;
916 const char * const errstring;
917 const int signable;
919 int hh, mm, ss, sign;
921 if (string == NULL || *string == '\0')
922 return 0;
923 if (!signable)
924 sign = 1;
925 else if (*string == '-') {
926 sign = -1;
927 ++string;
928 } else sign = 1;
929 if (sscanf(string, scheck(string, "%d"), &hh) == 1)
930 mm = ss = 0;
931 else if (sscanf(string, scheck(string, "%d:%d"), &hh, &mm) == 2)
932 ss = 0;
933 else if (sscanf(string, scheck(string, "%d:%d:%d"),
934 &hh, &mm, &ss) != 3) {
935 error(errstring);
936 return 0;
938 if ((hh < 0 || hh >= HOURSPERDAY ||
939 mm < 0 || mm >= MINSPERHOUR ||
940 ss < 0 || ss > SECSPERMIN) &&
941 !(hh == HOURSPERDAY && mm == 0 && ss == 0)) {
942 error(errstring);
943 return 0;
945 if (noise && hh == HOURSPERDAY)
946 warning(_("24:00 not handled by pre-1998 versions of zic"));
947 return eitol(sign) *
948 (eitol(hh * MINSPERHOUR + mm) *
949 eitol(SECSPERMIN) + eitol(ss));
952 static void
953 inrule(fields, nfields)
954 register char ** const fields;
955 const int nfields;
957 static struct rule r;
959 if (nfields != RULE_FIELDS) {
960 error(_("wrong number of fields on Rule line"));
961 return;
963 if (*fields[RF_NAME] == '\0') {
964 error(_("nameless rule"));
965 return;
967 r.r_filename = filename;
968 r.r_linenum = linenum;
969 r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
970 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
971 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
972 r.r_name = ecpyalloc(fields[RF_NAME]);
973 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
974 rules = (struct rule *) (void *) erealloc((char *) rules,
975 (int) ((nrules + 1) * sizeof *rules));
976 rules[nrules++] = r;
979 static int
980 inzone(fields, nfields)
981 register char ** const fields;
982 const int nfields;
984 register int i;
985 static char * buf;
987 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
988 error(_("wrong number of fields on Zone line"));
989 return FALSE;
991 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
992 buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
993 (void) sprintf(buf,
994 _("\"Zone %s\" line and -l option are mutually exclusive"),
995 TZDEFAULT);
996 error(buf);
997 return FALSE;
999 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
1000 buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
1001 (void) sprintf(buf,
1002 _("\"Zone %s\" line and -p option are mutually exclusive"),
1003 TZDEFRULES);
1004 error(buf);
1005 return FALSE;
1007 for (i = 0; i < nzones; ++i)
1008 if (zones[i].z_name != NULL &&
1009 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
1010 buf = erealloc(buf, (int) (132 +
1011 strlen(fields[ZF_NAME]) +
1012 strlen(zones[i].z_filename)));
1013 (void) sprintf(buf,
1014 _("duplicate zone name %s (file \"%s\", line %d)"),
1015 fields[ZF_NAME],
1016 zones[i].z_filename,
1017 zones[i].z_linenum);
1018 error(buf);
1019 return FALSE;
1021 return inzsub(fields, nfields, FALSE);
1024 static int
1025 inzcont(fields, nfields)
1026 register char ** const fields;
1027 const int nfields;
1029 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
1030 error(_("wrong number of fields on Zone continuation line"));
1031 return FALSE;
1033 return inzsub(fields, nfields, TRUE);
1036 static int
1037 inzsub(fields, nfields, iscont)
1038 register char ** const fields;
1039 const int nfields;
1040 const int iscont;
1042 register char * cp;
1043 static struct zone z;
1044 register int i_gmtoff, i_rule, i_format;
1045 register int i_untilyear, i_untilmonth;
1046 register int i_untilday, i_untiltime;
1047 register int hasuntil;
1049 if (iscont) {
1050 i_gmtoff = ZFC_GMTOFF;
1051 i_rule = ZFC_RULE;
1052 i_format = ZFC_FORMAT;
1053 i_untilyear = ZFC_TILYEAR;
1054 i_untilmonth = ZFC_TILMONTH;
1055 i_untilday = ZFC_TILDAY;
1056 i_untiltime = ZFC_TILTIME;
1057 z.z_name = NULL;
1058 } else {
1059 i_gmtoff = ZF_GMTOFF;
1060 i_rule = ZF_RULE;
1061 i_format = ZF_FORMAT;
1062 i_untilyear = ZF_TILYEAR;
1063 i_untilmonth = ZF_TILMONTH;
1064 i_untilday = ZF_TILDAY;
1065 i_untiltime = ZF_TILTIME;
1066 z.z_name = ecpyalloc(fields[ZF_NAME]);
1068 z.z_filename = filename;
1069 z.z_linenum = linenum;
1070 z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UTC offset"), TRUE);
1071 if ((cp = strchr(fields[i_format], '%')) != 0) {
1072 if (*++cp != 's' || strchr(cp, '%') != 0) {
1073 error(_("invalid abbreviation format"));
1074 return FALSE;
1077 z.z_rule = ecpyalloc(fields[i_rule]);
1078 z.z_format = ecpyalloc(fields[i_format]);
1079 hasuntil = nfields > i_untilyear;
1080 if (hasuntil) {
1081 z.z_untilrule.r_filename = filename;
1082 z.z_untilrule.r_linenum = linenum;
1083 rulesub(&z.z_untilrule,
1084 fields[i_untilyear],
1085 "only",
1087 (nfields > i_untilmonth) ?
1088 fields[i_untilmonth] : "Jan",
1089 (nfields > i_untilday) ? fields[i_untilday] : "1",
1090 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1091 z.z_untiltime = rpytime(&z.z_untilrule,
1092 z.z_untilrule.r_loyear);
1093 if (iscont && nzones > 0 &&
1094 z.z_untiltime > min_time &&
1095 z.z_untiltime < max_time &&
1096 zones[nzones - 1].z_untiltime > min_time &&
1097 zones[nzones - 1].z_untiltime < max_time &&
1098 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1099 error(_("Zone continuation line end time is not after end time of previous line"));
1100 return FALSE;
1103 zones = (struct zone *) (void *) erealloc((char *) zones,
1104 (int) ((nzones + 1) * sizeof *zones));
1105 zones[nzones++] = z;
1107 ** If there was an UNTIL field on this line,
1108 ** there's more information about the zone on the next line.
1110 return hasuntil;
1113 static void
1114 inleap(fields, nfields)
1115 register char ** const fields;
1116 const int nfields;
1118 register const char * cp;
1119 register const struct lookup * lp;
1120 register int i, j;
1121 int year, month, day;
1122 long dayoff, tod;
1123 time_t t;
1125 if (nfields != LEAP_FIELDS) {
1126 error(_("wrong number of fields on Leap line"));
1127 return;
1129 dayoff = 0;
1130 cp = fields[LP_YEAR];
1131 if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
1133 * Leapin' Lizards!
1135 error(_("invalid leaping year"));
1136 return;
1138 j = EPOCH_YEAR;
1139 while (j != year) {
1140 if (year > j) {
1141 i = len_years[isleap(j)];
1142 ++j;
1143 } else {
1144 --j;
1145 i = -len_years[isleap(j)];
1147 dayoff = oadd(dayoff, eitol(i));
1149 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1150 error(_("invalid month name"));
1151 return;
1153 month = lp->l_value;
1154 j = TM_JANUARY;
1155 while (j != month) {
1156 i = len_months[isleap(year)][j];
1157 dayoff = oadd(dayoff, eitol(i));
1158 ++j;
1160 cp = fields[LP_DAY];
1161 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1162 day <= 0 || day > len_months[isleap(year)][month]) {
1163 error(_("invalid day of month"));
1164 return;
1166 dayoff = oadd(dayoff, eitol(day - 1));
1167 if (dayoff < 0 && !TYPE_SIGNED(time_t)) {
1168 error(_("time before zero"));
1169 return;
1171 if (dayoff < min_time / SECSPERDAY) {
1172 error(_("time too small"));
1173 return;
1175 if (dayoff > max_time / SECSPERDAY) {
1176 error(_("time too large"));
1177 return;
1179 t = (time_t) dayoff * SECSPERDAY;
1180 tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1181 cp = fields[LP_CORR];
1183 register int positive;
1184 int count;
1186 if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1187 positive = FALSE;
1188 count = 1;
1189 } else if (strcmp(cp, "--") == 0) {
1190 positive = FALSE;
1191 count = 2;
1192 } else if (strcmp(cp, "+") == 0) {
1193 positive = TRUE;
1194 count = 1;
1195 } else if (strcmp(cp, "++") == 0) {
1196 positive = TRUE;
1197 count = 2;
1198 } else {
1199 error(_("illegal CORRECTION field on Leap line"));
1200 return;
1202 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1203 error(_("illegal Rolling/Stationary field on Leap line"));
1204 return;
1206 leapadd(tadd(t, tod), positive, lp->l_value, count);
1210 static void
1211 inlink(fields, nfields)
1212 register char ** const fields;
1213 const int nfields;
1215 struct link l;
1217 if (nfields != LINK_FIELDS) {
1218 error(_("wrong number of fields on Link line"));
1219 return;
1221 if (*fields[LF_FROM] == '\0') {
1222 error(_("blank FROM field on Link line"));
1223 return;
1225 if (*fields[LF_TO] == '\0') {
1226 error(_("blank TO field on Link line"));
1227 return;
1229 l.l_filename = filename;
1230 l.l_linenum = linenum;
1231 l.l_from = ecpyalloc(fields[LF_FROM]);
1232 l.l_to = ecpyalloc(fields[LF_TO]);
1233 links = (struct link *) (void *) erealloc((char *) links,
1234 (int) ((nlinks + 1) * sizeof *links));
1235 links[nlinks++] = l;
1238 static void
1239 rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
1240 register struct rule * const rp;
1241 const char * const loyearp;
1242 const char * const hiyearp;
1243 const char * const typep;
1244 const char * const monthp;
1245 const char * const dayp;
1246 const char * const timep;
1248 register const struct lookup * lp;
1249 register const char * cp;
1250 register char * dp;
1251 register char * ep;
1253 if ((lp = byword(monthp, mon_names)) == NULL) {
1254 error(_("invalid month name"));
1255 return;
1257 rp->r_month = lp->l_value;
1258 rp->r_todisstd = FALSE;
1259 rp->r_todisgmt = FALSE;
1260 dp = ecpyalloc(timep);
1261 if (*dp != '\0') {
1262 ep = dp + strlen(dp) - 1;
1263 switch (lowerit(*ep)) {
1264 case 's': /* Standard */
1265 rp->r_todisstd = TRUE;
1266 rp->r_todisgmt = FALSE;
1267 *ep = '\0';
1268 break;
1269 case 'w': /* Wall */
1270 rp->r_todisstd = FALSE;
1271 rp->r_todisgmt = FALSE;
1272 *ep = '\0';
1273 break;
1274 case 'g': /* Greenwich */
1275 case 'u': /* Universal */
1276 case 'z': /* Zulu */
1277 rp->r_todisstd = TRUE;
1278 rp->r_todisgmt = TRUE;
1279 *ep = '\0';
1280 break;
1283 rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1284 ifree(dp);
1286 ** Year work.
1288 cp = loyearp;
1289 lp = byword(cp, begin_years);
1290 if (lp != NULL) switch ((int) lp->l_value) {
1291 case YR_MINIMUM:
1292 rp->r_loyear = INT_MIN;
1293 break;
1294 case YR_MAXIMUM:
1295 rp->r_loyear = INT_MAX;
1296 break;
1297 default: /* "cannot happen" */
1298 (void) fprintf(stderr,
1299 _("%s: panic: Invalid l_value %d\n"),
1300 progname, lp->l_value);
1301 (void) exit(EXIT_FAILURE);
1302 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
1303 error(_("invalid starting year"));
1304 return;
1305 } else if (noise) {
1306 if (rp->r_loyear < min_year_representable)
1307 warning(_("starting year too low to be represented"));
1308 else if (rp->r_loyear > max_year_representable)
1309 warning(_("starting year too high to be represented"));
1311 cp = hiyearp;
1312 if ((lp = byword(cp, end_years)) != NULL) switch ((int) lp->l_value) {
1313 case YR_MINIMUM:
1314 rp->r_hiyear = INT_MIN;
1315 break;
1316 case YR_MAXIMUM:
1317 rp->r_hiyear = INT_MAX;
1318 break;
1319 case YR_ONLY:
1320 rp->r_hiyear = rp->r_loyear;
1321 break;
1322 default: /* "cannot happen" */
1323 (void) fprintf(stderr,
1324 _("%s: panic: Invalid l_value %d\n"),
1325 progname, lp->l_value);
1326 (void) exit(EXIT_FAILURE);
1327 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
1328 error(_("invalid ending year"));
1329 return;
1330 } else if (noise) {
1331 if (rp->r_loyear < min_year_representable)
1332 warning(_("ending year too low to be represented"));
1333 else if (rp->r_loyear > max_year_representable)
1334 warning(_("ending year too high to be represented"));
1336 if (rp->r_loyear > rp->r_hiyear) {
1337 error(_("starting year greater than ending year"));
1338 return;
1340 if (*typep == '\0')
1341 rp->r_yrtype = NULL;
1342 else {
1343 if (rp->r_loyear == rp->r_hiyear) {
1344 error(_("typed single year"));
1345 return;
1347 rp->r_yrtype = ecpyalloc(typep);
1349 if (rp->r_loyear < min_year && rp->r_loyear > 0)
1350 min_year = rp->r_loyear;
1352 ** Day work.
1353 ** Accept things such as:
1354 ** 1
1355 ** last-Sunday
1356 ** Sun<=20
1357 ** Sun>=7
1359 dp = ecpyalloc(dayp);
1360 if ((lp = byword(dp, lasts)) != NULL) {
1361 rp->r_dycode = DC_DOWLEQ;
1362 rp->r_wday = lp->l_value;
1363 rp->r_dayofmonth = len_months[1][rp->r_month];
1364 } else {
1365 if ((ep = strchr(dp, '<')) != 0)
1366 rp->r_dycode = DC_DOWLEQ;
1367 else if ((ep = strchr(dp, '>')) != 0)
1368 rp->r_dycode = DC_DOWGEQ;
1369 else {
1370 ep = dp;
1371 rp->r_dycode = DC_DOM;
1373 if (rp->r_dycode != DC_DOM) {
1374 *ep++ = 0;
1375 if (*ep++ != '=') {
1376 error(_("invalid day of month"));
1377 ifree(dp);
1378 return;
1380 if ((lp = byword(dp, wday_names)) == NULL) {
1381 error(_("invalid weekday name"));
1382 ifree(dp);
1383 return;
1385 rp->r_wday = lp->l_value;
1387 if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1388 rp->r_dayofmonth <= 0 ||
1389 (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1390 error(_("invalid day of month"));
1391 ifree(dp);
1392 return;
1395 ifree(dp);
1398 static void
1399 convert(val, buf)
1400 const long val;
1401 char * const buf;
1403 register int i;
1404 register long shift;
1406 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1407 buf[i] = val >> shift;
1410 static void
1411 puttzcode(val, fp)
1412 const long val;
1413 FILE * const fp;
1415 char buf[4];
1417 convert(val, buf);
1418 (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1421 static int
1422 atcomp(avp, bvp)
1423 void * avp;
1424 void * bvp;
1426 if (((struct attype *) avp)->at < ((struct attype *) bvp)->at)
1427 return -1;
1428 else if (((struct attype *) avp)->at > ((struct attype *) bvp)->at)
1429 return 1;
1430 else return 0;
1433 static void
1434 writezone(name)
1435 const char * const name;
1437 register FILE * fp;
1438 register int i, j;
1439 static char * fullname;
1440 static struct tzhead tzh;
1441 time_t ats[TZ_MAX_TIMES];
1442 unsigned char types[TZ_MAX_TIMES];
1445 ** Sort.
1447 if (timecnt > 1)
1448 (void) qsort((void *) attypes, (size_t) timecnt,
1449 (size_t) sizeof *attypes, atcomp);
1451 ** Optimize.
1454 int fromi;
1455 int toi;
1457 toi = 0;
1458 fromi = 0;
1459 while (fromi < timecnt && attypes[fromi].at < min_time)
1460 ++fromi;
1461 if (isdsts[0] == 0)
1462 while (fromi < timecnt && attypes[fromi].type == 0)
1463 ++fromi; /* handled by default rule */
1464 for ( ; fromi < timecnt; ++fromi) {
1465 if (toi != 0
1466 && ((attypes[fromi].at
1467 + gmtoffs[attypes[toi - 1].type])
1468 <= (attypes[toi - 1].at
1469 + gmtoffs[toi == 1 ? 0
1470 : attypes[toi - 2].type]))) {
1471 attypes[toi - 1].type = attypes[fromi].type;
1472 continue;
1474 if (toi == 0 ||
1475 attypes[toi - 1].type != attypes[fromi].type)
1476 attypes[toi++] = attypes[fromi];
1478 timecnt = toi;
1481 ** Transfer.
1483 for (i = 0; i < timecnt; ++i) {
1484 ats[i] = attypes[i].at;
1485 types[i] = attypes[i].type;
1487 fullname = erealloc(fullname,
1488 (int) (strlen(directory) + 1 + strlen(name) + 1));
1489 (void) sprintf(fullname, "%s/%s", directory, name);
1491 ** Remove old file, if any, to snap links.
1493 if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT) {
1494 const char *e = strerror(errno);
1496 (void) fprintf(stderr, _("%s: Can't remove %s: %s\n"),
1497 progname, fullname, e);
1498 (void) exit(EXIT_FAILURE);
1500 if ((fp = fopen(fullname, "wb")) == NULL) {
1501 if (mkdirs(fullname) != 0)
1502 (void) exit(EXIT_FAILURE);
1503 if ((fp = fopen(fullname, "wb")) == NULL) {
1504 const char *e = strerror(errno);
1506 (void) fprintf(stderr, _("%s: Can't create %s: %s\n"),
1507 progname, fullname, e);
1508 (void) exit(EXIT_FAILURE);
1511 convert(eitol(typecnt), tzh.tzh_ttisgmtcnt);
1512 convert(eitol(typecnt), tzh.tzh_ttisstdcnt);
1513 convert(eitol(leapcnt), tzh.tzh_leapcnt);
1514 convert(eitol(timecnt), tzh.tzh_timecnt);
1515 convert(eitol(typecnt), tzh.tzh_typecnt);
1516 convert(eitol(charcnt), tzh.tzh_charcnt);
1517 (void) strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1518 #define DO(field) (void) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp)
1519 DO(tzh_magic);
1520 DO(tzh_reserved);
1521 DO(tzh_ttisgmtcnt);
1522 DO(tzh_ttisstdcnt);
1523 DO(tzh_leapcnt);
1524 DO(tzh_timecnt);
1525 DO(tzh_typecnt);
1526 DO(tzh_charcnt);
1527 #undef DO
1528 for (i = 0; i < timecnt; ++i) {
1529 j = leapcnt;
1530 while (--j >= 0)
1531 if (ats[i] >= trans[j]) {
1532 ats[i] = tadd(ats[i], corr[j]);
1533 break;
1535 puttzcode((long) ats[i], fp);
1537 if (timecnt > 0)
1538 (void) fwrite((void *) types, (size_t) sizeof types[0],
1539 (size_t) timecnt, fp);
1540 for (i = 0; i < typecnt; ++i) {
1541 puttzcode((long) gmtoffs[i], fp);
1542 (void) putc(isdsts[i], fp);
1543 (void) putc(abbrinds[i], fp);
1545 if (charcnt != 0)
1546 (void) fwrite((void *) chars, (size_t) sizeof chars[0],
1547 (size_t) charcnt, fp);
1548 for (i = 0; i < leapcnt; ++i) {
1549 if (roll[i]) {
1550 if (timecnt == 0 || trans[i] < ats[0]) {
1551 j = 0;
1552 while (isdsts[j])
1553 if (++j >= typecnt) {
1554 j = 0;
1555 break;
1557 } else {
1558 j = 1;
1559 while (j < timecnt && trans[i] >= ats[j])
1560 ++j;
1561 j = types[j - 1];
1563 puttzcode((long) tadd(trans[i], -gmtoffs[j]), fp);
1564 } else puttzcode((long) trans[i], fp);
1565 puttzcode((long) corr[i], fp);
1567 for (i = 0; i < typecnt; ++i)
1568 (void) putc(ttisstds[i], fp);
1569 for (i = 0; i < typecnt; ++i)
1570 (void) putc(ttisgmts[i], fp);
1571 if (ferror(fp) || fclose(fp)) {
1572 (void) fprintf(stderr, _("%s: Error writing %s\n"),
1573 progname, fullname);
1574 (void) exit(EXIT_FAILURE);
1578 static void
1579 doabbr(abbr, format, letters, isdst)
1580 char * const abbr;
1581 const char * const format;
1582 const char * const letters;
1583 const int isdst;
1585 if (strchr(format, '/') == NULL) {
1586 if (letters == NULL)
1587 (void) strcpy(abbr, format);
1588 else (void) sprintf(abbr, format, letters);
1589 } else if (isdst)
1590 (void) strcpy(abbr, strchr(format, '/') + 1);
1591 else {
1592 (void) strcpy(abbr, format);
1593 *strchr(abbr, '/') = '\0';
1597 static void
1598 outzone(zpfirst, zonecount)
1599 const struct zone * const zpfirst;
1600 const int zonecount;
1602 register const struct zone * zp;
1603 register struct rule * rp;
1604 register int i, j;
1605 register int usestart, useuntil;
1606 register time_t starttime, untiltime;
1607 register long gmtoff;
1608 register long stdoff;
1609 register int year;
1610 register long startoff;
1611 register int startttisstd;
1612 register int startttisgmt;
1613 register int type;
1614 char startbuf[BUFSIZ];
1616 INITIALIZE(untiltime);
1617 INITIALIZE(starttime);
1619 ** Now. . .finally. . .generate some useful data!
1621 timecnt = 0;
1622 typecnt = 0;
1623 charcnt = 0;
1625 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1626 ** for noting the need to unconditionally initialize startttisstd.
1628 startttisstd = FALSE;
1629 startttisgmt = FALSE;
1630 for (i = 0; i < zonecount; ++i) {
1632 ** A guess that may well be corrected later.
1634 stdoff = 0;
1635 zp = &zpfirst[i];
1636 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
1637 useuntil = i < (zonecount - 1);
1638 if (useuntil && zp->z_untiltime <= min_time)
1639 continue;
1640 gmtoff = zp->z_gmtoff;
1641 eat(zp->z_filename, zp->z_linenum);
1642 *startbuf = '\0';
1643 startoff = zp->z_gmtoff;
1644 if (zp->z_nrules == 0) {
1645 stdoff = zp->z_stdoff;
1646 doabbr(startbuf, zp->z_format,
1647 (char *) NULL, stdoff != 0);
1648 type = addtype(oadd(zp->z_gmtoff, stdoff),
1649 startbuf, stdoff != 0, startttisstd,
1650 startttisgmt);
1651 if (usestart) {
1652 addtt(starttime, type);
1653 usestart = FALSE;
1654 } else if (stdoff != 0)
1655 addtt(min_time, type);
1656 } else for (year = min_year; year <= max_year; ++year) {
1657 if (useuntil && year > zp->z_untilrule.r_hiyear)
1658 break;
1660 ** Mark which rules to do in the current year.
1661 ** For those to do, calculate rpytime(rp, year);
1663 for (j = 0; j < zp->z_nrules; ++j) {
1664 rp = &zp->z_rules[j];
1665 eats(zp->z_filename, zp->z_linenum,
1666 rp->r_filename, rp->r_linenum);
1667 rp->r_todo = year >= rp->r_loyear &&
1668 year <= rp->r_hiyear &&
1669 yearistype(year, rp->r_yrtype);
1670 if (rp->r_todo)
1671 rp->r_temp = rpytime(rp, year);
1673 for ( ; ; ) {
1674 register int k;
1675 register time_t jtime, ktime;
1676 register long offset;
1677 char buf[BUFSIZ];
1679 INITIALIZE(ktime);
1680 if (useuntil) {
1682 ** Turn untiltime into UTC
1683 ** assuming the current gmtoff and
1684 ** stdoff values.
1686 untiltime = zp->z_untiltime;
1687 if (!zp->z_untilrule.r_todisgmt)
1688 untiltime = tadd(untiltime,
1689 -gmtoff);
1690 if (!zp->z_untilrule.r_todisstd)
1691 untiltime = tadd(untiltime,
1692 -stdoff);
1695 ** Find the rule (of those to do, if any)
1696 ** that takes effect earliest in the year.
1698 k = -1;
1699 for (j = 0; j < zp->z_nrules; ++j) {
1700 rp = &zp->z_rules[j];
1701 if (!rp->r_todo)
1702 continue;
1703 eats(zp->z_filename, zp->z_linenum,
1704 rp->r_filename, rp->r_linenum);
1705 offset = rp->r_todisgmt ? 0 : gmtoff;
1706 if (!rp->r_todisstd)
1707 offset = oadd(offset, stdoff);
1708 jtime = rp->r_temp;
1709 if (jtime == min_time ||
1710 jtime == max_time)
1711 continue;
1712 jtime = tadd(jtime, -offset);
1713 if (k < 0 || jtime < ktime) {
1714 k = j;
1715 ktime = jtime;
1718 if (k < 0)
1719 break; /* go on to next year */
1720 rp = &zp->z_rules[k];
1721 rp->r_todo = FALSE;
1722 if (useuntil && ktime >= untiltime)
1723 break;
1724 stdoff = rp->r_stdoff;
1725 if (usestart && ktime == starttime)
1726 usestart = FALSE;
1727 if (usestart) {
1728 if (ktime < starttime) {
1729 startoff = oadd(zp->z_gmtoff,
1730 stdoff);
1731 doabbr(startbuf, zp->z_format,
1732 rp->r_abbrvar,
1733 rp->r_stdoff != 0);
1734 continue;
1736 if (*startbuf == '\0' &&
1737 startoff == oadd(zp->z_gmtoff,
1738 stdoff)) {
1739 doabbr(startbuf, zp->z_format,
1740 rp->r_abbrvar,
1741 rp->r_stdoff != 0);
1744 eats(zp->z_filename, zp->z_linenum,
1745 rp->r_filename, rp->r_linenum);
1746 doabbr(buf, zp->z_format, rp->r_abbrvar,
1747 rp->r_stdoff != 0);
1748 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
1749 type = addtype(offset, buf, rp->r_stdoff != 0,
1750 rp->r_todisstd, rp->r_todisgmt);
1751 addtt(ktime, type);
1754 if (usestart) {
1755 if (*startbuf == '\0' &&
1756 zp->z_format != NULL &&
1757 strchr(zp->z_format, '%') == NULL &&
1758 strchr(zp->z_format, '/') == NULL)
1759 (void) strcpy(startbuf, zp->z_format);
1760 eat(zp->z_filename, zp->z_linenum);
1761 if (*startbuf == '\0')
1762 error(_("can't determine time zone abbreviation to use just after until time"));
1763 else addtt(starttime,
1764 addtype(startoff, startbuf,
1765 startoff != zp->z_gmtoff,
1766 startttisstd,
1767 startttisgmt));
1770 ** Now we may get to set starttime for the next zone line.
1772 if (useuntil) {
1773 startttisstd = zp->z_untilrule.r_todisstd;
1774 startttisgmt = zp->z_untilrule.r_todisgmt;
1775 starttime = zp->z_untiltime;
1776 if (!startttisstd)
1777 starttime = tadd(starttime, -stdoff);
1778 if (!startttisgmt)
1779 starttime = tadd(starttime, -gmtoff);
1782 writezone(zpfirst->z_name);
1785 static void
1786 addtt(starttime, type)
1787 const time_t starttime;
1788 int type;
1790 if (starttime <= min_time ||
1791 (timecnt == 1 && attypes[0].at < min_time)) {
1792 gmtoffs[0] = gmtoffs[type];
1793 isdsts[0] = isdsts[type];
1794 ttisstds[0] = ttisstds[type];
1795 ttisgmts[0] = ttisgmts[type];
1796 if (abbrinds[type] != 0)
1797 (void) strcpy(chars, &chars[abbrinds[type]]);
1798 abbrinds[0] = 0;
1799 charcnt = strlen(chars) + 1;
1800 typecnt = 1;
1801 timecnt = 0;
1802 type = 0;
1804 if (timecnt >= TZ_MAX_TIMES) {
1805 error(_("too many transitions?!"));
1806 (void) exit(EXIT_FAILURE);
1808 attypes[timecnt].at = starttime;
1809 attypes[timecnt].type = type;
1810 ++timecnt;
1813 static int
1814 addtype(gmtoff, abbr, isdst, ttisstd, ttisgmt)
1815 const long gmtoff;
1816 const char * const abbr;
1817 const int isdst;
1818 const int ttisstd;
1819 const int ttisgmt;
1821 register int i, j;
1823 if (isdst != TRUE && isdst != FALSE) {
1824 error(_("internal error - addtype called with bad isdst"));
1825 (void) exit(EXIT_FAILURE);
1827 if (ttisstd != TRUE && ttisstd != FALSE) {
1828 error(_("internal error - addtype called with bad ttisstd"));
1829 (void) exit(EXIT_FAILURE);
1831 if (ttisgmt != TRUE && ttisgmt != FALSE) {
1832 error(_("internal error - addtype called with bad ttisgmt"));
1833 (void) exit(EXIT_FAILURE);
1836 ** See if there's already an entry for this zone type.
1837 ** If so, just return its index.
1839 for (i = 0; i < typecnt; ++i) {
1840 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
1841 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
1842 ttisstd == ttisstds[i] &&
1843 ttisgmt == ttisgmts[i])
1844 return i;
1847 ** There isn't one; add a new one, unless there are already too
1848 ** many.
1850 if (typecnt >= TZ_MAX_TYPES) {
1851 error(_("too many local time types"));
1852 (void) exit(EXIT_FAILURE);
1854 gmtoffs[i] = gmtoff;
1855 isdsts[i] = isdst;
1856 ttisstds[i] = ttisstd;
1857 ttisgmts[i] = ttisgmt;
1859 for (j = 0; j < charcnt; ++j)
1860 if (strcmp(&chars[j], abbr) == 0)
1861 break;
1862 if (j == charcnt)
1863 newabbr(abbr);
1864 abbrinds[i] = j;
1865 ++typecnt;
1866 return i;
1869 static void
1870 leapadd(t, positive, rolling, count)
1871 const time_t t;
1872 const int positive;
1873 const int rolling;
1874 int count;
1876 register int i, j;
1878 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
1879 error(_("too many leap seconds"));
1880 (void) exit(EXIT_FAILURE);
1882 for (i = 0; i < leapcnt; ++i)
1883 if (t <= trans[i]) {
1884 if (t == trans[i]) {
1885 error(_("repeated leap second moment"));
1886 (void) exit(EXIT_FAILURE);
1888 break;
1890 do {
1891 for (j = leapcnt; j > i; --j) {
1892 trans[j] = trans[j - 1];
1893 corr[j] = corr[j - 1];
1894 roll[j] = roll[j - 1];
1896 trans[i] = t;
1897 corr[i] = positive ? 1L : eitol(-count);
1898 roll[i] = rolling;
1899 ++leapcnt;
1900 } while (positive && --count != 0);
1903 static void
1904 adjleap P((void))
1906 register int i;
1907 register long last = 0;
1910 ** propagate leap seconds forward
1912 for (i = 0; i < leapcnt; ++i) {
1913 trans[i] = tadd(trans[i], last);
1914 last = corr[i] += last;
1918 static int
1919 yearistype(year, type)
1920 const int year;
1921 const char * const type;
1923 static char * buf;
1924 int result;
1926 if (type == NULL || *type == '\0')
1927 return TRUE;
1928 buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
1929 (void) sprintf(buf, "%s %d %s", yitcommand, year, type);
1930 result = system(buf);
1931 if (WIFEXITED(result)) switch (WEXITSTATUS(result)) {
1932 case 0:
1933 return TRUE;
1934 case 1:
1935 return FALSE;
1937 error(_("Wild result from command execution"));
1938 (void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
1939 progname, buf, result);
1940 for ( ; ; )
1941 (void) exit(EXIT_FAILURE);
1944 static int
1945 lowerit(a)
1946 int a;
1948 a = (unsigned char) a;
1949 return (isascii(a) && isupper(a)) ? tolower(a) : a;
1952 static int
1953 ciequal(ap, bp) /* case-insensitive equality */
1954 register const char * ap;
1955 register const char * bp;
1957 while (lowerit(*ap) == lowerit(*bp++))
1958 if (*ap++ == '\0')
1959 return TRUE;
1960 return FALSE;
1963 static int
1964 itsabbr(abbr, word)
1965 register const char * abbr;
1966 register const char * word;
1968 if (lowerit(*abbr) != lowerit(*word))
1969 return FALSE;
1970 ++word;
1971 while (*++abbr != '\0')
1972 do {
1973 if (*word == '\0')
1974 return FALSE;
1975 } while (lowerit(*word++) != lowerit(*abbr));
1976 return TRUE;
1979 static const struct lookup *
1980 byword(word, table)
1981 register const char * const word;
1982 register const struct lookup * const table;
1984 register const struct lookup * foundlp;
1985 register const struct lookup * lp;
1987 if (word == NULL || table == NULL)
1988 return NULL;
1990 ** Look for exact match.
1992 for (lp = table; lp->l_word != NULL; ++lp)
1993 if (ciequal(word, lp->l_word))
1994 return lp;
1996 ** Look for inexact match.
1998 foundlp = NULL;
1999 for (lp = table; lp->l_word != NULL; ++lp)
2000 if (itsabbr(word, lp->l_word)) {
2001 if (foundlp == NULL)
2002 foundlp = lp;
2003 else return NULL; /* multiple inexact matches */
2005 return foundlp;
2008 static char **
2009 getfields(cp)
2010 register char * cp;
2012 register char * dp;
2013 register char ** array;
2014 register int nsubs;
2016 if (cp == NULL)
2017 return NULL;
2018 array = (char **) (void *)
2019 emalloc((int) ((strlen(cp) + 1) * sizeof *array));
2020 nsubs = 0;
2021 for ( ; ; ) {
2022 while (isascii(*cp) && isspace((unsigned char) *cp))
2023 ++cp;
2024 if (*cp == '\0' || *cp == '#')
2025 break;
2026 array[nsubs++] = dp = cp;
2027 do {
2028 if ((*dp = *cp++) != '"')
2029 ++dp;
2030 else while ((*dp = *cp++) != '"')
2031 if (*dp != '\0')
2032 ++dp;
2033 else error(_("Odd number of quotation marks"));
2034 } while (*cp != '\0' && *cp != '#' &&
2035 (!isascii(*cp) || !isspace((unsigned char) *cp)));
2036 if (isascii(*cp) && isspace((unsigned char) *cp))
2037 ++cp;
2038 *dp = '\0';
2040 array[nsubs] = NULL;
2041 return array;
2044 static long
2045 oadd(t1, t2)
2046 const long t1;
2047 const long t2;
2049 register long t;
2051 t = t1 + t2;
2052 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2053 error(_("time overflow"));
2054 (void) exit(EXIT_FAILURE);
2056 return t;
2059 static time_t
2060 tadd(t1, t2)
2061 const time_t t1;
2062 const long t2;
2064 register time_t t;
2066 if (t1 == max_time && t2 > 0)
2067 return max_time;
2068 if (t1 == min_time && t2 < 0)
2069 return min_time;
2070 t = t1 + t2;
2071 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2072 error(_("time overflow"));
2073 (void) exit(EXIT_FAILURE);
2075 return t;
2079 ** Given a rule, and a year, compute the date - in seconds since January 1,
2080 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2083 static time_t
2084 rpytime(rp, wantedy)
2085 register const struct rule * const rp;
2086 register const int wantedy;
2088 register int y, m, i;
2089 register long dayoff; /* with a nod to Margaret O. */
2090 register time_t t;
2092 if (wantedy == INT_MIN)
2093 return min_time;
2094 if (wantedy == INT_MAX)
2095 return max_time;
2096 dayoff = 0;
2097 m = TM_JANUARY;
2098 y = EPOCH_YEAR;
2099 while (wantedy != y) {
2100 if (wantedy > y) {
2101 i = len_years[isleap(y)];
2102 ++y;
2103 } else {
2104 --y;
2105 i = -len_years[isleap(y)];
2107 dayoff = oadd(dayoff, eitol(i));
2109 while (m != rp->r_month) {
2110 i = len_months[isleap(y)][m];
2111 dayoff = oadd(dayoff, eitol(i));
2112 ++m;
2114 i = rp->r_dayofmonth;
2115 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
2116 if (rp->r_dycode == DC_DOWLEQ)
2117 --i;
2118 else {
2119 error(_("use of 2/29 in non leap-year"));
2120 (void) exit(EXIT_FAILURE);
2123 --i;
2124 dayoff = oadd(dayoff, eitol(i));
2125 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
2126 register long wday;
2128 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2129 wday = eitol(EPOCH_WDAY);
2131 ** Don't trust mod of negative numbers.
2133 if (dayoff >= 0)
2134 wday = (wday + dayoff) % LDAYSPERWEEK;
2135 else {
2136 wday -= ((-dayoff) % LDAYSPERWEEK);
2137 if (wday < 0)
2138 wday += LDAYSPERWEEK;
2140 while (wday != eitol(rp->r_wday))
2141 if (rp->r_dycode == DC_DOWGEQ) {
2142 dayoff = oadd(dayoff, (long) 1);
2143 if (++wday >= LDAYSPERWEEK)
2144 wday = 0;
2145 ++i;
2146 } else {
2147 dayoff = oadd(dayoff, (long) -1);
2148 if (--wday < 0)
2149 wday = LDAYSPERWEEK - 1;
2150 --i;
2152 if (i < 0 || i >= len_months[isleap(y)][m]) {
2153 if (noise)
2154 warning(_("rule goes past start/end of month--will not work with pre-2004 versions of zic"));
2157 if (dayoff < 0 && !TYPE_SIGNED(time_t))
2158 return min_time;
2159 if (dayoff < min_time / SECSPERDAY)
2160 return min_time;
2161 if (dayoff > max_time / SECSPERDAY)
2162 return max_time;
2163 t = (time_t) dayoff * SECSPERDAY;
2164 if (t > 0 && max_time - t < rp->r_tod)
2165 return max_time;
2166 return tadd(t, rp->r_tod);
2169 static void
2170 newabbr(string)
2171 const char * const string;
2173 register int i;
2175 i = strlen(string) + 1;
2176 if (charcnt + i > TZ_MAX_CHARS) {
2177 error(_("too many, or too long, time zone abbreviations"));
2178 (void) exit(EXIT_FAILURE);
2180 (void) strcpy(&chars[charcnt], string);
2181 charcnt += eitol(i);
2184 static int
2185 mkdirs(argname)
2186 char * const argname;
2188 register char * name;
2189 register char * cp;
2191 if (argname == NULL || *argname == '\0')
2192 return 0;
2193 cp = name = ecpyalloc(argname);
2194 while ((cp = strchr(cp + 1, '/')) != 0) {
2195 *cp = '\0';
2196 #ifndef unix
2198 ** DOS drive specifier?
2200 if (isalpha((unsigned char) name[0]) &&
2201 name[1] == ':' && name[2] == '\0') {
2202 *cp = '/';
2203 continue;
2205 #endif /* !defined unix */
2206 if (!itsdir(name)) {
2208 ** It doesn't seem to exist, so we try to create it.
2209 ** Creation may fail because of the directory being
2210 ** created by some other multiprocessor, so we get
2211 ** to do extra checking.
2213 if (mkdir(name, MKDIR_UMASK) != 0) {
2214 const char *e = strerror(errno);
2216 if (errno != EEXIST || !itsdir(name)) {
2217 (void) fprintf(stderr,
2218 _("%s: Can't create directory %s: %s\n"),
2219 progname, name, e);
2220 ifree(name);
2221 return -1;
2225 *cp = '/';
2227 ifree(name);
2228 return 0;
2231 static long
2232 eitol(i)
2233 const int i;
2235 long l;
2237 l = i;
2238 if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0)) {
2239 (void) fprintf(stderr,
2240 _("%s: %d did not sign extend correctly\n"),
2241 progname, i);
2242 (void) exit(EXIT_FAILURE);
2244 return l;
2248 ** UNIX was a registered trademark of The Open Group in 2003.