* sysdeps/unix/readdir.c: Make sure we don't modify errno when we reached EOF.
[glibc.git] / timezone / zic.c
blob6f2f6570f5fd0ad9772c20944ccd5720cd808bec
1 #ifndef lint
2 #ifndef NOID
3 static char elsieid[] = "@(#)zic.c 7.101";
4 #endif /* !defined NOID */
5 #endif /* !defined lint */
7 #include "private.h"
8 #include "locale.h"
9 #include "tzfile.h"
10 #ifdef unix
11 #include "sys/stat.h" /* for umask manifest constants */
12 #endif /* defined unix */
15 ** On some ancient hosts, predicates like `isspace(C)' are defined
16 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
17 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
18 ** Neither the C Standard nor Posix require that `isascii' exist.
19 ** For portability, we check both ancient and modern requirements.
20 ** If isascii is not defined, the isascii check succeeds trivially.
22 #include "ctype.h"
23 #ifndef isascii
24 #define isascii(x) 1
25 #endif
27 struct rule {
28 const char * r_filename;
29 int r_linenum;
30 const char * r_name;
32 int r_loyear; /* for example, 1986 */
33 int r_hiyear; /* for example, 1986 */
34 const char * r_yrtype;
36 int r_month; /* 0..11 */
38 int r_dycode; /* see below */
39 int r_dayofmonth;
40 int r_wday;
42 long r_tod; /* time from midnight */
43 int r_todisstd; /* above is standard time if TRUE */
44 /* or wall clock time if FALSE */
45 int r_todisgmt; /* above is GMT if TRUE */
46 /* or local time if FALSE */
47 long r_stdoff; /* offset from standard time */
48 const char * r_abbrvar; /* variable part of abbreviation */
50 int r_todo; /* a rule to do (used in outzone) */
51 time_t r_temp; /* used in outzone */
55 ** r_dycode r_dayofmonth r_wday
58 #define DC_DOM 0 /* 1..31 */ /* unused */
59 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
60 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
62 struct zone {
63 const char * z_filename;
64 int z_linenum;
66 const char * z_name;
67 long z_gmtoff;
68 const char * z_rule;
69 const char * z_format;
71 long z_stdoff;
73 struct rule * z_rules;
74 int z_nrules;
76 struct rule z_untilrule;
77 time_t z_untiltime;
80 extern int getopt P((int argc, char * const argv[],
81 const char * options));
82 extern int link P((const char * fromname, const char * toname));
83 extern char * optarg;
84 extern int optind;
86 static void addtt P((time_t starttime, int type));
87 static int addtype P((long gmtoff, const char * abbr, int isdst,
88 int ttisstd, int ttisgmt));
89 static void leapadd P((time_t t, int positive, int rolling, int count));
90 static void adjleap P((void));
91 static void associate P((void));
92 static int ciequal P((const char * ap, const char * bp));
93 static void convert P((long val, char * buf));
94 static void dolink P((const char * fromfile, const char * tofile));
95 static void doabbr P((char * abbr, const char * format,
96 const char * letters, int isdst));
97 static void eat P((const char * name, int num));
98 static void eats P((const char * name, int num,
99 const char * rname, int rnum));
100 static long eitol P((int i));
101 static void error P((const char * message));
102 static char ** getfields P((char * buf));
103 static long gethms P((const char * string, const char * errstrng,
104 int signable));
105 static void infile P((const char * filename));
106 static void inleap P((char ** fields, int nfields));
107 static void inlink P((char ** fields, int nfields));
108 static void inrule P((char ** fields, int nfields));
109 static int inzcont P((char ** fields, int nfields));
110 static int inzone P((char ** fields, int nfields));
111 static int inzsub P((char ** fields, int nfields, int iscont));
112 static int itsabbr P((const char * abbr, const char * word));
113 static int itsdir P((const char * name));
114 static int lowerit P((int c));
115 static char * memcheck P((char * tocheck));
116 static int mkdirs P((char * filename));
117 static void newabbr P((const char * abbr));
118 static long oadd P((long t1, long t2));
119 static void outzone P((const struct zone * zp, int ntzones));
120 static void puttzcode P((long code, FILE * fp));
121 static int rcomp P((const void * leftp, const void * rightp));
122 static time_t rpytime P((const struct rule * rp, int wantedy));
123 static void rulesub P((struct rule * rp,
124 const char * loyearp, const char * hiyearp,
125 const char * typep, const char * monthp,
126 const char * dayp, const char * timep));
127 static void setboundaries P((void));
128 static time_t tadd P((time_t t1, long t2));
129 static void usage P((void));
130 static void writezone P((const char * name));
131 static int yearistype P((int year, const char * type));
133 #if !(HAVE_STRERROR - 0)
134 static char * strerror P((int));
135 #endif /* !(HAVE_STRERROR - 0) */
137 static int charcnt;
138 static int errors;
139 static const char * filename;
140 static int leapcnt;
141 static int linenum;
142 static time_t max_time;
143 static int max_year;
144 static int max_year_representable;
145 static time_t min_time;
146 static int min_year;
147 static int min_year_representable;
148 static int noise;
149 static const char * rfilename;
150 static int rlinenum;
151 static const char * progname;
152 static int timecnt;
153 static int typecnt;
156 ** Line codes.
159 #define LC_RULE 0
160 #define LC_ZONE 1
161 #define LC_LINK 2
162 #define LC_LEAP 3
165 ** Which fields are which on a Zone line.
168 #define ZF_NAME 1
169 #define ZF_GMTOFF 2
170 #define ZF_RULE 3
171 #define ZF_FORMAT 4
172 #define ZF_TILYEAR 5
173 #define ZF_TILMONTH 6
174 #define ZF_TILDAY 7
175 #define ZF_TILTIME 8
176 #define ZONE_MINFIELDS 5
177 #define ZONE_MAXFIELDS 9
180 ** Which fields are which on a Zone continuation line.
183 #define ZFC_GMTOFF 0
184 #define ZFC_RULE 1
185 #define ZFC_FORMAT 2
186 #define ZFC_TILYEAR 3
187 #define ZFC_TILMONTH 4
188 #define ZFC_TILDAY 5
189 #define ZFC_TILTIME 6
190 #define ZONEC_MINFIELDS 3
191 #define ZONEC_MAXFIELDS 7
194 ** Which files are which on a Rule line.
197 #define RF_NAME 1
198 #define RF_LOYEAR 2
199 #define RF_HIYEAR 3
200 #define RF_COMMAND 4
201 #define RF_MONTH 5
202 #define RF_DAY 6
203 #define RF_TOD 7
204 #define RF_STDOFF 8
205 #define RF_ABBRVAR 9
206 #define RULE_FIELDS 10
209 ** Which fields are which on a Link line.
212 #define LF_FROM 1
213 #define LF_TO 2
214 #define LINK_FIELDS 3
217 ** Which fields are which on a Leap line.
220 #define LP_YEAR 1
221 #define LP_MONTH 2
222 #define LP_DAY 3
223 #define LP_TIME 4
224 #define LP_CORR 5
225 #define LP_ROLL 6
226 #define LEAP_FIELDS 7
229 ** Year synonyms.
232 #define YR_MINIMUM 0
233 #define YR_MAXIMUM 1
234 #define YR_ONLY 2
236 static struct rule * rules;
237 static int nrules; /* number of rules */
239 static struct zone * zones;
240 static int nzones; /* number of zones */
242 struct link {
243 const char * l_filename;
244 int l_linenum;
245 const char * l_from;
246 const char * l_to;
249 static struct link * links;
250 static int nlinks;
252 struct lookup {
253 const char * l_word;
254 const int l_value;
257 static struct lookup const * byword P((const char * string,
258 const struct lookup * lp));
260 static struct lookup const line_codes[] = {
261 { "Rule", LC_RULE },
262 { "Zone", LC_ZONE },
263 { "Link", LC_LINK },
264 { "Leap", LC_LEAP },
265 { NULL, 0}
268 static struct lookup const mon_names[] = {
269 { "January", TM_JANUARY },
270 { "February", TM_FEBRUARY },
271 { "March", TM_MARCH },
272 { "April", TM_APRIL },
273 { "May", TM_MAY },
274 { "June", TM_JUNE },
275 { "July", TM_JULY },
276 { "August", TM_AUGUST },
277 { "September", TM_SEPTEMBER },
278 { "October", TM_OCTOBER },
279 { "November", TM_NOVEMBER },
280 { "December", TM_DECEMBER },
281 { NULL, 0 }
284 static struct lookup const wday_names[] = {
285 { "Sunday", TM_SUNDAY },
286 { "Monday", TM_MONDAY },
287 { "Tuesday", TM_TUESDAY },
288 { "Wednesday", TM_WEDNESDAY },
289 { "Thursday", TM_THURSDAY },
290 { "Friday", TM_FRIDAY },
291 { "Saturday", TM_SATURDAY },
292 { NULL, 0 }
295 static struct lookup const lasts[] = {
296 { "last-Sunday", TM_SUNDAY },
297 { "last-Monday", TM_MONDAY },
298 { "last-Tuesday", TM_TUESDAY },
299 { "last-Wednesday", TM_WEDNESDAY },
300 { "last-Thursday", TM_THURSDAY },
301 { "last-Friday", TM_FRIDAY },
302 { "last-Saturday", TM_SATURDAY },
303 { NULL, 0 }
306 static struct lookup const begin_years[] = {
307 { "minimum", YR_MINIMUM },
308 { "maximum", YR_MAXIMUM },
309 { NULL, 0 }
312 static struct lookup const end_years[] = {
313 { "minimum", YR_MINIMUM },
314 { "maximum", YR_MAXIMUM },
315 { "only", YR_ONLY },
316 { NULL, 0 }
319 static struct lookup const leap_types[] = {
320 { "Rolling", TRUE },
321 { "Stationary", FALSE },
322 { NULL, 0 }
325 static const int len_months[2][MONSPERYEAR] = {
326 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
327 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
330 static const int len_years[2] = {
331 DAYSPERNYEAR, DAYSPERLYEAR
334 static struct attype {
335 time_t at;
336 unsigned char type;
337 } attypes[TZ_MAX_TIMES];
338 static long gmtoffs[TZ_MAX_TYPES];
339 static char isdsts[TZ_MAX_TYPES];
340 static unsigned char abbrinds[TZ_MAX_TYPES];
341 static char ttisstds[TZ_MAX_TYPES];
342 static char ttisgmts[TZ_MAX_TYPES];
343 static char chars[TZ_MAX_CHARS];
344 static time_t trans[TZ_MAX_LEAPS];
345 static long corr[TZ_MAX_LEAPS];
346 static char roll[TZ_MAX_LEAPS];
349 ** Memory allocation.
352 static char *
353 memcheck(ptr)
354 char * const ptr;
356 if (ptr == NULL) {
357 const char *e = strerror(errno);
359 (void) fprintf(stderr, _("%s: Memory exhausted: %s\n"),
360 progname, e);
361 (void) exit(EXIT_FAILURE);
363 return ptr;
366 #define emalloc(size) memcheck(imalloc(size))
367 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
368 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
369 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
372 ** Error handling.
375 #if !(HAVE_STRERROR - 0)
376 static char *
377 strerror(errnum)
378 int errnum;
380 extern char * sys_errlist[];
381 extern int sys_nerr;
383 return (errnum > 0 && errnum <= sys_nerr) ?
384 sys_errlist[errnum] : _("Unknown system error");
386 #endif /* !(HAVE_STRERROR - 0) */
388 static void
389 eats(name, num, rname, rnum)
390 const char * const name;
391 const int num;
392 const char * const rname;
393 const int rnum;
395 filename = name;
396 linenum = num;
397 rfilename = rname;
398 rlinenum = rnum;
401 static void
402 eat(name, num)
403 const char * const name;
404 const int num;
406 eats(name, num, (char *) NULL, -1);
409 static void
410 error(string)
411 const char * const string;
414 ** Match the format of "cc" to allow sh users to
415 ** zic ... 2>&1 | error -t "*" -v
416 ** on BSD systems.
418 (void) fprintf(stderr, _("\"%s\", line %d: %s"),
419 filename, linenum, string);
420 if (rfilename != NULL)
421 (void) fprintf(stderr, _(" (rule from \"%s\", line %d)"),
422 rfilename, rlinenum);
423 (void) fprintf(stderr, "\n");
424 ++errors;
427 static void
428 warning(string)
429 const char * const string;
431 char * cp;
433 cp = ecpyalloc(_("warning: "));
434 cp = ecatalloc(cp, string);
435 error(cp);
436 ifree(cp);
437 --errors;
440 static void
441 usage P((void))
443 (void) fprintf(stderr, _("%s: usage is %s [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
444 progname, progname);
445 (void) exit(EXIT_FAILURE);
448 static const char * psxrules;
449 static const char * lcltime;
450 static const char * directory;
451 static const char * leapsec;
452 static const char * yitcommand;
453 static int sflag = FALSE;
456 main(argc, argv)
457 int argc;
458 char * argv[];
460 register int i;
461 register int j;
462 register int c;
464 #ifdef unix
465 (void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
466 #endif /* defined unix */
467 #if HAVE_GETTEXT - 0
468 (void) setlocale(LC_CTYPE, "");
469 (void) setlocale(LC_MESSAGES, "");
470 #ifdef TZ_DOMAINDIR
471 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
472 #endif /* defined TEXTDOMAINDIR */
473 (void) textdomain(TZ_DOMAIN);
474 #endif /* HAVE_GETTEXT - 0 */
475 progname = argv[0];
476 while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF && c != -1)
477 switch (c) {
478 default:
479 usage();
480 case 'd':
481 if (directory == NULL)
482 directory = optarg;
483 else {
484 (void) fprintf(stderr,
485 _("%s: More than one -d option specified\n"),
486 progname);
487 (void) exit(EXIT_FAILURE);
489 break;
490 case 'l':
491 if (lcltime == NULL)
492 lcltime = optarg;
493 else {
494 (void) fprintf(stderr,
495 _("%s: More than one -l option specified\n"),
496 progname);
497 (void) exit(EXIT_FAILURE);
499 break;
500 case 'p':
501 if (psxrules == NULL)
502 psxrules = optarg;
503 else {
504 (void) fprintf(stderr,
505 _("%s: More than one -p option specified\n"),
506 progname);
507 (void) exit(EXIT_FAILURE);
509 break;
510 case 'y':
511 if (yitcommand == NULL)
512 yitcommand = optarg;
513 else {
514 (void) fprintf(stderr,
515 _("%s: More than one -y option specified\n"),
516 progname);
517 (void) exit(EXIT_FAILURE);
519 break;
520 case 'L':
521 if (leapsec == NULL)
522 leapsec = optarg;
523 else {
524 (void) fprintf(stderr,
525 _("%s: More than one -L option specified\n"),
526 progname);
527 (void) exit(EXIT_FAILURE);
529 break;
530 case 'v':
531 noise = TRUE;
532 break;
533 case 's':
534 sflag = TRUE;
535 break;
537 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
538 usage(); /* usage message by request */
539 if (directory == NULL)
540 directory = TZDIR;
541 if (yitcommand == NULL)
542 yitcommand = "yearistype";
544 setboundaries();
546 if (optind < argc && leapsec != NULL) {
547 infile(leapsec);
548 adjleap();
551 for (i = optind; i < argc; ++i)
552 infile(argv[i]);
553 if (errors)
554 (void) exit(EXIT_FAILURE);
555 associate();
556 for (i = 0; i < nzones; i = j) {
558 ** Find the next non-continuation zone entry.
560 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
561 continue;
562 outzone(&zones[i], j - i);
565 ** Make links.
567 for (i = 0; i < nlinks; ++i) {
568 eat(links[i].l_filename, links[i].l_linenum);
569 dolink(links[i].l_from, links[i].l_to);
571 if (lcltime != NULL) {
572 eat("command line", 1);
573 dolink(lcltime, TZDEFAULT);
575 if (psxrules != NULL) {
576 eat("command line", 1);
577 dolink(psxrules, TZDEFRULES);
579 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
582 static void
583 dolink(fromfile, tofile)
584 const char * const fromfile;
585 const char * const tofile;
587 register char * fromname;
588 register char * toname;
590 if (fromfile[0] == '/')
591 fromname = ecpyalloc(fromfile);
592 else {
593 fromname = ecpyalloc(directory);
594 fromname = ecatalloc(fromname, "/");
595 fromname = ecatalloc(fromname, fromfile);
597 if (tofile[0] == '/')
598 toname = ecpyalloc(tofile);
599 else {
600 toname = ecpyalloc(directory);
601 toname = ecatalloc(toname, "/");
602 toname = ecatalloc(toname, tofile);
605 ** We get to be careful here since
606 ** there's a fair chance of root running us.
608 if (!itsdir(toname))
609 (void) remove(toname);
610 if (link(fromname, toname) != 0) {
611 int result;
613 if (mkdirs(toname) != 0)
614 (void) exit(EXIT_FAILURE);
616 result = link(fromname, toname);
617 #if (HAVE_SYMLINK - 0)
618 if (result != 0) {
619 const char *s = tofile;
620 register char *symlinkcontents = NULL;
621 while ((s = strchr(s+1, '/')) != NULL)
622 symlinkcontents = ecatalloc(symlinkcontents, "../");
623 symlinkcontents = ecatalloc(symlinkcontents, fromname);
625 result = unlink(toname);
626 if (result != 0 && errno != ENOENT) {
627 const char *e = strerror(errno);
629 (void) fprintf(stderr,
630 _("%s: Can't unlink %s: %s\n"),
631 progname, toname, e);
632 (void) exit(EXIT_FAILURE);
635 result = symlink(symlinkcontents, toname);
636 if (result == 0)
637 warning(_("hard link failed, symbolic link used"));
638 ifree(symlinkcontents);
640 #endif
641 if (result != 0) {
642 const char *e = strerror(errno);
644 (void) fprintf(stderr,
645 _("%s: Can't link from %s to %s: %s\n"),
646 progname, fromname, toname, e);
647 (void) exit(EXIT_FAILURE);
650 ifree(fromname);
651 ifree(toname);
654 #ifndef INT_MAX
655 #define INT_MAX ((int) (((unsigned)~0)>>1))
656 #endif /* !defined INT_MAX */
658 #ifndef INT_MIN
659 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
660 #endif /* !defined INT_MIN */
663 ** The tz file format currently allows at most 32-bit quantities.
664 ** This restriction should be removed before signed 32-bit values
665 ** wrap around in 2038, but unfortunately this will require a
666 ** change to the tz file format.
669 #define MAX_BITS_IN_FILE 32
670 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(time_t) < MAX_BITS_IN_FILE) ? TYPE_BIT(time_t) : MAX_BITS_IN_FILE)
672 static void
673 setboundaries P((void))
675 if (TYPE_SIGNED(time_t)) {
676 min_time = ~ (time_t) 0;
677 min_time <<= TIME_T_BITS_IN_FILE - 1;
678 max_time = ~ (time_t) 0 - min_time;
679 if (sflag)
680 min_time = 0;
681 } else {
682 min_time = 0;
683 max_time = 2 - sflag;
684 max_time <<= TIME_T_BITS_IN_FILE - 1;
685 --max_time;
687 min_year = TM_YEAR_BASE + gmtime(&min_time)->tm_year;
688 max_year = TM_YEAR_BASE + gmtime(&max_time)->tm_year;
689 min_year_representable = min_year;
690 max_year_representable = max_year;
693 static int
694 itsdir(name)
695 const char * const name;
697 register char * myname;
698 register int accres;
700 myname = ecpyalloc(name);
701 myname = ecatalloc(myname, "/.");
702 accres = access(myname, F_OK);
703 ifree(myname);
704 return accres == 0;
708 ** Associate sets of rules with zones.
712 ** Sort by rule name.
715 static int
716 rcomp(cp1, cp2)
717 const void * cp1;
718 const void * cp2;
720 return strcmp(((const struct rule *) cp1)->r_name,
721 ((const struct rule *) cp2)->r_name);
724 static void
725 associate P((void))
727 register struct zone * zp;
728 register struct rule * rp;
729 register int base, out;
730 register int i, j;
732 if (nrules != 0) {
733 (void) qsort((void *) rules, (size_t) nrules,
734 (size_t) sizeof *rules, rcomp);
735 for (i = 0; i < nrules - 1; ++i) {
736 if (strcmp(rules[i].r_name,
737 rules[i + 1].r_name) != 0)
738 continue;
739 if (strcmp(rules[i].r_filename,
740 rules[i + 1].r_filename) == 0)
741 continue;
742 eat(rules[i].r_filename, rules[i].r_linenum);
743 warning(_("same rule name in multiple files"));
744 eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
745 warning(_("same rule name in multiple files"));
746 for (j = i + 2; j < nrules; ++j) {
747 if (strcmp(rules[i].r_name,
748 rules[j].r_name) != 0)
749 break;
750 if (strcmp(rules[i].r_filename,
751 rules[j].r_filename) == 0)
752 continue;
753 if (strcmp(rules[i + 1].r_filename,
754 rules[j].r_filename) == 0)
755 continue;
756 break;
758 i = j - 1;
761 for (i = 0; i < nzones; ++i) {
762 zp = &zones[i];
763 zp->z_rules = NULL;
764 zp->z_nrules = 0;
766 for (base = 0; base < nrules; base = out) {
767 rp = &rules[base];
768 for (out = base + 1; out < nrules; ++out)
769 if (strcmp(rp->r_name, rules[out].r_name) != 0)
770 break;
771 for (i = 0; i < nzones; ++i) {
772 zp = &zones[i];
773 if (strcmp(zp->z_rule, rp->r_name) != 0)
774 continue;
775 zp->z_rules = rp;
776 zp->z_nrules = out - base;
779 for (i = 0; i < nzones; ++i) {
780 zp = &zones[i];
781 if (zp->z_nrules == 0) {
783 ** Maybe we have a local standard time offset.
785 eat(zp->z_filename, zp->z_linenum);
786 zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
787 TRUE);
789 ** Note, though, that if there's no rule,
790 ** a '%s' in the format is a bad thing.
792 if (strchr(zp->z_format, '%') != 0)
793 error(_("%s in ruleless zone"));
796 if (errors)
797 (void) exit(EXIT_FAILURE);
800 static void
801 infile(name)
802 const char * name;
804 register FILE * fp;
805 register char ** fields;
806 register char * cp;
807 register const struct lookup * lp;
808 register int nfields;
809 register int wantcont;
810 register int num;
811 char buf[BUFSIZ];
813 if (strcmp(name, "-") == 0) {
814 name = _("standard input");
815 fp = stdin;
816 } else if ((fp = fopen(name, "r")) == NULL) {
817 const char *e = strerror(errno);
819 (void) fprintf(stderr, _("%s: Can't open %s: %s\n"),
820 progname, name, e);
821 (void) exit(EXIT_FAILURE);
823 wantcont = FALSE;
824 for (num = 1; ; ++num) {
825 eat(name, num);
826 if (fgets(buf, (int) sizeof buf, fp) != buf)
827 break;
828 cp = strchr(buf, '\n');
829 if (cp == NULL) {
830 error(_("line too long"));
831 (void) exit(EXIT_FAILURE);
833 *cp = '\0';
834 fields = getfields(buf);
835 nfields = 0;
836 while (fields[nfields] != NULL) {
837 static char nada;
839 if (strcmp(fields[nfields], "-") == 0)
840 fields[nfields] = &nada;
841 ++nfields;
843 if (nfields == 0) {
844 /* nothing to do */
845 } else if (wantcont) {
846 wantcont = inzcont(fields, nfields);
847 } else {
848 lp = byword(fields[0], line_codes);
849 if (lp == NULL)
850 error(_("input line of unknown type"));
851 else switch ((int) (lp->l_value)) {
852 case LC_RULE:
853 inrule(fields, nfields);
854 wantcont = FALSE;
855 break;
856 case LC_ZONE:
857 wantcont = inzone(fields, nfields);
858 break;
859 case LC_LINK:
860 inlink(fields, nfields);
861 wantcont = FALSE;
862 break;
863 case LC_LEAP:
864 if (name != leapsec)
865 (void) fprintf(stderr,
866 _("%s: Leap line in non leap seconds file %s\n"),
867 progname, name);
868 else inleap(fields, nfields);
869 wantcont = FALSE;
870 break;
871 default: /* "cannot happen" */
872 (void) fprintf(stderr,
873 _("%s: panic: Invalid l_value %d\n"),
874 progname, lp->l_value);
875 (void) exit(EXIT_FAILURE);
878 ifree((char *) fields);
880 if (ferror(fp)) {
881 (void) fprintf(stderr, _("%s: Error reading %s\n"),
882 progname, filename);
883 (void) exit(EXIT_FAILURE);
885 if (fp != stdin && fclose(fp)) {
886 const char *e = strerror(errno);
888 (void) fprintf(stderr, _("%s: Error closing %s: %s\n"),
889 progname, filename, e);
890 (void) exit(EXIT_FAILURE);
892 if (wantcont)
893 error(_("expected continuation line not found"));
897 ** Convert a string of one of the forms
898 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
899 ** into a number of seconds.
900 ** A null string maps to zero.
901 ** Call error with errstring and return zero on errors.
904 static long
905 gethms(string, errstring, signable)
906 const char * string;
907 const char * const errstring;
908 const int signable;
910 int hh, mm, ss, sign;
912 if (string == NULL || *string == '\0')
913 return 0;
914 if (!signable)
915 sign = 1;
916 else if (*string == '-') {
917 sign = -1;
918 ++string;
919 } else sign = 1;
920 if (sscanf(string, scheck(string, "%d"), &hh) == 1)
921 mm = ss = 0;
922 else if (sscanf(string, scheck(string, "%d:%d"), &hh, &mm) == 2)
923 ss = 0;
924 else if (sscanf(string, scheck(string, "%d:%d:%d"),
925 &hh, &mm, &ss) != 3) {
926 error(errstring);
927 return 0;
929 if ((hh < 0 || hh >= HOURSPERDAY ||
930 mm < 0 || mm >= MINSPERHOUR ||
931 ss < 0 || ss > SECSPERMIN) &&
932 !(hh == HOURSPERDAY && mm == 0 && ss == 0)) {
933 error(errstring);
934 return 0;
936 return eitol(sign) *
937 (eitol(hh * MINSPERHOUR + mm) *
938 eitol(SECSPERMIN) + eitol(ss));
941 static void
942 inrule(fields, nfields)
943 register char ** const fields;
944 const int nfields;
946 static struct rule r;
948 if (nfields != RULE_FIELDS) {
949 error(_("wrong number of fields on Rule line"));
950 return;
952 if (*fields[RF_NAME] == '\0') {
953 error(_("nameless rule"));
954 return;
956 r.r_filename = filename;
957 r.r_linenum = linenum;
958 r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
959 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
960 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
961 r.r_name = ecpyalloc(fields[RF_NAME]);
962 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
963 rules = (struct rule *) (void *) erealloc((char *) rules,
964 (int) ((nrules + 1) * sizeof *rules));
965 rules[nrules++] = r;
968 static int
969 inzone(fields, nfields)
970 register char ** const fields;
971 const int nfields;
973 register int i;
974 static char * buf;
976 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
977 error(_("wrong number of fields on Zone line"));
978 return FALSE;
980 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
981 buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
982 (void) sprintf(buf,
983 _("\"Zone %s\" line and -l option are mutually exclusive"),
984 TZDEFAULT);
985 error(buf);
986 return FALSE;
988 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
989 buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
990 (void) sprintf(buf,
991 _("\"Zone %s\" line and -p option are mutually exclusive"),
992 TZDEFRULES);
993 error(buf);
994 return FALSE;
996 for (i = 0; i < nzones; ++i)
997 if (zones[i].z_name != NULL &&
998 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
999 buf = erealloc(buf, (int) (132 +
1000 strlen(fields[ZF_NAME]) +
1001 strlen(zones[i].z_filename)));
1002 (void) sprintf(buf,
1003 _("duplicate zone name %s (file \"%s\", line %d)"),
1004 fields[ZF_NAME],
1005 zones[i].z_filename,
1006 zones[i].z_linenum);
1007 error(buf);
1008 return FALSE;
1010 return inzsub(fields, nfields, FALSE);
1013 static int
1014 inzcont(fields, nfields)
1015 register char ** const fields;
1016 const int nfields;
1018 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
1019 error(_("wrong number of fields on Zone continuation line"));
1020 return FALSE;
1022 return inzsub(fields, nfields, TRUE);
1025 static int
1026 inzsub(fields, nfields, iscont)
1027 register char ** const fields;
1028 const int nfields;
1029 const int iscont;
1031 register char * cp;
1032 static struct zone z;
1033 register int i_gmtoff, i_rule, i_format;
1034 register int i_untilyear, i_untilmonth;
1035 register int i_untilday, i_untiltime;
1036 register int hasuntil;
1038 if (iscont) {
1039 i_gmtoff = ZFC_GMTOFF;
1040 i_rule = ZFC_RULE;
1041 i_format = ZFC_FORMAT;
1042 i_untilyear = ZFC_TILYEAR;
1043 i_untilmonth = ZFC_TILMONTH;
1044 i_untilday = ZFC_TILDAY;
1045 i_untiltime = ZFC_TILTIME;
1046 z.z_name = NULL;
1047 } else {
1048 i_gmtoff = ZF_GMTOFF;
1049 i_rule = ZF_RULE;
1050 i_format = ZF_FORMAT;
1051 i_untilyear = ZF_TILYEAR;
1052 i_untilmonth = ZF_TILMONTH;
1053 i_untilday = ZF_TILDAY;
1054 i_untiltime = ZF_TILTIME;
1055 z.z_name = ecpyalloc(fields[ZF_NAME]);
1057 z.z_filename = filename;
1058 z.z_linenum = linenum;
1059 z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UTC offset"), TRUE);
1060 if ((cp = strchr(fields[i_format], '%')) != 0) {
1061 if (*++cp != 's' || strchr(cp, '%') != 0) {
1062 error(_("invalid abbreviation format"));
1063 return FALSE;
1066 z.z_rule = ecpyalloc(fields[i_rule]);
1067 z.z_format = ecpyalloc(fields[i_format]);
1068 hasuntil = nfields > i_untilyear;
1069 if (hasuntil) {
1070 z.z_untilrule.r_filename = filename;
1071 z.z_untilrule.r_linenum = linenum;
1072 rulesub(&z.z_untilrule,
1073 fields[i_untilyear],
1074 "only",
1076 (nfields > i_untilmonth) ?
1077 fields[i_untilmonth] : "Jan",
1078 (nfields > i_untilday) ? fields[i_untilday] : "1",
1079 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1080 z.z_untiltime = rpytime(&z.z_untilrule,
1081 z.z_untilrule.r_loyear);
1082 if (iscont && nzones > 0 &&
1083 z.z_untiltime > min_time &&
1084 z.z_untiltime < max_time &&
1085 zones[nzones - 1].z_untiltime > min_time &&
1086 zones[nzones - 1].z_untiltime < max_time &&
1087 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1088 error(_("Zone continuation line end time is not after end time of previous line"));
1089 return FALSE;
1092 zones = (struct zone *) (void *) erealloc((char *) zones,
1093 (int) ((nzones + 1) * sizeof *zones));
1094 zones[nzones++] = z;
1096 ** If there was an UNTIL field on this line,
1097 ** there's more information about the zone on the next line.
1099 return hasuntil;
1102 static void
1103 inleap(fields, nfields)
1104 register char ** const fields;
1105 const int nfields;
1107 register const char * cp;
1108 register const struct lookup * lp;
1109 register int i, j;
1110 int year, month, day;
1111 long dayoff, tod;
1112 time_t t;
1114 if (nfields != LEAP_FIELDS) {
1115 error(_("wrong number of fields on Leap line"));
1116 return;
1118 dayoff = 0;
1119 cp = fields[LP_YEAR];
1120 if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
1122 * Leapin' Lizards!
1124 error(_("invalid leaping year"));
1125 return;
1127 j = EPOCH_YEAR;
1128 while (j != year) {
1129 if (year > j) {
1130 i = len_years[isleap(j)];
1131 ++j;
1132 } else {
1133 --j;
1134 i = -len_years[isleap(j)];
1136 dayoff = oadd(dayoff, eitol(i));
1138 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1139 error(_("invalid month name"));
1140 return;
1142 month = lp->l_value;
1143 j = TM_JANUARY;
1144 while (j != month) {
1145 i = len_months[isleap(year)][j];
1146 dayoff = oadd(dayoff, eitol(i));
1147 ++j;
1149 cp = fields[LP_DAY];
1150 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1151 day <= 0 || day > len_months[isleap(year)][month]) {
1152 error(_("invalid day of month"));
1153 return;
1155 dayoff = oadd(dayoff, eitol(day - 1));
1156 if (dayoff < 0 && !TYPE_SIGNED(time_t)) {
1157 error(_("time before zero"));
1158 return;
1160 t = (time_t) dayoff * SECSPERDAY;
1162 ** Cheap overflow check.
1164 if (t / SECSPERDAY != dayoff) {
1165 error(_("time overflow"));
1166 return;
1168 tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1169 cp = fields[LP_CORR];
1171 register int positive;
1172 int count;
1174 if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1175 positive = FALSE;
1176 count = 1;
1177 } else if (strcmp(cp, "--") == 0) {
1178 positive = FALSE;
1179 count = 2;
1180 } else if (strcmp(cp, "+") == 0) {
1181 positive = TRUE;
1182 count = 1;
1183 } else if (strcmp(cp, "++") == 0) {
1184 positive = TRUE;
1185 count = 2;
1186 } else {
1187 error(_("illegal CORRECTION field on Leap line"));
1188 return;
1190 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1191 error(_("illegal Rolling/Stationary field on Leap line"));
1192 return;
1194 leapadd(tadd(t, tod), positive, lp->l_value, count);
1198 static void
1199 inlink(fields, nfields)
1200 register char ** const fields;
1201 const int nfields;
1203 struct link l;
1205 if (nfields != LINK_FIELDS) {
1206 error(_("wrong number of fields on Link line"));
1207 return;
1209 if (*fields[LF_FROM] == '\0') {
1210 error(_("blank FROM field on Link line"));
1211 return;
1213 if (*fields[LF_TO] == '\0') {
1214 error(_("blank TO field on Link line"));
1215 return;
1217 l.l_filename = filename;
1218 l.l_linenum = linenum;
1219 l.l_from = ecpyalloc(fields[LF_FROM]);
1220 l.l_to = ecpyalloc(fields[LF_TO]);
1221 links = (struct link *) (void *) erealloc((char *) links,
1222 (int) ((nlinks + 1) * sizeof *links));
1223 links[nlinks++] = l;
1226 static void
1227 rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
1228 register struct rule * const rp;
1229 const char * const loyearp;
1230 const char * const hiyearp;
1231 const char * const typep;
1232 const char * const monthp;
1233 const char * const dayp;
1234 const char * const timep;
1236 register const struct lookup * lp;
1237 register const char * cp;
1238 register char * dp;
1239 register char * ep;
1241 if ((lp = byword(monthp, mon_names)) == NULL) {
1242 error(_("invalid month name"));
1243 return;
1245 rp->r_month = lp->l_value;
1246 rp->r_todisstd = FALSE;
1247 rp->r_todisgmt = FALSE;
1248 dp = ecpyalloc(timep);
1249 if (*dp != '\0') {
1250 ep = dp + strlen(dp) - 1;
1251 switch (lowerit(*ep)) {
1252 case 's': /* Standard */
1253 rp->r_todisstd = TRUE;
1254 rp->r_todisgmt = FALSE;
1255 *ep = '\0';
1256 break;
1257 case 'w': /* Wall */
1258 rp->r_todisstd = FALSE;
1259 rp->r_todisgmt = FALSE;
1260 *ep = '\0';
1261 break;
1262 case 'g': /* Greenwich */
1263 case 'u': /* Universal */
1264 case 'z': /* Zulu */
1265 rp->r_todisstd = TRUE;
1266 rp->r_todisgmt = TRUE;
1267 *ep = '\0';
1268 break;
1271 rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1272 ifree(dp);
1274 ** Year work.
1276 cp = loyearp;
1277 lp = byword(cp, begin_years);
1278 if (lp != NULL) switch ((int) lp->l_value) {
1279 case YR_MINIMUM:
1280 rp->r_loyear = INT_MIN;
1281 break;
1282 case YR_MAXIMUM:
1283 rp->r_loyear = INT_MAX;
1284 break;
1285 default: /* "cannot happen" */
1286 (void) fprintf(stderr,
1287 _("%s: panic: Invalid l_value %d\n"),
1288 progname, lp->l_value);
1289 (void) exit(EXIT_FAILURE);
1290 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
1291 error(_("invalid starting year"));
1292 return;
1293 } else if (noise) {
1294 if (rp->r_loyear < min_year_representable)
1295 warning(_("starting year too low to be represented"));
1296 else if (rp->r_loyear > max_year_representable)
1297 warning(_("starting year too high to be represented"));
1299 cp = hiyearp;
1300 if ((lp = byword(cp, end_years)) != NULL) switch ((int) lp->l_value) {
1301 case YR_MINIMUM:
1302 rp->r_hiyear = INT_MIN;
1303 break;
1304 case YR_MAXIMUM:
1305 rp->r_hiyear = INT_MAX;
1306 break;
1307 case YR_ONLY:
1308 rp->r_hiyear = rp->r_loyear;
1309 break;
1310 default: /* "cannot happen" */
1311 (void) fprintf(stderr,
1312 _("%s: panic: Invalid l_value %d\n"),
1313 progname, lp->l_value);
1314 (void) exit(EXIT_FAILURE);
1315 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
1316 error(_("invalid ending year"));
1317 return;
1318 } else if (noise) {
1319 if (rp->r_loyear < min_year_representable)
1320 warning(_("starting year too low to be represented"));
1321 else if (rp->r_loyear > max_year_representable)
1322 warning(_("starting year too high to be represented"));
1324 if (rp->r_loyear > rp->r_hiyear) {
1325 error(_("starting year greater than ending year"));
1326 return;
1328 if (*typep == '\0')
1329 rp->r_yrtype = NULL;
1330 else {
1331 if (rp->r_loyear == rp->r_hiyear) {
1332 error(_("typed single year"));
1333 return;
1335 rp->r_yrtype = ecpyalloc(typep);
1337 if (rp->r_loyear < min_year && rp->r_loyear > 0)
1338 min_year = rp->r_loyear;
1340 ** Day work.
1341 ** Accept things such as:
1342 ** 1
1343 ** last-Sunday
1344 ** Sun<=20
1345 ** Sun>=7
1347 dp = ecpyalloc(dayp);
1348 if ((lp = byword(dp, lasts)) != NULL) {
1349 rp->r_dycode = DC_DOWLEQ;
1350 rp->r_wday = lp->l_value;
1351 rp->r_dayofmonth = len_months[1][rp->r_month];
1352 } else {
1353 if ((ep = strchr(dp, '<')) != 0)
1354 rp->r_dycode = DC_DOWLEQ;
1355 else if ((ep = strchr(dp, '>')) != 0)
1356 rp->r_dycode = DC_DOWGEQ;
1357 else {
1358 ep = dp;
1359 rp->r_dycode = DC_DOM;
1361 if (rp->r_dycode != DC_DOM) {
1362 *ep++ = 0;
1363 if (*ep++ != '=') {
1364 error(_("invalid day of month"));
1365 ifree(dp);
1366 return;
1368 if ((lp = byword(dp, wday_names)) == NULL) {
1369 error(_("invalid weekday name"));
1370 ifree(dp);
1371 return;
1373 rp->r_wday = lp->l_value;
1375 if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1376 rp->r_dayofmonth <= 0 ||
1377 (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1378 error(_("invalid day of month"));
1379 ifree(dp);
1380 return;
1383 ifree(dp);
1386 static void
1387 convert(val, buf)
1388 const long val;
1389 char * const buf;
1391 register int i;
1392 register long shift;
1394 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1395 buf[i] = val >> shift;
1398 static void
1399 puttzcode(val, fp)
1400 const long val;
1401 FILE * const fp;
1403 char buf[4];
1405 convert(val, buf);
1406 (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1409 static int
1410 atcomp(avp, bvp)
1411 void * avp;
1412 void * bvp;
1414 if (((struct attype *) avp)->at < ((struct attype *) bvp)->at)
1415 return -1;
1416 else if (((struct attype *) avp)->at > ((struct attype *) bvp)->at)
1417 return 1;
1418 else return 0;
1421 static void
1422 writezone(name)
1423 const char * const name;
1425 register FILE * fp;
1426 register int i, j;
1427 static char * fullname;
1428 static struct tzhead tzh;
1429 time_t ats[TZ_MAX_TIMES];
1430 unsigned char types[TZ_MAX_TIMES];
1433 ** Sort.
1435 if (timecnt > 1)
1436 (void) qsort((void *) attypes, (size_t) timecnt,
1437 (size_t) sizeof *attypes, atcomp);
1439 ** Optimize.
1442 int fromi;
1443 int toi;
1445 toi = 0;
1446 fromi = 0;
1447 while (fromi < timecnt && attypes[fromi].at < min_time)
1448 ++fromi;
1449 if (isdsts[0] == 0)
1450 while (fromi < timecnt && attypes[fromi].type == 0)
1451 ++fromi; /* handled by default rule */
1452 for ( ; fromi < timecnt; ++fromi) {
1453 if (toi != 0
1454 && ((attypes[fromi].at
1455 + gmtoffs[attypes[toi - 1].type])
1456 <= (attypes[toi - 1].at
1457 + gmtoffs[toi == 1 ? 0
1458 : attypes[toi - 2].type]))) {
1459 attypes[toi - 1].type = attypes[fromi].type;
1460 continue;
1462 if (toi == 0 ||
1463 attypes[toi - 1].type != attypes[fromi].type)
1464 attypes[toi++] = attypes[fromi];
1466 timecnt = toi;
1469 ** Transfer.
1471 for (i = 0; i < timecnt; ++i) {
1472 ats[i] = attypes[i].at;
1473 types[i] = attypes[i].type;
1475 fullname = erealloc(fullname,
1476 (int) (strlen(directory) + 1 + strlen(name) + 1));
1477 (void) sprintf(fullname, "%s/%s", directory, name);
1479 ** Remove old file, if any, to snap links.
1481 if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT) {
1482 const char *e = strerror(errno);
1484 (void) fprintf(stderr, _("%s: Can't remove %s: %s\n"),
1485 progname, fullname, e);
1486 (void) exit(EXIT_FAILURE);
1488 if ((fp = fopen(fullname, "wb")) == NULL) {
1489 if (mkdirs(fullname) != 0)
1490 (void) exit(EXIT_FAILURE);
1491 if ((fp = fopen(fullname, "wb")) == NULL) {
1492 const char *e = strerror(errno);
1494 (void) fprintf(stderr, _("%s: Can't create %s: %s\n"),
1495 progname, fullname, e);
1496 (void) exit(EXIT_FAILURE);
1499 convert(eitol(typecnt), tzh.tzh_ttisgmtcnt);
1500 convert(eitol(typecnt), tzh.tzh_ttisstdcnt);
1501 convert(eitol(leapcnt), tzh.tzh_leapcnt);
1502 convert(eitol(timecnt), tzh.tzh_timecnt);
1503 convert(eitol(typecnt), tzh.tzh_typecnt);
1504 convert(eitol(charcnt), tzh.tzh_charcnt);
1505 (void) strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1506 #define DO(field) (void) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp)
1507 DO(tzh_magic);
1508 DO(tzh_reserved);
1509 DO(tzh_ttisgmtcnt);
1510 DO(tzh_ttisstdcnt);
1511 DO(tzh_leapcnt);
1512 DO(tzh_timecnt);
1513 DO(tzh_typecnt);
1514 DO(tzh_charcnt);
1515 #undef DO
1516 for (i = 0; i < timecnt; ++i) {
1517 j = leapcnt;
1518 while (--j >= 0)
1519 if (ats[i] >= trans[j]) {
1520 ats[i] = tadd(ats[i], corr[j]);
1521 break;
1523 puttzcode((long) ats[i], fp);
1525 if (timecnt > 0)
1526 (void) fwrite((void *) types, (size_t) sizeof types[0],
1527 (size_t) timecnt, fp);
1528 for (i = 0; i < typecnt; ++i) {
1529 puttzcode((long) gmtoffs[i], fp);
1530 (void) putc(isdsts[i], fp);
1531 (void) putc(abbrinds[i], fp);
1533 if (charcnt != 0)
1534 (void) fwrite((void *) chars, (size_t) sizeof chars[0],
1535 (size_t) charcnt, fp);
1536 for (i = 0; i < leapcnt; ++i) {
1537 if (roll[i]) {
1538 if (timecnt == 0 || trans[i] < ats[0]) {
1539 j = 0;
1540 while (isdsts[j])
1541 if (++j >= typecnt) {
1542 j = 0;
1543 break;
1545 } else {
1546 j = 1;
1547 while (j < timecnt && trans[i] >= ats[j])
1548 ++j;
1549 j = types[j - 1];
1551 puttzcode((long) tadd(trans[i], -gmtoffs[j]), fp);
1552 } else puttzcode((long) trans[i], fp);
1553 puttzcode((long) corr[i], fp);
1555 for (i = 0; i < typecnt; ++i)
1556 (void) putc(ttisstds[i], fp);
1557 for (i = 0; i < typecnt; ++i)
1558 (void) putc(ttisgmts[i], fp);
1559 if (ferror(fp) || fclose(fp)) {
1560 (void) fprintf(stderr, _("%s: Error writing %s\n"),
1561 progname, fullname);
1562 (void) exit(EXIT_FAILURE);
1566 static void
1567 doabbr(abbr, format, letters, isdst)
1568 char * const abbr;
1569 const char * const format;
1570 const char * const letters;
1571 const int isdst;
1573 if (strchr(format, '/') == NULL) {
1574 if (letters == NULL)
1575 (void) strcpy(abbr, format);
1576 else (void) sprintf(abbr, format, letters);
1577 } else if (isdst)
1578 (void) strcpy(abbr, strchr(format, '/') + 1);
1579 else {
1580 (void) strcpy(abbr, format);
1581 *strchr(abbr, '/') = '\0';
1585 static void
1586 outzone(zpfirst, zonecount)
1587 const struct zone * const zpfirst;
1588 const int zonecount;
1590 register const struct zone * zp;
1591 register struct rule * rp;
1592 register int i, j;
1593 register int usestart, useuntil;
1594 register time_t starttime, untiltime;
1595 register long gmtoff;
1596 register long stdoff;
1597 register int year;
1598 register long startoff;
1599 register int startttisstd;
1600 register int startttisgmt;
1601 register int type;
1602 char startbuf[BUFSIZ];
1604 INITIALIZE(untiltime);
1605 INITIALIZE(starttime);
1607 ** Now. . .finally. . .generate some useful data!
1609 timecnt = 0;
1610 typecnt = 0;
1611 charcnt = 0;
1613 ** A guess that may well be corrected later.
1615 stdoff = 0;
1617 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1618 ** for noting the need to unconditionally initialize startttisstd.
1620 startttisstd = FALSE;
1621 startttisgmt = FALSE;
1622 for (i = 0; i < zonecount; ++i) {
1623 zp = &zpfirst[i];
1624 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
1625 useuntil = i < (zonecount - 1);
1626 if (useuntil && zp->z_untiltime <= min_time)
1627 continue;
1628 gmtoff = zp->z_gmtoff;
1629 eat(zp->z_filename, zp->z_linenum);
1630 *startbuf = '\0';
1631 startoff = zp->z_gmtoff;
1632 if (zp->z_nrules == 0) {
1633 stdoff = zp->z_stdoff;
1634 doabbr(startbuf, zp->z_format,
1635 (char *) NULL, stdoff != 0);
1636 type = addtype(oadd(zp->z_gmtoff, stdoff),
1637 startbuf, stdoff != 0, startttisstd,
1638 startttisgmt);
1639 if (usestart) {
1640 addtt(starttime, type);
1641 usestart = FALSE;
1643 else if (stdoff != 0)
1644 addtt(min_time, type);
1645 } else for (year = min_year; year <= max_year; ++year) {
1646 if (useuntil && year > zp->z_untilrule.r_hiyear)
1647 break;
1649 ** Mark which rules to do in the current year.
1650 ** For those to do, calculate rpytime(rp, year);
1652 for (j = 0; j < zp->z_nrules; ++j) {
1653 rp = &zp->z_rules[j];
1654 eats(zp->z_filename, zp->z_linenum,
1655 rp->r_filename, rp->r_linenum);
1656 rp->r_todo = year >= rp->r_loyear &&
1657 year <= rp->r_hiyear &&
1658 yearistype(year, rp->r_yrtype);
1659 if (rp->r_todo)
1660 rp->r_temp = rpytime(rp, year);
1662 for ( ; ; ) {
1663 register int k;
1664 register time_t jtime, ktime;
1665 register long offset;
1666 char buf[BUFSIZ];
1668 INITIALIZE(ktime);
1669 if (useuntil) {
1671 ** Turn untiltime into UTC
1672 ** assuming the current gmtoff and
1673 ** stdoff values.
1675 untiltime = zp->z_untiltime;
1676 if (!zp->z_untilrule.r_todisgmt)
1677 untiltime = tadd(untiltime,
1678 -gmtoff);
1679 if (!zp->z_untilrule.r_todisstd)
1680 untiltime = tadd(untiltime,
1681 -stdoff);
1684 ** Find the rule (of those to do, if any)
1685 ** that takes effect earliest in the year.
1687 k = -1;
1688 for (j = 0; j < zp->z_nrules; ++j) {
1689 rp = &zp->z_rules[j];
1690 if (!rp->r_todo)
1691 continue;
1692 eats(zp->z_filename, zp->z_linenum,
1693 rp->r_filename, rp->r_linenum);
1694 offset = rp->r_todisgmt ? 0 : gmtoff;
1695 if (!rp->r_todisstd)
1696 offset = oadd(offset, stdoff);
1697 jtime = rp->r_temp;
1698 if (jtime == min_time ||
1699 jtime == max_time)
1700 continue;
1701 jtime = tadd(jtime, -offset);
1702 if (k < 0 || jtime < ktime) {
1703 k = j;
1704 ktime = jtime;
1707 if (k < 0)
1708 break; /* go on to next year */
1709 rp = &zp->z_rules[k];
1710 rp->r_todo = FALSE;
1711 if (useuntil && ktime >= untiltime)
1712 break;
1713 stdoff = rp->r_stdoff;
1714 if (usestart && ktime == starttime)
1715 usestart = FALSE;
1716 if (usestart) {
1717 if (ktime < starttime) {
1718 startoff = oadd(zp->z_gmtoff,
1719 stdoff);
1720 doabbr(startbuf, zp->z_format,
1721 rp->r_abbrvar,
1722 rp->r_stdoff != 0);
1723 continue;
1725 if (*startbuf == '\0' &&
1726 startoff == oadd(zp->z_gmtoff,
1727 stdoff)) {
1728 doabbr(startbuf, zp->z_format,
1729 rp->r_abbrvar,
1730 rp->r_stdoff != 0);
1733 eats(zp->z_filename, zp->z_linenum,
1734 rp->r_filename, rp->r_linenum);
1735 doabbr(buf, zp->z_format, rp->r_abbrvar,
1736 rp->r_stdoff != 0);
1737 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
1738 type = addtype(offset, buf, rp->r_stdoff != 0,
1739 rp->r_todisstd, rp->r_todisgmt);
1740 addtt(ktime, type);
1743 if (usestart) {
1744 if (*startbuf == '\0' &&
1745 zp->z_format != NULL &&
1746 strchr(zp->z_format, '%') == NULL &&
1747 strchr(zp->z_format, '/') == NULL)
1748 (void) strcpy(startbuf, zp->z_format);
1749 eat(zp->z_filename, zp->z_linenum);
1750 if (*startbuf == '\0')
1751 error(_("can't determine time zone abbreviation to use just after until time"));
1752 else addtt(starttime,
1753 addtype(startoff, startbuf,
1754 startoff != zp->z_gmtoff,
1755 startttisstd,
1756 startttisgmt));
1759 ** Now we may get to set starttime for the next zone line.
1761 if (useuntil) {
1762 startttisstd = zp->z_untilrule.r_todisstd;
1763 startttisgmt = zp->z_untilrule.r_todisgmt;
1764 starttime = zp->z_untiltime;
1765 if (!startttisstd)
1766 starttime = tadd(starttime, -stdoff);
1767 if (!startttisgmt)
1768 starttime = tadd(starttime, -gmtoff);
1771 writezone(zpfirst->z_name);
1774 static void
1775 addtt(starttime, type)
1776 const time_t starttime;
1777 int type;
1779 if (starttime <= min_time ||
1780 (timecnt == 1 && attypes[0].at < min_time)) {
1781 gmtoffs[0] = gmtoffs[type];
1782 isdsts[0] = isdsts[type];
1783 ttisstds[0] = ttisstds[type];
1784 ttisgmts[0] = ttisgmts[type];
1785 if (abbrinds[type] != 0)
1786 (void) strcpy(chars, &chars[abbrinds[type]]);
1787 abbrinds[0] = 0;
1788 charcnt = strlen(chars) + 1;
1789 typecnt = 1;
1790 timecnt = 0;
1791 type = 0;
1793 if (timecnt >= TZ_MAX_TIMES) {
1794 error(_("too many transitions?!"));
1795 (void) exit(EXIT_FAILURE);
1797 attypes[timecnt].at = starttime;
1798 attypes[timecnt].type = type;
1799 ++timecnt;
1802 static int
1803 addtype(gmtoff, abbr, isdst, ttisstd, ttisgmt)
1804 const long gmtoff;
1805 const char * const abbr;
1806 const int isdst;
1807 const int ttisstd;
1808 const int ttisgmt;
1810 register int i, j;
1812 if (isdst != TRUE && isdst != FALSE) {
1813 error(_("internal error - addtype called with bad isdst"));
1814 (void) exit(EXIT_FAILURE);
1816 if (ttisstd != TRUE && ttisstd != FALSE) {
1817 error(_("internal error - addtype called with bad ttisstd"));
1818 (void) exit(EXIT_FAILURE);
1820 if (ttisgmt != TRUE && ttisgmt != FALSE) {
1821 error(_("internal error - addtype called with bad ttisgmt"));
1822 (void) exit(EXIT_FAILURE);
1825 ** See if there's already an entry for this zone type.
1826 ** If so, just return its index.
1828 for (i = 0; i < typecnt; ++i) {
1829 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
1830 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
1831 ttisstd == ttisstds[i] &&
1832 ttisgmt == ttisgmts[i])
1833 return i;
1836 ** There isn't one; add a new one, unless there are already too
1837 ** many.
1839 if (typecnt >= TZ_MAX_TYPES) {
1840 error(_("too many local time types"));
1841 (void) exit(EXIT_FAILURE);
1843 gmtoffs[i] = gmtoff;
1844 isdsts[i] = isdst;
1845 ttisstds[i] = ttisstd;
1846 ttisgmts[i] = ttisgmt;
1848 for (j = 0; j < charcnt; ++j)
1849 if (strcmp(&chars[j], abbr) == 0)
1850 break;
1851 if (j == charcnt)
1852 newabbr(abbr);
1853 abbrinds[i] = j;
1854 ++typecnt;
1855 return i;
1858 static void
1859 leapadd(t, positive, rolling, count)
1860 const time_t t;
1861 const int positive;
1862 const int rolling;
1863 int count;
1865 register int i, j;
1867 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
1868 error(_("too many leap seconds"));
1869 (void) exit(EXIT_FAILURE);
1871 for (i = 0; i < leapcnt; ++i)
1872 if (t <= trans[i]) {
1873 if (t == trans[i]) {
1874 error(_("repeated leap second moment"));
1875 (void) exit(EXIT_FAILURE);
1877 break;
1879 do {
1880 for (j = leapcnt; j > i; --j) {
1881 trans[j] = trans[j - 1];
1882 corr[j] = corr[j - 1];
1883 roll[j] = roll[j - 1];
1885 trans[i] = t;
1886 corr[i] = positive ? 1L : eitol(-count);
1887 roll[i] = rolling;
1888 ++leapcnt;
1889 } while (positive && --count != 0);
1892 static void
1893 adjleap P((void))
1895 register int i;
1896 register long last = 0;
1899 ** propagate leap seconds forward
1901 for (i = 0; i < leapcnt; ++i) {
1902 trans[i] = tadd(trans[i], last);
1903 last = corr[i] += last;
1907 static int
1908 yearistype(year, type)
1909 const int year;
1910 const char * const type;
1912 static char * buf;
1913 int result;
1915 if (type == NULL || *type == '\0')
1916 return TRUE;
1917 buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
1918 (void) sprintf(buf, "%s %d %s", yitcommand, year, type);
1919 result = system(buf);
1920 if (WIFEXITED(result)) switch (WEXITSTATUS(result)) {
1921 case 0:
1922 return TRUE;
1923 case 1:
1924 return FALSE;
1926 error(_("Wild result from command execution"));
1927 (void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
1928 progname, buf, result);
1929 for ( ; ; )
1930 (void) exit(EXIT_FAILURE);
1933 static int
1934 lowerit(a)
1935 int a;
1937 a = (unsigned char) a;
1938 return (isascii(a) && isupper(a)) ? tolower(a) : a;
1941 static int
1942 ciequal(ap, bp) /* case-insensitive equality */
1943 register const char * ap;
1944 register const char * bp;
1946 while (lowerit(*ap) == lowerit(*bp++))
1947 if (*ap++ == '\0')
1948 return TRUE;
1949 return FALSE;
1952 static int
1953 itsabbr(abbr, word)
1954 register const char * abbr;
1955 register const char * word;
1957 if (lowerit(*abbr) != lowerit(*word))
1958 return FALSE;
1959 ++word;
1960 while (*++abbr != '\0')
1961 do {
1962 if (*word == '\0')
1963 return FALSE;
1964 } while (lowerit(*word++) != lowerit(*abbr));
1965 return TRUE;
1968 static const struct lookup *
1969 byword(word, table)
1970 register const char * const word;
1971 register const struct lookup * const table;
1973 register const struct lookup * foundlp;
1974 register const struct lookup * lp;
1976 if (word == NULL || table == NULL)
1977 return NULL;
1979 ** Look for exact match.
1981 for (lp = table; lp->l_word != NULL; ++lp)
1982 if (ciequal(word, lp->l_word))
1983 return lp;
1985 ** Look for inexact match.
1987 foundlp = NULL;
1988 for (lp = table; lp->l_word != NULL; ++lp)
1989 if (itsabbr(word, lp->l_word)) {
1990 if (foundlp == NULL)
1991 foundlp = lp;
1992 else return NULL; /* multiple inexact matches */
1994 return foundlp;
1997 static char **
1998 getfields(cp)
1999 register char * cp;
2001 register char * dp;
2002 register char ** array;
2003 register int nsubs;
2005 if (cp == NULL)
2006 return NULL;
2007 array = (char **) (void *)
2008 emalloc((int) ((strlen(cp) + 1) * sizeof *array));
2009 nsubs = 0;
2010 for ( ; ; ) {
2011 while (isascii(*cp) && isspace((unsigned char) *cp))
2012 ++cp;
2013 if (*cp == '\0' || *cp == '#')
2014 break;
2015 array[nsubs++] = dp = cp;
2016 do {
2017 if ((*dp = *cp++) != '"')
2018 ++dp;
2019 else while ((*dp = *cp++) != '"')
2020 if (*dp != '\0')
2021 ++dp;
2022 else error(_("Odd number of quotation marks"));
2023 } while (*cp != '\0' && *cp != '#' &&
2024 (!isascii(*cp) || !isspace((unsigned char) *cp)));
2025 if (isascii(*cp) && isspace((unsigned char) *cp))
2026 ++cp;
2027 *dp = '\0';
2029 array[nsubs] = NULL;
2030 return array;
2033 static long
2034 oadd(t1, t2)
2035 const long t1;
2036 const long t2;
2038 register long t;
2040 t = t1 + t2;
2041 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2042 error(_("time overflow"));
2043 (void) exit(EXIT_FAILURE);
2045 return t;
2048 static time_t
2049 tadd(t1, t2)
2050 const time_t t1;
2051 const long t2;
2053 register time_t t;
2055 if (t1 == max_time && t2 > 0)
2056 return max_time;
2057 if (t1 == min_time && t2 < 0)
2058 return min_time;
2059 t = t1 + t2;
2060 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2061 error(_("time overflow"));
2062 (void) exit(EXIT_FAILURE);
2064 return t;
2068 ** Given a rule, and a year, compute the date - in seconds since January 1,
2069 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2072 static time_t
2073 rpytime(rp, wantedy)
2074 register const struct rule * const rp;
2075 register const int wantedy;
2077 register int y, m, i;
2078 register long dayoff; /* with a nod to Margaret O. */
2079 register time_t t;
2081 if (wantedy == INT_MIN)
2082 return min_time;
2083 if (wantedy == INT_MAX)
2084 return max_time;
2085 dayoff = 0;
2086 m = TM_JANUARY;
2087 y = EPOCH_YEAR;
2088 while (wantedy != y) {
2089 if (wantedy > y) {
2090 i = len_years[isleap(y)];
2091 ++y;
2092 } else {
2093 --y;
2094 i = -len_years[isleap(y)];
2096 dayoff = oadd(dayoff, eitol(i));
2098 while (m != rp->r_month) {
2099 i = len_months[isleap(y)][m];
2100 dayoff = oadd(dayoff, eitol(i));
2101 ++m;
2103 i = rp->r_dayofmonth;
2104 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
2105 if (rp->r_dycode == DC_DOWLEQ)
2106 --i;
2107 else {
2108 error(_("use of 2/29 in non leap-year"));
2109 (void) exit(EXIT_FAILURE);
2112 --i;
2113 dayoff = oadd(dayoff, eitol(i));
2114 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
2115 register long wday;
2117 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2118 wday = eitol(EPOCH_WDAY);
2120 ** Don't trust mod of negative numbers.
2122 if (dayoff >= 0)
2123 wday = (wday + dayoff) % LDAYSPERWEEK;
2124 else {
2125 wday -= ((-dayoff) % LDAYSPERWEEK);
2126 if (wday < 0)
2127 wday += LDAYSPERWEEK;
2129 while (wday != eitol(rp->r_wday))
2130 if (rp->r_dycode == DC_DOWGEQ) {
2131 dayoff = oadd(dayoff, (long) 1);
2132 if (++wday >= LDAYSPERWEEK)
2133 wday = 0;
2134 ++i;
2135 } else {
2136 dayoff = oadd(dayoff, (long) -1);
2137 if (--wday < 0)
2138 wday = LDAYSPERWEEK - 1;
2139 --i;
2141 if (i < 0 || i >= len_months[isleap(y)][m]) {
2142 error(_("no day in month matches rule"));
2143 (void) exit(EXIT_FAILURE);
2146 if (dayoff < 0 && !TYPE_SIGNED(time_t))
2147 return min_time;
2148 t = (time_t) dayoff * SECSPERDAY;
2150 ** Cheap overflow check.
2152 if (t / SECSPERDAY != dayoff)
2153 return (dayoff > 0) ? max_time : min_time;
2154 return tadd(t, rp->r_tod);
2157 static void
2158 newabbr(string)
2159 const char * const string;
2161 register int i;
2163 i = strlen(string) + 1;
2164 if (charcnt + i > TZ_MAX_CHARS) {
2165 error(_("too many, or too long, time zone abbreviations"));
2166 (void) exit(EXIT_FAILURE);
2168 (void) strcpy(&chars[charcnt], string);
2169 charcnt += eitol(i);
2172 static int
2173 mkdirs(argname)
2174 char * const argname;
2176 register char * name;
2177 register char * cp;
2179 if (argname == NULL || *argname == '\0')
2180 return 0;
2181 cp = name = ecpyalloc(argname);
2182 while ((cp = strchr(cp + 1, '/')) != 0) {
2183 *cp = '\0';
2184 #ifndef unix
2186 ** DOS drive specifier?
2188 if (isalpha((unsigned char) name[0]) &&
2189 name[1] == ':' && name[2] == '\0') {
2190 *cp = '/';
2191 continue;
2193 #endif /* !defined unix */
2194 if (!itsdir(name)) {
2196 ** It doesn't seem to exist, so we try to create it.
2197 ** Creation may fail because of the directory being
2198 ** created by some other multiprocessor, so we get
2199 ** to do extra checking.
2201 if (mkdir(name, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) != 0) {
2202 const char *e = strerror(errno);
2204 if (errno != EEXIST || !itsdir(name)) {
2205 (void) fprintf(stderr,
2206 _("%s: Can't create directory %s: %s\n"),
2207 progname, name, e);
2208 ifree(name);
2209 return -1;
2213 *cp = '/';
2215 ifree(name);
2216 return 0;
2219 static long
2220 eitol(i)
2221 const int i;
2223 long l;
2225 l = i;
2226 if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0)) {
2227 (void) fprintf(stderr,
2228 _("%s: %d did not sign extend correctly\n"),
2229 progname, i);
2230 (void) exit(EXIT_FAILURE);
2232 return l;
2236 ** UNIX was a registered trademark of UNIX System Laboratories in 1993.