original netbsd's tr(1)
[minix.git] / commands / zoneinfo / zic.c
blobcd2b3b735b90867ed9dfe67ace2fdcd1499107b8
1 static char elsieid[] = "@(#)zic.c 7.128";
3 /*
4 ** Regardless of the type of time_t, we do our work using this type.
5 */
7 typedef int zic_t;
9 #include "private.h"
10 #include "locale.h"
11 #include "tzfile.h"
13 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
14 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
15 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
17 #if HAVE_SYS_STAT_H
18 #include "sys/stat.h"
19 #endif
20 #ifdef S_IRUSR
21 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
22 #else
23 #define MKDIR_UMASK 0755
24 #endif
27 ** On some ancient hosts, predicates like `isspace(C)' are defined
28 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
29 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
30 ** Neither the C Standard nor Posix require that `isascii' exist.
31 ** For portability, we check both ancient and modern requirements.
32 ** If isascii is not defined, the isascii check succeeds trivially.
34 #include "ctype.h"
35 #ifndef isascii
36 #define isascii(x) 1
37 #endif
39 struct rule {
40 const char * r_filename;
41 int r_linenum;
42 const char * r_name;
44 int r_loyear; /* for example, 1986 */
45 int r_hiyear; /* for example, 1986 */
46 const char * r_yrtype;
48 int r_month; /* 0..11 */
50 int r_dycode; /* see below */
51 int r_dayofmonth;
52 int r_wday;
54 long r_tod; /* time from midnight */
55 int r_todisstd; /* above is standard time if TRUE */
56 /* or wall clock time if FALSE */
57 int r_todisgmt; /* above is GMT if TRUE */
58 /* or local time if FALSE */
59 long r_stdoff; /* offset from standard time */
60 const char * r_abbrvar; /* variable part of abbreviation */
62 int r_todo; /* a rule to do (used in outzone) */
63 zic_t r_temp; /* used in outzone */
67 ** r_dycode r_dayofmonth r_wday
70 #define DC_DOM 0 /* 1..31 */ /* unused */
71 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
72 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
74 struct zone {
75 const char * z_filename;
76 int z_linenum;
78 const char * z_name;
79 long z_gmtoff;
80 const char * z_rule;
81 const char * z_format;
83 long z_stdoff;
85 struct rule * z_rules;
86 int z_nrules;
88 struct rule z_untilrule;
89 zic_t z_untiltime;
92 extern int getopt P((int argc, char * const argv[],
93 const char * options));
94 extern int link P((const char * fromname, const char * toname));
95 extern char * optarg;
96 extern int optind;
98 static void addtt P((zic_t starttime, int type));
99 static int addtype P((long gmtoff, const char * abbr, int isdst,
100 int ttisstd, int ttisgmt));
101 static void leapadd P((zic_t t, int positive, int rolling, int count));
102 static void adjleap P((void));
103 static void associate P((void));
104 static int ciequal P((const char * ap, const char * bp));
105 static void convert P((long val, char * buf));
106 static void dolink P((const char * fromfile, const char * tofile));
107 static void doabbr P((char * abbr, const char * format,
108 const char * letters, int isdst));
109 static void eat P((const char * name, int num));
110 static void eats P((const char * name, int num,
111 const char * rname, int rnum));
112 static long eitol P((int i));
113 static void error P((const char * message));
114 static char ** getfields P((char * buf));
115 static long gethms P((const char * string, const char * errstrng,
116 int signable));
117 static void infile P((const char * filename));
118 static void inleap P((char ** fields, int nfields));
119 static void inlink P((char ** fields, int nfields));
120 static void inrule P((char ** fields, int nfields));
121 static int inzcont P((char ** fields, int nfields));
122 static int inzone P((char ** fields, int nfields));
123 static int inzsub P((char ** fields, int nfields, int iscont));
124 static int itsabbr P((const char * abbr, const char * word));
125 static int itsdir P((const char * name));
126 static int lowerit P((int c));
127 static char * memcheck P((char * tocheck));
128 static int mkdirs P((char * filename));
129 static void newabbr P((const char * abbr));
130 static long oadd P((long t1, long t2));
131 static void outzone P((const struct zone * zp, int ntzones));
132 static void puttzcode P((long code, FILE * fp));
133 static int rcomp P((const void * leftp, const void * rightp));
134 static zic_t rpytime P((const struct rule * rp, int wantedy));
135 static void rulesub P((struct rule * rp,
136 const char * loyearp, const char * hiyearp,
137 const char * typep, const char * monthp,
138 const char * dayp, const char * timep));
139 static void setboundaries P((void));
140 static zic_t tadd P((zic_t t1, long t2));
141 static void usage P((void));
142 static void writezone P((const char * name));
143 static int yearistype P((int year, const char * type));
145 #if !HAVE_STRERROR
146 static char * strerror P((int));
147 #endif /* !HAVE_STRERROR */
149 static int charcnt;
150 static int errors;
151 static const char * filename;
152 static int leapcnt;
153 static int linenum;
154 static zic_t max_time;
155 static int max_year;
156 static int max_year_representable;
157 static zic_t min_time;
158 static int min_year;
159 static int min_year_representable;
160 static int noise;
161 static const char * rfilename;
162 static int rlinenum;
163 static const char * progname;
164 static int timecnt;
165 static int typecnt;
168 ** Line codes.
171 #define LC_RULE 0
172 #define LC_ZONE 1
173 #define LC_LINK 2
174 #define LC_LEAP 3
177 ** Which fields are which on a Zone line.
180 #define ZF_NAME 1
181 #define ZF_GMTOFF 2
182 #define ZF_RULE 3
183 #define ZF_FORMAT 4
184 #define ZF_TILYEAR 5
185 #define ZF_TILMONTH 6
186 #define ZF_TILDAY 7
187 #define ZF_TILTIME 8
188 #define ZONE_MINFIELDS 5
189 #define ZONE_MAXFIELDS 9
192 ** Which fields are which on a Zone continuation line.
195 #define ZFC_GMTOFF 0
196 #define ZFC_RULE 1
197 #define ZFC_FORMAT 2
198 #define ZFC_TILYEAR 3
199 #define ZFC_TILMONTH 4
200 #define ZFC_TILDAY 5
201 #define ZFC_TILTIME 6
202 #define ZONEC_MINFIELDS 3
203 #define ZONEC_MAXFIELDS 7
206 ** Which files are which on a Rule line.
209 #define RF_NAME 1
210 #define RF_LOYEAR 2
211 #define RF_HIYEAR 3
212 #define RF_COMMAND 4
213 #define RF_MONTH 5
214 #define RF_DAY 6
215 #define RF_TOD 7
216 #define RF_STDOFF 8
217 #define RF_ABBRVAR 9
218 #define RULE_FIELDS 10
221 ** Which fields are which on a Link line.
224 #define LF_FROM 1
225 #define LF_TO 2
226 #define LINK_FIELDS 3
229 ** Which fields are which on a Leap line.
232 #define LP_YEAR 1
233 #define LP_MONTH 2
234 #define LP_DAY 3
235 #define LP_TIME 4
236 #define LP_CORR 5
237 #define LP_ROLL 6
238 #define LEAP_FIELDS 7
241 ** Year synonyms.
244 #define YR_MINIMUM 0
245 #define YR_MAXIMUM 1
246 #define YR_ONLY 2
248 static struct rule * rules;
249 static int nrules; /* number of rules */
251 static struct zone * zones;
252 static int nzones; /* number of zones */
254 struct link {
255 const char * l_filename;
256 int l_linenum;
257 const char * l_from;
258 const char * l_to;
261 static struct link * links;
262 static int nlinks;
264 struct lookup {
265 const char * l_word;
266 const int l_value;
269 static struct lookup const * byword P((const char * string,
270 const struct lookup * lp));
272 static struct lookup const line_codes[] = {
273 { "Rule", LC_RULE },
274 { "Zone", LC_ZONE },
275 { "Link", LC_LINK },
276 { "Leap", LC_LEAP },
277 { NULL, 0}
280 static struct lookup const mon_names[] = {
281 { "January", TM_JANUARY },
282 { "February", TM_FEBRUARY },
283 { "March", TM_MARCH },
284 { "April", TM_APRIL },
285 { "May", TM_MAY },
286 { "June", TM_JUNE },
287 { "July", TM_JULY },
288 { "August", TM_AUGUST },
289 { "September", TM_SEPTEMBER },
290 { "October", TM_OCTOBER },
291 { "November", TM_NOVEMBER },
292 { "December", TM_DECEMBER },
293 { NULL, 0 }
296 static struct lookup const wday_names[] = {
297 { "Sunday", TM_SUNDAY },
298 { "Monday", TM_MONDAY },
299 { "Tuesday", TM_TUESDAY },
300 { "Wednesday", TM_WEDNESDAY },
301 { "Thursday", TM_THURSDAY },
302 { "Friday", TM_FRIDAY },
303 { "Saturday", TM_SATURDAY },
304 { NULL, 0 }
307 static struct lookup const lasts[] = {
308 { "last-Sunday", TM_SUNDAY },
309 { "last-Monday", TM_MONDAY },
310 { "last-Tuesday", TM_TUESDAY },
311 { "last-Wednesday", TM_WEDNESDAY },
312 { "last-Thursday", TM_THURSDAY },
313 { "last-Friday", TM_FRIDAY },
314 { "last-Saturday", TM_SATURDAY },
315 { NULL, 0 }
318 static struct lookup const begin_years[] = {
319 { "minimum", YR_MINIMUM },
320 { "maximum", YR_MAXIMUM },
321 { NULL, 0 }
324 static struct lookup const end_years[] = {
325 { "minimum", YR_MINIMUM },
326 { "maximum", YR_MAXIMUM },
327 { "only", YR_ONLY },
328 { NULL, 0 }
331 static struct lookup const leap_types[] = {
332 { "Rolling", TRUE },
333 { "Stationary", FALSE },
334 { NULL, 0 }
337 static const int len_months[2][MONSPERYEAR] = {
338 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
339 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
342 static const int len_years[2] = {
343 DAYSPERNYEAR, DAYSPERLYEAR
346 static struct attype {
347 zic_t at;
348 unsigned char type;
349 } attypes[TZ_MAX_TIMES];
350 static long gmtoffs[TZ_MAX_TYPES];
351 static char isdsts[TZ_MAX_TYPES];
352 static unsigned char abbrinds[TZ_MAX_TYPES];
353 static char ttisstds[TZ_MAX_TYPES];
354 static char ttisgmts[TZ_MAX_TYPES];
355 static char chars[TZ_MAX_CHARS];
356 static zic_t trans[TZ_MAX_LEAPS];
357 static long corr[TZ_MAX_LEAPS];
358 static char roll[TZ_MAX_LEAPS];
361 ** Memory allocation.
364 static char *
365 memcheck(ptr)
366 char * const ptr;
368 if (ptr == NULL) {
369 const char *e = strerror(errno);
371 (void) fprintf(stderr, _("%s: Memory exhausted: %s\n"),
372 progname, e);
373 exit(EXIT_FAILURE);
375 return ptr;
378 #define emalloc(size) memcheck(imalloc(size))
379 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
380 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
381 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
384 ** Error handling.
387 #if !HAVE_STRERROR
388 static char *
389 strerror(errnum)
390 int errnum;
392 extern char * sys_errlist[];
393 extern int sys_nerr;
395 return (errnum > 0 && errnum <= sys_nerr) ?
396 sys_errlist[errnum] : _("Unknown system error");
398 #endif /* !HAVE_STRERROR */
400 static void
401 eats(name, num, rname, rnum)
402 const char * const name;
403 const int num;
404 const char * const rname;
405 const int rnum;
407 filename = name;
408 linenum = num;
409 rfilename = rname;
410 rlinenum = rnum;
413 static void
414 eat(name, num)
415 const char * const name;
416 const int num;
418 eats(name, num, (char *) NULL, -1);
421 static void
422 error(string)
423 const char * const string;
426 ** Match the format of "cc" to allow sh users to
427 ** zic ... 2>&1 | error -t "*" -v
428 ** on BSD systems.
430 (void) fprintf(stderr, _("\"%s\", line %d: %s"),
431 filename, linenum, string);
432 if (rfilename != NULL)
433 (void) fprintf(stderr, _(" (rule from \"%s\", line %d)"),
434 rfilename, rlinenum);
435 (void) fprintf(stderr, "\n");
436 ++errors;
439 static void
440 warning(const char * const string)
442 char * cp;
444 cp = ecpyalloc(_("warning: "));
445 cp = ecatalloc(cp, string);
446 error(cp);
447 ifree(cp);
448 --errors;
451 static void
452 usage P((void))
454 (void) fprintf(stderr, _("%s: usage is %s \
455 [ --version ] [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\
456 \t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
457 progname, progname);
458 exit(EXIT_FAILURE);
461 static const char * psxrules;
462 static const char * lcltime;
463 static const char * directory;
464 static const char * leapsec;
465 static const char * yitcommand;
466 static int sflag = FALSE;
469 main( int argc, char * argv[])
471 register int i;
472 register int j;
473 register int c;
475 #ifdef unix
476 (void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
477 #endif /* defined unix */
478 #if HAVE_GETTEXT
479 (void) setlocale(LC_ALL, "");
480 #ifdef TZ_DOMAINDIR
481 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
482 #endif /* defined TEXTDOMAINDIR */
483 (void) textdomain(TZ_DOMAIN);
484 #endif /* HAVE_GETTEXT */
485 progname = argv[0];
486 for (i = 1; i < argc; ++i)
487 if (strcmp(argv[i], "--version") == 0) {
488 (void) printf("%s\n", elsieid);
489 exit(EXIT_SUCCESS);
491 while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF && c != -1)
492 switch (c) {
493 default:
494 usage();
495 case 'd':
496 if (directory == NULL)
497 directory = optarg;
498 else {
499 (void) fprintf(stderr,
500 _("%s: More than one -d option specified\n"),
501 progname);
502 exit(EXIT_FAILURE);
504 break;
505 case 'l':
506 if (lcltime == NULL)
507 lcltime = optarg;
508 else {
509 (void) fprintf(stderr,
510 _("%s: More than one -l option specified\n"),
511 progname);
512 exit(EXIT_FAILURE);
514 break;
515 case 'p':
516 if (psxrules == NULL)
517 psxrules = optarg;
518 else {
519 (void) fprintf(stderr,
520 _("%s: More than one -p option specified\n"),
521 progname);
522 exit(EXIT_FAILURE);
524 break;
525 case 'y':
526 if (yitcommand == NULL)
527 yitcommand = optarg;
528 else {
529 (void) fprintf(stderr,
530 _("%s: More than one -y option specified\n"),
531 progname);
532 exit(EXIT_FAILURE);
534 break;
535 case 'L':
536 if (leapsec == NULL)
537 leapsec = optarg;
538 else {
539 (void) fprintf(stderr,
540 _("%s: More than one -L option specified\n"),
541 progname);
542 exit(EXIT_FAILURE);
544 break;
545 case 'v':
546 noise = TRUE;
547 break;
548 case 's':
549 sflag = TRUE;
550 break;
552 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
553 usage(); /* usage message by request */
554 if (directory == NULL)
555 directory = TZDIR;
556 if (yitcommand == NULL)
557 yitcommand = "yearistype";
559 setboundaries();
561 if (optind < argc && leapsec != NULL) {
562 infile(leapsec);
563 adjleap();
566 for (i = optind; i < argc; ++i)
567 infile(argv[i]);
568 if (errors)
569 exit(EXIT_FAILURE);
570 associate();
571 for (i = 0; i < nzones; i = j) {
573 ** Find the next non-continuation zone entry.
575 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
576 continue;
577 outzone(&zones[i], j - i);
580 ** Make links.
582 for (i = 0; i < nlinks; ++i) {
583 eat(links[i].l_filename, links[i].l_linenum);
584 dolink(links[i].l_from, links[i].l_to);
585 if (noise)
586 for (j = 0; j < nlinks; ++j)
587 if (strcmp(links[i].l_to,
588 links[j].l_from) == 0)
589 warning(_("link to link"));
591 if (lcltime != NULL) {
592 eat("command line", 1);
593 dolink(lcltime, TZDEFAULT);
595 if (psxrules != NULL) {
596 eat("command line", 1);
597 dolink(psxrules, TZDEFRULES);
599 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
602 static void
603 dolink(fromfile, tofile)
604 const char * const fromfile;
605 const char * const tofile;
607 register char * fromname;
608 register char * toname;
610 if (fromfile[0] == '/')
611 fromname = ecpyalloc(fromfile);
612 else {
613 fromname = ecpyalloc(directory);
614 fromname = ecatalloc(fromname, "/");
615 fromname = ecatalloc(fromname, fromfile);
617 if (tofile[0] == '/')
618 toname = ecpyalloc(tofile);
619 else {
620 toname = ecpyalloc(directory);
621 toname = ecatalloc(toname, "/");
622 toname = ecatalloc(toname, tofile);
625 ** We get to be careful here since
626 ** there's a fair chance of root running us.
628 if (!itsdir(toname))
629 (void) remove(toname);
630 if (link(fromname, toname) != 0) {
631 int result;
633 if (mkdirs(toname) != 0)
634 exit(EXIT_FAILURE);
636 result = link(fromname, toname);
637 #if HAVE_SYMLINK
638 if (result != 0 &&
639 access(fromname, F_OK) == 0 &&
640 !itsdir(fromname)) {
641 const char *s = tofile;
642 register char * symlinkcontents = NULL;
644 while ((s = strchr(s+1, '/')) != NULL)
645 symlinkcontents =
646 ecatalloc(symlinkcontents,
647 "../");
648 symlinkcontents =
649 ecatalloc(symlinkcontents,
650 fromfile);
651 result = symlink(symlinkcontents,
652 toname);
653 if (result == 0)
654 warning(_("hard link failed, symbolic link used"));
655 ifree(symlinkcontents);
657 #endif /* HAVE_SYMLINK */
658 if (result != 0) {
659 const char *e = strerror(errno);
661 (void) fprintf(stderr,
662 _("%s: Can't link from %s to %s: %s\n"),
663 progname, fromname, toname, e);
664 exit(EXIT_FAILURE);
667 ifree(fromname);
668 ifree(toname);
671 #ifndef INT_MAX
672 #define INT_MAX ((int) (((unsigned)~0)>>1))
673 #endif /* !defined INT_MAX */
675 #ifndef INT_MIN
676 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
677 #endif /* !defined INT_MIN */
680 ** The tz file format currently allows at most 32-bit quantities.
681 ** This restriction should be removed before signed 32-bit values
682 ** wrap around in 2038, but unfortunately this will require a
683 ** change to the tz file format.
686 #define MAX_BITS_IN_FILE 32
687 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(zic_t) < MAX_BITS_IN_FILE) ? \
688 TYPE_BIT(zic_t) : MAX_BITS_IN_FILE)
690 static void
691 setboundaries P((void))
693 register int i;
695 if (TYPE_SIGNED(zic_t)) {
696 min_time = -1;
697 for (i = 0; i < TIME_T_BITS_IN_FILE - 1; ++i)
698 min_time *= 2;
699 max_time = -(min_time + 1);
700 if (sflag)
701 min_time = 0;
702 } else {
703 min_time = 0;
704 max_time = 2 - sflag;
705 for (i = 0; i < TIME_T_BITS_IN_FILE - 1; ++i)
706 max_time *= 2;
707 --max_time;
710 time_t t;
712 t = (time_t) min_time;
713 min_year = TM_YEAR_BASE + gmtime(&t)->tm_year;
714 t = (time_t) max_time;
715 max_year = TM_YEAR_BASE + gmtime(&t)->tm_year;
717 min_year_representable = min_year;
718 max_year_representable = max_year;
721 static int
722 itsdir(name)
723 const char * const name;
725 register char * myname;
726 register int accres;
728 myname = ecpyalloc(name);
729 myname = ecatalloc(myname, "/.");
730 accres = access(myname, F_OK);
731 ifree(myname);
732 return accres == 0;
736 ** Associate sets of rules with zones.
740 ** Sort by rule name.
743 static int
744 rcomp(cp1, cp2)
745 const void * cp1;
746 const void * cp2;
748 return strcmp(((const struct rule *) cp1)->r_name,
749 ((const struct rule *) cp2)->r_name);
752 static void
753 associate P((void))
755 register struct zone * zp;
756 register struct rule * rp;
757 register int base, out;
758 register int i, j;
760 if (nrules != 0) {
761 (void) qsort((void *) rules, (size_t) nrules,
762 (size_t) sizeof *rules, rcomp);
763 for (i = 0; i < nrules - 1; ++i) {
764 if (strcmp(rules[i].r_name,
765 rules[i + 1].r_name) != 0)
766 continue;
767 if (strcmp(rules[i].r_filename,
768 rules[i + 1].r_filename) == 0)
769 continue;
770 eat(rules[i].r_filename, rules[i].r_linenum);
771 warning(_("same rule name in multiple files"));
772 eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
773 warning(_("same rule name in multiple files"));
774 for (j = i + 2; j < nrules; ++j) {
775 if (strcmp(rules[i].r_name,
776 rules[j].r_name) != 0)
777 break;
778 if (strcmp(rules[i].r_filename,
779 rules[j].r_filename) == 0)
780 continue;
781 if (strcmp(rules[i + 1].r_filename,
782 rules[j].r_filename) == 0)
783 continue;
784 break;
786 i = j - 1;
789 for (i = 0; i < nzones; ++i) {
790 zp = &zones[i];
791 zp->z_rules = NULL;
792 zp->z_nrules = 0;
794 for (base = 0; base < nrules; base = out) {
795 rp = &rules[base];
796 for (out = base + 1; out < nrules; ++out)
797 if (strcmp(rp->r_name, rules[out].r_name) != 0)
798 break;
799 for (i = 0; i < nzones; ++i) {
800 zp = &zones[i];
801 if (strcmp(zp->z_rule, rp->r_name) != 0)
802 continue;
803 zp->z_rules = rp;
804 zp->z_nrules = out - base;
807 for (i = 0; i < nzones; ++i) {
808 zp = &zones[i];
809 if (zp->z_nrules == 0) {
811 ** Maybe we have a local standard time offset.
813 eat(zp->z_filename, zp->z_linenum);
814 zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
815 TRUE);
817 ** Note, though, that if there's no rule,
818 ** a '%s' in the format is a bad thing.
820 if (strchr(zp->z_format, '%') != 0)
821 error(_("%s in ruleless zone"));
824 if (errors)
825 exit(EXIT_FAILURE);
828 static void
829 infile(name)
830 const char * name;
832 register FILE * fp;
833 register char ** fields;
834 register char * cp;
835 register const struct lookup * lp;
836 register int nfields;
837 register int wantcont;
838 register int num;
839 char buf[BUFSIZ];
841 if (strcmp(name, "-") == 0) {
842 name = _("standard input");
843 fp = stdin;
844 } else if ((fp = fopen(name, "r")) == NULL) {
845 const char *e = strerror(errno);
847 (void) fprintf(stderr, _("%s: Can't open %s: %s\n"),
848 progname, name, e);
849 exit(EXIT_FAILURE);
851 wantcont = FALSE;
852 for (num = 1; ; ++num) {
853 eat(name, num);
854 if (fgets(buf, (int) sizeof buf, fp) != buf)
855 break;
856 cp = strchr(buf, '\n');
857 if (cp == NULL) {
858 error(_("line too long"));
859 exit(EXIT_FAILURE);
861 *cp = '\0';
862 fields = getfields(buf);
863 nfields = 0;
864 while (fields[nfields] != NULL) {
865 static char nada;
867 if (strcmp(fields[nfields], "-") == 0)
868 fields[nfields] = &nada;
869 ++nfields;
871 if (nfields == 0) {
872 /* nothing to do */
873 } else if (wantcont) {
874 wantcont = inzcont(fields, nfields);
875 } else {
876 lp = byword(fields[0], line_codes);
877 if (lp == NULL)
878 error(_("input line of unknown type"));
879 else switch ((int) (lp->l_value)) {
880 case LC_RULE:
881 inrule(fields, nfields);
882 wantcont = FALSE;
883 break;
884 case LC_ZONE:
885 wantcont = inzone(fields, nfields);
886 break;
887 case LC_LINK:
888 inlink(fields, nfields);
889 wantcont = FALSE;
890 break;
891 case LC_LEAP:
892 if (name != leapsec)
893 (void) fprintf(stderr,
894 _("%s: Leap line in non leap seconds file %s\n"),
895 progname, name);
896 else inleap(fields, nfields);
897 wantcont = FALSE;
898 break;
899 default: /* "cannot happen" */
900 (void) fprintf(stderr,
901 _("%s: panic: Invalid l_value %d\n"),
902 progname, lp->l_value);
903 exit(EXIT_FAILURE);
906 ifree((char *) fields);
908 if (ferror(fp)) {
909 (void) fprintf(stderr, _("%s: Error reading %s\n"),
910 progname, filename);
911 exit(EXIT_FAILURE);
913 if (fp != stdin && fclose(fp)) {
914 const char *e = strerror(errno);
916 (void) fprintf(stderr, _("%s: Error closing %s: %s\n"),
917 progname, filename, e);
918 exit(EXIT_FAILURE);
920 if (wantcont)
921 error(_("expected continuation line not found"));
925 ** Convert a string of one of the forms
926 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
927 ** into a number of seconds.
928 ** A null string maps to zero.
929 ** Call error with errstring and return zero on errors.
932 static long
933 gethms(string, errstring, signable)
934 const char * string;
935 const char * const errstring;
936 const int signable;
938 int hh, mm, ss, sign;
940 if (string == NULL || *string == '\0')
941 return 0;
942 if (!signable)
943 sign = 1;
944 else if (*string == '-') {
945 sign = -1;
946 ++string;
947 } else sign = 1;
948 if (sscanf(string, scheck(string, "%d"), &hh) == 1)
949 mm = ss = 0;
950 else if (sscanf(string, scheck(string, "%d:%d"), &hh, &mm) == 2)
951 ss = 0;
952 else if (sscanf(string, scheck(string, "%d:%d:%d"),
953 &hh, &mm, &ss) != 3) {
954 error(errstring);
955 return 0;
957 if ((hh < 0 || hh >= HOURSPERDAY ||
958 mm < 0 || mm >= MINSPERHOUR ||
959 ss < 0 || ss > SECSPERMIN) &&
960 !(hh == HOURSPERDAY && mm == 0 && ss == 0)) {
961 error(errstring);
962 return 0;
964 if (noise && hh == HOURSPERDAY)
965 warning(_("24:00 not handled by pre-1998 versions of zic"));
966 return eitol(sign) *
967 (eitol(hh * MINSPERHOUR + mm) *
968 eitol(SECSPERMIN) + eitol(ss));
971 static void
972 inrule(fields, nfields)
973 register char ** const fields;
974 const int nfields;
976 static struct rule r;
978 if (nfields != RULE_FIELDS) {
979 error(_("wrong number of fields on Rule line"));
980 return;
982 if (*fields[RF_NAME] == '\0') {
983 error(_("nameless rule"));
984 return;
986 r.r_filename = filename;
987 r.r_linenum = linenum;
988 r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
989 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
990 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
991 r.r_name = ecpyalloc(fields[RF_NAME]);
992 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
993 rules = (struct rule *) (void *) erealloc((char *) rules,
994 (int) ((nrules + 1) * sizeof *rules));
995 rules[nrules++] = r;
998 static int
999 inzone(fields, nfields)
1000 register char ** const fields;
1001 const int nfields;
1003 register int i;
1004 static char * buf;
1006 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
1007 error(_("wrong number of fields on Zone line"));
1008 return FALSE;
1010 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
1011 buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
1012 (void) sprintf(buf,
1013 _("\"Zone %s\" line and -l option are mutually exclusive"),
1014 TZDEFAULT);
1015 error(buf);
1016 return FALSE;
1018 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
1019 buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
1020 (void) sprintf(buf,
1021 _("\"Zone %s\" line and -p option are mutually exclusive"),
1022 TZDEFRULES);
1023 error(buf);
1024 return FALSE;
1026 for (i = 0; i < nzones; ++i)
1027 if (zones[i].z_name != NULL &&
1028 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
1029 buf = erealloc(buf, (int) (132 +
1030 strlen(fields[ZF_NAME]) +
1031 strlen(zones[i].z_filename)));
1032 (void) sprintf(buf,
1033 _("duplicate zone name %s (file \"%s\", line %d)"),
1034 fields[ZF_NAME],
1035 zones[i].z_filename,
1036 zones[i].z_linenum);
1037 error(buf);
1038 return FALSE;
1040 return inzsub(fields, nfields, FALSE);
1043 static int
1044 inzcont(fields, nfields)
1045 register char ** const fields;
1046 const int nfields;
1048 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
1049 error(_("wrong number of fields on Zone continuation line"));
1050 return FALSE;
1052 return inzsub(fields, nfields, TRUE);
1055 static int
1056 inzsub(fields, nfields, iscont)
1057 register char ** const fields;
1058 const int nfields;
1059 const int iscont;
1061 register char * cp;
1062 static struct zone z;
1063 register int i_gmtoff, i_rule, i_format;
1064 register int i_untilyear, i_untilmonth;
1065 register int i_untilday, i_untiltime;
1066 register int hasuntil;
1068 if (iscont) {
1069 i_gmtoff = ZFC_GMTOFF;
1070 i_rule = ZFC_RULE;
1071 i_format = ZFC_FORMAT;
1072 i_untilyear = ZFC_TILYEAR;
1073 i_untilmonth = ZFC_TILMONTH;
1074 i_untilday = ZFC_TILDAY;
1075 i_untiltime = ZFC_TILTIME;
1076 z.z_name = NULL;
1077 } else {
1078 i_gmtoff = ZF_GMTOFF;
1079 i_rule = ZF_RULE;
1080 i_format = ZF_FORMAT;
1081 i_untilyear = ZF_TILYEAR;
1082 i_untilmonth = ZF_TILMONTH;
1083 i_untilday = ZF_TILDAY;
1084 i_untiltime = ZF_TILTIME;
1085 z.z_name = ecpyalloc(fields[ZF_NAME]);
1087 z.z_filename = filename;
1088 z.z_linenum = linenum;
1089 z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UTC offset"), TRUE);
1090 if ((cp = strchr(fields[i_format], '%')) != 0) {
1091 if (*++cp != 's' || strchr(cp, '%') != 0) {
1092 error(_("invalid abbreviation format"));
1093 return FALSE;
1096 z.z_rule = ecpyalloc(fields[i_rule]);
1097 z.z_format = ecpyalloc(fields[i_format]);
1098 hasuntil = nfields > i_untilyear;
1099 if (hasuntil) {
1100 z.z_untilrule.r_filename = filename;
1101 z.z_untilrule.r_linenum = linenum;
1102 rulesub(&z.z_untilrule,
1103 fields[i_untilyear],
1104 "only",
1106 (nfields > i_untilmonth) ?
1107 fields[i_untilmonth] : "Jan",
1108 (nfields > i_untilday) ? fields[i_untilday] : "1",
1109 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1110 z.z_untiltime = rpytime(&z.z_untilrule,
1111 z.z_untilrule.r_loyear);
1112 if (iscont && nzones > 0 &&
1113 z.z_untiltime > min_time &&
1114 z.z_untiltime < max_time &&
1115 zones[nzones - 1].z_untiltime > min_time &&
1116 zones[nzones - 1].z_untiltime < max_time &&
1117 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1118 error(_(
1119 "Zone continuation line end time is not after end time of previous line"
1121 return FALSE;
1124 zones = (struct zone *) (void *) erealloc((char *) zones,
1125 (int) ((nzones + 1) * sizeof *zones));
1126 zones[nzones++] = z;
1128 ** If there was an UNTIL field on this line,
1129 ** there's more information about the zone on the next line.
1131 return hasuntil;
1134 static void
1135 inleap(fields, nfields)
1136 register char ** const fields;
1137 const int nfields;
1139 register const char * cp;
1140 register const struct lookup * lp;
1141 register int i, j;
1142 int year, month, day;
1143 long dayoff, tod;
1144 zic_t t;
1146 if (nfields != LEAP_FIELDS) {
1147 error(_("wrong number of fields on Leap line"));
1148 return;
1150 dayoff = 0;
1151 cp = fields[LP_YEAR];
1152 if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
1154 ** Leapin' Lizards!
1156 error(_("invalid leaping year"));
1157 return;
1159 j = EPOCH_YEAR;
1160 while (j != year) {
1161 if (year > j) {
1162 i = len_years[isleap(j)];
1163 ++j;
1164 } else {
1165 --j;
1166 i = -len_years[isleap(j)];
1168 dayoff = oadd(dayoff, eitol(i));
1170 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1171 error(_("invalid month name"));
1172 return;
1174 month = lp->l_value;
1175 j = TM_JANUARY;
1176 while (j != month) {
1177 i = len_months[isleap(year)][j];
1178 dayoff = oadd(dayoff, eitol(i));
1179 ++j;
1181 cp = fields[LP_DAY];
1182 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1183 day <= 0 || day > len_months[isleap(year)][month]) {
1184 error(_("invalid day of month"));
1185 return;
1187 dayoff = oadd(dayoff, eitol(day - 1));
1188 if (dayoff < 0 && !TYPE_SIGNED(zic_t)) {
1189 error(_("time before zero"));
1190 return;
1192 if (dayoff < min_time / SECSPERDAY) {
1193 error(_("time too small"));
1194 return;
1196 if (dayoff > max_time / SECSPERDAY) {
1197 error(_("time too large"));
1198 return;
1200 t = (zic_t) dayoff * SECSPERDAY;
1201 tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1202 cp = fields[LP_CORR];
1204 register int positive;
1205 int count;
1207 if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1208 positive = FALSE;
1209 count = 1;
1210 } else if (strcmp(cp, "--") == 0) {
1211 positive = FALSE;
1212 count = 2;
1213 } else if (strcmp(cp, "+") == 0) {
1214 positive = TRUE;
1215 count = 1;
1216 } else if (strcmp(cp, "++") == 0) {
1217 positive = TRUE;
1218 count = 2;
1219 } else {
1220 error(_("illegal CORRECTION field on Leap line"));
1221 return;
1223 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1224 error(_(
1225 "illegal Rolling/Stationary field on Leap line"
1227 return;
1229 leapadd(tadd(t, tod), positive, lp->l_value, count);
1233 static void
1234 inlink(fields, nfields)
1235 register char ** const fields;
1236 const int nfields;
1238 struct link l;
1240 if (nfields != LINK_FIELDS) {
1241 error(_("wrong number of fields on Link line"));
1242 return;
1244 if (*fields[LF_FROM] == '\0') {
1245 error(_("blank FROM field on Link line"));
1246 return;
1248 if (*fields[LF_TO] == '\0') {
1249 error(_("blank TO field on Link line"));
1250 return;
1252 l.l_filename = filename;
1253 l.l_linenum = linenum;
1254 l.l_from = ecpyalloc(fields[LF_FROM]);
1255 l.l_to = ecpyalloc(fields[LF_TO]);
1256 links = (struct link *) (void *) erealloc((char *) links,
1257 (int) ((nlinks + 1) * sizeof *links));
1258 links[nlinks++] = l;
1261 static void
1262 rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
1263 register struct rule * const rp;
1264 const char * const loyearp;
1265 const char * const hiyearp;
1266 const char * const typep;
1267 const char * const monthp;
1268 const char * const dayp;
1269 const char * const timep;
1271 register const struct lookup * lp;
1272 register const char * cp;
1273 register char * dp;
1274 register char * ep;
1276 if ((lp = byword(monthp, mon_names)) == NULL) {
1277 error(_("invalid month name"));
1278 return;
1280 rp->r_month = lp->l_value;
1281 rp->r_todisstd = FALSE;
1282 rp->r_todisgmt = FALSE;
1283 dp = ecpyalloc(timep);
1284 if (*dp != '\0') {
1285 ep = dp + strlen(dp) - 1;
1286 switch (lowerit(*ep)) {
1287 case 's': /* Standard */
1288 rp->r_todisstd = TRUE;
1289 rp->r_todisgmt = FALSE;
1290 *ep = '\0';
1291 break;
1292 case 'w': /* Wall */
1293 rp->r_todisstd = FALSE;
1294 rp->r_todisgmt = FALSE;
1295 *ep = '\0';
1296 break;
1297 case 'g': /* Greenwich */
1298 case 'u': /* Universal */
1299 case 'z': /* Zulu */
1300 rp->r_todisstd = TRUE;
1301 rp->r_todisgmt = TRUE;
1302 *ep = '\0';
1303 break;
1306 rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1307 ifree(dp);
1309 ** Year work.
1311 cp = loyearp;
1312 lp = byword(cp, begin_years);
1313 if (lp != NULL) switch ((int) lp->l_value) {
1314 case YR_MINIMUM:
1315 rp->r_loyear = INT_MIN;
1316 break;
1317 case YR_MAXIMUM:
1318 rp->r_loyear = INT_MAX;
1319 break;
1320 default: /* "cannot happen" */
1321 (void) fprintf(stderr,
1322 _("%s: panic: Invalid l_value %d\n"),
1323 progname, lp->l_value);
1324 exit(EXIT_FAILURE);
1325 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
1326 error(_("invalid starting year"));
1327 return;
1328 } else if (noise) {
1329 if (rp->r_loyear < min_year_representable)
1330 warning(_("starting year too low to be represented"));
1331 else if (rp->r_loyear > max_year_representable)
1332 warning(_("starting year too high to be represented"));
1334 cp = hiyearp;
1335 if ((lp = byword(cp, end_years)) != NULL) switch ((int) lp->l_value) {
1336 case YR_MINIMUM:
1337 rp->r_hiyear = INT_MIN;
1338 break;
1339 case YR_MAXIMUM:
1340 rp->r_hiyear = INT_MAX;
1341 break;
1342 case YR_ONLY:
1343 rp->r_hiyear = rp->r_loyear;
1344 break;
1345 default: /* "cannot happen" */
1346 (void) fprintf(stderr,
1347 _("%s: panic: Invalid l_value %d\n"),
1348 progname, lp->l_value);
1349 exit(EXIT_FAILURE);
1350 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
1351 error(_("invalid ending year"));
1352 return;
1353 } else if (noise) {
1354 if (rp->r_loyear < min_year_representable)
1355 warning(_("ending year too low to be represented"));
1356 else if (rp->r_loyear > max_year_representable)
1357 warning(_("ending year too high to be represented"));
1359 if (rp->r_loyear > rp->r_hiyear) {
1360 error(_("starting year greater than ending year"));
1361 return;
1363 if (*typep == '\0')
1364 rp->r_yrtype = NULL;
1365 else {
1366 if (rp->r_loyear == rp->r_hiyear) {
1367 error(_("typed single year"));
1368 return;
1370 rp->r_yrtype = ecpyalloc(typep);
1372 if (rp->r_loyear < min_year && rp->r_loyear > 0)
1373 min_year = rp->r_loyear;
1375 ** Day work.
1376 ** Accept things such as:
1377 ** 1
1378 ** last-Sunday
1379 ** Sun<=20
1380 ** Sun>=7
1382 dp = ecpyalloc(dayp);
1383 if ((lp = byword(dp, lasts)) != NULL) {
1384 rp->r_dycode = DC_DOWLEQ;
1385 rp->r_wday = lp->l_value;
1386 rp->r_dayofmonth = len_months[1][rp->r_month];
1387 } else {
1388 if ((ep = strchr(dp, '<')) != 0)
1389 rp->r_dycode = DC_DOWLEQ;
1390 else if ((ep = strchr(dp, '>')) != 0)
1391 rp->r_dycode = DC_DOWGEQ;
1392 else {
1393 ep = dp;
1394 rp->r_dycode = DC_DOM;
1396 if (rp->r_dycode != DC_DOM) {
1397 *ep++ = 0;
1398 if (*ep++ != '=') {
1399 error(_("invalid day of month"));
1400 ifree(dp);
1401 return;
1403 if ((lp = byword(dp, wday_names)) == NULL) {
1404 error(_("invalid weekday name"));
1405 ifree(dp);
1406 return;
1408 rp->r_wday = lp->l_value;
1410 if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1411 rp->r_dayofmonth <= 0 ||
1412 (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1413 error(_("invalid day of month"));
1414 ifree(dp);
1415 return;
1418 ifree(dp);
1421 static void
1422 convert(val, buf)
1423 const long val;
1424 char * const buf;
1426 register int i;
1427 register long shift;
1429 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1430 buf[i] = val >> shift;
1433 static void
1434 puttzcode(val, fp)
1435 const long val;
1436 FILE * const fp;
1438 char buf[4];
1440 convert(val, buf);
1441 (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1444 static int
1445 atcomp(const void * avp, const void * bvp)
1447 if (((struct attype *) avp)->at < ((struct attype *) bvp)->at)
1448 return -1;
1449 else if (((struct attype *) avp)->at > ((struct attype *) bvp)->at)
1450 return 1;
1451 else return 0;
1454 static void
1455 writezone(name)
1456 const char * const name;
1458 register FILE * fp;
1459 register int i, j;
1460 static char * fullname;
1461 static struct tzhead tzh;
1462 zic_t ats[TZ_MAX_TIMES];
1463 unsigned char types[TZ_MAX_TIMES];
1466 ** Sort.
1468 if (timecnt > 1)
1469 (void) qsort((void *) attypes, (size_t) timecnt,
1470 (size_t) sizeof *attypes, atcomp);
1472 ** Optimize.
1475 int fromi;
1476 int toi;
1478 toi = 0;
1479 fromi = 0;
1480 while (fromi < timecnt && attypes[fromi].at < min_time)
1481 ++fromi;
1482 if (isdsts[0] == 0)
1483 while (fromi < timecnt && attypes[fromi].type == 0)
1484 ++fromi; /* handled by default rule */
1485 for ( ; fromi < timecnt; ++fromi) {
1486 if (toi != 0 && ((attypes[fromi].at +
1487 gmtoffs[attypes[toi - 1].type]) <=
1488 (attypes[toi - 1].at + gmtoffs[toi == 1 ? 0
1489 : attypes[toi - 2].type]))) {
1490 attypes[toi - 1].type =
1491 attypes[fromi].type;
1492 continue;
1494 if (toi == 0 ||
1495 attypes[toi - 1].type != attypes[fromi].type)
1496 attypes[toi++] = attypes[fromi];
1498 timecnt = toi;
1501 ** Transfer.
1503 for (i = 0; i < timecnt; ++i) {
1504 ats[i] = attypes[i].at;
1505 types[i] = attypes[i].type;
1507 fullname = erealloc(fullname,
1508 (int) (strlen(directory) + 1 + strlen(name) + 1));
1509 (void) sprintf(fullname, "%s/%s", directory, name);
1511 ** Remove old file, if any, to snap links.
1513 if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT) {
1514 const char *e = strerror(errno);
1516 (void) fprintf(stderr, _("%s: Can't remove %s: %s\n"),
1517 progname, fullname, e);
1518 exit(EXIT_FAILURE);
1520 if ((fp = fopen(fullname, "wb")) == NULL) {
1521 if (mkdirs(fullname) != 0)
1522 exit(EXIT_FAILURE);
1523 if ((fp = fopen(fullname, "wb")) == NULL) {
1524 const char *e = strerror(errno);
1526 (void) fprintf(stderr, _("%s: Can't create %s: %s\n"),
1527 progname, fullname, e);
1528 exit(EXIT_FAILURE);
1531 convert(eitol(typecnt), tzh.tzh_ttisgmtcnt);
1532 convert(eitol(typecnt), tzh.tzh_ttisstdcnt);
1533 convert(eitol(leapcnt), tzh.tzh_leapcnt);
1534 convert(eitol(timecnt), tzh.tzh_timecnt);
1535 convert(eitol(typecnt), tzh.tzh_typecnt);
1536 convert(eitol(charcnt), tzh.tzh_charcnt);
1537 (void) strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1538 #define DO(field) (void) fwrite((void *) tzh.field, \
1539 (size_t) sizeof tzh.field, (size_t) 1, fp)
1540 DO(tzh_magic);
1541 DO(tzh_reserved);
1542 DO(tzh_ttisgmtcnt);
1543 DO(tzh_ttisstdcnt);
1544 DO(tzh_leapcnt);
1545 DO(tzh_timecnt);
1546 DO(tzh_typecnt);
1547 DO(tzh_charcnt);
1548 #undef DO
1549 for (i = 0; i < timecnt; ++i) {
1550 j = leapcnt;
1551 while (--j >= 0)
1552 if (ats[i] >= trans[j]) {
1553 ats[i] = tadd(ats[i], corr[j]);
1554 break;
1556 puttzcode((long) ats[i], fp);
1558 if (timecnt > 0)
1559 (void) fwrite((void *) types, (size_t) sizeof types[0],
1560 (size_t) timecnt, fp);
1561 for (i = 0; i < typecnt; ++i) {
1562 puttzcode((long) gmtoffs[i], fp);
1563 (void) putc(isdsts[i], fp);
1564 (void) putc(abbrinds[i], fp);
1566 if (charcnt != 0)
1567 (void) fwrite((void *) chars, (size_t) sizeof chars[0],
1568 (size_t) charcnt, fp);
1569 for (i = 0; i < leapcnt; ++i) {
1570 if (roll[i]) {
1571 if (timecnt == 0 || trans[i] < ats[0]) {
1572 j = 0;
1573 while (isdsts[j])
1574 if (++j >= typecnt) {
1575 j = 0;
1576 break;
1578 } else {
1579 j = 1;
1580 while (j < timecnt && trans[i] >= ats[j])
1581 ++j;
1582 j = types[j - 1];
1584 puttzcode((long) tadd(trans[i], -gmtoffs[j]), fp);
1585 } else puttzcode((long) trans[i], fp);
1586 puttzcode((long) corr[i], fp);
1588 for (i = 0; i < typecnt; ++i)
1589 (void) putc(ttisstds[i], fp);
1590 for (i = 0; i < typecnt; ++i)
1591 (void) putc(ttisgmts[i], fp);
1592 if (ferror(fp) || fclose(fp)) {
1593 (void) fprintf(stderr, _("%s: Error writing %s\n"),
1594 progname, fullname);
1595 exit(EXIT_FAILURE);
1599 static void
1600 doabbr(abbr, format, letters, isdst)
1601 char * const abbr;
1602 const char * const format;
1603 const char * const letters;
1604 const int isdst;
1606 if (strchr(format, '/') == NULL) {
1607 if (letters == NULL)
1608 (void) strcpy(abbr, format);
1609 else (void) sprintf(abbr, format, letters);
1610 } else if (isdst)
1611 (void) strcpy(abbr, strchr(format, '/') + 1);
1612 else {
1613 (void) strcpy(abbr, format);
1614 *strchr(abbr, '/') = '\0';
1618 static void
1619 outzone(zpfirst, zonecount)
1620 const struct zone * const zpfirst;
1621 const int zonecount;
1623 register const struct zone * zp;
1624 register struct rule * rp;
1625 register int i, j;
1626 register int usestart, useuntil;
1627 register zic_t starttime, untiltime;
1628 register long gmtoff;
1629 register long stdoff;
1630 register int year;
1631 register long startoff;
1632 register int startttisstd;
1633 register int startttisgmt;
1634 register int type;
1635 char startbuf[BUFSIZ];
1637 INITIALIZE(untiltime);
1638 INITIALIZE(starttime);
1640 ** Now. . .finally. . .generate some useful data!
1642 timecnt = 0;
1643 typecnt = 0;
1644 charcnt = 0;
1646 ** Thanks to Earl Chew
1647 ** for noting the need to unconditionally initialize startttisstd.
1649 startttisstd = FALSE;
1650 startttisgmt = FALSE;
1651 for (i = 0; i < zonecount; ++i) {
1653 ** A guess that may well be corrected later.
1655 stdoff = 0;
1656 zp = &zpfirst[i];
1657 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
1658 useuntil = i < (zonecount - 1);
1659 if (useuntil && zp->z_untiltime <= min_time)
1660 continue;
1661 gmtoff = zp->z_gmtoff;
1662 eat(zp->z_filename, zp->z_linenum);
1663 *startbuf = '\0';
1664 startoff = zp->z_gmtoff;
1665 if (zp->z_nrules == 0) {
1666 stdoff = zp->z_stdoff;
1667 doabbr(startbuf, zp->z_format,
1668 (char *) NULL, stdoff != 0);
1669 type = addtype(oadd(zp->z_gmtoff, stdoff),
1670 startbuf, stdoff != 0, startttisstd,
1671 startttisgmt);
1672 if (usestart) {
1673 addtt(starttime, type);
1674 usestart = FALSE;
1675 } else if (stdoff != 0)
1676 addtt(min_time, type);
1677 } else for (year = min_year; year <= max_year; ++year) {
1678 if (useuntil && year > zp->z_untilrule.r_hiyear)
1679 break;
1681 ** Mark which rules to do in the current year.
1682 ** For those to do, calculate rpytime(rp, year);
1684 for (j = 0; j < zp->z_nrules; ++j) {
1685 rp = &zp->z_rules[j];
1686 eats(zp->z_filename, zp->z_linenum,
1687 rp->r_filename, rp->r_linenum);
1688 rp->r_todo = year >= rp->r_loyear &&
1689 year <= rp->r_hiyear &&
1690 yearistype(year, rp->r_yrtype);
1691 if (rp->r_todo)
1692 rp->r_temp = rpytime(rp, year);
1694 for ( ; ; ) {
1695 register int k;
1696 register zic_t jtime, ktime;
1697 register long offset;
1698 char buf[BUFSIZ];
1700 INITIALIZE(ktime);
1701 if (useuntil) {
1703 ** Turn untiltime into UTC
1704 ** assuming the current gmtoff and
1705 ** stdoff values.
1707 untiltime = zp->z_untiltime;
1708 if (!zp->z_untilrule.r_todisgmt)
1709 untiltime = tadd(untiltime,
1710 -gmtoff);
1711 if (!zp->z_untilrule.r_todisstd)
1712 untiltime = tadd(untiltime,
1713 -stdoff);
1716 ** Find the rule (of those to do, if any)
1717 ** that takes effect earliest in the year.
1719 k = -1;
1720 for (j = 0; j < zp->z_nrules; ++j) {
1721 rp = &zp->z_rules[j];
1722 if (!rp->r_todo)
1723 continue;
1724 eats(zp->z_filename, zp->z_linenum,
1725 rp->r_filename, rp->r_linenum);
1726 offset = rp->r_todisgmt ? 0 : gmtoff;
1727 if (!rp->r_todisstd)
1728 offset = oadd(offset, stdoff);
1729 jtime = rp->r_temp;
1730 if (jtime == min_time ||
1731 jtime == max_time)
1732 continue;
1733 jtime = tadd(jtime, -offset);
1734 if (k < 0 || jtime < ktime) {
1735 k = j;
1736 ktime = jtime;
1739 if (k < 0)
1740 break; /* go on to next year */
1741 rp = &zp->z_rules[k];
1742 rp->r_todo = FALSE;
1743 if (useuntil && ktime >= untiltime)
1744 break;
1745 stdoff = rp->r_stdoff;
1746 if (usestart && ktime == starttime)
1747 usestart = FALSE;
1748 if (usestart) {
1749 if (ktime < starttime) {
1750 startoff = oadd(zp->z_gmtoff,
1751 stdoff);
1752 doabbr(startbuf, zp->z_format,
1753 rp->r_abbrvar,
1754 rp->r_stdoff != 0);
1755 continue;
1757 if (*startbuf == '\0' &&
1758 startoff == oadd(zp->z_gmtoff,
1759 stdoff))
1760 doabbr(startbuf,
1761 zp->z_format,
1762 rp->r_abbrvar,
1763 rp->r_stdoff !=
1766 eats(zp->z_filename, zp->z_linenum,
1767 rp->r_filename, rp->r_linenum);
1768 doabbr(buf, zp->z_format, rp->r_abbrvar,
1769 rp->r_stdoff != 0);
1770 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
1771 type = addtype(offset, buf, rp->r_stdoff != 0,
1772 rp->r_todisstd, rp->r_todisgmt);
1773 addtt(ktime, type);
1776 if (usestart) {
1777 if (*startbuf == '\0' &&
1778 zp->z_format != NULL &&
1779 strchr(zp->z_format, '%') == NULL &&
1780 strchr(zp->z_format, '/') == NULL)
1781 (void) strcpy(startbuf, zp->z_format);
1782 eat(zp->z_filename, zp->z_linenum);
1783 if (*startbuf == '\0')
1784 error(_("can't determine time zone abbreviation to use just after until time"));
1785 else addtt(starttime,
1786 addtype(startoff, startbuf,
1787 startoff != zp->z_gmtoff,
1788 startttisstd,
1789 startttisgmt));
1792 ** Now we may get to set starttime for the next zone line.
1794 if (useuntil) {
1795 startttisstd = zp->z_untilrule.r_todisstd;
1796 startttisgmt = zp->z_untilrule.r_todisgmt;
1797 starttime = zp->z_untiltime;
1798 if (!startttisstd)
1799 starttime = tadd(starttime, -stdoff);
1800 if (!startttisgmt)
1801 starttime = tadd(starttime, -gmtoff);
1804 writezone(zpfirst->z_name);
1807 static void
1808 addtt(starttime, type)
1809 const zic_t starttime;
1810 int type;
1812 if (starttime <= min_time ||
1813 (timecnt == 1 && attypes[0].at < min_time)) {
1814 gmtoffs[0] = gmtoffs[type];
1815 isdsts[0] = isdsts[type];
1816 ttisstds[0] = ttisstds[type];
1817 ttisgmts[0] = ttisgmts[type];
1818 if (abbrinds[type] != 0)
1819 (void) strcpy(chars, &chars[abbrinds[type]]);
1820 abbrinds[0] = 0;
1821 charcnt = strlen(chars) + 1;
1822 typecnt = 1;
1823 timecnt = 0;
1824 type = 0;
1826 if (timecnt >= TZ_MAX_TIMES) {
1827 error(_("too many transitions?!"));
1828 exit(EXIT_FAILURE);
1830 attypes[timecnt].at = starttime;
1831 attypes[timecnt].type = type;
1832 ++timecnt;
1835 static int
1836 addtype(gmtoff, abbr, isdst, ttisstd, ttisgmt)
1837 const long gmtoff;
1838 const char * const abbr;
1839 const int isdst;
1840 const int ttisstd;
1841 const int ttisgmt;
1843 register int i, j;
1845 if (isdst != TRUE && isdst != FALSE) {
1846 error(_("internal error - addtype called with bad isdst"));
1847 exit(EXIT_FAILURE);
1849 if (ttisstd != TRUE && ttisstd != FALSE) {
1850 error(_("internal error - addtype called with bad ttisstd"));
1851 exit(EXIT_FAILURE);
1853 if (ttisgmt != TRUE && ttisgmt != FALSE) {
1854 error(_("internal error - addtype called with bad ttisgmt"));
1855 exit(EXIT_FAILURE);
1858 ** See if there's already an entry for this zone type.
1859 ** If so, just return its index.
1861 for (i = 0; i < typecnt; ++i) {
1862 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
1863 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
1864 ttisstd == ttisstds[i] &&
1865 ttisgmt == ttisgmts[i])
1866 return i;
1869 ** There isn't one; add a new one, unless there are already too
1870 ** many.
1872 if (typecnt >= TZ_MAX_TYPES) {
1873 error(_("too many local time types"));
1874 exit(EXIT_FAILURE);
1876 gmtoffs[i] = gmtoff;
1877 isdsts[i] = isdst;
1878 ttisstds[i] = ttisstd;
1879 ttisgmts[i] = ttisgmt;
1881 for (j = 0; j < charcnt; ++j)
1882 if (strcmp(&chars[j], abbr) == 0)
1883 break;
1884 if (j == charcnt)
1885 newabbr(abbr);
1886 abbrinds[i] = j;
1887 ++typecnt;
1888 return i;
1891 static void
1892 leapadd(t, positive, rolling, count)
1893 const zic_t t;
1894 const int positive;
1895 const int rolling;
1896 int count;
1898 register int i, j;
1900 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
1901 error(_("too many leap seconds"));
1902 exit(EXIT_FAILURE);
1904 for (i = 0; i < leapcnt; ++i)
1905 if (t <= trans[i]) {
1906 if (t == trans[i]) {
1907 error(_("repeated leap second moment"));
1908 exit(EXIT_FAILURE);
1910 break;
1912 do {
1913 for (j = leapcnt; j > i; --j) {
1914 trans[j] = trans[j - 1];
1915 corr[j] = corr[j - 1];
1916 roll[j] = roll[j - 1];
1918 trans[i] = t;
1919 corr[i] = positive ? 1L : eitol(-count);
1920 roll[i] = rolling;
1921 ++leapcnt;
1922 } while (positive && --count != 0);
1925 static void
1926 adjleap P((void))
1928 register int i;
1929 register long last = 0;
1932 ** propagate leap seconds forward
1934 for (i = 0; i < leapcnt; ++i) {
1935 trans[i] = tadd(trans[i], last);
1936 last = corr[i] += last;
1940 static int
1941 yearistype(year, type)
1942 const int year;
1943 const char * const type;
1945 static char * buf;
1946 int result;
1948 if (type == NULL || *type == '\0')
1949 return TRUE;
1950 buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
1951 (void) sprintf(buf, "%s %d %s", yitcommand, year, type);
1952 result = system(buf);
1953 if (WIFEXITED(result)) switch (WEXITSTATUS(result)) {
1954 case 0:
1955 return TRUE;
1956 case 1:
1957 return FALSE;
1959 error(_("Wild result from command execution"));
1960 (void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
1961 progname, buf, result);
1962 for ( ; ; )
1963 exit(EXIT_FAILURE);
1966 static int
1967 lowerit(a)
1968 int a;
1970 a = (unsigned char) a;
1971 return (isascii(a) && isupper(a)) ? tolower(a) : a;
1974 static int
1975 ciequal(ap, bp) /* case-insensitive equality */
1976 register const char * ap;
1977 register const char * bp;
1979 while (lowerit(*ap) == lowerit(*bp++))
1980 if (*ap++ == '\0')
1981 return TRUE;
1982 return FALSE;
1985 static int
1986 itsabbr(abbr, word)
1987 register const char * abbr;
1988 register const char * word;
1990 if (lowerit(*abbr) != lowerit(*word))
1991 return FALSE;
1992 ++word;
1993 while (*++abbr != '\0')
1994 do {
1995 if (*word == '\0')
1996 return FALSE;
1997 } while (lowerit(*word++) != lowerit(*abbr));
1998 return TRUE;
2001 static const struct lookup *
2002 byword(word, table)
2003 register const char * const word;
2004 register const struct lookup * const table;
2006 register const struct lookup * foundlp;
2007 register const struct lookup * lp;
2009 if (word == NULL || table == NULL)
2010 return NULL;
2012 ** Look for exact match.
2014 for (lp = table; lp->l_word != NULL; ++lp)
2015 if (ciequal(word, lp->l_word))
2016 return lp;
2018 ** Look for inexact match.
2020 foundlp = NULL;
2021 for (lp = table; lp->l_word != NULL; ++lp)
2022 if (itsabbr(word, lp->l_word)) {
2023 if (foundlp == NULL)
2024 foundlp = lp;
2025 else return NULL; /* multiple inexact matches */
2027 return foundlp;
2030 static char **
2031 getfields(cp)
2032 register char * cp;
2034 register char * dp;
2035 register char ** array;
2036 register int nsubs;
2038 if (cp == NULL)
2039 return NULL;
2040 array = (char **) (void *)
2041 emalloc((int) ((strlen(cp) + 1) * sizeof *array));
2042 nsubs = 0;
2043 for ( ; ; ) {
2044 while (isascii((unsigned char) *cp) &&
2045 isspace((unsigned char) *cp))
2046 ++cp;
2047 if (*cp == '\0' || *cp == '#')
2048 break;
2049 array[nsubs++] = dp = cp;
2050 do {
2051 if ((*dp = *cp++) != '"')
2052 ++dp;
2053 else while ((*dp = *cp++) != '"')
2054 if (*dp != '\0')
2055 ++dp;
2056 else error(_(
2057 "Odd number of quotation marks"
2059 } while (*cp != '\0' && *cp != '#' &&
2060 (!isascii(*cp) || !isspace((unsigned char) *cp)));
2061 if (isascii(*cp) && isspace((unsigned char) *cp))
2062 ++cp;
2063 *dp = '\0';
2065 array[nsubs] = NULL;
2066 return array;
2069 static long
2070 oadd(t1, t2)
2071 const long t1;
2072 const long t2;
2074 register long t;
2076 t = t1 + t2;
2077 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2078 error(_("time overflow"));
2079 exit(EXIT_FAILURE);
2081 return t;
2084 static zic_t
2085 tadd(t1, t2)
2086 const zic_t t1;
2087 const long t2;
2089 register zic_t t;
2091 if (t1 == max_time && t2 > 0)
2092 return max_time;
2093 if (t1 == min_time && t2 < 0)
2094 return min_time;
2095 t = t1 + t2;
2096 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2097 error(_("time overflow"));
2098 exit(EXIT_FAILURE);
2100 return t;
2104 ** Given a rule, and a year, compute the date - in seconds since January 1,
2105 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2108 static zic_t
2109 rpytime(rp, wantedy)
2110 register const struct rule * const rp;
2111 register const int wantedy;
2113 register int y, m, i;
2114 register long dayoff; /* with a nod to Margaret O. */
2115 register zic_t t;
2117 if (wantedy == INT_MIN)
2118 return min_time;
2119 if (wantedy == INT_MAX)
2120 return max_time;
2121 dayoff = 0;
2122 m = TM_JANUARY;
2123 y = EPOCH_YEAR;
2124 while (wantedy != y) {
2125 if (wantedy > y) {
2126 i = len_years[isleap(y)];
2127 ++y;
2128 } else {
2129 --y;
2130 i = -len_years[isleap(y)];
2132 dayoff = oadd(dayoff, eitol(i));
2134 while (m != rp->r_month) {
2135 i = len_months[isleap(y)][m];
2136 dayoff = oadd(dayoff, eitol(i));
2137 ++m;
2139 i = rp->r_dayofmonth;
2140 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
2141 if (rp->r_dycode == DC_DOWLEQ)
2142 --i;
2143 else {
2144 error(_("use of 2/29 in non leap-year"));
2145 exit(EXIT_FAILURE);
2148 --i;
2149 dayoff = oadd(dayoff, eitol(i));
2150 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
2151 register long wday;
2153 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2154 wday = eitol(EPOCH_WDAY);
2156 ** Don't trust mod of negative numbers.
2158 if (dayoff >= 0)
2159 wday = (wday + dayoff) % LDAYSPERWEEK;
2160 else {
2161 wday -= ((-dayoff) % LDAYSPERWEEK);
2162 if (wday < 0)
2163 wday += LDAYSPERWEEK;
2165 while (wday != eitol(rp->r_wday))
2166 if (rp->r_dycode == DC_DOWGEQ) {
2167 dayoff = oadd(dayoff, (long) 1);
2168 if (++wday >= LDAYSPERWEEK)
2169 wday = 0;
2170 ++i;
2171 } else {
2172 dayoff = oadd(dayoff, (long) -1);
2173 if (--wday < 0)
2174 wday = LDAYSPERWEEK - 1;
2175 --i;
2177 if (i < 0 || i >= len_months[isleap(y)][m]) {
2178 if (noise)
2179 warning(_("rule goes past start/end of month--\
2180 will not work with pre-2004 versions of zic"));
2183 if (dayoff < 0 && !TYPE_SIGNED(zic_t))
2184 return min_time;
2185 if (dayoff < min_time / SECSPERDAY)
2186 return min_time;
2187 if (dayoff > max_time / SECSPERDAY)
2188 return max_time;
2189 t = (zic_t) dayoff * SECSPERDAY;
2190 return tadd(t, rp->r_tod);
2193 static void
2194 newabbr(string)
2195 const char * const string;
2197 register int i;
2199 if (strcmp(string, GRANDPARENTED) != 0) {
2200 register const char * cp;
2201 register char * wp;
2204 ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2205 ** optionally followed by a + or - and a number from 1 to 14.
2207 cp = string;
2208 wp = NULL;
2209 while (isascii((unsigned char) *cp) &&
2210 isalpha((unsigned char) *cp))
2211 ++cp;
2212 if (cp - string == 0)
2213 wp = _("time zone abbreviation lacks alphabetic at start");
2214 if (noise && cp - string > 3)
2215 wp = _("time zone abbreviation has more than 3 alphabetics");
2216 if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
2217 wp = _("time zone abbreviation has too many alphabetics");
2218 if (wp == NULL && (*cp == '+' || *cp == '-')) {
2219 ++cp;
2220 if (isascii((unsigned char) *cp) &&
2221 isdigit((unsigned char) *cp))
2222 if (*cp++ == '1' &&
2223 *cp >= '0' && *cp <= '4')
2224 ++cp;
2226 if (*cp != '\0')
2227 wp = _("time zone abbreviation differs from POSIX standard");
2228 if (wp != NULL) {
2229 wp = ecpyalloc(wp);
2230 wp = ecatalloc(wp, " (");
2231 wp = ecatalloc(wp, string);
2232 wp = ecatalloc(wp, ")");
2233 warning(wp);
2234 ifree(wp);
2237 i = strlen(string) + 1;
2238 if (charcnt + i > TZ_MAX_CHARS) {
2239 error(_("too many, or too long, time zone abbreviations"));
2240 exit(EXIT_FAILURE);
2242 (void) strcpy(&chars[charcnt], string);
2243 charcnt += eitol(i);
2246 static int
2247 mkdirs(argname)
2248 char * const argname;
2250 register char * name;
2251 register char * cp;
2253 if (argname == NULL || *argname == '\0')
2254 return 0;
2255 cp = name = ecpyalloc(argname);
2256 while ((cp = strchr(cp + 1, '/')) != 0) {
2257 *cp = '\0';
2258 #ifndef unix
2260 ** DOS drive specifier?
2262 if (isalpha((unsigned char) name[0]) &&
2263 name[1] == ':' && name[2] == '\0') {
2264 *cp = '/';
2265 continue;
2267 #endif /* !defined unix */
2268 if (!itsdir(name)) {
2270 ** It doesn't seem to exist, so we try to create it.
2271 ** Creation may fail because of the directory being
2272 ** created by some other multiprocessor, so we get
2273 ** to do extra checking.
2275 if (mkdir(name, MKDIR_UMASK) != 0) {
2276 const char *e = strerror(errno);
2278 if (errno != EEXIST || !itsdir(name)) {
2279 (void) fprintf(stderr,
2280 _("%s: Can't create directory %s: %s\n"),
2281 progname, name, e);
2282 ifree(name);
2283 return -1;
2287 *cp = '/';
2289 ifree(name);
2290 return 0;
2293 static long
2294 eitol(i)
2295 const int i;
2297 long l;
2299 l = i;
2300 if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0)) {
2301 (void) fprintf(stderr,
2302 _("%s: %d did not sign extend correctly\n"),
2303 progname, i);
2304 exit(EXIT_FAILURE);
2306 return l;
2310 ** UNIX was a registered trademark of The Open Group in 2003.