Updated to fedora-glibc-20051220T1028
[glibc.git] / timezone / zic.c
blob4e3f42f2cc7337062081ee8ad74bc5375eaa9153
1 static char elsieid[] = "@(#)zic.c 7.124";
3 /*
4 ** Regardless of the type of time_t, we do our work using this type.
5 */
7 typedef int zic_t;
9 #include "private.h"
10 #include "locale.h"
11 #include "tzfile.h"
13 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
14 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
15 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
17 #if HAVE_SYS_STAT_H
18 #include "sys/stat.h"
19 #endif
20 #ifdef S_IRUSR
21 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
22 #else
23 #define MKDIR_UMASK 0755
24 #endif
27 ** On some ancient hosts, predicates like `isspace(C)' are defined
28 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
29 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
30 ** Neither the C Standard nor Posix require that `isascii' exist.
31 ** For portability, we check both ancient and modern requirements.
32 ** If isascii is not defined, the isascii check succeeds trivially.
34 #include "ctype.h"
35 #ifndef isascii
36 #define isascii(x) 1
37 #endif
39 struct rule {
40 const char * r_filename;
41 int r_linenum;
42 const char * r_name;
44 int r_loyear; /* for example, 1986 */
45 int r_hiyear; /* for example, 1986 */
46 const char * r_yrtype;
48 int r_month; /* 0..11 */
50 int r_dycode; /* see below */
51 int r_dayofmonth;
52 int r_wday;
54 long r_tod; /* time from midnight */
55 int r_todisstd; /* above is standard time if TRUE */
56 /* or wall clock time if FALSE */
57 int r_todisgmt; /* above is GMT if TRUE */
58 /* or local time if FALSE */
59 long r_stdoff; /* offset from standard time */
60 const char * r_abbrvar; /* variable part of abbreviation */
62 int r_todo; /* a rule to do (used in outzone) */
63 zic_t r_temp; /* used in outzone */
67 ** r_dycode r_dayofmonth r_wday
70 #define DC_DOM 0 /* 1..31 */ /* unused */
71 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
72 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
74 struct zone {
75 const char * z_filename;
76 int z_linenum;
78 const char * z_name;
79 long z_gmtoff;
80 const char * z_rule;
81 const char * z_format;
83 long z_stdoff;
85 struct rule * z_rules;
86 int z_nrules;
88 struct rule z_untilrule;
89 zic_t z_untiltime;
92 extern int getopt P((int argc, char * const argv[],
93 const char * options));
94 extern int link P((const char * fromname, const char * toname));
95 extern char * optarg;
96 extern int optind;
98 static void addtt P((zic_t starttime, int type));
99 static int addtype P((long gmtoff, const char * abbr, int isdst,
100 int ttisstd, int ttisgmt));
101 static void leapadd P((zic_t t, int positive, int rolling, int count));
102 static void adjleap P((void));
103 static void associate P((void));
104 static int ciequal P((const char * ap, const char * bp));
105 static void convert P((long val, char * buf));
106 static void dolink P((const char * fromfile, const char * tofile));
107 static void doabbr P((char * abbr, const char * format,
108 const char * letters, int isdst));
109 static void eat P((const char * name, int num));
110 static void eats P((const char * name, int num,
111 const char * rname, int rnum));
112 static long eitol P((int i));
113 static void error P((const char * message));
114 static char ** getfields P((char * buf));
115 static long gethms P((const char * string, const char * errstrng,
116 int signable));
117 static void infile P((const char * filename));
118 static void inleap P((char ** fields, int nfields));
119 static void inlink P((char ** fields, int nfields));
120 static void inrule P((char ** fields, int nfields));
121 static int inzcont P((char ** fields, int nfields));
122 static int inzone P((char ** fields, int nfields));
123 static int inzsub P((char ** fields, int nfields, int iscont));
124 static int itsabbr P((const char * abbr, const char * word));
125 static int itsdir P((const char * name));
126 static int lowerit P((int c));
127 static char * memcheck P((char * tocheck));
128 static int mkdirs P((char * filename));
129 static void newabbr P((const char * abbr));
130 static long oadd P((long t1, long t2));
131 static void outzone P((const struct zone * zp, int ntzones));
132 static void puttzcode P((long code, FILE * fp));
133 static int rcomp P((const void * leftp, const void * rightp));
134 static zic_t rpytime P((const struct rule * rp, int wantedy));
135 static void rulesub P((struct rule * rp,
136 const char * loyearp, const char * hiyearp,
137 const char * typep, const char * monthp,
138 const char * dayp, const char * timep));
139 static void setboundaries P((void));
140 static zic_t tadd P((zic_t t1, long t2));
141 static void usage P((void));
142 static void writezone P((const char * name));
143 static int yearistype P((int year, const char * type));
145 #if !HAVE_STRERROR
146 static char * strerror P((int));
147 #endif /* !HAVE_STRERROR */
149 static int charcnt;
150 static int errors;
151 static const char * filename;
152 static int leapcnt;
153 static int linenum;
154 static zic_t max_time;
155 static int max_year;
156 static int max_year_representable;
157 static zic_t min_time;
158 static int min_year;
159 static int min_year_representable;
160 static int noise;
161 static const char * rfilename;
162 static int rlinenum;
163 static const char * progname;
164 static int timecnt;
165 static int typecnt;
168 ** Line codes.
171 #define LC_RULE 0
172 #define LC_ZONE 1
173 #define LC_LINK 2
174 #define LC_LEAP 3
177 ** Which fields are which on a Zone line.
180 #define ZF_NAME 1
181 #define ZF_GMTOFF 2
182 #define ZF_RULE 3
183 #define ZF_FORMAT 4
184 #define ZF_TILYEAR 5
185 #define ZF_TILMONTH 6
186 #define ZF_TILDAY 7
187 #define ZF_TILTIME 8
188 #define ZONE_MINFIELDS 5
189 #define ZONE_MAXFIELDS 9
192 ** Which fields are which on a Zone continuation line.
195 #define ZFC_GMTOFF 0
196 #define ZFC_RULE 1
197 #define ZFC_FORMAT 2
198 #define ZFC_TILYEAR 3
199 #define ZFC_TILMONTH 4
200 #define ZFC_TILDAY 5
201 #define ZFC_TILTIME 6
202 #define ZONEC_MINFIELDS 3
203 #define ZONEC_MAXFIELDS 7
206 ** Which files are which on a Rule line.
209 #define RF_NAME 1
210 #define RF_LOYEAR 2
211 #define RF_HIYEAR 3
212 #define RF_COMMAND 4
213 #define RF_MONTH 5
214 #define RF_DAY 6
215 #define RF_TOD 7
216 #define RF_STDOFF 8
217 #define RF_ABBRVAR 9
218 #define RULE_FIELDS 10
221 ** Which fields are which on a Link line.
224 #define LF_FROM 1
225 #define LF_TO 2
226 #define LINK_FIELDS 3
229 ** Which fields are which on a Leap line.
232 #define LP_YEAR 1
233 #define LP_MONTH 2
234 #define LP_DAY 3
235 #define LP_TIME 4
236 #define LP_CORR 5
237 #define LP_ROLL 6
238 #define LEAP_FIELDS 7
241 ** Year synonyms.
244 #define YR_MINIMUM 0
245 #define YR_MAXIMUM 1
246 #define YR_ONLY 2
248 static struct rule * rules;
249 static int nrules; /* number of rules */
251 static struct zone * zones;
252 static int nzones; /* number of zones */
254 struct link {
255 const char * l_filename;
256 int l_linenum;
257 const char * l_from;
258 const char * l_to;
261 static struct link * links;
262 static int nlinks;
264 struct lookup {
265 const char * l_word;
266 const int l_value;
269 static struct lookup const * byword P((const char * string,
270 const struct lookup * lp));
272 static struct lookup const line_codes[] = {
273 { "Rule", LC_RULE },
274 { "Zone", LC_ZONE },
275 { "Link", LC_LINK },
276 { "Leap", LC_LEAP },
277 { NULL, 0}
280 static struct lookup const mon_names[] = {
281 { "January", TM_JANUARY },
282 { "February", TM_FEBRUARY },
283 { "March", TM_MARCH },
284 { "April", TM_APRIL },
285 { "May", TM_MAY },
286 { "June", TM_JUNE },
287 { "July", TM_JULY },
288 { "August", TM_AUGUST },
289 { "September", TM_SEPTEMBER },
290 { "October", TM_OCTOBER },
291 { "November", TM_NOVEMBER },
292 { "December", TM_DECEMBER },
293 { NULL, 0 }
296 static struct lookup const wday_names[] = {
297 { "Sunday", TM_SUNDAY },
298 { "Monday", TM_MONDAY },
299 { "Tuesday", TM_TUESDAY },
300 { "Wednesday", TM_WEDNESDAY },
301 { "Thursday", TM_THURSDAY },
302 { "Friday", TM_FRIDAY },
303 { "Saturday", TM_SATURDAY },
304 { NULL, 0 }
307 static struct lookup const lasts[] = {
308 { "last-Sunday", TM_SUNDAY },
309 { "last-Monday", TM_MONDAY },
310 { "last-Tuesday", TM_TUESDAY },
311 { "last-Wednesday", TM_WEDNESDAY },
312 { "last-Thursday", TM_THURSDAY },
313 { "last-Friday", TM_FRIDAY },
314 { "last-Saturday", TM_SATURDAY },
315 { NULL, 0 }
318 static struct lookup const begin_years[] = {
319 { "minimum", YR_MINIMUM },
320 { "maximum", YR_MAXIMUM },
321 { NULL, 0 }
324 static struct lookup const end_years[] = {
325 { "minimum", YR_MINIMUM },
326 { "maximum", YR_MAXIMUM },
327 { "only", YR_ONLY },
328 { NULL, 0 }
331 static struct lookup const leap_types[] = {
332 { "Rolling", TRUE },
333 { "Stationary", FALSE },
334 { NULL, 0 }
337 static const int len_months[2][MONSPERYEAR] = {
338 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
339 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
342 static const int len_years[2] = {
343 DAYSPERNYEAR, DAYSPERLYEAR
346 static struct attype {
347 zic_t at;
348 unsigned char type;
349 } attypes[TZ_MAX_TIMES];
350 static long gmtoffs[TZ_MAX_TYPES];
351 static char isdsts[TZ_MAX_TYPES];
352 static unsigned char abbrinds[TZ_MAX_TYPES];
353 static char ttisstds[TZ_MAX_TYPES];
354 static char ttisgmts[TZ_MAX_TYPES];
355 static char chars[TZ_MAX_CHARS];
356 static zic_t trans[TZ_MAX_LEAPS];
357 static long corr[TZ_MAX_LEAPS];
358 static char roll[TZ_MAX_LEAPS];
361 ** Memory allocation.
364 static char *
365 memcheck(ptr)
366 char * const ptr;
368 if (ptr == NULL) {
369 const char *e = strerror(errno);
371 (void) fprintf(stderr, _("%s: Memory exhausted: %s\n"),
372 progname, e);
373 (void) exit(EXIT_FAILURE);
375 return ptr;
378 #define emalloc(size) memcheck(imalloc(size))
379 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
380 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
381 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
384 ** Error handling.
387 #if !HAVE_STRERROR
388 static char *
389 strerror(errnum)
390 int errnum;
392 extern char * sys_errlist[];
393 extern int sys_nerr;
395 return (errnum > 0 && errnum <= sys_nerr) ?
396 sys_errlist[errnum] : _("Unknown system error");
398 #endif /* !HAVE_STRERROR */
400 static void
401 eats(name, num, rname, rnum)
402 const char * const name;
403 const int num;
404 const char * const rname;
405 const int rnum;
407 filename = name;
408 linenum = num;
409 rfilename = rname;
410 rlinenum = rnum;
413 static void
414 eat(name, num)
415 const char * const name;
416 const int num;
418 eats(name, num, (char *) NULL, -1);
421 static void
422 error(string)
423 const char * const string;
426 ** Match the format of "cc" to allow sh users to
427 ** zic ... 2>&1 | error -t "*" -v
428 ** on BSD systems.
430 (void) fprintf(stderr, _("\"%s\", line %d: %s"),
431 filename, linenum, string);
432 if (rfilename != NULL)
433 (void) fprintf(stderr, _(" (rule from \"%s\", line %d)"),
434 rfilename, rlinenum);
435 (void) fprintf(stderr, "\n");
436 ++errors;
439 static void
440 warning(string)
441 const char * const string;
443 char * cp;
445 cp = ecpyalloc(_("warning: "));
446 cp = ecatalloc(cp, string);
447 error(cp);
448 ifree(cp);
449 --errors;
452 static void
453 usage P((void))
455 (void) fprintf(stderr, _("%s: usage is %s \
456 [ --version ] [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\
457 \t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
458 progname, progname);
459 (void) exit(EXIT_FAILURE);
462 static const char * psxrules;
463 static const char * lcltime;
464 static const char * directory;
465 static const char * leapsec;
466 static const char * yitcommand;
467 static int sflag = FALSE;
470 main(argc, argv)
471 int argc;
472 char * argv[];
474 register int i;
475 register int j;
476 register int c;
478 #ifdef unix
479 (void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
480 #endif /* defined unix */
481 #if HAVE_GETTEXT
482 (void) setlocale(LC_ALL, "");
483 #ifdef TZ_DOMAINDIR
484 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
485 #endif /* defined TEXTDOMAINDIR */
486 (void) textdomain(TZ_DOMAIN);
487 #endif /* HAVE_GETTEXT */
488 progname = argv[0];
489 for (i = 1; i < argc; ++i)
490 if (strcmp(argv[i], "--version") == 0) {
491 (void) printf("%s\n", elsieid);
492 (void) exit(EXIT_SUCCESS);
494 while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF && c != -1)
495 switch (c) {
496 default:
497 usage();
498 case 'd':
499 if (directory == NULL)
500 directory = optarg;
501 else {
502 (void) fprintf(stderr,
503 _("%s: More than one -d option specified\n"),
504 progname);
505 (void) exit(EXIT_FAILURE);
507 break;
508 case 'l':
509 if (lcltime == NULL)
510 lcltime = optarg;
511 else {
512 (void) fprintf(stderr,
513 _("%s: More than one -l option specified\n"),
514 progname);
515 (void) exit(EXIT_FAILURE);
517 break;
518 case 'p':
519 if (psxrules == NULL)
520 psxrules = optarg;
521 else {
522 (void) fprintf(stderr,
523 _("%s: More than one -p option specified\n"),
524 progname);
525 (void) exit(EXIT_FAILURE);
527 break;
528 case 'y':
529 if (yitcommand == NULL)
530 yitcommand = optarg;
531 else {
532 (void) fprintf(stderr,
533 _("%s: More than one -y option specified\n"),
534 progname);
535 (void) exit(EXIT_FAILURE);
537 break;
538 case 'L':
539 if (leapsec == NULL)
540 leapsec = optarg;
541 else {
542 (void) fprintf(stderr,
543 _("%s: More than one -L option specified\n"),
544 progname);
545 (void) exit(EXIT_FAILURE);
547 break;
548 case 'v':
549 noise = TRUE;
550 break;
551 case 's':
552 sflag = TRUE;
553 break;
555 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
556 usage(); /* usage message by request */
557 if (directory == NULL)
558 directory = TZDIR;
559 if (yitcommand == NULL)
560 yitcommand = "yearistype";
562 setboundaries();
564 if (optind < argc && leapsec != NULL) {
565 infile(leapsec);
566 adjleap();
569 for (i = optind; i < argc; ++i)
570 infile(argv[i]);
571 if (errors)
572 (void) exit(EXIT_FAILURE);
573 associate();
574 for (i = 0; i < nzones; i = j) {
576 ** Find the next non-continuation zone entry.
578 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
579 continue;
580 outzone(&zones[i], j - i);
583 ** Make links.
585 for (i = 0; i < nlinks; ++i) {
586 eat(links[i].l_filename, links[i].l_linenum);
587 dolink(links[i].l_from, links[i].l_to);
588 if (noise)
589 for (j = 0; j < nlinks; ++j)
590 if (strcmp(links[i].l_to,
591 links[j].l_from) == 0)
592 warning(_("link to link"));
594 if (lcltime != NULL) {
595 eat("command line", 1);
596 dolink(lcltime, TZDEFAULT);
598 if (psxrules != NULL) {
599 eat("command line", 1);
600 dolink(psxrules, TZDEFRULES);
602 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
605 static void
606 dolink(fromfile, tofile)
607 const char * const fromfile;
608 const char * const tofile;
610 register char * fromname;
611 register char * toname;
613 if (fromfile[0] == '/')
614 fromname = ecpyalloc(fromfile);
615 else {
616 fromname = ecpyalloc(directory);
617 fromname = ecatalloc(fromname, "/");
618 fromname = ecatalloc(fromname, fromfile);
620 if (tofile[0] == '/')
621 toname = ecpyalloc(tofile);
622 else {
623 toname = ecpyalloc(directory);
624 toname = ecatalloc(toname, "/");
625 toname = ecatalloc(toname, tofile);
628 ** We get to be careful here since
629 ** there's a fair chance of root running us.
631 if (!itsdir(toname))
632 (void) remove(toname);
633 if (link(fromname, toname) != 0) {
634 int result;
636 if (mkdirs(toname) != 0)
637 (void) exit(EXIT_FAILURE);
639 result = link(fromname, toname);
640 #if HAVE_SYMLINK
641 if (result != 0 &&
642 access(fromname, F_OK) == 0 &&
643 !itsdir(fromname)) {
644 const char *s = tofile;
645 register char * symlinkcontents = NULL;
647 while ((s = strchr(s+1, '/')) != NULL)
648 symlinkcontents =
649 ecatalloc(symlinkcontents,
650 "../");
651 symlinkcontents =
652 ecatalloc(symlinkcontents,
653 fromname);
654 result = symlink(symlinkcontents,
655 toname);
656 if (result == 0)
657 warning(_("hard link failed, symbolic link used"));
658 ifree(symlinkcontents);
660 #endif /* HAVE_SYMLINK */
661 if (result != 0) {
662 const char *e = strerror(errno);
664 (void) fprintf(stderr,
665 _("%s: Can't link from %s to %s: %s\n"),
666 progname, fromname, toname, e);
667 (void) exit(EXIT_FAILURE);
670 ifree(fromname);
671 ifree(toname);
674 #ifndef INT_MAX
675 #define INT_MAX ((int) (((unsigned)~0)>>1))
676 #endif /* !defined INT_MAX */
678 #ifndef INT_MIN
679 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
680 #endif /* !defined INT_MIN */
683 ** The tz file format currently allows at most 32-bit quantities.
684 ** This restriction should be removed before signed 32-bit values
685 ** wrap around in 2038, but unfortunately this will require a
686 ** change to the tz file format.
689 #define MAX_BITS_IN_FILE 32
690 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(zic_t) < MAX_BITS_IN_FILE) ? \
691 TYPE_BIT(zic_t) : MAX_BITS_IN_FILE)
693 static void
694 setboundaries P((void))
696 register int i;
698 if (TYPE_SIGNED(zic_t)) {
699 min_time = -1;
700 for (i = 0; i < TIME_T_BITS_IN_FILE - 1; ++i)
701 min_time *= 2;
702 max_time = -(min_time + 1);
703 if (sflag)
704 min_time = 0;
705 } else {
706 min_time = 0;
707 max_time = 2 - sflag;
708 for (i = 0; i < TIME_T_BITS_IN_FILE - 1; ++i)
709 max_time *= 2;
710 --max_time;
713 time_t t;
715 t = (time_t) min_time;
716 min_year = TM_YEAR_BASE + gmtime(&t)->tm_year;
717 t = (time_t) max_time;
718 max_year = TM_YEAR_BASE + gmtime(&t)->tm_year;
720 min_year_representable = min_year;
721 max_year_representable = max_year;
724 static int
725 itsdir(name)
726 const char * const name;
728 register char * myname;
729 register int accres;
731 myname = ecpyalloc(name);
732 myname = ecatalloc(myname, "/.");
733 accres = access(myname, F_OK);
734 ifree(myname);
735 return accres == 0;
739 ** Associate sets of rules with zones.
743 ** Sort by rule name.
746 static int
747 rcomp(cp1, cp2)
748 const void * cp1;
749 const void * cp2;
751 return strcmp(((const struct rule *) cp1)->r_name,
752 ((const struct rule *) cp2)->r_name);
755 static void
756 associate P((void))
758 register struct zone * zp;
759 register struct rule * rp;
760 register int base, out;
761 register int i, j;
763 if (nrules != 0) {
764 (void) qsort((void *) rules, (size_t) nrules,
765 (size_t) sizeof *rules, rcomp);
766 for (i = 0; i < nrules - 1; ++i) {
767 if (strcmp(rules[i].r_name,
768 rules[i + 1].r_name) != 0)
769 continue;
770 if (strcmp(rules[i].r_filename,
771 rules[i + 1].r_filename) == 0)
772 continue;
773 eat(rules[i].r_filename, rules[i].r_linenum);
774 warning(_("same rule name in multiple files"));
775 eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
776 warning(_("same rule name in multiple files"));
777 for (j = i + 2; j < nrules; ++j) {
778 if (strcmp(rules[i].r_name,
779 rules[j].r_name) != 0)
780 break;
781 if (strcmp(rules[i].r_filename,
782 rules[j].r_filename) == 0)
783 continue;
784 if (strcmp(rules[i + 1].r_filename,
785 rules[j].r_filename) == 0)
786 continue;
787 break;
789 i = j - 1;
792 for (i = 0; i < nzones; ++i) {
793 zp = &zones[i];
794 zp->z_rules = NULL;
795 zp->z_nrules = 0;
797 for (base = 0; base < nrules; base = out) {
798 rp = &rules[base];
799 for (out = base + 1; out < nrules; ++out)
800 if (strcmp(rp->r_name, rules[out].r_name) != 0)
801 break;
802 for (i = 0; i < nzones; ++i) {
803 zp = &zones[i];
804 if (strcmp(zp->z_rule, rp->r_name) != 0)
805 continue;
806 zp->z_rules = rp;
807 zp->z_nrules = out - base;
810 for (i = 0; i < nzones; ++i) {
811 zp = &zones[i];
812 if (zp->z_nrules == 0) {
814 ** Maybe we have a local standard time offset.
816 eat(zp->z_filename, zp->z_linenum);
817 zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
818 TRUE);
820 ** Note, though, that if there's no rule,
821 ** a '%s' in the format is a bad thing.
823 if (strchr(zp->z_format, '%') != 0)
824 error(_("%s in ruleless zone"));
827 if (errors)
828 (void) exit(EXIT_FAILURE);
831 static void
832 infile(name)
833 const char * name;
835 register FILE * fp;
836 register char ** fields;
837 register char * cp;
838 register const struct lookup * lp;
839 register int nfields;
840 register int wantcont;
841 register int num;
842 char buf[BUFSIZ];
844 if (strcmp(name, "-") == 0) {
845 name = _("standard input");
846 fp = stdin;
847 } else if ((fp = fopen(name, "r")) == NULL) {
848 const char *e = strerror(errno);
850 (void) fprintf(stderr, _("%s: Can't open %s: %s\n"),
851 progname, name, e);
852 (void) exit(EXIT_FAILURE);
854 wantcont = FALSE;
855 for (num = 1; ; ++num) {
856 eat(name, num);
857 if (fgets(buf, (int) sizeof buf, fp) != buf)
858 break;
859 cp = strchr(buf, '\n');
860 if (cp == NULL) {
861 error(_("line too long"));
862 (void) exit(EXIT_FAILURE);
864 *cp = '\0';
865 fields = getfields(buf);
866 nfields = 0;
867 while (fields[nfields] != NULL) {
868 static char nada;
870 if (strcmp(fields[nfields], "-") == 0)
871 fields[nfields] = &nada;
872 ++nfields;
874 if (nfields == 0) {
875 /* nothing to do */
876 } else if (wantcont) {
877 wantcont = inzcont(fields, nfields);
878 } else {
879 lp = byword(fields[0], line_codes);
880 if (lp == NULL)
881 error(_("input line of unknown type"));
882 else switch ((int) (lp->l_value)) {
883 case LC_RULE:
884 inrule(fields, nfields);
885 wantcont = FALSE;
886 break;
887 case LC_ZONE:
888 wantcont = inzone(fields, nfields);
889 break;
890 case LC_LINK:
891 inlink(fields, nfields);
892 wantcont = FALSE;
893 break;
894 case LC_LEAP:
895 if (name != leapsec)
896 (void) fprintf(stderr,
897 _("%s: Leap line in non leap seconds file %s\n"),
898 progname, name);
899 else inleap(fields, nfields);
900 wantcont = FALSE;
901 break;
902 default: /* "cannot happen" */
903 (void) fprintf(stderr,
904 _("%s: panic: Invalid l_value %d\n"),
905 progname, lp->l_value);
906 (void) exit(EXIT_FAILURE);
909 ifree((char *) fields);
911 if (ferror(fp)) {
912 (void) fprintf(stderr, _("%s: Error reading %s\n"),
913 progname, filename);
914 (void) exit(EXIT_FAILURE);
916 if (fp != stdin && fclose(fp)) {
917 const char *e = strerror(errno);
919 (void) fprintf(stderr, _("%s: Error closing %s: %s\n"),
920 progname, filename, e);
921 (void) exit(EXIT_FAILURE);
923 if (wantcont)
924 error(_("expected continuation line not found"));
928 ** Convert a string of one of the forms
929 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
930 ** into a number of seconds.
931 ** A null string maps to zero.
932 ** Call error with errstring and return zero on errors.
935 static long
936 gethms(string, errstring, signable)
937 const char * string;
938 const char * const errstring;
939 const int signable;
941 int hh, mm, ss, sign;
943 if (string == NULL || *string == '\0')
944 return 0;
945 if (!signable)
946 sign = 1;
947 else if (*string == '-') {
948 sign = -1;
949 ++string;
950 } else sign = 1;
951 if (sscanf(string, scheck(string, "%d"), &hh) == 1)
952 mm = ss = 0;
953 else if (sscanf(string, scheck(string, "%d:%d"), &hh, &mm) == 2)
954 ss = 0;
955 else if (sscanf(string, scheck(string, "%d:%d:%d"),
956 &hh, &mm, &ss) != 3) {
957 error(errstring);
958 return 0;
960 if ((hh < 0 || hh >= HOURSPERDAY ||
961 mm < 0 || mm >= MINSPERHOUR ||
962 ss < 0 || ss > SECSPERMIN) &&
963 !(hh == HOURSPERDAY && mm == 0 && ss == 0)) {
964 error(errstring);
965 return 0;
967 if (noise && hh == HOURSPERDAY)
968 warning(_("24:00 not handled by pre-1998 versions of zic"));
969 return eitol(sign) *
970 (eitol(hh * MINSPERHOUR + mm) *
971 eitol(SECSPERMIN) + eitol(ss));
974 static void
975 inrule(fields, nfields)
976 register char ** const fields;
977 const int nfields;
979 static struct rule r;
981 if (nfields != RULE_FIELDS) {
982 error(_("wrong number of fields on Rule line"));
983 return;
985 if (*fields[RF_NAME] == '\0') {
986 error(_("nameless rule"));
987 return;
989 r.r_filename = filename;
990 r.r_linenum = linenum;
991 r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
992 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
993 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
994 r.r_name = ecpyalloc(fields[RF_NAME]);
995 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
996 rules = (struct rule *) (void *) erealloc((char *) rules,
997 (int) ((nrules + 1) * sizeof *rules));
998 rules[nrules++] = r;
1001 static int
1002 inzone(fields, nfields)
1003 register char ** const fields;
1004 const int nfields;
1006 register int i;
1007 static char * buf;
1009 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
1010 error(_("wrong number of fields on Zone line"));
1011 return FALSE;
1013 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
1014 buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
1015 (void) sprintf(buf,
1016 _("\"Zone %s\" line and -l option are mutually exclusive"),
1017 TZDEFAULT);
1018 error(buf);
1019 return FALSE;
1021 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
1022 buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
1023 (void) sprintf(buf,
1024 _("\"Zone %s\" line and -p option are mutually exclusive"),
1025 TZDEFRULES);
1026 error(buf);
1027 return FALSE;
1029 for (i = 0; i < nzones; ++i)
1030 if (zones[i].z_name != NULL &&
1031 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
1032 buf = erealloc(buf, (int) (132 +
1033 strlen(fields[ZF_NAME]) +
1034 strlen(zones[i].z_filename)));
1035 (void) sprintf(buf,
1036 _("duplicate zone name %s (file \"%s\", line %d)"),
1037 fields[ZF_NAME],
1038 zones[i].z_filename,
1039 zones[i].z_linenum);
1040 error(buf);
1041 return FALSE;
1043 return inzsub(fields, nfields, FALSE);
1046 static int
1047 inzcont(fields, nfields)
1048 register char ** const fields;
1049 const int nfields;
1051 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
1052 error(_("wrong number of fields on Zone continuation line"));
1053 return FALSE;
1055 return inzsub(fields, nfields, TRUE);
1058 static int
1059 inzsub(fields, nfields, iscont)
1060 register char ** const fields;
1061 const int nfields;
1062 const int iscont;
1064 register char * cp;
1065 static struct zone z;
1066 register int i_gmtoff, i_rule, i_format;
1067 register int i_untilyear, i_untilmonth;
1068 register int i_untilday, i_untiltime;
1069 register int hasuntil;
1071 if (iscont) {
1072 i_gmtoff = ZFC_GMTOFF;
1073 i_rule = ZFC_RULE;
1074 i_format = ZFC_FORMAT;
1075 i_untilyear = ZFC_TILYEAR;
1076 i_untilmonth = ZFC_TILMONTH;
1077 i_untilday = ZFC_TILDAY;
1078 i_untiltime = ZFC_TILTIME;
1079 z.z_name = NULL;
1080 } else {
1081 i_gmtoff = ZF_GMTOFF;
1082 i_rule = ZF_RULE;
1083 i_format = ZF_FORMAT;
1084 i_untilyear = ZF_TILYEAR;
1085 i_untilmonth = ZF_TILMONTH;
1086 i_untilday = ZF_TILDAY;
1087 i_untiltime = ZF_TILTIME;
1088 z.z_name = ecpyalloc(fields[ZF_NAME]);
1090 z.z_filename = filename;
1091 z.z_linenum = linenum;
1092 z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UTC offset"), TRUE);
1093 if ((cp = strchr(fields[i_format], '%')) != 0) {
1094 if (*++cp != 's' || strchr(cp, '%') != 0) {
1095 error(_("invalid abbreviation format"));
1096 return FALSE;
1099 z.z_rule = ecpyalloc(fields[i_rule]);
1100 z.z_format = ecpyalloc(fields[i_format]);
1101 hasuntil = nfields > i_untilyear;
1102 if (hasuntil) {
1103 z.z_untilrule.r_filename = filename;
1104 z.z_untilrule.r_linenum = linenum;
1105 rulesub(&z.z_untilrule,
1106 fields[i_untilyear],
1107 "only",
1109 (nfields > i_untilmonth) ?
1110 fields[i_untilmonth] : "Jan",
1111 (nfields > i_untilday) ? fields[i_untilday] : "1",
1112 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1113 z.z_untiltime = rpytime(&z.z_untilrule,
1114 z.z_untilrule.r_loyear);
1115 if (iscont && nzones > 0 &&
1116 z.z_untiltime > min_time &&
1117 z.z_untiltime < max_time &&
1118 zones[nzones - 1].z_untiltime > min_time &&
1119 zones[nzones - 1].z_untiltime < max_time &&
1120 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1121 error(_(
1122 "Zone continuation line end time is not after end time of previous line"
1124 return FALSE;
1127 zones = (struct zone *) (void *) erealloc((char *) zones,
1128 (int) ((nzones + 1) * sizeof *zones));
1129 zones[nzones++] = z;
1131 ** If there was an UNTIL field on this line,
1132 ** there's more information about the zone on the next line.
1134 return hasuntil;
1137 static void
1138 inleap(fields, nfields)
1139 register char ** const fields;
1140 const int nfields;
1142 register const char * cp;
1143 register const struct lookup * lp;
1144 register int i, j;
1145 int year, month, day;
1146 long dayoff, tod;
1147 zic_t t;
1149 if (nfields != LEAP_FIELDS) {
1150 error(_("wrong number of fields on Leap line"));
1151 return;
1153 dayoff = 0;
1154 cp = fields[LP_YEAR];
1155 if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
1157 ** Leapin' Lizards!
1159 error(_("invalid leaping year"));
1160 return;
1162 j = EPOCH_YEAR;
1163 while (j != year) {
1164 if (year > j) {
1165 i = len_years[isleap(j)];
1166 ++j;
1167 } else {
1168 --j;
1169 i = -len_years[isleap(j)];
1171 dayoff = oadd(dayoff, eitol(i));
1173 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1174 error(_("invalid month name"));
1175 return;
1177 month = lp->l_value;
1178 j = TM_JANUARY;
1179 while (j != month) {
1180 i = len_months[isleap(year)][j];
1181 dayoff = oadd(dayoff, eitol(i));
1182 ++j;
1184 cp = fields[LP_DAY];
1185 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1186 day <= 0 || day > len_months[isleap(year)][month]) {
1187 error(_("invalid day of month"));
1188 return;
1190 dayoff = oadd(dayoff, eitol(day - 1));
1191 if (dayoff < 0 && !TYPE_SIGNED(zic_t)) {
1192 error(_("time before zero"));
1193 return;
1195 if (dayoff < min_time / SECSPERDAY) {
1196 error(_("time too small"));
1197 return;
1199 if (dayoff > max_time / SECSPERDAY) {
1200 error(_("time too large"));
1201 return;
1203 t = (zic_t) dayoff * SECSPERDAY;
1204 tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1205 cp = fields[LP_CORR];
1207 register int positive;
1208 int count;
1210 if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1211 positive = FALSE;
1212 count = 1;
1213 } else if (strcmp(cp, "--") == 0) {
1214 positive = FALSE;
1215 count = 2;
1216 } else if (strcmp(cp, "+") == 0) {
1217 positive = TRUE;
1218 count = 1;
1219 } else if (strcmp(cp, "++") == 0) {
1220 positive = TRUE;
1221 count = 2;
1222 } else {
1223 error(_("illegal CORRECTION field on Leap line"));
1224 return;
1226 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1227 error(_(
1228 "illegal Rolling/Stationary field on Leap line"
1230 return;
1232 leapadd(tadd(t, tod), positive, lp->l_value, count);
1236 static void
1237 inlink(fields, nfields)
1238 register char ** const fields;
1239 const int nfields;
1241 struct link l;
1243 if (nfields != LINK_FIELDS) {
1244 error(_("wrong number of fields on Link line"));
1245 return;
1247 if (*fields[LF_FROM] == '\0') {
1248 error(_("blank FROM field on Link line"));
1249 return;
1251 if (*fields[LF_TO] == '\0') {
1252 error(_("blank TO field on Link line"));
1253 return;
1255 l.l_filename = filename;
1256 l.l_linenum = linenum;
1257 l.l_from = ecpyalloc(fields[LF_FROM]);
1258 l.l_to = ecpyalloc(fields[LF_TO]);
1259 links = (struct link *) (void *) erealloc((char *) links,
1260 (int) ((nlinks + 1) * sizeof *links));
1261 links[nlinks++] = l;
1264 static void
1265 rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
1266 register struct rule * const rp;
1267 const char * const loyearp;
1268 const char * const hiyearp;
1269 const char * const typep;
1270 const char * const monthp;
1271 const char * const dayp;
1272 const char * const timep;
1274 register const struct lookup * lp;
1275 register const char * cp;
1276 register char * dp;
1277 register char * ep;
1279 if ((lp = byword(monthp, mon_names)) == NULL) {
1280 error(_("invalid month name"));
1281 return;
1283 rp->r_month = lp->l_value;
1284 rp->r_todisstd = FALSE;
1285 rp->r_todisgmt = FALSE;
1286 dp = ecpyalloc(timep);
1287 if (*dp != '\0') {
1288 ep = dp + strlen(dp) - 1;
1289 switch (lowerit(*ep)) {
1290 case 's': /* Standard */
1291 rp->r_todisstd = TRUE;
1292 rp->r_todisgmt = FALSE;
1293 *ep = '\0';
1294 break;
1295 case 'w': /* Wall */
1296 rp->r_todisstd = FALSE;
1297 rp->r_todisgmt = FALSE;
1298 *ep = '\0';
1299 break;
1300 case 'g': /* Greenwich */
1301 case 'u': /* Universal */
1302 case 'z': /* Zulu */
1303 rp->r_todisstd = TRUE;
1304 rp->r_todisgmt = TRUE;
1305 *ep = '\0';
1306 break;
1309 rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1310 ifree(dp);
1312 ** Year work.
1314 cp = loyearp;
1315 lp = byword(cp, begin_years);
1316 if (lp != NULL) switch ((int) lp->l_value) {
1317 case YR_MINIMUM:
1318 rp->r_loyear = INT_MIN;
1319 break;
1320 case YR_MAXIMUM:
1321 rp->r_loyear = INT_MAX;
1322 break;
1323 default: /* "cannot happen" */
1324 (void) fprintf(stderr,
1325 _("%s: panic: Invalid l_value %d\n"),
1326 progname, lp->l_value);
1327 (void) exit(EXIT_FAILURE);
1328 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
1329 error(_("invalid starting year"));
1330 return;
1331 } else if (noise) {
1332 if (rp->r_loyear < min_year_representable)
1333 warning(_("starting year too low to be represented"));
1334 else if (rp->r_loyear > max_year_representable)
1335 warning(_("starting year too high to be represented"));
1337 cp = hiyearp;
1338 if ((lp = byword(cp, end_years)) != NULL) switch ((int) lp->l_value) {
1339 case YR_MINIMUM:
1340 rp->r_hiyear = INT_MIN;
1341 break;
1342 case YR_MAXIMUM:
1343 rp->r_hiyear = INT_MAX;
1344 break;
1345 case YR_ONLY:
1346 rp->r_hiyear = rp->r_loyear;
1347 break;
1348 default: /* "cannot happen" */
1349 (void) fprintf(stderr,
1350 _("%s: panic: Invalid l_value %d\n"),
1351 progname, lp->l_value);
1352 (void) exit(EXIT_FAILURE);
1353 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
1354 error(_("invalid ending year"));
1355 return;
1356 } else if (noise) {
1357 if (rp->r_loyear < min_year_representable)
1358 warning(_("ending year too low to be represented"));
1359 else if (rp->r_loyear > max_year_representable)
1360 warning(_("ending year too high to be represented"));
1362 if (rp->r_loyear > rp->r_hiyear) {
1363 error(_("starting year greater than ending year"));
1364 return;
1366 if (*typep == '\0')
1367 rp->r_yrtype = NULL;
1368 else {
1369 if (rp->r_loyear == rp->r_hiyear) {
1370 error(_("typed single year"));
1371 return;
1373 rp->r_yrtype = ecpyalloc(typep);
1375 if (rp->r_loyear < min_year && rp->r_loyear > 0)
1376 min_year = rp->r_loyear;
1378 ** Day work.
1379 ** Accept things such as:
1380 ** 1
1381 ** last-Sunday
1382 ** Sun<=20
1383 ** Sun>=7
1385 dp = ecpyalloc(dayp);
1386 if ((lp = byword(dp, lasts)) != NULL) {
1387 rp->r_dycode = DC_DOWLEQ;
1388 rp->r_wday = lp->l_value;
1389 rp->r_dayofmonth = len_months[1][rp->r_month];
1390 } else {
1391 if ((ep = strchr(dp, '<')) != 0)
1392 rp->r_dycode = DC_DOWLEQ;
1393 else if ((ep = strchr(dp, '>')) != 0)
1394 rp->r_dycode = DC_DOWGEQ;
1395 else {
1396 ep = dp;
1397 rp->r_dycode = DC_DOM;
1399 if (rp->r_dycode != DC_DOM) {
1400 *ep++ = 0;
1401 if (*ep++ != '=') {
1402 error(_("invalid day of month"));
1403 ifree(dp);
1404 return;
1406 if ((lp = byword(dp, wday_names)) == NULL) {
1407 error(_("invalid weekday name"));
1408 ifree(dp);
1409 return;
1411 rp->r_wday = lp->l_value;
1413 if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1414 rp->r_dayofmonth <= 0 ||
1415 (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1416 error(_("invalid day of month"));
1417 ifree(dp);
1418 return;
1421 ifree(dp);
1424 static void
1425 convert(val, buf)
1426 const long val;
1427 char * const buf;
1429 register int i;
1430 register long shift;
1432 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1433 buf[i] = val >> shift;
1436 static void
1437 puttzcode(val, fp)
1438 const long val;
1439 FILE * const fp;
1441 char buf[4];
1443 convert(val, buf);
1444 (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1447 static int
1448 atcomp(avp, bvp)
1449 void * avp;
1450 void * bvp;
1452 if (((struct attype *) avp)->at < ((struct attype *) bvp)->at)
1453 return -1;
1454 else if (((struct attype *) avp)->at > ((struct attype *) bvp)->at)
1455 return 1;
1456 else return 0;
1459 static void
1460 writezone(name)
1461 const char * const name;
1463 register FILE * fp;
1464 register int i, j;
1465 static char * fullname;
1466 static struct tzhead tzh;
1467 zic_t ats[TZ_MAX_TIMES];
1468 unsigned char types[TZ_MAX_TIMES];
1471 ** Sort.
1473 if (timecnt > 1)
1474 (void) qsort((void *) attypes, (size_t) timecnt,
1475 (size_t) sizeof *attypes, atcomp);
1477 ** Optimize.
1480 int fromi;
1481 int toi;
1483 toi = 0;
1484 fromi = 0;
1485 while (fromi < timecnt && attypes[fromi].at < min_time)
1486 ++fromi;
1487 if (isdsts[0] == 0)
1488 while (fromi < timecnt && attypes[fromi].type == 0)
1489 ++fromi; /* handled by default rule */
1490 for ( ; fromi < timecnt; ++fromi) {
1491 if (toi != 0 && ((attypes[fromi].at +
1492 gmtoffs[attypes[toi - 1].type]) <=
1493 (attypes[toi - 1].at + gmtoffs[toi == 1 ? 0
1494 : attypes[toi - 2].type]))) {
1495 attypes[toi - 1].type =
1496 attypes[fromi].type;
1497 continue;
1499 if (toi == 0 ||
1500 attypes[toi - 1].type != attypes[fromi].type)
1501 attypes[toi++] = attypes[fromi];
1503 timecnt = toi;
1506 ** Transfer.
1508 for (i = 0; i < timecnt; ++i) {
1509 ats[i] = attypes[i].at;
1510 types[i] = attypes[i].type;
1512 fullname = erealloc(fullname,
1513 (int) (strlen(directory) + 1 + strlen(name) + 1));
1514 (void) sprintf(fullname, "%s/%s", directory, name);
1516 ** Remove old file, if any, to snap links.
1518 if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT) {
1519 const char *e = strerror(errno);
1521 (void) fprintf(stderr, _("%s: Can't remove %s: %s\n"),
1522 progname, fullname, e);
1523 (void) exit(EXIT_FAILURE);
1525 if ((fp = fopen(fullname, "wb")) == NULL) {
1526 if (mkdirs(fullname) != 0)
1527 (void) exit(EXIT_FAILURE);
1528 if ((fp = fopen(fullname, "wb")) == NULL) {
1529 const char *e = strerror(errno);
1531 (void) fprintf(stderr, _("%s: Can't create %s: %s\n"),
1532 progname, fullname, e);
1533 (void) exit(EXIT_FAILURE);
1536 convert(eitol(typecnt), tzh.tzh_ttisgmtcnt);
1537 convert(eitol(typecnt), tzh.tzh_ttisstdcnt);
1538 convert(eitol(leapcnt), tzh.tzh_leapcnt);
1539 convert(eitol(timecnt), tzh.tzh_timecnt);
1540 convert(eitol(typecnt), tzh.tzh_typecnt);
1541 convert(eitol(charcnt), tzh.tzh_charcnt);
1542 (void) strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1543 #define DO(field) (void) fwrite((void *) tzh.field, \
1544 (size_t) sizeof tzh.field, (size_t) 1, fp)
1545 DO(tzh_magic);
1546 DO(tzh_reserved);
1547 DO(tzh_ttisgmtcnt);
1548 DO(tzh_ttisstdcnt);
1549 DO(tzh_leapcnt);
1550 DO(tzh_timecnt);
1551 DO(tzh_typecnt);
1552 DO(tzh_charcnt);
1553 #undef DO
1554 for (i = 0; i < timecnt; ++i) {
1555 j = leapcnt;
1556 while (--j >= 0)
1557 if (ats[i] >= trans[j]) {
1558 ats[i] = tadd(ats[i], corr[j]);
1559 break;
1561 puttzcode((long) ats[i], fp);
1563 if (timecnt > 0)
1564 (void) fwrite((void *) types, (size_t) sizeof types[0],
1565 (size_t) timecnt, fp);
1566 for (i = 0; i < typecnt; ++i) {
1567 puttzcode((long) gmtoffs[i], fp);
1568 (void) putc(isdsts[i], fp);
1569 (void) putc(abbrinds[i], fp);
1571 if (charcnt != 0)
1572 (void) fwrite((void *) chars, (size_t) sizeof chars[0],
1573 (size_t) charcnt, fp);
1574 for (i = 0; i < leapcnt; ++i) {
1575 if (roll[i]) {
1576 if (timecnt == 0 || trans[i] < ats[0]) {
1577 j = 0;
1578 while (isdsts[j])
1579 if (++j >= typecnt) {
1580 j = 0;
1581 break;
1583 } else {
1584 j = 1;
1585 while (j < timecnt && trans[i] >= ats[j])
1586 ++j;
1587 j = types[j - 1];
1589 puttzcode((long) tadd(trans[i], -gmtoffs[j]), fp);
1590 } else puttzcode((long) trans[i], fp);
1591 puttzcode((long) corr[i], fp);
1593 for (i = 0; i < typecnt; ++i)
1594 (void) putc(ttisstds[i], fp);
1595 for (i = 0; i < typecnt; ++i)
1596 (void) putc(ttisgmts[i], fp);
1597 if (ferror(fp) || fclose(fp)) {
1598 (void) fprintf(stderr, _("%s: Error writing %s\n"),
1599 progname, fullname);
1600 (void) exit(EXIT_FAILURE);
1604 static void
1605 doabbr(abbr, format, letters, isdst)
1606 char * const abbr;
1607 const char * const format;
1608 const char * const letters;
1609 const int isdst;
1611 if (strchr(format, '/') == NULL) {
1612 if (letters == NULL)
1613 (void) strcpy(abbr, format);
1614 else (void) sprintf(abbr, format, letters);
1615 } else if (isdst)
1616 (void) strcpy(abbr, strchr(format, '/') + 1);
1617 else {
1618 (void) strcpy(abbr, format);
1619 *strchr(abbr, '/') = '\0';
1623 static void
1624 outzone(zpfirst, zonecount)
1625 const struct zone * const zpfirst;
1626 const int zonecount;
1628 register const struct zone * zp;
1629 register struct rule * rp;
1630 register int i, j;
1631 register int usestart, useuntil;
1632 register zic_t starttime, untiltime;
1633 register long gmtoff;
1634 register long stdoff;
1635 register int year;
1636 register long startoff;
1637 register int startttisstd;
1638 register int startttisgmt;
1639 register int type;
1640 char startbuf[BUFSIZ];
1642 INITIALIZE(untiltime);
1643 INITIALIZE(starttime);
1645 ** Now. . .finally. . .generate some useful data!
1647 timecnt = 0;
1648 typecnt = 0;
1649 charcnt = 0;
1651 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1652 ** for noting the need to unconditionally initialize startttisstd.
1654 startttisstd = FALSE;
1655 startttisgmt = FALSE;
1656 for (i = 0; i < zonecount; ++i) {
1658 ** A guess that may well be corrected later.
1660 stdoff = 0;
1661 zp = &zpfirst[i];
1662 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
1663 useuntil = i < (zonecount - 1);
1664 if (useuntil && zp->z_untiltime <= min_time)
1665 continue;
1666 gmtoff = zp->z_gmtoff;
1667 eat(zp->z_filename, zp->z_linenum);
1668 *startbuf = '\0';
1669 startoff = zp->z_gmtoff;
1670 if (zp->z_nrules == 0) {
1671 stdoff = zp->z_stdoff;
1672 doabbr(startbuf, zp->z_format,
1673 (char *) NULL, stdoff != 0);
1674 type = addtype(oadd(zp->z_gmtoff, stdoff),
1675 startbuf, stdoff != 0, startttisstd,
1676 startttisgmt);
1677 if (usestart) {
1678 addtt(starttime, type);
1679 usestart = FALSE;
1680 } else if (stdoff != 0)
1681 addtt(min_time, type);
1682 } else for (year = min_year; year <= max_year; ++year) {
1683 if (useuntil && year > zp->z_untilrule.r_hiyear)
1684 break;
1686 ** Mark which rules to do in the current year.
1687 ** For those to do, calculate rpytime(rp, year);
1689 for (j = 0; j < zp->z_nrules; ++j) {
1690 rp = &zp->z_rules[j];
1691 eats(zp->z_filename, zp->z_linenum,
1692 rp->r_filename, rp->r_linenum);
1693 rp->r_todo = year >= rp->r_loyear &&
1694 year <= rp->r_hiyear &&
1695 yearistype(year, rp->r_yrtype);
1696 if (rp->r_todo)
1697 rp->r_temp = rpytime(rp, year);
1699 for ( ; ; ) {
1700 register int k;
1701 register zic_t jtime, ktime;
1702 register long offset;
1703 char buf[BUFSIZ];
1705 INITIALIZE(ktime);
1706 if (useuntil) {
1708 ** Turn untiltime into UTC
1709 ** assuming the current gmtoff and
1710 ** stdoff values.
1712 untiltime = zp->z_untiltime;
1713 if (!zp->z_untilrule.r_todisgmt)
1714 untiltime = tadd(untiltime,
1715 -gmtoff);
1716 if (!zp->z_untilrule.r_todisstd)
1717 untiltime = tadd(untiltime,
1718 -stdoff);
1721 ** Find the rule (of those to do, if any)
1722 ** that takes effect earliest in the year.
1724 k = -1;
1725 for (j = 0; j < zp->z_nrules; ++j) {
1726 rp = &zp->z_rules[j];
1727 if (!rp->r_todo)
1728 continue;
1729 eats(zp->z_filename, zp->z_linenum,
1730 rp->r_filename, rp->r_linenum);
1731 offset = rp->r_todisgmt ? 0 : gmtoff;
1732 if (!rp->r_todisstd)
1733 offset = oadd(offset, stdoff);
1734 jtime = rp->r_temp;
1735 if (jtime == min_time ||
1736 jtime == max_time)
1737 continue;
1738 jtime = tadd(jtime, -offset);
1739 if (k < 0 || jtime < ktime) {
1740 k = j;
1741 ktime = jtime;
1744 if (k < 0)
1745 break; /* go on to next year */
1746 rp = &zp->z_rules[k];
1747 rp->r_todo = FALSE;
1748 if (useuntil && ktime >= untiltime)
1749 break;
1750 stdoff = rp->r_stdoff;
1751 if (usestart && ktime == starttime)
1752 usestart = FALSE;
1753 if (usestart) {
1754 if (ktime < starttime) {
1755 startoff = oadd(zp->z_gmtoff,
1756 stdoff);
1757 doabbr(startbuf, zp->z_format,
1758 rp->r_abbrvar,
1759 rp->r_stdoff != 0);
1760 continue;
1762 if (*startbuf == '\0' &&
1763 startoff == oadd(zp->z_gmtoff,
1764 stdoff))
1765 doabbr(startbuf,
1766 zp->z_format,
1767 rp->r_abbrvar,
1768 rp->r_stdoff !=
1771 eats(zp->z_filename, zp->z_linenum,
1772 rp->r_filename, rp->r_linenum);
1773 doabbr(buf, zp->z_format, rp->r_abbrvar,
1774 rp->r_stdoff != 0);
1775 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
1776 type = addtype(offset, buf, rp->r_stdoff != 0,
1777 rp->r_todisstd, rp->r_todisgmt);
1778 addtt(ktime, type);
1781 if (usestart) {
1782 if (*startbuf == '\0' &&
1783 zp->z_format != NULL &&
1784 strchr(zp->z_format, '%') == NULL &&
1785 strchr(zp->z_format, '/') == NULL)
1786 (void) strcpy(startbuf, zp->z_format);
1787 eat(zp->z_filename, zp->z_linenum);
1788 if (*startbuf == '\0')
1789 error(_("can't determine time zone abbreviation to use just after until time"));
1790 else addtt(starttime,
1791 addtype(startoff, startbuf,
1792 startoff != zp->z_gmtoff,
1793 startttisstd,
1794 startttisgmt));
1797 ** Now we may get to set starttime for the next zone line.
1799 if (useuntil) {
1800 startttisstd = zp->z_untilrule.r_todisstd;
1801 startttisgmt = zp->z_untilrule.r_todisgmt;
1802 starttime = zp->z_untiltime;
1803 if (!startttisstd)
1804 starttime = tadd(starttime, -stdoff);
1805 if (!startttisgmt)
1806 starttime = tadd(starttime, -gmtoff);
1809 writezone(zpfirst->z_name);
1812 static void
1813 addtt(starttime, type)
1814 const zic_t starttime;
1815 int type;
1817 if (starttime <= min_time ||
1818 (timecnt == 1 && attypes[0].at < min_time)) {
1819 gmtoffs[0] = gmtoffs[type];
1820 isdsts[0] = isdsts[type];
1821 ttisstds[0] = ttisstds[type];
1822 ttisgmts[0] = ttisgmts[type];
1823 if (abbrinds[type] != 0)
1824 (void) strcpy(chars, &chars[abbrinds[type]]);
1825 abbrinds[0] = 0;
1826 charcnt = strlen(chars) + 1;
1827 typecnt = 1;
1828 timecnt = 0;
1829 type = 0;
1831 if (timecnt >= TZ_MAX_TIMES) {
1832 error(_("too many transitions?!"));
1833 (void) exit(EXIT_FAILURE);
1835 attypes[timecnt].at = starttime;
1836 attypes[timecnt].type = type;
1837 ++timecnt;
1840 static int
1841 addtype(gmtoff, abbr, isdst, ttisstd, ttisgmt)
1842 const long gmtoff;
1843 const char * const abbr;
1844 const int isdst;
1845 const int ttisstd;
1846 const int ttisgmt;
1848 register int i, j;
1850 if (isdst != TRUE && isdst != FALSE) {
1851 error(_("internal error - addtype called with bad isdst"));
1852 (void) exit(EXIT_FAILURE);
1854 if (ttisstd != TRUE && ttisstd != FALSE) {
1855 error(_("internal error - addtype called with bad ttisstd"));
1856 (void) exit(EXIT_FAILURE);
1858 if (ttisgmt != TRUE && ttisgmt != FALSE) {
1859 error(_("internal error - addtype called with bad ttisgmt"));
1860 (void) exit(EXIT_FAILURE);
1863 ** See if there's already an entry for this zone type.
1864 ** If so, just return its index.
1866 for (i = 0; i < typecnt; ++i) {
1867 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
1868 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
1869 ttisstd == ttisstds[i] &&
1870 ttisgmt == ttisgmts[i])
1871 return i;
1874 ** There isn't one; add a new one, unless there are already too
1875 ** many.
1877 if (typecnt >= TZ_MAX_TYPES) {
1878 error(_("too many local time types"));
1879 (void) exit(EXIT_FAILURE);
1881 gmtoffs[i] = gmtoff;
1882 isdsts[i] = isdst;
1883 ttisstds[i] = ttisstd;
1884 ttisgmts[i] = ttisgmt;
1886 for (j = 0; j < charcnt; ++j)
1887 if (strcmp(&chars[j], abbr) == 0)
1888 break;
1889 if (j == charcnt)
1890 newabbr(abbr);
1891 abbrinds[i] = j;
1892 ++typecnt;
1893 return i;
1896 static void
1897 leapadd(t, positive, rolling, count)
1898 const zic_t t;
1899 const int positive;
1900 const int rolling;
1901 int count;
1903 register int i, j;
1905 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
1906 error(_("too many leap seconds"));
1907 (void) exit(EXIT_FAILURE);
1909 for (i = 0; i < leapcnt; ++i)
1910 if (t <= trans[i]) {
1911 if (t == trans[i]) {
1912 error(_("repeated leap second moment"));
1913 (void) exit(EXIT_FAILURE);
1915 break;
1917 do {
1918 for (j = leapcnt; j > i; --j) {
1919 trans[j] = trans[j - 1];
1920 corr[j] = corr[j - 1];
1921 roll[j] = roll[j - 1];
1923 trans[i] = t;
1924 corr[i] = positive ? 1L : eitol(-count);
1925 roll[i] = rolling;
1926 ++leapcnt;
1927 } while (positive && --count != 0);
1930 static void
1931 adjleap P((void))
1933 register int i;
1934 register long last = 0;
1937 ** propagate leap seconds forward
1939 for (i = 0; i < leapcnt; ++i) {
1940 trans[i] = tadd(trans[i], last);
1941 last = corr[i] += last;
1945 static int
1946 yearistype(year, type)
1947 const int year;
1948 const char * const type;
1950 static char * buf;
1951 int result;
1953 if (type == NULL || *type == '\0')
1954 return TRUE;
1955 buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
1956 (void) sprintf(buf, "%s %d %s", yitcommand, year, type);
1957 result = system(buf);
1958 if (WIFEXITED(result)) switch (WEXITSTATUS(result)) {
1959 case 0:
1960 return TRUE;
1961 case 1:
1962 return FALSE;
1964 error(_("Wild result from command execution"));
1965 (void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
1966 progname, buf, result);
1967 for ( ; ; )
1968 (void) exit(EXIT_FAILURE);
1971 static int
1972 lowerit(a)
1973 int a;
1975 a = (unsigned char) a;
1976 return (isascii(a) && isupper(a)) ? tolower(a) : a;
1979 static int
1980 ciequal(ap, bp) /* case-insensitive equality */
1981 register const char * ap;
1982 register const char * bp;
1984 while (lowerit(*ap) == lowerit(*bp++))
1985 if (*ap++ == '\0')
1986 return TRUE;
1987 return FALSE;
1990 static int
1991 itsabbr(abbr, word)
1992 register const char * abbr;
1993 register const char * word;
1995 if (lowerit(*abbr) != lowerit(*word))
1996 return FALSE;
1997 ++word;
1998 while (*++abbr != '\0')
1999 do {
2000 if (*word == '\0')
2001 return FALSE;
2002 } while (lowerit(*word++) != lowerit(*abbr));
2003 return TRUE;
2006 static const struct lookup *
2007 byword(word, table)
2008 register const char * const word;
2009 register const struct lookup * const table;
2011 register const struct lookup * foundlp;
2012 register const struct lookup * lp;
2014 if (word == NULL || table == NULL)
2015 return NULL;
2017 ** Look for exact match.
2019 for (lp = table; lp->l_word != NULL; ++lp)
2020 if (ciequal(word, lp->l_word))
2021 return lp;
2023 ** Look for inexact match.
2025 foundlp = NULL;
2026 for (lp = table; lp->l_word != NULL; ++lp)
2027 if (itsabbr(word, lp->l_word)) {
2028 if (foundlp == NULL)
2029 foundlp = lp;
2030 else return NULL; /* multiple inexact matches */
2032 return foundlp;
2035 static char **
2036 getfields(cp)
2037 register char * cp;
2039 register char * dp;
2040 register char ** array;
2041 register int nsubs;
2043 if (cp == NULL)
2044 return NULL;
2045 array = (char **) (void *)
2046 emalloc((int) ((strlen(cp) + 1) * sizeof *array));
2047 nsubs = 0;
2048 for ( ; ; ) {
2049 while (isascii(*cp) && isspace((unsigned char) *cp))
2050 ++cp;
2051 if (*cp == '\0' || *cp == '#')
2052 break;
2053 array[nsubs++] = dp = cp;
2054 do {
2055 if ((*dp = *cp++) != '"')
2056 ++dp;
2057 else while ((*dp = *cp++) != '"')
2058 if (*dp != '\0')
2059 ++dp;
2060 else error(_(
2061 "Odd number of quotation marks"
2063 } while (*cp != '\0' && *cp != '#' &&
2064 (!isascii(*cp) || !isspace((unsigned char) *cp)));
2065 if (isascii(*cp) && isspace((unsigned char) *cp))
2066 ++cp;
2067 *dp = '\0';
2069 array[nsubs] = NULL;
2070 return array;
2073 static long
2074 oadd(t1, t2)
2075 const long t1;
2076 const long t2;
2078 register long t;
2080 t = t1 + t2;
2081 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2082 error(_("time overflow"));
2083 (void) exit(EXIT_FAILURE);
2085 return t;
2088 static zic_t
2089 tadd(t1, t2)
2090 const zic_t t1;
2091 const long t2;
2093 register zic_t t;
2095 if (t1 == max_time && t2 > 0)
2096 return max_time;
2097 if (t1 == min_time && t2 < 0)
2098 return min_time;
2099 t = t1 + t2;
2100 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2101 error(_("time overflow"));
2102 (void) exit(EXIT_FAILURE);
2104 return t;
2108 ** Given a rule, and a year, compute the date - in seconds since January 1,
2109 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2112 static zic_t
2113 rpytime(rp, wantedy)
2114 register const struct rule * const rp;
2115 register const int wantedy;
2117 register int y, m, i;
2118 register long dayoff; /* with a nod to Margaret O. */
2119 register zic_t t;
2121 if (wantedy == INT_MIN)
2122 return min_time;
2123 if (wantedy == INT_MAX)
2124 return max_time;
2125 dayoff = 0;
2126 m = TM_JANUARY;
2127 y = EPOCH_YEAR;
2128 while (wantedy != y) {
2129 if (wantedy > y) {
2130 i = len_years[isleap(y)];
2131 ++y;
2132 } else {
2133 --y;
2134 i = -len_years[isleap(y)];
2136 dayoff = oadd(dayoff, eitol(i));
2138 while (m != rp->r_month) {
2139 i = len_months[isleap(y)][m];
2140 dayoff = oadd(dayoff, eitol(i));
2141 ++m;
2143 i = rp->r_dayofmonth;
2144 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
2145 if (rp->r_dycode == DC_DOWLEQ)
2146 --i;
2147 else {
2148 error(_("use of 2/29 in non leap-year"));
2149 (void) exit(EXIT_FAILURE);
2152 --i;
2153 dayoff = oadd(dayoff, eitol(i));
2154 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
2155 register long wday;
2157 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2158 wday = eitol(EPOCH_WDAY);
2160 ** Don't trust mod of negative numbers.
2162 if (dayoff >= 0)
2163 wday = (wday + dayoff) % LDAYSPERWEEK;
2164 else {
2165 wday -= ((-dayoff) % LDAYSPERWEEK);
2166 if (wday < 0)
2167 wday += LDAYSPERWEEK;
2169 while (wday != eitol(rp->r_wday))
2170 if (rp->r_dycode == DC_DOWGEQ) {
2171 dayoff = oadd(dayoff, (long) 1);
2172 if (++wday >= LDAYSPERWEEK)
2173 wday = 0;
2174 ++i;
2175 } else {
2176 dayoff = oadd(dayoff, (long) -1);
2177 if (--wday < 0)
2178 wday = LDAYSPERWEEK - 1;
2179 --i;
2181 if (i < 0 || i >= len_months[isleap(y)][m]) {
2182 if (noise)
2183 warning(_("rule goes past start/end of month--\
2184 will not work with pre-2004 versions of zic"));
2187 if (dayoff < 0 && !TYPE_SIGNED(zic_t))
2188 return min_time;
2189 if (dayoff < min_time / SECSPERDAY)
2190 return min_time;
2191 if (dayoff > max_time / SECSPERDAY)
2192 return max_time;
2193 t = (zic_t) dayoff * SECSPERDAY;
2194 return tadd(t, rp->r_tod);
2197 static void
2198 newabbr(string)
2199 const char * const string;
2201 register int i;
2203 if (strcmp(string, GRANDPARENTED) != 0) {
2204 register const char * cp;
2205 register char * wp;
2208 ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2209 ** optionally followed by a + or - and a number from 1 to 14.
2211 cp = string;
2212 wp = NULL;
2213 while (isascii(*cp) && isalpha(*cp))
2214 ++cp;
2215 if (cp - string == 0)
2216 wp = _("time zone abbreviation lacks alphabetic at start");
2217 if (noise && cp - string > 3)
2218 wp = _("time zone abbreviation has more than 3 alphabetics");
2219 if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
2220 wp = _("time zone abbreviation has too many alphabetics");
2221 if (wp == NULL && (*cp == '+' || *cp == '-')) {
2222 ++cp;
2223 if (isascii(*cp) && isdigit(*cp))
2224 if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
2225 ++cp;
2227 if (*cp != '\0')
2228 wp = _("time zone abbreviation differs from POSIX standard");
2229 if (wp != NULL) {
2230 wp = ecpyalloc(wp);
2231 wp = ecatalloc(wp, " (");
2232 wp = ecatalloc(wp, string);
2233 wp = ecatalloc(wp, ")");
2234 warning(wp);
2235 ifree(wp);
2238 i = strlen(string) + 1;
2239 if (charcnt + i > TZ_MAX_CHARS) {
2240 error(_("too many, or too long, time zone abbreviations"));
2241 (void) exit(EXIT_FAILURE);
2243 (void) strcpy(&chars[charcnt], string);
2244 charcnt += eitol(i);
2247 static int
2248 mkdirs(argname)
2249 char * const argname;
2251 register char * name;
2252 register char * cp;
2254 if (argname == NULL || *argname == '\0')
2255 return 0;
2256 cp = name = ecpyalloc(argname);
2257 while ((cp = strchr(cp + 1, '/')) != 0) {
2258 *cp = '\0';
2259 #ifndef unix
2261 ** DOS drive specifier?
2263 if (isalpha((unsigned char) name[0]) &&
2264 name[1] == ':' && name[2] == '\0') {
2265 *cp = '/';
2266 continue;
2268 #endif /* !defined unix */
2269 if (!itsdir(name)) {
2271 ** It doesn't seem to exist, so we try to create it.
2272 ** Creation may fail because of the directory being
2273 ** created by some other multiprocessor, so we get
2274 ** to do extra checking.
2276 if (mkdir(name, MKDIR_UMASK) != 0) {
2277 const char *e = strerror(errno);
2279 if (errno != EEXIST || !itsdir(name)) {
2280 (void) fprintf(stderr,
2281 _("%s: Can't create directory %s: %s\n"),
2282 progname, name, e);
2283 ifree(name);
2284 return -1;
2288 *cp = '/';
2290 ifree(name);
2291 return 0;
2294 static long
2295 eitol(i)
2296 const int i;
2298 long l;
2300 l = i;
2301 if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0)) {
2302 (void) fprintf(stderr,
2303 _("%s: %d did not sign extend correctly\n"),
2304 progname, i);
2305 (void) exit(EXIT_FAILURE);
2307 return l;
2311 ** UNIX was a registered trademark of The Open Group in 2003.