* sysdeps/alpha/backtrace.c: New.
[glibc.git] / timezone / zic.c
blobb164b36738b3559300b64472c3a629501baf4a9c
1 static char elsieid[] = "@(#)zic.c 7.113";
3 #include "private.h"
4 #include "locale.h"
5 #include "tzfile.h"
7 #if HAVE_SYS_STAT_H
8 #include "sys/stat.h"
9 #endif
10 #ifdef S_IRUSR
11 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
12 #else
13 #define MKDIR_UMASK 0755
14 #endif
17 ** On some ancient hosts, predicates like `isspace(C)' are defined
18 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
19 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
20 ** Neither the C Standard nor Posix require that `isascii' exist.
21 ** For portability, we check both ancient and modern requirements.
22 ** If isascii is not defined, the isascii check succeeds trivially.
24 #include "ctype.h"
25 #ifndef isascii
26 #define isascii(x) 1
27 #endif
29 struct rule {
30 const char * r_filename;
31 int r_linenum;
32 const char * r_name;
34 int r_loyear; /* for example, 1986 */
35 int r_hiyear; /* for example, 1986 */
36 const char * r_yrtype;
38 int r_month; /* 0..11 */
40 int r_dycode; /* see below */
41 int r_dayofmonth;
42 int r_wday;
44 long r_tod; /* time from midnight */
45 int r_todisstd; /* above is standard time if TRUE */
46 /* or wall clock time if FALSE */
47 int r_todisgmt; /* above is GMT if TRUE */
48 /* or local time if FALSE */
49 long r_stdoff; /* offset from standard time */
50 const char * r_abbrvar; /* variable part of abbreviation */
52 int r_todo; /* a rule to do (used in outzone) */
53 time_t r_temp; /* used in outzone */
57 ** r_dycode r_dayofmonth r_wday
60 #define DC_DOM 0 /* 1..31 */ /* unused */
61 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
62 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
64 struct zone {
65 const char * z_filename;
66 int z_linenum;
68 const char * z_name;
69 long z_gmtoff;
70 const char * z_rule;
71 const char * z_format;
73 long z_stdoff;
75 struct rule * z_rules;
76 int z_nrules;
78 struct rule z_untilrule;
79 time_t z_untiltime;
82 extern int getopt P((int argc, char * const argv[],
83 const char * options));
84 extern int link P((const char * fromname, const char * toname));
85 extern char * optarg;
86 extern int optind;
88 static void addtt P((time_t starttime, int type));
89 static int addtype P((long gmtoff, const char * abbr, int isdst,
90 int ttisstd, int ttisgmt));
91 static void leapadd P((time_t t, int positive, int rolling, int count));
92 static void adjleap P((void));
93 static void associate P((void));
94 static int ciequal P((const char * ap, const char * bp));
95 static void convert P((long val, char * buf));
96 static void dolink P((const char * fromfile, const char * tofile));
97 static void doabbr P((char * abbr, const char * format,
98 const char * letters, int isdst));
99 static void eat P((const char * name, int num));
100 static void eats P((const char * name, int num,
101 const char * rname, int rnum));
102 static long eitol P((int i));
103 static void error P((const char * message));
104 static char ** getfields P((char * buf));
105 static long gethms P((const char * string, const char * errstrng,
106 int signable));
107 static void infile P((const char * filename));
108 static void inleap P((char ** fields, int nfields));
109 static void inlink P((char ** fields, int nfields));
110 static void inrule P((char ** fields, int nfields));
111 static int inzcont P((char ** fields, int nfields));
112 static int inzone P((char ** fields, int nfields));
113 static int inzsub P((char ** fields, int nfields, int iscont));
114 static int itsabbr P((const char * abbr, const char * word));
115 static int itsdir P((const char * name));
116 static int lowerit P((int c));
117 static char * memcheck P((char * tocheck));
118 static int mkdirs P((char * filename));
119 static void newabbr P((const char * abbr));
120 static long oadd P((long t1, long t2));
121 static void outzone P((const struct zone * zp, int ntzones));
122 static void puttzcode P((long code, FILE * fp));
123 static int rcomp P((const void * leftp, const void * rightp));
124 static time_t rpytime P((const struct rule * rp, int wantedy));
125 static void rulesub P((struct rule * rp,
126 const char * loyearp, const char * hiyearp,
127 const char * typep, const char * monthp,
128 const char * dayp, const char * timep));
129 static void setboundaries P((void));
130 static time_t tadd P((time_t t1, long t2));
131 static void usage P((void));
132 static void writezone P((const char * name));
133 static int yearistype P((int year, const char * type));
135 #if !(HAVE_STRERROR - 0)
136 static char * strerror P((int));
137 #endif /* !(HAVE_STRERROR - 0) */
139 static int charcnt;
140 static int errors;
141 static const char * filename;
142 static int leapcnt;
143 static int linenum;
144 static time_t max_time;
145 static int max_year;
146 static int max_year_representable;
147 static time_t min_time;
148 static int min_year;
149 static int min_year_representable;
150 static int noise;
151 static const char * rfilename;
152 static int rlinenum;
153 static const char * progname;
154 static int timecnt;
155 static int typecnt;
158 ** Line codes.
161 #define LC_RULE 0
162 #define LC_ZONE 1
163 #define LC_LINK 2
164 #define LC_LEAP 3
167 ** Which fields are which on a Zone line.
170 #define ZF_NAME 1
171 #define ZF_GMTOFF 2
172 #define ZF_RULE 3
173 #define ZF_FORMAT 4
174 #define ZF_TILYEAR 5
175 #define ZF_TILMONTH 6
176 #define ZF_TILDAY 7
177 #define ZF_TILTIME 8
178 #define ZONE_MINFIELDS 5
179 #define ZONE_MAXFIELDS 9
182 ** Which fields are which on a Zone continuation line.
185 #define ZFC_GMTOFF 0
186 #define ZFC_RULE 1
187 #define ZFC_FORMAT 2
188 #define ZFC_TILYEAR 3
189 #define ZFC_TILMONTH 4
190 #define ZFC_TILDAY 5
191 #define ZFC_TILTIME 6
192 #define ZONEC_MINFIELDS 3
193 #define ZONEC_MAXFIELDS 7
196 ** Which files are which on a Rule line.
199 #define RF_NAME 1
200 #define RF_LOYEAR 2
201 #define RF_HIYEAR 3
202 #define RF_COMMAND 4
203 #define RF_MONTH 5
204 #define RF_DAY 6
205 #define RF_TOD 7
206 #define RF_STDOFF 8
207 #define RF_ABBRVAR 9
208 #define RULE_FIELDS 10
211 ** Which fields are which on a Link line.
214 #define LF_FROM 1
215 #define LF_TO 2
216 #define LINK_FIELDS 3
219 ** Which fields are which on a Leap line.
222 #define LP_YEAR 1
223 #define LP_MONTH 2
224 #define LP_DAY 3
225 #define LP_TIME 4
226 #define LP_CORR 5
227 #define LP_ROLL 6
228 #define LEAP_FIELDS 7
231 ** Year synonyms.
234 #define YR_MINIMUM 0
235 #define YR_MAXIMUM 1
236 #define YR_ONLY 2
238 static struct rule * rules;
239 static int nrules; /* number of rules */
241 static struct zone * zones;
242 static int nzones; /* number of zones */
244 struct link {
245 const char * l_filename;
246 int l_linenum;
247 const char * l_from;
248 const char * l_to;
251 static struct link * links;
252 static int nlinks;
254 struct lookup {
255 const char * l_word;
256 const int l_value;
259 static struct lookup const * byword P((const char * string,
260 const struct lookup * lp));
262 static struct lookup const line_codes[] = {
263 { "Rule", LC_RULE },
264 { "Zone", LC_ZONE },
265 { "Link", LC_LINK },
266 { "Leap", LC_LEAP },
267 { NULL, 0}
270 static struct lookup const mon_names[] = {
271 { "January", TM_JANUARY },
272 { "February", TM_FEBRUARY },
273 { "March", TM_MARCH },
274 { "April", TM_APRIL },
275 { "May", TM_MAY },
276 { "June", TM_JUNE },
277 { "July", TM_JULY },
278 { "August", TM_AUGUST },
279 { "September", TM_SEPTEMBER },
280 { "October", TM_OCTOBER },
281 { "November", TM_NOVEMBER },
282 { "December", TM_DECEMBER },
283 { NULL, 0 }
286 static struct lookup const wday_names[] = {
287 { "Sunday", TM_SUNDAY },
288 { "Monday", TM_MONDAY },
289 { "Tuesday", TM_TUESDAY },
290 { "Wednesday", TM_WEDNESDAY },
291 { "Thursday", TM_THURSDAY },
292 { "Friday", TM_FRIDAY },
293 { "Saturday", TM_SATURDAY },
294 { NULL, 0 }
297 static struct lookup const lasts[] = {
298 { "last-Sunday", TM_SUNDAY },
299 { "last-Monday", TM_MONDAY },
300 { "last-Tuesday", TM_TUESDAY },
301 { "last-Wednesday", TM_WEDNESDAY },
302 { "last-Thursday", TM_THURSDAY },
303 { "last-Friday", TM_FRIDAY },
304 { "last-Saturday", TM_SATURDAY },
305 { NULL, 0 }
308 static struct lookup const begin_years[] = {
309 { "minimum", YR_MINIMUM },
310 { "maximum", YR_MAXIMUM },
311 { NULL, 0 }
314 static struct lookup const end_years[] = {
315 { "minimum", YR_MINIMUM },
316 { "maximum", YR_MAXIMUM },
317 { "only", YR_ONLY },
318 { NULL, 0 }
321 static struct lookup const leap_types[] = {
322 { "Rolling", TRUE },
323 { "Stationary", FALSE },
324 { NULL, 0 }
327 static const int len_months[2][MONSPERYEAR] = {
328 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
329 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
332 static const int len_years[2] = {
333 DAYSPERNYEAR, DAYSPERLYEAR
336 static struct attype {
337 time_t at;
338 unsigned char type;
339 } attypes[TZ_MAX_TIMES];
340 static long gmtoffs[TZ_MAX_TYPES];
341 static char isdsts[TZ_MAX_TYPES];
342 static unsigned char abbrinds[TZ_MAX_TYPES];
343 static char ttisstds[TZ_MAX_TYPES];
344 static char ttisgmts[TZ_MAX_TYPES];
345 static char chars[TZ_MAX_CHARS];
346 static time_t trans[TZ_MAX_LEAPS];
347 static long corr[TZ_MAX_LEAPS];
348 static char roll[TZ_MAX_LEAPS];
351 ** Memory allocation.
354 static char *
355 memcheck(ptr)
356 char * const ptr;
358 if (ptr == NULL) {
359 const char *e = strerror(errno);
361 (void) fprintf(stderr, _("%s: Memory exhausted: %s\n"),
362 progname, e);
363 (void) exit(EXIT_FAILURE);
365 return ptr;
368 #define emalloc(size) memcheck(imalloc(size))
369 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
370 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
371 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
374 ** Error handling.
377 #if !(HAVE_STRERROR - 0)
378 static char *
379 strerror(errnum)
380 int errnum;
382 extern char * sys_errlist[];
383 extern int sys_nerr;
385 return (errnum > 0 && errnum <= sys_nerr) ?
386 sys_errlist[errnum] : _("Unknown system error");
388 #endif /* !(HAVE_STRERROR - 0) */
390 static void
391 eats(name, num, rname, rnum)
392 const char * const name;
393 const int num;
394 const char * const rname;
395 const int rnum;
397 filename = name;
398 linenum = num;
399 rfilename = rname;
400 rlinenum = rnum;
403 static void
404 eat(name, num)
405 const char * const name;
406 const int num;
408 eats(name, num, (char *) NULL, -1);
411 static void
412 error(string)
413 const char * const string;
416 ** Match the format of "cc" to allow sh users to
417 ** zic ... 2>&1 | error -t "*" -v
418 ** on BSD systems.
420 (void) fprintf(stderr, _("\"%s\", line %d: %s"),
421 filename, linenum, string);
422 if (rfilename != NULL)
423 (void) fprintf(stderr, _(" (rule from \"%s\", line %d)"),
424 rfilename, rlinenum);
425 (void) fprintf(stderr, "\n");
426 ++errors;
429 static void
430 warning(string)
431 const char * const string;
433 char * cp;
435 cp = ecpyalloc(_("warning: "));
436 cp = ecatalloc(cp, string);
437 error(cp);
438 ifree(cp);
439 --errors;
442 static void
443 usage P((void))
445 (void) fprintf(stderr, _("%s: usage is %s [ --version ] [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
446 progname, progname);
447 (void) exit(EXIT_FAILURE);
450 static const char * psxrules;
451 static const char * lcltime;
452 static const char * directory;
453 static const char * leapsec;
454 static const char * yitcommand;
455 static int sflag = FALSE;
458 main(argc, argv)
459 int argc;
460 char * argv[];
462 register int i;
463 register int j;
464 register int c;
466 #ifdef unix
467 (void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
468 #endif /* defined unix */
469 #if HAVE_GETTEXT - 0
470 (void) setlocale(LC_CTYPE, "");
471 (void) setlocale(LC_MESSAGES, "");
472 #ifdef TZ_DOMAINDIR
473 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
474 #endif /* defined TEXTDOMAINDIR */
475 (void) textdomain(TZ_DOMAIN);
476 #endif /* HAVE_GETTEXT - 0 */
477 progname = argv[0];
478 for (i = 1; i < argc; ++i)
479 if (strcmp(argv[i], "--version") == 0) {
480 (void) printf("%s\n", elsieid);
481 (void) exit(EXIT_SUCCESS);
483 while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF && c != -1)
484 switch (c) {
485 default:
486 usage();
487 case 'd':
488 if (directory == NULL)
489 directory = optarg;
490 else {
491 (void) fprintf(stderr,
492 _("%s: More than one -d option specified\n"),
493 progname);
494 (void) exit(EXIT_FAILURE);
496 break;
497 case 'l':
498 if (lcltime == NULL)
499 lcltime = optarg;
500 else {
501 (void) fprintf(stderr,
502 _("%s: More than one -l option specified\n"),
503 progname);
504 (void) exit(EXIT_FAILURE);
506 break;
507 case 'p':
508 if (psxrules == NULL)
509 psxrules = optarg;
510 else {
511 (void) fprintf(stderr,
512 _("%s: More than one -p option specified\n"),
513 progname);
514 (void) exit(EXIT_FAILURE);
516 break;
517 case 'y':
518 if (yitcommand == NULL)
519 yitcommand = optarg;
520 else {
521 (void) fprintf(stderr,
522 _("%s: More than one -y option specified\n"),
523 progname);
524 (void) exit(EXIT_FAILURE);
526 break;
527 case 'L':
528 if (leapsec == NULL)
529 leapsec = optarg;
530 else {
531 (void) fprintf(stderr,
532 _("%s: More than one -L option specified\n"),
533 progname);
534 (void) exit(EXIT_FAILURE);
536 break;
537 case 'v':
538 noise = TRUE;
539 break;
540 case 's':
541 sflag = TRUE;
542 break;
544 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
545 usage(); /* usage message by request */
546 if (directory == NULL)
547 directory = TZDIR;
548 if (yitcommand == NULL)
549 yitcommand = "yearistype";
551 setboundaries();
553 if (optind < argc && leapsec != NULL) {
554 infile(leapsec);
555 adjleap();
558 for (i = optind; i < argc; ++i)
559 infile(argv[i]);
560 if (errors)
561 (void) exit(EXIT_FAILURE);
562 associate();
563 for (i = 0; i < nzones; i = j) {
565 ** Find the next non-continuation zone entry.
567 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
568 continue;
569 outzone(&zones[i], j - i);
572 ** Make links.
574 for (i = 0; i < nlinks; ++i) {
575 eat(links[i].l_filename, links[i].l_linenum);
576 dolink(links[i].l_from, links[i].l_to);
578 if (lcltime != NULL) {
579 eat("command line", 1);
580 dolink(lcltime, TZDEFAULT);
582 if (psxrules != NULL) {
583 eat("command line", 1);
584 dolink(psxrules, TZDEFRULES);
586 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
589 static void
590 dolink(fromfile, tofile)
591 const char * const fromfile;
592 const char * const tofile;
594 register char * fromname;
595 register char * toname;
597 if (fromfile[0] == '/')
598 fromname = ecpyalloc(fromfile);
599 else {
600 fromname = ecpyalloc(directory);
601 fromname = ecatalloc(fromname, "/");
602 fromname = ecatalloc(fromname, fromfile);
604 if (tofile[0] == '/')
605 toname = ecpyalloc(tofile);
606 else {
607 toname = ecpyalloc(directory);
608 toname = ecatalloc(toname, "/");
609 toname = ecatalloc(toname, tofile);
612 ** We get to be careful here since
613 ** there's a fair chance of root running us.
615 if (!itsdir(toname))
616 (void) remove(toname);
617 if (link(fromname, toname) != 0) {
618 int result;
620 if (mkdirs(toname) != 0)
621 (void) exit(EXIT_FAILURE);
623 result = link(fromname, toname);
624 #if (HAVE_SYMLINK - 0)
625 if (result != 0 &&
626 access(fromname, F_OK) == 0 &&
627 !itsdir(fromname)) {
628 const char *s = tofile;
629 register char * symlinkcontents = NULL;
630 while ((s = strchr(s+1, '/')) != NULL)
631 symlinkcontents = ecatalloc(symlinkcontents, "../");
632 symlinkcontents = ecatalloc(symlinkcontents, fromname);
634 result = unlink(toname);
635 if (result != 0 && errno != ENOENT) {
636 const char *e = strerror(errno);
638 (void) fprintf(stderr,
639 _("%s: Can't unlink %s: %s\n"),
640 progname, toname, e);
641 (void) exit(EXIT_FAILURE);
644 result = symlink(symlinkcontents, toname);
645 if (result == 0)
646 warning(_("hard link failed, symbolic link used"));
647 ifree(symlinkcontents);
649 #endif
650 if (result != 0) {
651 const char *e = strerror(errno);
653 (void) fprintf(stderr,
654 _("%s: Can't link from %s to %s: %s\n"),
655 progname, fromname, toname, e);
656 (void) exit(EXIT_FAILURE);
659 ifree(fromname);
660 ifree(toname);
663 #ifndef INT_MAX
664 #define INT_MAX ((int) (((unsigned)~0)>>1))
665 #endif /* !defined INT_MAX */
667 #ifndef INT_MIN
668 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
669 #endif /* !defined INT_MIN */
672 ** The tz file format currently allows at most 32-bit quantities.
673 ** This restriction should be removed before signed 32-bit values
674 ** wrap around in 2038, but unfortunately this will require a
675 ** change to the tz file format.
678 #define MAX_BITS_IN_FILE 32
679 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(time_t) < MAX_BITS_IN_FILE) ? TYPE_BIT(time_t) : MAX_BITS_IN_FILE)
681 static void
682 setboundaries P((void))
684 if (TYPE_SIGNED(time_t)) {
685 min_time = ~ (time_t) 0;
686 min_time <<= TIME_T_BITS_IN_FILE - 1;
687 max_time = ~ (time_t) 0 - min_time;
688 if (sflag)
689 min_time = 0;
690 } else {
691 min_time = 0;
692 max_time = 2 - sflag;
693 max_time <<= TIME_T_BITS_IN_FILE - 1;
694 --max_time;
696 min_year = TM_YEAR_BASE + gmtime(&min_time)->tm_year;
697 max_year = TM_YEAR_BASE + gmtime(&max_time)->tm_year;
698 min_year_representable = min_year;
699 max_year_representable = max_year;
702 static int
703 itsdir(name)
704 const char * const name;
706 register char * myname;
707 register int accres;
709 myname = ecpyalloc(name);
710 myname = ecatalloc(myname, "/.");
711 accres = access(myname, F_OK);
712 ifree(myname);
713 return accres == 0;
717 ** Associate sets of rules with zones.
721 ** Sort by rule name.
724 static int
725 rcomp(cp1, cp2)
726 const void * cp1;
727 const void * cp2;
729 return strcmp(((const struct rule *) cp1)->r_name,
730 ((const struct rule *) cp2)->r_name);
733 static void
734 associate P((void))
736 register struct zone * zp;
737 register struct rule * rp;
738 register int base, out;
739 register int i, j;
741 if (nrules != 0) {
742 (void) qsort((void *) rules, (size_t) nrules,
743 (size_t) sizeof *rules, rcomp);
744 for (i = 0; i < nrules - 1; ++i) {
745 if (strcmp(rules[i].r_name,
746 rules[i + 1].r_name) != 0)
747 continue;
748 if (strcmp(rules[i].r_filename,
749 rules[i + 1].r_filename) == 0)
750 continue;
751 eat(rules[i].r_filename, rules[i].r_linenum);
752 warning(_("same rule name in multiple files"));
753 eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
754 warning(_("same rule name in multiple files"));
755 for (j = i + 2; j < nrules; ++j) {
756 if (strcmp(rules[i].r_name,
757 rules[j].r_name) != 0)
758 break;
759 if (strcmp(rules[i].r_filename,
760 rules[j].r_filename) == 0)
761 continue;
762 if (strcmp(rules[i + 1].r_filename,
763 rules[j].r_filename) == 0)
764 continue;
765 break;
767 i = j - 1;
770 for (i = 0; i < nzones; ++i) {
771 zp = &zones[i];
772 zp->z_rules = NULL;
773 zp->z_nrules = 0;
775 for (base = 0; base < nrules; base = out) {
776 rp = &rules[base];
777 for (out = base + 1; out < nrules; ++out)
778 if (strcmp(rp->r_name, rules[out].r_name) != 0)
779 break;
780 for (i = 0; i < nzones; ++i) {
781 zp = &zones[i];
782 if (strcmp(zp->z_rule, rp->r_name) != 0)
783 continue;
784 zp->z_rules = rp;
785 zp->z_nrules = out - base;
788 for (i = 0; i < nzones; ++i) {
789 zp = &zones[i];
790 if (zp->z_nrules == 0) {
792 ** Maybe we have a local standard time offset.
794 eat(zp->z_filename, zp->z_linenum);
795 zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
796 TRUE);
798 ** Note, though, that if there's no rule,
799 ** a '%s' in the format is a bad thing.
801 if (strchr(zp->z_format, '%') != 0)
802 error(_("%s in ruleless zone"));
805 if (errors)
806 (void) exit(EXIT_FAILURE);
809 static void
810 infile(name)
811 const char * name;
813 register FILE * fp;
814 register char ** fields;
815 register char * cp;
816 register const struct lookup * lp;
817 register int nfields;
818 register int wantcont;
819 register int num;
820 char buf[BUFSIZ];
822 if (strcmp(name, "-") == 0) {
823 name = _("standard input");
824 fp = stdin;
825 } else if ((fp = fopen(name, "r")) == NULL) {
826 const char *e = strerror(errno);
828 (void) fprintf(stderr, _("%s: Can't open %s: %s\n"),
829 progname, name, e);
830 (void) exit(EXIT_FAILURE);
832 wantcont = FALSE;
833 for (num = 1; ; ++num) {
834 eat(name, num);
835 if (fgets(buf, (int) sizeof buf, fp) != buf)
836 break;
837 cp = strchr(buf, '\n');
838 if (cp == NULL) {
839 error(_("line too long"));
840 (void) exit(EXIT_FAILURE);
842 *cp = '\0';
843 fields = getfields(buf);
844 nfields = 0;
845 while (fields[nfields] != NULL) {
846 static char nada;
848 if (strcmp(fields[nfields], "-") == 0)
849 fields[nfields] = &nada;
850 ++nfields;
852 if (nfields == 0) {
853 /* nothing to do */
854 } else if (wantcont) {
855 wantcont = inzcont(fields, nfields);
856 } else {
857 lp = byword(fields[0], line_codes);
858 if (lp == NULL)
859 error(_("input line of unknown type"));
860 else switch ((int) (lp->l_value)) {
861 case LC_RULE:
862 inrule(fields, nfields);
863 wantcont = FALSE;
864 break;
865 case LC_ZONE:
866 wantcont = inzone(fields, nfields);
867 break;
868 case LC_LINK:
869 inlink(fields, nfields);
870 wantcont = FALSE;
871 break;
872 case LC_LEAP:
873 if (name != leapsec)
874 (void) fprintf(stderr,
875 _("%s: Leap line in non leap seconds file %s\n"),
876 progname, name);
877 else inleap(fields, nfields);
878 wantcont = FALSE;
879 break;
880 default: /* "cannot happen" */
881 (void) fprintf(stderr,
882 _("%s: panic: Invalid l_value %d\n"),
883 progname, lp->l_value);
884 (void) exit(EXIT_FAILURE);
887 ifree((char *) fields);
889 if (ferror(fp)) {
890 (void) fprintf(stderr, _("%s: Error reading %s\n"),
891 progname, filename);
892 (void) exit(EXIT_FAILURE);
894 if (fp != stdin && fclose(fp)) {
895 const char *e = strerror(errno);
897 (void) fprintf(stderr, _("%s: Error closing %s: %s\n"),
898 progname, filename, e);
899 (void) exit(EXIT_FAILURE);
901 if (wantcont)
902 error(_("expected continuation line not found"));
906 ** Convert a string of one of the forms
907 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
908 ** into a number of seconds.
909 ** A null string maps to zero.
910 ** Call error with errstring and return zero on errors.
913 static long
914 gethms(string, errstring, signable)
915 const char * string;
916 const char * const errstring;
917 const int signable;
919 int hh, mm, ss, sign;
921 if (string == NULL || *string == '\0')
922 return 0;
923 if (!signable)
924 sign = 1;
925 else if (*string == '-') {
926 sign = -1;
927 ++string;
928 } else sign = 1;
929 if (sscanf(string, scheck(string, "%d"), &hh) == 1)
930 mm = ss = 0;
931 else if (sscanf(string, scheck(string, "%d:%d"), &hh, &mm) == 2)
932 ss = 0;
933 else if (sscanf(string, scheck(string, "%d:%d:%d"),
934 &hh, &mm, &ss) != 3) {
935 error(errstring);
936 return 0;
938 if ((hh < 0 || hh >= HOURSPERDAY ||
939 mm < 0 || mm >= MINSPERHOUR ||
940 ss < 0 || ss > SECSPERMIN) &&
941 !(hh == HOURSPERDAY && mm == 0 && ss == 0)) {
942 error(errstring);
943 return 0;
945 return eitol(sign) *
946 (eitol(hh * MINSPERHOUR + mm) *
947 eitol(SECSPERMIN) + eitol(ss));
950 static void
951 inrule(fields, nfields)
952 register char ** const fields;
953 const int nfields;
955 static struct rule r;
957 if (nfields != RULE_FIELDS) {
958 error(_("wrong number of fields on Rule line"));
959 return;
961 if (*fields[RF_NAME] == '\0') {
962 error(_("nameless rule"));
963 return;
965 r.r_filename = filename;
966 r.r_linenum = linenum;
967 r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
968 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
969 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
970 r.r_name = ecpyalloc(fields[RF_NAME]);
971 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
972 rules = (struct rule *) (void *) erealloc((char *) rules,
973 (int) ((nrules + 1) * sizeof *rules));
974 rules[nrules++] = r;
977 static int
978 inzone(fields, nfields)
979 register char ** const fields;
980 const int nfields;
982 register int i;
983 static char * buf;
985 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
986 error(_("wrong number of fields on Zone line"));
987 return FALSE;
989 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
990 buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
991 (void) sprintf(buf,
992 _("\"Zone %s\" line and -l option are mutually exclusive"),
993 TZDEFAULT);
994 error(buf);
995 return FALSE;
997 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
998 buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
999 (void) sprintf(buf,
1000 _("\"Zone %s\" line and -p option are mutually exclusive"),
1001 TZDEFRULES);
1002 error(buf);
1003 return FALSE;
1005 for (i = 0; i < nzones; ++i)
1006 if (zones[i].z_name != NULL &&
1007 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
1008 buf = erealloc(buf, (int) (132 +
1009 strlen(fields[ZF_NAME]) +
1010 strlen(zones[i].z_filename)));
1011 (void) sprintf(buf,
1012 _("duplicate zone name %s (file \"%s\", line %d)"),
1013 fields[ZF_NAME],
1014 zones[i].z_filename,
1015 zones[i].z_linenum);
1016 error(buf);
1017 return FALSE;
1019 return inzsub(fields, nfields, FALSE);
1022 static int
1023 inzcont(fields, nfields)
1024 register char ** const fields;
1025 const int nfields;
1027 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
1028 error(_("wrong number of fields on Zone continuation line"));
1029 return FALSE;
1031 return inzsub(fields, nfields, TRUE);
1034 static int
1035 inzsub(fields, nfields, iscont)
1036 register char ** const fields;
1037 const int nfields;
1038 const int iscont;
1040 register char * cp;
1041 static struct zone z;
1042 register int i_gmtoff, i_rule, i_format;
1043 register int i_untilyear, i_untilmonth;
1044 register int i_untilday, i_untiltime;
1045 register int hasuntil;
1047 if (iscont) {
1048 i_gmtoff = ZFC_GMTOFF;
1049 i_rule = ZFC_RULE;
1050 i_format = ZFC_FORMAT;
1051 i_untilyear = ZFC_TILYEAR;
1052 i_untilmonth = ZFC_TILMONTH;
1053 i_untilday = ZFC_TILDAY;
1054 i_untiltime = ZFC_TILTIME;
1055 z.z_name = NULL;
1056 } else {
1057 i_gmtoff = ZF_GMTOFF;
1058 i_rule = ZF_RULE;
1059 i_format = ZF_FORMAT;
1060 i_untilyear = ZF_TILYEAR;
1061 i_untilmonth = ZF_TILMONTH;
1062 i_untilday = ZF_TILDAY;
1063 i_untiltime = ZF_TILTIME;
1064 z.z_name = ecpyalloc(fields[ZF_NAME]);
1066 z.z_filename = filename;
1067 z.z_linenum = linenum;
1068 z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UTC offset"), TRUE);
1069 if ((cp = strchr(fields[i_format], '%')) != 0) {
1070 if (*++cp != 's' || strchr(cp, '%') != 0) {
1071 error(_("invalid abbreviation format"));
1072 return FALSE;
1075 z.z_rule = ecpyalloc(fields[i_rule]);
1076 z.z_format = ecpyalloc(fields[i_format]);
1077 hasuntil = nfields > i_untilyear;
1078 if (hasuntil) {
1079 z.z_untilrule.r_filename = filename;
1080 z.z_untilrule.r_linenum = linenum;
1081 rulesub(&z.z_untilrule,
1082 fields[i_untilyear],
1083 "only",
1085 (nfields > i_untilmonth) ?
1086 fields[i_untilmonth] : "Jan",
1087 (nfields > i_untilday) ? fields[i_untilday] : "1",
1088 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1089 z.z_untiltime = rpytime(&z.z_untilrule,
1090 z.z_untilrule.r_loyear);
1091 if (iscont && nzones > 0 &&
1092 z.z_untiltime > min_time &&
1093 z.z_untiltime < max_time &&
1094 zones[nzones - 1].z_untiltime > min_time &&
1095 zones[nzones - 1].z_untiltime < max_time &&
1096 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1097 error(_("Zone continuation line end time is not after end time of previous line"));
1098 return FALSE;
1101 zones = (struct zone *) (void *) erealloc((char *) zones,
1102 (int) ((nzones + 1) * sizeof *zones));
1103 zones[nzones++] = z;
1105 ** If there was an UNTIL field on this line,
1106 ** there's more information about the zone on the next line.
1108 return hasuntil;
1111 static void
1112 inleap(fields, nfields)
1113 register char ** const fields;
1114 const int nfields;
1116 register const char * cp;
1117 register const struct lookup * lp;
1118 register int i, j;
1119 int year, month, day;
1120 long dayoff, tod;
1121 time_t t;
1123 if (nfields != LEAP_FIELDS) {
1124 error(_("wrong number of fields on Leap line"));
1125 return;
1127 dayoff = 0;
1128 cp = fields[LP_YEAR];
1129 if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
1131 * Leapin' Lizards!
1133 error(_("invalid leaping year"));
1134 return;
1136 j = EPOCH_YEAR;
1137 while (j != year) {
1138 if (year > j) {
1139 i = len_years[isleap(j)];
1140 ++j;
1141 } else {
1142 --j;
1143 i = -len_years[isleap(j)];
1145 dayoff = oadd(dayoff, eitol(i));
1147 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1148 error(_("invalid month name"));
1149 return;
1151 month = lp->l_value;
1152 j = TM_JANUARY;
1153 while (j != month) {
1154 i = len_months[isleap(year)][j];
1155 dayoff = oadd(dayoff, eitol(i));
1156 ++j;
1158 cp = fields[LP_DAY];
1159 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1160 day <= 0 || day > len_months[isleap(year)][month]) {
1161 error(_("invalid day of month"));
1162 return;
1164 dayoff = oadd(dayoff, eitol(day - 1));
1165 if (dayoff < 0 && !TYPE_SIGNED(time_t)) {
1166 error(_("time before zero"));
1167 return;
1169 if (dayoff < min_time / SECSPERDAY) {
1170 error(_("time too small"));
1171 return;
1173 if (dayoff > max_time / SECSPERDAY) {
1174 error(_("time too large"));
1175 return;
1177 t = (time_t) dayoff * SECSPERDAY;
1178 tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1179 cp = fields[LP_CORR];
1181 register int positive;
1182 int count;
1184 if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1185 positive = FALSE;
1186 count = 1;
1187 } else if (strcmp(cp, "--") == 0) {
1188 positive = FALSE;
1189 count = 2;
1190 } else if (strcmp(cp, "+") == 0) {
1191 positive = TRUE;
1192 count = 1;
1193 } else if (strcmp(cp, "++") == 0) {
1194 positive = TRUE;
1195 count = 2;
1196 } else {
1197 error(_("illegal CORRECTION field on Leap line"));
1198 return;
1200 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1201 error(_("illegal Rolling/Stationary field on Leap line"));
1202 return;
1204 leapadd(tadd(t, tod), positive, lp->l_value, count);
1208 static void
1209 inlink(fields, nfields)
1210 register char ** const fields;
1211 const int nfields;
1213 struct link l;
1215 if (nfields != LINK_FIELDS) {
1216 error(_("wrong number of fields on Link line"));
1217 return;
1219 if (*fields[LF_FROM] == '\0') {
1220 error(_("blank FROM field on Link line"));
1221 return;
1223 if (*fields[LF_TO] == '\0') {
1224 error(_("blank TO field on Link line"));
1225 return;
1227 l.l_filename = filename;
1228 l.l_linenum = linenum;
1229 l.l_from = ecpyalloc(fields[LF_FROM]);
1230 l.l_to = ecpyalloc(fields[LF_TO]);
1231 links = (struct link *) (void *) erealloc((char *) links,
1232 (int) ((nlinks + 1) * sizeof *links));
1233 links[nlinks++] = l;
1236 static void
1237 rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
1238 register struct rule * const rp;
1239 const char * const loyearp;
1240 const char * const hiyearp;
1241 const char * const typep;
1242 const char * const monthp;
1243 const char * const dayp;
1244 const char * const timep;
1246 register const struct lookup * lp;
1247 register const char * cp;
1248 register char * dp;
1249 register char * ep;
1251 if ((lp = byword(monthp, mon_names)) == NULL) {
1252 error(_("invalid month name"));
1253 return;
1255 rp->r_month = lp->l_value;
1256 rp->r_todisstd = FALSE;
1257 rp->r_todisgmt = FALSE;
1258 dp = ecpyalloc(timep);
1259 if (*dp != '\0') {
1260 ep = dp + strlen(dp) - 1;
1261 switch (lowerit(*ep)) {
1262 case 's': /* Standard */
1263 rp->r_todisstd = TRUE;
1264 rp->r_todisgmt = FALSE;
1265 *ep = '\0';
1266 break;
1267 case 'w': /* Wall */
1268 rp->r_todisstd = FALSE;
1269 rp->r_todisgmt = FALSE;
1270 *ep = '\0';
1271 break;
1272 case 'g': /* Greenwich */
1273 case 'u': /* Universal */
1274 case 'z': /* Zulu */
1275 rp->r_todisstd = TRUE;
1276 rp->r_todisgmt = TRUE;
1277 *ep = '\0';
1278 break;
1281 rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1282 ifree(dp);
1284 ** Year work.
1286 cp = loyearp;
1287 lp = byword(cp, begin_years);
1288 if (lp != NULL) switch ((int) lp->l_value) {
1289 case YR_MINIMUM:
1290 rp->r_loyear = INT_MIN;
1291 break;
1292 case YR_MAXIMUM:
1293 rp->r_loyear = INT_MAX;
1294 break;
1295 default: /* "cannot happen" */
1296 (void) fprintf(stderr,
1297 _("%s: panic: Invalid l_value %d\n"),
1298 progname, lp->l_value);
1299 (void) exit(EXIT_FAILURE);
1300 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
1301 error(_("invalid starting year"));
1302 return;
1303 } else if (noise) {
1304 if (rp->r_loyear < min_year_representable)
1305 warning(_("starting year too low to be represented"));
1306 else if (rp->r_loyear > max_year_representable)
1307 warning(_("starting year too high to be represented"));
1309 cp = hiyearp;
1310 if ((lp = byword(cp, end_years)) != NULL) switch ((int) lp->l_value) {
1311 case YR_MINIMUM:
1312 rp->r_hiyear = INT_MIN;
1313 break;
1314 case YR_MAXIMUM:
1315 rp->r_hiyear = INT_MAX;
1316 break;
1317 case YR_ONLY:
1318 rp->r_hiyear = rp->r_loyear;
1319 break;
1320 default: /* "cannot happen" */
1321 (void) fprintf(stderr,
1322 _("%s: panic: Invalid l_value %d\n"),
1323 progname, lp->l_value);
1324 (void) exit(EXIT_FAILURE);
1325 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
1326 error(_("invalid ending year"));
1327 return;
1328 } else if (noise) {
1329 if (rp->r_loyear < min_year_representable)
1330 warning(_("ending year too low to be represented"));
1331 else if (rp->r_loyear > max_year_representable)
1332 warning(_("ending year too high to be represented"));
1334 if (rp->r_loyear > rp->r_hiyear) {
1335 error(_("starting year greater than ending year"));
1336 return;
1338 if (*typep == '\0')
1339 rp->r_yrtype = NULL;
1340 else {
1341 if (rp->r_loyear == rp->r_hiyear) {
1342 error(_("typed single year"));
1343 return;
1345 rp->r_yrtype = ecpyalloc(typep);
1347 if (rp->r_loyear < min_year && rp->r_loyear > 0)
1348 min_year = rp->r_loyear;
1350 ** Day work.
1351 ** Accept things such as:
1352 ** 1
1353 ** last-Sunday
1354 ** Sun<=20
1355 ** Sun>=7
1357 dp = ecpyalloc(dayp);
1358 if ((lp = byword(dp, lasts)) != NULL) {
1359 rp->r_dycode = DC_DOWLEQ;
1360 rp->r_wday = lp->l_value;
1361 rp->r_dayofmonth = len_months[1][rp->r_month];
1362 } else {
1363 if ((ep = strchr(dp, '<')) != 0)
1364 rp->r_dycode = DC_DOWLEQ;
1365 else if ((ep = strchr(dp, '>')) != 0)
1366 rp->r_dycode = DC_DOWGEQ;
1367 else {
1368 ep = dp;
1369 rp->r_dycode = DC_DOM;
1371 if (rp->r_dycode != DC_DOM) {
1372 *ep++ = 0;
1373 if (*ep++ != '=') {
1374 error(_("invalid day of month"));
1375 ifree(dp);
1376 return;
1378 if ((lp = byword(dp, wday_names)) == NULL) {
1379 error(_("invalid weekday name"));
1380 ifree(dp);
1381 return;
1383 rp->r_wday = lp->l_value;
1385 if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1386 rp->r_dayofmonth <= 0 ||
1387 (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1388 error(_("invalid day of month"));
1389 ifree(dp);
1390 return;
1393 ifree(dp);
1396 static void
1397 convert(val, buf)
1398 const long val;
1399 char * const buf;
1401 register int i;
1402 register long shift;
1404 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1405 buf[i] = val >> shift;
1408 static void
1409 puttzcode(val, fp)
1410 const long val;
1411 FILE * const fp;
1413 char buf[4];
1415 convert(val, buf);
1416 (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1419 static int
1420 atcomp(avp, bvp)
1421 void * avp;
1422 void * bvp;
1424 if (((struct attype *) avp)->at < ((struct attype *) bvp)->at)
1425 return -1;
1426 else if (((struct attype *) avp)->at > ((struct attype *) bvp)->at)
1427 return 1;
1428 else return 0;
1431 static void
1432 writezone(name)
1433 const char * const name;
1435 register FILE * fp;
1436 register int i, j;
1437 static char * fullname;
1438 static struct tzhead tzh;
1439 time_t ats[TZ_MAX_TIMES];
1440 unsigned char types[TZ_MAX_TIMES];
1443 ** Sort.
1445 if (timecnt > 1)
1446 (void) qsort((void *) attypes, (size_t) timecnt,
1447 (size_t) sizeof *attypes, atcomp);
1449 ** Optimize.
1452 int fromi;
1453 int toi;
1455 toi = 0;
1456 fromi = 0;
1457 while (fromi < timecnt && attypes[fromi].at < min_time)
1458 ++fromi;
1459 if (isdsts[0] == 0)
1460 while (fromi < timecnt && attypes[fromi].type == 0)
1461 ++fromi; /* handled by default rule */
1462 for ( ; fromi < timecnt; ++fromi) {
1463 if (toi != 0
1464 && ((attypes[fromi].at
1465 + gmtoffs[attypes[toi - 1].type])
1466 <= (attypes[toi - 1].at
1467 + gmtoffs[toi == 1 ? 0
1468 : attypes[toi - 2].type]))) {
1469 attypes[toi - 1].type = attypes[fromi].type;
1470 continue;
1472 if (toi == 0 ||
1473 attypes[toi - 1].type != attypes[fromi].type)
1474 attypes[toi++] = attypes[fromi];
1476 timecnt = toi;
1479 ** Transfer.
1481 for (i = 0; i < timecnt; ++i) {
1482 ats[i] = attypes[i].at;
1483 types[i] = attypes[i].type;
1485 fullname = erealloc(fullname,
1486 (int) (strlen(directory) + 1 + strlen(name) + 1));
1487 (void) sprintf(fullname, "%s/%s", directory, name);
1489 ** Remove old file, if any, to snap links.
1491 if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT) {
1492 const char *e = strerror(errno);
1494 (void) fprintf(stderr, _("%s: Can't remove %s: %s\n"),
1495 progname, fullname, e);
1496 (void) exit(EXIT_FAILURE);
1498 if ((fp = fopen(fullname, "wb")) == NULL) {
1499 if (mkdirs(fullname) != 0)
1500 (void) exit(EXIT_FAILURE);
1501 if ((fp = fopen(fullname, "wb")) == NULL) {
1502 const char *e = strerror(errno);
1504 (void) fprintf(stderr, _("%s: Can't create %s: %s\n"),
1505 progname, fullname, e);
1506 (void) exit(EXIT_FAILURE);
1509 convert(eitol(typecnt), tzh.tzh_ttisgmtcnt);
1510 convert(eitol(typecnt), tzh.tzh_ttisstdcnt);
1511 convert(eitol(leapcnt), tzh.tzh_leapcnt);
1512 convert(eitol(timecnt), tzh.tzh_timecnt);
1513 convert(eitol(typecnt), tzh.tzh_typecnt);
1514 convert(eitol(charcnt), tzh.tzh_charcnt);
1515 (void) strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1516 #define DO(field) (void) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp)
1517 DO(tzh_magic);
1518 DO(tzh_reserved);
1519 DO(tzh_ttisgmtcnt);
1520 DO(tzh_ttisstdcnt);
1521 DO(tzh_leapcnt);
1522 DO(tzh_timecnt);
1523 DO(tzh_typecnt);
1524 DO(tzh_charcnt);
1525 #undef DO
1526 for (i = 0; i < timecnt; ++i) {
1527 j = leapcnt;
1528 while (--j >= 0)
1529 if (ats[i] >= trans[j]) {
1530 ats[i] = tadd(ats[i], corr[j]);
1531 break;
1533 puttzcode((long) ats[i], fp);
1535 if (timecnt > 0)
1536 (void) fwrite((void *) types, (size_t) sizeof types[0],
1537 (size_t) timecnt, fp);
1538 for (i = 0; i < typecnt; ++i) {
1539 puttzcode((long) gmtoffs[i], fp);
1540 (void) putc(isdsts[i], fp);
1541 (void) putc(abbrinds[i], fp);
1543 if (charcnt != 0)
1544 (void) fwrite((void *) chars, (size_t) sizeof chars[0],
1545 (size_t) charcnt, fp);
1546 for (i = 0; i < leapcnt; ++i) {
1547 if (roll[i]) {
1548 if (timecnt == 0 || trans[i] < ats[0]) {
1549 j = 0;
1550 while (isdsts[j])
1551 if (++j >= typecnt) {
1552 j = 0;
1553 break;
1555 } else {
1556 j = 1;
1557 while (j < timecnt && trans[i] >= ats[j])
1558 ++j;
1559 j = types[j - 1];
1561 puttzcode((long) tadd(trans[i], -gmtoffs[j]), fp);
1562 } else puttzcode((long) trans[i], fp);
1563 puttzcode((long) corr[i], fp);
1565 for (i = 0; i < typecnt; ++i)
1566 (void) putc(ttisstds[i], fp);
1567 for (i = 0; i < typecnt; ++i)
1568 (void) putc(ttisgmts[i], fp);
1569 if (ferror(fp) || fclose(fp)) {
1570 (void) fprintf(stderr, _("%s: Error writing %s\n"),
1571 progname, fullname);
1572 (void) exit(EXIT_FAILURE);
1576 static void
1577 doabbr(abbr, format, letters, isdst)
1578 char * const abbr;
1579 const char * const format;
1580 const char * const letters;
1581 const int isdst;
1583 if (strchr(format, '/') == NULL) {
1584 if (letters == NULL)
1585 (void) strcpy(abbr, format);
1586 else (void) sprintf(abbr, format, letters);
1587 } else if (isdst)
1588 (void) strcpy(abbr, strchr(format, '/') + 1);
1589 else {
1590 (void) strcpy(abbr, format);
1591 *strchr(abbr, '/') = '\0';
1595 static void
1596 outzone(zpfirst, zonecount)
1597 const struct zone * const zpfirst;
1598 const int zonecount;
1600 register const struct zone * zp;
1601 register struct rule * rp;
1602 register int i, j;
1603 register int usestart, useuntil;
1604 register time_t starttime, untiltime;
1605 register long gmtoff;
1606 register long stdoff;
1607 register int year;
1608 register long startoff;
1609 register int startttisstd;
1610 register int startttisgmt;
1611 register int type;
1612 char startbuf[BUFSIZ];
1614 INITIALIZE(untiltime);
1615 INITIALIZE(starttime);
1617 ** Now. . .finally. . .generate some useful data!
1619 timecnt = 0;
1620 typecnt = 0;
1621 charcnt = 0;
1623 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1624 ** for noting the need to unconditionally initialize startttisstd.
1626 startttisstd = FALSE;
1627 startttisgmt = FALSE;
1628 for (i = 0; i < zonecount; ++i) {
1630 ** A guess that may well be corrected later.
1632 stdoff = 0;
1633 zp = &zpfirst[i];
1634 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
1635 useuntil = i < (zonecount - 1);
1636 if (useuntil && zp->z_untiltime <= min_time)
1637 continue;
1638 gmtoff = zp->z_gmtoff;
1639 eat(zp->z_filename, zp->z_linenum);
1640 *startbuf = '\0';
1641 startoff = zp->z_gmtoff;
1642 if (zp->z_nrules == 0) {
1643 stdoff = zp->z_stdoff;
1644 doabbr(startbuf, zp->z_format,
1645 (char *) NULL, stdoff != 0);
1646 type = addtype(oadd(zp->z_gmtoff, stdoff),
1647 startbuf, stdoff != 0, startttisstd,
1648 startttisgmt);
1649 if (usestart) {
1650 addtt(starttime, type);
1651 usestart = FALSE;
1652 } else if (stdoff != 0)
1653 addtt(min_time, type);
1654 } else for (year = min_year; year <= max_year; ++year) {
1655 if (useuntil && year > zp->z_untilrule.r_hiyear)
1656 break;
1658 ** Mark which rules to do in the current year.
1659 ** For those to do, calculate rpytime(rp, year);
1661 for (j = 0; j < zp->z_nrules; ++j) {
1662 rp = &zp->z_rules[j];
1663 eats(zp->z_filename, zp->z_linenum,
1664 rp->r_filename, rp->r_linenum);
1665 rp->r_todo = year >= rp->r_loyear &&
1666 year <= rp->r_hiyear &&
1667 yearistype(year, rp->r_yrtype);
1668 if (rp->r_todo)
1669 rp->r_temp = rpytime(rp, year);
1671 for ( ; ; ) {
1672 register int k;
1673 register time_t jtime, ktime;
1674 register long offset;
1675 char buf[BUFSIZ];
1677 INITIALIZE(ktime);
1678 if (useuntil) {
1680 ** Turn untiltime into UTC
1681 ** assuming the current gmtoff and
1682 ** stdoff values.
1684 untiltime = zp->z_untiltime;
1685 if (!zp->z_untilrule.r_todisgmt)
1686 untiltime = tadd(untiltime,
1687 -gmtoff);
1688 if (!zp->z_untilrule.r_todisstd)
1689 untiltime = tadd(untiltime,
1690 -stdoff);
1693 ** Find the rule (of those to do, if any)
1694 ** that takes effect earliest in the year.
1696 k = -1;
1697 for (j = 0; j < zp->z_nrules; ++j) {
1698 rp = &zp->z_rules[j];
1699 if (!rp->r_todo)
1700 continue;
1701 eats(zp->z_filename, zp->z_linenum,
1702 rp->r_filename, rp->r_linenum);
1703 offset = rp->r_todisgmt ? 0 : gmtoff;
1704 if (!rp->r_todisstd)
1705 offset = oadd(offset, stdoff);
1706 jtime = rp->r_temp;
1707 if (jtime == min_time ||
1708 jtime == max_time)
1709 continue;
1710 jtime = tadd(jtime, -offset);
1711 if (k < 0 || jtime < ktime) {
1712 k = j;
1713 ktime = jtime;
1716 if (k < 0)
1717 break; /* go on to next year */
1718 rp = &zp->z_rules[k];
1719 rp->r_todo = FALSE;
1720 if (useuntil && ktime >= untiltime)
1721 break;
1722 stdoff = rp->r_stdoff;
1723 if (usestart && ktime == starttime)
1724 usestart = FALSE;
1725 if (usestart) {
1726 if (ktime < starttime) {
1727 startoff = oadd(zp->z_gmtoff,
1728 stdoff);
1729 doabbr(startbuf, zp->z_format,
1730 rp->r_abbrvar,
1731 rp->r_stdoff != 0);
1732 continue;
1734 if (*startbuf == '\0' &&
1735 startoff == oadd(zp->z_gmtoff,
1736 stdoff)) {
1737 doabbr(startbuf, zp->z_format,
1738 rp->r_abbrvar,
1739 rp->r_stdoff != 0);
1742 eats(zp->z_filename, zp->z_linenum,
1743 rp->r_filename, rp->r_linenum);
1744 doabbr(buf, zp->z_format, rp->r_abbrvar,
1745 rp->r_stdoff != 0);
1746 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
1747 type = addtype(offset, buf, rp->r_stdoff != 0,
1748 rp->r_todisstd, rp->r_todisgmt);
1749 addtt(ktime, type);
1752 if (usestart) {
1753 if (*startbuf == '\0' &&
1754 zp->z_format != NULL &&
1755 strchr(zp->z_format, '%') == NULL &&
1756 strchr(zp->z_format, '/') == NULL)
1757 (void) strcpy(startbuf, zp->z_format);
1758 eat(zp->z_filename, zp->z_linenum);
1759 if (*startbuf == '\0')
1760 error(_("can't determine time zone abbreviation to use just after until time"));
1761 else addtt(starttime,
1762 addtype(startoff, startbuf,
1763 startoff != zp->z_gmtoff,
1764 startttisstd,
1765 startttisgmt));
1768 ** Now we may get to set starttime for the next zone line.
1770 if (useuntil) {
1771 startttisstd = zp->z_untilrule.r_todisstd;
1772 startttisgmt = zp->z_untilrule.r_todisgmt;
1773 starttime = zp->z_untiltime;
1774 if (!startttisstd)
1775 starttime = tadd(starttime, -stdoff);
1776 if (!startttisgmt)
1777 starttime = tadd(starttime, -gmtoff);
1780 writezone(zpfirst->z_name);
1783 static void
1784 addtt(starttime, type)
1785 const time_t starttime;
1786 int type;
1788 if (starttime <= min_time ||
1789 (timecnt == 1 && attypes[0].at < min_time)) {
1790 gmtoffs[0] = gmtoffs[type];
1791 isdsts[0] = isdsts[type];
1792 ttisstds[0] = ttisstds[type];
1793 ttisgmts[0] = ttisgmts[type];
1794 if (abbrinds[type] != 0)
1795 (void) strcpy(chars, &chars[abbrinds[type]]);
1796 abbrinds[0] = 0;
1797 charcnt = strlen(chars) + 1;
1798 typecnt = 1;
1799 timecnt = 0;
1800 type = 0;
1802 if (timecnt >= TZ_MAX_TIMES) {
1803 error(_("too many transitions?!"));
1804 (void) exit(EXIT_FAILURE);
1806 attypes[timecnt].at = starttime;
1807 attypes[timecnt].type = type;
1808 ++timecnt;
1811 static int
1812 addtype(gmtoff, abbr, isdst, ttisstd, ttisgmt)
1813 const long gmtoff;
1814 const char * const abbr;
1815 const int isdst;
1816 const int ttisstd;
1817 const int ttisgmt;
1819 register int i, j;
1821 if (isdst != TRUE && isdst != FALSE) {
1822 error(_("internal error - addtype called with bad isdst"));
1823 (void) exit(EXIT_FAILURE);
1825 if (ttisstd != TRUE && ttisstd != FALSE) {
1826 error(_("internal error - addtype called with bad ttisstd"));
1827 (void) exit(EXIT_FAILURE);
1829 if (ttisgmt != TRUE && ttisgmt != FALSE) {
1830 error(_("internal error - addtype called with bad ttisgmt"));
1831 (void) exit(EXIT_FAILURE);
1834 ** See if there's already an entry for this zone type.
1835 ** If so, just return its index.
1837 for (i = 0; i < typecnt; ++i) {
1838 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
1839 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
1840 ttisstd == ttisstds[i] &&
1841 ttisgmt == ttisgmts[i])
1842 return i;
1845 ** There isn't one; add a new one, unless there are already too
1846 ** many.
1848 if (typecnt >= TZ_MAX_TYPES) {
1849 error(_("too many local time types"));
1850 (void) exit(EXIT_FAILURE);
1852 gmtoffs[i] = gmtoff;
1853 isdsts[i] = isdst;
1854 ttisstds[i] = ttisstd;
1855 ttisgmts[i] = ttisgmt;
1857 for (j = 0; j < charcnt; ++j)
1858 if (strcmp(&chars[j], abbr) == 0)
1859 break;
1860 if (j == charcnt)
1861 newabbr(abbr);
1862 abbrinds[i] = j;
1863 ++typecnt;
1864 return i;
1867 static void
1868 leapadd(t, positive, rolling, count)
1869 const time_t t;
1870 const int positive;
1871 const int rolling;
1872 int count;
1874 register int i, j;
1876 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
1877 error(_("too many leap seconds"));
1878 (void) exit(EXIT_FAILURE);
1880 for (i = 0; i < leapcnt; ++i)
1881 if (t <= trans[i]) {
1882 if (t == trans[i]) {
1883 error(_("repeated leap second moment"));
1884 (void) exit(EXIT_FAILURE);
1886 break;
1888 do {
1889 for (j = leapcnt; j > i; --j) {
1890 trans[j] = trans[j - 1];
1891 corr[j] = corr[j - 1];
1892 roll[j] = roll[j - 1];
1894 trans[i] = t;
1895 corr[i] = positive ? 1L : eitol(-count);
1896 roll[i] = rolling;
1897 ++leapcnt;
1898 } while (positive && --count != 0);
1901 static void
1902 adjleap P((void))
1904 register int i;
1905 register long last = 0;
1908 ** propagate leap seconds forward
1910 for (i = 0; i < leapcnt; ++i) {
1911 trans[i] = tadd(trans[i], last);
1912 last = corr[i] += last;
1916 static int
1917 yearistype(year, type)
1918 const int year;
1919 const char * const type;
1921 static char * buf;
1922 int result;
1924 if (type == NULL || *type == '\0')
1925 return TRUE;
1926 buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
1927 (void) sprintf(buf, "%s %d %s", yitcommand, year, type);
1928 result = system(buf);
1929 if (WIFEXITED(result)) switch (WEXITSTATUS(result)) {
1930 case 0:
1931 return TRUE;
1932 case 1:
1933 return FALSE;
1935 error(_("Wild result from command execution"));
1936 (void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
1937 progname, buf, result);
1938 for ( ; ; )
1939 (void) exit(EXIT_FAILURE);
1942 static int
1943 lowerit(a)
1944 int a;
1946 a = (unsigned char) a;
1947 return (isascii(a) && isupper(a)) ? tolower(a) : a;
1950 static int
1951 ciequal(ap, bp) /* case-insensitive equality */
1952 register const char * ap;
1953 register const char * bp;
1955 while (lowerit(*ap) == lowerit(*bp++))
1956 if (*ap++ == '\0')
1957 return TRUE;
1958 return FALSE;
1961 static int
1962 itsabbr(abbr, word)
1963 register const char * abbr;
1964 register const char * word;
1966 if (lowerit(*abbr) != lowerit(*word))
1967 return FALSE;
1968 ++word;
1969 while (*++abbr != '\0')
1970 do {
1971 if (*word == '\0')
1972 return FALSE;
1973 } while (lowerit(*word++) != lowerit(*abbr));
1974 return TRUE;
1977 static const struct lookup *
1978 byword(word, table)
1979 register const char * const word;
1980 register const struct lookup * const table;
1982 register const struct lookup * foundlp;
1983 register const struct lookup * lp;
1985 if (word == NULL || table == NULL)
1986 return NULL;
1988 ** Look for exact match.
1990 for (lp = table; lp->l_word != NULL; ++lp)
1991 if (ciequal(word, lp->l_word))
1992 return lp;
1994 ** Look for inexact match.
1996 foundlp = NULL;
1997 for (lp = table; lp->l_word != NULL; ++lp)
1998 if (itsabbr(word, lp->l_word)) {
1999 if (foundlp == NULL)
2000 foundlp = lp;
2001 else return NULL; /* multiple inexact matches */
2003 return foundlp;
2006 static char **
2007 getfields(cp)
2008 register char * cp;
2010 register char * dp;
2011 register char ** array;
2012 register int nsubs;
2014 if (cp == NULL)
2015 return NULL;
2016 array = (char **) (void *)
2017 emalloc((int) ((strlen(cp) + 1) * sizeof *array));
2018 nsubs = 0;
2019 for ( ; ; ) {
2020 while (isascii(*cp) && isspace((unsigned char) *cp))
2021 ++cp;
2022 if (*cp == '\0' || *cp == '#')
2023 break;
2024 array[nsubs++] = dp = cp;
2025 do {
2026 if ((*dp = *cp++) != '"')
2027 ++dp;
2028 else while ((*dp = *cp++) != '"')
2029 if (*dp != '\0')
2030 ++dp;
2031 else error(_("Odd number of quotation marks"));
2032 } while (*cp != '\0' && *cp != '#' &&
2033 (!isascii(*cp) || !isspace((unsigned char) *cp)));
2034 if (isascii(*cp) && isspace((unsigned char) *cp))
2035 ++cp;
2036 *dp = '\0';
2038 array[nsubs] = NULL;
2039 return array;
2042 static long
2043 oadd(t1, t2)
2044 const long t1;
2045 const long t2;
2047 register long t;
2049 t = t1 + t2;
2050 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2051 error(_("time overflow"));
2052 (void) exit(EXIT_FAILURE);
2054 return t;
2057 static time_t
2058 tadd(t1, t2)
2059 const time_t t1;
2060 const long t2;
2062 register time_t t;
2064 if (t1 == max_time && t2 > 0)
2065 return max_time;
2066 if (t1 == min_time && t2 < 0)
2067 return min_time;
2068 t = t1 + t2;
2069 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2070 error(_("time overflow"));
2071 (void) exit(EXIT_FAILURE);
2073 return t;
2077 ** Given a rule, and a year, compute the date - in seconds since January 1,
2078 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2081 static time_t
2082 rpytime(rp, wantedy)
2083 register const struct rule * const rp;
2084 register const int wantedy;
2086 register int y, m, i;
2087 register long dayoff; /* with a nod to Margaret O. */
2088 register time_t t;
2090 if (wantedy == INT_MIN)
2091 return min_time;
2092 if (wantedy == INT_MAX)
2093 return max_time;
2094 dayoff = 0;
2095 m = TM_JANUARY;
2096 y = EPOCH_YEAR;
2097 while (wantedy != y) {
2098 if (wantedy > y) {
2099 i = len_years[isleap(y)];
2100 ++y;
2101 } else {
2102 --y;
2103 i = -len_years[isleap(y)];
2105 dayoff = oadd(dayoff, eitol(i));
2107 while (m != rp->r_month) {
2108 i = len_months[isleap(y)][m];
2109 dayoff = oadd(dayoff, eitol(i));
2110 ++m;
2112 i = rp->r_dayofmonth;
2113 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
2114 if (rp->r_dycode == DC_DOWLEQ)
2115 --i;
2116 else {
2117 error(_("use of 2/29 in non leap-year"));
2118 (void) exit(EXIT_FAILURE);
2121 --i;
2122 dayoff = oadd(dayoff, eitol(i));
2123 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
2124 register long wday;
2126 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2127 wday = eitol(EPOCH_WDAY);
2129 ** Don't trust mod of negative numbers.
2131 if (dayoff >= 0)
2132 wday = (wday + dayoff) % LDAYSPERWEEK;
2133 else {
2134 wday -= ((-dayoff) % LDAYSPERWEEK);
2135 if (wday < 0)
2136 wday += LDAYSPERWEEK;
2138 while (wday != eitol(rp->r_wday))
2139 if (rp->r_dycode == DC_DOWGEQ) {
2140 dayoff = oadd(dayoff, (long) 1);
2141 if (++wday >= LDAYSPERWEEK)
2142 wday = 0;
2143 ++i;
2144 } else {
2145 dayoff = oadd(dayoff, (long) -1);
2146 if (--wday < 0)
2147 wday = LDAYSPERWEEK - 1;
2148 --i;
2150 if (i < 0 || i >= len_months[isleap(y)][m]) {
2151 error(_("no day in month matches rule"));
2152 (void) exit(EXIT_FAILURE);
2155 if (dayoff < 0 && !TYPE_SIGNED(time_t))
2156 return min_time;
2157 if (dayoff < min_time / SECSPERDAY)
2158 return min_time;
2159 if (dayoff > max_time / SECSPERDAY)
2160 return max_time;
2161 t = (time_t) dayoff * SECSPERDAY;
2162 if (t > 0 && max_time - t < rp->r_tod)
2163 return max_time;
2164 return tadd(t, rp->r_tod);
2167 static void
2168 newabbr(string)
2169 const char * const string;
2171 register int i;
2173 i = strlen(string) + 1;
2174 if (charcnt + i > TZ_MAX_CHARS) {
2175 error(_("too many, or too long, time zone abbreviations"));
2176 (void) exit(EXIT_FAILURE);
2178 (void) strcpy(&chars[charcnt], string);
2179 charcnt += eitol(i);
2182 static int
2183 mkdirs(argname)
2184 char * const argname;
2186 register char * name;
2187 register char * cp;
2189 if (argname == NULL || *argname == '\0')
2190 return 0;
2191 cp = name = ecpyalloc(argname);
2192 while ((cp = strchr(cp + 1, '/')) != 0) {
2193 *cp = '\0';
2194 #ifndef unix
2196 ** DOS drive specifier?
2198 if (isalpha((unsigned char) name[0]) &&
2199 name[1] == ':' && name[2] == '\0') {
2200 *cp = '/';
2201 continue;
2203 #endif /* !defined unix */
2204 if (!itsdir(name)) {
2206 ** It doesn't seem to exist, so we try to create it.
2207 ** Creation may fail because of the directory being
2208 ** created by some other multiprocessor, so we get
2209 ** to do extra checking.
2211 if (mkdir(name, MKDIR_UMASK) != 0) {
2212 const char *e = strerror(errno);
2214 if (errno != EEXIST || !itsdir(name)) {
2215 (void) fprintf(stderr,
2216 _("%s: Can't create directory %s: %s\n"),
2217 progname, name, e);
2218 ifree(name);
2219 return -1;
2223 *cp = '/';
2225 ifree(name);
2226 return 0;
2229 static long
2230 eitol(i)
2231 const int i;
2233 long l;
2235 l = i;
2236 if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0)) {
2237 (void) fprintf(stderr,
2238 _("%s: %d did not sign extend correctly\n"),
2239 progname, i);
2240 (void) exit(EXIT_FAILURE);
2242 return l;
2246 ** UNIX was a registered trademark of UNIX System Laboratories in 1993.