taskqueue.9: Do some mdoc cleanup & add missing MLINKS.
[dragonfly.git] / usr.sbin / zic / zic.c
blob7b70fea42ad2b14d26ce9e0aaf5a215044926dee
1 /*
2 ** This file is in the public domain, so clarified as of
3 ** 2006-07-17 by Arthur David Olson.
4 **
5 ** @(#)zic.c 8.20
6 ** $FreeBSD: src/usr.sbin/zic/zic.c,v 1.11 1999/08/28 01:21:20 peter Exp $
7 ** $DragonFly: src/usr.sbin/zic/zic.c,v 1.7 2008/10/19 20:15:58 swildner Exp $
8 */
10 #include <err.h>
11 #include <locale.h>
12 #include <sys/stat.h> /* for umask manifest constants */
13 #include <sys/types.h>
14 #include <unistd.h>
15 #include "private.h"
16 #include "tzfile.h"
18 #define ZIC_VERSION '2'
20 typedef int_fast64_t zic_t;
22 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
23 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
24 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
26 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
29 ** On some ancient hosts, predicates like `isspace(C)' are defined
30 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
31 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
32 ** Neither the C Standard nor Posix require that `isascii' exist.
33 ** For portability, we check both ancient and modern requirements.
34 ** If isascii is not defined, the isascii check succeeds trivially.
36 #include "ctype.h"
37 #ifndef isascii
38 #define isascii(x) 1
39 #endif
41 #define OFFSET_STRLEN_MAXIMUM (7 + INT_STRLEN_MAXIMUM(long))
42 #define RULE_STRLEN_MAXIMUM 8 /* "Mdd.dd.d" */
44 #define end(cp) (strchr((cp), '\0'))
46 struct rule {
47 const char * r_filename;
48 int r_linenum;
49 const char * r_name;
51 int r_loyear; /* for example, 1986 */
52 int r_hiyear; /* for example, 1986 */
53 const char * r_yrtype;
54 int r_lowasnum;
55 int r_hiwasnum;
57 int r_month; /* 0..11 */
59 int r_dycode; /* see below */
60 int r_dayofmonth;
61 int r_wday;
63 long r_tod; /* time from midnight */
64 int r_todisstd; /* above is standard time if TRUE */
65 /* or wall clock time if FALSE */
66 int r_todisgmt; /* above is GMT if TRUE */
67 /* or local time if FALSE */
68 long r_stdoff; /* offset from standard time */
69 const char * r_abbrvar; /* variable part of abbreviation */
71 int r_todo; /* a rule to do (used in outzone) */
72 zic_t r_temp; /* used in outzone */
76 ** r_dycode r_dayofmonth r_wday
79 #define DC_DOM 0 /* 1..31 */ /* unused */
80 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
81 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
83 struct zone {
84 const char * z_filename;
85 int z_linenum;
87 const char * z_name;
88 long z_gmtoff;
89 const char * z_rule;
90 const char * z_format;
92 long z_stdoff;
94 struct rule * z_rules;
95 int z_nrules;
97 struct rule z_untilrule;
98 zic_t z_untiltime;
101 static void addtt(zic_t starttime, int type);
102 static int addtype(long gmtoff, const char *abbr, int isdst,
103 int ttisstd, int ttisgmt);
104 static void leapadd(zic_t t, int positive, int rolling, int count);
105 static void adjleap(void);
106 static void associate(void);
107 static int ciequal(const char *ap, const char *bp);
108 static void convert(long val, char *buf);
109 static void convert64(zic_t val, char *buf);
110 static void dolink(const char *fromfield, const char *tofield);
111 static void doabbr(char *abbr, const char *format,
112 const char *letters, int isdst, int doquotes);
113 static void eat(const char *name, int num);
114 static void eats(const char *name, int num,
115 const char *rname, int rnum);
116 static long eitol(int i);
117 static void error(const char *message);
118 static char ** getfields(char *buf);
119 static long gethms(const char *string, const char *errstrng,
120 int signable);
121 static void infile(const char *filename);
122 static void inleap(char **fields, int nfields);
123 static void inlink(char **fields, int nfields);
124 static void inrule(char **fields, int nfields);
125 static int inzcont(char **fields, int nfields);
126 static int inzone(char **fields, int nfields);
127 static int inzsub(char **fields, int nfields, int iscont);
128 static int is32(zic_t x);
129 static int itsabbr(const char *abbr, const char *word);
130 static int itsdir(const char *name);
131 static int lowerit(int c);
132 static char * memcheck(char *tocheck);
133 static int mkdirs(char *filename);
134 static void newabbr(const char *abbr);
135 static long oadd(long t1, long t2);
136 static void outzone(const struct zone *zp, int ntzones);
137 static void puttzcode(long code, FILE *fp);
138 static void puttzcode64(zic_t code, FILE *fp);
139 static int rcomp(const void *leftp, const void *rightp);
140 static zic_t rpytime(const struct rule *rp, int wantedy);
141 static void rulesub(struct rule *rp,
142 const char *loyearp, const char *hiyearp,
143 const char *typep, const char *monthp,
144 const char *dayp, const char *timep);
145 static int stringoffset(char *result, long offset);
146 static int stringrule(char *result, const struct rule *rp,
147 long dstoff, long gmtoff);
148 static void stringzone(char *result,
149 const struct zone *zp, int ntzones);
150 static void setboundaries(void);
151 static void setgroup(gid_t *flag, const char *name);
152 static void setuser(uid_t *flag, const char *name);
153 static zic_t tadd(const zic_t t1, const long t2);
154 static void usage(void);
155 static void writezone(const char *name, const char *string);
156 static int yearistype(int year, const char *type);
158 static int charcnt;
159 static int errors;
160 static const char * filename;
161 static int leapcnt;
162 static int leapseen;
163 static int leapminyear;
164 static int leapmaxyear;
165 static int linenum;
166 static int max_abbrvar_len;
167 static int max_format_len;
168 static zic_t max_time;
169 static int max_year;
170 static zic_t min_time;
171 static int min_year;
172 static int noise;
173 static const char * rfilename;
174 static int rlinenum;
175 static int timecnt;
176 static int typecnt;
179 ** Line codes.
182 #define LC_RULE 0
183 #define LC_ZONE 1
184 #define LC_LINK 2
185 #define LC_LEAP 3
188 ** Which fields are which on a Zone line.
191 #define ZF_NAME 1
192 #define ZF_GMTOFF 2
193 #define ZF_RULE 3
194 #define ZF_FORMAT 4
195 #define ZF_TILYEAR 5
196 #define ZF_TILMONTH 6
197 #define ZF_TILDAY 7
198 #define ZF_TILTIME 8
199 #define ZONE_MINFIELDS 5
200 #define ZONE_MAXFIELDS 9
203 ** Which fields are which on a Zone continuation line.
206 #define ZFC_GMTOFF 0
207 #define ZFC_RULE 1
208 #define ZFC_FORMAT 2
209 #define ZFC_TILYEAR 3
210 #define ZFC_TILMONTH 4
211 #define ZFC_TILDAY 5
212 #define ZFC_TILTIME 6
213 #define ZONEC_MINFIELDS 3
214 #define ZONEC_MAXFIELDS 7
217 ** Which files are which on a Rule line.
220 #define RF_NAME 1
221 #define RF_LOYEAR 2
222 #define RF_HIYEAR 3
223 #define RF_COMMAND 4
224 #define RF_MONTH 5
225 #define RF_DAY 6
226 #define RF_TOD 7
227 #define RF_STDOFF 8
228 #define RF_ABBRVAR 9
229 #define RULE_FIELDS 10
232 ** Which fields are which on a Link line.
235 #define LF_FROM 1
236 #define LF_TO 2
237 #define LINK_FIELDS 3
240 ** Which fields are which on a Leap line.
243 #define LP_YEAR 1
244 #define LP_MONTH 2
245 #define LP_DAY 3
246 #define LP_TIME 4
247 #define LP_CORR 5
248 #define LP_ROLL 6
249 #define LEAP_FIELDS 7
252 ** Year synonyms.
255 #define YR_MINIMUM 0
256 #define YR_MAXIMUM 1
257 #define YR_ONLY 2
259 static struct rule * rules;
260 static int nrules; /* number of rules */
262 static struct zone * zones;
263 static int nzones; /* number of zones */
265 struct link {
266 const char * l_filename;
267 int l_linenum;
268 const char * l_from;
269 const char * l_to;
272 static struct link * links;
273 static int nlinks;
275 struct lookup {
276 const char * l_word;
277 const int l_value;
280 static struct lookup const *byword(const char *string,
281 const struct lookup *lp);
283 static struct lookup const line_codes[] = {
284 { "Rule", LC_RULE },
285 { "Zone", LC_ZONE },
286 { "Link", LC_LINK },
287 { "Leap", LC_LEAP },
288 { NULL, 0}
291 static struct lookup const mon_names[] = {
292 { "January", TM_JANUARY },
293 { "February", TM_FEBRUARY },
294 { "March", TM_MARCH },
295 { "April", TM_APRIL },
296 { "May", TM_MAY },
297 { "June", TM_JUNE },
298 { "July", TM_JULY },
299 { "August", TM_AUGUST },
300 { "September", TM_SEPTEMBER },
301 { "October", TM_OCTOBER },
302 { "November", TM_NOVEMBER },
303 { "December", TM_DECEMBER },
304 { NULL, 0 }
307 static struct lookup const wday_names[] = {
308 { "Sunday", TM_SUNDAY },
309 { "Monday", TM_MONDAY },
310 { "Tuesday", TM_TUESDAY },
311 { "Wednesday", TM_WEDNESDAY },
312 { "Thursday", TM_THURSDAY },
313 { "Friday", TM_FRIDAY },
314 { "Saturday", TM_SATURDAY },
315 { NULL, 0 }
318 static struct lookup const lasts[] = {
319 { "last-Sunday", TM_SUNDAY },
320 { "last-Monday", TM_MONDAY },
321 { "last-Tuesday", TM_TUESDAY },
322 { "last-Wednesday", TM_WEDNESDAY },
323 { "last-Thursday", TM_THURSDAY },
324 { "last-Friday", TM_FRIDAY },
325 { "last-Saturday", TM_SATURDAY },
326 { NULL, 0 }
329 static struct lookup const begin_years[] = {
330 { "minimum", YR_MINIMUM },
331 { "maximum", YR_MAXIMUM },
332 { NULL, 0 }
335 static struct lookup const end_years[] = {
336 { "minimum", YR_MINIMUM },
337 { "maximum", YR_MAXIMUM },
338 { "only", YR_ONLY },
339 { NULL, 0 }
342 static struct lookup const leap_types[] = {
343 { "Rolling", TRUE },
344 { "Stationary", FALSE },
345 { NULL, 0 }
348 static const int len_months[2][MONSPERYEAR] = {
349 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
350 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
353 static const int len_years[2] = {
354 DAYSPERNYEAR, DAYSPERLYEAR
357 static struct attype {
358 zic_t at;
359 unsigned char type;
360 } attypes[TZ_MAX_TIMES];
361 static long gmtoffs[TZ_MAX_TYPES];
362 static char isdsts[TZ_MAX_TYPES];
363 static unsigned char abbrinds[TZ_MAX_TYPES];
364 static char ttisstds[TZ_MAX_TYPES];
365 static char ttisgmts[TZ_MAX_TYPES];
366 static char chars[TZ_MAX_CHARS];
367 static zic_t trans[TZ_MAX_LEAPS];
368 static long corr[TZ_MAX_LEAPS];
369 static char roll[TZ_MAX_LEAPS];
372 ** Memory allocation.
375 static char *
376 memcheck(char * const ptr)
378 if (ptr == NULL)
379 errx(EXIT_FAILURE, _("memory exhausted"));
380 return ptr;
383 #define emalloc(size) memcheck(imalloc(size))
384 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
385 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
386 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
389 ** Error handling.
392 static void
393 eats(const char * const name, const int num,
394 const char * const rname, const int rnum)
396 filename = name;
397 linenum = num;
398 rfilename = rname;
399 rlinenum = rnum;
402 static void
403 eat(const char * const name, const int num)
405 eats(name, num, NULL, -1);
408 static void
409 error(const char * const string)
412 ** Match the format of "cc" to allow sh users to
413 ** zic ... 2>&1 | error -t "*" -v
414 ** on BSD systems.
416 fprintf(stderr, _("\"%s\", line %d: %s"),
417 filename, linenum, string);
418 if (rfilename != NULL)
419 fprintf(stderr, _(" (rule from \"%s\", line %d)"),
420 rfilename, rlinenum);
421 fprintf(stderr, "\n");
422 ++errors;
425 static void
426 warning(const char * const string)
428 char * cp;
430 cp = ecpyalloc(_("warning: "));
431 cp = ecatalloc(cp, string);
432 error(cp);
433 ifree(cp);
434 --errors;
437 static void
438 usage(void)
440 fprintf(stderr, "%s\n%s\n",
441 _("usage: zic [-v] [-l localtime] [-p posixrules] [-d directory]"),
442 _(" [-L leapseconds] [-y yearistype] [filename ...]"));
443 exit(EXIT_FAILURE);
446 static const char * psxrules;
447 static const char * lcltime;
448 static const char * directory;
449 static const char * leapsec;
450 static const char * yitcommand;
451 static int Dflag;
452 static uid_t uflag = (uid_t)-1;
453 static gid_t gflag = (gid_t)-1;
454 static mode_t mflag = (S_IRUSR | S_IRGRP | S_IROTH
455 | S_IWUSR);
458 main(int argc, char *argv[])
460 int i;
461 int j;
462 int c;
464 #ifdef unix
465 umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
466 #endif /* defined unix */
467 while ((c = getopt(argc, argv, "Dd:g:l:m:p:L:u:vsy:")) != -1)
468 switch (c) {
469 default:
470 usage();
471 case 'D':
472 Dflag = 1;
473 break;
474 case 'd':
475 if (directory == NULL)
476 directory = optarg;
477 else
478 errx(EXIT_FAILURE,
479 _("more than one -d option specified"));
480 break;
481 case 'g':
482 setgroup(&gflag, optarg);
483 break;
484 case 'l':
485 if (lcltime == NULL)
486 lcltime = optarg;
487 else
488 errx(EXIT_FAILURE,
489 _("more than one -l option specified"));
490 break;
491 case 'm':
493 void *set = setmode(optarg);
494 getmode(set, mflag);
495 break;
497 case 'p':
498 if (psxrules == NULL)
499 psxrules = optarg;
500 else
501 errx(EXIT_FAILURE,
502 _("more than one -p option specified"));
503 break;
504 case 'u':
505 setuser(&uflag, optarg);
506 break;
507 case 'y':
508 if (yitcommand == NULL)
509 yitcommand = optarg;
510 else
511 errx(EXIT_FAILURE,
512 _("more than one -y option specified"));
513 break;
514 case 'L':
515 if (leapsec == NULL)
516 leapsec = optarg;
517 else
518 errx(EXIT_FAILURE,
519 _("more than one -L option specified"));
520 break;
521 case 'v':
522 noise = TRUE;
523 break;
524 case 's':
525 warnx(_("-s ignored\n"));
526 break;
528 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
529 usage(); /* usage message by request */
530 if (directory == NULL)
531 directory = TZDIR;
532 if (yitcommand == NULL)
533 yitcommand = "yearistype";
535 setboundaries();
537 if (optind < argc && leapsec != NULL) {
538 infile(leapsec);
539 adjleap();
542 for (i = optind; i < argc; ++i)
543 infile(argv[i]);
544 if (errors)
545 exit(EXIT_FAILURE);
546 associate();
547 for (i = 0; i < nzones; i = j) {
549 ** Find the next non-continuation zone entry.
551 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
552 continue;
553 outzone(&zones[i], j - i);
556 ** Make links.
558 for (i = 0; i < nlinks; ++i) {
559 eat(links[i].l_filename, links[i].l_linenum);
560 dolink(links[i].l_from, links[i].l_to);
561 if (noise)
562 for (j = 0; j < nlinks; ++j)
563 if (strcmp(links[i].l_to,
564 links[j].l_from) == 0)
565 warning(_("link to link"));
567 if (lcltime != NULL) {
568 eat("command line", 1);
569 dolink(lcltime, TZDEFAULT);
571 if (psxrules != NULL) {
572 eat("command line", 1);
573 dolink(psxrules, TZDEFRULES);
575 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
578 static void
579 dolink(const char * const fromfield, const char * const tofield)
581 char *fromname;
582 char *toname;
584 if (fromfield[0] == '/')
585 fromname = ecpyalloc(fromfield);
586 else {
587 fromname = ecpyalloc(directory);
588 fromname = ecatalloc(fromname, "/");
589 fromname = ecatalloc(fromname, fromfield);
591 if (tofield[0] == '/')
592 toname = ecpyalloc(tofield);
593 else {
594 toname = ecpyalloc(directory);
595 toname = ecatalloc(toname, "/");
596 toname = ecatalloc(toname, tofield);
599 ** We get to be careful here since
600 ** there's a fair chance of root running us.
602 if (!itsdir(toname))
603 remove(toname);
604 if (link(fromname, toname) != 0) {
605 int result;
607 if (mkdirs(toname) != 0)
608 exit(EXIT_FAILURE);
610 result = link(fromname, toname);
611 if (result != 0 &&
612 access(fromname, F_OK) == 0 &&
613 !itsdir(fromname)) {
614 const char *s = tofield;
615 char * symlinkcontents = NULL;
617 while ((s = strchr(s+1, '/')) != NULL)
618 symlinkcontents =
619 ecatalloc(symlinkcontents,
620 "../");
621 symlinkcontents =
622 ecatalloc(symlinkcontents,
623 fromname);
624 result = symlink(symlinkcontents,
625 toname);
626 if (result == 0)
627 warning(_("hard link failed, symbolic link used"));
628 ifree(symlinkcontents);
630 if (result != 0) {
631 err(EXIT_FAILURE, _("can't link from %s to %s"),
632 fromname, toname);
635 ifree(fromname);
636 ifree(toname);
639 #define TIME_T_BITS_IN_FILE 64
641 static void
642 setboundaries(void)
644 int i;
646 min_time = -1;
647 for (i = 0; i < TIME_T_BITS_IN_FILE - 1; ++i)
648 min_time *= 2;
649 max_time = -(min_time + 1);
652 static int
653 itsdir(const char * const name)
655 char * myname;
656 int accres;
658 myname = ecpyalloc(name);
659 myname = ecatalloc(myname, "/.");
660 accres = access(myname, F_OK);
661 ifree(myname);
662 return accres == 0;
666 ** Associate sets of rules with zones.
670 ** Sort by rule name.
673 static int
674 rcomp(const void *cp1, const void *cp2)
676 return strcmp(((const struct rule *) cp1)->r_name,
677 ((const struct rule *) cp2)->r_name);
680 static void
681 associate(void)
683 struct zone *zp;
684 struct rule *rp;
685 int base, out;
686 int i, j;
688 if (nrules != 0) {
689 qsort((void *) rules, (size_t) nrules,
690 (size_t) sizeof *rules, rcomp);
691 for (i = 0; i < nrules - 1; ++i) {
692 if (strcmp(rules[i].r_name,
693 rules[i + 1].r_name) != 0)
694 continue;
695 if (strcmp(rules[i].r_filename,
696 rules[i + 1].r_filename) == 0)
697 continue;
698 eat(rules[i].r_filename, rules[i].r_linenum);
699 warning(_("same rule name in multiple files"));
700 eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
701 warning(_("same rule name in multiple files"));
702 for (j = i + 2; j < nrules; ++j) {
703 if (strcmp(rules[i].r_name,
704 rules[j].r_name) != 0)
705 break;
706 if (strcmp(rules[i].r_filename,
707 rules[j].r_filename) == 0)
708 continue;
709 if (strcmp(rules[i + 1].r_filename,
710 rules[j].r_filename) == 0)
711 continue;
712 break;
714 i = j - 1;
717 for (i = 0; i < nzones; ++i) {
718 zp = &zones[i];
719 zp->z_rules = NULL;
720 zp->z_nrules = 0;
722 for (base = 0; base < nrules; base = out) {
723 rp = &rules[base];
724 for (out = base + 1; out < nrules; ++out)
725 if (strcmp(rp->r_name, rules[out].r_name) != 0)
726 break;
727 for (i = 0; i < nzones; ++i) {
728 zp = &zones[i];
729 if (strcmp(zp->z_rule, rp->r_name) != 0)
730 continue;
731 zp->z_rules = rp;
732 zp->z_nrules = out - base;
735 for (i = 0; i < nzones; ++i) {
736 zp = &zones[i];
737 if (zp->z_nrules == 0) {
739 ** Maybe we have a local standard time offset.
741 eat(zp->z_filename, zp->z_linenum);
742 zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
743 TRUE);
745 ** Note, though, that if there's no rule,
746 ** a '%s' in the format is a bad thing.
748 if (strchr(zp->z_format, '%') != 0)
749 error(_("%s in ruleless zone"));
752 if (errors)
753 exit(EXIT_FAILURE);
756 static void
757 infile(const char *name)
759 FILE *fp;
760 char **fields;
761 char *cp;
762 const struct lookup *lp;
763 int nfields;
764 int wantcont;
765 int num;
766 char buf[BUFSIZ];
768 if (strcmp(name, "-") == 0) {
769 name = _("standard input");
770 fp = stdin;
771 } else if ((fp = fopen(name, "r")) == NULL)
772 err(EXIT_FAILURE, _("can't open %s"), name);
773 wantcont = FALSE;
774 for (num = 1; ; ++num) {
775 eat(name, num);
776 if (fgets(buf, (int) sizeof buf, fp) != buf)
777 break;
778 cp = strchr(buf, '\n');
779 if (cp == NULL) {
780 error(_("line too long"));
781 exit(EXIT_FAILURE);
783 *cp = '\0';
784 fields = getfields(buf);
785 nfields = 0;
786 while (fields[nfields] != NULL) {
787 static char nada;
789 if (strcmp(fields[nfields], "-") == 0)
790 fields[nfields] = &nada;
791 ++nfields;
793 if (nfields == 0) {
794 /* nothing to do */
795 } else if (wantcont) {
796 wantcont = inzcont(fields, nfields);
797 } else {
798 lp = byword(fields[0], line_codes);
799 if (lp == NULL)
800 error(_("input line of unknown type"));
801 else switch ((int) (lp->l_value)) {
802 case LC_RULE:
803 inrule(fields, nfields);
804 wantcont = FALSE;
805 break;
806 case LC_ZONE:
807 wantcont = inzone(fields, nfields);
808 break;
809 case LC_LINK:
810 inlink(fields, nfields);
811 wantcont = FALSE;
812 break;
813 case LC_LEAP:
814 if (name != leapsec)
815 warnx(
816 _("leap line in non leap seconds file %s"), name);
817 else inleap(fields, nfields);
818 wantcont = FALSE;
819 break;
820 default: /* "cannot happen" */
821 errx(EXIT_FAILURE,
822 _("panic: invalid l_value %d"), lp->l_value);
825 ifree((char *) fields);
827 if (ferror(fp))
828 errx(EXIT_FAILURE, _("error reading %s"), filename);
829 if (fp != stdin && fclose(fp))
830 err(EXIT_FAILURE, _("error closing %s"), filename);
831 if (wantcont)
832 error(_("expected continuation line not found"));
836 ** Convert a string of one of the forms
837 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
838 ** into a number of seconds.
839 ** A null string maps to zero.
840 ** Call error with errstring and return zero on errors.
843 static long
844 gethms(const char *string, const char * const errstring, const int signable)
846 long hh;
847 int mm, ss, sign;
849 if (string == NULL || *string == '\0')
850 return 0;
851 if (!signable)
852 sign = 1;
853 else if (*string == '-') {
854 sign = -1;
855 ++string;
856 } else sign = 1;
857 if (sscanf(string, scheck(string, "%ld"), &hh) == 1)
858 mm = ss = 0;
859 else if (sscanf(string, scheck(string, "%ld:%d"), &hh, &mm) == 2)
860 ss = 0;
861 else if (sscanf(string, scheck(string, "%ld:%d:%d"),
862 &hh, &mm, &ss) != 3) {
863 error(errstring);
864 return 0;
866 if (hh < 0 ||
867 mm < 0 || mm >= MINSPERHOUR ||
868 ss < 0 || ss > SECSPERMIN) {
869 error(errstring);
870 return 0;
872 if (LONG_MAX / SECSPERHOUR < hh) {
873 error(_("time overflow"));
874 return 0;
876 if (noise && hh == HOURSPERDAY && mm == 0 && ss == 0)
877 warning(_("24:00 not handled by pre-1998 versions of zic"));
878 if (noise && (hh > HOURSPERDAY ||
879 (hh == HOURSPERDAY && (mm != 0 || ss != 0))))
880 warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
881 return oadd(eitol(sign) * hh * eitol(SECSPERHOUR),
882 eitol(sign) * (eitol(mm) * eitol(SECSPERMIN) + eitol(ss)));
885 static void
886 inrule(char ** const fields, const int nfields)
888 static struct rule r;
890 if (nfields != RULE_FIELDS) {
891 error(_("wrong number of fields on Rule line"));
892 return;
894 if (*fields[RF_NAME] == '\0') {
895 error(_("nameless rule"));
896 return;
898 r.r_filename = filename;
899 r.r_linenum = linenum;
900 r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
901 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
902 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
903 r.r_name = ecpyalloc(fields[RF_NAME]);
904 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
905 if (max_abbrvar_len < strlen(r.r_abbrvar))
906 max_abbrvar_len = strlen(r.r_abbrvar);
907 rules = (struct rule *) (void *) erealloc((char *) rules,
908 (int) ((nrules + 1) * sizeof *rules));
909 rules[nrules++] = r;
912 static int
913 inzone(char ** const fields, const int nfields)
915 int i;
916 static char *buf;
918 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
919 error(_("wrong number of fields on Zone line"));
920 return FALSE;
922 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
923 buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
924 sprintf(buf,
925 _("\"Zone %s\" line and -l option are mutually exclusive"),
926 TZDEFAULT);
927 error(buf);
928 return FALSE;
930 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
931 buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
932 sprintf(buf,
933 _("\"Zone %s\" line and -p option are mutually exclusive"),
934 TZDEFRULES);
935 error(buf);
936 return FALSE;
938 for (i = 0; i < nzones; ++i)
939 if (zones[i].z_name != NULL &&
940 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
941 buf = erealloc(buf, (int) (132 +
942 strlen(fields[ZF_NAME]) +
943 strlen(zones[i].z_filename)));
944 sprintf(buf,
945 _("duplicate zone name %s (file \"%s\", line %d)"),
946 fields[ZF_NAME],
947 zones[i].z_filename,
948 zones[i].z_linenum);
949 error(buf);
950 return FALSE;
952 return inzsub(fields, nfields, FALSE);
955 static int
956 inzcont(char ** const fields, const int nfields)
958 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
959 error(_("wrong number of fields on Zone continuation line"));
960 return FALSE;
962 return inzsub(fields, nfields, TRUE);
965 static int
966 inzsub(char ** const fields, const int nfields, const int iscont)
968 char *cp;
969 static struct zone z;
970 int i_gmtoff, i_rule, i_format;
971 int i_untilyear, i_untilmonth;
972 int i_untilday, i_untiltime;
973 int hasuntil;
975 if (iscont) {
976 i_gmtoff = ZFC_GMTOFF;
977 i_rule = ZFC_RULE;
978 i_format = ZFC_FORMAT;
979 i_untilyear = ZFC_TILYEAR;
980 i_untilmonth = ZFC_TILMONTH;
981 i_untilday = ZFC_TILDAY;
982 i_untiltime = ZFC_TILTIME;
983 z.z_name = NULL;
984 } else {
985 i_gmtoff = ZF_GMTOFF;
986 i_rule = ZF_RULE;
987 i_format = ZF_FORMAT;
988 i_untilyear = ZF_TILYEAR;
989 i_untilmonth = ZF_TILMONTH;
990 i_untilday = ZF_TILDAY;
991 i_untiltime = ZF_TILTIME;
992 z.z_name = ecpyalloc(fields[ZF_NAME]);
994 z.z_filename = filename;
995 z.z_linenum = linenum;
996 z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UTC offset"), TRUE);
997 if ((cp = strchr(fields[i_format], '%')) != 0) {
998 if (*++cp != 's' || strchr(cp, '%') != 0) {
999 error(_("invalid abbreviation format"));
1000 return FALSE;
1003 z.z_rule = ecpyalloc(fields[i_rule]);
1004 z.z_format = ecpyalloc(fields[i_format]);
1005 if (max_format_len < strlen(z.z_format))
1006 max_format_len = strlen(z.z_format);
1007 hasuntil = nfields > i_untilyear;
1008 if (hasuntil) {
1009 z.z_untilrule.r_filename = filename;
1010 z.z_untilrule.r_linenum = linenum;
1011 rulesub(&z.z_untilrule,
1012 fields[i_untilyear],
1013 "only",
1015 (nfields > i_untilmonth) ?
1016 fields[i_untilmonth] : "Jan",
1017 (nfields > i_untilday) ? fields[i_untilday] : "1",
1018 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1019 z.z_untiltime = rpytime(&z.z_untilrule,
1020 z.z_untilrule.r_loyear);
1021 if (iscont && nzones > 0 &&
1022 z.z_untiltime > min_time &&
1023 z.z_untiltime < max_time &&
1024 zones[nzones - 1].z_untiltime > min_time &&
1025 zones[nzones - 1].z_untiltime < max_time &&
1026 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1027 error(_(
1028 "Zone continuation line end time is not after end time of previous line"
1030 return FALSE;
1033 zones = (struct zone *) (void *) erealloc((char *) zones,
1034 (int) ((nzones + 1) * sizeof *zones));
1035 zones[nzones++] = z;
1037 ** If there was an UNTIL field on this line,
1038 ** there's more information about the zone on the next line.
1040 return hasuntil;
1043 static void
1044 inleap(char ** const fields, const int nfields)
1046 const char *cp;
1047 const struct lookup *lp;
1048 int i, j;
1049 int year, month, day;
1050 long dayoff, tod;
1051 zic_t t;
1053 if (nfields != LEAP_FIELDS) {
1054 error(_("wrong number of fields on Leap line"));
1055 return;
1057 dayoff = 0;
1058 cp = fields[LP_YEAR];
1059 if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
1061 ** Leapin' Lizards!
1063 error(_("invalid leaping year"));
1064 return;
1066 if (!leapseen || leapmaxyear < year)
1067 leapmaxyear = year;
1068 if (!leapseen || leapminyear > year)
1069 leapminyear = year;
1070 leapseen = TRUE;
1071 j = EPOCH_YEAR;
1072 while (j != year) {
1073 if (year > j) {
1074 i = len_years[isleap(j)];
1075 ++j;
1076 } else {
1077 --j;
1078 i = -len_years[isleap(j)];
1080 dayoff = oadd(dayoff, eitol(i));
1082 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1083 error(_("invalid month name"));
1084 return;
1086 month = lp->l_value;
1087 j = TM_JANUARY;
1088 while (j != month) {
1089 i = len_months[isleap(year)][j];
1090 dayoff = oadd(dayoff, eitol(i));
1091 ++j;
1093 cp = fields[LP_DAY];
1094 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1095 day <= 0 || day > len_months[isleap(year)][month]) {
1096 error(_("invalid day of month"));
1097 return;
1099 dayoff = oadd(dayoff, eitol(day - 1));
1100 if (dayoff < 0 && !TYPE_SIGNED(zic_t)) {
1101 error(_("time before zero"));
1102 return;
1104 if (dayoff < min_time / SECSPERDAY) {
1105 error(_("time too small"));
1106 return;
1108 if (dayoff > max_time / SECSPERDAY) {
1109 error(_("time too large"));
1110 return;
1112 t = (zic_t) dayoff * SECSPERDAY;
1113 tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1114 cp = fields[LP_CORR];
1116 int positive;
1117 int count;
1119 if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1120 positive = FALSE;
1121 count = 1;
1122 } else if (strcmp(cp, "--") == 0) {
1123 positive = FALSE;
1124 count = 2;
1125 } else if (strcmp(cp, "+") == 0) {
1126 positive = TRUE;
1127 count = 1;
1128 } else if (strcmp(cp, "++") == 0) {
1129 positive = TRUE;
1130 count = 2;
1131 } else {
1132 error(_("illegal CORRECTION field on Leap line"));
1133 return;
1135 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1136 error(_(
1137 "illegal Rolling/Stationary field on Leap line"
1139 return;
1141 leapadd(tadd(t, tod), positive, lp->l_value, count);
1145 static void
1146 inlink(char ** const fields, const int nfields)
1148 struct link l;
1150 if (nfields != LINK_FIELDS) {
1151 error(_("wrong number of fields on Link line"));
1152 return;
1154 if (*fields[LF_FROM] == '\0') {
1155 error(_("blank FROM field on Link line"));
1156 return;
1158 if (*fields[LF_TO] == '\0') {
1159 error(_("blank TO field on Link line"));
1160 return;
1162 l.l_filename = filename;
1163 l.l_linenum = linenum;
1164 l.l_from = ecpyalloc(fields[LF_FROM]);
1165 l.l_to = ecpyalloc(fields[LF_TO]);
1166 links = (struct link *) (void *) erealloc((char *) links,
1167 (int) ((nlinks + 1) * sizeof *links));
1168 links[nlinks++] = l;
1171 static void
1172 rulesub(struct rule * const rp,
1173 const char * const loyearp,
1174 const char * const hiyearp,
1175 const char * const typep,
1176 const char * const monthp,
1177 const char * const dayp,
1178 const char * const timep)
1180 const struct lookup *lp;
1181 const char *cp;
1182 char *dp;
1183 char *ep;
1185 if ((lp = byword(monthp, mon_names)) == NULL) {
1186 error(_("invalid month name"));
1187 return;
1189 rp->r_month = lp->l_value;
1190 rp->r_todisstd = FALSE;
1191 rp->r_todisgmt = FALSE;
1192 dp = ecpyalloc(timep);
1193 if (*dp != '\0') {
1194 ep = dp + strlen(dp) - 1;
1195 switch (lowerit(*ep)) {
1196 case 's': /* Standard */
1197 rp->r_todisstd = TRUE;
1198 rp->r_todisgmt = FALSE;
1199 *ep = '\0';
1200 break;
1201 case 'w': /* Wall */
1202 rp->r_todisstd = FALSE;
1203 rp->r_todisgmt = FALSE;
1204 *ep = '\0';
1205 break;
1206 case 'g': /* Greenwich */
1207 case 'u': /* Universal */
1208 case 'z': /* Zulu */
1209 rp->r_todisstd = TRUE;
1210 rp->r_todisgmt = TRUE;
1211 *ep = '\0';
1212 break;
1215 rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1216 ifree(dp);
1218 ** Year work.
1220 cp = loyearp;
1221 lp = byword(cp, begin_years);
1222 rp->r_lowasnum = lp == NULL;
1223 if (!rp->r_lowasnum) switch ((int) lp->l_value) {
1224 case YR_MINIMUM:
1225 rp->r_loyear = INT_MIN;
1226 break;
1227 case YR_MAXIMUM:
1228 rp->r_loyear = INT_MAX;
1229 break;
1230 default: /* "cannot happen" */
1231 errx(EXIT_FAILURE,
1232 _("panic: invalid l_value %d"), lp->l_value);
1233 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
1234 error(_("invalid starting year"));
1235 return;
1237 cp = hiyearp;
1238 lp = byword(cp, end_years);
1239 rp->r_hiwasnum = lp == NULL;
1240 if (!rp->r_hiwasnum) switch ((int) lp->l_value) {
1241 case YR_MINIMUM:
1242 rp->r_hiyear = INT_MIN;
1243 break;
1244 case YR_MAXIMUM:
1245 rp->r_hiyear = INT_MAX;
1246 break;
1247 case YR_ONLY:
1248 rp->r_hiyear = rp->r_loyear;
1249 break;
1250 default: /* "cannot happen" */
1251 errx(EXIT_FAILURE,
1252 _("panic: invalid l_value %d"), lp->l_value);
1253 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
1254 error(_("invalid ending year"));
1255 return;
1257 if (rp->r_loyear > rp->r_hiyear) {
1258 error(_("starting year greater than ending year"));
1259 return;
1261 if (*typep == '\0')
1262 rp->r_yrtype = NULL;
1263 else {
1264 if (rp->r_loyear == rp->r_hiyear) {
1265 error(_("typed single year"));
1266 return;
1268 rp->r_yrtype = ecpyalloc(typep);
1271 ** Day work.
1272 ** Accept things such as:
1273 ** 1
1274 ** last-Sunday
1275 ** Sun<=20
1276 ** Sun>=7
1278 dp = ecpyalloc(dayp);
1279 if ((lp = byword(dp, lasts)) != NULL) {
1280 rp->r_dycode = DC_DOWLEQ;
1281 rp->r_wday = lp->l_value;
1282 rp->r_dayofmonth = len_months[1][rp->r_month];
1283 } else {
1284 if ((ep = strchr(dp, '<')) != 0)
1285 rp->r_dycode = DC_DOWLEQ;
1286 else if ((ep = strchr(dp, '>')) != 0)
1287 rp->r_dycode = DC_DOWGEQ;
1288 else {
1289 ep = dp;
1290 rp->r_dycode = DC_DOM;
1292 if (rp->r_dycode != DC_DOM) {
1293 *ep++ = 0;
1294 if (*ep++ != '=') {
1295 error(_("invalid day of month"));
1296 ifree(dp);
1297 return;
1299 if ((lp = byword(dp, wday_names)) == NULL) {
1300 error(_("invalid weekday name"));
1301 ifree(dp);
1302 return;
1304 rp->r_wday = lp->l_value;
1306 if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1307 rp->r_dayofmonth <= 0 ||
1308 (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1309 error(_("invalid day of month"));
1310 ifree(dp);
1311 return;
1314 ifree(dp);
1317 static void
1318 convert(const long val, char * const buf)
1320 int i;
1321 int shift;
1323 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1324 buf[i] = val >> shift;
1327 static void
1328 convert64(const zic_t val, char * const buf)
1330 int i;
1331 int shift;
1333 for (i = 0, shift = 56; i < 8; ++i, shift -= 8)
1334 buf[i] = val >> shift;
1337 static void
1338 puttzcode(const long val, FILE * const fp)
1340 char buf[4];
1342 convert(val, buf);
1343 fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1346 static void
1347 puttzcode64(const zic_t val, FILE * const fp)
1349 char buf[8];
1351 convert64(val, buf);
1352 fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1355 static int
1356 atcomp(const void * avp, const void * bvp)
1358 const zic_t a = ((const struct attype *) avp)->at;
1359 const zic_t b = ((const struct attype *) bvp)->at;
1361 return (a < b) ? -1 : (a > b);
1364 static int
1365 is32(const zic_t x)
1367 return INT32_MIN <= x && x <= INT32_MAX;
1370 static void
1371 writezone(const char * const name, const char * const string)
1373 FILE * fp;
1374 int i, j;
1375 int leapcnt32, leapi32;
1376 int timecnt32, timei32;
1377 int pass;
1378 static char * fullname;
1379 static const struct tzhead tzh0;
1380 static struct tzhead tzh;
1381 zic_t ats[TZ_MAX_TIMES];
1382 unsigned char types[TZ_MAX_TIMES];
1385 ** Sort.
1387 if (timecnt > 1)
1388 qsort((void *) attypes, (size_t) timecnt,
1389 (size_t) sizeof *attypes, atcomp);
1391 ** Optimize.
1394 int fromi;
1395 int toi;
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 && ((attypes[fromi].at +
1406 gmtoffs[attypes[toi - 1].type]) <=
1407 (attypes[toi - 1].at + gmtoffs[toi == 1 ? 0
1408 : attypes[toi - 2].type]))) {
1409 attypes[toi - 1].type =
1410 attypes[fromi].type;
1411 continue;
1413 if (toi == 0 ||
1414 attypes[toi - 1].type != attypes[fromi].type)
1415 attypes[toi++] = attypes[fromi];
1417 timecnt = toi;
1420 ** Transfer.
1422 for (i = 0; i < timecnt; ++i) {
1423 ats[i] = attypes[i].at;
1424 types[i] = attypes[i].type;
1427 ** Correct for leap seconds.
1429 for (i = 0; i < timecnt; ++i) {
1430 j = leapcnt;
1431 while (--j >= 0)
1432 if (ats[i] > trans[j] - corr[j]) {
1433 ats[i] = tadd(ats[i], corr[j]);
1434 break;
1438 ** Figure out 32-bit-limited starts and counts.
1440 timecnt32 = timecnt;
1441 timei32 = 0;
1442 leapcnt32 = leapcnt;
1443 leapi32 = 0;
1444 while (timecnt32 > 0 && !is32(ats[timecnt32 - 1]))
1445 --timecnt32;
1446 while (timecnt32 > 0 && !is32(ats[timei32])) {
1447 --timecnt32;
1448 ++timei32;
1450 while (leapcnt32 > 0 && !is32(trans[leapcnt32 - 1]))
1451 --leapcnt32;
1452 while (leapcnt32 > 0 && !is32(trans[leapi32])) {
1453 --leapcnt32;
1454 ++leapi32;
1456 fullname = erealloc(fullname,
1457 (int) (strlen(directory) + 1 + strlen(name) + 1));
1458 sprintf(fullname, "%s/%s", directory, name);
1460 ** Remove old file, if any, to snap links.
1462 if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT)
1463 err(EXIT_FAILURE, _("can't remove %s"), fullname);
1464 if ((fp = fopen(fullname, "wb")) == NULL) {
1465 if (mkdirs(fullname) != 0)
1466 exit(EXIT_FAILURE);
1467 if ((fp = fopen(fullname, "wb")) == NULL)
1468 err(EXIT_FAILURE, _("can't create %s"), fullname);
1470 for (pass = 1; pass <= 2; ++pass) {
1471 int thistimei, thistimecnt;
1472 int thisleapi, thisleapcnt;
1473 int thistimelim, thisleaplim;
1474 int writetype[TZ_MAX_TIMES];
1475 int typemap[TZ_MAX_TYPES];
1476 int thistypecnt;
1477 char thischars[TZ_MAX_CHARS];
1478 char thischarcnt;
1479 int indmap[TZ_MAX_CHARS];
1481 if (pass == 1) {
1482 thistimei = timei32;
1483 thistimecnt = timecnt32;
1484 thisleapi = leapi32;
1485 thisleapcnt = leapcnt32;
1486 } else {
1487 thistimei = 0;
1488 thistimecnt = timecnt;
1489 thisleapi = 0;
1490 thisleapcnt = leapcnt;
1492 thistimelim = thistimei + thistimecnt;
1493 thisleaplim = thisleapi + thisleapcnt;
1494 for (i = 0; i < typecnt; ++i)
1495 writetype[i] = thistimecnt == timecnt;
1496 if (thistimecnt == 0) {
1498 ** No transition times fall in the current
1499 ** (32- or 64-bit) window.
1501 if (typecnt != 0)
1502 writetype[typecnt - 1] = TRUE;
1503 } else {
1504 for (i = thistimei - 1; i < thistimelim; ++i)
1505 if (i >= 0)
1506 writetype[types[i]] = TRUE;
1508 ** For America/Godthab and Antarctica/Palmer
1510 if (thistimei == 0)
1511 writetype[0] = TRUE;
1513 thistypecnt = 0;
1514 for (i = 0; i < typecnt; ++i)
1515 typemap[i] = writetype[i] ? thistypecnt++ : -1;
1516 for (i = 0; i < sizeof indmap / sizeof indmap[0]; ++i)
1517 indmap[i] = -1;
1518 thischarcnt = 0;
1519 for (i = 0; i < typecnt; ++i) {
1520 char * thisabbr;
1522 if (!writetype[i])
1523 continue;
1524 if (indmap[abbrinds[i]] >= 0)
1525 continue;
1526 thisabbr = &chars[abbrinds[i]];
1527 for (j = 0; j < thischarcnt; ++j)
1528 if (strcmp(&thischars[j], thisabbr) == 0)
1529 break;
1530 if (j == thischarcnt) {
1531 strcpy(&thischars[(int) thischarcnt],
1532 thisabbr);
1533 thischarcnt += strlen(thisabbr) + 1;
1535 indmap[abbrinds[i]] = j;
1537 #define DO(field) fwrite((void *) tzh.field, \
1538 (size_t) sizeof tzh.field, (size_t) 1, fp)
1539 tzh = tzh0;
1540 strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1541 tzh.tzh_version[0] = ZIC_VERSION;
1542 convert(eitol(thistypecnt), tzh.tzh_ttisgmtcnt);
1543 convert(eitol(thistypecnt), tzh.tzh_ttisstdcnt);
1544 convert(eitol(thisleapcnt), tzh.tzh_leapcnt);
1545 convert(eitol(thistimecnt), tzh.tzh_timecnt);
1546 convert(eitol(thistypecnt), tzh.tzh_typecnt);
1547 convert(eitol(thischarcnt), tzh.tzh_charcnt);
1548 DO(tzh_magic);
1549 DO(tzh_version);
1550 DO(tzh_reserved);
1551 DO(tzh_ttisgmtcnt);
1552 DO(tzh_ttisstdcnt);
1553 DO(tzh_leapcnt);
1554 DO(tzh_timecnt);
1555 DO(tzh_typecnt);
1556 DO(tzh_charcnt);
1557 #undef DO
1558 for (i = thistimei; i < thistimelim; ++i)
1559 if (pass == 1)
1560 puttzcode((long) ats[i], fp);
1561 else puttzcode64(ats[i], fp);
1562 for (i = thistimei; i < thistimelim; ++i) {
1563 unsigned char uc;
1565 uc = typemap[types[i]];
1566 fwrite((void *) &uc,
1567 (size_t) sizeof uc,
1568 (size_t) 1,
1569 fp);
1571 for (i = 0; i < typecnt; ++i)
1572 if (writetype[i]) {
1573 puttzcode(gmtoffs[i], fp);
1574 putc(isdsts[i], fp);
1575 putc((unsigned char) indmap[abbrinds[i]], fp);
1577 if (thischarcnt != 0)
1578 fwrite((void *) thischars,
1579 (size_t) sizeof thischars[0],
1580 (size_t) thischarcnt, fp);
1581 for (i = thisleapi; i < thisleaplim; ++i) {
1582 register zic_t todo;
1584 if (roll[i]) {
1585 if (timecnt == 0 || trans[i] < ats[0]) {
1586 j = 0;
1587 while (isdsts[j])
1588 if (++j >= typecnt) {
1589 j = 0;
1590 break;
1592 } else {
1593 j = 1;
1594 while (j < timecnt &&
1595 trans[i] >= ats[j])
1596 ++j;
1597 j = types[j - 1];
1599 todo = tadd(trans[i], -gmtoffs[j]);
1600 } else todo = trans[i];
1601 if (pass == 1)
1602 puttzcode((long) todo, fp);
1603 else puttzcode64(todo, fp);
1604 puttzcode(corr[i], fp);
1606 for (i = 0; i < typecnt; ++i)
1607 if (writetype[i])
1608 putc(ttisstds[i], fp);
1609 for (i = 0; i < typecnt; ++i)
1610 if (writetype[i])
1611 putc(ttisgmts[i], fp);
1613 fprintf(fp, "\n%s\n", string);
1614 if (ferror(fp) || fclose(fp))
1615 errx(EXIT_FAILURE, _("error writing %s"), fullname);
1616 if (chmod(fullname, mflag) < 0)
1617 err(EXIT_FAILURE, _("cannot change mode of %s to %03o"),
1618 fullname, (unsigned)mflag);
1619 if ((uflag != (uid_t)-1 || gflag != (gid_t)-1)
1620 && chown(fullname, uflag, gflag) < 0)
1621 err(EXIT_FAILURE, _("cannot change ownership of %s"),
1622 fullname);
1625 static void
1626 doabbr(char * const abbr, const char * const format,
1627 const char * const letters, const int isdst, const int doquotes)
1629 char * cp;
1630 char * slashp;
1631 int len;
1633 slashp = strchr(format, '/');
1634 if (slashp == NULL) {
1635 if (letters == NULL)
1636 strcpy(abbr, format);
1637 else sprintf(abbr, format, letters);
1638 } else if (isdst) {
1639 strcpy(abbr, slashp + 1);
1640 } else {
1641 if (slashp > format)
1642 strncpy(abbr, format,
1643 (unsigned) (slashp - format));
1644 abbr[slashp - format] = '\0';
1646 if (!doquotes)
1647 return;
1648 for (cp = abbr; *cp != '\0'; ++cp)
1649 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp) == NULL &&
1650 strchr("abcdefghijklmnopqrstuvwxyz", *cp) == NULL)
1651 break;
1652 len = strlen(abbr);
1653 if (len > 0 && *cp == '\0')
1654 return;
1655 abbr[len + 2] = '\0';
1656 abbr[len + 1] = '>';
1657 for ( ; len > 0; --len)
1658 abbr[len] = abbr[len - 1];
1659 abbr[0] = '<';
1662 static void
1663 updateminmax(const int x)
1665 if (min_year > x)
1666 min_year = x;
1667 if (max_year < x)
1668 max_year = x;
1671 static int
1672 stringoffset(char *result, long offset)
1674 int hours;
1675 int minutes;
1676 int seconds;
1678 result[0] = '\0';
1679 if (offset < 0) {
1680 strcpy(result, "-");
1681 offset = -offset;
1683 seconds = offset % SECSPERMIN;
1684 offset /= SECSPERMIN;
1685 minutes = offset % MINSPERHOUR;
1686 offset /= MINSPERHOUR;
1687 hours = offset;
1688 if (hours >= HOURSPERDAY) {
1689 result[0] = '\0';
1690 return -1;
1692 sprintf(end(result), "%d", hours);
1693 if (minutes != 0 || seconds != 0) {
1694 sprintf(end(result), ":%02d", minutes);
1695 if (seconds != 0)
1696 sprintf(end(result), ":%02d", seconds);
1698 return 0;
1701 static int
1702 stringrule(char *result, const struct rule * const rp, const long dstoff,
1703 const long gmtoff)
1705 long tod;
1707 result = end(result);
1708 if (rp->r_dycode == DC_DOM) {
1709 int month, total;
1711 if (rp->r_dayofmonth == 29 && rp->r_month == TM_FEBRUARY)
1712 return -1;
1713 total = 0;
1714 for (month = 0; month < rp->r_month; ++month)
1715 total += len_months[0][month];
1716 sprintf(result, "J%d", total + rp->r_dayofmonth);
1717 } else {
1718 int week;
1720 if (rp->r_dycode == DC_DOWGEQ) {
1721 week = 1 + rp->r_dayofmonth / DAYSPERWEEK;
1722 if ((week - 1) * DAYSPERWEEK + 1 != rp->r_dayofmonth)
1723 return -1;
1724 } else if (rp->r_dycode == DC_DOWLEQ) {
1725 if (rp->r_dayofmonth == len_months[1][rp->r_month])
1726 week = 5;
1727 else {
1728 week = 1 + rp->r_dayofmonth / DAYSPERWEEK;
1729 if (week * DAYSPERWEEK - 1 != rp->r_dayofmonth)
1730 return -1;
1732 } else return -1; /* "cannot happen" */
1733 sprintf(result, "M%d.%d.%d",
1734 rp->r_month + 1, week, rp->r_wday);
1736 tod = rp->r_tod;
1737 if (rp->r_todisgmt)
1738 tod += gmtoff;
1739 if (rp->r_todisstd && rp->r_stdoff == 0)
1740 tod += dstoff;
1741 if (tod < 0) {
1742 result[0] = '\0';
1743 return -1;
1745 if (tod != 2 * SECSPERMIN * MINSPERHOUR) {
1746 strcat(result, "/");
1747 if (stringoffset(end(result), tod) != 0)
1748 return -1;
1750 return 0;
1753 static void
1754 stringzone(char *result, const struct zone * const zpfirst,
1755 const int zonecount)
1757 const struct zone * zp;
1758 struct rule * rp;
1759 struct rule * stdrp;
1760 struct rule * dstrp;
1761 int i;
1762 const char * abbrvar;
1764 result[0] = '\0';
1765 zp = zpfirst + zonecount - 1;
1766 stdrp = dstrp = NULL;
1767 for (i = 0; i < zp->z_nrules; ++i) {
1768 rp = &zp->z_rules[i];
1769 if (rp->r_hiwasnum || rp->r_hiyear != INT_MAX)
1770 continue;
1771 if (rp->r_yrtype != NULL)
1772 continue;
1773 if (rp->r_stdoff == 0) {
1774 if (stdrp == NULL)
1775 stdrp = rp;
1776 else return;
1777 } else {
1778 if (dstrp == NULL)
1779 dstrp = rp;
1780 else return;
1783 if (stdrp == NULL && dstrp == NULL) {
1785 ** There are no rules running through "max".
1786 ** Let's find the latest rule.
1788 for (i = 0; i < zp->z_nrules; ++i) {
1789 rp = &zp->z_rules[i];
1790 if (stdrp == NULL || rp->r_hiyear > stdrp->r_hiyear ||
1791 (rp->r_hiyear == stdrp->r_hiyear &&
1792 rp->r_month > stdrp->r_month))
1793 stdrp = rp;
1795 if (stdrp != NULL && stdrp->r_stdoff != 0)
1796 return; /* We end up in DST (a POSIX no-no). */
1798 ** Horrid special case: if year is 2037,
1799 ** presume this is a zone handled on a year-by-year basis;
1800 ** do not try to apply a rule to the zone.
1802 if (stdrp != NULL && stdrp->r_hiyear == 2037)
1803 return;
1805 if (stdrp == NULL && (zp->z_nrules != 0 || zp->z_stdoff != 0))
1806 return;
1807 abbrvar = (stdrp == NULL) ? "" : stdrp->r_abbrvar;
1808 doabbr(result, zp->z_format, abbrvar, FALSE, TRUE);
1809 if (stringoffset(end(result), -zp->z_gmtoff) != 0) {
1810 result[0] = '\0';
1811 return;
1813 if (dstrp == NULL)
1814 return;
1815 doabbr(end(result), zp->z_format, dstrp->r_abbrvar, TRUE, TRUE);
1816 if (dstrp->r_stdoff != SECSPERMIN * MINSPERHOUR)
1817 if (stringoffset(end(result),
1818 -(zp->z_gmtoff + dstrp->r_stdoff)) != 0) {
1819 result[0] = '\0';
1820 return;
1822 strcat(result, ",");
1823 if (stringrule(result, dstrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) {
1824 result[0] = '\0';
1825 return;
1827 strcat(result, ",");
1828 if (stringrule(result, stdrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) {
1829 result[0] = '\0';
1830 return;
1834 static void
1835 outzone(const struct zone * const zpfirst, const int zonecount)
1837 const struct zone *zp;
1838 struct rule *rp;
1839 int i, j;
1840 int usestart, useuntil;
1841 zic_t starttime, untiltime;
1842 long gmtoff;
1843 long stdoff;
1844 int year;
1845 long startoff;
1846 int startttisstd;
1847 int startttisgmt;
1848 int type;
1849 char *startbuf;
1850 char *ab;
1851 char *envvar;
1852 int max_abbr_len;
1853 int max_envvar_len;
1855 max_abbr_len = 2 + max_format_len + max_abbrvar_len;
1856 max_envvar_len = 2 * max_abbr_len + 5 * 9;
1857 startbuf = emalloc(max_abbr_len + 1);
1858 ab = emalloc(max_abbr_len + 1);
1859 envvar = emalloc(max_envvar_len + 1);
1860 INITIALIZE(untiltime);
1861 INITIALIZE(starttime);
1863 ** Now. . .finally. . .generate some useful data!
1865 timecnt = 0;
1866 typecnt = 0;
1867 charcnt = 0;
1869 ** Thanks to Earl Chew
1870 ** for noting the need to unconditionally initialize startttisstd.
1872 startttisstd = FALSE;
1873 startttisgmt = FALSE;
1874 min_year = max_year = EPOCH_YEAR;
1875 if (leapseen) {
1876 updateminmax(leapminyear);
1877 updateminmax(leapmaxyear + (leapmaxyear < INT_MAX));
1879 for (i = 0; i < zonecount; ++i) {
1880 zp = &zpfirst[i];
1881 if (i < zonecount - 1)
1882 updateminmax(zp->z_untilrule.r_loyear);
1883 for (j = 0; j < zp->z_nrules; ++j) {
1884 rp = &zp->z_rules[j];
1885 if (rp->r_lowasnum)
1886 updateminmax(rp->r_loyear);
1887 if (rp->r_hiwasnum)
1888 updateminmax(rp->r_hiyear);
1892 ** Generate lots of data if a rule can't cover all future times.
1894 stringzone(envvar, zpfirst, zonecount);
1895 if (noise && envvar[0] == '\0') {
1896 char * wp;
1898 wp = ecpyalloc(_("no POSIX environment variable for zone"));
1899 wp = ecatalloc(wp, " ");
1900 wp = ecatalloc(wp, zpfirst->z_name);
1901 warning(wp);
1902 ifree(wp);
1904 if (envvar[0] == '\0') {
1905 if (min_year >= INT_MIN + YEARSPERREPEAT)
1906 min_year -= YEARSPERREPEAT;
1907 else min_year = INT_MIN;
1908 if (max_year <= INT_MAX - YEARSPERREPEAT)
1909 max_year += YEARSPERREPEAT;
1910 else max_year = INT_MAX;
1913 ** For the benefit of older systems,
1914 ** generate data from 1900 through 2037.
1916 if (min_year > 1900)
1917 min_year = 1900;
1918 if (max_year < 2037)
1919 max_year = 2037;
1920 for (i = 0; i < zonecount; ++i) {
1922 ** A guess that may well be corrected later.
1924 stdoff = 0;
1925 zp = &zpfirst[i];
1926 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
1927 useuntil = i < (zonecount - 1);
1928 if (useuntil && zp->z_untiltime <= min_time)
1929 continue;
1930 gmtoff = zp->z_gmtoff;
1931 eat(zp->z_filename, zp->z_linenum);
1932 *startbuf = '\0';
1933 startoff = zp->z_gmtoff;
1934 if (zp->z_nrules == 0) {
1935 stdoff = zp->z_stdoff;
1936 doabbr(startbuf, zp->z_format,
1937 NULL, stdoff != 0, FALSE);
1938 type = addtype(oadd(zp->z_gmtoff, stdoff),
1939 startbuf, stdoff != 0, startttisstd,
1940 startttisgmt);
1941 if (usestart) {
1942 addtt(starttime, type);
1943 usestart = FALSE;
1944 } else if (stdoff != 0)
1945 addtt(min_time, type);
1946 } else for (year = min_year; year <= max_year; ++year) {
1947 if (useuntil && year > zp->z_untilrule.r_hiyear)
1948 break;
1950 ** Mark which rules to do in the current year.
1951 ** For those to do, calculate rpytime(rp, year);
1953 for (j = 0; j < zp->z_nrules; ++j) {
1954 rp = &zp->z_rules[j];
1955 eats(zp->z_filename, zp->z_linenum,
1956 rp->r_filename, rp->r_linenum);
1957 rp->r_todo = year >= rp->r_loyear &&
1958 year <= rp->r_hiyear &&
1959 yearistype(year, rp->r_yrtype);
1960 if (rp->r_todo)
1961 rp->r_temp = rpytime(rp, year);
1963 for ( ; ; ) {
1964 int k;
1965 zic_t jtime, ktime;
1966 long offset;
1968 INITIALIZE(ktime);
1969 if (useuntil) {
1971 ** Turn untiltime into UTC
1972 ** assuming the current gmtoff and
1973 ** stdoff values.
1975 untiltime = zp->z_untiltime;
1976 if (!zp->z_untilrule.r_todisgmt)
1977 untiltime = tadd(untiltime,
1978 -gmtoff);
1979 if (!zp->z_untilrule.r_todisstd)
1980 untiltime = tadd(untiltime,
1981 -stdoff);
1984 ** Find the rule (of those to do, if any)
1985 ** that takes effect earliest in the year.
1987 k = -1;
1988 for (j = 0; j < zp->z_nrules; ++j) {
1989 rp = &zp->z_rules[j];
1990 if (!rp->r_todo)
1991 continue;
1992 eats(zp->z_filename, zp->z_linenum,
1993 rp->r_filename, rp->r_linenum);
1994 offset = rp->r_todisgmt ? 0 : gmtoff;
1995 if (!rp->r_todisstd)
1996 offset = oadd(offset, stdoff);
1997 jtime = rp->r_temp;
1998 if (jtime == min_time ||
1999 jtime == max_time)
2000 continue;
2001 jtime = tadd(jtime, -offset);
2002 if (k < 0 || jtime < ktime) {
2003 k = j;
2004 ktime = jtime;
2007 if (k < 0)
2008 break; /* go on to next year */
2009 rp = &zp->z_rules[k];
2010 rp->r_todo = FALSE;
2011 if (useuntil && ktime >= untiltime)
2012 break;
2013 stdoff = rp->r_stdoff;
2014 if (usestart && ktime == starttime)
2015 usestart = FALSE;
2016 if (usestart) {
2017 if (ktime < starttime) {
2018 startoff = oadd(zp->z_gmtoff,
2019 stdoff);
2020 doabbr(startbuf, zp->z_format,
2021 rp->r_abbrvar,
2022 rp->r_stdoff != 0,
2023 FALSE);
2024 continue;
2026 if (*startbuf == '\0' &&
2027 startoff == oadd(zp->z_gmtoff,
2028 stdoff)) {
2029 doabbr(startbuf,
2030 zp->z_format,
2031 rp->r_abbrvar,
2032 rp->r_stdoff !=
2034 FALSE);
2037 eats(zp->z_filename, zp->z_linenum,
2038 rp->r_filename, rp->r_linenum);
2039 doabbr(ab, zp->z_format, rp->r_abbrvar,
2040 rp->r_stdoff != 0, FALSE);
2041 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
2042 type = addtype(offset, ab, rp->r_stdoff != 0,
2043 rp->r_todisstd, rp->r_todisgmt);
2044 addtt(ktime, type);
2047 if (usestart) {
2048 if (*startbuf == '\0' &&
2049 zp->z_format != NULL &&
2050 strchr(zp->z_format, '%') == NULL &&
2051 strchr(zp->z_format, '/') == NULL)
2052 strcpy(startbuf, zp->z_format);
2053 eat(zp->z_filename, zp->z_linenum);
2054 if (*startbuf == '\0')
2055 error(_("can't determine time zone abbreviation to use just after until time"));
2056 else addtt(starttime,
2057 addtype(startoff, startbuf,
2058 startoff != zp->z_gmtoff,
2059 startttisstd,
2060 startttisgmt));
2063 ** Now we may get to set starttime for the next zone line.
2065 if (useuntil) {
2066 startttisstd = zp->z_untilrule.r_todisstd;
2067 startttisgmt = zp->z_untilrule.r_todisgmt;
2068 starttime = zp->z_untiltime;
2069 if (!startttisstd)
2070 starttime = tadd(starttime, -stdoff);
2071 if (!startttisgmt)
2072 starttime = tadd(starttime, -gmtoff);
2075 writezone(zpfirst->z_name, envvar);
2076 ifree(startbuf);
2077 ifree(ab);
2078 ifree(envvar);
2081 static void
2082 addtt(const zic_t starttime, int type)
2084 if (starttime <= min_time ||
2085 (timecnt == 1 && attypes[0].at < min_time)) {
2086 gmtoffs[0] = gmtoffs[type];
2087 isdsts[0] = isdsts[type];
2088 ttisstds[0] = ttisstds[type];
2089 ttisgmts[0] = ttisgmts[type];
2090 if (abbrinds[type] != 0)
2091 strcpy(chars, &chars[abbrinds[type]]);
2092 abbrinds[0] = 0;
2093 charcnt = strlen(chars) + 1;
2094 typecnt = 1;
2095 timecnt = 0;
2096 type = 0;
2098 if (timecnt >= TZ_MAX_TIMES) {
2099 error(_("too many transitions?!"));
2100 exit(EXIT_FAILURE);
2102 attypes[timecnt].at = starttime;
2103 attypes[timecnt].type = type;
2104 ++timecnt;
2107 static int
2108 addtype(const long gmtoff, const char * const abbr, const int isdst,
2109 const int ttisstd, const int ttisgmt)
2111 int i, j;
2113 if (isdst != TRUE && isdst != FALSE) {
2114 error(_("internal error - addtype called with bad isdst"));
2115 exit(EXIT_FAILURE);
2117 if (ttisstd != TRUE && ttisstd != FALSE) {
2118 error(_("internal error - addtype called with bad ttisstd"));
2119 exit(EXIT_FAILURE);
2121 if (ttisgmt != TRUE && ttisgmt != FALSE) {
2122 error(_("internal error - addtype called with bad ttisgmt"));
2123 exit(EXIT_FAILURE);
2126 ** See if there's already an entry for this zone type.
2127 ** If so, just return its index.
2129 for (i = 0; i < typecnt; ++i) {
2130 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
2131 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
2132 ttisstd == ttisstds[i] &&
2133 ttisgmt == ttisgmts[i])
2134 return i;
2137 ** There isn't one; add a new one, unless there are already too
2138 ** many.
2140 if (typecnt >= TZ_MAX_TYPES) {
2141 error(_("too many local time types"));
2142 exit(EXIT_FAILURE);
2144 if (! (-1L - 2147483647L <= gmtoff && gmtoff <= 2147483647L)) {
2145 error(_("UTC offset out of range"));
2146 exit(EXIT_FAILURE);
2148 gmtoffs[i] = gmtoff;
2149 isdsts[i] = isdst;
2150 ttisstds[i] = ttisstd;
2151 ttisgmts[i] = ttisgmt;
2153 for (j = 0; j < charcnt; ++j)
2154 if (strcmp(&chars[j], abbr) == 0)
2155 break;
2156 if (j == charcnt)
2157 newabbr(abbr);
2158 abbrinds[i] = j;
2159 ++typecnt;
2160 return i;
2163 static void
2164 leapadd(const zic_t t, const int positive, const int rolling, int count)
2166 int i, j;
2168 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
2169 error(_("too many leap seconds"));
2170 exit(EXIT_FAILURE);
2172 for (i = 0; i < leapcnt; ++i)
2173 if (t <= trans[i]) {
2174 if (t == trans[i]) {
2175 error(_("repeated leap second moment"));
2176 exit(EXIT_FAILURE);
2178 break;
2180 do {
2181 for (j = leapcnt; j > i; --j) {
2182 trans[j] = trans[j - 1];
2183 corr[j] = corr[j - 1];
2184 roll[j] = roll[j - 1];
2186 trans[i] = t;
2187 corr[i] = positive ? 1L : eitol(-count);
2188 roll[i] = rolling;
2189 ++leapcnt;
2190 } while (positive && --count != 0);
2193 static void
2194 adjleap(void)
2196 int i;
2197 long last = 0;
2200 ** propagate leap seconds forward
2202 for (i = 0; i < leapcnt; ++i) {
2203 trans[i] = tadd(trans[i], last);
2204 last = corr[i] += last;
2208 static int
2209 yearistype(const int year, const char * const type)
2211 static char * buf;
2212 int result;
2214 if (type == NULL || *type == '\0')
2215 return TRUE;
2216 buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
2217 sprintf(buf, "%s %d %s", yitcommand, year, type);
2218 result = system(buf);
2219 if (WIFEXITED(result)) switch (WEXITSTATUS(result)) {
2220 case 0:
2221 return TRUE;
2222 case 1:
2223 return FALSE;
2225 error(_("Wild result from command execution"));
2226 warnx(_("command was '%s', result was %d"), buf, result);
2227 for ( ; ; )
2228 exit(EXIT_FAILURE);
2231 static int
2232 lowerit(int a)
2234 a = (unsigned char) a;
2235 return (isascii(a) && isupper(a)) ? tolower(a) : a;
2238 static int
2239 ciequal(const char *ap, const char *bp) /* case-insensitive equality */
2241 while (lowerit(*ap) == lowerit(*bp++))
2242 if (*ap++ == '\0')
2243 return TRUE;
2244 return FALSE;
2247 static int
2248 itsabbr(const char *abbr, const char *word)
2250 if (lowerit(*abbr) != lowerit(*word))
2251 return FALSE;
2252 ++word;
2253 while (*++abbr != '\0')
2254 do {
2255 if (*word == '\0')
2256 return FALSE;
2257 } while (lowerit(*word++) != lowerit(*abbr));
2258 return TRUE;
2261 static const struct lookup *
2262 byword(const char * const word, const struct lookup * const table)
2264 const struct lookup *foundlp;
2265 const struct lookup *lp;
2267 if (word == NULL || table == NULL)
2268 return NULL;
2270 ** Look for exact match.
2272 for (lp = table; lp->l_word != NULL; ++lp)
2273 if (ciequal(word, lp->l_word))
2274 return lp;
2276 ** Look for inexact match.
2278 foundlp = NULL;
2279 for (lp = table; lp->l_word != NULL; ++lp)
2280 if (itsabbr(word, lp->l_word)) {
2281 if (foundlp == NULL)
2282 foundlp = lp;
2283 else return NULL; /* multiple inexact matches */
2285 return foundlp;
2288 static char **
2289 getfields(char *cp)
2291 char *dp;
2292 char **array;
2293 int nsubs;
2295 if (cp == NULL)
2296 return NULL;
2297 array = (char **) (void *)
2298 emalloc((int) ((strlen(cp) + 1) * sizeof *array));
2299 nsubs = 0;
2300 for ( ; ; ) {
2301 while (isascii((unsigned char) *cp) &&
2302 isspace((unsigned char) *cp))
2303 ++cp;
2304 if (*cp == '\0' || *cp == '#')
2305 break;
2306 array[nsubs++] = dp = cp;
2307 do {
2308 if ((*dp = *cp++) != '"')
2309 ++dp;
2310 else while ((*dp = *cp++) != '"')
2311 if (*dp != '\0')
2312 ++dp;
2313 else {
2314 error(_(
2315 "Odd number of quotation marks"
2317 exit(1);
2319 } while (*cp != '\0' && *cp != '#' &&
2320 (!isascii(*cp) || !isspace((unsigned char) *cp)));
2321 if (isascii(*cp) && isspace((unsigned char) *cp))
2322 ++cp;
2323 *dp = '\0';
2325 array[nsubs] = NULL;
2326 return array;
2329 static long
2330 oadd(const long t1, const long t2)
2332 long t;
2334 t = t1 + t2;
2335 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2336 error(_("time overflow"));
2337 exit(EXIT_FAILURE);
2339 return t;
2342 static zic_t
2343 tadd(const zic_t t1, const long t2)
2345 zic_t t;
2347 if (t1 == max_time && t2 > 0)
2348 return max_time;
2349 if (t1 == min_time && t2 < 0)
2350 return min_time;
2351 t = t1 + t2;
2352 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2353 error(_("time overflow"));
2354 exit(EXIT_FAILURE);
2356 return t;
2360 ** Given a rule, and a year, compute the date - in seconds since January 1,
2361 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2364 static zic_t
2365 rpytime(const struct rule * const rp, const int wantedy)
2367 int y, m, i;
2368 long dayoff; /* with a nod to Margaret O. */
2369 zic_t t;
2371 if (wantedy == INT_MIN)
2372 return min_time;
2373 if (wantedy == INT_MAX)
2374 return max_time;
2375 dayoff = 0;
2376 m = TM_JANUARY;
2377 y = EPOCH_YEAR;
2378 while (wantedy != y) {
2379 if (wantedy > y) {
2380 i = len_years[isleap(y)];
2381 ++y;
2382 } else {
2383 --y;
2384 i = -len_years[isleap(y)];
2386 dayoff = oadd(dayoff, eitol(i));
2388 while (m != rp->r_month) {
2389 i = len_months[isleap(y)][m];
2390 dayoff = oadd(dayoff, eitol(i));
2391 ++m;
2393 i = rp->r_dayofmonth;
2394 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
2395 if (rp->r_dycode == DC_DOWLEQ)
2396 --i;
2397 else {
2398 error(_("use of 2/29 in non leap-year"));
2399 exit(EXIT_FAILURE);
2402 --i;
2403 dayoff = oadd(dayoff, eitol(i));
2404 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
2405 long wday;
2407 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2408 wday = eitol(EPOCH_WDAY);
2410 ** Don't trust mod of negative numbers.
2412 if (dayoff >= 0)
2413 wday = (wday + dayoff) % LDAYSPERWEEK;
2414 else {
2415 wday -= ((-dayoff) % LDAYSPERWEEK);
2416 if (wday < 0)
2417 wday += LDAYSPERWEEK;
2419 while (wday != eitol(rp->r_wday))
2420 if (rp->r_dycode == DC_DOWGEQ) {
2421 dayoff = oadd(dayoff, (long) 1);
2422 if (++wday >= LDAYSPERWEEK)
2423 wday = 0;
2424 ++i;
2425 } else {
2426 dayoff = oadd(dayoff, (long) -1);
2427 if (--wday < 0)
2428 wday = LDAYSPERWEEK - 1;
2429 --i;
2431 if (i < 0 || i >= len_months[isleap(y)][m]) {
2432 if (noise)
2433 warning(_("rule goes past start/end of month--\
2434 will not work with pre-2004 versions of zic"));
2437 if (dayoff < min_time / SECSPERDAY)
2438 return min_time;
2439 if (dayoff > max_time / SECSPERDAY)
2440 return max_time;
2441 t = (zic_t) dayoff * SECSPERDAY;
2442 return tadd(t, rp->r_tod);
2445 static void
2446 newabbr(const char * const string)
2448 int i;
2450 if (strcmp(string, GRANDPARENTED) != 0) {
2451 const char * cp;
2452 char * wp;
2455 ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2456 ** optionally followed by a + or - and a number from 1 to 14.
2458 cp = string;
2459 wp = NULL;
2460 while (isascii((unsigned char) *cp) &&
2461 isalpha((unsigned char) *cp))
2462 ++cp;
2463 if (cp - string == 0)
2464 wp = _("time zone abbreviation lacks alphabetic at start");
2465 if (noise && cp - string > 3)
2466 wp = _("time zone abbreviation has more than 3 alphabetics");
2467 if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
2468 wp = _("time zone abbreviation has too many alphabetics");
2469 if (wp == NULL && (*cp == '+' || *cp == '-')) {
2470 ++cp;
2471 if (isascii((unsigned char) *cp) &&
2472 isdigit((unsigned char) *cp))
2473 if (*cp++ == '1' &&
2474 *cp >= '0' && *cp <= '4')
2475 ++cp;
2477 if (*cp != '\0')
2478 wp = _("time zone abbreviation differs from POSIX standard");
2479 if (wp != NULL) {
2480 wp = ecpyalloc(wp);
2481 wp = ecatalloc(wp, " (");
2482 wp = ecatalloc(wp, string);
2483 wp = ecatalloc(wp, ")");
2484 warning(wp);
2485 ifree(wp);
2488 i = strlen(string) + 1;
2489 if (charcnt + i > TZ_MAX_CHARS) {
2490 error(_("too many, or too long, time zone abbreviations"));
2491 exit(EXIT_FAILURE);
2493 strcpy(&chars[charcnt], string);
2494 charcnt += eitol(i);
2497 static int
2498 mkdirs(char *argname)
2500 char *name;
2501 char *cp;
2503 if (argname == NULL || *argname == '\0' || Dflag)
2504 return 0;
2505 cp = name = ecpyalloc(argname);
2506 while ((cp = strchr(cp + 1, '/')) != 0) {
2507 *cp = '\0';
2508 #ifndef unix
2510 ** DOS drive specifier?
2512 if (isalpha((unsigned char) name[0]) &&
2513 name[1] == ':' && name[2] == '\0') {
2514 *cp = '/';
2515 continue;
2517 #endif /* !defined unix */
2518 if (!itsdir(name)) {
2520 ** It doesn't seem to exist, so we try to create it.
2521 ** Creation may fail because of the directory being
2522 ** created by some other multiprocessor, so we get
2523 ** to do extra checking.
2525 if ((mkdir(name, MKDIR_UMASK) != 0) &&
2526 (errno != EEXIST || !itsdir(name))) {
2527 warn(_("can't create directory %s"), name);
2528 ifree(name);
2529 return -1;
2532 *cp = '/';
2534 ifree(name);
2535 return 0;
2538 static long
2539 eitol(const int i)
2541 long l;
2543 l = i;
2544 if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0))
2545 errx(EXIT_FAILURE, _("%d did not sign extend correctly"), i);
2546 return l;
2549 #include <grp.h>
2550 #include <pwd.h>
2552 static void
2553 setgroup(gid_t *flag, const char *name)
2555 struct group *gr;
2557 if (*flag != (gid_t)-1)
2558 errx(EXIT_FAILURE, _("multiple -g flags specified"));
2560 gr = getgrnam(name);
2561 if (gr == 0) {
2562 char *ep;
2563 unsigned long ul;
2565 ul = strtoul(name, &ep, 10);
2566 if (ul == (unsigned long)(gid_t)ul && *ep == '\0') {
2567 *flag = ul;
2568 return;
2570 errx(EXIT_FAILURE, _("group `%s' not found"), name);
2572 *flag = gr->gr_gid;
2575 static void
2576 setuser(uid_t *flag, const char *name)
2578 struct passwd *pw;
2580 if (*flag != (gid_t)-1)
2581 errx(EXIT_FAILURE, _("multiple -u flags specified"));
2583 pw = getpwnam(name);
2584 if (pw == 0) {
2585 char *ep;
2586 unsigned long ul;
2588 ul = strtoul(name, &ep, 10);
2589 if (ul == (unsigned long)(gid_t)ul && *ep == '\0') {
2590 *flag = ul;
2591 return;
2593 errx(EXIT_FAILURE, _("user `%s' not found"), name);
2595 *flag = pw->pw_uid;
2599 ** UNIX was a registered trademark of The Open Group in 2003.