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