Sun May 12 22:52:22 1996 Bruce Elliott <belliott@accessone.com>
[glibc.git] / time / zic.c
blob756b7eb84a3d3ba66362553829b6f2d68cc1caa7
1 #ifndef lint
2 #ifndef NOID
3 static char elsieid[] = "@(#)zic.c 7.62";
4 #endif /* !defined NOID */
5 #endif /* !defined lint */
7 #include "private.h"
8 #include "locale.h"
9 #include "tzfile.h"
10 #ifdef unix
11 #include "sys/stat.h" /* for umask manifest constants */
12 #endif /* defined unix */
15 ** On some ancient hosts, predicates like `isspace(C)' are defined
16 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
17 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
18 ** Neither the C Standard nor Posix require that `isascii' exist.
19 ** For portability, we check both ancient and modern requirements.
20 ** If isascii is not defined, the isascii check succeeds trivially.
22 #include "ctype.h"
23 #ifndef isascii
24 #define isascii(x) 1
25 #endif
27 struct rule {
28 const char * r_filename;
29 int r_linenum;
30 const char * r_name;
32 int r_loyear; /* for example, 1986 */
33 int r_hiyear; /* for example, 1986 */
34 const char * r_yrtype;
36 int r_month; /* 0..11 */
38 int r_dycode; /* see below */
39 int r_dayofmonth;
40 int r_wday;
42 long r_tod; /* time from midnight */
43 int r_todisstd; /* above is standard time if TRUE */
44 /* or wall clock time if FALSE */
45 int r_todisgmt; /* above is GMT if TRUE */
46 /* or local time if FALSE */
47 long r_stdoff; /* offset from standard time */
48 const char * r_abbrvar; /* variable part of abbreviation */
50 int r_todo; /* a rule to do (used in outzone) */
51 time_t r_temp; /* used in outzone */
55 ** r_dycode r_dayofmonth r_wday
58 #define DC_DOM 0 /* 1..31 */ /* unused */
59 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
60 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
62 struct zone {
63 const char * z_filename;
64 int z_linenum;
66 const char * z_name;
67 long z_gmtoff;
68 const char * z_rule;
69 const char * z_format;
71 long z_stdoff;
73 struct rule * z_rules;
74 int z_nrules;
76 struct rule z_untilrule;
77 time_t z_untiltime;
80 extern int getopt P((int argc, char * const argv[],
81 const char * options));
82 extern char * icatalloc P((char * old, const char * new));
83 extern char * icpyalloc P((const char * string));
84 extern void ifree P((char * p));
85 extern char * imalloc P((int n));
86 extern void * irealloc P((void * old, int n));
87 extern int link P((const char * fromname, const char * toname));
88 extern char * optarg;
89 extern int optind;
90 extern char * scheck P((const char * string, const char * format));
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
140 static char * strerror P((int));
141 #endif /* !HAVE_STRERROR */
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 time_t min_time;
151 static int min_year;
152 static int noise;
153 static const char * rfilename;
154 static int rlinenum;
155 static const char * progname;
156 static int timecnt;
157 static int typecnt;
160 ** Line codes.
163 #define LC_RULE 0
164 #define LC_ZONE 1
165 #define LC_LINK 2
166 #define LC_LEAP 3
169 ** Which fields are which on a Zone line.
172 #define ZF_NAME 1
173 #define ZF_GMTOFF 2
174 #define ZF_RULE 3
175 #define ZF_FORMAT 4
176 #define ZF_TILYEAR 5
177 #define ZF_TILMONTH 6
178 #define ZF_TILDAY 7
179 #define ZF_TILTIME 8
180 #define ZONE_MINFIELDS 5
181 #define ZONE_MAXFIELDS 9
184 ** Which fields are which on a Zone continuation line.
187 #define ZFC_GMTOFF 0
188 #define ZFC_RULE 1
189 #define ZFC_FORMAT 2
190 #define ZFC_TILYEAR 3
191 #define ZFC_TILMONTH 4
192 #define ZFC_TILDAY 5
193 #define ZFC_TILTIME 6
194 #define ZONEC_MINFIELDS 3
195 #define ZONEC_MAXFIELDS 7
198 ** Which files are which on a Rule line.
201 #define RF_NAME 1
202 #define RF_LOYEAR 2
203 #define RF_HIYEAR 3
204 #define RF_COMMAND 4
205 #define RF_MONTH 5
206 #define RF_DAY 6
207 #define RF_TOD 7
208 #define RF_STDOFF 8
209 #define RF_ABBRVAR 9
210 #define RULE_FIELDS 10
213 ** Which fields are which on a Link line.
216 #define LF_FROM 1
217 #define LF_TO 2
218 #define LINK_FIELDS 3
221 ** Which fields are which on a Leap line.
224 #define LP_YEAR 1
225 #define LP_MONTH 2
226 #define LP_DAY 3
227 #define LP_TIME 4
228 #define LP_CORR 5
229 #define LP_ROLL 6
230 #define LEAP_FIELDS 7
233 ** Year synonyms.
236 #define YR_MINIMUM 0
237 #define YR_MAXIMUM 1
238 #define YR_ONLY 2
240 static struct rule * rules;
241 static int nrules; /* number of rules */
243 static struct zone * zones;
244 static int nzones; /* number of zones */
246 struct link {
247 const char * l_filename;
248 int l_linenum;
249 const char * l_from;
250 const char * l_to;
253 static struct link * links;
254 static int nlinks;
256 struct lookup {
257 const char * l_word;
258 const int l_value;
261 static struct lookup const * byword P((const char * string,
262 const struct lookup * lp));
264 static struct lookup const line_codes[] = {
265 { "Rule", LC_RULE },
266 { "Zone", LC_ZONE },
267 { "Link", LC_LINK },
268 { "Leap", LC_LEAP },
269 { NULL, 0}
272 static struct lookup const mon_names[] = {
273 { "January", TM_JANUARY },
274 { "February", TM_FEBRUARY },
275 { "March", TM_MARCH },
276 { "April", TM_APRIL },
277 { "May", TM_MAY },
278 { "June", TM_JUNE },
279 { "July", TM_JULY },
280 { "August", TM_AUGUST },
281 { "September", TM_SEPTEMBER },
282 { "October", TM_OCTOBER },
283 { "November", TM_NOVEMBER },
284 { "December", TM_DECEMBER },
285 { NULL, 0 }
288 static struct lookup const wday_names[] = {
289 { "Sunday", TM_SUNDAY },
290 { "Monday", TM_MONDAY },
291 { "Tuesday", TM_TUESDAY },
292 { "Wednesday", TM_WEDNESDAY },
293 { "Thursday", TM_THURSDAY },
294 { "Friday", TM_FRIDAY },
295 { "Saturday", TM_SATURDAY },
296 { NULL, 0 }
299 static struct lookup const lasts[] = {
300 { "last-Sunday", TM_SUNDAY },
301 { "last-Monday", TM_MONDAY },
302 { "last-Tuesday", TM_TUESDAY },
303 { "last-Wednesday", TM_WEDNESDAY },
304 { "last-Thursday", TM_THURSDAY },
305 { "last-Friday", TM_FRIDAY },
306 { "last-Saturday", TM_SATURDAY },
307 { NULL, 0 }
310 static struct lookup const begin_years[] = {
311 { "minimum", YR_MINIMUM },
312 { "maximum", YR_MAXIMUM },
313 { NULL, 0 }
316 static struct lookup const end_years[] = {
317 { "minimum", YR_MINIMUM },
318 { "maximum", YR_MAXIMUM },
319 { "only", YR_ONLY },
320 { NULL, 0 }
323 static struct lookup const leap_types[] = {
324 { "Rolling", TRUE },
325 { "Stationary", FALSE },
326 { NULL, 0 }
329 static const int len_months[2][MONSPERYEAR] = {
330 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
331 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
334 static const int len_years[2] = {
335 DAYSPERNYEAR, DAYSPERLYEAR
338 static time_t ats[TZ_MAX_TIMES];
339 static unsigned char types[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);
360 (void) fprintf(stderr, _("%s: Memory exhausted: %s\n"),
361 progname, e);
362 (void) exit(EXIT_FAILURE);
364 return ptr;
367 #define emalloc(size) memcheck(imalloc(size))
368 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
369 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
370 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
373 ** Error handling.
376 #if ! HAVE_STRERROR
377 static char *
378 strerror(errnum)
379 int errnum;
381 extern char *sys_errlist[];
382 extern int sys_nerr;
384 if (errnum > 0 && errnum <= sys_nerr)
385 return sys_errlist[errnum];
386 return "Unknown system error";
388 #endif /* ! HAVE_STRERROR */
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 usage P((void))
432 (void) fprintf(stderr, _("%s: usage is %s \
433 [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] [ -d directory ]\n\
434 \t[ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
435 progname, progname);
436 (void) exit(EXIT_FAILURE);
439 static const char * psxrules;
440 static const char * lcltime;
441 static const char * directory;
442 static const char * leapsec;
443 static const char * yitcommand;
444 static int sflag = FALSE;
447 main(argc, argv)
448 int argc;
449 char * argv[];
451 register int i;
452 register int j;
453 register int c;
455 #ifdef unix
456 (void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
457 #endif /* defined unix */
458 #if HAVE_GETTEXT - 0
459 (void) setlocale(LC_MESSAGES, "");
460 #ifdef TZ_DOMAINDIR
461 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
462 #endif /* defined TEXTDOMAINDIR */
463 (void) textdomain(TZ_DOMAIN);
464 #endif /* HAVE_GETTEXT - 0 */
465 progname = argv[0];
466 while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF)
467 switch (c) {
468 default:
469 usage();
470 case 'd':
471 if (directory == NULL)
472 directory = optarg;
473 else {
474 (void) fprintf(stderr,
475 _("%s: More than one -d option specified\n"),
476 progname);
477 (void) exit(EXIT_FAILURE);
479 break;
480 case 'l':
481 if (lcltime == NULL)
482 lcltime = optarg;
483 else {
484 (void) fprintf(stderr,
485 _("%s: More than one -l option specified\n"),
486 progname);
487 (void) exit(EXIT_FAILURE);
489 break;
490 case 'p':
491 if (psxrules == NULL)
492 psxrules = optarg;
493 else {
494 (void) fprintf(stderr,
495 _("%s: More than one -p option specified\n"),
496 progname);
497 (void) exit(EXIT_FAILURE);
499 break;
500 case 'y':
501 if (yitcommand == NULL)
502 yitcommand = optarg;
503 else {
504 (void) fprintf(stderr,
505 _("%s: More than one -y option specified\n"),
506 progname);
507 (void) exit(EXIT_FAILURE);
509 break;
510 case 'L':
511 if (leapsec == NULL)
512 leapsec = optarg;
513 else {
514 (void) fprintf(stderr,
515 _("%s: More than one -L option specified\n"),
516 progname);
517 (void) exit(EXIT_FAILURE);
519 break;
520 case 'v':
521 noise = TRUE;
522 break;
523 case 's':
524 sflag = TRUE;
525 break;
527 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
528 usage(); /* usage message by request */
529 if (directory == NULL)
530 directory = TZDIR;
531 if (yitcommand == NULL)
532 yitcommand = "yearistype";
534 setboundaries();
536 if (optind < argc && leapsec != NULL) {
537 infile(leapsec);
538 adjleap();
541 for (i = optind; i < argc; ++i)
542 infile(argv[i]);
543 if (errors)
544 (void) exit(EXIT_FAILURE);
545 associate();
546 for (i = 0; i < nzones; i = j) {
548 ** Find the next non-continuation zone entry.
550 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
551 continue;
552 outzone(&zones[i], j - i);
555 ** Make links.
557 for (i = 0; i < nlinks; ++i)
558 dolink(links[i].l_from, links[i].l_to);
559 if (lcltime != NULL)
560 dolink(lcltime, TZDEFAULT);
561 if (psxrules != NULL)
562 dolink(psxrules, TZDEFRULES);
563 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
566 static void
567 dolink(fromfile, tofile)
568 const char * const fromfile;
569 const char * const tofile;
571 register char * fromname;
572 register char * toname;
574 if (fromfile[0] == '/')
575 fromname = ecpyalloc(fromfile);
576 else {
577 fromname = ecpyalloc(directory);
578 fromname = ecatalloc(fromname, "/");
579 fromname = ecatalloc(fromname, fromfile);
581 if (tofile[0] == '/')
582 toname = ecpyalloc(tofile);
583 else {
584 toname = ecpyalloc(directory);
585 toname = ecatalloc(toname, "/");
586 toname = ecatalloc(toname, tofile);
589 ** We get to be careful here since
590 ** there's a fair chance of root running us.
592 if (!itsdir(toname))
593 (void) remove(toname);
594 if (link(fromname, toname) != 0) {
595 if (mkdirs(toname) != 0)
596 (void) exit(EXIT_FAILURE);
597 if (link(fromname, toname) != 0) {
598 const char *e = strerror(errno);
599 (void) fprintf(stderr,
600 _("%s: Can't link from %s to %s: %s\n"),
601 progname, fromname, toname, e);
602 (void) exit(EXIT_FAILURE);
605 ifree(fromname);
606 ifree(toname);
609 #ifndef INT_MAX
610 #define INT_MAX ((int) (((unsigned)~0)>>1))
611 #endif /* !defined INT_MAX */
613 #ifndef INT_MIN
614 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
615 #endif /* !defined INT_MIN */
618 ** The tz file format currently allows at most 32-bit quantities.
619 ** This restriction should be removed before signed 32-bit values
620 ** wrap around in 2038, but unfortunately this will require a
621 ** change to the tz file format.
624 #define MAX_BITS_IN_FILE 32
625 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(time_t) < MAX_BITS_IN_FILE) ? \
626 TYPE_BIT(time_t) : MAX_BITS_IN_FILE)
628 static void
629 setboundaries P((void))
631 if (TYPE_SIGNED(time_t)) {
632 min_time = ~ (time_t) 0;
633 min_time <<= TIME_T_BITS_IN_FILE - 1;
634 max_time = ~ (time_t) 0 - min_time;
635 if (sflag)
636 min_time = 0;
637 } else {
638 min_time = 0;
639 max_time = 2 - sflag;
640 max_time <<= TIME_T_BITS_IN_FILE - 1;
641 --max_time;
643 min_year = TM_YEAR_BASE + gmtime(&min_time)->tm_year;
644 max_year = TM_YEAR_BASE + gmtime(&max_time)->tm_year;
647 static int
648 itsdir(name)
649 const char * const name;
651 register char * myname;
652 register int accres;
654 myname = ecpyalloc(name);
655 myname = ecatalloc(myname, "/.");
656 accres = access(myname, F_OK);
657 ifree(myname);
658 return accres == 0;
662 ** Associate sets of rules with zones.
666 ** Sort by rule name.
669 static int
670 rcomp(cp1, cp2)
671 const void * cp1;
672 const void * cp2;
674 return strcmp(((const struct rule *) cp1)->r_name,
675 ((const struct rule *) cp2)->r_name);
678 static void
679 associate P((void))
681 register struct zone * zp;
682 register struct rule * rp;
683 register int base, out;
684 register int i;
686 if (nrules != 0)
687 (void) qsort((void *) rules, (size_t) nrules,
688 (size_t) sizeof *rules, rcomp);
689 for (i = 0; i < nzones; ++i) {
690 zp = &zones[i];
691 zp->z_rules = NULL;
692 zp->z_nrules = 0;
694 for (base = 0; base < nrules; base = out) {
695 rp = &rules[base];
696 for (out = base + 1; out < nrules; ++out)
697 if (strcmp(rp->r_name, rules[out].r_name) != 0)
698 break;
699 for (i = 0; i < nzones; ++i) {
700 zp = &zones[i];
701 if (strcmp(zp->z_rule, rp->r_name) != 0)
702 continue;
703 zp->z_rules = rp;
704 zp->z_nrules = out - base;
707 for (i = 0; i < nzones; ++i) {
708 zp = &zones[i];
709 if (zp->z_nrules == 0) {
711 ** Maybe we have a local standard time offset.
713 eat(zp->z_filename, zp->z_linenum);
714 zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
715 TRUE);
717 ** Note, though, that if there's no rule,
718 ** a '%s' in the format is a bad thing.
720 if (strchr(zp->z_format, '%') != 0)
721 error(_("%s in ruleless zone"));
724 if (errors)
725 (void) exit(EXIT_FAILURE);
728 static void
729 infile(name)
730 const char * name;
732 register FILE * fp;
733 register char ** fields;
734 register char * cp;
735 register const struct lookup * lp;
736 register int nfields;
737 register int wantcont;
738 register int num;
739 char buf[BUFSIZ];
741 if (strcmp(name, "-") == 0) {
742 name = _("standard input");
743 fp = stdin;
744 } else if ((fp = fopen(name, "r")) == NULL) {
745 const char *e = strerror(errno);
746 (void) fprintf(stderr, _("%s: Can't open %s: %s\n"),
747 progname, name, e);
748 (void) exit(EXIT_FAILURE);
750 wantcont = FALSE;
751 for (num = 1; ; ++num) {
752 eat(name, num);
753 if (fgets(buf, (int) sizeof buf, fp) != buf)
754 break;
755 cp = strchr(buf, '\n');
756 if (cp == NULL) {
757 error(_("line too long"));
758 (void) exit(EXIT_FAILURE);
760 *cp = '\0';
761 fields = getfields(buf);
762 nfields = 0;
763 while (fields[nfields] != NULL) {
764 static char nada;
766 if (strcmp(fields[nfields], "-") == 0)
767 fields[nfields] = &nada;
768 ++nfields;
770 if (nfields == 0) {
771 /* nothing to do */
772 } else if (wantcont) {
773 wantcont = inzcont(fields, nfields);
774 } else {
775 lp = byword(fields[0], line_codes);
776 if (lp == NULL)
777 error(_("input line of unknown type"));
778 else switch ((int) (lp->l_value)) {
779 case LC_RULE:
780 inrule(fields, nfields);
781 wantcont = FALSE;
782 break;
783 case LC_ZONE:
784 wantcont = inzone(fields, nfields);
785 break;
786 case LC_LINK:
787 inlink(fields, nfields);
788 wantcont = FALSE;
789 break;
790 case LC_LEAP:
791 if (name != leapsec)
792 (void) fprintf(stderr,
793 _("%s: Leap line in non leap seconds file %s\n"),
794 progname, name);
795 else inleap(fields, nfields);
796 wantcont = FALSE;
797 break;
798 default: /* "cannot happen" */
799 (void) fprintf(stderr,
800 _("%s: panic: Invalid l_value %d\n"),
801 progname, lp->l_value);
802 (void) exit(EXIT_FAILURE);
805 ifree((char *) fields);
807 if (ferror(fp)) {
808 (void) fprintf(stderr, _("%s: Error reading %s\n"),
809 progname, filename);
810 (void) exit(EXIT_FAILURE);
812 if (fp != stdin && fclose(fp)) {
813 const char *e = strerror(errno);
814 (void) fprintf(stderr, _("%s: Error closing %s: %s\n"),
815 progname, filename, e);
816 (void) exit(EXIT_FAILURE);
818 if (wantcont)
819 error(_("expected continuation line not found"));
823 ** Convert a string of one of the forms
824 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
825 ** into a number of seconds.
826 ** A null string maps to zero.
827 ** Call error with errstring and return zero on errors.
830 static long
831 gethms(string, errstring, signable)
832 const char * string;
833 const char * const errstring;
834 const int signable;
836 int hh, mm, ss, sign;
838 if (string == NULL || *string == '\0')
839 return 0;
840 if (!signable)
841 sign = 1;
842 else if (*string == '-') {
843 sign = -1;
844 ++string;
845 } else sign = 1;
846 if (sscanf(string, scheck(string, "%d"), &hh) == 1)
847 mm = ss = 0;
848 else if (sscanf(string, scheck(string, "%d:%d"), &hh, &mm) == 2)
849 ss = 0;
850 else if (sscanf(string, scheck(string, "%d:%d:%d"),
851 &hh, &mm, &ss) != 3) {
852 error(errstring);
853 return 0;
855 if (hh < 0 || hh >= HOURSPERDAY ||
856 mm < 0 || mm >= MINSPERHOUR ||
857 ss < 0 || ss > SECSPERMIN) {
858 error(errstring);
859 return 0;
861 return eitol(sign) *
862 (eitol(hh * MINSPERHOUR + mm) *
863 eitol(SECSPERMIN) + eitol(ss));
866 static void
867 inrule(fields, nfields)
868 register char ** const fields;
869 const int nfields;
871 static struct rule r;
873 if (nfields != RULE_FIELDS) {
874 error(_("wrong number of fields on Rule line"));
875 return;
877 if (*fields[RF_NAME] == '\0') {
878 error(_("nameless rule"));
879 return;
881 r.r_filename = filename;
882 r.r_linenum = linenum;
883 r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
884 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
885 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
886 r.r_name = ecpyalloc(fields[RF_NAME]);
887 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
888 rules = (struct rule *) (void *) erealloc((char *) rules,
889 (int) ((nrules + 1) * sizeof *rules));
890 rules[nrules++] = r;
893 static int
894 inzone(fields, nfields)
895 register char ** const fields;
896 const int nfields;
898 register int i;
899 static char * buf;
901 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
902 error(_("wrong number of fields on Zone line"));
903 return FALSE;
905 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
906 buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
907 (void) sprintf(buf,
908 _("\"Zone %s\" line and -l option are mutually exclusive"),
909 TZDEFAULT);
910 error(buf);
911 return FALSE;
913 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
914 buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
915 (void) sprintf(buf,
916 _("\"Zone %s\" line and -p option are mutually exclusive"),
917 TZDEFRULES);
918 error(buf);
919 return FALSE;
921 for (i = 0; i < nzones; ++i)
922 if (zones[i].z_name != NULL &&
923 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
924 buf = erealloc(buf, (int) (132 +
925 strlen(fields[ZF_NAME]) +
926 strlen(zones[i].z_filename)));
927 (void) sprintf(buf,
928 _("duplicate zone name %s (file \"%s\", line %d)"),
929 fields[ZF_NAME],
930 zones[i].z_filename,
931 zones[i].z_linenum);
932 error(buf);
933 return FALSE;
935 return inzsub(fields, nfields, FALSE);
938 static int
939 inzcont(fields, nfields)
940 register char ** const fields;
941 const int nfields;
943 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
944 error(_("wrong number of fields on Zone continuation line"));
945 return FALSE;
947 return inzsub(fields, nfields, TRUE);
950 static int
951 inzsub(fields, nfields, iscont)
952 register char ** const fields;
953 const int nfields;
954 const int iscont;
956 register char * cp;
957 static struct zone z;
958 register int i_gmtoff, i_rule, i_format;
959 register int i_untilyear, i_untilmonth;
960 register int i_untilday, i_untiltime;
961 register int hasuntil;
963 if (iscont) {
964 i_gmtoff = ZFC_GMTOFF;
965 i_rule = ZFC_RULE;
966 i_format = ZFC_FORMAT;
967 i_untilyear = ZFC_TILYEAR;
968 i_untilmonth = ZFC_TILMONTH;
969 i_untilday = ZFC_TILDAY;
970 i_untiltime = ZFC_TILTIME;
971 z.z_name = NULL;
972 } else {
973 i_gmtoff = ZF_GMTOFF;
974 i_rule = ZF_RULE;
975 i_format = ZF_FORMAT;
976 i_untilyear = ZF_TILYEAR;
977 i_untilmonth = ZF_TILMONTH;
978 i_untilday = ZF_TILDAY;
979 i_untiltime = ZF_TILTIME;
980 z.z_name = ecpyalloc(fields[ZF_NAME]);
982 z.z_filename = filename;
983 z.z_linenum = linenum;
984 z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid GMT offset"), TRUE);
985 if ((cp = strchr(fields[i_format], '%')) != 0) {
986 if (*++cp != 's' || strchr(cp, '%') != 0) {
987 error(_("invalid abbreviation format"));
988 return FALSE;
991 z.z_rule = ecpyalloc(fields[i_rule]);
992 z.z_format = ecpyalloc(fields[i_format]);
993 hasuntil = nfields > i_untilyear;
994 if (hasuntil) {
995 z.z_untilrule.r_filename = filename;
996 z.z_untilrule.r_linenum = linenum;
997 rulesub(&z.z_untilrule,
998 fields[i_untilyear],
999 "only",
1001 (nfields > i_untilmonth) ?
1002 fields[i_untilmonth] : "Jan",
1003 (nfields > i_untilday) ? fields[i_untilday] : "1",
1004 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1005 z.z_untiltime = rpytime(&z.z_untilrule,
1006 z.z_untilrule.r_loyear);
1007 if (iscont && nzones > 0 &&
1008 z.z_untiltime > min_time &&
1009 z.z_untiltime < max_time &&
1010 zones[nzones - 1].z_untiltime > min_time &&
1011 zones[nzones - 1].z_untiltime < max_time &&
1012 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1013 error(_("Zone continuation line end time is \
1014 not after end time of previous line"));
1015 return FALSE;
1018 zones = (struct zone *) (void *) erealloc((char *) zones,
1019 (int) ((nzones + 1) * sizeof *zones));
1020 zones[nzones++] = z;
1022 ** If there was an UNTIL field on this line,
1023 ** there's more information about the zone on the next line.
1025 return hasuntil;
1028 static void
1029 inleap(fields, nfields)
1030 register char ** const fields;
1031 const int nfields;
1033 register const char * cp;
1034 register const struct lookup * lp;
1035 register int i, j;
1036 int year, month, day;
1037 long dayoff, tod;
1038 time_t t;
1040 if (nfields != LEAP_FIELDS) {
1041 error(_("wrong number of fields on Leap line"));
1042 return;
1044 dayoff = 0;
1045 cp = fields[LP_YEAR];
1046 if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
1048 * Leapin' Lizards!
1050 error(_("invalid leaping year"));
1051 return;
1053 j = EPOCH_YEAR;
1054 while (j != year) {
1055 if (year > j) {
1056 i = len_years[isleap(j)];
1057 ++j;
1058 } else {
1059 --j;
1060 i = -len_years[isleap(j)];
1062 dayoff = oadd(dayoff, eitol(i));
1064 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1065 error(_("invalid month name"));
1066 return;
1068 month = lp->l_value;
1069 j = TM_JANUARY;
1070 while (j != month) {
1071 i = len_months[isleap(year)][j];
1072 dayoff = oadd(dayoff, eitol(i));
1073 ++j;
1075 cp = fields[LP_DAY];
1076 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1077 day <= 0 || day > len_months[isleap(year)][month]) {
1078 error(_("invalid day of month"));
1079 return;
1081 dayoff = oadd(dayoff, eitol(day - 1));
1082 if (dayoff < 0 && !TYPE_SIGNED(time_t)) {
1083 error(_("time before zero"));
1084 return;
1086 t = (time_t) dayoff * SECSPERDAY;
1088 ** Cheap overflow check.
1090 if (t / SECSPERDAY != dayoff) {
1091 error(_("time overflow"));
1092 return;
1094 tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1095 cp = fields[LP_CORR];
1097 register int positive;
1098 int count;
1100 if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1101 positive = FALSE;
1102 count = 1;
1103 } else if (strcmp(cp, "--") == 0) {
1104 positive = FALSE;
1105 count = 2;
1106 } else if (strcmp(cp, "+") == 0) {
1107 positive = TRUE;
1108 count = 1;
1109 } else if (strcmp(cp, "++") == 0) {
1110 positive = TRUE;
1111 count = 2;
1112 } else {
1113 error(_("illegal CORRECTION field on Leap line"));
1114 return;
1116 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1117 error(_("illegal Rolling/Stationary field on Leap line"));
1118 return;
1120 leapadd(tadd(t, tod), positive, lp->l_value, count);
1124 static void
1125 inlink(fields, nfields)
1126 register char ** const fields;
1127 const int nfields;
1129 struct link l;
1131 if (nfields != LINK_FIELDS) {
1132 error(_("wrong number of fields on Link line"));
1133 return;
1135 if (*fields[LF_FROM] == '\0') {
1136 error(_("blank FROM field on Link line"));
1137 return;
1139 if (*fields[LF_TO] == '\0') {
1140 error(_("blank TO field on Link line"));
1141 return;
1143 l.l_filename = filename;
1144 l.l_linenum = linenum;
1145 l.l_from = ecpyalloc(fields[LF_FROM]);
1146 l.l_to = ecpyalloc(fields[LF_TO]);
1147 links = (struct link *) (void *) erealloc((char *) links,
1148 (int) ((nlinks + 1) * sizeof *links));
1149 links[nlinks++] = l;
1152 static void
1153 rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
1154 register struct rule * const rp;
1155 const char * const loyearp;
1156 const char * const hiyearp;
1157 const char * const typep;
1158 const char * const monthp;
1159 const char * const dayp;
1160 const char * const timep;
1162 register const struct lookup * lp;
1163 register const char * cp;
1164 register char * dp;
1165 register char * ep;
1167 if ((lp = byword(monthp, mon_names)) == NULL) {
1168 error(_("invalid month name"));
1169 return;
1171 rp->r_month = lp->l_value;
1172 rp->r_todisstd = FALSE;
1173 rp->r_todisgmt = FALSE;
1174 dp = ecpyalloc(timep);
1175 if (*dp != '\0') {
1176 ep = dp + strlen(dp) - 1;
1177 switch (lowerit(*ep)) {
1178 case 's': /* Standard */
1179 rp->r_todisstd = TRUE;
1180 rp->r_todisgmt = FALSE;
1181 *ep = '\0';
1182 break;
1183 case 'w': /* Wall */
1184 rp->r_todisstd = FALSE;
1185 rp->r_todisgmt = FALSE;
1186 *ep = '\0';
1187 case 'g': /* Greenwich */
1188 case 'u': /* Universal */
1189 case 'z': /* Zulu */
1190 rp->r_todisstd = TRUE;
1191 rp->r_todisgmt = TRUE;
1192 *ep = '\0';
1193 break;
1196 rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1197 ifree(dp);
1199 ** Year work.
1201 cp = loyearp;
1202 lp = byword(cp, begin_years);
1203 if (lp != NULL) switch ((int) lp->l_value) {
1204 case YR_MINIMUM:
1205 rp->r_loyear = INT_MIN;
1206 break;
1207 case YR_MAXIMUM:
1208 rp->r_loyear = INT_MAX;
1209 break;
1210 default: /* "cannot happen" */
1211 (void) fprintf(stderr,
1212 _("%s: panic: Invalid l_value %d\n"),
1213 progname, lp->l_value);
1214 (void) exit(EXIT_FAILURE);
1215 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
1216 error(_("invalid starting year"));
1217 return;
1219 cp = hiyearp;
1220 if ((lp = byword(cp, end_years)) != NULL) switch ((int) lp->l_value) {
1221 case YR_MINIMUM:
1222 rp->r_hiyear = INT_MIN;
1223 break;
1224 case YR_MAXIMUM:
1225 rp->r_hiyear = INT_MAX;
1226 break;
1227 case YR_ONLY:
1228 rp->r_hiyear = rp->r_loyear;
1229 break;
1230 default: /* "cannot happen" */
1231 (void) fprintf(stderr,
1232 _("%s: panic: Invalid l_value %d\n"),
1233 progname, lp->l_value);
1234 (void) exit(EXIT_FAILURE);
1235 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
1236 error(_("invalid ending year"));
1237 return;
1239 if (rp->r_loyear > rp->r_hiyear) {
1240 error(_("starting year greater than ending year"));
1241 return;
1243 if (*typep == '\0')
1244 rp->r_yrtype = NULL;
1245 else {
1246 if (rp->r_loyear == rp->r_hiyear) {
1247 error(_("typed single year"));
1248 return;
1250 rp->r_yrtype = ecpyalloc(typep);
1253 ** Day work.
1254 ** Accept things such as:
1255 ** 1
1256 ** last-Sunday
1257 ** Sun<=20
1258 ** Sun>=7
1260 dp = ecpyalloc(dayp);
1261 if ((lp = byword(dp, lasts)) != NULL) {
1262 rp->r_dycode = DC_DOWLEQ;
1263 rp->r_wday = lp->l_value;
1264 rp->r_dayofmonth = len_months[1][rp->r_month];
1265 } else {
1266 if ((ep = strchr(dp, '<')) != 0)
1267 rp->r_dycode = DC_DOWLEQ;
1268 else if ((ep = strchr(dp, '>')) != 0)
1269 rp->r_dycode = DC_DOWGEQ;
1270 else {
1271 ep = dp;
1272 rp->r_dycode = DC_DOM;
1274 if (rp->r_dycode != DC_DOM) {
1275 *ep++ = 0;
1276 if (*ep++ != '=') {
1277 error(_("invalid day of month"));
1278 ifree(dp);
1279 return;
1281 if ((lp = byword(dp, wday_names)) == NULL) {
1282 error(_("invalid weekday name"));
1283 ifree(dp);
1284 return;
1286 rp->r_wday = lp->l_value;
1288 if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1289 rp->r_dayofmonth <= 0 ||
1290 (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1291 error(_("invalid day of month"));
1292 ifree(dp);
1293 return;
1296 ifree(dp);
1299 static void
1300 convert(val, buf)
1301 const long val;
1302 char * const buf;
1304 register int i;
1305 register long shift;
1307 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1308 buf[i] = val >> shift;
1311 static void
1312 puttzcode(val, fp)
1313 const long val;
1314 FILE * const fp;
1316 char buf[4];
1318 convert(val, buf);
1319 (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1322 static void
1323 writezone(name)
1324 const char * const name;
1326 register FILE * fp;
1327 register int i, j;
1328 static char * fullname;
1329 static struct tzhead tzh;
1331 fullname = erealloc(fullname,
1332 (int) (strlen(directory) + 1 + strlen(name) + 1));
1333 (void) sprintf(fullname, "%s/%s", directory, name);
1334 if ((fp = fopen(fullname, "wb")) == NULL) {
1335 if (mkdirs(fullname) != 0)
1336 (void) exit(EXIT_FAILURE);
1337 if ((fp = fopen(fullname, "wb")) == NULL) {
1338 const char *e = strerror(errno);
1339 (void) fprintf(stderr, _("%s: Can't create %s: %s\n"),
1340 progname, fullname, e);
1341 (void) exit(EXIT_FAILURE);
1344 convert(eitol(typecnt), tzh.tzh_ttisgmtcnt);
1345 convert(eitol(typecnt), tzh.tzh_ttisstdcnt);
1346 convert(eitol(leapcnt), tzh.tzh_leapcnt);
1347 convert(eitol(timecnt), tzh.tzh_timecnt);
1348 convert(eitol(typecnt), tzh.tzh_typecnt);
1349 convert(eitol(charcnt), tzh.tzh_charcnt);
1350 #define DO(field) (void) fwrite((void *) tzh.field, \
1351 (size_t) sizeof tzh.field, (size_t) 1, fp)
1352 DO(tzh_reserved);
1353 DO(tzh_ttisgmtcnt);
1354 DO(tzh_ttisstdcnt);
1355 DO(tzh_leapcnt);
1356 DO(tzh_timecnt);
1357 DO(tzh_typecnt);
1358 DO(tzh_charcnt);
1359 #undef DO
1360 for (i = 0; i < timecnt; ++i) {
1361 j = leapcnt;
1362 while (--j >= 0)
1363 if (ats[i] >= trans[j]) {
1364 ats[i] = tadd(ats[i], corr[j]);
1365 break;
1367 puttzcode((long) ats[i], fp);
1369 if (timecnt > 0)
1370 (void) fwrite((void *) types, (size_t) sizeof types[0],
1371 (size_t) timecnt, fp);
1372 for (i = 0; i < typecnt; ++i) {
1373 puttzcode((long) gmtoffs[i], fp);
1374 (void) putc(isdsts[i], fp);
1375 (void) putc(abbrinds[i], fp);
1377 if (charcnt != 0)
1378 (void) fwrite((void *) chars, (size_t) sizeof chars[0],
1379 (size_t) charcnt, fp);
1380 for (i = 0; i < leapcnt; ++i) {
1381 if (roll[i]) {
1382 if (timecnt == 0 || trans[i] < ats[0]) {
1383 j = 0;
1384 while (isdsts[j])
1385 if (++j >= typecnt) {
1386 j = 0;
1387 break;
1389 } else {
1390 j = 1;
1391 while (j < timecnt && trans[i] >= ats[j])
1392 ++j;
1393 j = types[j - 1];
1395 puttzcode((long) tadd(trans[i], -gmtoffs[j]), fp);
1396 } else puttzcode((long) trans[i], fp);
1397 puttzcode((long) corr[i], fp);
1399 for (i = 0; i < typecnt; ++i)
1400 (void) putc(ttisstds[i], fp);
1401 for (i = 0; i < typecnt; ++i)
1402 (void) putc(ttisgmts[i], fp);
1403 if (ferror(fp) || fclose(fp)) {
1404 (void) fprintf(stderr, _("%s: Error writing %s\n"),
1405 progname, fullname);
1406 (void) exit(EXIT_FAILURE);
1410 static void
1411 doabbr(abbr, format, letters, isdst)
1412 char * const abbr;
1413 const char * const format;
1414 const char * const letters;
1415 const int isdst;
1417 if (strchr(format, '/') == NULL) {
1418 if (letters == NULL)
1419 (void) strcpy(abbr, format);
1420 else (void) sprintf(abbr, format, letters);
1421 } else if (isdst)
1422 (void) strcpy(abbr, strchr(format, '/') + 1);
1423 else {
1424 (void) strcpy(abbr, format);
1425 *strchr(abbr, '/') = '\0';
1429 static void
1430 outzone(zpfirst, zonecount)
1431 const struct zone * const zpfirst;
1432 const int zonecount;
1434 register const struct zone * zp;
1435 register struct rule * rp;
1436 register int i, j;
1437 register int usestart, useuntil;
1438 register time_t starttime, untiltime;
1439 register long gmtoff;
1440 register long stdoff;
1441 register int year;
1442 register long startoff;
1443 register int startisdst;
1444 register int startttisstd;
1445 register int startttisgmt;
1446 register int type;
1447 char startbuf[BUFSIZ];
1449 INITIALIZE(untiltime);
1450 INITIALIZE(starttime);
1451 INITIALIZE(startoff);
1453 ** Now. . .finally. . .generate some useful data!
1455 timecnt = 0;
1456 typecnt = 0;
1457 charcnt = 0;
1459 ** A guess that may well be corrected later.
1461 stdoff = 0;
1463 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1464 ** for noting the need to unconditionally initialize startttisstd.
1466 startttisstd = FALSE;
1467 startttisgmt = FALSE;
1468 for (i = 0; i < zonecount; ++i) {
1469 zp = &zpfirst[i];
1470 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
1471 useuntil = i < (zonecount - 1);
1472 if (useuntil && zp->z_untiltime <= min_time)
1473 continue;
1474 gmtoff = zp->z_gmtoff;
1475 eat(zp->z_filename, zp->z_linenum);
1476 startisdst = -1;
1477 if (zp->z_nrules == 0) {
1478 stdoff = zp->z_stdoff;
1479 doabbr(startbuf, zp->z_format,
1480 (char *) NULL, stdoff != 0);
1481 type = addtype(oadd(zp->z_gmtoff, stdoff),
1482 startbuf, stdoff != 0, startttisstd,
1483 startttisgmt);
1484 if (usestart)
1485 addtt(starttime, type);
1486 else if (stdoff != 0)
1487 addtt(min_time, type);
1488 } else for (year = min_year; year <= max_year; ++year) {
1489 if (useuntil && year > zp->z_untilrule.r_hiyear)
1490 break;
1492 ** Mark which rules to do in the current year.
1493 ** For those to do, calculate rpytime(rp, year);
1495 for (j = 0; j < zp->z_nrules; ++j) {
1496 rp = &zp->z_rules[j];
1497 eats(zp->z_filename, zp->z_linenum,
1498 rp->r_filename, rp->r_linenum);
1499 rp->r_todo = year >= rp->r_loyear &&
1500 year <= rp->r_hiyear &&
1501 yearistype(year, rp->r_yrtype);
1502 if (rp->r_todo)
1503 rp->r_temp = rpytime(rp, year);
1505 for ( ; ; ) {
1506 register int k;
1507 register time_t jtime, ktime;
1508 register long offset;
1509 char buf[BUFSIZ];
1511 INITIALIZE(ktime);
1512 if (useuntil) {
1514 ** Turn untiltime into GMT
1515 ** assuming the current gmtoff and
1516 ** stdoff values.
1518 untiltime = zp->z_untiltime;
1519 if (!zp->z_untilrule.r_todisgmt)
1520 untiltime = tadd(untiltime,
1521 -gmtoff);
1522 if (!zp->z_untilrule.r_todisstd)
1523 untiltime = tadd(untiltime,
1524 -stdoff);
1527 ** Find the rule (of those to do, if any)
1528 ** that takes effect earliest in the year.
1530 k = -1;
1531 for (j = 0; j < zp->z_nrules; ++j) {
1532 rp = &zp->z_rules[j];
1533 if (!rp->r_todo)
1534 continue;
1535 eats(zp->z_filename, zp->z_linenum,
1536 rp->r_filename, rp->r_linenum);
1537 offset = rp->r_todisgmt ? 0 : gmtoff;
1538 if (!rp->r_todisstd)
1539 offset = oadd(offset, stdoff);
1540 jtime = rp->r_temp;
1541 if (jtime == min_time ||
1542 jtime == max_time)
1543 continue;
1544 jtime = tadd(jtime, -offset);
1545 if (k < 0 || jtime < ktime) {
1546 k = j;
1547 ktime = jtime;
1550 if (k < 0)
1551 break; /* go on to next year */
1552 rp = &zp->z_rules[k];
1553 rp->r_todo = FALSE;
1554 if (useuntil && ktime >= untiltime)
1555 break;
1556 if (usestart) {
1557 if (ktime < starttime) {
1558 stdoff = rp->r_stdoff;
1559 startoff = oadd(zp->z_gmtoff,
1560 rp->r_stdoff);
1561 doabbr(startbuf, zp->z_format,
1562 rp->r_abbrvar,
1563 rp->r_stdoff != 0);
1564 startisdst = rp->r_stdoff != 0;
1565 continue;
1567 usestart = FALSE;
1568 if (ktime != starttime) {
1569 if (startisdst < 0 &&
1570 zp->z_gmtoff !=
1571 (zp - 1)->z_gmtoff) {
1572 type = (timecnt == 0) ? 0 :
1573 types[timecnt - 1];
1574 startoff = oadd(gmtoffs[type],
1575 -(zp - 1)->z_gmtoff);
1576 startisdst = startoff != 0;
1577 startoff = oadd(startoff,
1578 zp->z_gmtoff);
1579 (void) strcpy(startbuf,
1580 &chars[abbrinds[type]]);
1582 if (startisdst >= 0)
1583 addtt(starttime, addtype(startoff, startbuf, startisdst, startttisstd,
1584 startttisgmt));
1587 eats(zp->z_filename, zp->z_linenum,
1588 rp->r_filename, rp->r_linenum);
1589 doabbr(buf, zp->z_format, rp->r_abbrvar,
1590 rp->r_stdoff != 0);
1591 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
1592 type = addtype(offset, buf, rp->r_stdoff != 0,
1593 rp->r_todisstd, rp->r_todisgmt);
1594 addtt(ktime, type);
1595 stdoff = rp->r_stdoff;
1599 ** Now we may get to set starttime for the next zone line.
1601 if (useuntil) {
1602 starttime = tadd(zp->z_untiltime, -gmtoff);
1603 startttisstd = zp->z_untilrule.r_todisstd;
1604 startttisgmt = zp->z_untilrule.r_todisgmt;
1605 if (!startttisstd)
1606 starttime = tadd(starttime, -stdoff);
1609 writezone(zpfirst->z_name);
1612 static void
1613 addtt(starttime, type)
1614 const time_t starttime;
1615 const int type;
1617 if (timecnt != 0 && type == types[timecnt - 1])
1618 return; /* easy enough! */
1619 if (timecnt == 0 && type == 0 && isdsts[0] == 0)
1620 return; /* handled by default rule */
1621 if (timecnt >= TZ_MAX_TIMES) {
1622 error(_("too many transitions?!"));
1623 (void) exit(EXIT_FAILURE);
1625 ats[timecnt] = starttime;
1626 types[timecnt] = type;
1627 ++timecnt;
1630 static int
1631 addtype(gmtoff, abbr, isdst, ttisstd, ttisgmt)
1632 const long gmtoff;
1633 const char * const abbr;
1634 const int isdst;
1635 const int ttisstd;
1636 const int ttisgmt;
1638 register int i, j;
1641 ** See if there's already an entry for this zone type.
1642 ** If so, just return its index.
1644 for (i = 0; i < typecnt; ++i) {
1645 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
1646 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
1647 ttisstd == ttisstds[i] &&
1648 ttisgmt == ttisgmts[i])
1649 return i;
1652 ** There isn't one; add a new one, unless there are already too
1653 ** many.
1655 if (typecnt >= TZ_MAX_TYPES) {
1656 error(_("too many local time types"));
1657 (void) exit(EXIT_FAILURE);
1659 gmtoffs[i] = gmtoff;
1660 isdsts[i] = isdst;
1661 ttisstds[i] = ttisstd;
1662 ttisgmts[i] = ttisgmt;
1664 for (j = 0; j < charcnt; ++j)
1665 if (strcmp(&chars[j], abbr) == 0)
1666 break;
1667 if (j == charcnt)
1668 newabbr(abbr);
1669 abbrinds[i] = j;
1670 ++typecnt;
1671 return i;
1674 static void
1675 leapadd(t, positive, rolling, count)
1676 const time_t t;
1677 const int positive;
1678 const int rolling;
1679 int count;
1681 register int i, j;
1683 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
1684 error(_("too many leap seconds"));
1685 (void) exit(EXIT_FAILURE);
1687 for (i = 0; i < leapcnt; ++i)
1688 if (t <= trans[i]) {
1689 if (t == trans[i]) {
1690 error(_("repeated leap second moment"));
1691 (void) exit(EXIT_FAILURE);
1693 break;
1695 do {
1696 for (j = leapcnt; j > i; --j) {
1697 trans[j] = trans[j - 1];
1698 corr[j] = corr[j - 1];
1699 roll[j] = roll[j - 1];
1701 trans[i] = t;
1702 corr[i] = positive ? 1L : eitol(-count);
1703 roll[i] = rolling;
1704 ++leapcnt;
1705 } while (positive && --count != 0);
1708 static void
1709 adjleap P((void))
1711 register int i;
1712 register long last = 0;
1715 ** propagate leap seconds forward
1717 for (i = 0; i < leapcnt; ++i) {
1718 trans[i] = tadd(trans[i], last);
1719 last = corr[i] += last;
1723 static int
1724 yearistype(year, type)
1725 const int year;
1726 const char * const type;
1728 static char * buf;
1729 int result;
1731 if (type == NULL || *type == '\0')
1732 return TRUE;
1733 buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
1734 (void) sprintf(buf, "%s %d %s", yitcommand, year, type);
1735 result = system(buf);
1736 if (result == 0)
1737 return TRUE;
1738 if (result == (1 << 8))
1739 return FALSE;
1740 error(_("Wild result from command execution"));
1741 (void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
1742 progname, buf, result);
1743 for ( ; ; )
1744 (void) exit(EXIT_FAILURE);
1747 static int
1748 lowerit(a)
1749 int a;
1751 a = (unsigned char) a;
1752 return (isascii(a) && isupper(a)) ? tolower(a) : a;
1755 static int
1756 ciequal(ap, bp) /* case-insensitive equality */
1757 register const char * ap;
1758 register const char * bp;
1760 while (lowerit(*ap) == lowerit(*bp++))
1761 if (*ap++ == '\0')
1762 return TRUE;
1763 return FALSE;
1766 static int
1767 itsabbr(abbr, word)
1768 register const char * abbr;
1769 register const char * word;
1771 if (lowerit(*abbr) != lowerit(*word))
1772 return FALSE;
1773 ++word;
1774 while (*++abbr != '\0')
1775 do {
1776 if (*word == '\0')
1777 return FALSE;
1778 } while (lowerit(*word++) != lowerit(*abbr));
1779 return TRUE;
1782 static const struct lookup *
1783 byword(word, table)
1784 register const char * const word;
1785 register const struct lookup * const table;
1787 register const struct lookup * foundlp;
1788 register const struct lookup * lp;
1790 if (word == NULL || table == NULL)
1791 return NULL;
1793 ** Look for exact match.
1795 for (lp = table; lp->l_word != NULL; ++lp)
1796 if (ciequal(word, lp->l_word))
1797 return lp;
1799 ** Look for inexact match.
1801 foundlp = NULL;
1802 for (lp = table; lp->l_word != NULL; ++lp)
1803 if (itsabbr(word, lp->l_word))
1804 if (foundlp == NULL)
1805 foundlp = lp;
1806 else return NULL; /* multiple inexact matches */
1807 return foundlp;
1810 static char **
1811 getfields(cp)
1812 register char * cp;
1814 register char * dp;
1815 register char ** array;
1816 register int nsubs;
1818 if (cp == NULL)
1819 return NULL;
1820 array = (char **) (void *)
1821 emalloc((int) ((strlen(cp) + 1) * sizeof *array));
1822 nsubs = 0;
1823 for ( ; ; ) {
1824 while (isascii(*cp) && isspace((unsigned char) *cp))
1825 ++cp;
1826 if (*cp == '\0' || *cp == '#')
1827 break;
1828 array[nsubs++] = dp = cp;
1829 do {
1830 if ((*dp = *cp++) != '"')
1831 ++dp;
1832 else while ((*dp = *cp++) != '"')
1833 if (*dp != '\0')
1834 ++dp;
1835 else error(_("Odd number of quotation marks"));
1836 } while (*cp != '\0' && *cp != '#' &&
1837 (!isascii(*cp) || !isspace((unsigned char) *cp)));
1838 if (isascii(*cp) && isspace((unsigned char) *cp))
1839 ++cp;
1840 *dp = '\0';
1842 array[nsubs] = NULL;
1843 return array;
1846 static long
1847 oadd(t1, t2)
1848 const long t1;
1849 const long t2;
1851 register long t;
1853 t = t1 + t2;
1854 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
1855 error(_("time overflow"));
1856 (void) exit(EXIT_FAILURE);
1858 return t;
1861 static time_t
1862 tadd(t1, t2)
1863 const time_t t1;
1864 const long t2;
1866 register time_t t;
1868 if (t1 == max_time && t2 > 0)
1869 return max_time;
1870 if (t1 == min_time && t2 < 0)
1871 return min_time;
1872 t = t1 + t2;
1873 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
1874 error(_("time overflow"));
1875 (void) exit(EXIT_FAILURE);
1877 return t;
1881 ** Given a rule, and a year, compute the date - in seconds since January 1,
1882 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
1885 static time_t
1886 rpytime(rp, wantedy)
1887 register const struct rule * const rp;
1888 register const int wantedy;
1890 register int y, m, i;
1891 register long dayoff; /* with a nod to Margaret O. */
1892 register time_t t;
1894 if (wantedy == INT_MIN)
1895 return min_time;
1896 if (wantedy == INT_MAX)
1897 return max_time;
1898 dayoff = 0;
1899 m = TM_JANUARY;
1900 y = EPOCH_YEAR;
1901 while (wantedy != y) {
1902 if (wantedy > y) {
1903 i = len_years[isleap(y)];
1904 ++y;
1905 } else {
1906 --y;
1907 i = -len_years[isleap(y)];
1909 dayoff = oadd(dayoff, eitol(i));
1911 while (m != rp->r_month) {
1912 i = len_months[isleap(y)][m];
1913 dayoff = oadd(dayoff, eitol(i));
1914 ++m;
1916 i = rp->r_dayofmonth;
1917 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
1918 if (rp->r_dycode == DC_DOWLEQ)
1919 --i;
1920 else {
1921 error(_("use of 2/29 in non leap-year"));
1922 (void) exit(EXIT_FAILURE);
1925 --i;
1926 dayoff = oadd(dayoff, eitol(i));
1927 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
1928 register long wday;
1930 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
1931 wday = eitol(EPOCH_WDAY);
1933 ** Don't trust mod of negative numbers.
1935 if (dayoff >= 0)
1936 wday = (wday + dayoff) % LDAYSPERWEEK;
1937 else {
1938 wday -= ((-dayoff) % LDAYSPERWEEK);
1939 if (wday < 0)
1940 wday += LDAYSPERWEEK;
1942 while (wday != eitol(rp->r_wday))
1943 if (rp->r_dycode == DC_DOWGEQ) {
1944 dayoff = oadd(dayoff, (long) 1);
1945 if (++wday >= LDAYSPERWEEK)
1946 wday = 0;
1947 ++i;
1948 } else {
1949 dayoff = oadd(dayoff, (long) -1);
1950 if (--wday < 0)
1951 wday = LDAYSPERWEEK - 1;
1952 --i;
1954 if (i < 0 || i >= len_months[isleap(y)][m]) {
1955 error(_("no day in month matches rule"));
1956 (void) exit(EXIT_FAILURE);
1959 if (dayoff < 0 && !TYPE_SIGNED(time_t))
1960 return min_time;
1961 t = (time_t) dayoff * SECSPERDAY;
1963 ** Cheap overflow check.
1965 if (t / SECSPERDAY != dayoff)
1966 return (dayoff > 0) ? max_time : min_time;
1967 return tadd(t, rp->r_tod);
1970 static void
1971 newabbr(string)
1972 const char * const string;
1974 register int i;
1976 i = strlen(string) + 1;
1977 if (charcnt + i > TZ_MAX_CHARS) {
1978 error(_("too many, or too long, time zone abbreviations"));
1979 (void) exit(EXIT_FAILURE);
1981 (void) strcpy(&chars[charcnt], string);
1982 charcnt += eitol(i);
1985 static int
1986 mkdirs(argname)
1987 char * const argname;
1989 register char * name;
1990 register char * cp;
1992 if (argname == NULL || *argname == '\0')
1993 return 0;
1994 cp = name = ecpyalloc(argname);
1995 while ((cp = strchr(cp + 1, '/')) != 0) {
1996 *cp = '\0';
1997 #ifndef unix
1999 ** DOS drive specifier?
2001 if (isalpha((unsigned char) name[0]) &&
2002 name[1] == ':' && name[2] == '\0') {
2003 *cp = '/';
2004 continue;
2006 #endif /* !defined unix */
2007 if (!itsdir(name)) {
2009 ** It doesn't seem to exist, so we try to create it.
2011 if (mkdir(name, 0755) != 0) {
2012 const char *e = strerror(errno);
2013 (void) fprintf(stderr,
2014 _("%s: Can't create directory %s: %s\n"),
2015 progname, name, e);
2016 ifree(name);
2017 return -1;
2020 *cp = '/';
2022 ifree(name);
2023 return 0;
2026 static long
2027 eitol(i)
2028 const int i;
2030 long l;
2032 l = i;
2033 if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0)) {
2034 (void) fprintf(stderr,
2035 _("%s: %d did not sign extend correctly\n"),
2036 progname, i);
2037 (void) exit(EXIT_FAILURE);
2039 return l;
2043 ** UNIX was a registered trademark of UNIX System Laboratories in 1993.