* sysdeps/m68k/fpu/s_finite.c: Add internal alias.
[glibc.git] / timezone / zic.c
blob64642b39c596aa047c840d2e6224b5959e56e4c2
1 #ifndef lint
2 #ifndef NOID
3 static char elsieid[] = "@(#)zic.c 7.107";
4 #endif /* !defined NOID */
5 #endif /* !defined lint */
7 #include "private.h"
8 #include "locale.h"
9 #include "tzfile.h"
11 #if HAVE_SYS_STAT_H
12 #include "sys/stat.h"
13 #endif
14 #ifdef S_IRUSR
15 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
16 #else
17 #define MKDIR_UMASK 0755
18 #endif
21 ** On some ancient hosts, predicates like `isspace(C)' are defined
22 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
23 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
24 ** Neither the C Standard nor Posix require that `isascii' exist.
25 ** For portability, we check both ancient and modern requirements.
26 ** If isascii is not defined, the isascii check succeeds trivially.
28 #include "ctype.h"
29 #ifndef isascii
30 #define isascii(x) 1
31 #endif
33 struct rule {
34 const char * r_filename;
35 int r_linenum;
36 const char * r_name;
38 int r_loyear; /* for example, 1986 */
39 int r_hiyear; /* for example, 1986 */
40 const char * r_yrtype;
42 int r_month; /* 0..11 */
44 int r_dycode; /* see below */
45 int r_dayofmonth;
46 int r_wday;
48 long r_tod; /* time from midnight */
49 int r_todisstd; /* above is standard time if TRUE */
50 /* or wall clock time if FALSE */
51 int r_todisgmt; /* above is GMT if TRUE */
52 /* or local time if FALSE */
53 long r_stdoff; /* offset from standard time */
54 const char * r_abbrvar; /* variable part of abbreviation */
56 int r_todo; /* a rule to do (used in outzone) */
57 time_t r_temp; /* used in outzone */
61 ** r_dycode r_dayofmonth r_wday
64 #define DC_DOM 0 /* 1..31 */ /* unused */
65 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
66 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
68 struct zone {
69 const char * z_filename;
70 int z_linenum;
72 const char * z_name;
73 long z_gmtoff;
74 const char * z_rule;
75 const char * z_format;
77 long z_stdoff;
79 struct rule * z_rules;
80 int z_nrules;
82 struct rule z_untilrule;
83 time_t z_untiltime;
86 extern int getopt P((int argc, char * const argv[],
87 const char * options));
88 extern int link P((const char * fromname, const char * toname));
89 extern char * optarg;
90 extern int optind;
92 static void addtt P((time_t starttime, int type));
93 static int addtype P((long gmtoff, const char * abbr, int isdst,
94 int ttisstd, int ttisgmt));
95 static void leapadd P((time_t t, int positive, int rolling, int count));
96 static void adjleap P((void));
97 static void associate P((void));
98 static int ciequal P((const char * ap, const char * bp));
99 static void convert P((long val, char * buf));
100 static void dolink P((const char * fromfile, const char * tofile));
101 static void doabbr P((char * abbr, const char * format,
102 const char * letters, int isdst));
103 static void eat P((const char * name, int num));
104 static void eats P((const char * name, int num,
105 const char * rname, int rnum));
106 static long eitol P((int i));
107 static void error P((const char * message));
108 static char ** getfields P((char * buf));
109 static long gethms P((const char * string, const char * errstrng,
110 int signable));
111 static void infile P((const char * filename));
112 static void inleap P((char ** fields, int nfields));
113 static void inlink P((char ** fields, int nfields));
114 static void inrule P((char ** fields, int nfields));
115 static int inzcont P((char ** fields, int nfields));
116 static int inzone P((char ** fields, int nfields));
117 static int inzsub P((char ** fields, int nfields, int iscont));
118 static int itsabbr P((const char * abbr, const char * word));
119 static int itsdir P((const char * name));
120 static int lowerit P((int c));
121 static char * memcheck P((char * tocheck));
122 static int mkdirs P((char * filename));
123 static void newabbr P((const char * abbr));
124 static long oadd P((long t1, long t2));
125 static void outzone P((const struct zone * zp, int ntzones));
126 static void puttzcode P((long code, FILE * fp));
127 static int rcomp P((const void * leftp, const void * rightp));
128 static time_t rpytime P((const struct rule * rp, int wantedy));
129 static void rulesub P((struct rule * rp,
130 const char * loyearp, const char * hiyearp,
131 const char * typep, const char * monthp,
132 const char * dayp, const char * timep));
133 static void setboundaries P((void));
134 static time_t tadd P((time_t t1, long t2));
135 static void usage P((void));
136 static void writezone P((const char * name));
137 static int yearistype P((int year, const char * type));
139 #if !(HAVE_STRERROR - 0)
140 static char * strerror P((int));
141 #endif /* !(HAVE_STRERROR - 0) */
143 static int charcnt;
144 static int errors;
145 static const char * filename;
146 static int leapcnt;
147 static int linenum;
148 static time_t max_time;
149 static int max_year;
150 static int max_year_representable;
151 static time_t min_time;
152 static int min_year;
153 static int min_year_representable;
154 static int noise;
155 static const char * rfilename;
156 static int rlinenum;
157 static const char * progname;
158 static int timecnt;
159 static int typecnt;
162 ** Line codes.
165 #define LC_RULE 0
166 #define LC_ZONE 1
167 #define LC_LINK 2
168 #define LC_LEAP 3
171 ** Which fields are which on a Zone line.
174 #define ZF_NAME 1
175 #define ZF_GMTOFF 2
176 #define ZF_RULE 3
177 #define ZF_FORMAT 4
178 #define ZF_TILYEAR 5
179 #define ZF_TILMONTH 6
180 #define ZF_TILDAY 7
181 #define ZF_TILTIME 8
182 #define ZONE_MINFIELDS 5
183 #define ZONE_MAXFIELDS 9
186 ** Which fields are which on a Zone continuation line.
189 #define ZFC_GMTOFF 0
190 #define ZFC_RULE 1
191 #define ZFC_FORMAT 2
192 #define ZFC_TILYEAR 3
193 #define ZFC_TILMONTH 4
194 #define ZFC_TILDAY 5
195 #define ZFC_TILTIME 6
196 #define ZONEC_MINFIELDS 3
197 #define ZONEC_MAXFIELDS 7
200 ** Which files are which on a Rule line.
203 #define RF_NAME 1
204 #define RF_LOYEAR 2
205 #define RF_HIYEAR 3
206 #define RF_COMMAND 4
207 #define RF_MONTH 5
208 #define RF_DAY 6
209 #define RF_TOD 7
210 #define RF_STDOFF 8
211 #define RF_ABBRVAR 9
212 #define RULE_FIELDS 10
215 ** Which fields are which on a Link line.
218 #define LF_FROM 1
219 #define LF_TO 2
220 #define LINK_FIELDS 3
223 ** Which fields are which on a Leap line.
226 #define LP_YEAR 1
227 #define LP_MONTH 2
228 #define LP_DAY 3
229 #define LP_TIME 4
230 #define LP_CORR 5
231 #define LP_ROLL 6
232 #define LEAP_FIELDS 7
235 ** Year synonyms.
238 #define YR_MINIMUM 0
239 #define YR_MAXIMUM 1
240 #define YR_ONLY 2
242 static struct rule * rules;
243 static int nrules; /* number of rules */
245 static struct zone * zones;
246 static int nzones; /* number of zones */
248 struct link {
249 const char * l_filename;
250 int l_linenum;
251 const char * l_from;
252 const char * l_to;
255 static struct link * links;
256 static int nlinks;
258 struct lookup {
259 const char * l_word;
260 const int l_value;
263 static struct lookup const * byword P((const char * string,
264 const struct lookup * lp));
266 static struct lookup const line_codes[] = {
267 { "Rule", LC_RULE },
268 { "Zone", LC_ZONE },
269 { "Link", LC_LINK },
270 { "Leap", LC_LEAP },
271 { NULL, 0}
274 static struct lookup const mon_names[] = {
275 { "January", TM_JANUARY },
276 { "February", TM_FEBRUARY },
277 { "March", TM_MARCH },
278 { "April", TM_APRIL },
279 { "May", TM_MAY },
280 { "June", TM_JUNE },
281 { "July", TM_JULY },
282 { "August", TM_AUGUST },
283 { "September", TM_SEPTEMBER },
284 { "October", TM_OCTOBER },
285 { "November", TM_NOVEMBER },
286 { "December", TM_DECEMBER },
287 { NULL, 0 }
290 static struct lookup const wday_names[] = {
291 { "Sunday", TM_SUNDAY },
292 { "Monday", TM_MONDAY },
293 { "Tuesday", TM_TUESDAY },
294 { "Wednesday", TM_WEDNESDAY },
295 { "Thursday", TM_THURSDAY },
296 { "Friday", TM_FRIDAY },
297 { "Saturday", TM_SATURDAY },
298 { NULL, 0 }
301 static struct lookup const lasts[] = {
302 { "last-Sunday", TM_SUNDAY },
303 { "last-Monday", TM_MONDAY },
304 { "last-Tuesday", TM_TUESDAY },
305 { "last-Wednesday", TM_WEDNESDAY },
306 { "last-Thursday", TM_THURSDAY },
307 { "last-Friday", TM_FRIDAY },
308 { "last-Saturday", TM_SATURDAY },
309 { NULL, 0 }
312 static struct lookup const begin_years[] = {
313 { "minimum", YR_MINIMUM },
314 { "maximum", YR_MAXIMUM },
315 { NULL, 0 }
318 static struct lookup const end_years[] = {
319 { "minimum", YR_MINIMUM },
320 { "maximum", YR_MAXIMUM },
321 { "only", YR_ONLY },
322 { NULL, 0 }
325 static struct lookup const leap_types[] = {
326 { "Rolling", TRUE },
327 { "Stationary", FALSE },
328 { NULL, 0 }
331 static const int len_months[2][MONSPERYEAR] = {
332 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
333 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
336 static const int len_years[2] = {
337 DAYSPERNYEAR, DAYSPERLYEAR
340 static struct attype {
341 time_t at;
342 unsigned char type;
343 } attypes[TZ_MAX_TIMES];
344 static long gmtoffs[TZ_MAX_TYPES];
345 static char isdsts[TZ_MAX_TYPES];
346 static unsigned char abbrinds[TZ_MAX_TYPES];
347 static char ttisstds[TZ_MAX_TYPES];
348 static char ttisgmts[TZ_MAX_TYPES];
349 static char chars[TZ_MAX_CHARS];
350 static time_t trans[TZ_MAX_LEAPS];
351 static long corr[TZ_MAX_LEAPS];
352 static char roll[TZ_MAX_LEAPS];
355 ** Memory allocation.
358 static char *
359 memcheck(ptr)
360 char * const ptr;
362 if (ptr == NULL) {
363 const char *e = strerror(errno);
365 (void) fprintf(stderr, _("%s: Memory exhausted: %s\n"),
366 progname, e);
367 (void) exit(EXIT_FAILURE);
369 return ptr;
372 #define emalloc(size) memcheck(imalloc(size))
373 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
374 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
375 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
378 ** Error handling.
381 #if !(HAVE_STRERROR - 0)
382 static char *
383 strerror(errnum)
384 int errnum;
386 extern char * sys_errlist[];
387 extern int sys_nerr;
389 return (errnum > 0 && errnum <= sys_nerr) ?
390 sys_errlist[errnum] : _("Unknown system error");
392 #endif /* !(HAVE_STRERROR - 0) */
394 static void
395 eats(name, num, rname, rnum)
396 const char * const name;
397 const int num;
398 const char * const rname;
399 const int rnum;
401 filename = name;
402 linenum = num;
403 rfilename = rname;
404 rlinenum = rnum;
407 static void
408 eat(name, num)
409 const char * const name;
410 const int num;
412 eats(name, num, (char *) NULL, -1);
415 static void
416 error(string)
417 const char * const string;
420 ** Match the format of "cc" to allow sh users to
421 ** zic ... 2>&1 | error -t "*" -v
422 ** on BSD systems.
424 (void) fprintf(stderr, _("\"%s\", line %d: %s"),
425 filename, linenum, string);
426 if (rfilename != NULL)
427 (void) fprintf(stderr, _(" (rule from \"%s\", line %d)"),
428 rfilename, rlinenum);
429 (void) fprintf(stderr, "\n");
430 ++errors;
433 static void
434 warning(string)
435 const char * const string;
437 char * cp;
439 cp = ecpyalloc(_("warning: "));
440 cp = ecatalloc(cp, string);
441 error(cp);
442 ifree(cp);
443 --errors;
446 static void
447 usage P((void))
449 (void) fprintf(stderr, _("%s: usage is %s [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
450 progname, progname);
451 (void) exit(EXIT_FAILURE);
454 static const char * psxrules;
455 static const char * lcltime;
456 static const char * directory;
457 static const char * leapsec;
458 static const char * yitcommand;
459 static int sflag = FALSE;
462 main(argc, argv)
463 int argc;
464 char * argv[];
466 register int i;
467 register int j;
468 register int c;
470 #ifdef unix
471 (void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
472 #endif /* defined unix */
473 #if HAVE_GETTEXT - 0
474 (void) setlocale(LC_CTYPE, "");
475 (void) setlocale(LC_MESSAGES, "");
476 #ifdef TZ_DOMAINDIR
477 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
478 #endif /* defined TEXTDOMAINDIR */
479 (void) textdomain(TZ_DOMAIN);
480 #endif /* HAVE_GETTEXT - 0 */
481 progname = argv[0];
482 while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF && c != -1)
483 switch (c) {
484 default:
485 usage();
486 case 'd':
487 if (directory == NULL)
488 directory = optarg;
489 else {
490 (void) fprintf(stderr,
491 _("%s: More than one -d option specified\n"),
492 progname);
493 (void) exit(EXIT_FAILURE);
495 break;
496 case 'l':
497 if (lcltime == NULL)
498 lcltime = optarg;
499 else {
500 (void) fprintf(stderr,
501 _("%s: More than one -l option specified\n"),
502 progname);
503 (void) exit(EXIT_FAILURE);
505 break;
506 case 'p':
507 if (psxrules == NULL)
508 psxrules = optarg;
509 else {
510 (void) fprintf(stderr,
511 _("%s: More than one -p option specified\n"),
512 progname);
513 (void) exit(EXIT_FAILURE);
515 break;
516 case 'y':
517 if (yitcommand == NULL)
518 yitcommand = optarg;
519 else {
520 (void) fprintf(stderr,
521 _("%s: More than one -y option specified\n"),
522 progname);
523 (void) exit(EXIT_FAILURE);
525 break;
526 case 'L':
527 if (leapsec == NULL)
528 leapsec = optarg;
529 else {
530 (void) fprintf(stderr,
531 _("%s: More than one -L option specified\n"),
532 progname);
533 (void) exit(EXIT_FAILURE);
535 break;
536 case 'v':
537 noise = TRUE;
538 break;
539 case 's':
540 sflag = TRUE;
541 break;
543 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
544 usage(); /* usage message by request */
545 if (directory == NULL)
546 directory = TZDIR;
547 if (yitcommand == NULL)
548 yitcommand = "yearistype";
550 setboundaries();
552 if (optind < argc && leapsec != NULL) {
553 infile(leapsec);
554 adjleap();
557 for (i = optind; i < argc; ++i)
558 infile(argv[i]);
559 if (errors)
560 (void) exit(EXIT_FAILURE);
561 associate();
562 for (i = 0; i < nzones; i = j) {
564 ** Find the next non-continuation zone entry.
566 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
567 continue;
568 outzone(&zones[i], j - i);
571 ** Make links.
573 for (i = 0; i < nlinks; ++i) {
574 eat(links[i].l_filename, links[i].l_linenum);
575 dolink(links[i].l_from, links[i].l_to);
577 if (lcltime != NULL) {
578 eat("command line", 1);
579 dolink(lcltime, TZDEFAULT);
581 if (psxrules != NULL) {
582 eat("command line", 1);
583 dolink(psxrules, TZDEFRULES);
585 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
588 static void
589 dolink(fromfile, tofile)
590 const char * const fromfile;
591 const char * const tofile;
593 register char * fromname;
594 register char * toname;
596 if (fromfile[0] == '/')
597 fromname = ecpyalloc(fromfile);
598 else {
599 fromname = ecpyalloc(directory);
600 fromname = ecatalloc(fromname, "/");
601 fromname = ecatalloc(fromname, fromfile);
603 if (tofile[0] == '/')
604 toname = ecpyalloc(tofile);
605 else {
606 toname = ecpyalloc(directory);
607 toname = ecatalloc(toname, "/");
608 toname = ecatalloc(toname, tofile);
611 ** We get to be careful here since
612 ** there's a fair chance of root running us.
614 if (!itsdir(toname))
615 (void) remove(toname);
616 if (link(fromname, toname) != 0) {
617 int result;
619 if (mkdirs(toname) != 0)
620 (void) exit(EXIT_FAILURE);
622 result = link(fromname, toname);
623 #if (HAVE_SYMLINK - 0)
624 if (result != 0 &&
625 access(fromname, F_OK) == 0 &&
626 !itsdir(fromname)) {
627 const char *s = tofile;
628 register char * symlinkcontents = NULL;
629 while ((s = strchr(s+1, '/')) != NULL)
630 symlinkcontents = ecatalloc(symlinkcontents, "../");
631 symlinkcontents = ecatalloc(symlinkcontents, fromname);
633 result = unlink(toname);
634 if (result != 0 && errno != ENOENT) {
635 const char *e = strerror(errno);
637 (void) fprintf(stderr,
638 _("%s: Can't unlink %s: %s\n"),
639 progname, toname, e);
640 (void) exit(EXIT_FAILURE);
643 result = symlink(symlinkcontents, toname);
644 if (result == 0)
645 warning(_("hard link failed, symbolic link used"));
646 ifree(symlinkcontents);
648 #endif
649 if (result != 0) {
650 const char *e = strerror(errno);
652 (void) fprintf(stderr,
653 _("%s: Can't link from %s to %s: %s\n"),
654 progname, fromname, toname, e);
655 (void) exit(EXIT_FAILURE);
658 ifree(fromname);
659 ifree(toname);
662 #ifndef INT_MAX
663 #define INT_MAX ((int) (((unsigned)~0)>>1))
664 #endif /* !defined INT_MAX */
666 #ifndef INT_MIN
667 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
668 #endif /* !defined INT_MIN */
671 ** The tz file format currently allows at most 32-bit quantities.
672 ** This restriction should be removed before signed 32-bit values
673 ** wrap around in 2038, but unfortunately this will require a
674 ** change to the tz file format.
677 #define MAX_BITS_IN_FILE 32
678 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(time_t) < MAX_BITS_IN_FILE) ? TYPE_BIT(time_t) : MAX_BITS_IN_FILE)
680 static void
681 setboundaries P((void))
683 if (TYPE_SIGNED(time_t)) {
684 min_time = ~ (time_t) 0;
685 min_time <<= TIME_T_BITS_IN_FILE - 1;
686 max_time = ~ (time_t) 0 - min_time;
687 if (sflag)
688 min_time = 0;
689 } else {
690 min_time = 0;
691 max_time = 2 - sflag;
692 max_time <<= TIME_T_BITS_IN_FILE - 1;
693 --max_time;
695 min_year = TM_YEAR_BASE + gmtime(&min_time)->tm_year;
696 max_year = TM_YEAR_BASE + gmtime(&max_time)->tm_year;
697 min_year_representable = min_year;
698 max_year_representable = max_year;
701 static int
702 itsdir(name)
703 const char * const name;
705 register char * myname;
706 register int accres;
708 myname = ecpyalloc(name);
709 myname = ecatalloc(myname, "/.");
710 accres = access(myname, F_OK);
711 ifree(myname);
712 return accres == 0;
716 ** Associate sets of rules with zones.
720 ** Sort by rule name.
723 static int
724 rcomp(cp1, cp2)
725 const void * cp1;
726 const void * cp2;
728 return strcmp(((const struct rule *) cp1)->r_name,
729 ((const struct rule *) cp2)->r_name);
732 static void
733 associate P((void))
735 register struct zone * zp;
736 register struct rule * rp;
737 register int base, out;
738 register int i, j;
740 if (nrules != 0) {
741 (void) qsort((void *) rules, (size_t) nrules,
742 (size_t) sizeof *rules, rcomp);
743 for (i = 0; i < nrules - 1; ++i) {
744 if (strcmp(rules[i].r_name,
745 rules[i + 1].r_name) != 0)
746 continue;
747 if (strcmp(rules[i].r_filename,
748 rules[i + 1].r_filename) == 0)
749 continue;
750 eat(rules[i].r_filename, rules[i].r_linenum);
751 warning(_("same rule name in multiple files"));
752 eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
753 warning(_("same rule name in multiple files"));
754 for (j = i + 2; j < nrules; ++j) {
755 if (strcmp(rules[i].r_name,
756 rules[j].r_name) != 0)
757 break;
758 if (strcmp(rules[i].r_filename,
759 rules[j].r_filename) == 0)
760 continue;
761 if (strcmp(rules[i + 1].r_filename,
762 rules[j].r_filename) == 0)
763 continue;
764 break;
766 i = j - 1;
769 for (i = 0; i < nzones; ++i) {
770 zp = &zones[i];
771 zp->z_rules = NULL;
772 zp->z_nrules = 0;
774 for (base = 0; base < nrules; base = out) {
775 rp = &rules[base];
776 for (out = base + 1; out < nrules; ++out)
777 if (strcmp(rp->r_name, rules[out].r_name) != 0)
778 break;
779 for (i = 0; i < nzones; ++i) {
780 zp = &zones[i];
781 if (strcmp(zp->z_rule, rp->r_name) != 0)
782 continue;
783 zp->z_rules = rp;
784 zp->z_nrules = out - base;
787 for (i = 0; i < nzones; ++i) {
788 zp = &zones[i];
789 if (zp->z_nrules == 0) {
791 ** Maybe we have a local standard time offset.
793 eat(zp->z_filename, zp->z_linenum);
794 zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
795 TRUE);
797 ** Note, though, that if there's no rule,
798 ** a '%s' in the format is a bad thing.
800 if (strchr(zp->z_format, '%') != 0)
801 error(_("%s in ruleless zone"));
804 if (errors)
805 (void) exit(EXIT_FAILURE);
808 static void
809 infile(name)
810 const char * name;
812 register FILE * fp;
813 register char ** fields;
814 register char * cp;
815 register const struct lookup * lp;
816 register int nfields;
817 register int wantcont;
818 register int num;
819 char buf[BUFSIZ];
821 if (strcmp(name, "-") == 0) {
822 name = _("standard input");
823 fp = stdin;
824 } else if ((fp = fopen(name, "r")) == NULL) {
825 const char *e = strerror(errno);
827 (void) fprintf(stderr, _("%s: Can't open %s: %s\n"),
828 progname, name, e);
829 (void) exit(EXIT_FAILURE);
831 wantcont = FALSE;
832 for (num = 1; ; ++num) {
833 eat(name, num);
834 if (fgets(buf, (int) sizeof buf, fp) != buf)
835 break;
836 cp = strchr(buf, '\n');
837 if (cp == NULL) {
838 error(_("line too long"));
839 (void) exit(EXIT_FAILURE);
841 *cp = '\0';
842 fields = getfields(buf);
843 nfields = 0;
844 while (fields[nfields] != NULL) {
845 static char nada;
847 if (strcmp(fields[nfields], "-") == 0)
848 fields[nfields] = &nada;
849 ++nfields;
851 if (nfields == 0) {
852 /* nothing to do */
853 } else if (wantcont) {
854 wantcont = inzcont(fields, nfields);
855 } else {
856 lp = byword(fields[0], line_codes);
857 if (lp == NULL)
858 error(_("input line of unknown type"));
859 else switch ((int) (lp->l_value)) {
860 case LC_RULE:
861 inrule(fields, nfields);
862 wantcont = FALSE;
863 break;
864 case LC_ZONE:
865 wantcont = inzone(fields, nfields);
866 break;
867 case LC_LINK:
868 inlink(fields, nfields);
869 wantcont = FALSE;
870 break;
871 case LC_LEAP:
872 if (name != leapsec)
873 (void) fprintf(stderr,
874 _("%s: Leap line in non leap seconds file %s\n"),
875 progname, name);
876 else inleap(fields, nfields);
877 wantcont = FALSE;
878 break;
879 default: /* "cannot happen" */
880 (void) fprintf(stderr,
881 _("%s: panic: Invalid l_value %d\n"),
882 progname, lp->l_value);
883 (void) exit(EXIT_FAILURE);
886 ifree((char *) fields);
888 if (ferror(fp)) {
889 (void) fprintf(stderr, _("%s: Error reading %s\n"),
890 progname, filename);
891 (void) exit(EXIT_FAILURE);
893 if (fp != stdin && fclose(fp)) {
894 const char *e = strerror(errno);
896 (void) fprintf(stderr, _("%s: Error closing %s: %s\n"),
897 progname, filename, e);
898 (void) exit(EXIT_FAILURE);
900 if (wantcont)
901 error(_("expected continuation line not found"));
905 ** Convert a string of one of the forms
906 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
907 ** into a number of seconds.
908 ** A null string maps to zero.
909 ** Call error with errstring and return zero on errors.
912 static long
913 gethms(string, errstring, signable)
914 const char * string;
915 const char * const errstring;
916 const int signable;
918 int hh, mm, ss, sign;
920 if (string == NULL || *string == '\0')
921 return 0;
922 if (!signable)
923 sign = 1;
924 else if (*string == '-') {
925 sign = -1;
926 ++string;
927 } else sign = 1;
928 if (sscanf(string, scheck(string, "%d"), &hh) == 1)
929 mm = ss = 0;
930 else if (sscanf(string, scheck(string, "%d:%d"), &hh, &mm) == 2)
931 ss = 0;
932 else if (sscanf(string, scheck(string, "%d:%d:%d"),
933 &hh, &mm, &ss) != 3) {
934 error(errstring);
935 return 0;
937 if ((hh < 0 || hh >= HOURSPERDAY ||
938 mm < 0 || mm >= MINSPERHOUR ||
939 ss < 0 || ss > SECSPERMIN) &&
940 !(hh == HOURSPERDAY && mm == 0 && ss == 0)) {
941 error(errstring);
942 return 0;
944 return eitol(sign) *
945 (eitol(hh * MINSPERHOUR + mm) *
946 eitol(SECSPERMIN) + eitol(ss));
949 static void
950 inrule(fields, nfields)
951 register char ** const fields;
952 const int nfields;
954 static struct rule r;
956 if (nfields != RULE_FIELDS) {
957 error(_("wrong number of fields on Rule line"));
958 return;
960 if (*fields[RF_NAME] == '\0') {
961 error(_("nameless rule"));
962 return;
964 r.r_filename = filename;
965 r.r_linenum = linenum;
966 r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
967 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
968 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
969 r.r_name = ecpyalloc(fields[RF_NAME]);
970 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
971 rules = (struct rule *) (void *) erealloc((char *) rules,
972 (int) ((nrules + 1) * sizeof *rules));
973 rules[nrules++] = r;
976 static int
977 inzone(fields, nfields)
978 register char ** const fields;
979 const int nfields;
981 register int i;
982 static char * buf;
984 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
985 error(_("wrong number of fields on Zone line"));
986 return FALSE;
988 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
989 buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
990 (void) sprintf(buf,
991 _("\"Zone %s\" line and -l option are mutually exclusive"),
992 TZDEFAULT);
993 error(buf);
994 return FALSE;
996 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
997 buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
998 (void) sprintf(buf,
999 _("\"Zone %s\" line and -p option are mutually exclusive"),
1000 TZDEFRULES);
1001 error(buf);
1002 return FALSE;
1004 for (i = 0; i < nzones; ++i)
1005 if (zones[i].z_name != NULL &&
1006 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
1007 buf = erealloc(buf, (int) (132 +
1008 strlen(fields[ZF_NAME]) +
1009 strlen(zones[i].z_filename)));
1010 (void) sprintf(buf,
1011 _("duplicate zone name %s (file \"%s\", line %d)"),
1012 fields[ZF_NAME],
1013 zones[i].z_filename,
1014 zones[i].z_linenum);
1015 error(buf);
1016 return FALSE;
1018 return inzsub(fields, nfields, FALSE);
1021 static int
1022 inzcont(fields, nfields)
1023 register char ** const fields;
1024 const int nfields;
1026 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
1027 error(_("wrong number of fields on Zone continuation line"));
1028 return FALSE;
1030 return inzsub(fields, nfields, TRUE);
1033 static int
1034 inzsub(fields, nfields, iscont)
1035 register char ** const fields;
1036 const int nfields;
1037 const int iscont;
1039 register char * cp;
1040 static struct zone z;
1041 register int i_gmtoff, i_rule, i_format;
1042 register int i_untilyear, i_untilmonth;
1043 register int i_untilday, i_untiltime;
1044 register int hasuntil;
1046 if (iscont) {
1047 i_gmtoff = ZFC_GMTOFF;
1048 i_rule = ZFC_RULE;
1049 i_format = ZFC_FORMAT;
1050 i_untilyear = ZFC_TILYEAR;
1051 i_untilmonth = ZFC_TILMONTH;
1052 i_untilday = ZFC_TILDAY;
1053 i_untiltime = ZFC_TILTIME;
1054 z.z_name = NULL;
1055 } else {
1056 i_gmtoff = ZF_GMTOFF;
1057 i_rule = ZF_RULE;
1058 i_format = ZF_FORMAT;
1059 i_untilyear = ZF_TILYEAR;
1060 i_untilmonth = ZF_TILMONTH;
1061 i_untilday = ZF_TILDAY;
1062 i_untiltime = ZF_TILTIME;
1063 z.z_name = ecpyalloc(fields[ZF_NAME]);
1065 z.z_filename = filename;
1066 z.z_linenum = linenum;
1067 z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UTC offset"), TRUE);
1068 if ((cp = strchr(fields[i_format], '%')) != 0) {
1069 if (*++cp != 's' || strchr(cp, '%') != 0) {
1070 error(_("invalid abbreviation format"));
1071 return FALSE;
1074 z.z_rule = ecpyalloc(fields[i_rule]);
1075 z.z_format = ecpyalloc(fields[i_format]);
1076 hasuntil = nfields > i_untilyear;
1077 if (hasuntil) {
1078 z.z_untilrule.r_filename = filename;
1079 z.z_untilrule.r_linenum = linenum;
1080 rulesub(&z.z_untilrule,
1081 fields[i_untilyear],
1082 "only",
1084 (nfields > i_untilmonth) ?
1085 fields[i_untilmonth] : "Jan",
1086 (nfields > i_untilday) ? fields[i_untilday] : "1",
1087 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1088 z.z_untiltime = rpytime(&z.z_untilrule,
1089 z.z_untilrule.r_loyear);
1090 if (iscont && nzones > 0 &&
1091 z.z_untiltime > min_time &&
1092 z.z_untiltime < max_time &&
1093 zones[nzones - 1].z_untiltime > min_time &&
1094 zones[nzones - 1].z_untiltime < max_time &&
1095 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1096 error(_("Zone continuation line end time is not after end time of previous line"));
1097 return FALSE;
1100 zones = (struct zone *) (void *) erealloc((char *) zones,
1101 (int) ((nzones + 1) * sizeof *zones));
1102 zones[nzones++] = z;
1104 ** If there was an UNTIL field on this line,
1105 ** there's more information about the zone on the next line.
1107 return hasuntil;
1110 static void
1111 inleap(fields, nfields)
1112 register char ** const fields;
1113 const int nfields;
1115 register const char * cp;
1116 register const struct lookup * lp;
1117 register int i, j;
1118 int year, month, day;
1119 long dayoff, tod;
1120 time_t t;
1122 if (nfields != LEAP_FIELDS) {
1123 error(_("wrong number of fields on Leap line"));
1124 return;
1126 dayoff = 0;
1127 cp = fields[LP_YEAR];
1128 if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
1130 * Leapin' Lizards!
1132 error(_("invalid leaping year"));
1133 return;
1135 j = EPOCH_YEAR;
1136 while (j != year) {
1137 if (year > j) {
1138 i = len_years[isleap(j)];
1139 ++j;
1140 } else {
1141 --j;
1142 i = -len_years[isleap(j)];
1144 dayoff = oadd(dayoff, eitol(i));
1146 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1147 error(_("invalid month name"));
1148 return;
1150 month = lp->l_value;
1151 j = TM_JANUARY;
1152 while (j != month) {
1153 i = len_months[isleap(year)][j];
1154 dayoff = oadd(dayoff, eitol(i));
1155 ++j;
1157 cp = fields[LP_DAY];
1158 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1159 day <= 0 || day > len_months[isleap(year)][month]) {
1160 error(_("invalid day of month"));
1161 return;
1163 dayoff = oadd(dayoff, eitol(day - 1));
1164 if (dayoff < 0 && !TYPE_SIGNED(time_t)) {
1165 error(_("time before zero"));
1166 return;
1168 t = (time_t) dayoff * SECSPERDAY;
1170 ** Cheap overflow check.
1172 if (t / SECSPERDAY != dayoff) {
1173 error(_("time overflow"));
1174 return;
1176 tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1177 cp = fields[LP_CORR];
1179 register int positive;
1180 int count;
1182 if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1183 positive = FALSE;
1184 count = 1;
1185 } else if (strcmp(cp, "--") == 0) {
1186 positive = FALSE;
1187 count = 2;
1188 } else if (strcmp(cp, "+") == 0) {
1189 positive = TRUE;
1190 count = 1;
1191 } else if (strcmp(cp, "++") == 0) {
1192 positive = TRUE;
1193 count = 2;
1194 } else {
1195 error(_("illegal CORRECTION field on Leap line"));
1196 return;
1198 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1199 error(_("illegal Rolling/Stationary field on Leap line"));
1200 return;
1202 leapadd(tadd(t, tod), positive, lp->l_value, count);
1206 static void
1207 inlink(fields, nfields)
1208 register char ** const fields;
1209 const int nfields;
1211 struct link l;
1213 if (nfields != LINK_FIELDS) {
1214 error(_("wrong number of fields on Link line"));
1215 return;
1217 if (*fields[LF_FROM] == '\0') {
1218 error(_("blank FROM field on Link line"));
1219 return;
1221 if (*fields[LF_TO] == '\0') {
1222 error(_("blank TO field on Link line"));
1223 return;
1225 l.l_filename = filename;
1226 l.l_linenum = linenum;
1227 l.l_from = ecpyalloc(fields[LF_FROM]);
1228 l.l_to = ecpyalloc(fields[LF_TO]);
1229 links = (struct link *) (void *) erealloc((char *) links,
1230 (int) ((nlinks + 1) * sizeof *links));
1231 links[nlinks++] = l;
1234 static void
1235 rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
1236 register struct rule * const rp;
1237 const char * const loyearp;
1238 const char * const hiyearp;
1239 const char * const typep;
1240 const char * const monthp;
1241 const char * const dayp;
1242 const char * const timep;
1244 register const struct lookup * lp;
1245 register const char * cp;
1246 register char * dp;
1247 register char * ep;
1249 if ((lp = byword(monthp, mon_names)) == NULL) {
1250 error(_("invalid month name"));
1251 return;
1253 rp->r_month = lp->l_value;
1254 rp->r_todisstd = FALSE;
1255 rp->r_todisgmt = FALSE;
1256 dp = ecpyalloc(timep);
1257 if (*dp != '\0') {
1258 ep = dp + strlen(dp) - 1;
1259 switch (lowerit(*ep)) {
1260 case 's': /* Standard */
1261 rp->r_todisstd = TRUE;
1262 rp->r_todisgmt = FALSE;
1263 *ep = '\0';
1264 break;
1265 case 'w': /* Wall */
1266 rp->r_todisstd = FALSE;
1267 rp->r_todisgmt = FALSE;
1268 *ep = '\0';
1269 break;
1270 case 'g': /* Greenwich */
1271 case 'u': /* Universal */
1272 case 'z': /* Zulu */
1273 rp->r_todisstd = TRUE;
1274 rp->r_todisgmt = TRUE;
1275 *ep = '\0';
1276 break;
1279 rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1280 ifree(dp);
1282 ** Year work.
1284 cp = loyearp;
1285 lp = byword(cp, begin_years);
1286 if (lp != NULL) switch ((int) lp->l_value) {
1287 case YR_MINIMUM:
1288 rp->r_loyear = INT_MIN;
1289 break;
1290 case YR_MAXIMUM:
1291 rp->r_loyear = INT_MAX;
1292 break;
1293 default: /* "cannot happen" */
1294 (void) fprintf(stderr,
1295 _("%s: panic: Invalid l_value %d\n"),
1296 progname, lp->l_value);
1297 (void) exit(EXIT_FAILURE);
1298 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
1299 error(_("invalid starting year"));
1300 return;
1301 } else if (noise) {
1302 if (rp->r_loyear < min_year_representable)
1303 warning(_("starting year too low to be represented"));
1304 else if (rp->r_loyear > max_year_representable)
1305 warning(_("starting year too high to be represented"));
1307 cp = hiyearp;
1308 if ((lp = byword(cp, end_years)) != NULL) switch ((int) lp->l_value) {
1309 case YR_MINIMUM:
1310 rp->r_hiyear = INT_MIN;
1311 break;
1312 case YR_MAXIMUM:
1313 rp->r_hiyear = INT_MAX;
1314 break;
1315 case YR_ONLY:
1316 rp->r_hiyear = rp->r_loyear;
1317 break;
1318 default: /* "cannot happen" */
1319 (void) fprintf(stderr,
1320 _("%s: panic: Invalid l_value %d\n"),
1321 progname, lp->l_value);
1322 (void) exit(EXIT_FAILURE);
1323 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
1324 error(_("invalid ending year"));
1325 return;
1326 } else if (noise) {
1327 if (rp->r_loyear < min_year_representable)
1328 warning(_("starting year too low to be represented"));
1329 else if (rp->r_loyear > max_year_representable)
1330 warning(_("starting year too high to be represented"));
1332 if (rp->r_loyear > rp->r_hiyear) {
1333 error(_("starting year greater than ending year"));
1334 return;
1336 if (*typep == '\0')
1337 rp->r_yrtype = NULL;
1338 else {
1339 if (rp->r_loyear == rp->r_hiyear) {
1340 error(_("typed single year"));
1341 return;
1343 rp->r_yrtype = ecpyalloc(typep);
1345 if (rp->r_loyear < min_year && rp->r_loyear > 0)
1346 min_year = rp->r_loyear;
1348 ** Day work.
1349 ** Accept things such as:
1350 ** 1
1351 ** last-Sunday
1352 ** Sun<=20
1353 ** Sun>=7
1355 dp = ecpyalloc(dayp);
1356 if ((lp = byword(dp, lasts)) != NULL) {
1357 rp->r_dycode = DC_DOWLEQ;
1358 rp->r_wday = lp->l_value;
1359 rp->r_dayofmonth = len_months[1][rp->r_month];
1360 } else {
1361 if ((ep = strchr(dp, '<')) != 0)
1362 rp->r_dycode = DC_DOWLEQ;
1363 else if ((ep = strchr(dp, '>')) != 0)
1364 rp->r_dycode = DC_DOWGEQ;
1365 else {
1366 ep = dp;
1367 rp->r_dycode = DC_DOM;
1369 if (rp->r_dycode != DC_DOM) {
1370 *ep++ = 0;
1371 if (*ep++ != '=') {
1372 error(_("invalid day of month"));
1373 ifree(dp);
1374 return;
1376 if ((lp = byword(dp, wday_names)) == NULL) {
1377 error(_("invalid weekday name"));
1378 ifree(dp);
1379 return;
1381 rp->r_wday = lp->l_value;
1383 if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1384 rp->r_dayofmonth <= 0 ||
1385 (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1386 error(_("invalid day of month"));
1387 ifree(dp);
1388 return;
1391 ifree(dp);
1394 static void
1395 convert(val, buf)
1396 const long val;
1397 char * const buf;
1399 register int i;
1400 register long shift;
1402 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1403 buf[i] = val >> shift;
1406 static void
1407 puttzcode(val, fp)
1408 const long val;
1409 FILE * const fp;
1411 char buf[4];
1413 convert(val, buf);
1414 (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1417 static int
1418 atcomp(avp, bvp)
1419 void * avp;
1420 void * bvp;
1422 if (((struct attype *) avp)->at < ((struct attype *) bvp)->at)
1423 return -1;
1424 else if (((struct attype *) avp)->at > ((struct attype *) bvp)->at)
1425 return 1;
1426 else return 0;
1429 static void
1430 writezone(name)
1431 const char * const name;
1433 register FILE * fp;
1434 register int i, j;
1435 static char * fullname;
1436 static struct tzhead tzh;
1437 time_t ats[TZ_MAX_TIMES];
1438 unsigned char types[TZ_MAX_TIMES];
1441 ** Sort.
1443 if (timecnt > 1)
1444 (void) qsort((void *) attypes, (size_t) timecnt,
1445 (size_t) sizeof *attypes, atcomp);
1447 ** Optimize.
1450 int fromi;
1451 int toi;
1453 toi = 0;
1454 fromi = 0;
1455 while (fromi < timecnt && attypes[fromi].at < min_time)
1456 ++fromi;
1457 if (isdsts[0] == 0)
1458 while (fromi < timecnt && attypes[fromi].type == 0)
1459 ++fromi; /* handled by default rule */
1460 for ( ; fromi < timecnt; ++fromi) {
1461 if (toi != 0
1462 && ((attypes[fromi].at
1463 + gmtoffs[attypes[toi - 1].type])
1464 <= (attypes[toi - 1].at
1465 + gmtoffs[toi == 1 ? 0
1466 : attypes[toi - 2].type]))) {
1467 attypes[toi - 1].type = attypes[fromi].type;
1468 continue;
1470 if (toi == 0 ||
1471 attypes[toi - 1].type != attypes[fromi].type)
1472 attypes[toi++] = attypes[fromi];
1474 timecnt = toi;
1477 ** Transfer.
1479 for (i = 0; i < timecnt; ++i) {
1480 ats[i] = attypes[i].at;
1481 types[i] = attypes[i].type;
1483 fullname = erealloc(fullname,
1484 (int) (strlen(directory) + 1 + strlen(name) + 1));
1485 (void) sprintf(fullname, "%s/%s", directory, name);
1487 ** Remove old file, if any, to snap links.
1489 if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT) {
1490 const char *e = strerror(errno);
1492 (void) fprintf(stderr, _("%s: Can't remove %s: %s\n"),
1493 progname, fullname, e);
1494 (void) exit(EXIT_FAILURE);
1496 if ((fp = fopen(fullname, "wb")) == NULL) {
1497 if (mkdirs(fullname) != 0)
1498 (void) exit(EXIT_FAILURE);
1499 if ((fp = fopen(fullname, "wb")) == NULL) {
1500 const char *e = strerror(errno);
1502 (void) fprintf(stderr, _("%s: Can't create %s: %s\n"),
1503 progname, fullname, e);
1504 (void) exit(EXIT_FAILURE);
1507 convert(eitol(typecnt), tzh.tzh_ttisgmtcnt);
1508 convert(eitol(typecnt), tzh.tzh_ttisstdcnt);
1509 convert(eitol(leapcnt), tzh.tzh_leapcnt);
1510 convert(eitol(timecnt), tzh.tzh_timecnt);
1511 convert(eitol(typecnt), tzh.tzh_typecnt);
1512 convert(eitol(charcnt), tzh.tzh_charcnt);
1513 (void) strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1514 #define DO(field) (void) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp)
1515 DO(tzh_magic);
1516 DO(tzh_reserved);
1517 DO(tzh_ttisgmtcnt);
1518 DO(tzh_ttisstdcnt);
1519 DO(tzh_leapcnt);
1520 DO(tzh_timecnt);
1521 DO(tzh_typecnt);
1522 DO(tzh_charcnt);
1523 #undef DO
1524 for (i = 0; i < timecnt; ++i) {
1525 j = leapcnt;
1526 while (--j >= 0)
1527 if (ats[i] >= trans[j]) {
1528 ats[i] = tadd(ats[i], corr[j]);
1529 break;
1531 puttzcode((long) ats[i], fp);
1533 if (timecnt > 0)
1534 (void) fwrite((void *) types, (size_t) sizeof types[0],
1535 (size_t) timecnt, fp);
1536 for (i = 0; i < typecnt; ++i) {
1537 puttzcode((long) gmtoffs[i], fp);
1538 (void) putc(isdsts[i], fp);
1539 (void) putc(abbrinds[i], fp);
1541 if (charcnt != 0)
1542 (void) fwrite((void *) chars, (size_t) sizeof chars[0],
1543 (size_t) charcnt, fp);
1544 for (i = 0; i < leapcnt; ++i) {
1545 if (roll[i]) {
1546 if (timecnt == 0 || trans[i] < ats[0]) {
1547 j = 0;
1548 while (isdsts[j])
1549 if (++j >= typecnt) {
1550 j = 0;
1551 break;
1553 } else {
1554 j = 1;
1555 while (j < timecnt && trans[i] >= ats[j])
1556 ++j;
1557 j = types[j - 1];
1559 puttzcode((long) tadd(trans[i], -gmtoffs[j]), fp);
1560 } else puttzcode((long) trans[i], fp);
1561 puttzcode((long) corr[i], fp);
1563 for (i = 0; i < typecnt; ++i)
1564 (void) putc(ttisstds[i], fp);
1565 for (i = 0; i < typecnt; ++i)
1566 (void) putc(ttisgmts[i], fp);
1567 if (ferror(fp) || fclose(fp)) {
1568 (void) fprintf(stderr, _("%s: Error writing %s\n"),
1569 progname, fullname);
1570 (void) exit(EXIT_FAILURE);
1574 static void
1575 doabbr(abbr, format, letters, isdst)
1576 char * const abbr;
1577 const char * const format;
1578 const char * const letters;
1579 const int isdst;
1581 if (strchr(format, '/') == NULL) {
1582 if (letters == NULL)
1583 (void) strcpy(abbr, format);
1584 else (void) sprintf(abbr, format, letters);
1585 } else if (isdst)
1586 (void) strcpy(abbr, strchr(format, '/') + 1);
1587 else {
1588 (void) strcpy(abbr, format);
1589 *strchr(abbr, '/') = '\0';
1593 static void
1594 outzone(zpfirst, zonecount)
1595 const struct zone * const zpfirst;
1596 const int zonecount;
1598 register const struct zone * zp;
1599 register struct rule * rp;
1600 register int i, j;
1601 register int usestart, useuntil;
1602 register time_t starttime, untiltime;
1603 register long gmtoff;
1604 register long stdoff;
1605 register int year;
1606 register long startoff;
1607 register int startttisstd;
1608 register int startttisgmt;
1609 register int type;
1610 char startbuf[BUFSIZ];
1612 INITIALIZE(untiltime);
1613 INITIALIZE(starttime);
1615 ** Now. . .finally. . .generate some useful data!
1617 timecnt = 0;
1618 typecnt = 0;
1619 charcnt = 0;
1621 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1622 ** for noting the need to unconditionally initialize startttisstd.
1624 startttisstd = FALSE;
1625 startttisgmt = FALSE;
1626 for (i = 0; i < zonecount; ++i) {
1628 ** A guess that may well be corrected later.
1630 stdoff = 0;
1631 zp = &zpfirst[i];
1632 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
1633 useuntil = i < (zonecount - 1);
1634 if (useuntil && zp->z_untiltime <= min_time)
1635 continue;
1636 gmtoff = zp->z_gmtoff;
1637 eat(zp->z_filename, zp->z_linenum);
1638 *startbuf = '\0';
1639 startoff = zp->z_gmtoff;
1640 if (zp->z_nrules == 0) {
1641 stdoff = zp->z_stdoff;
1642 doabbr(startbuf, zp->z_format,
1643 (char *) NULL, stdoff != 0);
1644 type = addtype(oadd(zp->z_gmtoff, stdoff),
1645 startbuf, stdoff != 0, startttisstd,
1646 startttisgmt);
1647 if (usestart) {
1648 addtt(starttime, type);
1649 usestart = FALSE;
1650 } else if (stdoff != 0)
1651 addtt(min_time, type);
1652 } else for (year = min_year; year <= max_year; ++year) {
1653 if (useuntil && year > zp->z_untilrule.r_hiyear)
1654 break;
1656 ** Mark which rules to do in the current year.
1657 ** For those to do, calculate rpytime(rp, year);
1659 for (j = 0; j < zp->z_nrules; ++j) {
1660 rp = &zp->z_rules[j];
1661 eats(zp->z_filename, zp->z_linenum,
1662 rp->r_filename, rp->r_linenum);
1663 rp->r_todo = year >= rp->r_loyear &&
1664 year <= rp->r_hiyear &&
1665 yearistype(year, rp->r_yrtype);
1666 if (rp->r_todo)
1667 rp->r_temp = rpytime(rp, year);
1669 for ( ; ; ) {
1670 register int k;
1671 register time_t jtime, ktime;
1672 register long offset;
1673 char buf[BUFSIZ];
1675 INITIALIZE(ktime);
1676 if (useuntil) {
1678 ** Turn untiltime into UTC
1679 ** assuming the current gmtoff and
1680 ** stdoff values.
1682 untiltime = zp->z_untiltime;
1683 if (!zp->z_untilrule.r_todisgmt)
1684 untiltime = tadd(untiltime,
1685 -gmtoff);
1686 if (!zp->z_untilrule.r_todisstd)
1687 untiltime = tadd(untiltime,
1688 -stdoff);
1691 ** Find the rule (of those to do, if any)
1692 ** that takes effect earliest in the year.
1694 k = -1;
1695 for (j = 0; j < zp->z_nrules; ++j) {
1696 rp = &zp->z_rules[j];
1697 if (!rp->r_todo)
1698 continue;
1699 eats(zp->z_filename, zp->z_linenum,
1700 rp->r_filename, rp->r_linenum);
1701 offset = rp->r_todisgmt ? 0 : gmtoff;
1702 if (!rp->r_todisstd)
1703 offset = oadd(offset, stdoff);
1704 jtime = rp->r_temp;
1705 if (jtime == min_time ||
1706 jtime == max_time)
1707 continue;
1708 jtime = tadd(jtime, -offset);
1709 if (k < 0 || jtime < ktime) {
1710 k = j;
1711 ktime = jtime;
1714 if (k < 0)
1715 break; /* go on to next year */
1716 rp = &zp->z_rules[k];
1717 rp->r_todo = FALSE;
1718 if (useuntil && ktime >= untiltime)
1719 break;
1720 stdoff = rp->r_stdoff;
1721 if (usestart && ktime == starttime)
1722 usestart = FALSE;
1723 if (usestart) {
1724 if (ktime < starttime) {
1725 startoff = oadd(zp->z_gmtoff,
1726 stdoff);
1727 doabbr(startbuf, zp->z_format,
1728 rp->r_abbrvar,
1729 rp->r_stdoff != 0);
1730 continue;
1732 if (*startbuf == '\0' &&
1733 startoff == oadd(zp->z_gmtoff,
1734 stdoff)) {
1735 doabbr(startbuf, zp->z_format,
1736 rp->r_abbrvar,
1737 rp->r_stdoff != 0);
1740 eats(zp->z_filename, zp->z_linenum,
1741 rp->r_filename, rp->r_linenum);
1742 doabbr(buf, zp->z_format, rp->r_abbrvar,
1743 rp->r_stdoff != 0);
1744 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
1745 type = addtype(offset, buf, rp->r_stdoff != 0,
1746 rp->r_todisstd, rp->r_todisgmt);
1747 addtt(ktime, type);
1750 if (usestart) {
1751 if (*startbuf == '\0' &&
1752 zp->z_format != NULL &&
1753 strchr(zp->z_format, '%') == NULL &&
1754 strchr(zp->z_format, '/') == NULL)
1755 (void) strcpy(startbuf, zp->z_format);
1756 eat(zp->z_filename, zp->z_linenum);
1757 if (*startbuf == '\0')
1758 error(_("can't determine time zone abbreviation to use just after until time"));
1759 else addtt(starttime,
1760 addtype(startoff, startbuf,
1761 startoff != zp->z_gmtoff,
1762 startttisstd,
1763 startttisgmt));
1766 ** Now we may get to set starttime for the next zone line.
1768 if (useuntil) {
1769 startttisstd = zp->z_untilrule.r_todisstd;
1770 startttisgmt = zp->z_untilrule.r_todisgmt;
1771 starttime = zp->z_untiltime;
1772 if (!startttisstd)
1773 starttime = tadd(starttime, -stdoff);
1774 if (!startttisgmt)
1775 starttime = tadd(starttime, -gmtoff);
1778 writezone(zpfirst->z_name);
1781 static void
1782 addtt(starttime, type)
1783 const time_t starttime;
1784 int type;
1786 if (starttime <= min_time ||
1787 (timecnt == 1 && attypes[0].at < min_time)) {
1788 gmtoffs[0] = gmtoffs[type];
1789 isdsts[0] = isdsts[type];
1790 ttisstds[0] = ttisstds[type];
1791 ttisgmts[0] = ttisgmts[type];
1792 if (abbrinds[type] != 0)
1793 (void) strcpy(chars, &chars[abbrinds[type]]);
1794 abbrinds[0] = 0;
1795 charcnt = strlen(chars) + 1;
1796 typecnt = 1;
1797 timecnt = 0;
1798 type = 0;
1800 if (timecnt >= TZ_MAX_TIMES) {
1801 error(_("too many transitions?!"));
1802 (void) exit(EXIT_FAILURE);
1804 attypes[timecnt].at = starttime;
1805 attypes[timecnt].type = type;
1806 ++timecnt;
1809 static int
1810 addtype(gmtoff, abbr, isdst, ttisstd, ttisgmt)
1811 const long gmtoff;
1812 const char * const abbr;
1813 const int isdst;
1814 const int ttisstd;
1815 const int ttisgmt;
1817 register int i, j;
1819 if (isdst != TRUE && isdst != FALSE) {
1820 error(_("internal error - addtype called with bad isdst"));
1821 (void) exit(EXIT_FAILURE);
1823 if (ttisstd != TRUE && ttisstd != FALSE) {
1824 error(_("internal error - addtype called with bad ttisstd"));
1825 (void) exit(EXIT_FAILURE);
1827 if (ttisgmt != TRUE && ttisgmt != FALSE) {
1828 error(_("internal error - addtype called with bad ttisgmt"));
1829 (void) exit(EXIT_FAILURE);
1832 ** See if there's already an entry for this zone type.
1833 ** If so, just return its index.
1835 for (i = 0; i < typecnt; ++i) {
1836 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
1837 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
1838 ttisstd == ttisstds[i] &&
1839 ttisgmt == ttisgmts[i])
1840 return i;
1843 ** There isn't one; add a new one, unless there are already too
1844 ** many.
1846 if (typecnt >= TZ_MAX_TYPES) {
1847 error(_("too many local time types"));
1848 (void) exit(EXIT_FAILURE);
1850 gmtoffs[i] = gmtoff;
1851 isdsts[i] = isdst;
1852 ttisstds[i] = ttisstd;
1853 ttisgmts[i] = ttisgmt;
1855 for (j = 0; j < charcnt; ++j)
1856 if (strcmp(&chars[j], abbr) == 0)
1857 break;
1858 if (j == charcnt)
1859 newabbr(abbr);
1860 abbrinds[i] = j;
1861 ++typecnt;
1862 return i;
1865 static void
1866 leapadd(t, positive, rolling, count)
1867 const time_t t;
1868 const int positive;
1869 const int rolling;
1870 int count;
1872 register int i, j;
1874 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
1875 error(_("too many leap seconds"));
1876 (void) exit(EXIT_FAILURE);
1878 for (i = 0; i < leapcnt; ++i)
1879 if (t <= trans[i]) {
1880 if (t == trans[i]) {
1881 error(_("repeated leap second moment"));
1882 (void) exit(EXIT_FAILURE);
1884 break;
1886 do {
1887 for (j = leapcnt; j > i; --j) {
1888 trans[j] = trans[j - 1];
1889 corr[j] = corr[j - 1];
1890 roll[j] = roll[j - 1];
1892 trans[i] = t;
1893 corr[i] = positive ? 1L : eitol(-count);
1894 roll[i] = rolling;
1895 ++leapcnt;
1896 } while (positive && --count != 0);
1899 static void
1900 adjleap P((void))
1902 register int i;
1903 register long last = 0;
1906 ** propagate leap seconds forward
1908 for (i = 0; i < leapcnt; ++i) {
1909 trans[i] = tadd(trans[i], last);
1910 last = corr[i] += last;
1914 static int
1915 yearistype(year, type)
1916 const int year;
1917 const char * const type;
1919 static char * buf;
1920 int result;
1922 if (type == NULL || *type == '\0')
1923 return TRUE;
1924 buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
1925 (void) sprintf(buf, "%s %d %s", yitcommand, year, type);
1926 result = system(buf);
1927 if (WIFEXITED(result)) switch (WEXITSTATUS(result)) {
1928 case 0:
1929 return TRUE;
1930 case 1:
1931 return FALSE;
1933 error(_("Wild result from command execution"));
1934 (void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
1935 progname, buf, result);
1936 for ( ; ; )
1937 (void) exit(EXIT_FAILURE);
1940 static int
1941 lowerit(a)
1942 int a;
1944 a = (unsigned char) a;
1945 return (isascii(a) && isupper(a)) ? tolower(a) : a;
1948 static int
1949 ciequal(ap, bp) /* case-insensitive equality */
1950 register const char * ap;
1951 register const char * bp;
1953 while (lowerit(*ap) == lowerit(*bp++))
1954 if (*ap++ == '\0')
1955 return TRUE;
1956 return FALSE;
1959 static int
1960 itsabbr(abbr, word)
1961 register const char * abbr;
1962 register const char * word;
1964 if (lowerit(*abbr) != lowerit(*word))
1965 return FALSE;
1966 ++word;
1967 while (*++abbr != '\0')
1968 do {
1969 if (*word == '\0')
1970 return FALSE;
1971 } while (lowerit(*word++) != lowerit(*abbr));
1972 return TRUE;
1975 static const struct lookup *
1976 byword(word, table)
1977 register const char * const word;
1978 register const struct lookup * const table;
1980 register const struct lookup * foundlp;
1981 register const struct lookup * lp;
1983 if (word == NULL || table == NULL)
1984 return NULL;
1986 ** Look for exact match.
1988 for (lp = table; lp->l_word != NULL; ++lp)
1989 if (ciequal(word, lp->l_word))
1990 return lp;
1992 ** Look for inexact match.
1994 foundlp = NULL;
1995 for (lp = table; lp->l_word != NULL; ++lp)
1996 if (itsabbr(word, lp->l_word)) {
1997 if (foundlp == NULL)
1998 foundlp = lp;
1999 else return NULL; /* multiple inexact matches */
2001 return foundlp;
2004 static char **
2005 getfields(cp)
2006 register char * cp;
2008 register char * dp;
2009 register char ** array;
2010 register int nsubs;
2012 if (cp == NULL)
2013 return NULL;
2014 array = (char **) (void *)
2015 emalloc((int) ((strlen(cp) + 1) * sizeof *array));
2016 nsubs = 0;
2017 for ( ; ; ) {
2018 while (isascii(*cp) && isspace((unsigned char) *cp))
2019 ++cp;
2020 if (*cp == '\0' || *cp == '#')
2021 break;
2022 array[nsubs++] = dp = cp;
2023 do {
2024 if ((*dp = *cp++) != '"')
2025 ++dp;
2026 else while ((*dp = *cp++) != '"')
2027 if (*dp != '\0')
2028 ++dp;
2029 else error(_("Odd number of quotation marks"));
2030 } while (*cp != '\0' && *cp != '#' &&
2031 (!isascii(*cp) || !isspace((unsigned char) *cp)));
2032 if (isascii(*cp) && isspace((unsigned char) *cp))
2033 ++cp;
2034 *dp = '\0';
2036 array[nsubs] = NULL;
2037 return array;
2040 static long
2041 oadd(t1, t2)
2042 const long t1;
2043 const long t2;
2045 register long t;
2047 t = t1 + t2;
2048 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2049 error(_("time overflow"));
2050 (void) exit(EXIT_FAILURE);
2052 return t;
2055 static time_t
2056 tadd(t1, t2)
2057 const time_t t1;
2058 const long t2;
2060 register time_t t;
2062 if (t1 == max_time && t2 > 0)
2063 return max_time;
2064 if (t1 == min_time && t2 < 0)
2065 return min_time;
2066 t = t1 + t2;
2067 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2068 error(_("time overflow"));
2069 (void) exit(EXIT_FAILURE);
2071 return t;
2075 ** Given a rule, and a year, compute the date - in seconds since January 1,
2076 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2079 static time_t
2080 rpytime(rp, wantedy)
2081 register const struct rule * const rp;
2082 register const int wantedy;
2084 register int y, m, i;
2085 register long dayoff; /* with a nod to Margaret O. */
2086 register time_t t;
2088 if (wantedy == INT_MIN)
2089 return min_time;
2090 if (wantedy == INT_MAX)
2091 return max_time;
2092 dayoff = 0;
2093 m = TM_JANUARY;
2094 y = EPOCH_YEAR;
2095 while (wantedy != y) {
2096 if (wantedy > y) {
2097 i = len_years[isleap(y)];
2098 ++y;
2099 } else {
2100 --y;
2101 i = -len_years[isleap(y)];
2103 dayoff = oadd(dayoff, eitol(i));
2105 while (m != rp->r_month) {
2106 i = len_months[isleap(y)][m];
2107 dayoff = oadd(dayoff, eitol(i));
2108 ++m;
2110 i = rp->r_dayofmonth;
2111 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
2112 if (rp->r_dycode == DC_DOWLEQ)
2113 --i;
2114 else {
2115 error(_("use of 2/29 in non leap-year"));
2116 (void) exit(EXIT_FAILURE);
2119 --i;
2120 dayoff = oadd(dayoff, eitol(i));
2121 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
2122 register long wday;
2124 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2125 wday = eitol(EPOCH_WDAY);
2127 ** Don't trust mod of negative numbers.
2129 if (dayoff >= 0)
2130 wday = (wday + dayoff) % LDAYSPERWEEK;
2131 else {
2132 wday -= ((-dayoff) % LDAYSPERWEEK);
2133 if (wday < 0)
2134 wday += LDAYSPERWEEK;
2136 while (wday != eitol(rp->r_wday))
2137 if (rp->r_dycode == DC_DOWGEQ) {
2138 dayoff = oadd(dayoff, (long) 1);
2139 if (++wday >= LDAYSPERWEEK)
2140 wday = 0;
2141 ++i;
2142 } else {
2143 dayoff = oadd(dayoff, (long) -1);
2144 if (--wday < 0)
2145 wday = LDAYSPERWEEK - 1;
2146 --i;
2148 if (i < 0 || i >= len_months[isleap(y)][m]) {
2149 error(_("no day in month matches rule"));
2150 (void) exit(EXIT_FAILURE);
2153 if (dayoff < 0 && !TYPE_SIGNED(time_t))
2154 return min_time;
2155 t = (time_t) dayoff * SECSPERDAY;
2157 ** Cheap overflow check.
2159 if (t / SECSPERDAY != dayoff)
2160 return (dayoff > 0) ? max_time : min_time;
2161 return tadd(t, rp->r_tod);
2164 static void
2165 newabbr(string)
2166 const char * const string;
2168 register int i;
2170 i = strlen(string) + 1;
2171 if (charcnt + i > TZ_MAX_CHARS) {
2172 error(_("too many, or too long, time zone abbreviations"));
2173 (void) exit(EXIT_FAILURE);
2175 (void) strcpy(&chars[charcnt], string);
2176 charcnt += eitol(i);
2179 static int
2180 mkdirs(argname)
2181 char * const argname;
2183 register char * name;
2184 register char * cp;
2186 if (argname == NULL || *argname == '\0')
2187 return 0;
2188 cp = name = ecpyalloc(argname);
2189 while ((cp = strchr(cp + 1, '/')) != 0) {
2190 *cp = '\0';
2191 #ifndef unix
2193 ** DOS drive specifier?
2195 if (isalpha((unsigned char) name[0]) &&
2196 name[1] == ':' && name[2] == '\0') {
2197 *cp = '/';
2198 continue;
2200 #endif /* !defined unix */
2201 if (!itsdir(name)) {
2203 ** It doesn't seem to exist, so we try to create it.
2204 ** Creation may fail because of the directory being
2205 ** created by some other multiprocessor, so we get
2206 ** to do extra checking.
2208 if (mkdir(name, MKDIR_UMASK) != 0) {
2209 const char *e = strerror(errno);
2211 if (errno != EEXIST || !itsdir(name)) {
2212 (void) fprintf(stderr,
2213 _("%s: Can't create directory %s: %s\n"),
2214 progname, name, e);
2215 ifree(name);
2216 return -1;
2220 *cp = '/';
2222 ifree(name);
2223 return 0;
2226 static long
2227 eitol(i)
2228 const int i;
2230 long l;
2232 l = i;
2233 if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0)) {
2234 (void) fprintf(stderr,
2235 _("%s: %d did not sign extend correctly\n"),
2236 progname, i);
2237 (void) exit(EXIT_FAILURE);
2239 return l;
2243 ** UNIX was a registered trademark of UNIX System Laboratories in 1993.