some coverity fixes.
[minix.git] / lib / libc / time / zic.c
blob3b59b6cd1f184b59c53540ed87a1d178304aa140
1 /* $NetBSD: zic.c,v 1.28 2011/01/15 12:31:57 martin Exp $ */
2 /*
3 ** This file is in the public domain, so clarified as of
4 ** 2006-07-17 by Arthur David Olson.
5 */
7 #if HAVE_NBTOOL_CONFIG_H
8 #include "nbtool_config.h"
9 #endif
11 #include <sys/cdefs.h>
12 #ifndef lint
13 __RCSID("$NetBSD: zic.c,v 1.28 2011/01/15 12:31:57 martin Exp $");
14 #endif /* !defined lint */
16 static char elsieid[] = "@(#)zic.c 8.20";
18 #include "private.h"
19 #include "locale.h"
20 #include "tzfile.h"
22 #define ZIC_VERSION '2'
24 typedef int_fast64_t zic_t;
26 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
27 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
28 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
30 #if HAVE_SYS_STAT_H
31 #include "sys/stat.h"
32 #endif
33 #ifdef S_IRUSR
34 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
35 #else
36 #define MKDIR_UMASK 0755
37 #endif
39 #include "unistd.h"
42 ** On some ancient hosts, predicates like `isspace(C)' are defined
43 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
44 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
45 ** Neither the C Standard nor Posix require that `isascii' exist.
46 ** For portability, we check both ancient and modern requirements.
47 ** If isascii is not defined, the isascii check succeeds trivially.
49 #include "ctype.h"
50 #ifndef isascii
51 #define isascii(x) 1
52 #endif
54 #define OFFSET_STRLEN_MAXIMUM (7 + INT_STRLEN_MAXIMUM(long))
55 #define RULE_STRLEN_MAXIMUM 8 /* "Mdd.dd.d" */
57 #define end(cp) (strchr((cp), '\0'))
59 struct rule {
60 const char * r_filename;
61 int r_linenum;
62 const char * r_name;
64 int r_loyear; /* for example, 1986 */
65 int r_hiyear; /* for example, 1986 */
66 const char * r_yrtype;
67 int r_lowasnum;
68 int r_hiwasnum;
70 int r_month; /* 0..11 */
72 int r_dycode; /* see below */
73 int r_dayofmonth;
74 int r_wday;
76 long r_tod; /* time from midnight */
77 int r_todisstd; /* above is standard time if TRUE */
78 /* or wall clock time if FALSE */
79 int r_todisgmt; /* above is GMT if TRUE */
80 /* or local time if FALSE */
81 long r_stdoff; /* offset from standard time */
82 const char * r_abbrvar; /* variable part of abbreviation */
84 int r_todo; /* a rule to do (used in outzone) */
85 zic_t r_temp; /* used in outzone */
89 ** r_dycode r_dayofmonth r_wday
92 #define DC_DOM 0 /* 1..31 */ /* unused */
93 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
94 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
96 struct zone {
97 const char * z_filename;
98 int z_linenum;
100 const char * z_name;
101 long z_gmtoff;
102 const char * z_rule;
103 const char * z_format;
105 long z_stdoff;
107 struct rule * z_rules;
108 int z_nrules;
110 struct rule z_untilrule;
111 zic_t z_untiltime;
114 extern int getopt(int argc, char * const argv[],
115 const char * options);
116 extern int link(const char * fromname, const char * toname);
117 extern char * optarg;
118 extern int optind;
120 static void addtt(zic_t starttime, int type);
121 static int addtype(long gmtoff, const char * abbr, int isdst,
122 int ttisstd, int ttisgmt);
123 static void leapadd(zic_t t, int positive, int rolling, int count);
124 static void adjleap(void);
125 static void associate(void);
126 static int ciequal(const char * ap, const char * bp);
127 static void convert(long val, char * buf);
128 static void convert64(zic_t val, char * buf);
129 static void dolink(const char * fromfield, const char * tofield);
130 static void doabbr(char * abbr, const int, const char * format,
131 const char * letters, int isdst, int doquotes);
132 static void eat(const char * name, int num);
133 static void eats(const char * name, int num,
134 const char * rname, int rnum);
135 static long eitol(int i);
136 static void error(const char * message);
137 static char ** getfields(char * buf);
138 static long gethms(const char * string, const char * errstrng,
139 int signable);
140 static void infile(const char * filename);
141 static void inleap(char ** fields, int nfields);
142 static void inlink(char ** fields, int nfields);
143 static void inrule(char ** fields, int nfields);
144 static int inzcont(char ** fields, int nfields);
145 static int inzone(char ** fields, int nfields);
146 static int inzsub(char ** fields, int nfields, int iscont);
147 static int is32(zic_t x);
148 static int itsabbr(const char * abbr, const char * word);
149 static int itsdir(const char * name);
150 static int lowerit(int c);
151 int main(int, char **);
152 static char * memcheck(char * tocheck);
153 static int mkdirs(char * filename);
154 static void newabbr(const char * abbr);
155 static long oadd(long t1, long t2);
156 static void outzone(const struct zone * zp, int ntzones);
157 static void puttzcode(long code, FILE * fp);
158 static void puttzcode64(zic_t code, FILE * fp);
159 static int rcomp(const void * leftp, const void * rightp);
160 static zic_t rpytime(const struct rule * rp, int wantedy);
161 static void rulesub(struct rule * rp,
162 const char * loyearp, const char * hiyearp,
163 const char * typep, const char * monthp,
164 const char * dayp, const char * timep);
165 static int stringoffset(char * result, long offset);
166 static int stringrule(char * result, const struct rule * rp,
167 long dstoff, long gmtoff);
168 static void stringzone(char * result, const int,
169 const struct zone * zp, int ntzones);
170 static void setboundaries(void);
171 static zic_t tadd(zic_t t1, long t2);
172 static void usage(FILE *stream, int status);
173 static void warning(const char * const);
174 static void writezone(const char * name, const char * string);
175 static int yearistype(int year, const char * type);
176 static int atcomp(const void *avp, const void *bvp);
177 static void updateminmax(int x);
179 static int charcnt;
180 static int errors;
181 static const char * filename;
182 static int leapcnt;
183 static int leapseen;
184 static int leapminyear;
185 static int leapmaxyear;
186 static int linenum;
187 static int max_abbrvar_len;
188 static int max_format_len;
189 static zic_t max_time;
190 static int max_year;
191 static zic_t min_time;
192 static int min_year;
193 static int noise;
194 static const char * rfilename;
195 static int rlinenum;
196 static const char * progname;
197 static int timecnt;
198 static int typecnt;
201 ** Line codes.
204 #define LC_RULE 0
205 #define LC_ZONE 1
206 #define LC_LINK 2
207 #define LC_LEAP 3
210 ** Which fields are which on a Zone line.
213 #define ZF_NAME 1
214 #define ZF_GMTOFF 2
215 #define ZF_RULE 3
216 #define ZF_FORMAT 4
217 #define ZF_TILYEAR 5
218 #define ZF_TILMONTH 6
219 #define ZF_TILDAY 7
220 #define ZF_TILTIME 8
221 #define ZONE_MINFIELDS 5
222 #define ZONE_MAXFIELDS 9
225 ** Which fields are which on a Zone continuation line.
228 #define ZFC_GMTOFF 0
229 #define ZFC_RULE 1
230 #define ZFC_FORMAT 2
231 #define ZFC_TILYEAR 3
232 #define ZFC_TILMONTH 4
233 #define ZFC_TILDAY 5
234 #define ZFC_TILTIME 6
235 #define ZONEC_MINFIELDS 3
236 #define ZONEC_MAXFIELDS 7
239 ** Which files are which on a Rule line.
242 #define RF_NAME 1
243 #define RF_LOYEAR 2
244 #define RF_HIYEAR 3
245 #define RF_COMMAND 4
246 #define RF_MONTH 5
247 #define RF_DAY 6
248 #define RF_TOD 7
249 #define RF_STDOFF 8
250 #define RF_ABBRVAR 9
251 #define RULE_FIELDS 10
254 ** Which fields are which on a Link line.
257 #define LF_FROM 1
258 #define LF_TO 2
259 #define LINK_FIELDS 3
262 ** Which fields are which on a Leap line.
265 #define LP_YEAR 1
266 #define LP_MONTH 2
267 #define LP_DAY 3
268 #define LP_TIME 4
269 #define LP_CORR 5
270 #define LP_ROLL 6
271 #define LEAP_FIELDS 7
274 ** Year synonyms.
277 #define YR_MINIMUM 0
278 #define YR_MAXIMUM 1
279 #define YR_ONLY 2
281 static struct rule * rules;
282 static int nrules; /* number of rules */
284 static struct zone * zones;
285 static int nzones; /* number of zones */
287 struct link {
288 const char * l_filename;
289 int l_linenum;
290 const char * l_from;
291 const char * l_to;
294 static struct link * links;
295 static int nlinks;
297 struct lookup {
298 const char * l_word;
299 const int l_value;
302 static struct lookup const * byword(const char * string,
303 const struct lookup * lp);
305 static struct lookup const line_codes[] = {
306 { "Rule", LC_RULE },
307 { "Zone", LC_ZONE },
308 { "Link", LC_LINK },
309 { "Leap", LC_LEAP },
310 { NULL, 0}
313 static struct lookup const mon_names[] = {
314 { "January", TM_JANUARY },
315 { "February", TM_FEBRUARY },
316 { "March", TM_MARCH },
317 { "April", TM_APRIL },
318 { "May", TM_MAY },
319 { "June", TM_JUNE },
320 { "July", TM_JULY },
321 { "August", TM_AUGUST },
322 { "September", TM_SEPTEMBER },
323 { "October", TM_OCTOBER },
324 { "November", TM_NOVEMBER },
325 { "December", TM_DECEMBER },
326 { NULL, 0 }
329 static struct lookup const wday_names[] = {
330 { "Sunday", TM_SUNDAY },
331 { "Monday", TM_MONDAY },
332 { "Tuesday", TM_TUESDAY },
333 { "Wednesday", TM_WEDNESDAY },
334 { "Thursday", TM_THURSDAY },
335 { "Friday", TM_FRIDAY },
336 { "Saturday", TM_SATURDAY },
337 { NULL, 0 }
340 static struct lookup const lasts[] = {
341 { "last-Sunday", TM_SUNDAY },
342 { "last-Monday", TM_MONDAY },
343 { "last-Tuesday", TM_TUESDAY },
344 { "last-Wednesday", TM_WEDNESDAY },
345 { "last-Thursday", TM_THURSDAY },
346 { "last-Friday", TM_FRIDAY },
347 { "last-Saturday", TM_SATURDAY },
348 { NULL, 0 }
351 static struct lookup const begin_years[] = {
352 { "minimum", YR_MINIMUM },
353 { "maximum", YR_MAXIMUM },
354 { NULL, 0 }
357 static struct lookup const end_years[] = {
358 { "minimum", YR_MINIMUM },
359 { "maximum", YR_MAXIMUM },
360 { "only", YR_ONLY },
361 { NULL, 0 }
364 static struct lookup const leap_types[] = {
365 { "Rolling", TRUE },
366 { "Stationary", FALSE },
367 { NULL, 0 }
370 static const int len_months[2][MONSPERYEAR] = {
371 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
372 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
375 static const int len_years[2] = {
376 DAYSPERNYEAR, DAYSPERLYEAR
379 static struct attype {
380 zic_t at;
381 unsigned char type;
382 } attypes[TZ_MAX_TIMES];
383 static long gmtoffs[TZ_MAX_TYPES];
384 static char isdsts[TZ_MAX_TYPES];
385 static unsigned char abbrinds[TZ_MAX_TYPES];
386 static char ttisstds[TZ_MAX_TYPES];
387 static char ttisgmts[TZ_MAX_TYPES];
388 static char chars[TZ_MAX_CHARS];
389 static zic_t trans[TZ_MAX_LEAPS];
390 static long corr[TZ_MAX_LEAPS];
391 static char roll[TZ_MAX_LEAPS];
394 ** Memory allocation.
397 static char *
398 memcheck(ptr)
399 char * const ptr;
401 if (ptr == NULL) {
402 const char *e = strerror(errno);
404 (void) fprintf(stderr, _("%s: Memory exhausted: %s\n"),
405 progname, e);
406 exit(EXIT_FAILURE);
408 return ptr;
411 #define emalloc(size) memcheck(imalloc(size))
412 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
413 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
414 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
417 ** Error handling.
420 static void
421 eats(name, num, rname, rnum)
422 const char * const name;
423 const int num;
424 const char * const rname;
425 const int rnum;
427 filename = name;
428 linenum = num;
429 rfilename = rname;
430 rlinenum = rnum;
433 static void
434 eat(name, num)
435 const char * const name;
436 const int num;
438 eats(name, num, (char *) NULL, -1);
441 static void
442 error(string)
443 const char * const string;
446 ** Match the format of "cc" to allow sh users to
447 ** zic ... 2>&1 | error -t "*" -v
448 ** on BSD systems.
450 (void) fprintf(stderr, _("\"%s\", line %d: %s"),
451 filename, linenum, string);
452 if (rfilename != NULL)
453 (void) fprintf(stderr, _(" (rule from \"%s\", line %d)"),
454 rfilename, rlinenum);
455 (void) fprintf(stderr, "\n");
456 ++errors;
459 static void
460 warning(string)
461 const char * const string;
463 char * cp;
465 cp = ecpyalloc(_("warning: "));
466 cp = ecatalloc(cp, string);
467 error(cp);
468 ifree(cp);
469 --errors;
472 static void
473 usage(FILE *stream, int status)
475 (void) fprintf(stream, _("%s: usage is %s \
476 [ --version ] [ --help ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\
477 \t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n\
479 Report bugs to tz@elsie.nci.nih.gov.\n"),
480 progname, progname);
481 exit(status);
484 static const char * psxrules;
485 static const char * lcltime;
486 static const char * directory;
487 static const char * leapsec;
488 static const char * yitcommand;
491 main(argc, argv)
492 int argc;
493 char * argv[];
495 register int i;
496 register int j;
497 register int c;
499 #ifdef _POSIX_VERSION
500 (void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
501 #endif /* defined _POSIX_VERSION */
502 #if HAVE_GETTEXT - 0
503 (void) setlocale(LC_MESSAGES, "");
504 #ifdef TZ_DOMAINDIR
505 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
506 #endif /* defined TEXTDOMAINDIR */
507 (void) textdomain(TZ_DOMAIN);
508 #endif /* HAVE_GETTEXT */
509 progname = argv[0];
510 if (TYPE_BIT(zic_t) < 64) {
511 (void) fprintf(stderr, "%s: %s\n", progname,
512 _("wild compilation-time specification of zic_t"));
513 exit(EXIT_FAILURE);
515 for (i = 1; i < argc; ++i)
516 if (strcmp(argv[i], "--version") == 0) {
517 (void) printf("%s\n", elsieid);
518 exit(EXIT_SUCCESS);
519 } else if (strcmp(argv[i], "--help") == 0) {
520 usage(stdout, EXIT_SUCCESS);
522 while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF && c != -1)
523 switch (c) {
524 default:
525 usage(stderr, EXIT_FAILURE);
526 case 'd':
527 if (directory == NULL)
528 directory = optarg;
529 else {
530 (void) fprintf(stderr,
531 _("%s: More than one -d option specified\n"),
532 progname);
533 exit(EXIT_FAILURE);
535 break;
536 case 'l':
537 if (lcltime == NULL)
538 lcltime = optarg;
539 else {
540 (void) fprintf(stderr,
541 _("%s: More than one -l option specified\n"),
542 progname);
543 exit(EXIT_FAILURE);
545 break;
546 case 'p':
547 if (psxrules == NULL)
548 psxrules = optarg;
549 else {
550 (void) fprintf(stderr,
551 _("%s: More than one -p option specified\n"),
552 progname);
553 exit(EXIT_FAILURE);
555 break;
556 case 'y':
557 if (yitcommand == NULL)
558 yitcommand = optarg;
559 else {
560 (void) fprintf(stderr,
561 _("%s: More than one -y option specified\n"),
562 progname);
563 exit(EXIT_FAILURE);
565 break;
566 case 'L':
567 if (leapsec == NULL)
568 leapsec = optarg;
569 else {
570 (void) fprintf(stderr,
571 _("%s: More than one -L option specified\n"),
572 progname);
573 exit(EXIT_FAILURE);
575 break;
576 case 'v':
577 noise = TRUE;
578 break;
579 case 's':
580 (void) printf("%s: -s ignored\n", progname);
581 break;
583 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
584 usage(stderr, EXIT_FAILURE); /* usage message by request */
585 if (directory == NULL)
586 directory = TZDIR;
587 if (yitcommand == NULL)
588 yitcommand = "yearistype";
590 setboundaries();
592 if (optind < argc && leapsec != NULL) {
593 infile(leapsec);
594 adjleap();
597 for (i = optind; i < argc; ++i)
598 infile(argv[i]);
599 if (errors)
600 exit(EXIT_FAILURE);
601 associate();
602 for (i = 0; i < nzones; i = j) {
604 ** Find the next non-continuation zone entry.
606 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
607 continue;
608 outzone(&zones[i], j - i);
611 ** Make links.
613 for (i = 0; i < nlinks; ++i) {
614 eat(links[i].l_filename, links[i].l_linenum);
615 dolink(links[i].l_from, links[i].l_to);
616 if (noise)
617 for (j = 0; j < nlinks; ++j)
618 if (strcmp(links[i].l_to,
619 links[j].l_from) == 0)
620 warning(_("link to link"));
622 if (lcltime != NULL) {
623 eat("command line", 1);
624 dolink(lcltime, TZDEFAULT);
626 if (psxrules != NULL) {
627 eat("command line", 1);
628 dolink(psxrules, TZDEFRULES);
630 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
633 static void
634 dolink(fromfield, tofield)
635 const char * const fromfield;
636 const char * const tofield;
638 register char * fromname;
639 register char * toname;
641 if (fromfield[0] == '/')
642 fromname = ecpyalloc(fromfield);
643 else {
644 fromname = ecpyalloc(directory);
645 fromname = ecatalloc(fromname, "/");
646 fromname = ecatalloc(fromname, fromfield);
648 if (tofield[0] == '/')
649 toname = ecpyalloc(tofield);
650 else {
651 toname = ecpyalloc(directory);
652 toname = ecatalloc(toname, "/");
653 toname = ecatalloc(toname, tofield);
656 ** We get to be careful here since
657 ** there's a fair chance of root running us.
659 if (!itsdir(toname))
660 (void) remove(toname);
661 if (link(fromname, toname) != 0) {
662 int result;
664 if (mkdirs(toname) != 0)
665 exit(EXIT_FAILURE);
667 result = link(fromname, toname);
668 #if HAVE_SYMLINK
669 if (result != 0 &&
670 access(fromname, F_OK) == 0 &&
671 !itsdir(fromname)) {
672 const char *s = tofield;
673 register char * symlinkcontents = NULL;
675 while ((s = strchr(s+1, '/')) != NULL)
676 symlinkcontents =
677 ecatalloc(symlinkcontents,
678 "../");
679 symlinkcontents =
680 ecatalloc(symlinkcontents,
681 fromname);
682 result = symlink(symlinkcontents,
683 toname);
684 if (result == 0)
685 warning(_("hard link failed, symbolic link used"));
686 ifree(symlinkcontents);
688 #endif /* HAVE_SYMLINK */
689 if (result != 0) {
690 const char *e = strerror(errno);
692 (void) fprintf(stderr,
693 _("%s: Can't link from %s to %s: %s\n"),
694 progname, fromname, toname, e);
695 exit(EXIT_FAILURE);
698 ifree(fromname);
699 ifree(toname);
702 #define TIME_T_BITS_IN_FILE 64
704 static void
705 setboundaries(void)
707 register int i;
709 min_time = -1;
710 for (i = 0; i < TIME_T_BITS_IN_FILE - 1; ++i)
711 min_time *= 2;
712 max_time = -(min_time + 1);
715 static int
716 itsdir(name)
717 const char * const name;
719 register char * myname;
720 register int accres;
722 myname = ecpyalloc(name);
723 myname = ecatalloc(myname, "/.");
724 accres = access(myname, F_OK);
725 ifree(myname);
726 return accres == 0;
730 ** Associate sets of rules with zones.
734 ** Sort by rule name.
737 static int
738 rcomp(cp1, cp2)
739 const void * cp1;
740 const void * cp2;
742 return strcmp(((const struct rule *) cp1)->r_name,
743 ((const struct rule *) cp2)->r_name);
746 static void
747 associate(void)
749 register struct zone * zp;
750 register struct rule * rp;
751 register int base, out;
752 register int i, j;
754 if (nrules != 0) {
755 (void) qsort((void *) rules, (size_t) nrules,
756 (size_t) sizeof *rules, rcomp);
757 for (i = 0; i < nrules - 1; ++i) {
758 if (strcmp(rules[i].r_name,
759 rules[i + 1].r_name) != 0)
760 continue;
761 if (strcmp(rules[i].r_filename,
762 rules[i + 1].r_filename) == 0)
763 continue;
764 eat(rules[i].r_filename, rules[i].r_linenum);
765 warning(_("same rule name in multiple files"));
766 eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
767 warning(_("same rule name in multiple files"));
768 for (j = i + 2; j < nrules; ++j) {
769 if (strcmp(rules[i].r_name,
770 rules[j].r_name) != 0)
771 break;
772 if (strcmp(rules[i].r_filename,
773 rules[j].r_filename) == 0)
774 continue;
775 if (strcmp(rules[i + 1].r_filename,
776 rules[j].r_filename) == 0)
777 continue;
778 break;
780 i = j - 1;
783 for (i = 0; i < nzones; ++i) {
784 zp = &zones[i];
785 zp->z_rules = NULL;
786 zp->z_nrules = 0;
788 for (base = 0; base < nrules; base = out) {
789 rp = &rules[base];
790 for (out = base + 1; out < nrules; ++out)
791 if (strcmp(rp->r_name, rules[out].r_name) != 0)
792 break;
793 for (i = 0; i < nzones; ++i) {
794 zp = &zones[i];
795 if (strcmp(zp->z_rule, rp->r_name) != 0)
796 continue;
797 zp->z_rules = rp;
798 zp->z_nrules = out - base;
801 for (i = 0; i < nzones; ++i) {
802 zp = &zones[i];
803 if (zp->z_nrules == 0) {
805 ** Maybe we have a local standard time offset.
807 eat(zp->z_filename, zp->z_linenum);
808 zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
809 TRUE);
811 ** Note, though, that if there's no rule,
812 ** a '%s' in the format is a bad thing.
814 if (strchr(zp->z_format, '%') != 0)
815 error(_("%s in ruleless zone"));
818 if (errors)
819 exit(EXIT_FAILURE);
822 static void
823 infile(name)
824 const char * name;
826 register FILE * fp;
827 register char ** fields;
828 register char * cp;
829 register const struct lookup * lp;
830 register int nfields;
831 register int wantcont;
832 register int num;
833 char buf[BUFSIZ];
835 if (strcmp(name, "-") == 0) {
836 name = _("standard input");
837 fp = stdin;
838 } else if ((fp = fopen(name, "r")) == NULL) {
839 const char *e = strerror(errno);
841 (void) fprintf(stderr, _("%s: Can't open %s: %s\n"),
842 progname, name, e);
843 exit(EXIT_FAILURE);
845 wantcont = FALSE;
846 for (num = 1; ; ++num) {
847 eat(name, num);
848 if (fgets(buf, (int) sizeof buf, fp) != buf)
849 break;
850 cp = strchr(buf, '\n');
851 if (cp == NULL) {
852 error(_("line too long"));
853 exit(EXIT_FAILURE);
855 *cp = '\0';
856 fields = getfields(buf);
857 nfields = 0;
858 while (fields[nfields] != NULL) {
859 static char nada;
861 if (strcmp(fields[nfields], "-") == 0)
862 fields[nfields] = &nada;
863 ++nfields;
865 if (nfields == 0) {
866 /* nothing to do */
867 } else if (wantcont) {
868 wantcont = inzcont(fields, nfields);
869 } else {
870 lp = byword(fields[0], line_codes);
871 if (lp == NULL)
872 error(_("input line of unknown type"));
873 else switch ((int) (lp->l_value)) {
874 case LC_RULE:
875 inrule(fields, nfields);
876 wantcont = FALSE;
877 break;
878 case LC_ZONE:
879 wantcont = inzone(fields, nfields);
880 break;
881 case LC_LINK:
882 inlink(fields, nfields);
883 wantcont = FALSE;
884 break;
885 case LC_LEAP:
886 if (name != leapsec)
887 (void) fprintf(stderr,
888 _("%s: Leap line in non leap seconds file %s\n"),
889 progname, name);
890 else inleap(fields, nfields);
891 wantcont = FALSE;
892 break;
893 default: /* "cannot happen" */
894 (void) fprintf(stderr,
895 _("%s: panic: Invalid l_value %d\n"),
896 progname, lp->l_value);
897 exit(EXIT_FAILURE);
900 ifree((char *) fields);
902 if (ferror(fp)) {
903 (void) fprintf(stderr, _("%s: Error reading %s\n"),
904 progname, filename);
905 exit(EXIT_FAILURE);
907 if (fp != stdin && fclose(fp)) {
908 const char *e = strerror(errno);
910 (void) fprintf(stderr, _("%s: Error closing %s: %s\n"),
911 progname, filename, e);
912 exit(EXIT_FAILURE);
914 if (wantcont)
915 error(_("expected continuation line not found"));
919 ** Convert a string of one of the forms
920 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
921 ** into a number of seconds.
922 ** A null string maps to zero.
923 ** Call error with errstring and return zero on errors.
926 static long
927 gethms(string, errstring, signable)
928 const char * string;
929 const char * const errstring;
930 const int signable;
932 long hh;
933 int mm, ss, sign;
935 if (string == NULL || *string == '\0')
936 return 0;
937 if (!signable)
938 sign = 1;
939 else if (*string == '-') {
940 sign = -1;
941 ++string;
942 } else sign = 1;
943 if (sscanf(string, scheck(string, "%ld"), &hh) == 1)
944 mm = ss = 0;
945 else if (sscanf(string, scheck(string, "%ld:%d"), &hh, &mm) == 2)
946 ss = 0;
947 else if (sscanf(string, scheck(string, "%ld:%d:%d"),
948 &hh, &mm, &ss) != 3) {
949 error(errstring);
950 return 0;
952 if (hh < 0 ||
953 mm < 0 || mm >= MINSPERHOUR ||
954 ss < 0 || ss > SECSPERMIN) {
955 error(errstring);
956 return 0;
958 if (LONG_MAX / SECSPERHOUR < hh) {
959 error(_("time overflow"));
960 return 0;
962 if (noise && hh == HOURSPERDAY && mm == 0 && ss == 0)
963 warning(_("24:00 not handled by pre-1998 versions of zic"));
964 if (noise && (hh > HOURSPERDAY ||
965 (hh == HOURSPERDAY && (mm != 0 || ss != 0))))
966 warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
967 return oadd(eitol(sign) * hh * eitol(SECSPERHOUR),
968 eitol(sign) * (eitol(mm) * 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 if (max_abbrvar_len < strlen(r.r_abbrvar))
994 max_abbrvar_len = strlen(r.r_abbrvar);
995 rules = (struct rule *) (void *) erealloc((char *) rules,
996 (int) ((nrules + 1) * sizeof *rules));
997 rules[nrules++] = r;
1000 static int
1001 inzone(fields, nfields)
1002 register char ** const fields;
1003 const int nfields;
1005 register int i;
1006 static char * buf;
1008 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
1009 error(_("wrong number of fields on Zone line"));
1010 return FALSE;
1012 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
1013 buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
1014 (void)sprintf(buf, /* XXX: sprintf is safe */
1015 _("\"Zone %s\" line and -l option are mutually exclusive"),
1016 TZDEFAULT);
1017 error(buf);
1018 return FALSE;
1020 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
1021 buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
1022 (void)sprintf(buf, /* XXX: sprintf is safe */
1023 _("\"Zone %s\" line and -p option are mutually exclusive"),
1024 TZDEFRULES);
1025 error(buf);
1026 return FALSE;
1028 for (i = 0; i < nzones; ++i)
1029 if (zones[i].z_name != NULL &&
1030 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
1031 buf = erealloc(buf, (int) (132 +
1032 strlen(fields[ZF_NAME]) +
1033 strlen(zones[i].z_filename)));
1034 (void)sprintf(buf, /* XXX: sprintf is safe */
1035 _("duplicate zone name %s (file \"%s\", line %d)"),
1036 fields[ZF_NAME],
1037 zones[i].z_filename,
1038 zones[i].z_linenum);
1039 error(buf);
1040 return FALSE;
1042 return inzsub(fields, nfields, FALSE);
1045 static int
1046 inzcont(fields, nfields)
1047 register char ** const fields;
1048 const int nfields;
1050 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
1051 error(_("wrong number of fields on Zone continuation line"));
1052 return FALSE;
1054 return inzsub(fields, nfields, TRUE);
1057 static int
1058 inzsub(fields, nfields, iscont)
1059 register char ** const fields;
1060 const int nfields;
1061 const int iscont;
1063 register char * cp;
1064 static struct zone z;
1065 register int i_gmtoff, i_rule, i_format;
1066 register int i_untilyear, i_untilmonth;
1067 register int i_untilday, i_untiltime;
1068 register int hasuntil;
1070 if (iscont) {
1071 i_gmtoff = ZFC_GMTOFF;
1072 i_rule = ZFC_RULE;
1073 i_format = ZFC_FORMAT;
1074 i_untilyear = ZFC_TILYEAR;
1075 i_untilmonth = ZFC_TILMONTH;
1076 i_untilday = ZFC_TILDAY;
1077 i_untiltime = ZFC_TILTIME;
1078 z.z_name = NULL;
1079 } else {
1080 i_gmtoff = ZF_GMTOFF;
1081 i_rule = ZF_RULE;
1082 i_format = ZF_FORMAT;
1083 i_untilyear = ZF_TILYEAR;
1084 i_untilmonth = ZF_TILMONTH;
1085 i_untilday = ZF_TILDAY;
1086 i_untiltime = ZF_TILTIME;
1087 z.z_name = ecpyalloc(fields[ZF_NAME]);
1089 z.z_filename = filename;
1090 z.z_linenum = linenum;
1091 z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UTC offset"), TRUE);
1092 if ((cp = strchr(fields[i_format], '%')) != 0) {
1093 if (*++cp != 's' || strchr(cp, '%') != 0) {
1094 error(_("invalid abbreviation format"));
1095 return FALSE;
1098 z.z_rule = ecpyalloc(fields[i_rule]);
1099 z.z_format = ecpyalloc(fields[i_format]);
1100 if (max_format_len < strlen(z.z_format))
1101 max_format_len = strlen(z.z_format);
1102 hasuntil = nfields > i_untilyear;
1103 if (hasuntil) {
1104 z.z_untilrule.r_filename = filename;
1105 z.z_untilrule.r_linenum = linenum;
1106 rulesub(&z.z_untilrule,
1107 fields[i_untilyear],
1108 "only",
1110 (nfields > i_untilmonth) ?
1111 fields[i_untilmonth] : "Jan",
1112 (nfields > i_untilday) ? fields[i_untilday] : "1",
1113 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1114 z.z_untiltime = rpytime(&z.z_untilrule,
1115 z.z_untilrule.r_loyear);
1116 if (iscont && nzones > 0 &&
1117 z.z_untiltime > min_time &&
1118 z.z_untiltime < max_time &&
1119 zones[nzones - 1].z_untiltime > min_time &&
1120 zones[nzones - 1].z_untiltime < max_time &&
1121 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1122 error(_(
1123 "Zone continuation line end time is not after end time of previous line"
1125 return FALSE;
1128 zones = (struct zone *) (void *) erealloc((char *) zones,
1129 (int) ((nzones + 1) * sizeof *zones));
1130 zones[nzones++] = z;
1132 ** If there was an UNTIL field on this line,
1133 ** there's more information about the zone on the next line.
1135 return hasuntil;
1138 static void
1139 inleap(fields, nfields)
1140 register char ** const fields;
1141 const int nfields;
1143 register const char * cp;
1144 register const struct lookup * lp;
1145 register int i, j;
1146 int year, month, day;
1147 long dayoff, tod;
1148 zic_t t;
1150 if (nfields != LEAP_FIELDS) {
1151 error(_("wrong number of fields on Leap line"));
1152 return;
1154 dayoff = 0;
1155 cp = fields[LP_YEAR];
1156 if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
1158 ** Leapin' Lizards!
1160 error(_("invalid leaping year"));
1161 return;
1163 if (!leapseen || leapmaxyear < year)
1164 leapmaxyear = year;
1165 if (!leapseen || leapminyear > year)
1166 leapminyear = year;
1167 leapseen = TRUE;
1168 j = EPOCH_YEAR;
1169 while (j != year) {
1170 if (year > j) {
1171 i = len_years[isleap(j)];
1172 ++j;
1173 } else {
1174 --j;
1175 i = -len_years[isleap(j)];
1177 dayoff = oadd(dayoff, eitol(i));
1179 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1180 error(_("invalid month name"));
1181 return;
1183 month = lp->l_value;
1184 j = TM_JANUARY;
1185 while (j != month) {
1186 i = len_months[isleap(year)][j];
1187 dayoff = oadd(dayoff, eitol(i));
1188 ++j;
1190 cp = fields[LP_DAY];
1191 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1192 day <= 0 || day > len_months[isleap(year)][month]) {
1193 error(_("invalid day of month"));
1194 return;
1196 dayoff = oadd(dayoff, eitol(day - 1));
1197 if (dayoff < 0 && !TYPE_SIGNED(zic_t)) {
1198 error(_("time before zero"));
1199 return;
1201 if (dayoff < min_time / SECSPERDAY) {
1202 error(_("time too small"));
1203 return;
1205 if (dayoff > max_time / SECSPERDAY) {
1206 error(_("time too large"));
1207 return;
1209 t = (zic_t) dayoff * SECSPERDAY;
1210 tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1211 cp = fields[LP_CORR];
1213 register int positive;
1214 int count;
1216 if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1217 positive = FALSE;
1218 count = 1;
1219 } else if (strcmp(cp, "--") == 0) {
1220 positive = FALSE;
1221 count = 2;
1222 } else if (strcmp(cp, "+") == 0) {
1223 positive = TRUE;
1224 count = 1;
1225 } else if (strcmp(cp, "++") == 0) {
1226 positive = TRUE;
1227 count = 2;
1228 } else {
1229 error(_("illegal CORRECTION field on Leap line"));
1230 return;
1232 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1233 error(_(
1234 "illegal Rolling/Stationary field on Leap line"
1236 return;
1238 leapadd(tadd(t, tod), positive, lp->l_value, count);
1242 static void
1243 inlink(fields, nfields)
1244 register char ** const fields;
1245 const int nfields;
1247 struct link l;
1249 if (nfields != LINK_FIELDS) {
1250 error(_("wrong number of fields on Link line"));
1251 return;
1253 if (*fields[LF_FROM] == '\0') {
1254 error(_("blank FROM field on Link line"));
1255 return;
1257 if (*fields[LF_TO] == '\0') {
1258 error(_("blank TO field on Link line"));
1259 return;
1261 l.l_filename = filename;
1262 l.l_linenum = linenum;
1263 l.l_from = ecpyalloc(fields[LF_FROM]);
1264 l.l_to = ecpyalloc(fields[LF_TO]);
1265 links = (struct link *) (void *) erealloc((char *) links,
1266 (int) ((nlinks + 1) * sizeof *links));
1267 links[nlinks++] = l;
1270 static void
1271 rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
1272 register struct rule * const rp;
1273 const char * const loyearp;
1274 const char * const hiyearp;
1275 const char * const typep;
1276 const char * const monthp;
1277 const char * const dayp;
1278 const char * const timep;
1280 register const struct lookup * lp;
1281 register const char * cp;
1282 register char * dp;
1283 register char * ep;
1285 if ((lp = byword(monthp, mon_names)) == NULL) {
1286 error(_("invalid month name"));
1287 return;
1289 rp->r_month = lp->l_value;
1290 rp->r_todisstd = FALSE;
1291 rp->r_todisgmt = FALSE;
1292 dp = ecpyalloc(timep);
1293 if (*dp != '\0') {
1294 ep = dp + strlen(dp) - 1;
1295 switch (lowerit(*ep)) {
1296 case 's': /* Standard */
1297 rp->r_todisstd = TRUE;
1298 rp->r_todisgmt = FALSE;
1299 *ep = '\0';
1300 break;
1301 case 'w': /* Wall */
1302 rp->r_todisstd = FALSE;
1303 rp->r_todisgmt = FALSE;
1304 *ep = '\0';
1305 break;
1306 case 'g': /* Greenwich */
1307 case 'u': /* Universal */
1308 case 'z': /* Zulu */
1309 rp->r_todisstd = TRUE;
1310 rp->r_todisgmt = TRUE;
1311 *ep = '\0';
1312 break;
1315 rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1316 ifree(dp);
1318 ** Year work.
1320 cp = loyearp;
1321 lp = byword(cp, begin_years);
1322 rp->r_lowasnum = lp == NULL;
1323 if (!rp->r_lowasnum) switch ((int) lp->l_value) {
1324 case YR_MINIMUM:
1325 rp->r_loyear = INT_MIN;
1326 break;
1327 case YR_MAXIMUM:
1328 rp->r_loyear = INT_MAX;
1329 break;
1330 default: /* "cannot happen" */
1331 (void) fprintf(stderr,
1332 _("%s: panic: Invalid l_value %d\n"),
1333 progname, lp->l_value);
1334 exit(EXIT_FAILURE);
1335 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
1336 error(_("invalid starting year"));
1337 return;
1339 cp = hiyearp;
1340 lp = byword(cp, end_years);
1341 rp->r_hiwasnum = lp == NULL;
1342 if (!rp->r_hiwasnum) switch ((int) lp->l_value) {
1343 case YR_MINIMUM:
1344 rp->r_hiyear = INT_MIN;
1345 break;
1346 case YR_MAXIMUM:
1347 rp->r_hiyear = INT_MAX;
1348 break;
1349 case YR_ONLY:
1350 rp->r_hiyear = rp->r_loyear;
1351 break;
1352 default: /* "cannot happen" */
1353 (void) fprintf(stderr,
1354 _("%s: panic: Invalid l_value %d\n"),
1355 progname, lp->l_value);
1356 exit(EXIT_FAILURE);
1357 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
1358 error(_("invalid ending year"));
1359 return;
1361 if (rp->r_loyear > rp->r_hiyear) {
1362 error(_("starting year greater than ending year"));
1363 return;
1365 if (*typep == '\0')
1366 rp->r_yrtype = NULL;
1367 else {
1368 if (rp->r_loyear == rp->r_hiyear) {
1369 error(_("typed single year"));
1370 return;
1372 rp->r_yrtype = ecpyalloc(typep);
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 int shift;
1429 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1430 buf[i] = val >> shift;
1433 static void
1434 convert64(val, buf)
1435 const zic_t val;
1436 char * const buf;
1438 register int i;
1439 register int shift;
1441 for (i = 0, shift = 56; i < 8; ++i, shift -= 8)
1442 buf[i] = val >> shift;
1445 static void
1446 puttzcode(val, fp)
1447 const long val;
1448 FILE * const fp;
1450 char buf[4];
1452 convert(val, buf);
1453 (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1456 static void
1457 puttzcode64(val, fp)
1458 const zic_t val;
1459 FILE * const fp;
1461 char buf[8];
1463 convert64(val, buf);
1464 (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1467 static int
1468 atcomp(avp, bvp)
1469 const void * avp;
1470 const void * bvp;
1472 const zic_t a = ((const struct attype *) avp)->at;
1473 const zic_t b = ((const struct attype *) bvp)->at;
1475 return (a < b) ? -1 : (a > b);
1478 static int
1479 is32(x)
1480 const zic_t x;
1482 return INT32_MIN <= x && x <= INT32_MAX;
1485 static void
1486 writezone(name, string)
1487 const char * const name;
1488 const char * const string;
1490 register FILE * fp;
1491 register int i, j;
1492 register int leapcnt32, leapi32;
1493 register int timecnt32, timei32;
1494 register int pass;
1495 static char * fullname;
1496 static const struct tzhead tzh0;
1497 static struct tzhead tzh;
1498 zic_t ats[TZ_MAX_TIMES];
1499 unsigned char types[TZ_MAX_TIMES];
1502 ** Sort.
1504 if (timecnt > 1)
1505 (void) qsort((void *) attypes, (size_t) timecnt,
1506 (size_t) sizeof *attypes, atcomp);
1508 ** Optimize.
1511 int fromi;
1512 int toi;
1514 toi = 0;
1515 fromi = 0;
1516 while (fromi < timecnt && attypes[fromi].at < min_time)
1517 ++fromi;
1518 if (isdsts[0] == 0)
1519 while (fromi < timecnt && attypes[fromi].type == 0)
1520 ++fromi; /* handled by default rule */
1521 for ( ; fromi < timecnt; ++fromi) {
1522 if (toi != 0 && ((attypes[fromi].at +
1523 gmtoffs[attypes[toi - 1].type]) <=
1524 (attypes[toi - 1].at + gmtoffs[toi == 1 ? 0
1525 : attypes[toi - 2].type]))) {
1526 attypes[toi - 1].type =
1527 attypes[fromi].type;
1528 continue;
1530 if (toi == 0 ||
1531 attypes[toi - 1].type != attypes[fromi].type)
1532 attypes[toi++] = attypes[fromi];
1534 timecnt = toi;
1537 ** Transfer.
1539 for (i = 0; i < timecnt; ++i) {
1540 ats[i] = attypes[i].at;
1541 types[i] = attypes[i].type;
1544 ** Correct for leap seconds.
1546 for (i = 0; i < timecnt; ++i) {
1547 j = leapcnt;
1548 while (--j >= 0)
1549 if (ats[i] > trans[j] - corr[j]) {
1550 ats[i] = tadd(ats[i], corr[j]);
1551 break;
1555 ** Figure out 32-bit-limited starts and counts.
1557 timecnt32 = timecnt;
1558 timei32 = 0;
1559 leapcnt32 = leapcnt;
1560 leapi32 = 0;
1561 while (timecnt32 > 0 && !is32(ats[timecnt32 - 1]))
1562 --timecnt32;
1563 while (timecnt32 > 0 && !is32(ats[timei32])) {
1564 --timecnt32;
1565 ++timei32;
1567 while (leapcnt32 > 0 && !is32(trans[leapcnt32 - 1]))
1568 --leapcnt32;
1569 while (leapcnt32 > 0 && !is32(trans[leapi32])) {
1570 --leapcnt32;
1571 ++leapi32;
1573 fullname = erealloc(fullname,
1574 (int) (strlen(directory) + 1 + strlen(name) + 1));
1575 (void) sprintf(fullname, "%s/%s", directory, name); /* XXX: sprintf is safe */
1577 ** Remove old file, if any, to snap links.
1579 if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT) {
1580 const char *e = strerror(errno);
1582 (void) fprintf(stderr, _("%s: Can't remove %s: %s\n"),
1583 progname, fullname, e);
1584 exit(EXIT_FAILURE);
1586 if ((fp = fopen(fullname, "wb")) == NULL) {
1587 if (mkdirs(fullname) != 0)
1588 exit(EXIT_FAILURE);
1589 if ((fp = fopen(fullname, "wb")) == NULL) {
1590 const char *e = strerror(errno);
1592 (void) fprintf(stderr, _("%s: Can't create %s: %s\n"),
1593 progname, fullname, e);
1594 exit(EXIT_FAILURE);
1597 for (pass = 1; pass <= 2; ++pass) {
1598 register int thistimei, thistimecnt;
1599 register int thisleapi, thisleapcnt;
1600 register int thistimelim, thisleaplim;
1601 int writetype[TZ_MAX_TIMES];
1602 int typemap[TZ_MAX_TYPES];
1603 register int thistypecnt;
1604 char thischars[TZ_MAX_CHARS];
1605 char thischarcnt;
1606 int indmap[TZ_MAX_CHARS];
1608 if (pass == 1) {
1609 thistimei = timei32;
1610 thistimecnt = timecnt32;
1611 thisleapi = leapi32;
1612 thisleapcnt = leapcnt32;
1613 } else {
1614 thistimei = 0;
1615 thistimecnt = timecnt;
1616 thisleapi = 0;
1617 thisleapcnt = leapcnt;
1619 thistimelim = thistimei + thistimecnt;
1620 thisleaplim = thisleapi + thisleapcnt;
1621 for (i = 0; i < typecnt; ++i)
1622 writetype[i] = thistimecnt == timecnt;
1623 if (thistimecnt == 0) {
1625 ** No transition times fall in the current
1626 ** (32- or 64-bit) window.
1628 if (typecnt != 0)
1629 writetype[typecnt - 1] = TRUE;
1630 } else {
1631 for (i = thistimei - 1; i < thistimelim; ++i)
1632 if (i >= 0)
1633 writetype[types[i]] = TRUE;
1635 ** For America/Godthab and Antarctica/Palmer
1637 if (thistimei == 0)
1638 writetype[0] = TRUE;
1640 thistypecnt = 0;
1641 for (i = 0; i < typecnt; ++i)
1642 typemap[i] = writetype[i] ? thistypecnt++ : -1;
1643 for (i = 0; i < sizeof indmap / sizeof indmap[0]; ++i)
1644 indmap[i] = -1;
1645 thischarcnt = 0;
1646 for (i = 0; i < typecnt; ++i) {
1647 register char * thisabbr;
1649 if (!writetype[i])
1650 continue;
1651 if (indmap[abbrinds[i]] >= 0)
1652 continue;
1653 thisabbr = &chars[abbrinds[i]];
1654 for (j = 0; j < thischarcnt; ++j)
1655 if (strcmp(&thischars[j], thisabbr) == 0)
1656 break;
1657 if (j == thischarcnt) {
1658 (void) strcpy(&thischars[(int) thischarcnt],
1659 thisabbr);
1660 thischarcnt += strlen(thisabbr) + 1;
1662 indmap[abbrinds[i]] = j;
1664 #define DO(field) (void) fwrite((void *) tzh.field, \
1665 (size_t) sizeof tzh.field, (size_t) 1, fp)
1666 tzh = tzh0;
1667 (void) strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1668 tzh.tzh_version[0] = ZIC_VERSION;
1669 convert(eitol(thistypecnt), tzh.tzh_ttisgmtcnt);
1670 convert(eitol(thistypecnt), tzh.tzh_ttisstdcnt);
1671 convert(eitol(thisleapcnt), tzh.tzh_leapcnt);
1672 convert(eitol(thistimecnt), tzh.tzh_timecnt);
1673 convert(eitol(thistypecnt), tzh.tzh_typecnt);
1674 convert(eitol(thischarcnt), tzh.tzh_charcnt);
1675 DO(tzh_magic);
1676 DO(tzh_version);
1677 DO(tzh_reserved);
1678 DO(tzh_ttisgmtcnt);
1679 DO(tzh_ttisstdcnt);
1680 DO(tzh_leapcnt);
1681 DO(tzh_timecnt);
1682 DO(tzh_typecnt);
1683 DO(tzh_charcnt);
1684 #undef DO
1685 for (i = thistimei; i < thistimelim; ++i)
1686 if (pass == 1)
1687 puttzcode((long) ats[i], fp);
1688 else puttzcode64(ats[i], fp);
1689 for (i = thistimei; i < thistimelim; ++i) {
1690 unsigned char uc;
1692 uc = typemap[types[i]];
1693 (void) fwrite((void *) &uc,
1694 (size_t) sizeof uc,
1695 (size_t) 1,
1696 fp);
1698 for (i = 0; i < typecnt; ++i)
1699 if (writetype[i]) {
1700 puttzcode(gmtoffs[i], fp);
1701 (void) putc(isdsts[i], fp);
1702 (void) putc((unsigned char) indmap[abbrinds[i]], fp);
1704 if (thischarcnt != 0)
1705 (void) fwrite((void *) thischars,
1706 (size_t) sizeof thischars[0],
1707 (size_t) thischarcnt, fp);
1708 for (i = thisleapi; i < thisleaplim; ++i) {
1709 register zic_t todo;
1711 if (roll[i]) {
1712 if (timecnt == 0 || trans[i] < ats[0]) {
1713 j = 0;
1714 while (isdsts[j])
1715 if (++j >= typecnt) {
1716 j = 0;
1717 break;
1719 } else {
1720 j = 1;
1721 while (j < timecnt &&
1722 trans[i] >= ats[j])
1723 ++j;
1724 j = types[j - 1];
1726 todo = tadd(trans[i], -gmtoffs[j]);
1727 } else todo = trans[i];
1728 if (pass == 1)
1729 puttzcode((long) todo, fp);
1730 else puttzcode64(todo, fp);
1731 puttzcode(corr[i], fp);
1733 for (i = 0; i < typecnt; ++i)
1734 if (writetype[i])
1735 (void) putc(ttisstds[i], fp);
1736 for (i = 0; i < typecnt; ++i)
1737 if (writetype[i])
1738 (void) putc(ttisgmts[i], fp);
1740 (void) fprintf(fp, "\n%s\n", string);
1741 if (ferror(fp) || fclose(fp)) {
1742 (void) fprintf(stderr, _("%s: Error writing %s\n"),
1743 progname, fullname);
1744 exit(EXIT_FAILURE);
1748 static void
1749 doabbr(abbr, abbrlen, format, letters, isdst, doquotes)
1750 char * const abbr;
1751 const int abbrlen;
1752 const char * const format;
1753 const char * const letters;
1754 const int isdst;
1755 const int doquotes;
1757 register char * cp;
1758 register char * slashp;
1759 register int len;
1761 slashp = strchr(format, '/');
1762 if (slashp == NULL) {
1763 if (letters == NULL)
1764 (void) strlcpy(abbr, format, abbrlen);
1765 else (void) snprintf(abbr, abbrlen, format, letters);
1766 } else if (isdst) {
1767 (void) strlcpy(abbr, slashp + 1, abbrlen);
1768 } else {
1769 if (slashp > format)
1770 (void) strncpy(abbr, format,
1771 (unsigned) (slashp - format));
1772 abbr[slashp - format] = '\0';
1774 if (!doquotes)
1775 return;
1776 for (cp = abbr; *cp != '\0'; ++cp)
1777 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp) == NULL &&
1778 strchr("abcdefghijklmnopqrstuvwxyz", *cp) == NULL)
1779 break;
1780 len = strlen(abbr);
1781 if (len > 0 && *cp == '\0')
1782 return;
1783 abbr[len + 2] = '\0';
1784 abbr[len + 1] = '>';
1785 for ( ; len > 0; --len)
1786 abbr[len] = abbr[len - 1];
1787 abbr[0] = '<';
1790 static void
1791 updateminmax(x)
1792 const int x;
1794 if (min_year > x)
1795 min_year = x;
1796 if (max_year < x)
1797 max_year = x;
1800 static int
1801 stringoffset(result, offset)
1802 char * result;
1803 long offset;
1805 register int hours;
1806 register int minutes;
1807 register int seconds;
1809 result[0] = '\0';
1810 if (offset < 0) {
1811 (void) strcpy(result, "-");
1812 offset = -offset;
1814 seconds = offset % SECSPERMIN;
1815 offset /= SECSPERMIN;
1816 minutes = offset % MINSPERHOUR;
1817 offset /= MINSPERHOUR;
1818 hours = offset;
1819 if (hours >= HOURSPERDAY) {
1820 result[0] = '\0';
1821 return -1;
1823 (void) sprintf(end(result), "%d", hours);
1824 if (minutes != 0 || seconds != 0) {
1825 (void) sprintf(end(result), ":%02d", minutes);
1826 if (seconds != 0)
1827 (void) sprintf(end(result), ":%02d", seconds);
1829 return 0;
1832 static int
1833 stringrule(result, rp, dstoff, gmtoff)
1834 char * result;
1835 const struct rule * const rp;
1836 const long dstoff;
1837 const long gmtoff;
1839 register long tod;
1841 result = end(result);
1842 if (rp->r_dycode == DC_DOM) {
1843 register int month, total;
1845 if (rp->r_dayofmonth == 29 && rp->r_month == TM_FEBRUARY)
1846 return -1;
1847 total = 0;
1848 for (month = 0; month < rp->r_month; ++month)
1849 total += len_months[0][month];
1850 (void) sprintf(result, "J%d", total + rp->r_dayofmonth);
1851 } else {
1852 register int week;
1854 if (rp->r_dycode == DC_DOWGEQ) {
1855 week = 1 + rp->r_dayofmonth / DAYSPERWEEK;
1856 if ((week - 1) * DAYSPERWEEK + 1 != rp->r_dayofmonth)
1857 return -1;
1858 } else if (rp->r_dycode == DC_DOWLEQ) {
1859 if (rp->r_dayofmonth == len_months[1][rp->r_month])
1860 week = 5;
1861 else {
1862 week = 1 + rp->r_dayofmonth / DAYSPERWEEK;
1863 if (week * DAYSPERWEEK - 1 != rp->r_dayofmonth)
1864 return -1;
1866 } else return -1; /* "cannot happen" */
1867 (void) sprintf(result, "M%d.%d.%d",
1868 rp->r_month + 1, week, rp->r_wday);
1870 tod = rp->r_tod;
1871 if (rp->r_todisgmt)
1872 tod += gmtoff;
1873 if (rp->r_todisstd && rp->r_stdoff == 0)
1874 tod += dstoff;
1875 if (tod < 0) {
1876 result[0] = '\0';
1877 return -1;
1879 if (tod != 2 * SECSPERMIN * MINSPERHOUR) {
1880 (void) strcat(result, "/");
1881 if (stringoffset(end(result), tod) != 0)
1882 return -1;
1884 return 0;
1887 static void
1888 stringzone(result, resultlen, zpfirst, zonecount)
1889 char * result;
1890 const int resultlen;
1891 const struct zone * const zpfirst;
1892 const int zonecount;
1894 register const struct zone * zp;
1895 register struct rule * rp;
1896 register struct rule * stdrp;
1897 register struct rule * dstrp;
1898 register int i;
1899 register const char * abbrvar;
1901 result[0] = '\0';
1902 zp = zpfirst + zonecount - 1;
1903 stdrp = dstrp = NULL;
1904 for (i = 0; i < zp->z_nrules; ++i) {
1905 rp = &zp->z_rules[i];
1906 if (rp->r_hiwasnum || rp->r_hiyear != INT_MAX)
1907 continue;
1908 if (rp->r_yrtype != NULL)
1909 continue;
1910 if (rp->r_stdoff == 0) {
1911 if (stdrp == NULL)
1912 stdrp = rp;
1913 else return;
1914 } else {
1915 if (dstrp == NULL)
1916 dstrp = rp;
1917 else return;
1920 if (stdrp == NULL && dstrp == NULL) {
1922 ** There are no rules running through "max".
1923 ** Let's find the latest rule.
1925 for (i = 0; i < zp->z_nrules; ++i) {
1926 rp = &zp->z_rules[i];
1927 if (stdrp == NULL || rp->r_hiyear > stdrp->r_hiyear ||
1928 (rp->r_hiyear == stdrp->r_hiyear &&
1929 rp->r_month > stdrp->r_month))
1930 stdrp = rp;
1932 if (stdrp != NULL && stdrp->r_stdoff != 0)
1933 return; /* We end up in DST (a POSIX no-no). */
1935 ** Horrid special case: if year is 2037,
1936 ** presume this is a zone handled on a year-by-year basis;
1937 ** do not try to apply a rule to the zone.
1939 if (stdrp != NULL && stdrp->r_hiyear == 2037)
1940 return;
1942 if (stdrp == NULL && (zp->z_nrules != 0 || zp->z_stdoff != 0))
1943 return;
1944 abbrvar = (stdrp == NULL) ? "" : stdrp->r_abbrvar;
1945 doabbr(result, resultlen, zp->z_format, abbrvar, FALSE, TRUE);
1946 if (stringoffset(end(result), -zp->z_gmtoff) != 0) {
1947 result[0] = '\0';
1948 return;
1950 if (dstrp == NULL)
1951 return;
1952 doabbr(end(result), resultlen - strlen(result),
1953 zp->z_format, dstrp->r_abbrvar, TRUE, TRUE);
1954 if (dstrp->r_stdoff != SECSPERMIN * MINSPERHOUR)
1955 if (stringoffset(end(result),
1956 -(zp->z_gmtoff + dstrp->r_stdoff)) != 0) {
1957 result[0] = '\0';
1958 return;
1960 (void) strcat(result, ",");
1961 if (stringrule(result, dstrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) {
1962 result[0] = '\0';
1963 return;
1965 (void) strcat(result, ",");
1966 if (stringrule(result, stdrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) {
1967 result[0] = '\0';
1968 return;
1972 static void
1973 outzone(zpfirst, zonecount)
1974 const struct zone * const zpfirst;
1975 const int zonecount;
1977 register const struct zone * zp;
1978 register struct rule * rp;
1979 register int i, j;
1980 register int usestart, useuntil;
1981 register zic_t starttime, untiltime;
1982 register long gmtoff;
1983 register long stdoff;
1984 register int year;
1985 register long startoff;
1986 register int startttisstd;
1987 register int startttisgmt;
1988 register int type;
1989 register char * startbuf;
1990 register char * ab;
1991 register char * envvar;
1992 register int max_abbr_len;
1993 register int max_envvar_len;
1995 max_abbr_len = 2 + max_format_len + max_abbrvar_len;
1996 max_envvar_len = 2 * max_abbr_len + 5 * 9;
1997 startbuf = emalloc(max_abbr_len + 1);
1998 ab = emalloc(max_abbr_len + 1);
1999 envvar = emalloc(max_envvar_len + 1);
2000 INITIALIZE(untiltime);
2001 INITIALIZE(starttime);
2003 ** Now. . .finally. . .generate some useful data!
2005 timecnt = 0;
2006 typecnt = 0;
2007 charcnt = 0;
2009 ** Thanks to Earl Chew
2010 ** for noting the need to unconditionally initialize startttisstd.
2012 startttisstd = FALSE;
2013 startttisgmt = FALSE;
2014 min_year = max_year = EPOCH_YEAR;
2015 if (leapseen) {
2016 updateminmax(leapminyear);
2017 updateminmax(leapmaxyear + (leapmaxyear < INT_MAX));
2019 for (i = 0; i < zonecount; ++i) {
2020 zp = &zpfirst[i];
2021 if (i < zonecount - 1)
2022 updateminmax(zp->z_untilrule.r_loyear);
2023 for (j = 0; j < zp->z_nrules; ++j) {
2024 rp = &zp->z_rules[j];
2025 if (rp->r_lowasnum)
2026 updateminmax(rp->r_loyear);
2027 if (rp->r_hiwasnum)
2028 updateminmax(rp->r_hiyear);
2032 ** Generate lots of data if a rule can't cover all future times.
2034 stringzone(envvar, max_envvar_len+1, zpfirst, zonecount);
2035 if (noise && envvar[0] == '\0') {
2036 register char * wp;
2038 wp = ecpyalloc(_("no POSIX environment variable for zone"));
2039 wp = ecatalloc(wp, " ");
2040 wp = ecatalloc(wp, zpfirst->z_name);
2041 warning(wp);
2042 ifree(wp);
2044 if (envvar[0] == '\0') {
2045 if (min_year >= INT_MIN + YEARSPERREPEAT)
2046 min_year -= YEARSPERREPEAT;
2047 else min_year = INT_MIN;
2048 if (max_year <= INT_MAX - YEARSPERREPEAT)
2049 max_year += YEARSPERREPEAT;
2050 else max_year = INT_MAX;
2053 ** For the benefit of older systems,
2054 ** generate data from 1900 through 2037.
2056 if (min_year > 1900)
2057 min_year = 1900;
2058 if (max_year < 2037)
2059 max_year = 2037;
2060 for (i = 0; i < zonecount; ++i) {
2062 ** A guess that may well be corrected later.
2064 stdoff = 0;
2065 zp = &zpfirst[i];
2066 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
2067 useuntil = i < (zonecount - 1);
2068 if (useuntil && zp->z_untiltime <= min_time)
2069 continue;
2070 gmtoff = zp->z_gmtoff;
2071 eat(zp->z_filename, zp->z_linenum);
2072 *startbuf = '\0';
2073 startoff = zp->z_gmtoff;
2074 if (zp->z_nrules == 0) {
2075 stdoff = zp->z_stdoff;
2076 doabbr(startbuf, max_abbr_len + 1, zp->z_format,
2077 (char *) NULL, stdoff != 0, FALSE);
2078 type = addtype(oadd(zp->z_gmtoff, stdoff),
2079 startbuf, stdoff != 0, startttisstd,
2080 startttisgmt);
2081 if (usestart) {
2082 addtt(starttime, type);
2083 usestart = FALSE;
2084 } else if (stdoff != 0)
2085 addtt(min_time, type);
2086 } else for (year = min_year; year <= max_year; ++year) {
2087 if (useuntil && year > zp->z_untilrule.r_hiyear)
2088 break;
2090 ** Mark which rules to do in the current year.
2091 ** For those to do, calculate rpytime(rp, year);
2093 for (j = 0; j < zp->z_nrules; ++j) {
2094 rp = &zp->z_rules[j];
2095 eats(zp->z_filename, zp->z_linenum,
2096 rp->r_filename, rp->r_linenum);
2097 rp->r_todo = year >= rp->r_loyear &&
2098 year <= rp->r_hiyear &&
2099 yearistype(year, rp->r_yrtype);
2100 if (rp->r_todo)
2101 rp->r_temp = rpytime(rp, year);
2103 for ( ; ; ) {
2104 register int k;
2105 register zic_t jtime, ktime;
2106 register long offset;
2108 INITIALIZE(ktime);
2109 if (useuntil) {
2111 ** Turn untiltime into UTC
2112 ** assuming the current gmtoff and
2113 ** stdoff values.
2115 untiltime = zp->z_untiltime;
2116 if (!zp->z_untilrule.r_todisgmt)
2117 untiltime = tadd(untiltime,
2118 -gmtoff);
2119 if (!zp->z_untilrule.r_todisstd)
2120 untiltime = tadd(untiltime,
2121 -stdoff);
2124 ** Find the rule (of those to do, if any)
2125 ** that takes effect earliest in the year.
2127 k = -1;
2128 for (j = 0; j < zp->z_nrules; ++j) {
2129 rp = &zp->z_rules[j];
2130 if (!rp->r_todo)
2131 continue;
2132 eats(zp->z_filename, zp->z_linenum,
2133 rp->r_filename, rp->r_linenum);
2134 offset = rp->r_todisgmt ? 0 : gmtoff;
2135 if (!rp->r_todisstd)
2136 offset = oadd(offset, stdoff);
2137 jtime = rp->r_temp;
2138 if (jtime == min_time ||
2139 jtime == max_time)
2140 continue;
2141 jtime = tadd(jtime, -offset);
2142 if (k < 0 || jtime < ktime) {
2143 k = j;
2144 ktime = jtime;
2147 if (k < 0)
2148 break; /* go on to next year */
2149 rp = &zp->z_rules[k];
2150 rp->r_todo = FALSE;
2151 if (useuntil && ktime >= untiltime)
2152 break;
2153 stdoff = rp->r_stdoff;
2154 if (usestart && ktime == starttime)
2155 usestart = FALSE;
2156 if (usestart) {
2157 if (ktime < starttime) {
2158 startoff = oadd(zp->z_gmtoff,
2159 stdoff);
2160 doabbr(startbuf,
2161 max_abbr_len + 1,
2162 zp->z_format,
2163 rp->r_abbrvar,
2164 rp->r_stdoff != 0,
2165 FALSE);
2166 continue;
2168 if (*startbuf == '\0' &&
2169 startoff == oadd(zp->z_gmtoff,
2170 stdoff)) {
2171 doabbr(startbuf,
2172 max_abbr_len + 1,
2173 zp->z_format,
2174 rp->r_abbrvar,
2175 rp->r_stdoff !=
2177 FALSE);
2180 eats(zp->z_filename, zp->z_linenum,
2181 rp->r_filename, rp->r_linenum);
2182 doabbr(ab, max_abbr_len+1, zp->z_format, rp->r_abbrvar,
2183 rp->r_stdoff != 0, FALSE);
2184 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
2185 type = addtype(offset, ab, rp->r_stdoff != 0,
2186 rp->r_todisstd, rp->r_todisgmt);
2187 addtt(ktime, type);
2190 if (usestart) {
2191 if (*startbuf == '\0' &&
2192 zp->z_format != NULL &&
2193 strchr(zp->z_format, '%') == NULL &&
2194 strchr(zp->z_format, '/') == NULL)
2195 (void)strncpy(startbuf, zp->z_format,
2196 max_abbr_len + 1 - 1);
2197 eat(zp->z_filename, zp->z_linenum);
2198 if (*startbuf == '\0')
2199 error(_("can't determine time zone abbreviation to use just after until time"));
2200 else addtt(starttime,
2201 addtype(startoff, startbuf,
2202 startoff != zp->z_gmtoff,
2203 startttisstd,
2204 startttisgmt));
2207 ** Now we may get to set starttime for the next zone line.
2209 if (useuntil) {
2210 startttisstd = zp->z_untilrule.r_todisstd;
2211 startttisgmt = zp->z_untilrule.r_todisgmt;
2212 starttime = zp->z_untiltime;
2213 if (!startttisstd)
2214 starttime = tadd(starttime, -stdoff);
2215 if (!startttisgmt)
2216 starttime = tadd(starttime, -gmtoff);
2219 writezone(zpfirst->z_name, envvar);
2220 ifree(startbuf);
2221 ifree(ab);
2222 ifree(envvar);
2225 static void
2226 addtt(starttime, type)
2227 const zic_t starttime;
2228 int type;
2230 if (starttime <= min_time ||
2231 (timecnt == 1 && attypes[0].at < min_time)) {
2232 gmtoffs[0] = gmtoffs[type];
2233 isdsts[0] = isdsts[type];
2234 ttisstds[0] = ttisstds[type];
2235 ttisgmts[0] = ttisgmts[type];
2236 if (abbrinds[type] != 0)
2237 (void) strcpy(chars, &chars[abbrinds[type]]);
2238 abbrinds[0] = 0;
2239 charcnt = strlen(chars) + 1;
2240 typecnt = 1;
2241 timecnt = 0;
2242 type = 0;
2244 if (timecnt >= TZ_MAX_TIMES) {
2245 error(_("too many transitions?!"));
2246 exit(EXIT_FAILURE);
2248 attypes[timecnt].at = starttime;
2249 attypes[timecnt].type = type;
2250 ++timecnt;
2253 static int
2254 addtype(gmtoff, abbr, isdst, ttisstd, ttisgmt)
2255 const long gmtoff;
2256 const char * const abbr;
2257 const int isdst;
2258 const int ttisstd;
2259 const int ttisgmt;
2261 register int i, j;
2263 if (isdst != TRUE && isdst != FALSE) {
2264 error(_("internal error - addtype called with bad isdst"));
2265 exit(EXIT_FAILURE);
2267 if (ttisstd != TRUE && ttisstd != FALSE) {
2268 error(_("internal error - addtype called with bad ttisstd"));
2269 exit(EXIT_FAILURE);
2271 if (ttisgmt != TRUE && ttisgmt != FALSE) {
2272 error(_("internal error - addtype called with bad ttisgmt"));
2273 exit(EXIT_FAILURE);
2276 ** See if there's already an entry for this zone type.
2277 ** If so, just return its index.
2279 for (i = 0; i < typecnt; ++i) {
2280 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
2281 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
2282 ttisstd == ttisstds[i] &&
2283 ttisgmt == ttisgmts[i])
2284 return i;
2287 ** There isn't one; add a new one, unless there are already too
2288 ** many.
2290 if (typecnt >= TZ_MAX_TYPES) {
2291 error(_("too many local time types"));
2292 exit(EXIT_FAILURE);
2294 if (! (-1L - 2147483647L <= gmtoff && gmtoff <= 2147483647L)) {
2295 error(_("UTC offset out of range"));
2296 exit(EXIT_FAILURE);
2298 gmtoffs[i] = gmtoff;
2299 isdsts[i] = isdst;
2300 ttisstds[i] = ttisstd;
2301 ttisgmts[i] = ttisgmt;
2303 for (j = 0; j < charcnt; ++j)
2304 if (strcmp(&chars[j], abbr) == 0)
2305 break;
2306 if (j == charcnt)
2307 newabbr(abbr);
2308 abbrinds[i] = j;
2309 ++typecnt;
2310 return i;
2313 static void
2314 leapadd(t, positive, rolling, count)
2315 const zic_t t;
2316 const int positive;
2317 const int rolling;
2318 int count;
2320 register int i, j;
2322 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
2323 error(_("too many leap seconds"));
2324 exit(EXIT_FAILURE);
2326 for (i = 0; i < leapcnt; ++i)
2327 if (t <= trans[i]) {
2328 if (t == trans[i]) {
2329 error(_("repeated leap second moment"));
2330 exit(EXIT_FAILURE);
2332 break;
2334 do {
2335 for (j = leapcnt; j > i; --j) {
2336 trans[j] = trans[j - 1];
2337 corr[j] = corr[j - 1];
2338 roll[j] = roll[j - 1];
2340 trans[i] = t;
2341 corr[i] = positive ? 1L : eitol(-count);
2342 roll[i] = rolling;
2343 ++leapcnt;
2344 } while (positive && --count != 0);
2347 static void
2348 adjleap(void)
2350 register int i;
2351 register long last = 0;
2354 ** propagate leap seconds forward
2356 for (i = 0; i < leapcnt; ++i) {
2357 trans[i] = tadd(trans[i], last);
2358 last = corr[i] += last;
2362 static int
2363 yearistype(year, type)
2364 const int year;
2365 const char * const type;
2367 static char * buf;
2368 int result;
2370 if (type == NULL || *type == '\0')
2371 return TRUE;
2372 buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
2373 (void)sprintf(buf, "%s %d %s", yitcommand, year, type); /* XXX: sprintf is safe */
2374 result = system(buf);
2375 if (WIFEXITED(result)) switch (WEXITSTATUS(result)) {
2376 case 0:
2377 return TRUE;
2378 case 1:
2379 return FALSE;
2381 error(_("Wild result from command execution"));
2382 (void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
2383 progname, buf, result);
2384 for ( ; ; )
2385 exit(EXIT_FAILURE);
2388 static int
2389 lowerit(a)
2390 int a;
2392 a = (unsigned char) a;
2393 return (isascii(a) && isupper(a)) ? tolower(a) : a;
2396 static int
2397 ciequal(ap, bp) /* case-insensitive equality */
2398 register const char * ap;
2399 register const char * bp;
2401 while (lowerit(*ap) == lowerit(*bp++))
2402 if (*ap++ == '\0')
2403 return TRUE;
2404 return FALSE;
2407 static int
2408 itsabbr(abbr, word)
2409 register const char * abbr;
2410 register const char * word;
2412 if (lowerit(*abbr) != lowerit(*word))
2413 return FALSE;
2414 ++word;
2415 while (*++abbr != '\0')
2416 do {
2417 if (*word == '\0')
2418 return FALSE;
2419 } while (lowerit(*word++) != lowerit(*abbr));
2420 return TRUE;
2423 static const struct lookup *
2424 byword(word, table)
2425 register const char * const word;
2426 register const struct lookup * const table;
2428 register const struct lookup * foundlp;
2429 register const struct lookup * lp;
2431 if (word == NULL || table == NULL)
2432 return NULL;
2434 ** Look for exact match.
2436 for (lp = table; lp->l_word != NULL; ++lp)
2437 if (ciequal(word, lp->l_word))
2438 return lp;
2440 ** Look for inexact match.
2442 foundlp = NULL;
2443 for (lp = table; lp->l_word != NULL; ++lp)
2444 if (itsabbr(word, lp->l_word)) {
2445 if (foundlp == NULL)
2446 foundlp = lp;
2447 else return NULL; /* multiple inexact matches */
2449 return foundlp;
2452 static char **
2453 getfields(cp)
2454 register char * cp;
2456 register char * dp;
2457 register char ** array;
2458 register int nsubs;
2460 if (cp == NULL)
2461 return NULL;
2462 array = (char **) (void *)
2463 emalloc((int) ((strlen(cp) + 1) * sizeof *array));
2464 nsubs = 0;
2465 for ( ; ; ) {
2466 while (isascii((unsigned char) *cp) &&
2467 isspace((unsigned char) *cp))
2468 ++cp;
2469 if (*cp == '\0' || *cp == '#')
2470 break;
2471 array[nsubs++] = dp = cp;
2472 do {
2473 if ((*dp = *cp++) != '"')
2474 ++dp;
2475 else while ((*dp = *cp++) != '"')
2476 if (*dp != '\0')
2477 ++dp;
2478 else {
2479 error(_(
2480 "Odd number of quotation marks"
2482 exit(1);
2484 } while (*cp != '\0' && *cp != '#' &&
2485 (!isascii(*cp) || !isspace((unsigned char) *cp)));
2486 if (isascii(*cp) && isspace((unsigned char) *cp))
2487 ++cp;
2488 *dp = '\0';
2490 array[nsubs] = NULL;
2491 return array;
2494 static long
2495 oadd(t1, t2)
2496 const long t1;
2497 const long t2;
2499 register long t;
2501 t = t1 + t2;
2502 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2503 error(_("time overflow"));
2504 exit(EXIT_FAILURE);
2506 return t;
2509 static zic_t
2510 tadd(t1, t2)
2511 const zic_t t1;
2512 const long t2;
2514 register zic_t t;
2516 if (t1 == max_time && t2 > 0)
2517 return max_time;
2518 if (t1 == min_time && t2 < 0)
2519 return min_time;
2520 t = t1 + t2;
2521 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2522 error(_("time overflow"));
2523 exit(EXIT_FAILURE);
2525 return t;
2529 ** Given a rule, and a year, compute the date - in seconds since January 1,
2530 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2533 static zic_t
2534 rpytime(rp, wantedy)
2535 register const struct rule * const rp;
2536 register const int wantedy;
2538 register int y, m, i;
2539 register long dayoff; /* with a nod to Margaret O. */
2540 register zic_t t;
2542 if (wantedy == INT_MIN)
2543 return min_time;
2544 if (wantedy == INT_MAX)
2545 return max_time;
2546 dayoff = 0;
2547 m = TM_JANUARY;
2548 y = EPOCH_YEAR;
2549 while (wantedy != y) {
2550 if (wantedy > y) {
2551 i = len_years[isleap(y)];
2552 ++y;
2553 } else {
2554 --y;
2555 i = -len_years[isleap(y)];
2557 dayoff = oadd(dayoff, eitol(i));
2559 while (m != rp->r_month) {
2560 i = len_months[isleap(y)][m];
2561 dayoff = oadd(dayoff, eitol(i));
2562 ++m;
2564 i = rp->r_dayofmonth;
2565 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
2566 if (rp->r_dycode == DC_DOWLEQ)
2567 --i;
2568 else {
2569 error(_("use of 2/29 in non leap-year"));
2570 exit(EXIT_FAILURE);
2573 --i;
2574 dayoff = oadd(dayoff, eitol(i));
2575 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
2576 register long wday;
2578 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2579 wday = eitol(EPOCH_WDAY);
2581 ** Don't trust mod of negative numbers.
2583 if (dayoff >= 0)
2584 wday = (wday + dayoff) % LDAYSPERWEEK;
2585 else {
2586 wday -= ((-dayoff) % LDAYSPERWEEK);
2587 if (wday < 0)
2588 wday += LDAYSPERWEEK;
2590 while (wday != eitol(rp->r_wday))
2591 if (rp->r_dycode == DC_DOWGEQ) {
2592 dayoff = oadd(dayoff, (long) 1);
2593 if (++wday >= LDAYSPERWEEK)
2594 wday = 0;
2595 ++i;
2596 } else {
2597 dayoff = oadd(dayoff, (long) -1);
2598 if (--wday < 0)
2599 wday = LDAYSPERWEEK - 1;
2600 --i;
2602 if (i < 0 || i >= len_months[isleap(y)][m]) {
2603 if (noise)
2604 warning(_("rule goes past start/end of month--\
2605 will not work with pre-2004 versions of zic"));
2608 if (dayoff < min_time / SECSPERDAY)
2609 return min_time;
2610 if (dayoff > max_time / SECSPERDAY)
2611 return max_time;
2612 t = (zic_t) dayoff * SECSPERDAY;
2613 return tadd(t, rp->r_tod);
2616 static void
2617 newabbr(string)
2618 const char * const string;
2620 register int i;
2622 if (strcmp(string, GRANDPARENTED) != 0) {
2623 register const char * cp;
2624 register char * wp;
2627 ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2628 ** optionally followed by a + or - and a number from 1 to 14.
2630 cp = string;
2631 wp = NULL;
2632 while (isascii((unsigned char) *cp) &&
2633 isalpha((unsigned char) *cp))
2634 ++cp;
2635 if (cp - string == 0)
2636 wp = _("time zone abbreviation lacks alphabetic at start");
2637 if (noise && cp - string > 3)
2638 wp = _("time zone abbreviation has more than 3 alphabetics");
2639 if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
2640 wp = _("time zone abbreviation has too many alphabetics");
2641 if (wp == NULL && (*cp == '+' || *cp == '-')) {
2642 ++cp;
2643 if (isascii((unsigned char) *cp) &&
2644 isdigit((unsigned char) *cp))
2645 if (*cp++ == '1' &&
2646 *cp >= '0' && *cp <= '4')
2647 ++cp;
2649 if (*cp != '\0')
2650 wp = _("time zone abbreviation differs from POSIX standard");
2651 if (wp != NULL) {
2652 wp = ecpyalloc(wp);
2653 wp = ecatalloc(wp, " (");
2654 wp = ecatalloc(wp, string);
2655 wp = ecatalloc(wp, ")");
2656 warning(wp);
2657 ifree(wp);
2660 i = strlen(string) + 1;
2661 if (charcnt + i > TZ_MAX_CHARS) {
2662 error(_("too many, or too long, time zone abbreviations"));
2663 exit(EXIT_FAILURE);
2665 (void)strncpy(&chars[charcnt], string, sizeof(chars) - charcnt - 1);
2666 charcnt += eitol(i);
2669 static int
2670 mkdirs(argname)
2671 char * argname;
2673 register char * name;
2674 register char * cp;
2676 if (argname == NULL || *argname == '\0')
2677 return 0;
2678 cp = name = ecpyalloc(argname);
2679 while ((cp = strchr(cp + 1, '/')) != 0) {
2680 *cp = '\0';
2681 #ifndef __NetBSD__
2683 ** DOS drive specifier?
2685 if (isalpha((unsigned char) name[0]) &&
2686 name[1] == ':' && name[2] == '\0') {
2687 *cp = '/';
2688 continue;
2690 #endif /* !defined __NetBSD__ */
2691 if (!itsdir(name)) {
2693 ** It doesn't seem to exist, so we try to create it.
2694 ** Creation may fail because of the directory being
2695 ** created by some other multiprocessor, so we get
2696 ** to do extra checking.
2698 if (mkdir(name, MKDIR_UMASK) != 0) {
2699 const char *e = strerror(errno);
2701 if (errno != EEXIST || !itsdir(name)) {
2702 (void) fprintf(stderr,
2703 _("%s: Can't create directory %s: %s\n"),
2704 progname, name, e);
2705 ifree(name);
2706 return -1;
2710 *cp = '/';
2712 ifree(name);
2713 return 0;
2716 static long
2717 eitol(i)
2718 const int i;
2720 long l;
2722 l = i;
2723 if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0)) {
2724 (void) fprintf(stderr,
2725 _("%s: %d did not sign extend correctly\n"),
2726 progname, i);
2727 exit(EXIT_FAILURE);
2729 return l;
2733 ** UNIX was a registered trademark of The Open Group in 2003.