3 static char elsieid[] = "@(#)zic.c 7.96";
4 #endif /* !defined NOID */
5 #endif /* !defined lint */
9 * $FreeBSD: src/usr.sbin/zic/zic.c,v 1.11 1999/08/28 01:21:20 peter Exp $
10 * $DragonFly: src/usr.sbin/zic/zic.c,v 1.2 2003/06/17 04:30:05 dillon Exp $
16 #include <sys/stat.h> /* for umask manifest constants */
17 #include <sys/types.h>
21 ** On some ancient hosts, predicates like `isspace(C)' are defined
22 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
23 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
24 ** Neither the C Standard nor Posix require that `isascii' exist.
25 ** For portability, we check both ancient and modern requirements.
26 ** If isascii is not defined, the isascii check succeeds trivially.
34 const char * r_filename;
38 int r_loyear; /* for example, 1986 */
39 int r_hiyear; /* for example, 1986 */
40 const char * r_yrtype;
42 int r_month; /* 0..11 */
44 int r_dycode; /* see below */
48 long r_tod; /* time from midnight */
49 int r_todisstd; /* above is standard time if TRUE */
50 /* or wall clock time if FALSE */
51 int r_todisgmt; /* above is GMT if TRUE */
52 /* or local time if FALSE */
53 long r_stdoff; /* offset from standard time */
54 const char * r_abbrvar; /* variable part of abbreviation */
56 int r_todo; /* a rule to do (used in outzone) */
57 time_t r_temp; /* used in outzone */
61 ** r_dycode r_dayofmonth r_wday
64 #define DC_DOM 0 /* 1..31 */ /* unused */
65 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
66 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
69 const char * z_filename;
75 const char * z_format;
79 struct rule * z_rules;
82 struct rule z_untilrule;
86 static void addtt P((time_t starttime, int type));
87 static int addtype P((long gmtoff, const char * abbr, int isdst,
88 int ttisstd, int ttisgmt));
89 static void leapadd P((time_t t, int positive, int rolling, int count));
90 static void adjleap P((void));
91 static void associate P((void));
92 static int ciequal P((const char * ap, const char * bp));
93 static void convert P((long val, char * buf));
94 static void dolink P((const char * fromfile, const char * tofile));
95 static void doabbr P((char * abbr, const char * format,
96 const char * letters, int isdst));
97 static void eat P((const char * name, int num));
98 static void eats P((const char * name, int num,
99 const char * rname, int rnum));
100 static long eitol P((int i));
101 static void error P((const char * message));
102 static char ** getfields P((char * buf));
103 static long gethms P((const char * string, const char * errstrng,
105 static void infile P((const char * filename));
106 static void inleap P((char ** fields, int nfields));
107 static void inlink P((char ** fields, int nfields));
108 static void inrule P((char ** fields, int nfields));
109 static int inzcont P((char ** fields, int nfields));
110 static int inzone P((char ** fields, int nfields));
111 static int inzsub P((char ** fields, int nfields, int iscont));
112 static int itsabbr P((const char * abbr, const char * word));
113 static int itsdir P((const char * name));
114 static int lowerit P((int c));
115 static char * memcheck P((char * tocheck));
116 static int mkdirs P((char * filename));
117 static void newabbr P((const char * abbr));
118 static long oadd P((long t1, long t2));
119 static void outzone P((const struct zone * zp, int ntzones));
120 static void puttzcode P((long code, FILE * fp));
121 static int rcomp P((const void * leftp, const void * rightp));
122 static time_t rpytime P((const struct rule * rp, int wantedy));
123 static void rulesub P((struct rule * rp,
124 const char * loyearp, const char * hiyearp,
125 const char * typep, const char * monthp,
126 const char * dayp, const char * timep));
127 static void setboundaries P((void));
128 static void setgroup P((gid_t *flag, const char *name));
129 static void setuser P((uid_t *flag, const char *name));
130 static time_t tadd P((time_t t1, long t2));
131 static void usage P((void));
132 static void writezone P((const char * name));
133 static int yearistype P((int year, const char * type));
135 #if !(HAVE_STRERROR - 0)
136 static char * strerror P((int));
137 #endif /* !(HAVE_STRERROR - 0) */
141 static const char * filename;
144 static time_t max_time;
146 static int max_year_representable;
147 static time_t min_time;
149 static int min_year_representable;
151 static const char * rfilename;
166 ** Which fields are which on a Zone line.
174 #define ZF_TILMONTH 6
177 #define ZONE_MINFIELDS 5
178 #define ZONE_MAXFIELDS 9
181 ** Which fields are which on a Zone continuation line.
187 #define ZFC_TILYEAR 3
188 #define ZFC_TILMONTH 4
190 #define ZFC_TILTIME 6
191 #define ZONEC_MINFIELDS 3
192 #define ZONEC_MAXFIELDS 7
195 ** Which files are which on a Rule line.
207 #define RULE_FIELDS 10
210 ** Which fields are which on a Link line.
215 #define LINK_FIELDS 3
218 ** Which fields are which on a Leap line.
227 #define LEAP_FIELDS 7
237 static struct rule * rules;
238 static int nrules; /* number of rules */
240 static struct zone * zones;
241 static int nzones; /* number of zones */
244 const char * l_filename;
250 static struct link * links;
258 static struct lookup const * byword P((const char * string,
259 const struct lookup * lp));
261 static struct lookup const line_codes[] = {
269 static struct lookup const mon_names[] = {
270 { "January", TM_JANUARY },
271 { "February", TM_FEBRUARY },
272 { "March", TM_MARCH },
273 { "April", TM_APRIL },
277 { "August", TM_AUGUST },
278 { "September", TM_SEPTEMBER },
279 { "October", TM_OCTOBER },
280 { "November", TM_NOVEMBER },
281 { "December", TM_DECEMBER },
285 static struct lookup const wday_names[] = {
286 { "Sunday", TM_SUNDAY },
287 { "Monday", TM_MONDAY },
288 { "Tuesday", TM_TUESDAY },
289 { "Wednesday", TM_WEDNESDAY },
290 { "Thursday", TM_THURSDAY },
291 { "Friday", TM_FRIDAY },
292 { "Saturday", TM_SATURDAY },
296 static struct lookup const lasts[] = {
297 { "last-Sunday", TM_SUNDAY },
298 { "last-Monday", TM_MONDAY },
299 { "last-Tuesday", TM_TUESDAY },
300 { "last-Wednesday", TM_WEDNESDAY },
301 { "last-Thursday", TM_THURSDAY },
302 { "last-Friday", TM_FRIDAY },
303 { "last-Saturday", TM_SATURDAY },
307 static struct lookup const begin_years[] = {
308 { "minimum", YR_MINIMUM },
309 { "maximum", YR_MAXIMUM },
313 static struct lookup const end_years[] = {
314 { "minimum", YR_MINIMUM },
315 { "maximum", YR_MAXIMUM },
320 static struct lookup const leap_types[] = {
322 { "Stationary", FALSE },
326 static const int len_months[2][MONSPERYEAR] = {
327 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
328 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
331 static const int len_years[2] = {
332 DAYSPERNYEAR, DAYSPERLYEAR
335 static struct attype {
338 } attypes[TZ_MAX_TIMES];
339 static long gmtoffs[TZ_MAX_TYPES];
340 static char isdsts[TZ_MAX_TYPES];
341 static unsigned char abbrinds[TZ_MAX_TYPES];
342 static char ttisstds[TZ_MAX_TYPES];
343 static char ttisgmts[TZ_MAX_TYPES];
344 static char chars[TZ_MAX_CHARS];
345 static time_t trans[TZ_MAX_LEAPS];
346 static long corr[TZ_MAX_LEAPS];
347 static char roll[TZ_MAX_LEAPS];
350 ** Memory allocation.
358 errx(EXIT_FAILURE, _("memory exhausted"));
362 #define emalloc(size) memcheck(imalloc(size))
363 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
364 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
365 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
371 #if !(HAVE_STRERROR - 0)
376 extern char * sys_errlist[];
379 return (errnum > 0 && errnum <= sys_nerr) ?
380 sys_errlist[errnum] : _("Unknown system error");
382 #endif /* !(HAVE_STRERROR - 0) */
385 eats(name, num, rname, rnum)
386 const char * const name;
388 const char * const rname;
399 const char * const name;
402 eats(name, num, (char *) NULL, -1);
407 const char * const string;
410 ** Match the format of "cc" to allow sh users to
411 ** zic ... 2>&1 | error -t "*" -v
414 (void) fprintf(stderr, _("\"%s\", line %d: %s"),
415 filename, linenum, string);
416 if (rfilename != NULL)
417 (void) fprintf(stderr, _(" (rule from \"%s\", line %d)"),
418 rfilename, rlinenum);
419 (void) fprintf(stderr, "\n");
425 const char * const string;
429 cp = ecpyalloc(_("warning: "));
430 cp = ecatalloc(cp, string);
439 (void) fprintf(stderr, "%s\n%s\n",
440 _("usage: zic [-s] [-v] [-l localtime] [-p posixrules] [-d directory]"),
441 _(" [-L leapseconds] [-y yearistype] [filename ... ]"));
442 (void) exit(EXIT_FAILURE);
445 static const char * psxrules;
446 static const char * lcltime;
447 static const char * directory;
448 static const char * leapsec;
449 static const char * yitcommand;
450 static int sflag = FALSE;
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
467 (void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
468 #endif /* defined unix */
470 (void) setlocale(LC_MESSAGES, "");
472 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
473 #endif /* defined TEXTDOMAINDIR */
474 (void) textdomain(TZ_DOMAIN);
475 #endif /* HAVE_GETTEXT - 0 */
476 while ((c = getopt(argc, argv, "Dd:g:l:m:p:L:u:vsy:")) != -1)
484 if (directory == NULL)
488 _("more than one -d option specified"));
491 setgroup(&gflag, optarg);
498 _("more than one -l option specified"));
502 void *set = setmode(optarg);
507 if (psxrules == NULL)
511 _("more than one -p option specified"));
514 setuser(&uflag, optarg);
517 if (yitcommand == NULL)
521 _("more than one -y option specified"));
528 _("more than one -L option specified"));
537 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
538 usage(); /* usage message by request */
539 if (directory == NULL)
541 if (yitcommand == NULL)
542 yitcommand = "yearistype";
546 if (optind < argc && leapsec != NULL) {
551 for (i = optind; i < argc; ++i)
554 (void) exit(EXIT_FAILURE);
556 for (i = 0; i < nzones; i = j) {
558 ** Find the next non-continuation zone entry.
560 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
562 outzone(&zones[i], j - i);
567 for (i = 0; i < nlinks; ++i)
568 dolink(links[i].l_from, links[i].l_to);
570 dolink(lcltime, TZDEFAULT);
571 if (psxrules != NULL)
572 dolink(psxrules, TZDEFRULES);
573 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
577 dolink(fromfile, tofile)
578 const char * const fromfile;
579 const char * const tofile;
581 register char * fromname;
582 register char * toname;
584 if (fromfile[0] == '/')
585 fromname = ecpyalloc(fromfile);
587 fromname = ecpyalloc(directory);
588 fromname = ecatalloc(fromname, "/");
589 fromname = ecatalloc(fromname, fromfile);
591 if (tofile[0] == '/')
592 toname = ecpyalloc(tofile);
594 toname = ecpyalloc(directory);
595 toname = ecatalloc(toname, "/");
596 toname = ecatalloc(toname, tofile);
599 ** We get to be careful here since
600 ** there's a fair chance of root running us.
603 (void) remove(toname);
604 if (link(fromname, toname) != 0) {
607 if (mkdirs(toname) != 0)
608 (void) exit(EXIT_FAILURE);
609 result = link(fromname, toname);
610 #if (HAVE_SYMLINK - 0)
612 result = symlink(fromname, toname);
614 warning(_("hard link failed, symbolic link used"));
618 err(EXIT_FAILURE, _("can't link from %s to %s"),
627 #define INT_MAX ((int) (((unsigned)~0)>>1))
628 #endif /* !defined INT_MAX */
631 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
632 #endif /* !defined INT_MIN */
635 ** The tz file format currently allows at most 32-bit quantities.
636 ** This restriction should be removed before signed 32-bit values
637 ** wrap around in 2038, but unfortunately this will require a
638 ** change to the tz file format.
641 #define MAX_BITS_IN_FILE 32
642 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(time_t) < MAX_BITS_IN_FILE) ? TYPE_BIT(time_t) : MAX_BITS_IN_FILE)
645 setboundaries P((void))
647 if (TYPE_SIGNED(time_t)) {
648 min_time = ~ (time_t) 0;
649 min_time <<= TIME_T_BITS_IN_FILE - 1;
650 max_time = ~ (time_t) 0 - min_time;
655 max_time = 2 - sflag;
656 max_time <<= TIME_T_BITS_IN_FILE - 1;
659 min_year = TM_YEAR_BASE + gmtime(&min_time)->tm_year;
660 max_year = TM_YEAR_BASE + gmtime(&max_time)->tm_year;
661 min_year_representable = min_year;
662 max_year_representable = max_year;
667 const char * const name;
669 register char * myname;
672 myname = ecpyalloc(name);
673 myname = ecatalloc(myname, "/.");
674 accres = access(myname, F_OK);
680 ** Associate sets of rules with zones.
684 ** Sort by rule name.
692 return strcmp(((const struct rule *) cp1)->r_name,
693 ((const struct rule *) cp2)->r_name);
699 register struct zone * zp;
700 register struct rule * rp;
701 register int base, out;
705 (void) qsort((void *) rules, (size_t) nrules,
706 (size_t) sizeof *rules, rcomp);
707 for (i = 0; i < nrules - 1; ++i) {
708 if (strcmp(rules[i].r_name,
709 rules[i + 1].r_name) != 0)
711 if (strcmp(rules[i].r_filename,
712 rules[i + 1].r_filename) == 0)
714 eat(rules[i].r_filename, rules[i].r_linenum);
715 warning(_("same rule name in multiple files"));
716 eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
717 warning(_("same rule name in multiple files"));
718 for (j = i + 2; j < nrules; ++j) {
719 if (strcmp(rules[i].r_name,
720 rules[j].r_name) != 0)
722 if (strcmp(rules[i].r_filename,
723 rules[j].r_filename) == 0)
725 if (strcmp(rules[i + 1].r_filename,
726 rules[j].r_filename) == 0)
733 for (i = 0; i < nzones; ++i) {
738 for (base = 0; base < nrules; base = out) {
740 for (out = base + 1; out < nrules; ++out)
741 if (strcmp(rp->r_name, rules[out].r_name) != 0)
743 for (i = 0; i < nzones; ++i) {
745 if (strcmp(zp->z_rule, rp->r_name) != 0)
748 zp->z_nrules = out - base;
751 for (i = 0; i < nzones; ++i) {
753 if (zp->z_nrules == 0) {
755 ** Maybe we have a local standard time offset.
757 eat(zp->z_filename, zp->z_linenum);
758 zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
761 ** Note, though, that if there's no rule,
762 ** a '%s' in the format is a bad thing.
764 if (strchr(zp->z_format, '%') != 0)
765 error(_("%s in ruleless zone"));
769 (void) exit(EXIT_FAILURE);
777 register char ** fields;
779 register const struct lookup * lp;
780 register int nfields;
781 register int wantcont;
785 if (strcmp(name, "-") == 0) {
786 name = _("standard input");
788 } else if ((fp = fopen(name, "r")) == NULL)
789 err(EXIT_FAILURE, _("can't open %s"), name);
791 for (num = 1; ; ++num) {
793 if (fgets(buf, (int) sizeof buf, fp) != buf)
795 cp = strchr(buf, '\n');
797 error(_("line too long"));
798 (void) exit(EXIT_FAILURE);
801 fields = getfields(buf);
803 while (fields[nfields] != NULL) {
806 if (strcmp(fields[nfields], "-") == 0)
807 fields[nfields] = &nada;
812 } else if (wantcont) {
813 wantcont = inzcont(fields, nfields);
815 lp = byword(fields[0], line_codes);
817 error(_("input line of unknown type"));
818 else switch ((int) (lp->l_value)) {
820 inrule(fields, nfields);
824 wantcont = inzone(fields, nfields);
827 inlink(fields, nfields);
833 _("leap line in non leap seconds file %s"), name);
834 else inleap(fields, nfields);
837 default: /* "cannot happen" */
839 _("panic: invalid l_value %d"), lp->l_value);
842 ifree((char *) fields);
845 errx(EXIT_FAILURE, _("error reading %s"), filename);
846 if (fp != stdin && fclose(fp))
847 err(EXIT_FAILURE, _("error closing %s"), filename);
849 error(_("expected continuation line not found"));
853 ** Convert a string of one of the forms
854 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
855 ** into a number of seconds.
856 ** A null string maps to zero.
857 ** Call error with errstring and return zero on errors.
861 gethms(string, errstring, signable)
863 const char * const errstring;
866 int hh, mm, ss, sign;
868 if (string == NULL || *string == '\0')
872 else if (*string == '-') {
876 if (sscanf(string, scheck(string, "%d"), &hh) == 1)
878 else if (sscanf(string, scheck(string, "%d:%d"), &hh, &mm) == 2)
880 else if (sscanf(string, scheck(string, "%d:%d:%d"),
881 &hh, &mm, &ss) != 3) {
885 if ((hh < 0 || hh >= HOURSPERDAY ||
886 mm < 0 || mm >= MINSPERHOUR ||
887 ss < 0 || ss > SECSPERMIN) &&
888 !(hh == HOURSPERDAY && mm == 0 && ss == 0)) {
893 (eitol(hh * MINSPERHOUR + mm) *
894 eitol(SECSPERMIN) + eitol(ss));
898 inrule(fields, nfields)
899 register char ** const fields;
902 static struct rule r;
904 if (nfields != RULE_FIELDS) {
905 error(_("wrong number of fields on Rule line"));
908 if (*fields[RF_NAME] == '\0') {
909 error(_("nameless rule"));
912 r.r_filename = filename;
913 r.r_linenum = linenum;
914 r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
915 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
916 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
917 r.r_name = ecpyalloc(fields[RF_NAME]);
918 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
919 rules = (struct rule *) (void *) erealloc((char *) rules,
920 (int) ((nrules + 1) * sizeof *rules));
925 inzone(fields, nfields)
926 register char ** const fields;
932 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
933 error(_("wrong number of fields on Zone line"));
936 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
937 buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
939 _("\"Zone %s\" line and -l option are mutually exclusive"),
944 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
945 buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
947 _("\"Zone %s\" line and -p option are mutually exclusive"),
952 for (i = 0; i < nzones; ++i)
953 if (zones[i].z_name != NULL &&
954 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
955 buf = erealloc(buf, (int) (132 +
956 strlen(fields[ZF_NAME]) +
957 strlen(zones[i].z_filename)));
959 _("duplicate zone name %s (file \"%s\", line %d)"),
966 return inzsub(fields, nfields, FALSE);
970 inzcont(fields, nfields)
971 register char ** const fields;
974 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
975 error(_("wrong number of fields on Zone continuation line"));
978 return inzsub(fields, nfields, TRUE);
982 inzsub(fields, nfields, iscont)
983 register char ** const fields;
988 static struct zone z;
989 register int i_gmtoff, i_rule, i_format;
990 register int i_untilyear, i_untilmonth;
991 register int i_untilday, i_untiltime;
992 register int hasuntil;
995 i_gmtoff = ZFC_GMTOFF;
997 i_format = ZFC_FORMAT;
998 i_untilyear = ZFC_TILYEAR;
999 i_untilmonth = ZFC_TILMONTH;
1000 i_untilday = ZFC_TILDAY;
1001 i_untiltime = ZFC_TILTIME;
1004 i_gmtoff = ZF_GMTOFF;
1006 i_format = ZF_FORMAT;
1007 i_untilyear = ZF_TILYEAR;
1008 i_untilmonth = ZF_TILMONTH;
1009 i_untilday = ZF_TILDAY;
1010 i_untiltime = ZF_TILTIME;
1011 z.z_name = ecpyalloc(fields[ZF_NAME]);
1013 z.z_filename = filename;
1014 z.z_linenum = linenum;
1015 z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UTC offset"), TRUE);
1016 if ((cp = strchr(fields[i_format], '%')) != 0) {
1017 if (*++cp != 's' || strchr(cp, '%') != 0) {
1018 error(_("invalid abbreviation format"));
1022 z.z_rule = ecpyalloc(fields[i_rule]);
1023 z.z_format = ecpyalloc(fields[i_format]);
1024 hasuntil = nfields > i_untilyear;
1026 z.z_untilrule.r_filename = filename;
1027 z.z_untilrule.r_linenum = linenum;
1028 rulesub(&z.z_untilrule,
1029 fields[i_untilyear],
1032 (nfields > i_untilmonth) ?
1033 fields[i_untilmonth] : "Jan",
1034 (nfields > i_untilday) ? fields[i_untilday] : "1",
1035 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1036 z.z_untiltime = rpytime(&z.z_untilrule,
1037 z.z_untilrule.r_loyear);
1038 if (iscont && nzones > 0 &&
1039 z.z_untiltime > min_time &&
1040 z.z_untiltime < max_time &&
1041 zones[nzones - 1].z_untiltime > min_time &&
1042 zones[nzones - 1].z_untiltime < max_time &&
1043 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1044 error(_("Zone continuation line end time is not after end time of previous line"));
1048 zones = (struct zone *) (void *) erealloc((char *) zones,
1049 (int) ((nzones + 1) * sizeof *zones));
1050 zones[nzones++] = z;
1052 ** If there was an UNTIL field on this line,
1053 ** there's more information about the zone on the next line.
1059 inleap(fields, nfields)
1060 register char ** const fields;
1063 register const char * cp;
1064 register const struct lookup * lp;
1066 int year, month, day;
1070 if (nfields != LEAP_FIELDS) {
1071 error(_("wrong number of fields on Leap line"));
1075 cp = fields[LP_YEAR];
1076 if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
1080 error(_("invalid leaping year"));
1086 i = len_years[isleap(j)];
1090 i = -len_years[isleap(j)];
1092 dayoff = oadd(dayoff, eitol(i));
1094 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1095 error(_("invalid month name"));
1098 month = lp->l_value;
1100 while (j != month) {
1101 i = len_months[isleap(year)][j];
1102 dayoff = oadd(dayoff, eitol(i));
1105 cp = fields[LP_DAY];
1106 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1107 day <= 0 || day > len_months[isleap(year)][month]) {
1108 error(_("invalid day of month"));
1111 dayoff = oadd(dayoff, eitol(day - 1));
1112 if (dayoff < 0 && !TYPE_SIGNED(time_t)) {
1113 error(_("time before zero"));
1116 t = (time_t) dayoff * SECSPERDAY;
1118 ** Cheap overflow check.
1120 if (t / SECSPERDAY != dayoff) {
1121 error(_("time overflow"));
1124 tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1125 cp = fields[LP_CORR];
1127 register int positive;
1130 if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1133 } else if (strcmp(cp, "--") == 0) {
1136 } else if (strcmp(cp, "+") == 0) {
1139 } else if (strcmp(cp, "++") == 0) {
1143 error(_("illegal CORRECTION field on Leap line"));
1146 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1147 error(_("illegal Rolling/Stationary field on Leap line"));
1150 leapadd(tadd(t, tod), positive, lp->l_value, count);
1155 inlink(fields, nfields)
1156 register char ** const fields;
1161 if (nfields != LINK_FIELDS) {
1162 error(_("wrong number of fields on Link line"));
1165 if (*fields[LF_FROM] == '\0') {
1166 error(_("blank FROM field on Link line"));
1169 if (*fields[LF_TO] == '\0') {
1170 error(_("blank TO field on Link line"));
1173 l.l_filename = filename;
1174 l.l_linenum = linenum;
1175 l.l_from = ecpyalloc(fields[LF_FROM]);
1176 l.l_to = ecpyalloc(fields[LF_TO]);
1177 links = (struct link *) (void *) erealloc((char *) links,
1178 (int) ((nlinks + 1) * sizeof *links));
1179 links[nlinks++] = l;
1183 rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
1184 register struct rule * const rp;
1185 const char * const loyearp;
1186 const char * const hiyearp;
1187 const char * const typep;
1188 const char * const monthp;
1189 const char * const dayp;
1190 const char * const timep;
1192 register const struct lookup * lp;
1193 register const char * cp;
1197 if ((lp = byword(monthp, mon_names)) == NULL) {
1198 error(_("invalid month name"));
1201 rp->r_month = lp->l_value;
1202 rp->r_todisstd = FALSE;
1203 rp->r_todisgmt = FALSE;
1204 dp = ecpyalloc(timep);
1206 ep = dp + strlen(dp) - 1;
1207 switch (lowerit(*ep)) {
1208 case 's': /* Standard */
1209 rp->r_todisstd = TRUE;
1210 rp->r_todisgmt = FALSE;
1213 case 'w': /* Wall */
1214 rp->r_todisstd = FALSE;
1215 rp->r_todisgmt = FALSE;
1218 case 'g': /* Greenwich */
1219 case 'u': /* Universal */
1220 case 'z': /* Zulu */
1221 rp->r_todisstd = TRUE;
1222 rp->r_todisgmt = TRUE;
1227 rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1233 lp = byword(cp, begin_years);
1234 if (lp != NULL) switch ((int) lp->l_value) {
1236 rp->r_loyear = INT_MIN;
1239 rp->r_loyear = INT_MAX;
1241 default: /* "cannot happen" */
1243 _("panic: invalid l_value %d"), lp->l_value);
1244 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
1245 error(_("invalid starting year"));
1248 if (rp->r_loyear < min_year_representable)
1249 warning(_("starting year too low to be represented"));
1250 else if (rp->r_loyear > max_year_representable)
1251 warning(_("starting year too high to be represented"));
1254 if ((lp = byword(cp, end_years)) != NULL) switch ((int) lp->l_value) {
1256 rp->r_hiyear = INT_MIN;
1259 rp->r_hiyear = INT_MAX;
1262 rp->r_hiyear = rp->r_loyear;
1264 default: /* "cannot happen" */
1266 _("panic: invalid l_value %d"), lp->l_value);
1267 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
1268 error(_("invalid ending year"));
1271 if (rp->r_loyear < min_year_representable)
1272 warning(_("starting year too low to be represented"));
1273 else if (rp->r_loyear > max_year_representable)
1274 warning(_("starting year too high to be represented"));
1276 if (rp->r_loyear > rp->r_hiyear) {
1277 error(_("starting year greater than ending year"));
1281 rp->r_yrtype = NULL;
1283 if (rp->r_loyear == rp->r_hiyear) {
1284 error(_("typed single year"));
1287 rp->r_yrtype = ecpyalloc(typep);
1289 if (rp->r_loyear < min_year && rp->r_loyear > 0)
1290 min_year = rp->r_loyear;
1293 ** Accept things such as:
1299 dp = ecpyalloc(dayp);
1300 if ((lp = byword(dp, lasts)) != NULL) {
1301 rp->r_dycode = DC_DOWLEQ;
1302 rp->r_wday = lp->l_value;
1303 rp->r_dayofmonth = len_months[1][rp->r_month];
1305 if ((ep = strchr(dp, '<')) != 0)
1306 rp->r_dycode = DC_DOWLEQ;
1307 else if ((ep = strchr(dp, '>')) != 0)
1308 rp->r_dycode = DC_DOWGEQ;
1311 rp->r_dycode = DC_DOM;
1313 if (rp->r_dycode != DC_DOM) {
1316 error(_("invalid day of month"));
1320 if ((lp = byword(dp, wday_names)) == NULL) {
1321 error(_("invalid weekday name"));
1325 rp->r_wday = lp->l_value;
1327 if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1328 rp->r_dayofmonth <= 0 ||
1329 (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1330 error(_("invalid day of month"));
1344 register long shift;
1346 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1347 buf[i] = val >> shift;
1358 (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1366 if (((struct attype *) avp)->at < ((struct attype *) bvp)->at)
1368 else if (((struct attype *) avp)->at > ((struct attype *) bvp)->at)
1375 const char * const name;
1379 static char * fullname;
1380 static struct tzhead tzh;
1381 time_t ats[TZ_MAX_TIMES];
1382 unsigned char types[TZ_MAX_TIMES];
1388 (void) qsort((void *) attypes, (size_t) timecnt,
1389 (size_t) sizeof *attypes, atcomp);
1399 while (fromi < timecnt && attypes[fromi].at < min_time)
1402 while (fromi < timecnt && attypes[fromi].type == 0)
1403 ++fromi; /* handled by default rule */
1404 for ( ; fromi < timecnt; ++fromi) {
1406 && ((attypes[fromi].at
1407 + gmtoffs[attypes[toi - 1].type])
1408 <= (attypes[toi - 1].at
1409 + gmtoffs[toi == 1 ? 0
1410 : attypes[toi - 2].type]))) {
1411 attypes[toi - 1].type = attypes[fromi].type;
1415 attypes[toi - 1].type != attypes[fromi].type)
1416 attypes[toi++] = attypes[fromi];
1423 for (i = 0; i < timecnt; ++i) {
1424 ats[i] = attypes[i].at;
1425 types[i] = attypes[i].type;
1427 fullname = erealloc(fullname,
1428 (int) (strlen(directory) + 1 + strlen(name) + 1));
1429 (void) sprintf(fullname, "%s/%s", directory, name);
1432 * Remove old file, if any, to snap links.
1434 if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT)
1435 err(EXIT_FAILURE, _("can't remove %s"), fullname);
1437 if ((fp = fopen(fullname, "wb")) == NULL) {
1438 if (mkdirs(fullname) != 0)
1439 (void) exit(EXIT_FAILURE);
1440 if ((fp = fopen(fullname, "wb")) == NULL)
1441 err(EXIT_FAILURE, _("can't create %s"), fullname);
1443 convert(eitol(typecnt), tzh.tzh_ttisgmtcnt);
1444 convert(eitol(typecnt), tzh.tzh_ttisstdcnt);
1445 convert(eitol(leapcnt), tzh.tzh_leapcnt);
1446 convert(eitol(timecnt), tzh.tzh_timecnt);
1447 convert(eitol(typecnt), tzh.tzh_typecnt);
1448 convert(eitol(charcnt), tzh.tzh_charcnt);
1449 (void) strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1450 #define DO(field) (void) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp)
1460 for (i = 0; i < timecnt; ++i) {
1463 if (ats[i] >= trans[j]) {
1464 ats[i] = tadd(ats[i], corr[j]);
1467 puttzcode((long) ats[i], fp);
1470 (void) fwrite((void *) types, (size_t) sizeof types[0],
1471 (size_t) timecnt, fp);
1472 for (i = 0; i < typecnt; ++i) {
1473 puttzcode((long) gmtoffs[i], fp);
1474 (void) putc(isdsts[i], fp);
1475 (void) putc(abbrinds[i], fp);
1478 (void) fwrite((void *) chars, (size_t) sizeof chars[0],
1479 (size_t) charcnt, fp);
1480 for (i = 0; i < leapcnt; ++i) {
1482 if (timecnt == 0 || trans[i] < ats[0]) {
1485 if (++j >= typecnt) {
1491 while (j < timecnt && trans[i] >= ats[j])
1495 puttzcode((long) tadd(trans[i], -gmtoffs[j]), fp);
1496 } else puttzcode((long) trans[i], fp);
1497 puttzcode((long) corr[i], fp);
1499 for (i = 0; i < typecnt; ++i)
1500 (void) putc(ttisstds[i], fp);
1501 for (i = 0; i < typecnt; ++i)
1502 (void) putc(ttisgmts[i], fp);
1503 if (ferror(fp) || fclose(fp))
1504 errx(EXIT_FAILURE, _("error writing %s"), fullname);
1505 if (chmod(fullname, mflag) < 0)
1506 err(EXIT_FAILURE, _("cannot change mode of %s to %03o"),
1507 fullname, (unsigned)mflag);
1508 if ((uflag != (uid_t)-1 || gflag != (gid_t)-1)
1509 && chown(fullname, uflag, gflag) < 0)
1510 err(EXIT_FAILURE, _("cannot change ownership of %s"),
1515 doabbr(abbr, format, letters, isdst)
1517 const char * const format;
1518 const char * const letters;
1521 if (strchr(format, '/') == NULL) {
1522 if (letters == NULL)
1523 (void) strcpy(abbr, format);
1524 else (void) sprintf(abbr, format, letters);
1526 (void) strcpy(abbr, strchr(format, '/') + 1);
1528 (void) strcpy(abbr, format);
1529 *strchr(abbr, '/') = '\0';
1534 outzone(zpfirst, zonecount)
1535 const struct zone * const zpfirst;
1536 const int zonecount;
1538 register const struct zone * zp;
1539 register struct rule * rp;
1541 register int usestart, useuntil;
1542 register time_t starttime, untiltime;
1543 register long gmtoff;
1544 register long stdoff;
1546 register long startoff;
1547 register int startttisstd;
1548 register int startttisgmt;
1550 char startbuf[BUFSIZ];
1552 INITIALIZE(untiltime);
1553 INITIALIZE(starttime);
1555 ** Now. . .finally. . .generate some useful data!
1561 ** A guess that may well be corrected later.
1565 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1566 ** for noting the need to unconditionally initialize startttisstd.
1568 startttisstd = FALSE;
1569 startttisgmt = FALSE;
1570 for (i = 0; i < zonecount; ++i) {
1572 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
1573 useuntil = i < (zonecount - 1);
1574 if (useuntil && zp->z_untiltime <= min_time)
1576 gmtoff = zp->z_gmtoff;
1577 eat(zp->z_filename, zp->z_linenum);
1579 startoff = zp->z_gmtoff;
1580 if (zp->z_nrules == 0) {
1581 stdoff = zp->z_stdoff;
1582 doabbr(startbuf, zp->z_format,
1583 (char *) NULL, stdoff != 0);
1584 type = addtype(oadd(zp->z_gmtoff, stdoff),
1585 startbuf, stdoff != 0, startttisstd,
1588 addtt(starttime, type);
1591 else if (stdoff != 0)
1592 addtt(min_time, type);
1593 } else for (year = min_year; year <= max_year; ++year) {
1594 if (useuntil && year > zp->z_untilrule.r_hiyear)
1597 ** Mark which rules to do in the current year.
1598 ** For those to do, calculate rpytime(rp, year);
1600 for (j = 0; j < zp->z_nrules; ++j) {
1601 rp = &zp->z_rules[j];
1602 eats(zp->z_filename, zp->z_linenum,
1603 rp->r_filename, rp->r_linenum);
1604 rp->r_todo = year >= rp->r_loyear &&
1605 year <= rp->r_hiyear &&
1606 yearistype(year, rp->r_yrtype);
1608 rp->r_temp = rpytime(rp, year);
1612 register time_t jtime, ktime;
1613 register long offset;
1619 ** Turn untiltime into UTC
1620 ** assuming the current gmtoff and
1623 untiltime = zp->z_untiltime;
1624 if (!zp->z_untilrule.r_todisgmt)
1625 untiltime = tadd(untiltime,
1627 if (!zp->z_untilrule.r_todisstd)
1628 untiltime = tadd(untiltime,
1632 ** Find the rule (of those to do, if any)
1633 ** that takes effect earliest in the year.
1636 for (j = 0; j < zp->z_nrules; ++j) {
1637 rp = &zp->z_rules[j];
1640 eats(zp->z_filename, zp->z_linenum,
1641 rp->r_filename, rp->r_linenum);
1642 offset = rp->r_todisgmt ? 0 : gmtoff;
1643 if (!rp->r_todisstd)
1644 offset = oadd(offset, stdoff);
1646 if (jtime == min_time ||
1649 jtime = tadd(jtime, -offset);
1650 if (k < 0 || jtime < ktime) {
1656 break; /* go on to next year */
1657 rp = &zp->z_rules[k];
1659 if (useuntil && ktime >= untiltime)
1661 stdoff = rp->r_stdoff;
1662 if (usestart && ktime == starttime)
1665 if (ktime < starttime) {
1666 startoff = oadd(zp->z_gmtoff,
1668 doabbr(startbuf, zp->z_format,
1673 if (*startbuf == '\0' &&
1674 startoff == oadd(zp->z_gmtoff,
1676 doabbr(startbuf, zp->z_format,
1681 eats(zp->z_filename, zp->z_linenum,
1682 rp->r_filename, rp->r_linenum);
1683 doabbr(buf, zp->z_format, rp->r_abbrvar,
1685 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
1686 type = addtype(offset, buf, rp->r_stdoff != 0,
1687 rp->r_todisstd, rp->r_todisgmt);
1692 if (*startbuf == '\0' &&
1693 zp->z_format != NULL &&
1694 strchr(zp->z_format, '%') == NULL &&
1695 strchr(zp->z_format, '/') == NULL)
1696 (void) strcpy(startbuf, zp->z_format);
1697 eat(zp->z_filename, zp->z_linenum);
1698 if (*startbuf == '\0')
1699 error(_("can't determine time zone abbreviation to use just after until time"));
1700 else addtt(starttime,
1701 addtype(startoff, startbuf,
1702 startoff != zp->z_gmtoff,
1707 ** Now we may get to set starttime for the next zone line.
1710 startttisstd = zp->z_untilrule.r_todisstd;
1711 startttisgmt = zp->z_untilrule.r_todisgmt;
1712 starttime = zp->z_untiltime;
1714 starttime = tadd(starttime, -stdoff);
1716 starttime = tadd(starttime, -gmtoff);
1719 writezone(zpfirst->z_name);
1723 addtt(starttime, type)
1724 const time_t starttime;
1727 if (starttime <= min_time ||
1728 (timecnt == 1 && attypes[0].at < min_time)) {
1729 gmtoffs[0] = gmtoffs[type];
1730 isdsts[0] = isdsts[type];
1731 ttisstds[0] = ttisstds[type];
1732 ttisgmts[0] = ttisgmts[type];
1733 if (abbrinds[type] != 0)
1734 (void) strcpy(chars, &chars[abbrinds[type]]);
1736 charcnt = strlen(chars) + 1;
1741 if (timecnt >= TZ_MAX_TIMES) {
1742 error(_("too many transitions?!"));
1743 (void) exit(EXIT_FAILURE);
1745 attypes[timecnt].at = starttime;
1746 attypes[timecnt].type = type;
1751 addtype(gmtoff, abbr, isdst, ttisstd, ttisgmt)
1753 const char * const abbr;
1760 if (isdst != TRUE && isdst != FALSE) {
1761 error(_("internal error - addtype called with bad isdst"));
1762 (void) exit(EXIT_FAILURE);
1764 if (ttisstd != TRUE && ttisstd != FALSE) {
1765 error(_("internal error - addtype called with bad ttisstd"));
1766 (void) exit(EXIT_FAILURE);
1768 if (ttisgmt != TRUE && ttisgmt != FALSE) {
1769 error(_("internal error - addtype called with bad ttisgmt"));
1770 (void) exit(EXIT_FAILURE);
1773 ** See if there's already an entry for this zone type.
1774 ** If so, just return its index.
1776 for (i = 0; i < typecnt; ++i) {
1777 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
1778 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
1779 ttisstd == ttisstds[i] &&
1780 ttisgmt == ttisgmts[i])
1784 ** There isn't one; add a new one, unless there are already too
1787 if (typecnt >= TZ_MAX_TYPES) {
1788 error(_("too many local time types"));
1789 (void) exit(EXIT_FAILURE);
1791 gmtoffs[i] = gmtoff;
1793 ttisstds[i] = ttisstd;
1794 ttisgmts[i] = ttisgmt;
1796 for (j = 0; j < charcnt; ++j)
1797 if (strcmp(&chars[j], abbr) == 0)
1807 leapadd(t, positive, rolling, count)
1815 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
1816 error(_("too many leap seconds"));
1817 (void) exit(EXIT_FAILURE);
1819 for (i = 0; i < leapcnt; ++i)
1820 if (t <= trans[i]) {
1821 if (t == trans[i]) {
1822 error(_("repeated leap second moment"));
1823 (void) exit(EXIT_FAILURE);
1828 for (j = leapcnt; j > i; --j) {
1829 trans[j] = trans[j - 1];
1830 corr[j] = corr[j - 1];
1831 roll[j] = roll[j - 1];
1834 corr[i] = positive ? 1L : eitol(-count);
1837 } while (positive && --count != 0);
1844 register long last = 0;
1847 ** propagate leap seconds forward
1849 for (i = 0; i < leapcnt; ++i) {
1850 trans[i] = tadd(trans[i], last);
1851 last = corr[i] += last;
1856 yearistype(year, type)
1858 const char * const type;
1863 if (type == NULL || *type == '\0')
1865 buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
1866 (void) sprintf(buf, "%s %d %s", yitcommand, year, type);
1867 result = system(buf);
1870 if (result == (1 << 8))
1872 error(_("wild result from command execution"));
1873 warnx(_("command was '%s', result was %d"), buf, result);
1875 (void) exit(EXIT_FAILURE);
1882 a = (unsigned char) a;
1883 return (isascii(a) && isupper(a)) ? tolower(a) : a;
1887 ciequal(ap, bp) /* case-insensitive equality */
1888 register const char * ap;
1889 register const char * bp;
1891 while (lowerit(*ap) == lowerit(*bp++))
1899 register const char * abbr;
1900 register const char * word;
1902 if (lowerit(*abbr) != lowerit(*word))
1905 while (*++abbr != '\0')
1909 } while (lowerit(*word++) != lowerit(*abbr));
1913 static const struct lookup *
1915 register const char * const word;
1916 register const struct lookup * const table;
1918 register const struct lookup * foundlp;
1919 register const struct lookup * lp;
1921 if (word == NULL || table == NULL)
1924 ** Look for exact match.
1926 for (lp = table; lp->l_word != NULL; ++lp)
1927 if (ciequal(word, lp->l_word))
1930 ** Look for inexact match.
1933 for (lp = table; lp->l_word != NULL; ++lp)
1934 if (itsabbr(word, lp->l_word)) {
1935 if (foundlp == NULL)
1937 else return NULL; /* multiple inexact matches */
1947 register char ** array;
1952 array = (char **) (void *)
1953 emalloc((int) ((strlen(cp) + 1) * sizeof *array));
1956 while (isascii(*cp) && isspace((unsigned char) *cp))
1958 if (*cp == '\0' || *cp == '#')
1960 array[nsubs++] = dp = cp;
1962 if ((*dp = *cp++) != '"')
1964 else while ((*dp = *cp++) != '"')
1967 else error(_("odd number of quotation marks"));
1968 } while (*cp != '\0' && *cp != '#' &&
1969 (!isascii(*cp) || !isspace((unsigned char) *cp)));
1970 if (isascii(*cp) && isspace((unsigned char) *cp))
1974 array[nsubs] = NULL;
1986 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
1987 error(_("time overflow"));
1988 (void) exit(EXIT_FAILURE);
2000 if (t1 == max_time && t2 > 0)
2002 if (t1 == min_time && t2 < 0)
2005 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2006 error(_("time overflow"));
2007 (void) exit(EXIT_FAILURE);
2013 ** Given a rule, and a year, compute the date - in seconds since January 1,
2014 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2018 rpytime(rp, wantedy)
2019 register const struct rule * const rp;
2020 register const int wantedy;
2022 register int y, m, i;
2023 register long dayoff; /* with a nod to Margaret O. */
2026 if (wantedy == INT_MIN)
2028 if (wantedy == INT_MAX)
2033 while (wantedy != y) {
2035 i = len_years[isleap(y)];
2039 i = -len_years[isleap(y)];
2041 dayoff = oadd(dayoff, eitol(i));
2043 while (m != rp->r_month) {
2044 i = len_months[isleap(y)][m];
2045 dayoff = oadd(dayoff, eitol(i));
2048 i = rp->r_dayofmonth;
2049 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
2050 if (rp->r_dycode == DC_DOWLEQ)
2053 error(_("use of 2/29 in non leap-year"));
2054 (void) exit(EXIT_FAILURE);
2058 dayoff = oadd(dayoff, eitol(i));
2059 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
2062 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2063 wday = eitol(EPOCH_WDAY);
2065 ** Don't trust mod of negative numbers.
2068 wday = (wday + dayoff) % LDAYSPERWEEK;
2070 wday -= ((-dayoff) % LDAYSPERWEEK);
2072 wday += LDAYSPERWEEK;
2074 while (wday != eitol(rp->r_wday))
2075 if (rp->r_dycode == DC_DOWGEQ) {
2076 dayoff = oadd(dayoff, (long) 1);
2077 if (++wday >= LDAYSPERWEEK)
2081 dayoff = oadd(dayoff, (long) -1);
2083 wday = LDAYSPERWEEK - 1;
2086 if (i < 0 || i >= len_months[isleap(y)][m]) {
2087 error(_("no day in month matches rule"));
2088 (void) exit(EXIT_FAILURE);
2091 if (dayoff < 0 && !TYPE_SIGNED(time_t))
2093 t = (time_t) dayoff * SECSPERDAY;
2095 ** Cheap overflow check.
2097 if (t / SECSPERDAY != dayoff)
2098 return (dayoff > 0) ? max_time : min_time;
2099 return tadd(t, rp->r_tod);
2104 const char * const string;
2108 i = strlen(string) + 1;
2109 if (charcnt + i > TZ_MAX_CHARS) {
2110 error(_("too many, or too long, time zone abbreviations"));
2111 (void) exit(EXIT_FAILURE);
2113 (void) strcpy(&chars[charcnt], string);
2114 charcnt += eitol(i);
2119 char * const argname;
2121 register char * name;
2124 if (argname == NULL || *argname == '\0' || Dflag)
2126 cp = name = ecpyalloc(argname);
2127 while ((cp = strchr(cp + 1, '/')) != 0) {
2131 ** DOS drive specifier?
2133 if (isalpha((unsigned char) name[0]) &&
2134 name[1] == ':' && name[2] == '\0') {
2138 #endif /* !defined unix */
2139 if (!itsdir(name)) {
2141 ** It doesn't seem to exist, so we try to create it.
2142 ** Creation may fail because of the directory being
2143 ** created by some other multiprocessor, so we get
2144 ** to do extra checking.
2146 if (mkdir(name, (S_IRUSR | S_IWUSR | S_IXUSR
2147 | S_IRGRP | S_IXGRP | S_IROTH
2149 && (errno != EEXIST || !itsdir(name))) {
2150 warn(_("can't create directory %s"), name);
2168 if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0))
2169 errx(EXIT_FAILURE, _("%d did not sign extend correctly"), i);
2177 setgroup(flag, name)
2183 if (*flag != (gid_t)-1)
2184 errx(EXIT_FAILURE, _("multiple -g flags specified"));
2186 gr = getgrnam(name);
2191 ul = strtoul(name, &ep, 10);
2192 if (ul == (unsigned long)(gid_t)ul && *ep == '\0') {
2196 errx(EXIT_FAILURE, _("group `%s' not found"), name);
2208 if (*flag != (gid_t)-1)
2209 errx(EXIT_FAILURE, _("multiple -u flags specified"));
2211 pw = getpwnam(name);
2216 ul = strtoul(name, &ep, 10);
2217 if (ul == (unsigned long)(gid_t)ul && *ep == '\0') {
2221 errx(EXIT_FAILURE, _("user `%s' not found"), name);
2227 ** UNIX was a registered trademark of UNIX System Laboratories in 1993.