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