2 ** This file is in the public domain, so clarified as of
3 ** 2006-07-17 by Arthur David Olson.
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 $
12 #include <sys/stat.h> /* for umask manifest constants */
13 #include <sys/types.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.
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'))
47 const char * r_filename;
51 int r_loyear; /* for example, 1986 */
52 int r_hiyear; /* for example, 1986 */
53 const char * r_yrtype;
57 int r_month; /* 0..11 */
59 int r_dycode; /* see below */
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) */
84 const char * z_filename;
90 const char * z_format;
94 struct rule * z_rules;
97 struct rule z_untilrule;
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,
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);
160 static const char * filename;
163 static int leapminyear;
164 static int leapmaxyear;
166 static int max_abbrvar_len;
167 static int max_format_len;
168 static zic_t max_time;
170 static zic_t min_time;
173 static const char * rfilename;
188 ** Which fields are which on a Zone line.
196 #define ZF_TILMONTH 6
199 #define ZONE_MINFIELDS 5
200 #define ZONE_MAXFIELDS 9
203 ** Which fields are which on a Zone continuation line.
209 #define ZFC_TILYEAR 3
210 #define ZFC_TILMONTH 4
212 #define ZFC_TILTIME 6
213 #define ZONEC_MINFIELDS 3
214 #define ZONEC_MAXFIELDS 7
217 ** Which files are which on a Rule line.
229 #define RULE_FIELDS 10
232 ** Which fields are which on a Link line.
237 #define LINK_FIELDS 3
240 ** Which fields are which on a Leap line.
249 #define LEAP_FIELDS 7
259 static struct rule * rules;
260 static int nrules; /* number of rules */
262 static struct zone * zones;
263 static int nzones; /* number of zones */
266 const char * l_filename;
272 static struct link * links;
280 static struct lookup const *byword(const char *string,
281 const struct lookup *lp);
283 static struct lookup const line_codes[] = {
291 static struct lookup const mon_names[] = {
292 { "January", TM_JANUARY },
293 { "February", TM_FEBRUARY },
294 { "March", TM_MARCH },
295 { "April", TM_APRIL },
299 { "August", TM_AUGUST },
300 { "September", TM_SEPTEMBER },
301 { "October", TM_OCTOBER },
302 { "November", TM_NOVEMBER },
303 { "December", TM_DECEMBER },
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 },
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 },
329 static struct lookup const begin_years[] = {
330 { "minimum", YR_MINIMUM },
331 { "maximum", YR_MAXIMUM },
335 static struct lookup const end_years[] = {
336 { "minimum", YR_MINIMUM },
337 { "maximum", YR_MAXIMUM },
342 static struct lookup const leap_types[] = {
344 { "Stationary", FALSE },
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 {
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.
376 memcheck(char * const ptr)
379 errx(EXIT_FAILURE, _("memory exhausted"));
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)))
393 eats(const char * const name, const int num,
394 const char * const rname, const int rnum)
403 eat(const char * const name, const int num)
405 eats(name, num, NULL, -1);
409 error(const char * const string)
412 ** Match the format of "cc" to allow sh users to
413 ** zic ... 2>&1 | error -t "*" -v
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");
426 warning(const char * const string)
430 cp = ecpyalloc(_("warning: "));
431 cp = ecatalloc(cp, string);
440 fprintf(stderr, "%s\n%s\n",
441 _("usage: zic [-v] [-l localtime] [-p posixrules] [-d directory]"),
442 _(" [-L leapseconds] [-y yearistype] [filename ...]"));
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;
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
458 main(int argc, char *argv[])
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)
475 if (directory == NULL)
479 _("more than one -d option specified"));
482 setgroup(&gflag, optarg);
489 _("more than one -l option specified"));
493 void *set = setmode(optarg);
498 if (psxrules == NULL)
502 _("more than one -p option specified"));
505 setuser(&uflag, optarg);
508 if (yitcommand == NULL)
512 _("more than one -y option specified"));
519 _("more than one -L option specified"));
525 warnx(_("-s ignored\n"));
528 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
529 usage(); /* usage message by request */
530 if (directory == NULL)
532 if (yitcommand == NULL)
533 yitcommand = "yearistype";
537 if (optind < argc && leapsec != NULL) {
542 for (i = optind; i < argc; ++i)
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)
553 outzone(&zones[i], j - i);
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);
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;
579 dolink(const char * const fromfield, const char * const tofield)
584 if (fromfield[0] == '/')
585 fromname = ecpyalloc(fromfield);
587 fromname = ecpyalloc(directory);
588 fromname = ecatalloc(fromname, "/");
589 fromname = ecatalloc(fromname, fromfield);
591 if (tofield[0] == '/')
592 toname = ecpyalloc(tofield);
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.
604 if (link(fromname, toname) != 0) {
607 if (mkdirs(toname) != 0)
610 result = link(fromname, toname);
613 access(fromname, F_OK) == 0 &&
615 const char *s = tofield;
616 char * symlinkcontents = NULL;
618 while ((s = strchr(s+1, '/')) != NULL)
620 ecatalloc(symlinkcontents,
623 ecatalloc(symlinkcontents,
625 result = symlink(symlinkcontents,
628 warning(_("hard link failed, symbolic link used"));
629 ifree(symlinkcontents);
631 #endif /* HAVE_SYMLINK */
633 err(EXIT_FAILURE, _("can't link from %s to %s"),
641 #define TIME_T_BITS_IN_FILE 64
649 for (i = 0; i < TIME_T_BITS_IN_FILE - 1; ++i)
651 max_time = -(min_time + 1);
655 itsdir(const char * const name)
660 myname = ecpyalloc(name);
661 myname = ecatalloc(myname, "/.");
662 accres = access(myname, F_OK);
668 ** Associate sets of rules with zones.
672 ** Sort by rule name.
676 rcomp(const void *cp1, const void *cp2)
678 return strcmp(((const struct rule *) cp1)->r_name,
679 ((const struct rule *) cp2)->r_name);
691 qsort((void *) rules, (size_t) nrules,
692 (size_t) sizeof *rules, rcomp);
693 for (i = 0; i < nrules - 1; ++i) {
694 if (strcmp(rules[i].r_name,
695 rules[i + 1].r_name) != 0)
697 if (strcmp(rules[i].r_filename,
698 rules[i + 1].r_filename) == 0)
700 eat(rules[i].r_filename, rules[i].r_linenum);
701 warning(_("same rule name in multiple files"));
702 eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
703 warning(_("same rule name in multiple files"));
704 for (j = i + 2; j < nrules; ++j) {
705 if (strcmp(rules[i].r_name,
706 rules[j].r_name) != 0)
708 if (strcmp(rules[i].r_filename,
709 rules[j].r_filename) == 0)
711 if (strcmp(rules[i + 1].r_filename,
712 rules[j].r_filename) == 0)
719 for (i = 0; i < nzones; ++i) {
724 for (base = 0; base < nrules; base = out) {
726 for (out = base + 1; out < nrules; ++out)
727 if (strcmp(rp->r_name, rules[out].r_name) != 0)
729 for (i = 0; i < nzones; ++i) {
731 if (strcmp(zp->z_rule, rp->r_name) != 0)
734 zp->z_nrules = out - base;
737 for (i = 0; i < nzones; ++i) {
739 if (zp->z_nrules == 0) {
741 ** Maybe we have a local standard time offset.
743 eat(zp->z_filename, zp->z_linenum);
744 zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
747 ** Note, though, that if there's no rule,
748 ** a '%s' in the format is a bad thing.
750 if (strchr(zp->z_format, '%') != 0)
751 error(_("%s in ruleless zone"));
759 infile(const char *name)
764 const struct lookup *lp;
770 if (strcmp(name, "-") == 0) {
771 name = _("standard input");
773 } else if ((fp = fopen(name, "r")) == NULL)
774 err(EXIT_FAILURE, _("can't open %s"), name);
776 for (num = 1; ; ++num) {
778 if (fgets(buf, (int) sizeof buf, fp) != buf)
780 cp = strchr(buf, '\n');
782 error(_("line too long"));
786 fields = getfields(buf);
788 while (fields[nfields] != NULL) {
791 if (strcmp(fields[nfields], "-") == 0)
792 fields[nfields] = &nada;
797 } else if (wantcont) {
798 wantcont = inzcont(fields, nfields);
800 lp = byword(fields[0], line_codes);
802 error(_("input line of unknown type"));
803 else switch ((int) (lp->l_value)) {
805 inrule(fields, nfields);
809 wantcont = inzone(fields, nfields);
812 inlink(fields, nfields);
818 _("leap line in non leap seconds file %s"), name);
819 else inleap(fields, nfields);
822 default: /* "cannot happen" */
824 _("panic: invalid l_value %d"), lp->l_value);
827 ifree((char *) fields);
830 errx(EXIT_FAILURE, _("error reading %s"), filename);
831 if (fp != stdin && fclose(fp))
832 err(EXIT_FAILURE, _("error closing %s"), filename);
834 error(_("expected continuation line not found"));
838 ** Convert a string of one of the forms
839 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
840 ** into a number of seconds.
841 ** A null string maps to zero.
842 ** Call error with errstring and return zero on errors.
846 gethms(const char *string, const char * const errstring, const int signable)
851 if (string == NULL || *string == '\0')
855 else if (*string == '-') {
859 if (sscanf(string, scheck(string, "%ld"), &hh) == 1)
861 else if (sscanf(string, scheck(string, "%ld:%d"), &hh, &mm) == 2)
863 else if (sscanf(string, scheck(string, "%ld:%d:%d"),
864 &hh, &mm, &ss) != 3) {
869 mm < 0 || mm >= MINSPERHOUR ||
870 ss < 0 || ss > SECSPERMIN) {
874 if (LONG_MAX / SECSPERHOUR < hh) {
875 error(_("time overflow"));
878 if (noise && hh == HOURSPERDAY && mm == 0 && ss == 0)
879 warning(_("24:00 not handled by pre-1998 versions of zic"));
880 if (noise && (hh > HOURSPERDAY ||
881 (hh == HOURSPERDAY && (mm != 0 || ss != 0))))
882 warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
883 return oadd(eitol(sign) * hh * eitol(SECSPERHOUR),
884 eitol(sign) * (eitol(mm) * eitol(SECSPERMIN) + eitol(ss)));
888 inrule(char ** const fields, const int nfields)
890 static struct rule r;
892 if (nfields != RULE_FIELDS) {
893 error(_("wrong number of fields on Rule line"));
896 if (*fields[RF_NAME] == '\0') {
897 error(_("nameless rule"));
900 r.r_filename = filename;
901 r.r_linenum = linenum;
902 r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
903 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
904 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
905 r.r_name = ecpyalloc(fields[RF_NAME]);
906 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
907 if (max_abbrvar_len < strlen(r.r_abbrvar))
908 max_abbrvar_len = strlen(r.r_abbrvar);
909 rules = (struct rule *) (void *) erealloc((char *) rules,
910 (int) ((nrules + 1) * sizeof *rules));
915 inzone(char ** const fields, const int nfields)
920 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
921 error(_("wrong number of fields on Zone line"));
924 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
925 buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
927 _("\"Zone %s\" line and -l option are mutually exclusive"),
932 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
933 buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
935 _("\"Zone %s\" line and -p option are mutually exclusive"),
940 for (i = 0; i < nzones; ++i)
941 if (zones[i].z_name != NULL &&
942 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
943 buf = erealloc(buf, (int) (132 +
944 strlen(fields[ZF_NAME]) +
945 strlen(zones[i].z_filename)));
947 _("duplicate zone name %s (file \"%s\", line %d)"),
954 return inzsub(fields, nfields, FALSE);
958 inzcont(char ** const fields, const int nfields)
960 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
961 error(_("wrong number of fields on Zone continuation line"));
964 return inzsub(fields, nfields, TRUE);
968 inzsub(char ** const fields, const int nfields, const int iscont)
971 static struct zone z;
972 int i_gmtoff, i_rule, i_format;
973 int i_untilyear, i_untilmonth;
974 int i_untilday, i_untiltime;
978 i_gmtoff = ZFC_GMTOFF;
980 i_format = ZFC_FORMAT;
981 i_untilyear = ZFC_TILYEAR;
982 i_untilmonth = ZFC_TILMONTH;
983 i_untilday = ZFC_TILDAY;
984 i_untiltime = ZFC_TILTIME;
987 i_gmtoff = ZF_GMTOFF;
989 i_format = ZF_FORMAT;
990 i_untilyear = ZF_TILYEAR;
991 i_untilmonth = ZF_TILMONTH;
992 i_untilday = ZF_TILDAY;
993 i_untiltime = ZF_TILTIME;
994 z.z_name = ecpyalloc(fields[ZF_NAME]);
996 z.z_filename = filename;
997 z.z_linenum = linenum;
998 z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UTC offset"), TRUE);
999 if ((cp = strchr(fields[i_format], '%')) != 0) {
1000 if (*++cp != 's' || strchr(cp, '%') != 0) {
1001 error(_("invalid abbreviation format"));
1005 z.z_rule = ecpyalloc(fields[i_rule]);
1006 z.z_format = ecpyalloc(fields[i_format]);
1007 if (max_format_len < strlen(z.z_format))
1008 max_format_len = strlen(z.z_format);
1009 hasuntil = nfields > i_untilyear;
1011 z.z_untilrule.r_filename = filename;
1012 z.z_untilrule.r_linenum = linenum;
1013 rulesub(&z.z_untilrule,
1014 fields[i_untilyear],
1017 (nfields > i_untilmonth) ?
1018 fields[i_untilmonth] : "Jan",
1019 (nfields > i_untilday) ? fields[i_untilday] : "1",
1020 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1021 z.z_untiltime = rpytime(&z.z_untilrule,
1022 z.z_untilrule.r_loyear);
1023 if (iscont && nzones > 0 &&
1024 z.z_untiltime > min_time &&
1025 z.z_untiltime < max_time &&
1026 zones[nzones - 1].z_untiltime > min_time &&
1027 zones[nzones - 1].z_untiltime < max_time &&
1028 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1030 "Zone continuation line end time is not after end time of previous line"
1035 zones = (struct zone *) (void *) erealloc((char *) zones,
1036 (int) ((nzones + 1) * sizeof *zones));
1037 zones[nzones++] = z;
1039 ** If there was an UNTIL field on this line,
1040 ** there's more information about the zone on the next line.
1046 inleap(char ** const fields, const int nfields)
1049 const struct lookup *lp;
1051 int year, month, day;
1055 if (nfields != LEAP_FIELDS) {
1056 error(_("wrong number of fields on Leap line"));
1060 cp = fields[LP_YEAR];
1061 if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
1065 error(_("invalid leaping year"));
1068 if (!leapseen || leapmaxyear < year)
1070 if (!leapseen || leapminyear > year)
1076 i = len_years[isleap(j)];
1080 i = -len_years[isleap(j)];
1082 dayoff = oadd(dayoff, eitol(i));
1084 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1085 error(_("invalid month name"));
1088 month = lp->l_value;
1090 while (j != month) {
1091 i = len_months[isleap(year)][j];
1092 dayoff = oadd(dayoff, eitol(i));
1095 cp = fields[LP_DAY];
1096 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1097 day <= 0 || day > len_months[isleap(year)][month]) {
1098 error(_("invalid day of month"));
1101 dayoff = oadd(dayoff, eitol(day - 1));
1102 if (dayoff < 0 && !TYPE_SIGNED(zic_t)) {
1103 error(_("time before zero"));
1106 if (dayoff < min_time / SECSPERDAY) {
1107 error(_("time too small"));
1110 if (dayoff > max_time / SECSPERDAY) {
1111 error(_("time too large"));
1114 t = (zic_t) dayoff * SECSPERDAY;
1115 tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1116 cp = fields[LP_CORR];
1121 if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1124 } else if (strcmp(cp, "--") == 0) {
1127 } else if (strcmp(cp, "+") == 0) {
1130 } else if (strcmp(cp, "++") == 0) {
1134 error(_("illegal CORRECTION field on Leap line"));
1137 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1139 "illegal Rolling/Stationary field on Leap line"
1143 leapadd(tadd(t, tod), positive, lp->l_value, count);
1148 inlink(char ** const fields, const int nfields)
1152 if (nfields != LINK_FIELDS) {
1153 error(_("wrong number of fields on Link line"));
1156 if (*fields[LF_FROM] == '\0') {
1157 error(_("blank FROM field on Link line"));
1160 if (*fields[LF_TO] == '\0') {
1161 error(_("blank TO field on Link line"));
1164 l.l_filename = filename;
1165 l.l_linenum = linenum;
1166 l.l_from = ecpyalloc(fields[LF_FROM]);
1167 l.l_to = ecpyalloc(fields[LF_TO]);
1168 links = (struct link *) (void *) erealloc((char *) links,
1169 (int) ((nlinks + 1) * sizeof *links));
1170 links[nlinks++] = l;
1174 rulesub(struct rule * const rp,
1175 const char * const loyearp,
1176 const char * const hiyearp,
1177 const char * const typep,
1178 const char * const monthp,
1179 const char * const dayp,
1180 const char * const timep)
1182 const struct lookup *lp;
1187 if ((lp = byword(monthp, mon_names)) == NULL) {
1188 error(_("invalid month name"));
1191 rp->r_month = lp->l_value;
1192 rp->r_todisstd = FALSE;
1193 rp->r_todisgmt = FALSE;
1194 dp = ecpyalloc(timep);
1196 ep = dp + strlen(dp) - 1;
1197 switch (lowerit(*ep)) {
1198 case 's': /* Standard */
1199 rp->r_todisstd = TRUE;
1200 rp->r_todisgmt = FALSE;
1203 case 'w': /* Wall */
1204 rp->r_todisstd = FALSE;
1205 rp->r_todisgmt = FALSE;
1208 case 'g': /* Greenwich */
1209 case 'u': /* Universal */
1210 case 'z': /* Zulu */
1211 rp->r_todisstd = TRUE;
1212 rp->r_todisgmt = TRUE;
1217 rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1223 lp = byword(cp, begin_years);
1224 rp->r_lowasnum = lp == NULL;
1225 if (!rp->r_lowasnum) switch ((int) lp->l_value) {
1227 rp->r_loyear = INT_MIN;
1230 rp->r_loyear = INT_MAX;
1232 default: /* "cannot happen" */
1234 _("panic: invalid l_value %d"), lp->l_value);
1235 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
1236 error(_("invalid starting year"));
1240 lp = byword(cp, end_years);
1241 rp->r_hiwasnum = lp == NULL;
1242 if (!rp->r_hiwasnum) switch ((int) lp->l_value) {
1244 rp->r_hiyear = INT_MIN;
1247 rp->r_hiyear = INT_MAX;
1250 rp->r_hiyear = rp->r_loyear;
1252 default: /* "cannot happen" */
1254 _("panic: invalid l_value %d"), lp->l_value);
1255 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
1256 error(_("invalid ending year"));
1259 if (rp->r_loyear > rp->r_hiyear) {
1260 error(_("starting year greater than ending year"));
1264 rp->r_yrtype = NULL;
1266 if (rp->r_loyear == rp->r_hiyear) {
1267 error(_("typed single year"));
1270 rp->r_yrtype = ecpyalloc(typep);
1274 ** Accept things such as:
1280 dp = ecpyalloc(dayp);
1281 if ((lp = byword(dp, lasts)) != NULL) {
1282 rp->r_dycode = DC_DOWLEQ;
1283 rp->r_wday = lp->l_value;
1284 rp->r_dayofmonth = len_months[1][rp->r_month];
1286 if ((ep = strchr(dp, '<')) != 0)
1287 rp->r_dycode = DC_DOWLEQ;
1288 else if ((ep = strchr(dp, '>')) != 0)
1289 rp->r_dycode = DC_DOWGEQ;
1292 rp->r_dycode = DC_DOM;
1294 if (rp->r_dycode != DC_DOM) {
1297 error(_("invalid day of month"));
1301 if ((lp = byword(dp, wday_names)) == NULL) {
1302 error(_("invalid weekday name"));
1306 rp->r_wday = lp->l_value;
1308 if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1309 rp->r_dayofmonth <= 0 ||
1310 (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1311 error(_("invalid day of month"));
1320 convert(const long val, char * const buf)
1325 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1326 buf[i] = val >> shift;
1330 convert64(const zic_t val, char * const buf)
1335 for (i = 0, shift = 56; i < 8; ++i, shift -= 8)
1336 buf[i] = val >> shift;
1340 puttzcode(const long val, FILE * const fp)
1345 fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1349 puttzcode64(const zic_t val, FILE * const fp)
1353 convert64(val, buf);
1354 fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1358 atcomp(const void * avp, const void * bvp)
1360 const zic_t a = ((const struct attype *) avp)->at;
1361 const zic_t b = ((const struct attype *) bvp)->at;
1363 return (a < b) ? -1 : (a > b);
1369 return INT32_MIN <= x && x <= INT32_MAX;
1373 writezone(const char * const name, const char * const string)
1377 int leapcnt32, leapi32;
1378 int timecnt32, timei32;
1380 static char * fullname;
1381 static const struct tzhead tzh0;
1382 static struct tzhead tzh;
1383 zic_t ats[TZ_MAX_TIMES];
1384 unsigned char types[TZ_MAX_TIMES];
1390 qsort((void *) attypes, (size_t) timecnt,
1391 (size_t) sizeof *attypes, atcomp);
1401 while (fromi < timecnt && attypes[fromi].at < min_time)
1404 while (fromi < timecnt && attypes[fromi].type == 0)
1405 ++fromi; /* handled by default rule */
1406 for ( ; fromi < timecnt; ++fromi) {
1407 if (toi != 0 && ((attypes[fromi].at +
1408 gmtoffs[attypes[toi - 1].type]) <=
1409 (attypes[toi - 1].at + gmtoffs[toi == 1 ? 0
1410 : attypes[toi - 2].type]))) {
1411 attypes[toi - 1].type =
1412 attypes[fromi].type;
1416 attypes[toi - 1].type != attypes[fromi].type)
1417 attypes[toi++] = attypes[fromi];
1424 for (i = 0; i < timecnt; ++i) {
1425 ats[i] = attypes[i].at;
1426 types[i] = attypes[i].type;
1429 ** Correct for leap seconds.
1431 for (i = 0; i < timecnt; ++i) {
1434 if (ats[i] > trans[j] - corr[j]) {
1435 ats[i] = tadd(ats[i], corr[j]);
1440 ** Figure out 32-bit-limited starts and counts.
1442 timecnt32 = timecnt;
1444 leapcnt32 = leapcnt;
1446 while (timecnt32 > 0 && !is32(ats[timecnt32 - 1]))
1448 while (timecnt32 > 0 && !is32(ats[timei32])) {
1452 while (leapcnt32 > 0 && !is32(trans[leapcnt32 - 1]))
1454 while (leapcnt32 > 0 && !is32(trans[leapi32])) {
1458 fullname = erealloc(fullname,
1459 (int) (strlen(directory) + 1 + strlen(name) + 1));
1460 sprintf(fullname, "%s/%s", directory, name);
1462 ** Remove old file, if any, to snap links.
1464 if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT)
1465 err(EXIT_FAILURE, _("can't remove %s"), fullname);
1466 if ((fp = fopen(fullname, "wb")) == NULL) {
1467 if (mkdirs(fullname) != 0)
1469 if ((fp = fopen(fullname, "wb")) == NULL)
1470 err(EXIT_FAILURE, _("can't create %s"), fullname);
1472 for (pass = 1; pass <= 2; ++pass) {
1473 int thistimei, thistimecnt;
1474 int thisleapi, thisleapcnt;
1475 int thistimelim, thisleaplim;
1476 int writetype[TZ_MAX_TIMES];
1477 int typemap[TZ_MAX_TYPES];
1479 char thischars[TZ_MAX_CHARS];
1481 int indmap[TZ_MAX_CHARS];
1484 thistimei = timei32;
1485 thistimecnt = timecnt32;
1486 thisleapi = leapi32;
1487 thisleapcnt = leapcnt32;
1490 thistimecnt = timecnt;
1492 thisleapcnt = leapcnt;
1494 thistimelim = thistimei + thistimecnt;
1495 thisleaplim = thisleapi + thisleapcnt;
1496 for (i = 0; i < typecnt; ++i)
1497 writetype[i] = thistimecnt == timecnt;
1498 if (thistimecnt == 0) {
1500 ** No transition times fall in the current
1501 ** (32- or 64-bit) window.
1504 writetype[typecnt - 1] = TRUE;
1506 for (i = thistimei - 1; i < thistimelim; ++i)
1508 writetype[types[i]] = TRUE;
1510 ** For America/Godthab and Antarctica/Palmer
1513 writetype[0] = TRUE;
1516 for (i = 0; i < typecnt; ++i)
1517 typemap[i] = writetype[i] ? thistypecnt++ : -1;
1518 for (i = 0; i < sizeof indmap / sizeof indmap[0]; ++i)
1521 for (i = 0; i < typecnt; ++i) {
1526 if (indmap[abbrinds[i]] >= 0)
1528 thisabbr = &chars[abbrinds[i]];
1529 for (j = 0; j < thischarcnt; ++j)
1530 if (strcmp(&thischars[j], thisabbr) == 0)
1532 if (j == thischarcnt) {
1533 strcpy(&thischars[(int) thischarcnt],
1535 thischarcnt += strlen(thisabbr) + 1;
1537 indmap[abbrinds[i]] = j;
1539 #define DO(field) fwrite((void *) tzh.field, \
1540 (size_t) sizeof tzh.field, (size_t) 1, fp)
1542 strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1543 tzh.tzh_version[0] = ZIC_VERSION;
1544 convert(eitol(thistypecnt), tzh.tzh_ttisgmtcnt);
1545 convert(eitol(thistypecnt), tzh.tzh_ttisstdcnt);
1546 convert(eitol(thisleapcnt), tzh.tzh_leapcnt);
1547 convert(eitol(thistimecnt), tzh.tzh_timecnt);
1548 convert(eitol(thistypecnt), tzh.tzh_typecnt);
1549 convert(eitol(thischarcnt), tzh.tzh_charcnt);
1560 for (i = thistimei; i < thistimelim; ++i)
1562 puttzcode((long) ats[i], fp);
1563 else puttzcode64(ats[i], fp);
1564 for (i = thistimei; i < thistimelim; ++i) {
1567 uc = typemap[types[i]];
1568 fwrite((void *) &uc,
1573 for (i = 0; i < typecnt; ++i)
1575 puttzcode(gmtoffs[i], fp);
1576 putc(isdsts[i], fp);
1577 putc((unsigned char) indmap[abbrinds[i]], fp);
1579 if (thischarcnt != 0)
1580 fwrite((void *) thischars,
1581 (size_t) sizeof thischars[0],
1582 (size_t) thischarcnt, fp);
1583 for (i = thisleapi; i < thisleaplim; ++i) {
1584 register zic_t todo;
1587 if (timecnt == 0 || trans[i] < ats[0]) {
1590 if (++j >= typecnt) {
1596 while (j < timecnt &&
1601 todo = tadd(trans[i], -gmtoffs[j]);
1602 } else todo = trans[i];
1604 puttzcode((long) todo, fp);
1605 else puttzcode64(todo, fp);
1606 puttzcode(corr[i], fp);
1608 for (i = 0; i < typecnt; ++i)
1610 putc(ttisstds[i], fp);
1611 for (i = 0; i < typecnt; ++i)
1613 putc(ttisgmts[i], fp);
1615 fprintf(fp, "\n%s\n", string);
1616 if (ferror(fp) || fclose(fp))
1617 errx(EXIT_FAILURE, _("error writing %s"), fullname);
1618 if (chmod(fullname, mflag) < 0)
1619 err(EXIT_FAILURE, _("cannot change mode of %s to %03o"),
1620 fullname, (unsigned)mflag);
1621 if ((uflag != (uid_t)-1 || gflag != (gid_t)-1)
1622 && chown(fullname, uflag, gflag) < 0)
1623 err(EXIT_FAILURE, _("cannot change ownership of %s"),
1628 doabbr(char * const abbr, const char * const format,
1629 const char * const letters, const int isdst, const int doquotes)
1635 slashp = strchr(format, '/');
1636 if (slashp == NULL) {
1637 if (letters == NULL)
1638 strcpy(abbr, format);
1639 else sprintf(abbr, format, letters);
1641 strcpy(abbr, slashp + 1);
1643 if (slashp > format)
1644 strncpy(abbr, format,
1645 (unsigned) (slashp - format));
1646 abbr[slashp - format] = '\0';
1650 for (cp = abbr; *cp != '\0'; ++cp)
1651 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp) == NULL &&
1652 strchr("abcdefghijklmnopqrstuvwxyz", *cp) == NULL)
1655 if (len > 0 && *cp == '\0')
1657 abbr[len + 2] = '\0';
1658 abbr[len + 1] = '>';
1659 for ( ; len > 0; --len)
1660 abbr[len] = abbr[len - 1];
1665 updateminmax(const int x)
1674 stringoffset(char *result, long offset)
1682 strcpy(result, "-");
1685 seconds = offset % SECSPERMIN;
1686 offset /= SECSPERMIN;
1687 minutes = offset % MINSPERHOUR;
1688 offset /= MINSPERHOUR;
1690 if (hours >= HOURSPERDAY) {
1694 sprintf(end(result), "%d", hours);
1695 if (minutes != 0 || seconds != 0) {
1696 sprintf(end(result), ":%02d", minutes);
1698 sprintf(end(result), ":%02d", seconds);
1704 stringrule(char *result, const struct rule * const rp, const long dstoff,
1709 result = end(result);
1710 if (rp->r_dycode == DC_DOM) {
1713 if (rp->r_dayofmonth == 29 && rp->r_month == TM_FEBRUARY)
1716 for (month = 0; month < rp->r_month; ++month)
1717 total += len_months[0][month];
1718 sprintf(result, "J%d", total + rp->r_dayofmonth);
1722 if (rp->r_dycode == DC_DOWGEQ) {
1723 week = 1 + rp->r_dayofmonth / DAYSPERWEEK;
1724 if ((week - 1) * DAYSPERWEEK + 1 != rp->r_dayofmonth)
1726 } else if (rp->r_dycode == DC_DOWLEQ) {
1727 if (rp->r_dayofmonth == len_months[1][rp->r_month])
1730 week = 1 + rp->r_dayofmonth / DAYSPERWEEK;
1731 if (week * DAYSPERWEEK - 1 != rp->r_dayofmonth)
1734 } else return -1; /* "cannot happen" */
1735 sprintf(result, "M%d.%d.%d",
1736 rp->r_month + 1, week, rp->r_wday);
1741 if (rp->r_todisstd && rp->r_stdoff == 0)
1747 if (tod != 2 * SECSPERMIN * MINSPERHOUR) {
1748 strcat(result, "/");
1749 if (stringoffset(end(result), tod) != 0)
1756 stringzone(char *result, const struct zone * const zpfirst,
1757 const int zonecount)
1759 const struct zone * zp;
1761 struct rule * stdrp;
1762 struct rule * dstrp;
1764 const char * abbrvar;
1767 zp = zpfirst + zonecount - 1;
1768 stdrp = dstrp = NULL;
1769 for (i = 0; i < zp->z_nrules; ++i) {
1770 rp = &zp->z_rules[i];
1771 if (rp->r_hiwasnum || rp->r_hiyear != INT_MAX)
1773 if (rp->r_yrtype != NULL)
1775 if (rp->r_stdoff == 0) {
1785 if (stdrp == NULL && dstrp == NULL) {
1787 ** There are no rules running through "max".
1788 ** Let's find the latest rule.
1790 for (i = 0; i < zp->z_nrules; ++i) {
1791 rp = &zp->z_rules[i];
1792 if (stdrp == NULL || rp->r_hiyear > stdrp->r_hiyear ||
1793 (rp->r_hiyear == stdrp->r_hiyear &&
1794 rp->r_month > stdrp->r_month))
1797 if (stdrp != NULL && stdrp->r_stdoff != 0)
1798 return; /* We end up in DST (a POSIX no-no). */
1800 ** Horrid special case: if year is 2037,
1801 ** presume this is a zone handled on a year-by-year basis;
1802 ** do not try to apply a rule to the zone.
1804 if (stdrp != NULL && stdrp->r_hiyear == 2037)
1807 if (stdrp == NULL && zp->z_nrules != 0)
1809 abbrvar = (stdrp == NULL) ? "" : stdrp->r_abbrvar;
1810 doabbr(result, zp->z_format, abbrvar, FALSE, TRUE);
1811 if (stringoffset(end(result), -zp->z_gmtoff) != 0) {
1817 doabbr(end(result), zp->z_format, dstrp->r_abbrvar, TRUE, TRUE);
1818 if (dstrp->r_stdoff != SECSPERMIN * MINSPERHOUR)
1819 if (stringoffset(end(result),
1820 -(zp->z_gmtoff + dstrp->r_stdoff)) != 0) {
1824 strcat(result, ",");
1825 if (stringrule(result, dstrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) {
1829 strcat(result, ",");
1830 if (stringrule(result, stdrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) {
1837 outzone(const struct zone * const zpfirst, const int zonecount)
1839 const struct zone *zp;
1842 int usestart, useuntil;
1843 zic_t starttime, untiltime;
1857 max_abbr_len = 2 + max_format_len + max_abbrvar_len;
1858 max_envvar_len = 2 * max_abbr_len + 5 * 9;
1859 startbuf = emalloc(max_abbr_len + 1);
1860 ab = emalloc(max_abbr_len + 1);
1861 envvar = emalloc(max_envvar_len + 1);
1862 INITIALIZE(untiltime);
1863 INITIALIZE(starttime);
1865 ** Now. . .finally. . .generate some useful data!
1871 ** Thanks to Earl Chew
1872 ** for noting the need to unconditionally initialize startttisstd.
1874 startttisstd = FALSE;
1875 startttisgmt = FALSE;
1876 min_year = max_year = EPOCH_YEAR;
1878 updateminmax(leapminyear);
1879 updateminmax(leapmaxyear + (leapmaxyear < INT_MAX));
1881 for (i = 0; i < zonecount; ++i) {
1883 if (i < zonecount - 1)
1884 updateminmax(zp->z_untilrule.r_loyear);
1885 for (j = 0; j < zp->z_nrules; ++j) {
1886 rp = &zp->z_rules[j];
1888 updateminmax(rp->r_loyear);
1890 updateminmax(rp->r_hiyear);
1894 ** Generate lots of data if a rule can't cover all future times.
1896 stringzone(envvar, zpfirst, zonecount);
1897 if (noise && envvar[0] == '\0') {
1900 wp = ecpyalloc(_("no POSIX environment variable for zone"));
1901 wp = ecatalloc(wp, " ");
1902 wp = ecatalloc(wp, zpfirst->z_name);
1906 if (envvar[0] == '\0') {
1907 if (min_year >= INT_MIN + YEARSPERREPEAT)
1908 min_year -= YEARSPERREPEAT;
1909 else min_year = INT_MIN;
1910 if (max_year <= INT_MAX - YEARSPERREPEAT)
1911 max_year += YEARSPERREPEAT;
1912 else max_year = INT_MAX;
1915 ** For the benefit of older systems,
1916 ** generate data from 1900 through 2037.
1918 if (min_year > 1900)
1920 if (max_year < 2037)
1922 for (i = 0; i < zonecount; ++i) {
1924 ** A guess that may well be corrected later.
1928 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
1929 useuntil = i < (zonecount - 1);
1930 if (useuntil && zp->z_untiltime <= min_time)
1932 gmtoff = zp->z_gmtoff;
1933 eat(zp->z_filename, zp->z_linenum);
1935 startoff = zp->z_gmtoff;
1936 if (zp->z_nrules == 0) {
1937 stdoff = zp->z_stdoff;
1938 doabbr(startbuf, zp->z_format,
1939 NULL, stdoff != 0, FALSE);
1940 type = addtype(oadd(zp->z_gmtoff, stdoff),
1941 startbuf, stdoff != 0, startttisstd,
1944 addtt(starttime, type);
1946 } else if (stdoff != 0)
1947 addtt(min_time, type);
1948 } else for (year = min_year; year <= max_year; ++year) {
1949 if (useuntil && year > zp->z_untilrule.r_hiyear)
1952 ** Mark which rules to do in the current year.
1953 ** For those to do, calculate rpytime(rp, year);
1955 for (j = 0; j < zp->z_nrules; ++j) {
1956 rp = &zp->z_rules[j];
1957 eats(zp->z_filename, zp->z_linenum,
1958 rp->r_filename, rp->r_linenum);
1959 rp->r_todo = year >= rp->r_loyear &&
1960 year <= rp->r_hiyear &&
1961 yearistype(year, rp->r_yrtype);
1963 rp->r_temp = rpytime(rp, year);
1973 ** Turn untiltime into UTC
1974 ** assuming the current gmtoff and
1977 untiltime = zp->z_untiltime;
1978 if (!zp->z_untilrule.r_todisgmt)
1979 untiltime = tadd(untiltime,
1981 if (!zp->z_untilrule.r_todisstd)
1982 untiltime = tadd(untiltime,
1986 ** Find the rule (of those to do, if any)
1987 ** that takes effect earliest in the year.
1990 for (j = 0; j < zp->z_nrules; ++j) {
1991 rp = &zp->z_rules[j];
1994 eats(zp->z_filename, zp->z_linenum,
1995 rp->r_filename, rp->r_linenum);
1996 offset = rp->r_todisgmt ? 0 : gmtoff;
1997 if (!rp->r_todisstd)
1998 offset = oadd(offset, stdoff);
2000 if (jtime == min_time ||
2003 jtime = tadd(jtime, -offset);
2004 if (k < 0 || jtime < ktime) {
2010 break; /* go on to next year */
2011 rp = &zp->z_rules[k];
2013 if (useuntil && ktime >= untiltime)
2015 stdoff = rp->r_stdoff;
2016 if (usestart && ktime == starttime)
2019 if (ktime < starttime) {
2020 startoff = oadd(zp->z_gmtoff,
2022 doabbr(startbuf, zp->z_format,
2028 if (*startbuf == '\0' &&
2029 startoff == oadd(zp->z_gmtoff,
2039 eats(zp->z_filename, zp->z_linenum,
2040 rp->r_filename, rp->r_linenum);
2041 doabbr(ab, zp->z_format, rp->r_abbrvar,
2042 rp->r_stdoff != 0, FALSE);
2043 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
2044 type = addtype(offset, ab, rp->r_stdoff != 0,
2045 rp->r_todisstd, rp->r_todisgmt);
2050 if (*startbuf == '\0' &&
2051 zp->z_format != NULL &&
2052 strchr(zp->z_format, '%') == NULL &&
2053 strchr(zp->z_format, '/') == NULL)
2054 strcpy(startbuf, zp->z_format);
2055 eat(zp->z_filename, zp->z_linenum);
2056 if (*startbuf == '\0')
2057 error(_("can't determine time zone abbreviation to use just after until time"));
2058 else addtt(starttime,
2059 addtype(startoff, startbuf,
2060 startoff != zp->z_gmtoff,
2065 ** Now we may get to set starttime for the next zone line.
2068 startttisstd = zp->z_untilrule.r_todisstd;
2069 startttisgmt = zp->z_untilrule.r_todisgmt;
2070 starttime = zp->z_untiltime;
2072 starttime = tadd(starttime, -stdoff);
2074 starttime = tadd(starttime, -gmtoff);
2077 writezone(zpfirst->z_name, envvar);
2084 addtt(const zic_t starttime, int type)
2086 if (starttime <= min_time ||
2087 (timecnt == 1 && attypes[0].at < min_time)) {
2088 gmtoffs[0] = gmtoffs[type];
2089 isdsts[0] = isdsts[type];
2090 ttisstds[0] = ttisstds[type];
2091 ttisgmts[0] = ttisgmts[type];
2092 if (abbrinds[type] != 0)
2093 strcpy(chars, &chars[abbrinds[type]]);
2095 charcnt = strlen(chars) + 1;
2100 if (timecnt >= TZ_MAX_TIMES) {
2101 error(_("too many transitions?!"));
2104 attypes[timecnt].at = starttime;
2105 attypes[timecnt].type = type;
2110 addtype(const long gmtoff, const char * const abbr, const int isdst,
2111 const int ttisstd, const int ttisgmt)
2115 if (isdst != TRUE && isdst != FALSE) {
2116 error(_("internal error - addtype called with bad isdst"));
2119 if (ttisstd != TRUE && ttisstd != FALSE) {
2120 error(_("internal error - addtype called with bad ttisstd"));
2123 if (ttisgmt != TRUE && ttisgmt != FALSE) {
2124 error(_("internal error - addtype called with bad ttisgmt"));
2128 ** See if there's already an entry for this zone type.
2129 ** If so, just return its index.
2131 for (i = 0; i < typecnt; ++i) {
2132 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
2133 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
2134 ttisstd == ttisstds[i] &&
2135 ttisgmt == ttisgmts[i])
2139 ** There isn't one; add a new one, unless there are already too
2142 if (typecnt >= TZ_MAX_TYPES) {
2143 error(_("too many local time types"));
2146 if (! (-1L - 2147483647L <= gmtoff && gmtoff <= 2147483647L)) {
2147 error(_("UTC offset out of range"));
2150 gmtoffs[i] = gmtoff;
2152 ttisstds[i] = ttisstd;
2153 ttisgmts[i] = ttisgmt;
2155 for (j = 0; j < charcnt; ++j)
2156 if (strcmp(&chars[j], abbr) == 0)
2166 leapadd(const zic_t t, const int positive, const int rolling, int count)
2170 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
2171 error(_("too many leap seconds"));
2174 for (i = 0; i < leapcnt; ++i)
2175 if (t <= trans[i]) {
2176 if (t == trans[i]) {
2177 error(_("repeated leap second moment"));
2183 for (j = leapcnt; j > i; --j) {
2184 trans[j] = trans[j - 1];
2185 corr[j] = corr[j - 1];
2186 roll[j] = roll[j - 1];
2189 corr[i] = positive ? 1L : eitol(-count);
2192 } while (positive && --count != 0);
2202 ** propagate leap seconds forward
2204 for (i = 0; i < leapcnt; ++i) {
2205 trans[i] = tadd(trans[i], last);
2206 last = corr[i] += last;
2211 yearistype(const int year, const char * const type)
2216 if (type == NULL || *type == '\0')
2218 buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
2219 sprintf(buf, "%s %d %s", yitcommand, year, type);
2220 result = system(buf);
2221 if (WIFEXITED(result)) switch (WEXITSTATUS(result)) {
2227 error(_("Wild result from command execution"));
2228 warnx(_("command was '%s', result was %d"), buf, result);
2236 a = (unsigned char) a;
2237 return (isascii(a) && isupper(a)) ? tolower(a) : a;
2241 ciequal(const char *ap, const char *bp) /* case-insensitive equality */
2243 while (lowerit(*ap) == lowerit(*bp++))
2250 itsabbr(const char *abbr, const char *word)
2252 if (lowerit(*abbr) != lowerit(*word))
2255 while (*++abbr != '\0')
2259 } while (lowerit(*word++) != lowerit(*abbr));
2263 static const struct lookup *
2264 byword(const char * const word, const struct lookup * const table)
2266 const struct lookup *foundlp;
2267 const struct lookup *lp;
2269 if (word == NULL || table == NULL)
2272 ** Look for exact match.
2274 for (lp = table; lp->l_word != NULL; ++lp)
2275 if (ciequal(word, lp->l_word))
2278 ** Look for inexact match.
2281 for (lp = table; lp->l_word != NULL; ++lp)
2282 if (itsabbr(word, lp->l_word)) {
2283 if (foundlp == NULL)
2285 else return NULL; /* multiple inexact matches */
2299 array = (char **) (void *)
2300 emalloc((int) ((strlen(cp) + 1) * sizeof *array));
2303 while (isascii((unsigned char) *cp) &&
2304 isspace((unsigned char) *cp))
2306 if (*cp == '\0' || *cp == '#')
2308 array[nsubs++] = dp = cp;
2310 if ((*dp = *cp++) != '"')
2312 else while ((*dp = *cp++) != '"')
2317 "Odd number of quotation marks"
2321 } while (*cp != '\0' && *cp != '#' &&
2322 (!isascii(*cp) || !isspace((unsigned char) *cp)));
2323 if (isascii(*cp) && isspace((unsigned char) *cp))
2327 array[nsubs] = NULL;
2332 oadd(const long t1, const long t2)
2337 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2338 error(_("time overflow"));
2345 tadd(const zic_t t1, const long t2)
2349 if (t1 == max_time && t2 > 0)
2351 if (t1 == min_time && t2 < 0)
2354 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2355 error(_("time overflow"));
2362 ** Given a rule, and a year, compute the date - in seconds since January 1,
2363 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2367 rpytime(const struct rule * const rp, const int wantedy)
2370 long dayoff; /* with a nod to Margaret O. */
2373 if (wantedy == INT_MIN)
2375 if (wantedy == INT_MAX)
2380 while (wantedy != y) {
2382 i = len_years[isleap(y)];
2386 i = -len_years[isleap(y)];
2388 dayoff = oadd(dayoff, eitol(i));
2390 while (m != rp->r_month) {
2391 i = len_months[isleap(y)][m];
2392 dayoff = oadd(dayoff, eitol(i));
2395 i = rp->r_dayofmonth;
2396 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
2397 if (rp->r_dycode == DC_DOWLEQ)
2400 error(_("use of 2/29 in non leap-year"));
2405 dayoff = oadd(dayoff, eitol(i));
2406 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
2409 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2410 wday = eitol(EPOCH_WDAY);
2412 ** Don't trust mod of negative numbers.
2415 wday = (wday + dayoff) % LDAYSPERWEEK;
2417 wday -= ((-dayoff) % LDAYSPERWEEK);
2419 wday += LDAYSPERWEEK;
2421 while (wday != eitol(rp->r_wday))
2422 if (rp->r_dycode == DC_DOWGEQ) {
2423 dayoff = oadd(dayoff, (long) 1);
2424 if (++wday >= LDAYSPERWEEK)
2428 dayoff = oadd(dayoff, (long) -1);
2430 wday = LDAYSPERWEEK - 1;
2433 if (i < 0 || i >= len_months[isleap(y)][m]) {
2435 warning(_("rule goes past start/end of month--\
2436 will not work with pre-2004 versions of zic"));
2439 if (dayoff < min_time / SECSPERDAY)
2441 if (dayoff > max_time / SECSPERDAY)
2443 t = (zic_t) dayoff * SECSPERDAY;
2444 return tadd(t, rp->r_tod);
2448 newabbr(const char * const string)
2452 if (strcmp(string, GRANDPARENTED) != 0) {
2457 ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2458 ** optionally followed by a + or - and a number from 1 to 14.
2462 while (isascii((unsigned char) *cp) &&
2463 isalpha((unsigned char) *cp))
2465 if (cp - string == 0)
2466 wp = _("time zone abbreviation lacks alphabetic at start");
2467 if (noise && cp - string > 3)
2468 wp = _("time zone abbreviation has more than 3 alphabetics");
2469 if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
2470 wp = _("time zone abbreviation has too many alphabetics");
2471 if (wp == NULL && (*cp == '+' || *cp == '-')) {
2473 if (isascii((unsigned char) *cp) &&
2474 isdigit((unsigned char) *cp))
2476 *cp >= '0' && *cp <= '4')
2480 wp = _("time zone abbreviation differs from POSIX standard");
2483 wp = ecatalloc(wp, " (");
2484 wp = ecatalloc(wp, string);
2485 wp = ecatalloc(wp, ")");
2490 i = strlen(string) + 1;
2491 if (charcnt + i > TZ_MAX_CHARS) {
2492 error(_("too many, or too long, time zone abbreviations"));
2495 strcpy(&chars[charcnt], string);
2496 charcnt += eitol(i);
2500 mkdirs(char *argname)
2505 if (argname == NULL || *argname == '\0' || Dflag)
2507 cp = name = ecpyalloc(argname);
2508 while ((cp = strchr(cp + 1, '/')) != 0) {
2512 ** DOS drive specifier?
2514 if (isalpha((unsigned char) name[0]) &&
2515 name[1] == ':' && name[2] == '\0') {
2519 #endif /* !defined unix */
2520 if (!itsdir(name)) {
2522 ** It doesn't seem to exist, so we try to create it.
2523 ** Creation may fail because of the directory being
2524 ** created by some other multiprocessor, so we get
2525 ** to do extra checking.
2527 if ((mkdir(name, MKDIR_UMASK) != 0) &&
2528 (errno != EEXIST || !itsdir(name))) {
2529 warn(_("can't create directory %s"), name);
2546 if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0))
2547 errx(EXIT_FAILURE, _("%d did not sign extend correctly"), i);
2555 setgroup(gid_t *flag, const char *name)
2559 if (*flag != (gid_t)-1)
2560 errx(EXIT_FAILURE, _("multiple -g flags specified"));
2562 gr = getgrnam(name);
2567 ul = strtoul(name, &ep, 10);
2568 if (ul == (unsigned long)(gid_t)ul && *ep == '\0') {
2572 errx(EXIT_FAILURE, _("group `%s' not found"), name);
2578 setuser(uid_t *flag, const char *name)
2582 if (*flag != (gid_t)-1)
2583 errx(EXIT_FAILURE, _("multiple -u flags specified"));
2585 pw = getpwnam(name);
2590 ul = strtoul(name, &ep, 10);
2591 if (ul == (unsigned long)(gid_t)ul && *ep == '\0') {
2595 errx(EXIT_FAILURE, _("user `%s' not found"), name);
2601 ** UNIX was a registered trademark of The Open Group in 2003.