Sun agreed to a change of the license for the RPC code to a BSD-like license.
[glibc/pb-stable.git] / timezone / zic.c
blob5f8028e480613fd1bfd3f838a1a6c4b15f170f96
1 /*
2 ** This file is in the public domain, so clarified as of
3 ** 2006-07-17 by Arthur David Olson.
4 */
6 static char elsieid[] = "@(#)zic.c 8.17";
8 #include "private.h"
9 #include "locale.h"
10 #include "tzfile.h"
12 #define ZIC_VERSION '2'
14 typedef int_fast64_t zic_t;
16 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
17 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
18 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
20 #if HAVE_SYS_STAT_H
21 #include "sys/stat.h"
22 #endif
23 #ifdef S_IRUSR
24 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
25 #else
26 #define MKDIR_UMASK 0755
27 #endif
30 ** On some ancient hosts, predicates like `isspace(C)' are defined
31 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
32 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
33 ** Neither the C Standard nor Posix require that `isascii' exist.
34 ** For portability, we check both ancient and modern requirements.
35 ** If isascii is not defined, the isascii check succeeds trivially.
37 #include "ctype.h"
38 #ifndef isascii
39 #define isascii(x) 1
40 #endif
42 #define OFFSET_STRLEN_MAXIMUM (7 + INT_STRLEN_MAXIMUM(long))
43 #define RULE_STRLEN_MAXIMUM 8 /* "Mdd.dd.d" */
45 #define end(cp) (strchr((cp), '\0'))
47 struct rule {
48 const char * r_filename;
49 int r_linenum;
50 const char * r_name;
52 int r_loyear; /* for example, 1986 */
53 int r_hiyear; /* for example, 1986 */
54 const char * r_yrtype;
55 int r_lowasnum;
56 int r_hiwasnum;
58 int r_month; /* 0..11 */
60 int r_dycode; /* see below */
61 int r_dayofmonth;
62 int r_wday;
64 long r_tod; /* time from midnight */
65 int r_todisstd; /* above is standard time if TRUE */
66 /* or wall clock time if FALSE */
67 int r_todisgmt; /* above is GMT if TRUE */
68 /* or local time if FALSE */
69 long r_stdoff; /* offset from standard time */
70 const char * r_abbrvar; /* variable part of abbreviation */
72 int r_todo; /* a rule to do (used in outzone) */
73 zic_t r_temp; /* used in outzone */
77 ** r_dycode r_dayofmonth r_wday
80 #define DC_DOM 0 /* 1..31 */ /* unused */
81 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
82 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
84 struct zone {
85 const char * z_filename;
86 int z_linenum;
88 const char * z_name;
89 long z_gmtoff;
90 const char * z_rule;
91 const char * z_format;
93 long z_stdoff;
95 struct rule * z_rules;
96 int z_nrules;
98 struct rule z_untilrule;
99 zic_t z_untiltime;
102 extern int getopt(int argc, char * const argv[],
103 const char * options);
104 extern int link(const char * fromname, const char * toname);
105 extern char * optarg;
106 extern int optind;
108 static void addtt(zic_t starttime, int type);
109 static int addtype(long gmtoff, const char * abbr, int isdst,
110 int ttisstd, int ttisgmt);
111 static void leapadd(zic_t t, int positive, int rolling, int count);
112 static void adjleap(void);
113 static void associate(void);
114 static int ciequal(const char * ap, const char * bp);
115 static void convert(long val, char * buf);
116 static void convert64(zic_t val, char * buf);
117 static void dolink(const char * fromfield, const char * tofield);
118 static void doabbr(char * abbr, const char * format,
119 const char * letters, int isdst, int doquotes);
120 static void eat(const char * name, int num);
121 static void eats(const char * name, int num,
122 const char * rname, int rnum);
123 static long eitol(int i);
124 static void error(const char * message);
125 static char ** getfields(char * buf);
126 static long gethms(const char * string, const char * errstrng,
127 int signable);
128 static void infile(const char * filename);
129 static void inleap(char ** fields, int nfields);
130 static void inlink(char ** fields, int nfields);
131 static void inrule(char ** fields, int nfields);
132 static int inzcont(char ** fields, int nfields);
133 static int inzone(char ** fields, int nfields);
134 static int inzsub(char ** fields, int nfields, int iscont);
135 static int is32(zic_t x);
136 static int itsabbr(const char * abbr, const char * word);
137 static int itsdir(const char * name);
138 static int lowerit(int c);
139 static char * memcheck(char * tocheck);
140 static int mkdirs(char * filename);
141 static void newabbr(const char * abbr);
142 static long oadd(long t1, long t2);
143 static void outzone(const struct zone * zp, int ntzones);
144 static void puttzcode(long code, FILE * fp);
145 static void puttzcode64(zic_t code, FILE * fp);
146 static int rcomp(const void * leftp, const void * rightp);
147 static zic_t rpytime(const struct rule * rp, int wantedy);
148 static void rulesub(struct rule * rp,
149 const char * loyearp, const char * hiyearp,
150 const char * typep, const char * monthp,
151 const char * dayp, const char * timep);
152 static int stringoffset(char * result, long offset);
153 static int stringrule(char * result, const struct rule * rp,
154 long dstoff, long gmtoff);
155 static void stringzone(char * result,
156 const struct zone * zp, int ntzones);
157 static void setboundaries(void);
158 static zic_t tadd(zic_t t1, long t2);
159 static void usage(void);
160 static void writezone(const char * name, const char * string);
161 static int yearistype(int year, const char * type);
163 static int charcnt;
164 static int errors;
165 static const char * filename;
166 static int leapcnt;
167 static int leapseen;
168 static int leapminyear;
169 static int leapmaxyear;
170 static int linenum;
171 static int max_abbrvar_len;
172 static int max_format_len;
173 static zic_t max_time;
174 static int max_year;
175 static zic_t min_time;
176 static int min_year;
177 static int noise;
178 static const char * rfilename;
179 static int rlinenum;
180 static const char * progname;
181 static int timecnt;
182 static int typecnt;
185 ** Line codes.
188 #define LC_RULE 0
189 #define LC_ZONE 1
190 #define LC_LINK 2
191 #define LC_LEAP 3
194 ** Which fields are which on a Zone line.
197 #define ZF_NAME 1
198 #define ZF_GMTOFF 2
199 #define ZF_RULE 3
200 #define ZF_FORMAT 4
201 #define ZF_TILYEAR 5
202 #define ZF_TILMONTH 6
203 #define ZF_TILDAY 7
204 #define ZF_TILTIME 8
205 #define ZONE_MINFIELDS 5
206 #define ZONE_MAXFIELDS 9
209 ** Which fields are which on a Zone continuation line.
212 #define ZFC_GMTOFF 0
213 #define ZFC_RULE 1
214 #define ZFC_FORMAT 2
215 #define ZFC_TILYEAR 3
216 #define ZFC_TILMONTH 4
217 #define ZFC_TILDAY 5
218 #define ZFC_TILTIME 6
219 #define ZONEC_MINFIELDS 3
220 #define ZONEC_MAXFIELDS 7
223 ** Which files are which on a Rule line.
226 #define RF_NAME 1
227 #define RF_LOYEAR 2
228 #define RF_HIYEAR 3
229 #define RF_COMMAND 4
230 #define RF_MONTH 5
231 #define RF_DAY 6
232 #define RF_TOD 7
233 #define RF_STDOFF 8
234 #define RF_ABBRVAR 9
235 #define RULE_FIELDS 10
238 ** Which fields are which on a Link line.
241 #define LF_FROM 1
242 #define LF_TO 2
243 #define LINK_FIELDS 3
246 ** Which fields are which on a Leap line.
249 #define LP_YEAR 1
250 #define LP_MONTH 2
251 #define LP_DAY 3
252 #define LP_TIME 4
253 #define LP_CORR 5
254 #define LP_ROLL 6
255 #define LEAP_FIELDS 7
258 ** Year synonyms.
261 #define YR_MINIMUM 0
262 #define YR_MAXIMUM 1
263 #define YR_ONLY 2
265 static struct rule * rules;
266 static int nrules; /* number of rules */
268 static struct zone * zones;
269 static int nzones; /* number of zones */
271 struct link {
272 const char * l_filename;
273 int l_linenum;
274 const char * l_from;
275 const char * l_to;
278 static struct link * links;
279 static int nlinks;
281 struct lookup {
282 const char * l_word;
283 const int l_value;
286 static struct lookup const * byword(const char * string,
287 const struct lookup * lp);
289 static struct lookup const line_codes[] = {
290 { "Rule", LC_RULE },
291 { "Zone", LC_ZONE },
292 { "Link", LC_LINK },
293 { "Leap", LC_LEAP },
294 { NULL, 0}
297 static struct lookup const mon_names[] = {
298 { "January", TM_JANUARY },
299 { "February", TM_FEBRUARY },
300 { "March", TM_MARCH },
301 { "April", TM_APRIL },
302 { "May", TM_MAY },
303 { "June", TM_JUNE },
304 { "July", TM_JULY },
305 { "August", TM_AUGUST },
306 { "September", TM_SEPTEMBER },
307 { "October", TM_OCTOBER },
308 { "November", TM_NOVEMBER },
309 { "December", TM_DECEMBER },
310 { NULL, 0 }
313 static struct lookup const wday_names[] = {
314 { "Sunday", TM_SUNDAY },
315 { "Monday", TM_MONDAY },
316 { "Tuesday", TM_TUESDAY },
317 { "Wednesday", TM_WEDNESDAY },
318 { "Thursday", TM_THURSDAY },
319 { "Friday", TM_FRIDAY },
320 { "Saturday", TM_SATURDAY },
321 { NULL, 0 }
324 static struct lookup const lasts[] = {
325 { "last-Sunday", TM_SUNDAY },
326 { "last-Monday", TM_MONDAY },
327 { "last-Tuesday", TM_TUESDAY },
328 { "last-Wednesday", TM_WEDNESDAY },
329 { "last-Thursday", TM_THURSDAY },
330 { "last-Friday", TM_FRIDAY },
331 { "last-Saturday", TM_SATURDAY },
332 { NULL, 0 }
335 static struct lookup const begin_years[] = {
336 { "minimum", YR_MINIMUM },
337 { "maximum", YR_MAXIMUM },
338 { NULL, 0 }
341 static struct lookup const end_years[] = {
342 { "minimum", YR_MINIMUM },
343 { "maximum", YR_MAXIMUM },
344 { "only", YR_ONLY },
345 { NULL, 0 }
348 static struct lookup const leap_types[] = {
349 { "Rolling", TRUE },
350 { "Stationary", FALSE },
351 { NULL, 0 }
354 static const int len_months[2][MONSPERYEAR] = {
355 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
356 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
359 static const int len_years[2] = {
360 DAYSPERNYEAR, DAYSPERLYEAR
363 static struct attype {
364 zic_t at;
365 unsigned char type;
366 } attypes[TZ_MAX_TIMES];
367 static long gmtoffs[TZ_MAX_TYPES];
368 static char isdsts[TZ_MAX_TYPES];
369 static unsigned char abbrinds[TZ_MAX_TYPES];
370 static char ttisstds[TZ_MAX_TYPES];
371 static char ttisgmts[TZ_MAX_TYPES];
372 static char chars[TZ_MAX_CHARS];
373 static zic_t trans[TZ_MAX_LEAPS];
374 static long corr[TZ_MAX_LEAPS];
375 static char roll[TZ_MAX_LEAPS];
378 ** Memory allocation.
381 static char *
382 memcheck(ptr)
383 char * const ptr;
385 if (ptr == NULL) {
386 const char *e = strerror(errno);
388 (void) fprintf(stderr, _("%s: Memory exhausted: %s\n"),
389 progname, e);
390 exit(EXIT_FAILURE);
392 return ptr;
395 #define emalloc(size) memcheck(imalloc(size))
396 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
397 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
398 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
401 ** Error handling.
404 static void
405 eats(name, num, rname, rnum)
406 const char * const name;
407 const int num;
408 const char * const rname;
409 const int rnum;
411 filename = name;
412 linenum = num;
413 rfilename = rname;
414 rlinenum = rnum;
417 static void
418 eat(name, num)
419 const char * const name;
420 const int num;
422 eats(name, num, (char *) NULL, -1);
425 static void
426 error(string)
427 const char * const string;
430 ** Match the format of "cc" to allow sh users to
431 ** zic ... 2>&1 | error -t "*" -v
432 ** on BSD systems.
434 (void) fprintf(stderr, _("\"%s\", line %d: %s"),
435 filename, linenum, string);
436 if (rfilename != NULL)
437 (void) fprintf(stderr, _(" (rule from \"%s\", line %d)"),
438 rfilename, rlinenum);
439 (void) fprintf(stderr, "\n");
440 ++errors;
443 static void
444 warning(string)
445 const char * const string;
447 char * cp;
449 cp = ecpyalloc(_("warning: "));
450 cp = ecatalloc(cp, string);
451 error(cp);
452 ifree(cp);
453 --errors;
456 static void
457 usage(void)
459 (void) fprintf(stderr, _("%s: usage is %s \
460 [ --version ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\
461 \t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
462 progname, progname);
463 exit(EXIT_FAILURE);
466 static const char * psxrules;
467 static const char * lcltime;
468 static const char * directory;
469 static const char * leapsec;
470 static const char * yitcommand;
473 main(argc, argv)
474 int argc;
475 char * argv[];
477 register int i;
478 register int j;
479 register int c;
481 #ifdef unix
482 (void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
483 #endif /* defined unix */
484 #if HAVE_GETTEXT
485 (void) setlocale(LC_ALL, "");
486 #ifdef TZ_DOMAINDIR
487 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
488 #endif /* defined TEXTDOMAINDIR */
489 (void) textdomain(TZ_DOMAIN);
490 #endif /* HAVE_GETTEXT */
491 progname = argv[0];
492 if (TYPE_BIT(zic_t) < 64) {
493 (void) fprintf(stderr, "%s: %s\n", progname,
494 _("wild compilation-time specification of zic_t"));
495 exit(EXIT_FAILURE);
497 for (i = 1; i < argc; ++i)
498 if (strcmp(argv[i], "--version") == 0) {
499 (void) printf("%s\n", elsieid);
500 exit(EXIT_SUCCESS);
502 while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF && c != -1)
503 switch (c) {
504 default:
505 usage();
506 case 'd':
507 if (directory == NULL)
508 directory = optarg;
509 else {
510 (void) fprintf(stderr,
511 _("%s: More than one -d option specified\n"),
512 progname);
513 exit(EXIT_FAILURE);
515 break;
516 case 'l':
517 if (lcltime == NULL)
518 lcltime = optarg;
519 else {
520 (void) fprintf(stderr,
521 _("%s: More than one -l option specified\n"),
522 progname);
523 exit(EXIT_FAILURE);
525 break;
526 case 'p':
527 if (psxrules == NULL)
528 psxrules = optarg;
529 else {
530 (void) fprintf(stderr,
531 _("%s: More than one -p option specified\n"),
532 progname);
533 exit(EXIT_FAILURE);
535 break;
536 case 'y':
537 if (yitcommand == NULL)
538 yitcommand = optarg;
539 else {
540 (void) fprintf(stderr,
541 _("%s: More than one -y option specified\n"),
542 progname);
543 exit(EXIT_FAILURE);
545 break;
546 case 'L':
547 if (leapsec == NULL)
548 leapsec = optarg;
549 else {
550 (void) fprintf(stderr,
551 _("%s: More than one -L option specified\n"),
552 progname);
553 exit(EXIT_FAILURE);
555 break;
556 case 'v':
557 noise = TRUE;
558 break;
559 case 's':
560 (void) printf("%s: -s ignored\n", progname);
561 break;
563 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
564 usage(); /* usage message by request */
565 if (directory == NULL)
566 directory = TZDIR;
567 if (yitcommand == NULL)
568 yitcommand = "yearistype";
570 setboundaries();
572 if (optind < argc && leapsec != NULL) {
573 infile(leapsec);
574 adjleap();
577 for (i = optind; i < argc; ++i)
578 infile(argv[i]);
579 if (errors)
580 exit(EXIT_FAILURE);
581 associate();
582 for (i = 0; i < nzones; i = j) {
584 ** Find the next non-continuation zone entry.
586 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
587 continue;
588 outzone(&zones[i], j - i);
591 ** Make links.
593 for (i = 0; i < nlinks; ++i) {
594 eat(links[i].l_filename, links[i].l_linenum);
595 dolink(links[i].l_from, links[i].l_to);
596 if (noise)
597 for (j = 0; j < nlinks; ++j)
598 if (strcmp(links[i].l_to,
599 links[j].l_from) == 0)
600 warning(_("link to link"));
602 if (lcltime != NULL) {
603 eat("command line", 1);
604 dolink(lcltime, TZDEFAULT);
606 if (psxrules != NULL) {
607 eat("command line", 1);
608 dolink(psxrules, TZDEFRULES);
610 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
613 static void
614 dolink(fromfield, tofield)
615 const char * const fromfield;
616 const char * const tofield;
618 register char * fromname;
619 register char * toname;
621 if (fromfield[0] == '/')
622 fromname = ecpyalloc(fromfield);
623 else {
624 fromname = ecpyalloc(directory);
625 fromname = ecatalloc(fromname, "/");
626 fromname = ecatalloc(fromname, fromfield);
628 if (tofield[0] == '/')
629 toname = ecpyalloc(tofield);
630 else {
631 toname = ecpyalloc(directory);
632 toname = ecatalloc(toname, "/");
633 toname = ecatalloc(toname, tofield);
636 ** We get to be careful here since
637 ** there's a fair chance of root running us.
639 if (!itsdir(toname))
640 (void) remove(toname);
641 if (link(fromname, toname) != 0) {
642 int result;
644 if (mkdirs(toname) != 0)
645 exit(EXIT_FAILURE);
647 result = link(fromname, toname);
648 #if HAVE_SYMLINK
649 if (result != 0 &&
650 access(fromname, F_OK) == 0 &&
651 !itsdir(fromname)) {
652 const char *s = tofield;
653 register char * symlinkcontents = NULL;
655 while ((s = strchr(s+1, '/')) != NULL)
656 symlinkcontents =
657 ecatalloc(symlinkcontents,
658 "../");
659 symlinkcontents =
660 ecatalloc(symlinkcontents,
661 fromname);
662 result = symlink(symlinkcontents,
663 toname);
664 if (result == 0)
665 warning(_("hard link failed, symbolic link used"));
666 ifree(symlinkcontents);
668 #endif /* HAVE_SYMLINK */
669 if (result != 0) {
670 const char *e = strerror(errno);
672 (void) fprintf(stderr,
673 _("%s: Can't link from %s to %s: %s\n"),
674 progname, fromname, toname, e);
675 exit(EXIT_FAILURE);
678 ifree(fromname);
679 ifree(toname);
682 #define TIME_T_BITS_IN_FILE 64
684 static void
685 setboundaries(void)
687 register int i;
689 min_time = -1;
690 for (i = 0; i < TIME_T_BITS_IN_FILE - 1; ++i)
691 min_time *= 2;
692 max_time = -(min_time + 1);
695 static int
696 itsdir(name)
697 const char * const name;
699 register char * myname;
700 register int accres;
702 myname = ecpyalloc(name);
703 myname = ecatalloc(myname, "/.");
704 accres = access(myname, F_OK);
705 ifree(myname);
706 return accres == 0;
710 ** Associate sets of rules with zones.
714 ** Sort by rule name.
717 static int
718 rcomp(cp1, cp2)
719 const void * cp1;
720 const void * cp2;
722 return strcmp(((const struct rule *) cp1)->r_name,
723 ((const struct rule *) cp2)->r_name);
726 static void
727 associate(void)
729 register struct zone * zp;
730 register struct rule * rp;
731 register int base, out;
732 register int i, j;
734 if (nrules != 0) {
735 (void) qsort((void *) rules, (size_t) nrules,
736 (size_t) sizeof *rules, rcomp);
737 for (i = 0; i < nrules - 1; ++i) {
738 if (strcmp(rules[i].r_name,
739 rules[i + 1].r_name) != 0)
740 continue;
741 if (strcmp(rules[i].r_filename,
742 rules[i + 1].r_filename) == 0)
743 continue;
744 eat(rules[i].r_filename, rules[i].r_linenum);
745 warning(_("same rule name in multiple files"));
746 eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
747 warning(_("same rule name in multiple files"));
748 for (j = i + 2; j < nrules; ++j) {
749 if (strcmp(rules[i].r_name,
750 rules[j].r_name) != 0)
751 break;
752 if (strcmp(rules[i].r_filename,
753 rules[j].r_filename) == 0)
754 continue;
755 if (strcmp(rules[i + 1].r_filename,
756 rules[j].r_filename) == 0)
757 continue;
758 break;
760 i = j - 1;
763 for (i = 0; i < nzones; ++i) {
764 zp = &zones[i];
765 zp->z_rules = NULL;
766 zp->z_nrules = 0;
768 for (base = 0; base < nrules; base = out) {
769 rp = &rules[base];
770 for (out = base + 1; out < nrules; ++out)
771 if (strcmp(rp->r_name, rules[out].r_name) != 0)
772 break;
773 for (i = 0; i < nzones; ++i) {
774 zp = &zones[i];
775 if (strcmp(zp->z_rule, rp->r_name) != 0)
776 continue;
777 zp->z_rules = rp;
778 zp->z_nrules = out - base;
781 for (i = 0; i < nzones; ++i) {
782 zp = &zones[i];
783 if (zp->z_nrules == 0) {
785 ** Maybe we have a local standard time offset.
787 eat(zp->z_filename, zp->z_linenum);
788 zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
789 TRUE);
791 ** Note, though, that if there's no rule,
792 ** a '%s' in the format is a bad thing.
794 if (strchr(zp->z_format, '%') != 0)
795 error(_("%s in ruleless zone"));
798 if (errors)
799 exit(EXIT_FAILURE);
802 static void
803 infile(name)
804 const char * name;
806 register FILE * fp;
807 register char ** fields;
808 register char * cp;
809 register const struct lookup * lp;
810 register int nfields;
811 register int wantcont;
812 register int num;
813 char buf[BUFSIZ];
815 if (strcmp(name, "-") == 0) {
816 name = _("standard input");
817 fp = stdin;
818 } else if ((fp = fopen(name, "r")) == NULL) {
819 const char *e = strerror(errno);
821 (void) fprintf(stderr, _("%s: Can't open %s: %s\n"),
822 progname, name, e);
823 exit(EXIT_FAILURE);
825 wantcont = FALSE;
826 for (num = 1; ; ++num) {
827 eat(name, num);
828 if (fgets(buf, (int) sizeof buf, fp) != buf)
829 break;
830 cp = strchr(buf, '\n');
831 if (cp == NULL) {
832 error(_("line too long"));
833 exit(EXIT_FAILURE);
835 *cp = '\0';
836 fields = getfields(buf);
837 nfields = 0;
838 while (fields[nfields] != NULL) {
839 static char nada;
841 if (strcmp(fields[nfields], "-") == 0)
842 fields[nfields] = &nada;
843 ++nfields;
845 if (nfields == 0) {
846 /* nothing to do */
847 } else if (wantcont) {
848 wantcont = inzcont(fields, nfields);
849 } else {
850 lp = byword(fields[0], line_codes);
851 if (lp == NULL)
852 error(_("input line of unknown type"));
853 else switch ((int) (lp->l_value)) {
854 case LC_RULE:
855 inrule(fields, nfields);
856 wantcont = FALSE;
857 break;
858 case LC_ZONE:
859 wantcont = inzone(fields, nfields);
860 break;
861 case LC_LINK:
862 inlink(fields, nfields);
863 wantcont = FALSE;
864 break;
865 case LC_LEAP:
866 if (name != leapsec)
867 (void) fprintf(stderr,
868 _("%s: Leap line in non leap seconds file %s\n"),
869 progname, name);
870 else inleap(fields, nfields);
871 wantcont = FALSE;
872 break;
873 default: /* "cannot happen" */
874 (void) fprintf(stderr,
875 _("%s: panic: Invalid l_value %d\n"),
876 progname, lp->l_value);
877 exit(EXIT_FAILURE);
880 ifree((char *) fields);
882 if (ferror(fp)) {
883 (void) fprintf(stderr, _("%s: Error reading %s\n"),
884 progname, filename);
885 exit(EXIT_FAILURE);
887 if (fp != stdin && fclose(fp)) {
888 const char *e = strerror(errno);
890 (void) fprintf(stderr, _("%s: Error closing %s: %s\n"),
891 progname, filename, e);
892 exit(EXIT_FAILURE);
894 if (wantcont)
895 error(_("expected continuation line not found"));
899 ** Convert a string of one of the forms
900 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
901 ** into a number of seconds.
902 ** A null string maps to zero.
903 ** Call error with errstring and return zero on errors.
906 static long
907 gethms(string, errstring, signable)
908 const char * string;
909 const char * const errstring;
910 const int signable;
912 long hh;
913 int mm, ss, sign;
915 if (string == NULL || *string == '\0')
916 return 0;
917 if (!signable)
918 sign = 1;
919 else if (*string == '-') {
920 sign = -1;
921 ++string;
922 } else sign = 1;
923 if (sscanf(string, scheck(string, "%ld"), &hh) == 1)
924 mm = ss = 0;
925 else if (sscanf(string, scheck(string, "%ld:%d"), &hh, &mm) == 2)
926 ss = 0;
927 else if (sscanf(string, scheck(string, "%ld:%d:%d"),
928 &hh, &mm, &ss) != 3) {
929 error(errstring);
930 return 0;
932 if (hh < 0 ||
933 mm < 0 || mm >= MINSPERHOUR ||
934 ss < 0 || ss > SECSPERMIN) {
935 error(errstring);
936 return 0;
938 if (LONG_MAX / SECSPERHOUR < hh) {
939 error(_("time overflow"));
940 return 0;
942 if (noise && hh == HOURSPERDAY && mm == 0 && ss == 0)
943 warning(_("24:00 not handled by pre-1998 versions of zic"));
944 if (noise && (hh > HOURSPERDAY ||
945 (hh == HOURSPERDAY && (mm != 0 || ss != 0))))
946 warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
947 return oadd(eitol(sign) * hh * eitol(SECSPERHOUR),
948 eitol(sign) * (eitol(mm) * eitol(SECSPERMIN) + eitol(ss)));
951 static void
952 inrule(fields, nfields)
953 register char ** const fields;
954 const int nfields;
956 static struct rule r;
958 if (nfields != RULE_FIELDS) {
959 error(_("wrong number of fields on Rule line"));
960 return;
962 if (*fields[RF_NAME] == '\0') {
963 error(_("nameless rule"));
964 return;
966 r.r_filename = filename;
967 r.r_linenum = linenum;
968 r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
969 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
970 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
971 r.r_name = ecpyalloc(fields[RF_NAME]);
972 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
973 if (max_abbrvar_len < strlen(r.r_abbrvar))
974 max_abbrvar_len = strlen(r.r_abbrvar);
975 rules = (struct rule *) (void *) erealloc((char *) rules,
976 (int) ((nrules + 1) * sizeof *rules));
977 rules[nrules++] = r;
980 static int
981 inzone(fields, nfields)
982 register char ** const fields;
983 const int nfields;
985 register int i;
986 static char * buf;
988 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
989 error(_("wrong number of fields on Zone line"));
990 return FALSE;
992 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
993 buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
994 (void) sprintf(buf,
995 _("\"Zone %s\" line and -l option are mutually exclusive"),
996 TZDEFAULT);
997 error(buf);
998 return FALSE;
1000 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
1001 buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
1002 (void) sprintf(buf,
1003 _("\"Zone %s\" line and -p option are mutually exclusive"),
1004 TZDEFRULES);
1005 error(buf);
1006 return FALSE;
1008 for (i = 0; i < nzones; ++i)
1009 if (zones[i].z_name != NULL &&
1010 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
1011 buf = erealloc(buf, (int) (132 +
1012 strlen(fields[ZF_NAME]) +
1013 strlen(zones[i].z_filename)));
1014 (void) sprintf(buf,
1015 _("duplicate zone name %s (file \"%s\", line %d)"),
1016 fields[ZF_NAME],
1017 zones[i].z_filename,
1018 zones[i].z_linenum);
1019 error(buf);
1020 return FALSE;
1022 return inzsub(fields, nfields, FALSE);
1025 static int
1026 inzcont(fields, nfields)
1027 register char ** const fields;
1028 const int nfields;
1030 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
1031 error(_("wrong number of fields on Zone continuation line"));
1032 return FALSE;
1034 return inzsub(fields, nfields, TRUE);
1037 static int
1038 inzsub(fields, nfields, iscont)
1039 register char ** const fields;
1040 const int nfields;
1041 const int iscont;
1043 register char * cp;
1044 static struct zone z;
1045 register int i_gmtoff, i_rule, i_format;
1046 register int i_untilyear, i_untilmonth;
1047 register int i_untilday, i_untiltime;
1048 register int hasuntil;
1050 if (iscont) {
1051 i_gmtoff = ZFC_GMTOFF;
1052 i_rule = ZFC_RULE;
1053 i_format = ZFC_FORMAT;
1054 i_untilyear = ZFC_TILYEAR;
1055 i_untilmonth = ZFC_TILMONTH;
1056 i_untilday = ZFC_TILDAY;
1057 i_untiltime = ZFC_TILTIME;
1058 z.z_name = NULL;
1059 } else {
1060 i_gmtoff = ZF_GMTOFF;
1061 i_rule = ZF_RULE;
1062 i_format = ZF_FORMAT;
1063 i_untilyear = ZF_TILYEAR;
1064 i_untilmonth = ZF_TILMONTH;
1065 i_untilday = ZF_TILDAY;
1066 i_untiltime = ZF_TILTIME;
1067 z.z_name = ecpyalloc(fields[ZF_NAME]);
1069 z.z_filename = filename;
1070 z.z_linenum = linenum;
1071 z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UTC offset"), TRUE);
1072 if ((cp = strchr(fields[i_format], '%')) != 0) {
1073 if (*++cp != 's' || strchr(cp, '%') != 0) {
1074 error(_("invalid abbreviation format"));
1075 return FALSE;
1078 z.z_rule = ecpyalloc(fields[i_rule]);
1079 z.z_format = ecpyalloc(fields[i_format]);
1080 if (max_format_len < strlen(z.z_format))
1081 max_format_len = strlen(z.z_format);
1082 hasuntil = nfields > i_untilyear;
1083 if (hasuntil) {
1084 z.z_untilrule.r_filename = filename;
1085 z.z_untilrule.r_linenum = linenum;
1086 rulesub(&z.z_untilrule,
1087 fields[i_untilyear],
1088 "only",
1090 (nfields > i_untilmonth) ?
1091 fields[i_untilmonth] : "Jan",
1092 (nfields > i_untilday) ? fields[i_untilday] : "1",
1093 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1094 z.z_untiltime = rpytime(&z.z_untilrule,
1095 z.z_untilrule.r_loyear);
1096 if (iscont && nzones > 0 &&
1097 z.z_untiltime > min_time &&
1098 z.z_untiltime < max_time &&
1099 zones[nzones - 1].z_untiltime > min_time &&
1100 zones[nzones - 1].z_untiltime < max_time &&
1101 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1102 error(_(
1103 "Zone continuation line end time is not after end time of previous line"
1105 return FALSE;
1108 zones = (struct zone *) (void *) erealloc((char *) zones,
1109 (int) ((nzones + 1) * sizeof *zones));
1110 zones[nzones++] = z;
1112 ** If there was an UNTIL field on this line,
1113 ** there's more information about the zone on the next line.
1115 return hasuntil;
1118 static void
1119 inleap(fields, nfields)
1120 register char ** const fields;
1121 const int nfields;
1123 register const char * cp;
1124 register const struct lookup * lp;
1125 register int i, j;
1126 int year, month, day;
1127 long dayoff, tod;
1128 zic_t t;
1130 if (nfields != LEAP_FIELDS) {
1131 error(_("wrong number of fields on Leap line"));
1132 return;
1134 dayoff = 0;
1135 cp = fields[LP_YEAR];
1136 if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
1138 ** Leapin' Lizards!
1140 error(_("invalid leaping year"));
1141 return;
1143 if (!leapseen || leapmaxyear < year)
1144 leapmaxyear = year;
1145 if (!leapseen || leapminyear > year)
1146 leapminyear = year;
1147 leapseen = TRUE;
1148 j = EPOCH_YEAR;
1149 while (j != year) {
1150 if (year > j) {
1151 i = len_years[isleap(j)];
1152 ++j;
1153 } else {
1154 --j;
1155 i = -len_years[isleap(j)];
1157 dayoff = oadd(dayoff, eitol(i));
1159 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1160 error(_("invalid month name"));
1161 return;
1163 month = lp->l_value;
1164 j = TM_JANUARY;
1165 while (j != month) {
1166 i = len_months[isleap(year)][j];
1167 dayoff = oadd(dayoff, eitol(i));
1168 ++j;
1170 cp = fields[LP_DAY];
1171 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1172 day <= 0 || day > len_months[isleap(year)][month]) {
1173 error(_("invalid day of month"));
1174 return;
1176 dayoff = oadd(dayoff, eitol(day - 1));
1177 if (dayoff < 0 && !TYPE_SIGNED(zic_t)) {
1178 error(_("time before zero"));
1179 return;
1181 if (dayoff < min_time / SECSPERDAY) {
1182 error(_("time too small"));
1183 return;
1185 if (dayoff > max_time / SECSPERDAY) {
1186 error(_("time too large"));
1187 return;
1189 t = (zic_t) dayoff * SECSPERDAY;
1190 tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1191 cp = fields[LP_CORR];
1193 register int positive;
1194 int count;
1196 if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1197 positive = FALSE;
1198 count = 1;
1199 } else if (strcmp(cp, "--") == 0) {
1200 positive = FALSE;
1201 count = 2;
1202 } else if (strcmp(cp, "+") == 0) {
1203 positive = TRUE;
1204 count = 1;
1205 } else if (strcmp(cp, "++") == 0) {
1206 positive = TRUE;
1207 count = 2;
1208 } else {
1209 error(_("illegal CORRECTION field on Leap line"));
1210 return;
1212 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1213 error(_(
1214 "illegal Rolling/Stationary field on Leap line"
1216 return;
1218 leapadd(tadd(t, tod), positive, lp->l_value, count);
1222 static void
1223 inlink(fields, nfields)
1224 register char ** const fields;
1225 const int nfields;
1227 struct link l;
1229 if (nfields != LINK_FIELDS) {
1230 error(_("wrong number of fields on Link line"));
1231 return;
1233 if (*fields[LF_FROM] == '\0') {
1234 error(_("blank FROM field on Link line"));
1235 return;
1237 if (*fields[LF_TO] == '\0') {
1238 error(_("blank TO field on Link line"));
1239 return;
1241 l.l_filename = filename;
1242 l.l_linenum = linenum;
1243 l.l_from = ecpyalloc(fields[LF_FROM]);
1244 l.l_to = ecpyalloc(fields[LF_TO]);
1245 links = (struct link *) (void *) erealloc((char *) links,
1246 (int) ((nlinks + 1) * sizeof *links));
1247 links[nlinks++] = l;
1250 static void
1251 rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
1252 register struct rule * const rp;
1253 const char * const loyearp;
1254 const char * const hiyearp;
1255 const char * const typep;
1256 const char * const monthp;
1257 const char * const dayp;
1258 const char * const timep;
1260 register const struct lookup * lp;
1261 register const char * cp;
1262 register char * dp;
1263 register char * ep;
1265 if ((lp = byword(monthp, mon_names)) == NULL) {
1266 error(_("invalid month name"));
1267 return;
1269 rp->r_month = lp->l_value;
1270 rp->r_todisstd = FALSE;
1271 rp->r_todisgmt = FALSE;
1272 dp = ecpyalloc(timep);
1273 if (*dp != '\0') {
1274 ep = dp + strlen(dp) - 1;
1275 switch (lowerit(*ep)) {
1276 case 's': /* Standard */
1277 rp->r_todisstd = TRUE;
1278 rp->r_todisgmt = FALSE;
1279 *ep = '\0';
1280 break;
1281 case 'w': /* Wall */
1282 rp->r_todisstd = FALSE;
1283 rp->r_todisgmt = FALSE;
1284 *ep = '\0';
1285 break;
1286 case 'g': /* Greenwich */
1287 case 'u': /* Universal */
1288 case 'z': /* Zulu */
1289 rp->r_todisstd = TRUE;
1290 rp->r_todisgmt = TRUE;
1291 *ep = '\0';
1292 break;
1295 rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1296 ifree(dp);
1298 ** Year work.
1300 cp = loyearp;
1301 lp = byword(cp, begin_years);
1302 rp->r_lowasnum = lp == NULL;
1303 if (!rp->r_lowasnum) switch ((int) lp->l_value) {
1304 case YR_MINIMUM:
1305 rp->r_loyear = INT_MIN;
1306 break;
1307 case YR_MAXIMUM:
1308 rp->r_loyear = INT_MAX;
1309 break;
1310 default: /* "cannot happen" */
1311 (void) fprintf(stderr,
1312 _("%s: panic: Invalid l_value %d\n"),
1313 progname, lp->l_value);
1314 exit(EXIT_FAILURE);
1315 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
1316 error(_("invalid starting year"));
1317 return;
1319 cp = hiyearp;
1320 lp = byword(cp, end_years);
1321 rp->r_hiwasnum = lp == NULL;
1322 if (!rp->r_hiwasnum) switch ((int) lp->l_value) {
1323 case YR_MINIMUM:
1324 rp->r_hiyear = INT_MIN;
1325 break;
1326 case YR_MAXIMUM:
1327 rp->r_hiyear = INT_MAX;
1328 break;
1329 case YR_ONLY:
1330 rp->r_hiyear = rp->r_loyear;
1331 break;
1332 default: /* "cannot happen" */
1333 (void) fprintf(stderr,
1334 _("%s: panic: Invalid l_value %d\n"),
1335 progname, lp->l_value);
1336 exit(EXIT_FAILURE);
1337 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
1338 error(_("invalid ending year"));
1339 return;
1341 if (rp->r_loyear > rp->r_hiyear) {
1342 error(_("starting year greater than ending year"));
1343 return;
1345 if (*typep == '\0')
1346 rp->r_yrtype = NULL;
1347 else {
1348 if (rp->r_loyear == rp->r_hiyear) {
1349 error(_("typed single year"));
1350 return;
1352 rp->r_yrtype = ecpyalloc(typep);
1355 ** Day work.
1356 ** Accept things such as:
1357 ** 1
1358 ** last-Sunday
1359 ** Sun<=20
1360 ** Sun>=7
1362 dp = ecpyalloc(dayp);
1363 if ((lp = byword(dp, lasts)) != NULL) {
1364 rp->r_dycode = DC_DOWLEQ;
1365 rp->r_wday = lp->l_value;
1366 rp->r_dayofmonth = len_months[1][rp->r_month];
1367 } else {
1368 if ((ep = strchr(dp, '<')) != 0)
1369 rp->r_dycode = DC_DOWLEQ;
1370 else if ((ep = strchr(dp, '>')) != 0)
1371 rp->r_dycode = DC_DOWGEQ;
1372 else {
1373 ep = dp;
1374 rp->r_dycode = DC_DOM;
1376 if (rp->r_dycode != DC_DOM) {
1377 *ep++ = 0;
1378 if (*ep++ != '=') {
1379 error(_("invalid day of month"));
1380 ifree(dp);
1381 return;
1383 if ((lp = byword(dp, wday_names)) == NULL) {
1384 error(_("invalid weekday name"));
1385 ifree(dp);
1386 return;
1388 rp->r_wday = lp->l_value;
1390 if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1391 rp->r_dayofmonth <= 0 ||
1392 (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1393 error(_("invalid day of month"));
1394 ifree(dp);
1395 return;
1398 ifree(dp);
1401 static void
1402 convert(val, buf)
1403 const long val;
1404 char * const buf;
1406 register int i;
1407 register int shift;
1409 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1410 buf[i] = val >> shift;
1413 static void
1414 convert64(val, buf)
1415 const zic_t val;
1416 char * const buf;
1418 register int i;
1419 register int shift;
1421 for (i = 0, shift = 56; i < 8; ++i, shift -= 8)
1422 buf[i] = val >> shift;
1425 static void
1426 puttzcode(val, fp)
1427 const long val;
1428 FILE * const fp;
1430 char buf[4];
1432 convert(val, buf);
1433 (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1436 static void
1437 puttzcode64(val, fp)
1438 const zic_t val;
1439 FILE * const fp;
1441 char buf[8];
1443 convert64(val, buf);
1444 (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1447 static int
1448 atcomp(avp, bvp)
1449 const void * avp;
1450 const void * bvp;
1452 const zic_t a = ((const struct attype *) avp)->at;
1453 const zic_t b = ((const struct attype *) bvp)->at;
1455 return (a < b) ? -1 : (a > b);
1458 static int
1459 is32(x)
1460 const zic_t x;
1462 return INT32_MIN <= x && x <= INT32_MAX;
1465 static void
1466 writezone(name, string)
1467 const char * const name;
1468 const char * const string;
1470 register FILE * fp;
1471 register int i, j;
1472 register int leapcnt32, leapi32;
1473 register int timecnt32, timei32;
1474 register int pass;
1475 static char * fullname;
1476 static const struct tzhead tzh0;
1477 static struct tzhead tzh;
1478 zic_t ats[TZ_MAX_TIMES];
1479 unsigned char types[TZ_MAX_TIMES];
1482 ** Sort.
1484 if (timecnt > 1)
1485 (void) qsort((void *) attypes, (size_t) timecnt,
1486 (size_t) sizeof *attypes, atcomp);
1488 ** Optimize.
1491 int fromi;
1492 int toi;
1494 toi = 0;
1495 fromi = 0;
1496 while (fromi < timecnt && attypes[fromi].at < min_time)
1497 ++fromi;
1498 if (isdsts[0] == 0)
1499 while (fromi < timecnt && attypes[fromi].type == 0)
1500 ++fromi; /* handled by default rule */
1501 for ( ; fromi < timecnt; ++fromi) {
1502 if (toi != 0 && ((attypes[fromi].at +
1503 gmtoffs[attypes[toi - 1].type]) <=
1504 (attypes[toi - 1].at + gmtoffs[toi == 1 ? 0
1505 : attypes[toi - 2].type]))) {
1506 attypes[toi - 1].type =
1507 attypes[fromi].type;
1508 continue;
1510 if (toi == 0 ||
1511 attypes[toi - 1].type != attypes[fromi].type)
1512 attypes[toi++] = attypes[fromi];
1514 timecnt = toi;
1517 ** Transfer.
1519 for (i = 0; i < timecnt; ++i) {
1520 ats[i] = attypes[i].at;
1521 types[i] = attypes[i].type;
1524 ** Correct for leap seconds.
1526 for (i = 0; i < timecnt; ++i) {
1527 j = leapcnt;
1528 while (--j >= 0)
1529 if (ats[i] > trans[j] - corr[j]) {
1530 ats[i] = tadd(ats[i], corr[j]);
1531 break;
1535 ** Figure out 32-bit-limited starts and counts.
1537 timecnt32 = timecnt;
1538 timei32 = 0;
1539 leapcnt32 = leapcnt;
1540 leapi32 = 0;
1541 while (timecnt32 > 0 && !is32(ats[timecnt32 - 1]))
1542 --timecnt32;
1543 while (timecnt32 > 0 && !is32(ats[timei32])) {
1544 --timecnt32;
1545 ++timei32;
1547 while (leapcnt32 > 0 && !is32(trans[leapcnt32 - 1]))
1548 --leapcnt32;
1549 while (leapcnt32 > 0 && !is32(trans[leapi32])) {
1550 --leapcnt32;
1551 ++leapi32;
1553 fullname = erealloc(fullname,
1554 (int) (strlen(directory) + 1 + strlen(name) + 1));
1555 (void) sprintf(fullname, "%s/%s", directory, name);
1557 ** Remove old file, if any, to snap links.
1559 if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT) {
1560 const char *e = strerror(errno);
1562 (void) fprintf(stderr, _("%s: Can't remove %s: %s\n"),
1563 progname, fullname, e);
1564 exit(EXIT_FAILURE);
1566 if ((fp = fopen(fullname, "wb")) == NULL) {
1567 if (mkdirs(fullname) != 0)
1568 exit(EXIT_FAILURE);
1569 if ((fp = fopen(fullname, "wb")) == NULL) {
1570 const char *e = strerror(errno);
1572 (void) fprintf(stderr, _("%s: Can't create %s: %s\n"),
1573 progname, fullname, e);
1574 exit(EXIT_FAILURE);
1577 for (pass = 1; pass <= 2; ++pass) {
1578 register int thistimei, thistimecnt;
1579 register int thisleapi, thisleapcnt;
1580 register int thistimelim, thisleaplim;
1581 int writetype[TZ_MAX_TIMES];
1582 int typemap[TZ_MAX_TYPES];
1583 register int thistypecnt;
1584 char thischars[TZ_MAX_CHARS];
1585 char thischarcnt;
1586 int indmap[TZ_MAX_CHARS];
1588 if (pass == 1) {
1589 thistimei = timei32;
1590 thistimecnt = timecnt32;
1591 thisleapi = leapi32;
1592 thisleapcnt = leapcnt32;
1593 } else {
1594 thistimei = 0;
1595 thistimecnt = timecnt;
1596 thisleapi = 0;
1597 thisleapcnt = leapcnt;
1599 thistimelim = thistimei + thistimecnt;
1600 thisleaplim = thisleapi + thisleapcnt;
1601 for (i = 0; i < typecnt; ++i)
1602 writetype[i] = thistimecnt == timecnt;
1603 if (thistimecnt == 0) {
1605 ** No transition times fall in the current
1606 ** (32- or 64-bit) window.
1608 if (typecnt != 0)
1609 writetype[typecnt - 1] = TRUE;
1610 } else {
1611 for (i = thistimei - 1; i < thistimelim; ++i)
1612 if (i >= 0)
1613 writetype[types[i]] = TRUE;
1615 ** For America/Godthab and Antarctica/Palmer
1617 if (thistimei == 0)
1618 writetype[0] = TRUE;
1620 thistypecnt = 0;
1621 for (i = 0; i < typecnt; ++i)
1622 typemap[i] = writetype[i] ? thistypecnt++ : -1;
1623 for (i = 0; i < sizeof indmap / sizeof indmap[0]; ++i)
1624 indmap[i] = -1;
1625 thischarcnt = 0;
1626 for (i = 0; i < typecnt; ++i) {
1627 register char * thisabbr;
1629 if (!writetype[i])
1630 continue;
1631 if (indmap[abbrinds[i]] >= 0)
1632 continue;
1633 thisabbr = &chars[abbrinds[i]];
1634 for (j = 0; j < thischarcnt; ++j)
1635 if (strcmp(&thischars[j], thisabbr) == 0)
1636 break;
1637 if (j == thischarcnt) {
1638 (void) strcpy(&thischars[(int) thischarcnt],
1639 thisabbr);
1640 thischarcnt += strlen(thisabbr) + 1;
1642 indmap[abbrinds[i]] = j;
1644 #define DO(field) (void) fwrite((void *) tzh.field, \
1645 (size_t) sizeof tzh.field, (size_t) 1, fp)
1646 tzh = tzh0;
1647 (void) strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1648 tzh.tzh_version[0] = ZIC_VERSION;
1649 convert(eitol(thistypecnt), tzh.tzh_ttisgmtcnt);
1650 convert(eitol(thistypecnt), tzh.tzh_ttisstdcnt);
1651 convert(eitol(thisleapcnt), tzh.tzh_leapcnt);
1652 convert(eitol(thistimecnt), tzh.tzh_timecnt);
1653 convert(eitol(thistypecnt), tzh.tzh_typecnt);
1654 convert(eitol(thischarcnt), tzh.tzh_charcnt);
1655 DO(tzh_magic);
1656 DO(tzh_version);
1657 DO(tzh_reserved);
1658 DO(tzh_ttisgmtcnt);
1659 DO(tzh_ttisstdcnt);
1660 DO(tzh_leapcnt);
1661 DO(tzh_timecnt);
1662 DO(tzh_typecnt);
1663 DO(tzh_charcnt);
1664 #undef DO
1665 for (i = thistimei; i < thistimelim; ++i)
1666 if (pass == 1)
1667 puttzcode((long) ats[i], fp);
1668 else puttzcode64(ats[i], fp);
1669 for (i = thistimei; i < thistimelim; ++i) {
1670 unsigned char uc;
1672 uc = typemap[types[i]];
1673 (void) fwrite((void *) &uc,
1674 (size_t) sizeof uc,
1675 (size_t) 1,
1676 fp);
1678 for (i = 0; i < typecnt; ++i)
1679 if (writetype[i]) {
1680 puttzcode(gmtoffs[i], fp);
1681 (void) putc(isdsts[i], fp);
1682 (void) putc((unsigned char) indmap[abbrinds[i]], fp);
1684 if (thischarcnt != 0)
1685 (void) fwrite((void *) thischars,
1686 (size_t) sizeof thischars[0],
1687 (size_t) thischarcnt, fp);
1688 for (i = thisleapi; i < thisleaplim; ++i) {
1689 register zic_t todo;
1691 if (roll[i]) {
1692 if (timecnt == 0 || trans[i] < ats[0]) {
1693 j = 0;
1694 while (isdsts[j])
1695 if (++j >= typecnt) {
1696 j = 0;
1697 break;
1699 } else {
1700 j = 1;
1701 while (j < timecnt &&
1702 trans[i] >= ats[j])
1703 ++j;
1704 j = types[j - 1];
1706 todo = tadd(trans[i], -gmtoffs[j]);
1707 } else todo = trans[i];
1708 if (pass == 1)
1709 puttzcode((long) todo, fp);
1710 else puttzcode64(todo, fp);
1711 puttzcode(corr[i], fp);
1713 for (i = 0; i < typecnt; ++i)
1714 if (writetype[i])
1715 (void) putc(ttisstds[i], fp);
1716 for (i = 0; i < typecnt; ++i)
1717 if (writetype[i])
1718 (void) putc(ttisgmts[i], fp);
1720 (void) fprintf(fp, "\n%s\n", string);
1721 if (ferror(fp) || fclose(fp)) {
1722 (void) fprintf(stderr, _("%s: Error writing %s\n"),
1723 progname, fullname);
1724 exit(EXIT_FAILURE);
1728 static void
1729 doabbr(abbr, format, letters, isdst, doquotes)
1730 char * const abbr;
1731 const char * const format;
1732 const char * const letters;
1733 const int isdst;
1734 const int doquotes;
1736 register char * cp;
1737 register char * slashp;
1738 register int len;
1740 slashp = strchr(format, '/');
1741 if (slashp == NULL) {
1742 if (letters == NULL)
1743 (void) strcpy(abbr, format);
1744 else (void) sprintf(abbr, format, letters);
1745 } else if (isdst) {
1746 (void) strcpy(abbr, slashp + 1);
1747 } else {
1748 if (slashp > format)
1749 (void) strncpy(abbr, format,
1750 (unsigned) (slashp - format));
1751 abbr[slashp - format] = '\0';
1753 if (!doquotes)
1754 return;
1755 for (cp = abbr; *cp != '\0'; ++cp)
1756 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp) == NULL &&
1757 strchr("abcdefghijklmnopqrstuvwxyz", *cp) == NULL)
1758 break;
1759 len = strlen(abbr);
1760 if (len > 0 && *cp == '\0')
1761 return;
1762 abbr[len + 2] = '\0';
1763 abbr[len + 1] = '>';
1764 for ( ; len > 0; --len)
1765 abbr[len] = abbr[len - 1];
1766 abbr[0] = '<';
1769 static void
1770 updateminmax(x)
1771 const int x;
1773 if (min_year > x)
1774 min_year = x;
1775 if (max_year < x)
1776 max_year = x;
1779 static int
1780 stringoffset(result, offset)
1781 char * result;
1782 long offset;
1784 register int hours;
1785 register int minutes;
1786 register int seconds;
1788 result[0] = '\0';
1789 if (offset < 0) {
1790 (void) strcpy(result, "-");
1791 offset = -offset;
1793 seconds = offset % SECSPERMIN;
1794 offset /= SECSPERMIN;
1795 minutes = offset % MINSPERHOUR;
1796 offset /= MINSPERHOUR;
1797 hours = offset;
1798 if (hours >= HOURSPERDAY) {
1799 result[0] = '\0';
1800 return -1;
1802 (void) sprintf(end(result), "%d", hours);
1803 if (minutes != 0 || seconds != 0) {
1804 (void) sprintf(end(result), ":%02d", minutes);
1805 if (seconds != 0)
1806 (void) sprintf(end(result), ":%02d", seconds);
1808 return 0;
1811 static int
1812 stringrule(result, rp, dstoff, gmtoff)
1813 char * result;
1814 const struct rule * const rp;
1815 const long dstoff;
1816 const long gmtoff;
1818 register long tod;
1820 result = end(result);
1821 if (rp->r_dycode == DC_DOM) {
1822 register int month, total;
1824 if (rp->r_dayofmonth == 29 && rp->r_month == TM_FEBRUARY)
1825 return -1;
1826 total = 0;
1827 for (month = 0; month < rp->r_month; ++month)
1828 total += len_months[0][month];
1829 (void) sprintf(result, "J%d", total + rp->r_dayofmonth);
1830 } else {
1831 register int week;
1833 if (rp->r_dycode == DC_DOWGEQ) {
1834 week = 1 + rp->r_dayofmonth / DAYSPERWEEK;
1835 if ((week - 1) * DAYSPERWEEK + 1 != rp->r_dayofmonth)
1836 return -1;
1837 } else if (rp->r_dycode == DC_DOWLEQ) {
1838 if (rp->r_dayofmonth == len_months[1][rp->r_month])
1839 week = 5;
1840 else {
1841 week = 1 + rp->r_dayofmonth / DAYSPERWEEK;
1842 if (week * DAYSPERWEEK - 1 != rp->r_dayofmonth)
1843 return -1;
1845 } else return -1; /* "cannot happen" */
1846 (void) sprintf(result, "M%d.%d.%d",
1847 rp->r_month + 1, week, rp->r_wday);
1849 tod = rp->r_tod;
1850 if (rp->r_todisgmt)
1851 tod += gmtoff;
1852 if (rp->r_todisstd && rp->r_stdoff == 0)
1853 tod += dstoff;
1854 if (tod < 0) {
1855 result[0] = '\0';
1856 return -1;
1858 if (tod != 2 * SECSPERMIN * MINSPERHOUR) {
1859 (void) strcat(result, "/");
1860 if (stringoffset(end(result), tod) != 0)
1861 return -1;
1863 return 0;
1866 static void
1867 stringzone(result, zpfirst, zonecount)
1868 char * result;
1869 const struct zone * const zpfirst;
1870 const int zonecount;
1872 register const struct zone * zp;
1873 register struct rule * rp;
1874 register struct rule * stdrp;
1875 register struct rule * dstrp;
1876 register int i;
1877 register const char * abbrvar;
1879 result[0] = '\0';
1880 zp = zpfirst + zonecount - 1;
1881 stdrp = dstrp = NULL;
1882 for (i = 0; i < zp->z_nrules; ++i) {
1883 rp = &zp->z_rules[i];
1884 if (rp->r_hiwasnum || rp->r_hiyear != INT_MAX)
1885 continue;
1886 if (rp->r_yrtype != NULL)
1887 continue;
1888 if (rp->r_stdoff == 0) {
1889 if (stdrp == NULL)
1890 stdrp = rp;
1891 else return;
1892 } else {
1893 if (dstrp == NULL)
1894 dstrp = rp;
1895 else return;
1898 if (stdrp == NULL && dstrp == NULL) {
1900 ** There are no rules running through "max".
1901 ** Let's find the latest rule.
1903 for (i = 0; i < zp->z_nrules; ++i) {
1904 rp = &zp->z_rules[i];
1905 if (stdrp == NULL || rp->r_hiyear > stdrp->r_hiyear ||
1906 (rp->r_hiyear == stdrp->r_hiyear &&
1907 rp->r_month > stdrp->r_month))
1908 stdrp = rp;
1910 if (stdrp != NULL && stdrp->r_stdoff != 0)
1911 return; /* We end up in DST (a POSIX no-no). */
1913 ** Horrid special case: if year is 2037,
1914 ** presume this is a zone handled on a year-by-year basis;
1915 ** do not try to apply a rule to the zone.
1917 if (stdrp != NULL && stdrp->r_hiyear == 2037)
1918 return;
1920 if (stdrp == NULL && zp->z_nrules != 0)
1921 return;
1922 abbrvar = (stdrp == NULL) ? "" : stdrp->r_abbrvar;
1923 doabbr(result, zp->z_format, abbrvar, FALSE, TRUE);
1924 if (stringoffset(end(result), -zp->z_gmtoff) != 0) {
1925 result[0] = '\0';
1926 return;
1928 if (dstrp == NULL)
1929 return;
1930 doabbr(end(result), zp->z_format, dstrp->r_abbrvar, TRUE, TRUE);
1931 if (dstrp->r_stdoff != SECSPERMIN * MINSPERHOUR)
1932 if (stringoffset(end(result),
1933 -(zp->z_gmtoff + dstrp->r_stdoff)) != 0) {
1934 result[0] = '\0';
1935 return;
1937 (void) strcat(result, ",");
1938 if (stringrule(result, dstrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) {
1939 result[0] = '\0';
1940 return;
1942 (void) strcat(result, ",");
1943 if (stringrule(result, stdrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) {
1944 result[0] = '\0';
1945 return;
1949 static void
1950 outzone(zpfirst, zonecount)
1951 const struct zone * const zpfirst;
1952 const int zonecount;
1954 register const struct zone * zp;
1955 register struct rule * rp;
1956 register int i, j;
1957 register int usestart, useuntil;
1958 register zic_t starttime, untiltime;
1959 register long gmtoff;
1960 register long stdoff;
1961 register int year;
1962 register long startoff;
1963 register int startttisstd;
1964 register int startttisgmt;
1965 register int type;
1966 register char * startbuf;
1967 register char * ab;
1968 register char * envvar;
1969 register int max_abbr_len;
1970 register int max_envvar_len;
1972 max_abbr_len = 2 + max_format_len + max_abbrvar_len;
1973 max_envvar_len = 2 * max_abbr_len + 5 * 9;
1974 startbuf = emalloc(max_abbr_len + 1);
1975 ab = emalloc(max_abbr_len + 1);
1976 envvar = emalloc(max_envvar_len + 1);
1977 INITIALIZE(untiltime);
1978 INITIALIZE(starttime);
1980 ** Now. . .finally. . .generate some useful data!
1982 timecnt = 0;
1983 typecnt = 0;
1984 charcnt = 0;
1986 ** Thanks to Earl Chew
1987 ** for noting the need to unconditionally initialize startttisstd.
1989 startttisstd = FALSE;
1990 startttisgmt = FALSE;
1991 min_year = max_year = EPOCH_YEAR;
1992 if (leapseen) {
1993 updateminmax(leapminyear);
1994 updateminmax(leapmaxyear);
1996 for (i = 0; i < zonecount; ++i) {
1997 zp = &zpfirst[i];
1998 if (i < zonecount - 1)
1999 updateminmax(zp->z_untilrule.r_loyear);
2000 for (j = 0; j < zp->z_nrules; ++j) {
2001 rp = &zp->z_rules[j];
2002 if (rp->r_lowasnum)
2003 updateminmax(rp->r_loyear);
2004 if (rp->r_hiwasnum)
2005 updateminmax(rp->r_hiyear);
2009 ** Generate lots of data if a rule can't cover all future times.
2011 stringzone(envvar, zpfirst, zonecount);
2012 if (noise && envvar[0] == '\0') {
2013 register char * wp;
2015 wp = ecpyalloc(_("no POSIX environment variable for zone"));
2016 wp = ecatalloc(wp, " ");
2017 wp = ecatalloc(wp, zpfirst->z_name);
2018 warning(wp);
2019 ifree(wp);
2021 if (envvar[0] == '\0') {
2022 if (min_year >= INT_MIN + YEARSPERREPEAT)
2023 min_year -= YEARSPERREPEAT;
2024 else min_year = INT_MIN;
2025 if (max_year <= INT_MAX - YEARSPERREPEAT)
2026 max_year += YEARSPERREPEAT;
2027 else max_year = INT_MAX;
2030 ** For the benefit of older systems,
2031 ** generate data from 1900 through 2037.
2033 if (min_year > 1900)
2034 min_year = 1900;
2035 if (max_year < 2037)
2036 max_year = 2037;
2037 for (i = 0; i < zonecount; ++i) {
2039 ** A guess that may well be corrected later.
2041 stdoff = 0;
2042 zp = &zpfirst[i];
2043 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
2044 useuntil = i < (zonecount - 1);
2045 if (useuntil && zp->z_untiltime <= min_time)
2046 continue;
2047 gmtoff = zp->z_gmtoff;
2048 eat(zp->z_filename, zp->z_linenum);
2049 *startbuf = '\0';
2050 startoff = zp->z_gmtoff;
2051 if (zp->z_nrules == 0) {
2052 stdoff = zp->z_stdoff;
2053 doabbr(startbuf, zp->z_format,
2054 (char *) NULL, stdoff != 0, FALSE);
2055 type = addtype(oadd(zp->z_gmtoff, stdoff),
2056 startbuf, stdoff != 0, startttisstd,
2057 startttisgmt);
2058 if (usestart) {
2059 addtt(starttime, type);
2060 usestart = FALSE;
2061 } else if (stdoff != 0)
2062 addtt(min_time, type);
2063 } else for (year = min_year; year <= max_year; ++year) {
2064 if (useuntil && year > zp->z_untilrule.r_hiyear)
2065 break;
2067 ** Mark which rules to do in the current year.
2068 ** For those to do, calculate rpytime(rp, year);
2070 for (j = 0; j < zp->z_nrules; ++j) {
2071 rp = &zp->z_rules[j];
2072 eats(zp->z_filename, zp->z_linenum,
2073 rp->r_filename, rp->r_linenum);
2074 rp->r_todo = year >= rp->r_loyear &&
2075 year <= rp->r_hiyear &&
2076 yearistype(year, rp->r_yrtype);
2077 if (rp->r_todo)
2078 rp->r_temp = rpytime(rp, year);
2080 for ( ; ; ) {
2081 register int k;
2082 register zic_t jtime, ktime;
2083 register long offset;
2085 INITIALIZE(ktime);
2086 if (useuntil) {
2088 ** Turn untiltime into UTC
2089 ** assuming the current gmtoff and
2090 ** stdoff values.
2092 untiltime = zp->z_untiltime;
2093 if (!zp->z_untilrule.r_todisgmt)
2094 untiltime = tadd(untiltime,
2095 -gmtoff);
2096 if (!zp->z_untilrule.r_todisstd)
2097 untiltime = tadd(untiltime,
2098 -stdoff);
2101 ** Find the rule (of those to do, if any)
2102 ** that takes effect earliest in the year.
2104 k = -1;
2105 for (j = 0; j < zp->z_nrules; ++j) {
2106 rp = &zp->z_rules[j];
2107 if (!rp->r_todo)
2108 continue;
2109 eats(zp->z_filename, zp->z_linenum,
2110 rp->r_filename, rp->r_linenum);
2111 offset = rp->r_todisgmt ? 0 : gmtoff;
2112 if (!rp->r_todisstd)
2113 offset = oadd(offset, stdoff);
2114 jtime = rp->r_temp;
2115 if (jtime == min_time ||
2116 jtime == max_time)
2117 continue;
2118 jtime = tadd(jtime, -offset);
2119 if (k < 0 || jtime < ktime) {
2120 k = j;
2121 ktime = jtime;
2124 if (k < 0)
2125 break; /* go on to next year */
2126 rp = &zp->z_rules[k];
2127 rp->r_todo = FALSE;
2128 if (useuntil && ktime >= untiltime)
2129 break;
2130 stdoff = rp->r_stdoff;
2131 if (usestart && ktime == starttime)
2132 usestart = FALSE;
2133 if (usestart) {
2134 if (ktime < starttime) {
2135 startoff = oadd(zp->z_gmtoff,
2136 stdoff);
2137 doabbr(startbuf, zp->z_format,
2138 rp->r_abbrvar,
2139 rp->r_stdoff != 0,
2140 FALSE);
2141 continue;
2143 if (*startbuf == '\0' &&
2144 startoff == oadd(zp->z_gmtoff,
2145 stdoff)) {
2146 doabbr(startbuf,
2147 zp->z_format,
2148 rp->r_abbrvar,
2149 rp->r_stdoff !=
2151 FALSE);
2154 eats(zp->z_filename, zp->z_linenum,
2155 rp->r_filename, rp->r_linenum);
2156 doabbr(ab, zp->z_format, rp->r_abbrvar,
2157 rp->r_stdoff != 0, FALSE);
2158 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
2159 type = addtype(offset, ab, rp->r_stdoff != 0,
2160 rp->r_todisstd, rp->r_todisgmt);
2161 addtt(ktime, type);
2164 if (usestart) {
2165 if (*startbuf == '\0' &&
2166 zp->z_format != NULL &&
2167 strchr(zp->z_format, '%') == NULL &&
2168 strchr(zp->z_format, '/') == NULL)
2169 (void) strcpy(startbuf, zp->z_format);
2170 eat(zp->z_filename, zp->z_linenum);
2171 if (*startbuf == '\0')
2172 error(_("can't determine time zone abbreviation to use just after until time"));
2173 else addtt(starttime,
2174 addtype(startoff, startbuf,
2175 startoff != zp->z_gmtoff,
2176 startttisstd,
2177 startttisgmt));
2180 ** Now we may get to set starttime for the next zone line.
2182 if (useuntil) {
2183 startttisstd = zp->z_untilrule.r_todisstd;
2184 startttisgmt = zp->z_untilrule.r_todisgmt;
2185 starttime = zp->z_untiltime;
2186 if (!startttisstd)
2187 starttime = tadd(starttime, -stdoff);
2188 if (!startttisgmt)
2189 starttime = tadd(starttime, -gmtoff);
2192 writezone(zpfirst->z_name, envvar);
2193 ifree(startbuf);
2194 ifree(ab);
2195 ifree(envvar);
2198 static void
2199 addtt(starttime, type)
2200 const zic_t starttime;
2201 int type;
2203 if (starttime <= min_time ||
2204 (timecnt == 1 && attypes[0].at < min_time)) {
2205 gmtoffs[0] = gmtoffs[type];
2206 isdsts[0] = isdsts[type];
2207 ttisstds[0] = ttisstds[type];
2208 ttisgmts[0] = ttisgmts[type];
2209 if (abbrinds[type] != 0)
2210 (void) strcpy(chars, &chars[abbrinds[type]]);
2211 abbrinds[0] = 0;
2212 charcnt = strlen(chars) + 1;
2213 typecnt = 1;
2214 timecnt = 0;
2215 type = 0;
2217 if (timecnt >= TZ_MAX_TIMES) {
2218 error(_("too many transitions?!"));
2219 exit(EXIT_FAILURE);
2221 attypes[timecnt].at = starttime;
2222 attypes[timecnt].type = type;
2223 ++timecnt;
2226 static int
2227 addtype(gmtoff, abbr, isdst, ttisstd, ttisgmt)
2228 const long gmtoff;
2229 const char * const abbr;
2230 const int isdst;
2231 const int ttisstd;
2232 const int ttisgmt;
2234 register int i, j;
2236 if (isdst != TRUE && isdst != FALSE) {
2237 error(_("internal error - addtype called with bad isdst"));
2238 exit(EXIT_FAILURE);
2240 if (ttisstd != TRUE && ttisstd != FALSE) {
2241 error(_("internal error - addtype called with bad ttisstd"));
2242 exit(EXIT_FAILURE);
2244 if (ttisgmt != TRUE && ttisgmt != FALSE) {
2245 error(_("internal error - addtype called with bad ttisgmt"));
2246 exit(EXIT_FAILURE);
2249 ** See if there's already an entry for this zone type.
2250 ** If so, just return its index.
2252 for (i = 0; i < typecnt; ++i) {
2253 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
2254 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
2255 ttisstd == ttisstds[i] &&
2256 ttisgmt == ttisgmts[i])
2257 return i;
2260 ** There isn't one; add a new one, unless there are already too
2261 ** many.
2263 if (typecnt >= TZ_MAX_TYPES) {
2264 error(_("too many local time types"));
2265 exit(EXIT_FAILURE);
2267 if (! (-1L - 2147483647L <= gmtoff && gmtoff <= 2147483647L)) {
2268 error(_("UTC offset out of range"));
2269 exit(EXIT_FAILURE);
2271 gmtoffs[i] = gmtoff;
2272 isdsts[i] = isdst;
2273 ttisstds[i] = ttisstd;
2274 ttisgmts[i] = ttisgmt;
2276 for (j = 0; j < charcnt; ++j)
2277 if (strcmp(&chars[j], abbr) == 0)
2278 break;
2279 if (j == charcnt)
2280 newabbr(abbr);
2281 abbrinds[i] = j;
2282 ++typecnt;
2283 return i;
2286 static void
2287 leapadd(t, positive, rolling, count)
2288 const zic_t t;
2289 const int positive;
2290 const int rolling;
2291 int count;
2293 register int i, j;
2295 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
2296 error(_("too many leap seconds"));
2297 exit(EXIT_FAILURE);
2299 for (i = 0; i < leapcnt; ++i)
2300 if (t <= trans[i]) {
2301 if (t == trans[i]) {
2302 error(_("repeated leap second moment"));
2303 exit(EXIT_FAILURE);
2305 break;
2307 do {
2308 for (j = leapcnt; j > i; --j) {
2309 trans[j] = trans[j - 1];
2310 corr[j] = corr[j - 1];
2311 roll[j] = roll[j - 1];
2313 trans[i] = t;
2314 corr[i] = positive ? 1L : eitol(-count);
2315 roll[i] = rolling;
2316 ++leapcnt;
2317 } while (positive && --count != 0);
2320 static void
2321 adjleap(void)
2323 register int i;
2324 register long last = 0;
2327 ** propagate leap seconds forward
2329 for (i = 0; i < leapcnt; ++i) {
2330 trans[i] = tadd(trans[i], last);
2331 last = corr[i] += last;
2335 static int
2336 yearistype(year, type)
2337 const int year;
2338 const char * const type;
2340 static char * buf;
2341 int result;
2343 if (type == NULL || *type == '\0')
2344 return TRUE;
2345 buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
2346 (void) sprintf(buf, "%s %d %s", yitcommand, year, type);
2347 result = system(buf);
2348 if (WIFEXITED(result)) switch (WEXITSTATUS(result)) {
2349 case 0:
2350 return TRUE;
2351 case 1:
2352 return FALSE;
2354 error(_("Wild result from command execution"));
2355 (void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
2356 progname, buf, result);
2357 for ( ; ; )
2358 exit(EXIT_FAILURE);
2361 static int
2362 lowerit(a)
2363 int a;
2365 a = (unsigned char) a;
2366 return (isascii(a) && isupper(a)) ? tolower(a) : a;
2369 static int
2370 ciequal(ap, bp) /* case-insensitive equality */
2371 register const char * ap;
2372 register const char * bp;
2374 while (lowerit(*ap) == lowerit(*bp++))
2375 if (*ap++ == '\0')
2376 return TRUE;
2377 return FALSE;
2380 static int
2381 itsabbr(abbr, word)
2382 register const char * abbr;
2383 register const char * word;
2385 if (lowerit(*abbr) != lowerit(*word))
2386 return FALSE;
2387 ++word;
2388 while (*++abbr != '\0')
2389 do {
2390 if (*word == '\0')
2391 return FALSE;
2392 } while (lowerit(*word++) != lowerit(*abbr));
2393 return TRUE;
2396 static const struct lookup *
2397 byword(word, table)
2398 register const char * const word;
2399 register const struct lookup * const table;
2401 register const struct lookup * foundlp;
2402 register const struct lookup * lp;
2404 if (word == NULL || table == NULL)
2405 return NULL;
2407 ** Look for exact match.
2409 for (lp = table; lp->l_word != NULL; ++lp)
2410 if (ciequal(word, lp->l_word))
2411 return lp;
2413 ** Look for inexact match.
2415 foundlp = NULL;
2416 for (lp = table; lp->l_word != NULL; ++lp)
2417 if (itsabbr(word, lp->l_word)) {
2418 if (foundlp == NULL)
2419 foundlp = lp;
2420 else return NULL; /* multiple inexact matches */
2422 return foundlp;
2425 static char **
2426 getfields(cp)
2427 register char * cp;
2429 register char * dp;
2430 register char ** array;
2431 register int nsubs;
2433 if (cp == NULL)
2434 return NULL;
2435 array = (char **) (void *)
2436 emalloc((int) ((strlen(cp) + 1) * sizeof *array));
2437 nsubs = 0;
2438 for ( ; ; ) {
2439 while (isascii((unsigned char) *cp) &&
2440 isspace((unsigned char) *cp))
2441 ++cp;
2442 if (*cp == '\0' || *cp == '#')
2443 break;
2444 array[nsubs++] = dp = cp;
2445 do {
2446 if ((*dp = *cp++) != '"')
2447 ++dp;
2448 else while ((*dp = *cp++) != '"')
2449 if (*dp != '\0')
2450 ++dp;
2451 else {
2452 error(_(
2453 "Odd number of quotation marks"
2455 exit(1);
2457 } while (*cp != '\0' && *cp != '#' &&
2458 (!isascii(*cp) || !isspace((unsigned char) *cp)));
2459 if (isascii(*cp) && isspace((unsigned char) *cp))
2460 ++cp;
2461 *dp = '\0';
2463 array[nsubs] = NULL;
2464 return array;
2467 static long
2468 oadd(t1, t2)
2469 const long t1;
2470 const long t2;
2472 register long t;
2474 t = t1 + t2;
2475 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2476 error(_("time overflow"));
2477 exit(EXIT_FAILURE);
2479 return t;
2482 static zic_t
2483 tadd(t1, t2)
2484 const zic_t t1;
2485 const long t2;
2487 register zic_t t;
2489 if (t1 == max_time && t2 > 0)
2490 return max_time;
2491 if (t1 == min_time && t2 < 0)
2492 return min_time;
2493 t = t1 + t2;
2494 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2495 error(_("time overflow"));
2496 exit(EXIT_FAILURE);
2498 return t;
2502 ** Given a rule, and a year, compute the date - in seconds since January 1,
2503 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2506 static zic_t
2507 rpytime(rp, wantedy)
2508 register const struct rule * const rp;
2509 register const int wantedy;
2511 register int y, m, i;
2512 register long dayoff; /* with a nod to Margaret O. */
2513 register zic_t t;
2515 if (wantedy == INT_MIN)
2516 return min_time;
2517 if (wantedy == INT_MAX)
2518 return max_time;
2519 dayoff = 0;
2520 m = TM_JANUARY;
2521 y = EPOCH_YEAR;
2522 while (wantedy != y) {
2523 if (wantedy > y) {
2524 i = len_years[isleap(y)];
2525 ++y;
2526 } else {
2527 --y;
2528 i = -len_years[isleap(y)];
2530 dayoff = oadd(dayoff, eitol(i));
2532 while (m != rp->r_month) {
2533 i = len_months[isleap(y)][m];
2534 dayoff = oadd(dayoff, eitol(i));
2535 ++m;
2537 i = rp->r_dayofmonth;
2538 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
2539 if (rp->r_dycode == DC_DOWLEQ)
2540 --i;
2541 else {
2542 error(_("use of 2/29 in non leap-year"));
2543 exit(EXIT_FAILURE);
2546 --i;
2547 dayoff = oadd(dayoff, eitol(i));
2548 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
2549 register long wday;
2551 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2552 wday = eitol(EPOCH_WDAY);
2554 ** Don't trust mod of negative numbers.
2556 if (dayoff >= 0)
2557 wday = (wday + dayoff) % LDAYSPERWEEK;
2558 else {
2559 wday -= ((-dayoff) % LDAYSPERWEEK);
2560 if (wday < 0)
2561 wday += LDAYSPERWEEK;
2563 while (wday != eitol(rp->r_wday))
2564 if (rp->r_dycode == DC_DOWGEQ) {
2565 dayoff = oadd(dayoff, (long) 1);
2566 if (++wday >= LDAYSPERWEEK)
2567 wday = 0;
2568 ++i;
2569 } else {
2570 dayoff = oadd(dayoff, (long) -1);
2571 if (--wday < 0)
2572 wday = LDAYSPERWEEK - 1;
2573 --i;
2575 if (i < 0 || i >= len_months[isleap(y)][m]) {
2576 if (noise)
2577 warning(_("rule goes past start/end of month--\
2578 will not work with pre-2004 versions of zic"));
2581 if (dayoff < min_time / SECSPERDAY)
2582 return min_time;
2583 if (dayoff > max_time / SECSPERDAY)
2584 return max_time;
2585 t = (zic_t) dayoff * SECSPERDAY;
2586 return tadd(t, rp->r_tod);
2589 static void
2590 newabbr(string)
2591 const char * const string;
2593 register int i;
2595 if (strcmp(string, GRANDPARENTED) != 0) {
2596 register const char * cp;
2597 register char * wp;
2600 ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2601 ** optionally followed by a + or - and a number from 1 to 14.
2603 cp = string;
2604 wp = NULL;
2605 while (isascii((unsigned char) *cp) &&
2606 isalpha((unsigned char) *cp))
2607 ++cp;
2608 if (cp - string == 0)
2609 wp = _("time zone abbreviation lacks alphabetic at start");
2610 if (noise && cp - string > 3)
2611 wp = _("time zone abbreviation has more than 3 alphabetics");
2612 if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
2613 wp = _("time zone abbreviation has too many alphabetics");
2614 if (wp == NULL && (*cp == '+' || *cp == '-')) {
2615 ++cp;
2616 if (isascii((unsigned char) *cp) &&
2617 isdigit((unsigned char) *cp))
2618 if (*cp++ == '1' &&
2619 *cp >= '0' && *cp <= '4')
2620 ++cp;
2622 if (*cp != '\0')
2623 wp = _("time zone abbreviation differs from POSIX standard");
2624 if (wp != NULL) {
2625 wp = ecpyalloc(wp);
2626 wp = ecatalloc(wp, " (");
2627 wp = ecatalloc(wp, string);
2628 wp = ecatalloc(wp, ")");
2629 warning(wp);
2630 ifree(wp);
2633 i = strlen(string) + 1;
2634 if (charcnt + i > TZ_MAX_CHARS) {
2635 error(_("too many, or too long, time zone abbreviations"));
2636 exit(EXIT_FAILURE);
2638 (void) strcpy(&chars[charcnt], string);
2639 charcnt += eitol(i);
2642 static int
2643 mkdirs(argname)
2644 char * argname;
2646 register char * name;
2647 register char * cp;
2649 if (argname == NULL || *argname == '\0')
2650 return 0;
2651 cp = name = ecpyalloc(argname);
2652 while ((cp = strchr(cp + 1, '/')) != 0) {
2653 *cp = '\0';
2654 #ifndef unix
2656 ** DOS drive specifier?
2658 if (isalpha((unsigned char) name[0]) &&
2659 name[1] == ':' && name[2] == '\0') {
2660 *cp = '/';
2661 continue;
2663 #endif /* !defined unix */
2664 if (!itsdir(name)) {
2666 ** It doesn't seem to exist, so we try to create it.
2667 ** Creation may fail because of the directory being
2668 ** created by some other multiprocessor, so we get
2669 ** to do extra checking.
2671 if (mkdir(name, MKDIR_UMASK) != 0) {
2672 const char *e = strerror(errno);
2674 if (errno != EEXIST || !itsdir(name)) {
2675 (void) fprintf(stderr,
2676 _("%s: Can't create directory %s: %s\n"),
2677 progname, name, e);
2678 ifree(name);
2679 return -1;
2683 *cp = '/';
2685 ifree(name);
2686 return 0;
2689 static long
2690 eitol(i)
2691 const int i;
2693 long l;
2695 l = i;
2696 if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0)) {
2697 (void) fprintf(stderr,
2698 _("%s: %d did not sign extend correctly\n"),
2699 progname, i);
2700 exit(EXIT_FAILURE);
2702 return l;
2706 ** UNIX was a registered trademark of The Open Group in 2003.