BSD 4_4 release
[unix-history] / usr / src / share / zoneinfo / zic.c
CommitLineData
6b4c1ff3 1/*-
ad787160
C
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
6b4c1ff3
KB
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Arthur David Olson of the National Cancer Institute.
7 *
ad787160
C
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
6b4c1ff3
KB
35 */
36
2c78da71 37#ifndef lint
ad787160 38static char sccsid[] = "@(#)zic.c 8.1 (Berkeley) 7/19/93";
6b4c1ff3
KB
39#endif /* not lint */
40
41#ifdef notdef
2c78da71 42static char elsieid[] = "@(#)zic.c 4.12";
6b4c1ff3 43#endif
2c78da71 44
256a180c
KB
45#include <sys/types.h>
46#include <sys/cdefs.h>
47#include <sys/stat.h>
48#include <time.h>
49#include <tzfile.h>
50#include <stdio.h>
51#include <ctype.h>
52#include <string.h>
53#include <stdlib.h>
2c78da71
KB
54
55#ifndef TRUE
56#define TRUE 1
57#define FALSE 0
58#endif /* !defined TRUE */
59
60struct rule {
61 const char * r_filename;
62 int r_linenum;
63 const char * r_name;
64
65 int r_loyear; /* for example, 1986 */
66 int r_hiyear; /* for example, 1986 */
67 const char * r_yrtype;
68
69 int r_month; /* 0..11 */
70
71 int r_dycode; /* see below */
72 int r_dayofmonth;
73 int r_wday;
74
75 long r_tod; /* time from midnight */
76 int r_todisstd; /* above is standard time if TRUE */
77 /* or wall clock time if FALSE */
78 long r_stdoff; /* offset from standard time */
79 const char * r_abbrvar; /* variable part of abbreviation */
80
81 int r_todo; /* a rule to do (used in outzone) */
82 time_t r_temp; /* used in outzone */
83};
84
85/*
86** r_dycode r_dayofmonth r_wday
87*/
88
89#define DC_DOM 0 /* 1..31 */ /* unused */
90#define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
91#define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
92
93struct zone {
94 const char * z_filename;
95 int z_linenum;
96
97 const char * z_name;
98 long z_gmtoff;
99 const char * z_rule;
100 const char * z_format;
101
102 long z_stdoff;
103
104 struct rule * z_rules;
105 int z_nrules;
106
107 struct rule z_untilrule;
108 time_t z_untiltime;
109};
110
256a180c
KB
111extern char * icatalloc __P((char * old, const char * new));
112extern char * icpyalloc __P((const char * string));
113extern void ifree __P((char * p));
114extern char * imalloc __P((int n));
115extern char * irealloc __P((char * old, int n));
116extern int link __P((const char * fromname, const char * toname));
2c78da71
KB
117extern char * optarg;
118extern int optind;
256a180c
KB
119extern void perror __P((const char * string));
120extern char * scheck __P((const char * string, const char * format));
121static void addtt __P((time_t starttime, int type));
122static int addtype
123 __P((long gmtoff, const char * abbr, int isdst,
124 int ttisstd));
125static void addleap __P((time_t t, int positive, int rolling));
126static void adjleap __P((void));
127static void associate __P((void));
128static int ciequal __P((const char * ap, const char * bp));
129static void convert __P((long val, char * buf));
130static void dolink __P((const char * fromfile, const char * tofile));
131static void eat __P((const char * name, int num));
132static void eats __P((const char * name, int num,
133 const char * rname, int rnum));
134static long eitol __P((int i));
135static void error __P((const char * message));
136static char ** getfields __P((char * buf));
137static long gethms __P((char * string, const char * errstrng,
138 int signable));
139static void infile __P((const char * filename));
140static void inleap __P((char ** fields, int nfields));
141static void inlink __P((char ** fields, int nfields));
142static void inrule __P((char ** fields, int nfields));
143static int inzcont __P((char ** fields, int nfields));
144static int inzone __P((char ** fields, int nfields));
145static int inzsub __P((char ** fields, int nfields, int iscont));
146static int itsabbr __P((const char * abbr, const char * word));
147static int itsdir __P((const char * name));
148static int lowerit __P((int c));
149static char * memcheck __P((char * tocheck));
150static int mkdirs __P((char * filename));
151static void newabbr __P((const char * abbr));
152static long oadd __P((long t1, long t2));
153static void outzone __P((const struct zone * zp, int ntzones));
154static void puttzcode __P((long code, FILE * fp));
155static int rcomp __P((const void *leftp, const void *rightp));
156static time_t rpytime __P((const struct rule * rp, int wantedy));
157static void rulesub __P((struct rule * rp, char * loyearp, char * hiyearp,
158 char * typep, char * monthp, char * dayp, char * timep));
159static void setboundaries __P((void));
160static time_t tadd __P((time_t t1, long t2));
161static void usage __P((void));
162static void writezone __P((const char * name));
163static int yearistype __P((int year, const char * type));
2c78da71
KB
164
165static int charcnt;
166static int errors;
167static const char * filename;
168static int leapcnt;
169static int linenum;
170static time_t max_time;
171static int max_year;
172static time_t min_time;
173static int min_year;
174static int noise;
175static const char * rfilename;
176static int rlinenum;
177static const char * progname;
178static int timecnt;
179static int typecnt;
180static int tt_signed;
181
182/*
183** Line codes.
184*/
185
186#define LC_RULE 0
187#define LC_ZONE 1
188#define LC_LINK 2
189#define LC_LEAP 3
190
191/*
192** Which fields are which on a Zone line.
193*/
194
195#define ZF_NAME 1
196#define ZF_GMTOFF 2
197#define ZF_RULE 3
198#define ZF_FORMAT 4
199#define ZF_TILYEAR 5
200#define ZF_TILMONTH 6
201#define ZF_TILDAY 7
202#define ZF_TILTIME 8
203#define ZONE_MINFIELDS 5
204#define ZONE_MAXFIELDS 9
205
206/*
207** Which fields are which on a Zone continuation line.
208*/
209
210#define ZFC_GMTOFF 0
211#define ZFC_RULE 1
212#define ZFC_FORMAT 2
213#define ZFC_TILYEAR 3
214#define ZFC_TILMONTH 4
215#define ZFC_TILDAY 5
216#define ZFC_TILTIME 6
217#define ZONEC_MINFIELDS 3
218#define ZONEC_MAXFIELDS 7
219
220/*
221** Which files are which on a Rule line.
222*/
223
224#define RF_NAME 1
225#define RF_LOYEAR 2
226#define RF_HIYEAR 3
227#define RF_COMMAND 4
228#define RF_MONTH 5
229#define RF_DAY 6
230#define RF_TOD 7
231#define RF_STDOFF 8
232#define RF_ABBRVAR 9
233#define RULE_FIELDS 10
234
235/*
236** Which fields are which on a Link line.
237*/
238
239#define LF_FROM 1
240#define LF_TO 2
241#define LINK_FIELDS 3
242
243/*
244** Which fields are which on a Leap line.
245*/
246
247#define LP_YEAR 1
248#define LP_MONTH 2
249#define LP_DAY 3
250#define LP_TIME 4
251#define LP_CORR 5
252#define LP_ROLL 6
253#define LEAP_FIELDS 7
254
255/*
256** Year synonyms.
257*/
258
259#define YR_MINIMUM 0
260#define YR_MAXIMUM 1
261#define YR_ONLY 2
262
263static struct rule * rules;
264static int nrules; /* number of rules */
265
266static struct zone * zones;
267static int nzones; /* number of zones */
268
269struct link {
270 const char * l_filename;
271 int l_linenum;
272 const char * l_from;
273 const char * l_to;
274};
275
276static struct link * links;
277static int nlinks;
278
279struct lookup {
280 const char * l_word;
281 const int l_value;
282};
283
256a180c 284static struct lookup const * byword __P((const char * string,
2c78da71
KB
285 const struct lookup * lp));
286
287static struct lookup const line_codes[] = {
288 "Rule", LC_RULE,
289 "Zone", LC_ZONE,
290 "Link", LC_LINK,
291 "Leap", LC_LEAP,
292 NULL, 0
293};
294
295static struct lookup const mon_names[] = {
296 "January", TM_JANUARY,
297 "February", TM_FEBRUARY,
298 "March", TM_MARCH,
299 "April", TM_APRIL,
300 "May", TM_MAY,
301 "June", TM_JUNE,
302 "July", TM_JULY,
303 "August", TM_AUGUST,
304 "September", TM_SEPTEMBER,
305 "October", TM_OCTOBER,
306 "November", TM_NOVEMBER,
307 "December", TM_DECEMBER,
308 NULL, 0
309};
310
311static struct lookup const wday_names[] = {
312 "Sunday", TM_SUNDAY,
313 "Monday", TM_MONDAY,
314 "Tuesday", TM_TUESDAY,
315 "Wednesday", TM_WEDNESDAY,
316 "Thursday", TM_THURSDAY,
317 "Friday", TM_FRIDAY,
318 "Saturday", TM_SATURDAY,
319 NULL, 0
320};
321
322static struct lookup const lasts[] = {
323 "last-Sunday", TM_SUNDAY,
324 "last-Monday", TM_MONDAY,
325 "last-Tuesday", TM_TUESDAY,
326 "last-Wednesday", TM_WEDNESDAY,
327 "last-Thursday", TM_THURSDAY,
328 "last-Friday", TM_FRIDAY,
329 "last-Saturday", TM_SATURDAY,
330 NULL, 0
331};
332
333static struct lookup const begin_years[] = {
334 "minimum", YR_MINIMUM,
335 "maximum", YR_MAXIMUM,
336 NULL, 0
337};
338
339static struct lookup const end_years[] = {
340 "minimum", YR_MINIMUM,
341 "maximum", YR_MAXIMUM,
342 "only", YR_ONLY,
343 NULL, 0
344};
345
346static struct lookup const leap_types[] = {
347 "Rolling", TRUE,
348 "Stationary", FALSE,
349 NULL, 0
350};
351
352static const int len_months[2][MONSPERYEAR] = {
353 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
354 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
355};
356
357static const int len_years[2] = {
358 DAYSPERNYEAR, DAYSPERLYEAR
359};
360
361static time_t ats[TZ_MAX_TIMES];
362static unsigned char types[TZ_MAX_TIMES];
363static long gmtoffs[TZ_MAX_TYPES];
364static char isdsts[TZ_MAX_TYPES];
365static char abbrinds[TZ_MAX_TYPES];
366static char ttisstds[TZ_MAX_TYPES];
367static char chars[TZ_MAX_CHARS];
368static time_t trans[TZ_MAX_LEAPS];
369static long corr[TZ_MAX_LEAPS];
370static char roll[TZ_MAX_LEAPS];
371
372/*
373** Memory allocation.
374*/
375
376static char *
377memcheck(ptr)
378char * const ptr;
379{
380 if (ptr == NULL) {
381 (void) perror(progname);
382 (void) exit(EXIT_FAILURE);
383 }
384 return ptr;
385}
386
387#define emalloc(size) memcheck(imalloc(size))
388#define erealloc(ptr, size) memcheck(irealloc(ptr, size))
389#define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
390#define ecatalloc(oldp, newp) memcheck(icatalloc(oldp, newp))
391
392/*
393** Error handling.
394*/
395
396static void
397eats(name, num, rname, rnum)
398const char * const name;
399const int num;
400const char * const rname;
401const int rnum;
402{
403 filename = name;
404 linenum = num;
405 rfilename = rname;
406 rlinenum = rnum;
407}
408
409static void
410eat(name, num)
411const char * const name;
412const int num;
413{
414 eats(name, num, (char *) NULL, -1);
415}
416
417static void
418error(string)
419const char * const string;
420{
421 /*
422 ** Match the format of "cc" to allow sh users to
423 ** zic ... 2>&1 | error -t "*" -v
424 ** on BSD systems.
425 */
426 (void) fprintf(stderr, "\"%s\", line %d: %s",
427 filename, linenum, string);
428 if (rfilename != NULL)
429 (void) fprintf(stderr, " (rule from \"%s\", line %d)",
430 rfilename, rlinenum);
431 (void) fprintf(stderr, "\n");
432 ++errors;
433}
434
435static void
436usage()
437{
438 (void) fprintf(stderr,
439"%s: usage is %s [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] [ -d directory ]\n\
440\t[ -L leapseconds ] [ filename ... ]\n",
441 progname, progname);
442 (void) exit(EXIT_FAILURE);
443}
444
445static const char * psxrules = NULL;
446static const char * lcltime = NULL;
447static const char * directory = NULL;
448static const char * leapsec = NULL;
449static int sflag = FALSE;
450
451int
452main(argc, argv)
453int argc;
454char * argv[];
455{
456 register int i, j;
457 register int c;
458
2c78da71 459 (void) umask(umask(022) | 022);
2c78da71
KB
460 progname = argv[0];
461 while ((c = getopt(argc, argv, "d:l:p:L:vs")) != EOF)
462 switch (c) {
463 default:
464 usage();
465 case 'd':
466 if (directory == NULL)
467 directory = optarg;
468 else {
469 (void) fprintf(stderr,
470"%s: More than one -d option specified\n",
471 progname);
472 (void) exit(EXIT_FAILURE);
473 }
474 break;
475 case 'l':
476 if (lcltime == NULL)
477 lcltime = optarg;
478 else {
479 (void) fprintf(stderr,
480"%s: More than one -l option specified\n",
481 progname);
482 (void) exit(EXIT_FAILURE);
483 }
484 break;
485 case 'p':
486 if (psxrules == NULL)
487 psxrules = optarg;
488 else {
489 (void) fprintf(stderr,
490"%s: More than one -p option specified\n",
491 progname);
492 (void) exit(EXIT_FAILURE);
493 }
494 break;
495 case 'L':
496 if (leapsec == NULL)
497 leapsec = optarg;
498 else {
499 (void) fprintf(stderr,
500"%s: More than one -L option specified\n",
501 progname);
502 (void) exit(EXIT_FAILURE);
503 }
504 break;
505 case 'v':
506 noise = TRUE;
507 break;
508 case 's':
509 sflag = TRUE;
510 break;
511 }
512 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
513 usage(); /* usage message by request */
514 if (directory == NULL)
515 directory = TZDIR;
516
517 setboundaries();
518
519 if (optind < argc && leapsec != NULL) {
520 infile(leapsec);
521 adjleap();
522 }
523
524 zones = (struct zone *) emalloc(0);
525 rules = (struct rule *) emalloc(0);
526 links = (struct link *) emalloc(0);
527 for (i = optind; i < argc; ++i)
528 infile(argv[i]);
529 if (errors)
530 (void) exit(EXIT_FAILURE);
531 associate();
532 for (i = 0; i < nzones; i = j) {
533 /*
534 ** Find the next non-continuation zone entry.
535 */
536 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
537 ;
538 outzone(&zones[i], j - i);
539 }
540 /*
541 ** Make links.
542 */
543 for (i = 0; i < nlinks; ++i)
544 dolink(links[i].l_from, links[i].l_to);
545 if (lcltime != NULL)
546 dolink(lcltime, TZDEFAULT);
547 if (psxrules != NULL)
548 dolink(psxrules, TZDEFRULES);
549 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
550}
551
552static void
553dolink(fromfile, tofile)
554const char * const fromfile;
555const char * const tofile;
556{
557 register char * fromname;
558 register char * toname;
559
560 fromname = ecpyalloc(directory);
561 fromname = ecatalloc(fromname, "/");
562 fromname = ecatalloc(fromname, fromfile);
563 toname = ecpyalloc(directory);
564 toname = ecatalloc(toname, "/");
565 toname = ecatalloc(toname, tofile);
566 /*
567 ** We get to be careful here since
568 ** there's a fair chance of root running us.
569 */
570 if (!itsdir(toname))
571 (void) remove(toname);
572 if (link(fromname, toname) != 0) {
573 (void) fprintf(stderr, "%s: Can't link from %s to ",
574 progname, fromname);
575 (void) perror(toname);
576 (void) exit(EXIT_FAILURE);
577 }
578 ifree(fromname);
579 ifree(toname);
580}
581
582static void
583setboundaries()
584{
585 register time_t bit;
586
587 for (bit = 1; bit > 0; bit <<= 1)
588 ;
589 if (bit == 0) { /* time_t is an unsigned type */
590 tt_signed = FALSE;
591 min_time = 0;
592 max_time = ~(time_t) 0;
593 if (sflag)
594 max_time >>= 1;
595 } else {
596 tt_signed = TRUE;
597 min_time = bit;
598 max_time = bit;
599 ++max_time;
600 max_time = -max_time;
601 if (sflag)
602 min_time = 0;
603 }
604 min_year = TM_YEAR_BASE + gmtime(&min_time)->tm_year;
605 max_year = TM_YEAR_BASE + gmtime(&max_time)->tm_year;
606}
607
608static int
609itsdir(name)
610const char * const name;
611{
612 struct stat s;
613
256a180c 614 return (stat(name, &s) == 0 && S_ISDIR(s.st_mode));
2c78da71
KB
615}
616
617/*
618** Associate sets of rules with zones.
619*/
620
621/*
622** Sort by rule name.
623*/
624
625static int
626rcomp(cp1, cp2)
256a180c
KB
627const void * cp1;
628const void * cp2;
2c78da71
KB
629{
630 return strcmp(((struct rule *) cp1)->r_name,
631 ((struct rule *) cp2)->r_name);
632}
633
634static void
635associate()
636{
637 register struct zone * zp;
638 register struct rule * rp;
639 register int base, out;
640 register int i;
641
642 if (nrules != 0)
256a180c
KB
643 (void) qsort((void *) rules, (size_t) nrules,
644 (size_t) sizeof *rules, rcomp);
2c78da71
KB
645 for (i = 0; i < nzones; ++i) {
646 zp = &zones[i];
647 zp->z_rules = NULL;
648 zp->z_nrules = 0;
649 }
650 for (base = 0; base < nrules; base = out) {
651 rp = &rules[base];
652 for (out = base + 1; out < nrules; ++out)
653 if (strcmp(rp->r_name, rules[out].r_name) != 0)
654 break;
655 for (i = 0; i < nzones; ++i) {
656 zp = &zones[i];
657 if (strcmp(zp->z_rule, rp->r_name) != 0)
658 continue;
659 zp->z_rules = rp;
660 zp->z_nrules = out - base;
661 }
662 }
663 for (i = 0; i < nzones; ++i) {
664 zp = &zones[i];
665 if (zp->z_nrules == 0) {
666 /*
667 ** Maybe we have a local standard time offset.
668 */
669 eat(zp->z_filename, zp->z_linenum);
256a180c
KB
670 zp->z_stdoff =
671 gethms((char *)zp->z_rule, "unruly zone", TRUE);
2c78da71
KB
672 /*
673 ** Note, though, that if there's no rule,
674 ** a '%s' in the format is a bad thing.
675 */
676 if (strchr(zp->z_format, '%') != 0)
677 error("%s in ruleless zone");
678 }
679 }
680 if (errors)
681 (void) exit(EXIT_FAILURE);
682}
683
684static void
685infile(name)
686const char * name;
687{
688 register FILE * fp;
689 register char ** fields;
690 register char * cp;
691 register const struct lookup * lp;
692 register int nfields;
693 register int wantcont;
694 register int num;
695 char buf[BUFSIZ];
696
697 if (strcmp(name, "-") == 0) {
698 name = "standard input";
699 fp = stdin;
700 } else if ((fp = fopen(name, "r")) == NULL) {
701 (void) fprintf(stderr, "%s: Can't open ", progname);
702 (void) perror(name);
703 (void) exit(EXIT_FAILURE);
704 }
705 wantcont = FALSE;
706 for (num = 1; ; ++num) {
707 eat(name, num);
708 if (fgets(buf, (int) sizeof buf, fp) != buf)
709 break;
710 cp = strchr(buf, '\n');
711 if (cp == NULL) {
712 error("line too long");
713 (void) exit(EXIT_FAILURE);
714 }
715 *cp = '\0';
716 fields = getfields(buf);
717 nfields = 0;
718 while (fields[nfields] != NULL) {
719 if (ciequal(fields[nfields], "-"))
720 fields[nfields] = "";
721 ++nfields;
722 }
723 if (nfields == 0) {
724 /* nothing to do */
725 } else if (wantcont) {
726 wantcont = inzcont(fields, nfields);
727 } else {
728 lp = byword(fields[0], line_codes);
729 if (lp == NULL)
730 error("input line of unknown type");
731 else switch ((int) (lp->l_value)) {
732 case LC_RULE:
733 inrule(fields, nfields);
734 wantcont = FALSE;
735 break;
736 case LC_ZONE:
737 wantcont = inzone(fields, nfields);
738 break;
739 case LC_LINK:
740 inlink(fields, nfields);
741 wantcont = FALSE;
742 break;
743 case LC_LEAP:
744 if (name != leapsec)
745 (void) fprintf(stderr,
746"%s: Leap line in non leap seconds file %s\n",
747 progname, name);
748 else inleap(fields, nfields);
749 wantcont = FALSE;
750 break;
751 default: /* "cannot happen" */
752 (void) fprintf(stderr,
753"%s: panic: Invalid l_value %d\n",
754 progname, lp->l_value);
755 (void) exit(EXIT_FAILURE);
756 }
757 }
758 ifree((char *) fields);
759 }
760 if (ferror(fp)) {
761 (void) fprintf(stderr, "%s: Error reading ", progname);
762 (void) perror(filename);
763 (void) exit(EXIT_FAILURE);
764 }
765 if (fp != stdin && fclose(fp)) {
766 (void) fprintf(stderr, "%s: Error closing ", progname);
767 (void) perror(filename);
768 (void) exit(EXIT_FAILURE);
769 }
770 if (wantcont)
771 error("expected continuation line not found");
772}
773
774/*
775** Convert a string of one of the forms
776** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
777** into a number of seconds.
778** A null string maps to zero.
779** Call error with errstring and return zero on errors.
780*/
781
782static long
783gethms(string, errstring, signable)
256a180c 784char * string;
2c78da71
KB
785const char * const errstring;
786const int signable;
787{
788 int hh, mm, ss, sign;
789
790 if (string == NULL || *string == '\0')
791 return 0;
792 if (!signable)
793 sign = 1;
794 else if (*string == '-') {
795 sign = -1;
796 ++string;
797 } else sign = 1;
798 if (sscanf(string, scheck(string, "%d"), &hh) == 1)
799 mm = ss = 0;
800 else if (sscanf(string, scheck(string, "%d:%d"), &hh, &mm) == 2)
801 ss = 0;
802 else if (sscanf(string, scheck(string, "%d:%d:%d"),
803 &hh, &mm, &ss) != 3) {
804 error(errstring);
805 return 0;
806 }
807 if (hh < 0 || hh >= HOURSPERDAY ||
808 mm < 0 || mm >= MINSPERHOUR ||
809 ss < 0 || ss > SECSPERMIN) {
810 error(errstring);
811 return 0;
812 }
813 return eitol(sign) *
814 (eitol(hh * MINSPERHOUR + mm) *
815 eitol(SECSPERMIN) + eitol(ss));
816}
817
818static void
819inrule(fields, nfields)
820register char ** const fields;
821const int nfields;
822{
823 static struct rule r;
824
825 if (nfields != RULE_FIELDS) {
826 error("wrong number of fields on Rule line");
827 return;
828 }
829 if (*fields[RF_NAME] == '\0') {
830 error("nameless rule");
831 return;
832 }
833 r.r_filename = filename;
834 r.r_linenum = linenum;
835 r.r_stdoff = gethms(fields[RF_STDOFF], "invalid saved time", TRUE);
836 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
837 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
838 r.r_name = ecpyalloc(fields[RF_NAME]);
839 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
840 rules = (struct rule *) erealloc((char *) rules,
841 (int) ((nrules + 1) * sizeof *rules));
842 rules[nrules++] = r;
843}
844
845static int
846inzone(fields, nfields)
847register char ** const fields;
848const int nfields;
849{
850 register int i;
851 char buf[132];
852
853 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
854 error("wrong number of fields on Zone line");
855 return FALSE;
856 }
857 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
858 (void) sprintf(buf,
859 "\"Zone %s\" line and -l option are mutually exclusive",
860 TZDEFAULT);
861 error(buf);
862 return FALSE;
863 }
864 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
865 (void) sprintf(buf,
866 "\"Zone %s\" line and -p option are mutually exclusive",
867 TZDEFRULES);
868 error(buf);
869 return FALSE;
870 }
871 for (i = 0; i < nzones; ++i)
872 if (zones[i].z_name != NULL &&
873 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
874 (void) sprintf(buf,
875"duplicate zone name %s (file \"%s\", line %d)",
876 fields[ZF_NAME],
877 zones[i].z_filename,
878 zones[i].z_linenum);
879 error(buf);
880 return FALSE;
881 }
882 return inzsub(fields, nfields, FALSE);
883}
884
885static int
886inzcont(fields, nfields)
887register char ** const fields;
888const int nfields;
889{
890 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
891 error("wrong number of fields on Zone continuation line");
892 return FALSE;
893 }
894 return inzsub(fields, nfields, TRUE);
895}
896
897static int
898inzsub(fields, nfields, iscont)
899register char ** const fields;
900const int nfields;
901const int iscont;
902{
903 register char * cp;
904 static struct zone z;
905 register int i_gmtoff, i_rule, i_format;
906 register int i_untilyear, i_untilmonth;
907 register int i_untilday, i_untiltime;
908 register int hasuntil;
909
910 if (iscont) {
911 i_gmtoff = ZFC_GMTOFF;
912 i_rule = ZFC_RULE;
913 i_format = ZFC_FORMAT;
914 i_untilyear = ZFC_TILYEAR;
915 i_untilmonth = ZFC_TILMONTH;
916 i_untilday = ZFC_TILDAY;
917 i_untiltime = ZFC_TILTIME;
918 z.z_name = NULL;
919 } else {
920 i_gmtoff = ZF_GMTOFF;
921 i_rule = ZF_RULE;
922 i_format = ZF_FORMAT;
923 i_untilyear = ZF_TILYEAR;
924 i_untilmonth = ZF_TILMONTH;
925 i_untilday = ZF_TILDAY;
926 i_untiltime = ZF_TILTIME;
927 z.z_name = ecpyalloc(fields[ZF_NAME]);
928 }
929 z.z_filename = filename;
930 z.z_linenum = linenum;
931 z.z_gmtoff = gethms(fields[i_gmtoff], "invalid GMT offset", TRUE);
932 if ((cp = strchr(fields[i_format], '%')) != 0) {
933 if (*++cp != 's' || strchr(cp, '%') != 0) {
934 error("invalid abbreviation format");
935 return FALSE;
936 }
937 }
938 z.z_rule = ecpyalloc(fields[i_rule]);
939 z.z_format = ecpyalloc(fields[i_format]);
940 hasuntil = nfields > i_untilyear;
941 if (hasuntil) {
942 z.z_untilrule.r_filename = filename;
943 z.z_untilrule.r_linenum = linenum;
944 rulesub(&z.z_untilrule,
945 fields[i_untilyear],
946 "only",
947 "",
948 (nfields > i_untilmonth) ? fields[i_untilmonth] : "Jan",
949 (nfields > i_untilday) ? fields[i_untilday] : "1",
950 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
951 z.z_untiltime = rpytime(&z.z_untilrule, z.z_untilrule.r_loyear);
952 if (iscont && nzones > 0 && z.z_untiltime < max_time &&
953 z.z_untiltime > min_time &&
954 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
955error("Zone continuation line end time is not after end time of previous line");
956 return FALSE;
957 }
958 }
959 zones = (struct zone *) erealloc((char *) zones,
960 (int) ((nzones + 1) * sizeof *zones));
961 zones[nzones++] = z;
962 /*
963 ** If there was an UNTIL field on this line,
964 ** there's more information about the zone on the next line.
965 */
966 return hasuntil;
967}
968
969static void
970inleap(fields, nfields)
971register char ** const fields;
972const int nfields;
973{
974 register const char * cp;
975 register const struct lookup * lp;
976 register int i, j;
977 int year, month, day;
978 long dayoff, tod;
979 time_t t;
980
981 if (nfields != LEAP_FIELDS) {
982 error("wrong number of fields on Leap line");
983 return;
984 }
985 dayoff = 0;
986 cp = fields[LP_YEAR];
256a180c 987 if (sscanf((char *)cp, scheck(cp, "%d"), &year) != 1 ||
2c78da71
KB
988 year < min_year || year > max_year) {
989 /*
990 * Leapin' Lizards!
991 */
992 error("invalid leaping year");
993 return;
994 }
995 j = EPOCH_YEAR;
996 while (j != year) {
997 if (year > j) {
998 i = len_years[isleap(j)];
999 ++j;
1000 } else {
1001 --j;
1002 i = -len_years[isleap(j)];
1003 }
1004 dayoff = oadd(dayoff, eitol(i));
1005 }
1006 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1007 error("invalid month name");
1008 return;
1009 }
1010 month = lp->l_value;
1011 j = TM_JANUARY;
1012 while (j != month) {
1013 i = len_months[isleap(year)][j];
1014 dayoff = oadd(dayoff, eitol(i));
1015 ++j;
1016 }
1017 cp = fields[LP_DAY];
256a180c 1018 if (sscanf((char *)cp, scheck(cp, "%d"), &day) != 1 ||
2c78da71
KB
1019 day <= 0 || day > len_months[isleap(year)][month]) {
1020 error("invalid day of month");
1021 return;
1022 }
1023 dayoff = oadd(dayoff, eitol(day - 1));
1024 if (dayoff < 0 && !tt_signed) {
1025 error("time before zero");
1026 return;
1027 }
1028 t = (time_t) dayoff * SECSPERDAY;
1029 /*
1030 ** Cheap overflow check.
1031 */
1032 if (t / SECSPERDAY != dayoff) {
1033 error("time overflow");
1034 return;
1035 }
1036 tod = gethms(fields[LP_TIME], "invalid time of day", FALSE);
1037 cp = fields[LP_CORR];
1038 if (strcmp(cp, "+") != 0 && strcmp(cp, "") != 0) {
1039 /* infile() turned "-" into "" */
1040 error("illegal CORRECTION field on Leap line");
1041 return;
1042 }
1043 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1044 error("illegal Rolling/Stationary field on Leap line");
1045 return;
1046 }
1047 addleap(tadd(t, tod), *cp == '+', lp->l_value);
1048}
1049
1050static void
1051inlink(fields, nfields)
1052register char ** const fields;
1053const int nfields;
1054{
1055 struct link l;
1056
1057 if (nfields != LINK_FIELDS) {
1058 error("wrong number of fields on Link line");
1059 return;
1060 }
1061 if (*fields[LF_FROM] == '\0') {
1062 error("blank FROM field on Link line");
1063 return;
1064 }
1065 if (*fields[LF_TO] == '\0') {
1066 error("blank TO field on Link line");
1067 return;
1068 }
1069 l.l_filename = filename;
1070 l.l_linenum = linenum;
1071 l.l_from = ecpyalloc(fields[LF_FROM]);
1072 l.l_to = ecpyalloc(fields[LF_TO]);
1073 links = (struct link *) erealloc((char *) links,
1074 (int) ((nlinks + 1) * sizeof *links));
1075 links[nlinks++] = l;
1076}
1077
1078static void
1079rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
1080register struct rule * const rp;
1081char * const loyearp;
1082char * const hiyearp;
1083char * const typep;
1084char * const monthp;
1085char * const dayp;
1086char * const timep;
1087{
1088 register struct lookup const * lp;
1089 register char * cp;
1090
1091 if ((lp = byword(monthp, mon_names)) == NULL) {
1092 error("invalid month name");
1093 return;
1094 }
1095 rp->r_month = lp->l_value;
1096 rp->r_todisstd = FALSE;
1097 cp = timep;
1098 if (*cp != '\0') {
1099 cp += strlen(cp) - 1;
1100 switch (lowerit(*cp)) {
1101 case 's':
1102 rp->r_todisstd = TRUE;
1103 *cp = '\0';
1104 break;
1105 case 'w':
1106 rp->r_todisstd = FALSE;
1107 *cp = '\0';
1108 break;
1109 }
1110 }
1111 rp->r_tod = gethms(timep, "invalid time of day", FALSE);
1112 /*
1113 ** Year work.
1114 */
1115 cp = loyearp;
1116 if ((lp = byword(cp, begin_years)) != NULL) switch ((int) lp->l_value) {
1117 case YR_MINIMUM:
1118 rp->r_loyear = min_year;
1119 break;
1120 case YR_MAXIMUM:
1121 rp->r_loyear = max_year;
1122 break;
1123 default: /* "cannot happen" */
1124 (void) fprintf(stderr,
1125 "%s: panic: Invalid l_value %d\n",
1126 progname, lp->l_value);
1127 (void) exit(EXIT_FAILURE);
1128 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1 ||
1129 rp->r_loyear < min_year || rp->r_loyear > max_year) {
1130 if (noise)
1131 error("invalid starting year");
1132 if (rp->r_loyear > max_year)
1133 return;
1134 }
1135 cp = hiyearp;
1136 if ((lp = byword(cp, end_years)) != NULL) switch ((int) lp->l_value) {
1137 case YR_MINIMUM:
1138 rp->r_hiyear = min_year;
1139 break;
1140 case YR_MAXIMUM:
1141 rp->r_hiyear = max_year;
1142 break;
1143 case YR_ONLY:
1144 rp->r_hiyear = rp->r_loyear;
1145 break;
1146 default: /* "cannot happen" */
1147 (void) fprintf(stderr,
1148 "%s: panic: Invalid l_value %d\n",
1149 progname, lp->l_value);
1150 (void) exit(EXIT_FAILURE);
1151 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1 ||
1152 rp->r_hiyear < min_year || rp->r_hiyear > max_year) {
1153 if (noise)
1154 error("invalid ending year");
1155 if (rp->r_hiyear < min_year)
1156 return;
1157 }
1158 if (rp->r_hiyear < min_year)
1159 return;
1160 if (rp->r_loyear < min_year)
1161 rp->r_loyear = min_year;
1162 if (rp->r_hiyear > max_year)
1163 rp->r_hiyear = max_year;
1164 if (rp->r_loyear > rp->r_hiyear) {
1165 error("starting year greater than ending year");
1166 return;
1167 }
1168 if (*typep == '\0')
1169 rp->r_yrtype = NULL;
1170 else {
1171 if (rp->r_loyear == rp->r_hiyear) {
1172 error("typed single year");
1173 return;
1174 }
1175 rp->r_yrtype = ecpyalloc(typep);
1176 }
1177 /*
1178 ** Day work.
1179 ** Accept things such as:
1180 ** 1
1181 ** last-Sunday
1182 ** Sun<=20
1183 ** Sun>=7
1184 */
1185 if ((lp = byword(dayp, lasts)) != NULL) {
1186 rp->r_dycode = DC_DOWLEQ;
1187 rp->r_wday = lp->l_value;
1188 rp->r_dayofmonth = len_months[1][rp->r_month];
1189 } else {
1190 if ((cp = strchr(dayp, '<')) != 0)
1191 rp->r_dycode = DC_DOWLEQ;
1192 else if ((cp = strchr(dayp, '>')) != 0)
1193 rp->r_dycode = DC_DOWGEQ;
1194 else {
1195 cp = dayp;
1196 rp->r_dycode = DC_DOM;
1197 }
1198 if (rp->r_dycode != DC_DOM) {
1199 *cp++ = 0;
1200 if (*cp++ != '=') {
1201 error("invalid day of month");
1202 return;
1203 }
1204 if ((lp = byword(dayp, wday_names)) == NULL) {
1205 error("invalid weekday name");
1206 return;
1207 }
1208 rp->r_wday = lp->l_value;
1209 }
1210 if (sscanf(cp, scheck(cp, "%d"), &rp->r_dayofmonth) != 1 ||
1211 rp->r_dayofmonth <= 0 ||
1212 (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1213 error("invalid day of month");
1214 return;
1215 }
1216 }
1217}
1218
1219static void
1220convert(val, buf)
1221const long val;
1222char * const buf;
1223{
1224 register int i;
1225 register long shift;
1226
1227 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1228 buf[i] = val >> shift;
1229}
1230
1231static void
1232puttzcode(val, fp)
1233const long val;
1234FILE * const fp;
1235{
1236 char buf[4];
1237
1238 convert(val, buf);
256a180c 1239 (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
2c78da71
KB
1240}
1241
1242static void
1243writezone(name)
1244const char * const name;
1245{
1246 register FILE * fp;
1247 register int i, j;
1248 char fullname[BUFSIZ];
1249 static struct tzhead tzh;
1250
1251 if (strlen(directory) + 1 + strlen(name) >= sizeof fullname) {
1252 (void) fprintf(stderr,
1253 "%s: File name %s/%s too long\n", progname,
1254 directory, name);
1255 (void) exit(EXIT_FAILURE);
1256 }
1257 (void) sprintf(fullname, "%s/%s", directory, name);
1258 if ((fp = fopen(fullname, "wb")) == NULL) {
1259 if (mkdirs(fullname) != 0)
1260 (void) exit(EXIT_FAILURE);
1261 if ((fp = fopen(fullname, "wb")) == NULL) {
1262 (void) fprintf(stderr, "%s: Can't create ", progname);
1263 (void) perror(fullname);
1264 (void) exit(EXIT_FAILURE);
1265 }
1266 }
1267 convert(eitol(typecnt), tzh.tzh_ttisstdcnt);
1268 convert(eitol(leapcnt), tzh.tzh_leapcnt);
1269 convert(eitol(timecnt), tzh.tzh_timecnt);
1270 convert(eitol(typecnt), tzh.tzh_typecnt);
1271 convert(eitol(charcnt), tzh.tzh_charcnt);
256a180c 1272 (void) fwrite((void *) &tzh, (size_t) sizeof tzh, (size_t) 1, fp);
2c78da71
KB
1273 for (i = 0; i < timecnt; ++i) {
1274 j = leapcnt;
1275 while (--j >= 0)
1276 if (ats[i] >= trans[j]) {
1277 ats[i] = tadd(ats[i], corr[j]);
1278 break;
1279 }
1280 puttzcode((long) ats[i], fp);
1281 }
1282 if (timecnt > 0)
256a180c
KB
1283 (void) fwrite((void *) types, (size_t) sizeof types[0],
1284 (size_t) timecnt, fp);
2c78da71
KB
1285 for (i = 0; i < typecnt; ++i) {
1286 puttzcode((long) gmtoffs[i], fp);
1287 (void) putc(isdsts[i], fp);
1288 (void) putc(abbrinds[i], fp);
1289 }
1290 if (charcnt != 0)
256a180c
KB
1291 (void) fwrite((void *) chars, (size_t) sizeof chars[0],
1292 (size_t) charcnt, fp);
2c78da71
KB
1293 for (i = 0; i < leapcnt; ++i) {
1294 if (roll[i]) {
1295 if (timecnt == 0 || trans[i] < ats[0]) {
1296 j = 0;
1297 while (isdsts[j])
1298 if (++j >= typecnt) {
1299 j = 0;
1300 break;
1301 }
1302 } else {
1303 j = 1;
1304 while (j < timecnt && trans[i] >= ats[j])
1305 ++j;
1306 j = types[j - 1];
1307 }
1308 puttzcode((long) tadd(trans[i], -gmtoffs[j]), fp);
1309 } else puttzcode((long) trans[i], fp);
1310 puttzcode((long) corr[i], fp);
1311 }
1312 for (i = 0; i < typecnt; ++i)
1313 (void) putc(ttisstds[i], fp);
1314 if (ferror(fp) || fclose(fp)) {
1315 (void) fprintf(stderr, "%s: Write error on ", progname);
1316 (void) perror(fullname);
1317 (void) exit(EXIT_FAILURE);
1318 }
1319}
1320
1321static void
1322outzone(zpfirst, zonecount)
1323const struct zone * const zpfirst;
1324const int zonecount;
1325{
1326 register const struct zone * zp;
1327 register struct rule * rp;
1328 register int i, j;
1329 register int usestart, useuntil;
1330 register time_t starttime, untiltime;
1331 register long gmtoff;
1332 register long stdoff;
1333 register int year;
1334 register long startoff;
1335 register int startisdst;
1336 register int startttisstd;
1337 register int type;
1338 char startbuf[BUFSIZ];
1339
1340 /*
1341 ** Now. . .finally. . .generate some useful data!
1342 */
1343 timecnt = 0;
1344 typecnt = 0;
1345 charcnt = 0;
1346 /*
1347 ** Two guesses. . .the second may well be corrected later.
1348 */
1349 gmtoff = zpfirst->z_gmtoff;
1350 stdoff = 0;
2c78da71
KB
1351 starttime = 0;
1352 startttisstd = FALSE;
2c78da71
KB
1353 for (i = 0; i < zonecount; ++i) {
1354 usestart = i > 0;
1355 useuntil = i < (zonecount - 1);
1356 zp = &zpfirst[i];
1357 eat(zp->z_filename, zp->z_linenum);
1358 startisdst = -1;
1359 if (zp->z_nrules == 0) {
1360 type = addtype(oadd(zp->z_gmtoff, zp->z_stdoff),
1361 zp->z_format, zp->z_stdoff != 0,
1362 startttisstd);
1363 if (usestart)
1364 addtt(starttime, type);
1365 gmtoff = zp->z_gmtoff;
1366 stdoff = zp->z_stdoff;
1367 } else for (year = min_year; year <= max_year; ++year) {
1368 if (useuntil && year > zp->z_untilrule.r_hiyear)
1369 break;
1370 /*
1371 ** Mark which rules to do in the current year.
1372 ** For those to do, calculate rpytime(rp, year);
1373 */
1374 for (j = 0; j < zp->z_nrules; ++j) {
1375 rp = &zp->z_rules[j];
1376 eats(zp->z_filename, zp->z_linenum,
1377 rp->r_filename, rp->r_linenum);
1378 rp->r_todo = year >= rp->r_loyear &&
1379 year <= rp->r_hiyear &&
1380 yearistype(year, rp->r_yrtype);
1381 if (rp->r_todo)
1382 rp->r_temp = rpytime(rp, year);
1383 }
1384 for ( ; ; ) {
1385 register int k;
1386 register time_t jtime, ktime;
1387 register long offset;
1388 char buf[BUFSIZ];
1389
1390 if (useuntil) {
1391 /*
1392 ** Turn untiltime into GMT
1393 ** assuming the current gmtoff and
1394 ** stdoff values.
1395 */
1396 offset = gmtoff;
1397 if (!zp->z_untilrule.r_todisstd)
1398 offset = oadd(offset, stdoff);
1399 untiltime = tadd(zp->z_untiltime,
1400 -offset);
1401 }
1402 /*
1403 ** Find the rule (of those to do, if any)
1404 ** that takes effect earliest in the year.
1405 */
1406 k = -1;
1407#ifdef lint
1408 ktime = 0;
1409#endif /* defined lint */
1410 for (j = 0; j < zp->z_nrules; ++j) {
1411 rp = &zp->z_rules[j];
1412 if (!rp->r_todo)
1413 continue;
1414 eats(zp->z_filename, zp->z_linenum,
1415 rp->r_filename, rp->r_linenum);
1416 offset = gmtoff;
1417 if (!rp->r_todisstd)
1418 offset = oadd(offset, stdoff);
1419 jtime = rp->r_temp;
1420 if (jtime == min_time ||
1421 jtime == max_time)
1422 continue;
1423 jtime = tadd(jtime, -offset);
1424 if (k < 0 || jtime < ktime) {
1425 k = j;
1426 ktime = jtime;
1427 }
1428 }
1429 if (k < 0)
1430 break; /* go on to next year */
1431 rp = &zp->z_rules[k];
1432 rp->r_todo = FALSE;
1433 if (useuntil && ktime >= untiltime)
1434 break;
1435 if (usestart) {
1436 if (ktime < starttime) {
1437 stdoff = rp->r_stdoff;
1438 startoff = oadd(zp->z_gmtoff,
1439 rp->r_stdoff);
1440 (void) sprintf(startbuf,
1441 zp->z_format,
1442 rp->r_abbrvar);
1443 startisdst =
1444 rp->r_stdoff != 0;
1445 continue;
1446 }
1447 if (ktime != starttime &&
1448 startisdst >= 0)
1449addtt(starttime, addtype(startoff, startbuf, startisdst, startttisstd));
1450 usestart = FALSE;
1451 }
1452 eats(zp->z_filename, zp->z_linenum,
1453 rp->r_filename, rp->r_linenum);
1454 (void) sprintf(buf, zp->z_format,
1455 rp->r_abbrvar);
1456 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
1457 type = addtype(offset, buf, rp->r_stdoff != 0,
1458 rp->r_todisstd);
1459 if (timecnt != 0 || rp->r_stdoff != 0)
1460 addtt(ktime, type);
1461 gmtoff = zp->z_gmtoff;
1462 stdoff = rp->r_stdoff;
1463 }
1464 }
1465 /*
1466 ** Now we may get to set starttime for the next zone line.
1467 */
1468 if (useuntil) {
1469 starttime = tadd(zp->z_untiltime,
1470 -gmtoffs[types[timecnt - 1]]);
1471 startttisstd = zp->z_untilrule.r_todisstd;
1472 }
1473 }
1474 writezone(zpfirst->z_name);
1475}
1476
1477static void
1478addtt(starttime, type)
1479const time_t starttime;
1480const int type;
1481{
1482 if (timecnt != 0 && type == types[timecnt - 1])
1483 return; /* easy enough! */
1484 if (timecnt >= TZ_MAX_TIMES) {
1485 error("too many transitions?!");
1486 (void) exit(EXIT_FAILURE);
1487 }
1488 ats[timecnt] = starttime;
1489 types[timecnt] = type;
1490 ++timecnt;
1491}
1492
1493static int
1494addtype(gmtoff, abbr, isdst, ttisstd)
1495const long gmtoff;
1496const char * const abbr;
1497const int isdst;
1498const int ttisstd;
1499{
1500 register int i, j;
1501
1502 /*
1503 ** See if there's already an entry for this zone type.
1504 ** If so, just return its index.
1505 */
1506 for (i = 0; i < typecnt; ++i) {
1507 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
1508 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
1509 ttisstd == ttisstds[i])
1510 return i;
1511 }
1512 /*
1513 ** There isn't one; add a new one, unless there are already too
1514 ** many.
1515 */
1516 if (typecnt >= TZ_MAX_TYPES) {
1517 error("too many local time types");
1518 (void) exit(EXIT_FAILURE);
1519 }
1520 gmtoffs[i] = gmtoff;
1521 isdsts[i] = isdst;
1522 ttisstds[i] = ttisstd;
1523
1524 for (j = 0; j < charcnt; ++j)
1525 if (strcmp(&chars[j], abbr) == 0)
1526 break;
1527 if (j == charcnt)
1528 newabbr(abbr);
1529 abbrinds[i] = j;
1530 ++typecnt;
1531 return i;
1532}
1533
1534static void
1535addleap(t, positive, rolling)
1536const time_t t;
1537const int positive;
1538const int rolling;
1539{
1540 register int i, j;
1541
1542 if (leapcnt >= TZ_MAX_LEAPS) {
1543 error("too many leap seconds");
1544 (void) exit(EXIT_FAILURE);
1545 }
1546 for (i = 0; i < leapcnt; ++i)
1547 if (t <= trans[i]) {
1548 if (t == trans[i]) {
1549 error("repeated leap second moment");
1550 (void) exit(EXIT_FAILURE);
1551 }
1552 break;
1553 }
1554 for (j = leapcnt; j > i; --j) {
1555 trans[j] = trans[j-1];
1556 corr[j] = corr[j-1];
1557 roll[j] = roll[j-1];
1558 }
1559 trans[i] = t;
1560 corr[i] = (positive ? 1L : -1L);
1561 roll[i] = rolling;
1562 ++leapcnt;
1563}
1564
1565static void
1566adjleap()
1567{
1568 register int i;
1569 register long last = 0;
1570
1571 /*
1572 ** propagate leap seconds forward
1573 */
1574 for (i = 0; i < leapcnt; ++i) {
1575 trans[i] = tadd(trans[i], last);
1576 last = corr[i] += last;
1577 }
1578}
1579
1580static int
1581yearistype(year, type)
1582const int year;
1583const char * const type;
1584{
1585 char buf[BUFSIZ];
1586 int result;
1587
1588 if (type == NULL || *type == '\0')
1589 return TRUE;
1590 if (strcmp(type, "uspres") == 0)
1591 return (year % 4) == 0;
1592 if (strcmp(type, "nonpres") == 0)
1593 return (year % 4) != 0;
1594 (void) sprintf(buf, "yearistype %d %s", year, type);
1595 result = system(buf);
1596 if (result == 0)
1597 return TRUE;
1598 if (result == (1 << 8))
1599 return FALSE;
1600 error("Wild result from command execution");
1601 (void) fprintf(stderr, "%s: command was '%s', result was %d\n",
1602 progname, buf, result);
1603 for ( ; ; )
1604 (void) exit(EXIT_FAILURE);
1605}
1606
1607static int
1608lowerit(a)
1609const int a;
1610{
1611 return (isascii(a) && isupper(a)) ? tolower(a) : a;
1612}
1613
1614static int
1615ciequal(ap, bp) /* case-insensitive equality */
1616register const char * ap;
1617register const char * bp;
1618{
1619 while (lowerit(*ap) == lowerit(*bp++))
1620 if (*ap++ == '\0')
1621 return TRUE;
1622 return FALSE;
1623}
1624
1625static int
1626itsabbr(abbr, word)
1627register const char * abbr;
1628register const char * word;
1629{
1630 if (lowerit(*abbr) != lowerit(*word))
1631 return FALSE;
1632 ++word;
1633 while (*++abbr != '\0')
1634 do if (*word == '\0')
1635 return FALSE;
1636 while (lowerit(*word++) != lowerit(*abbr));
1637 return TRUE;
1638}
1639
1640static const struct lookup *
1641byword(word, table)
1642register const char * const word;
1643register const struct lookup * const table;
1644{
1645 register const struct lookup * foundlp;
1646 register const struct lookup * lp;
1647
1648 if (word == NULL || table == NULL)
1649 return NULL;
1650 /*
1651 ** Look for exact match.
1652 */
1653 for (lp = table; lp->l_word != NULL; ++lp)
1654 if (ciequal(word, lp->l_word))
1655 return lp;
1656 /*
1657 ** Look for inexact match.
1658 */
1659 foundlp = NULL;
1660 for (lp = table; lp->l_word != NULL; ++lp)
1661 if (itsabbr(word, lp->l_word))
1662 if (foundlp == NULL)
1663 foundlp = lp;
1664 else return NULL; /* multiple inexact matches */
1665 return foundlp;
1666}
1667
1668static char **
1669getfields(cp)
1670register char * cp;
1671{
1672 register char * dp;
1673 register char ** array;
1674 register int nsubs;
1675
1676 if (cp == NULL)
1677 return NULL;
1678 array = (char **) emalloc((int) ((strlen(cp) + 1) * sizeof *array));
1679 nsubs = 0;
1680 for ( ; ; ) {
1681 while (isascii(*cp) && isspace(*cp))
1682 ++cp;
1683 if (*cp == '\0' || *cp == '#')
1684 break;
1685 array[nsubs++] = dp = cp;
1686 do {
1687 if ((*dp = *cp++) != '"')
1688 ++dp;
1689 else while ((*dp = *cp++) != '"')
1690 if (*dp != '\0')
1691 ++dp;
1692 else error("Odd number of quotation marks");
1693 } while (*cp != '\0' && *cp != '#' &&
1694 (!isascii(*cp) || !isspace(*cp)));
1695 if (isascii(*cp) && isspace(*cp))
1696 ++cp;
1697 *dp = '\0';
1698 }
1699 array[nsubs] = NULL;
1700 return array;
1701}
1702
1703static long
1704oadd(t1, t2)
1705const long t1;
1706const long t2;
1707{
1708 register long t;
1709
1710 t = t1 + t2;
1711 if (t2 > 0 && t <= t1 || t2 < 0 && t >= t1) {
1712 error("time overflow");
1713 (void) exit(EXIT_FAILURE);
1714 }
1715 return t;
1716}
1717
1718static time_t
1719tadd(t1, t2)
1720const time_t t1;
1721const long t2;
1722{
1723 register time_t t;
1724
1725 if (t1 == max_time && t2 > 0)
1726 return max_time;
1727 if (t1 == min_time && t2 < 0)
1728 return min_time;
1729 t = t1 + t2;
1730 if (t2 > 0 && t <= t1 || t2 < 0 && t >= t1) {
1731 error("time overflow");
1732 (void) exit(EXIT_FAILURE);
1733 }
1734 return t;
1735}
1736
1737/*
1738** Given a rule, and a year, compute the date - in seconds since January 1,
1739** 1970, 00:00 LOCAL time - in that year that the rule refers to.
1740*/
1741
1742static time_t
1743rpytime(rp, wantedy)
1744register const struct rule * const rp;
1745register const int wantedy;
1746{
1747 register int y, m, i;
1748 register long dayoff; /* with a nod to Margaret O. */
1749 register time_t t;
1750
1751 dayoff = 0;
1752 m = TM_JANUARY;
1753 y = EPOCH_YEAR;
1754 while (wantedy != y) {
1755 if (wantedy > y) {
1756 i = len_years[isleap(y)];
1757 ++y;
1758 } else {
1759 --y;
1760 i = -len_years[isleap(y)];
1761 }
1762 dayoff = oadd(dayoff, eitol(i));
1763 }
1764 while (m != rp->r_month) {
1765 i = len_months[isleap(y)][m];
1766 dayoff = oadd(dayoff, eitol(i));
1767 ++m;
1768 }
1769 i = rp->r_dayofmonth;
1770 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
1771 if (rp->r_dycode == DC_DOWLEQ)
1772 --i;
1773 else {
1774 error("use of 2/29 in non leap-year");
1775 (void) exit(EXIT_FAILURE);
1776 }
1777 }
1778 --i;
1779 dayoff = oadd(dayoff, eitol(i));
1780 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
1781 register long wday;
1782
1783#define LDAYSPERWEEK ((long) DAYSPERWEEK)
1784 wday = eitol(EPOCH_WDAY);
1785 /*
1786 ** Don't trust mod of negative numbers.
1787 */
1788 if (dayoff >= 0)
1789 wday = (wday + dayoff) % LDAYSPERWEEK;
1790 else {
1791 wday -= ((-dayoff) % LDAYSPERWEEK);
1792 if (wday < 0)
1793 wday += LDAYSPERWEEK;
1794 }
1795 while (wday != eitol(rp->r_wday))
1796 if (rp->r_dycode == DC_DOWGEQ) {
1797 dayoff = oadd(dayoff, (long) 1);
1798 if (++wday >= LDAYSPERWEEK)
1799 wday = 0;
1800 ++i;
1801 } else {
1802 dayoff = oadd(dayoff, (long) -1);
1803 if (--wday < 0)
ad787160 1804 wday = LDAYSPERWEEK - 1;
2c78da71
KB
1805 --i;
1806 }
1807 if (i < 0 || i >= len_months[isleap(y)][m]) {
1808 error("no day in month matches rule");
1809 (void) exit(EXIT_FAILURE);
1810 }
1811 }
1812 if (dayoff < 0 && !tt_signed) {
1813 if (wantedy == rp->r_loyear)
1814 return min_time;
1815 error("time before zero");
1816 (void) exit(EXIT_FAILURE);
1817 }
1818 t = (time_t) dayoff * SECSPERDAY;
1819 /*
1820 ** Cheap overflow check.
1821 */
1822 if (t / SECSPERDAY != dayoff) {
1823 if (wantedy == rp->r_hiyear)
1824 return max_time;
1825 if (wantedy == rp->r_loyear)
1826 return min_time;
1827 error("time overflow");
1828 (void) exit(EXIT_FAILURE);
1829 }
1830 return tadd(t, rp->r_tod);
1831}
1832
1833static void
1834newabbr(string)
1835const char * const string;
1836{
1837 register int i;
1838
1839 i = strlen(string) + 1;
1840 if (charcnt + i >= TZ_MAX_CHARS) {
1841 error("too many, or too long, time zone abbreviations");
1842 (void) exit(EXIT_FAILURE);
1843 }
1844 (void) strcpy(&chars[charcnt], string);
1845 charcnt += eitol(i);
1846}
1847
1848static int
1849mkdirs(name)
1850char * const name;
1851{
1852 register char * cp;
1853
1854 if ((cp = name) == NULL || *cp == '\0')
1855 return 0;
1856 while ((cp = strchr(cp + 1, '/')) != 0) {
1857 *cp = '\0';
2c78da71
KB
1858 if (!itsdir(name)) {
1859 /*
1860 ** It doesn't seem to exist, so we try to create it.
1861 */
256a180c 1862 if (mkdir(name, 0755) != 0) {
2c78da71
KB
1863 (void) fprintf(stderr,
1864 "%s: Can't create directory ",
1865 progname);
1866 (void) perror(name);
1867 return -1;
1868 }
1869 }
1870 *cp = '/';
1871 }
1872 return 0;
1873}
1874
1875static long
1876eitol(i)
1877const int i;
1878{
1879 long l;
1880
1881 l = i;
1882 if (i < 0 && l >= 0 || i == 0 && l != 0 || i > 0 && l <= 0) {
1883 (void) fprintf(stderr, "%s: %d did not sign extend correctly\n",
1884 progname, i);
1885 (void) exit(EXIT_FAILURE);
1886 }
1887 return l;
1888}
1889
1890/*
1891** UNIX is a registered trademark of AT&T.
1892*/