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.6 2004/12/18 22:48:15 swildner 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(time_t starttime
, int type
);
87 static int addtype(long gmtoff
, const char *abbr
, int isdst
,
88 int ttisstd
, int ttisgmt
);
89 static void leapadd(time_t t
, int positive
, int rolling
, int count
);
90 static void adjleap(void);
91 static void associate(void);
92 static int ciequal(const char *ap
, const char *bp
);
93 static void convert(long val
, char *buf
);
94 static void dolink(const char *fromfile
, const char *tofile
);
95 static void doabbr(char *abbr
, const char *format
,
96 const char *letters
, int isdst
);
97 static void eat(const char *name
, int num
);
98 static void eats(const char *name
, int num
,
99 const char *rname
, int rnum
);
100 static long eitol(int i
);
101 static void error(const char *message
);
102 static char ** getfields(char *buf
);
103 static long gethms(const char *string
, const char *errstrng
,
105 static void infile(const char *filename
);
106 static void inleap(char **fields
, int nfields
);
107 static void inlink(char **fields
, int nfields
);
108 static void inrule(char **fields
, int nfields
);
109 static int inzcont(char **fields
, int nfields
);
110 static int inzone(char **fields
, int nfields
);
111 static int inzsub(char **fields
, int nfields
, int iscont
);
112 static int itsabbr(const char *abbr
, const char *word
);
113 static int itsdir(const char *name
);
114 static int lowerit(int c
);
115 static char * memcheck(char *tocheck
);
116 static int mkdirs(char *filename
);
117 static void newabbr(const char *abbr
);
118 static long oadd(long t1
, long t2
);
119 static void outzone(const struct zone
*zp
, int ntzones
);
120 static void puttzcode(long code
, FILE *fp
);
121 static int rcomp(const void *leftp
, const void *rightp
);
122 static time_t rpytime(const struct rule
*rp
, int wantedy
);
123 static void rulesub(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(void);
128 static void setgroup(gid_t
*flag
, const char *name
);
129 static void setuser(uid_t
*flag
, const char *name
);
130 static time_t tadd(time_t t1
, long t2
);
131 static void usage(void);
132 static void writezone(const char *name
);
133 static int yearistype(int year
, const char *type
);
135 #if !(HAVE_STRERROR - 0)
136 static char * strerror(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(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.
354 memcheck(char * const ptr
)
357 errx(EXIT_FAILURE
, _("memory exhausted"));
361 #define emalloc(size) memcheck(imalloc(size))
362 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
363 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
364 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
370 #if !(HAVE_STRERROR - 0)
374 extern char * sys_errlist
[];
377 return (errnum
> 0 && errnum
<= sys_nerr
) ?
378 sys_errlist
[errnum
] : _("Unknown system error");
380 #endif /* !(HAVE_STRERROR - 0) */
383 eats(const char * const name
, const int num
,
384 const char * const rname
, const int rnum
)
393 eat(const char * const name
, const int num
)
395 eats(name
, num
, (char *) NULL
, -1);
399 error(const char * const string
)
402 ** Match the format of "cc" to allow sh users to
403 ** zic ... 2>&1 | error -t "*" -v
406 fprintf(stderr
, _("\"%s\", line %d: %s"),
407 filename
, linenum
, string
);
408 if (rfilename
!= NULL
)
409 fprintf(stderr
, _(" (rule from \"%s\", line %d)"),
410 rfilename
, rlinenum
);
411 fprintf(stderr
, "\n");
416 warning(const char * const string
)
420 cp
= ecpyalloc(_("warning: "));
421 cp
= ecatalloc(cp
, string
);
430 fprintf(stderr
, "%s\n%s\n",
431 _("usage: zic [-s] [-v] [-l localtime] [-p posixrules] [-d directory]"),
432 _(" [-L leapseconds] [-y yearistype] [filename ... ]"));
436 static const char * psxrules
;
437 static const char * lcltime
;
438 static const char * directory
;
439 static const char * leapsec
;
440 static const char * yitcommand
;
441 static int sflag
= FALSE
;
443 static uid_t uflag
= (uid_t
)-1;
444 static gid_t gflag
= (gid_t
)-1;
445 static mode_t mflag
= (S_IRUSR
| S_IRGRP
| S_IROTH
449 main(int argc
, char *argv
[])
456 umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
457 #endif /* defined unix */
459 setlocale(LC_MESSAGES
, "");
461 bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
462 #endif /* defined TEXTDOMAINDIR */
463 textdomain(TZ_DOMAIN
);
464 #endif /* HAVE_GETTEXT - 0 */
465 while ((c
= getopt(argc
, argv
, "Dd:g:l:m:p:L:u:vsy:")) != -1)
473 if (directory
== NULL
)
477 _("more than one -d option specified"));
480 setgroup(&gflag
, optarg
);
487 _("more than one -l option specified"));
491 void *set
= setmode(optarg
);
496 if (psxrules
== NULL
)
500 _("more than one -p option specified"));
503 setuser(&uflag
, optarg
);
506 if (yitcommand
== NULL
)
510 _("more than one -y option specified"));
517 _("more than one -L option specified"));
526 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
527 usage(); /* usage message by request */
528 if (directory
== NULL
)
530 if (yitcommand
== NULL
)
531 yitcommand
= "yearistype";
535 if (optind
< argc
&& leapsec
!= NULL
) {
540 for (i
= optind
; i
< argc
; ++i
)
545 for (i
= 0; i
< nzones
; i
= j
) {
547 ** Find the next non-continuation zone entry.
549 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
551 outzone(&zones
[i
], j
- i
);
556 for (i
= 0; i
< nlinks
; ++i
)
557 dolink(links
[i
].l_from
, links
[i
].l_to
);
559 dolink(lcltime
, TZDEFAULT
);
560 if (psxrules
!= NULL
)
561 dolink(psxrules
, TZDEFRULES
);
562 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
566 dolink(const char * const fromfile
, const char * const tofile
)
571 if (fromfile
[0] == '/')
572 fromname
= ecpyalloc(fromfile
);
574 fromname
= ecpyalloc(directory
);
575 fromname
= ecatalloc(fromname
, "/");
576 fromname
= ecatalloc(fromname
, fromfile
);
578 if (tofile
[0] == '/')
579 toname
= ecpyalloc(tofile
);
581 toname
= ecpyalloc(directory
);
582 toname
= ecatalloc(toname
, "/");
583 toname
= ecatalloc(toname
, tofile
);
586 ** We get to be careful here since
587 ** there's a fair chance of root running us.
591 if (link(fromname
, toname
) != 0) {
594 if (mkdirs(toname
) != 0)
596 result
= link(fromname
, toname
);
597 #if (HAVE_SYMLINK - 0)
599 result
= symlink(fromname
, toname
);
601 warning(_("hard link failed, symbolic link used"));
605 err(EXIT_FAILURE
, _("can't link from %s to %s"),
614 #define INT_MAX ((int) (((unsigned)~0)>>1))
615 #endif /* !defined INT_MAX */
618 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
619 #endif /* !defined INT_MIN */
622 ** The tz file format currently allows at most 32-bit quantities.
623 ** This restriction should be removed before signed 32-bit values
624 ** wrap around in 2038, but unfortunately this will require a
625 ** change to the tz file format.
628 #define MAX_BITS_IN_FILE 32
629 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(time_t) < MAX_BITS_IN_FILE) ? TYPE_BIT(time_t) : MAX_BITS_IN_FILE)
634 if (TYPE_SIGNED(time_t)) {
635 min_time
= ~ (time_t) 0;
636 min_time
<<= TIME_T_BITS_IN_FILE
- 1;
637 max_time
= ~ (time_t) 0 - min_time
;
642 max_time
= 2 - sflag
;
643 max_time
<<= TIME_T_BITS_IN_FILE
- 1;
646 min_year
= TM_YEAR_BASE
+ gmtime(&min_time
)->tm_year
;
647 max_year
= TM_YEAR_BASE
+ gmtime(&max_time
)->tm_year
;
648 min_year_representable
= min_year
;
649 max_year_representable
= max_year
;
653 itsdir(const char * const name
)
658 myname
= ecpyalloc(name
);
659 myname
= ecatalloc(myname
, "/.");
660 accres
= access(myname
, F_OK
);
666 ** Associate sets of rules with zones.
670 ** Sort by rule name.
674 rcomp(const void *cp1
, const void *cp2
)
676 return strcmp(((const struct rule
*) cp1
)->r_name
,
677 ((const struct rule
*) cp2
)->r_name
);
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)
695 if (strcmp(rules
[i
].r_filename
,
696 rules
[i
+ 1].r_filename
) == 0)
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)
706 if (strcmp(rules
[i
].r_filename
,
707 rules
[j
].r_filename
) == 0)
709 if (strcmp(rules
[i
+ 1].r_filename
,
710 rules
[j
].r_filename
) == 0)
717 for (i
= 0; i
< nzones
; ++i
) {
722 for (base
= 0; base
< nrules
; base
= out
) {
724 for (out
= base
+ 1; out
< nrules
; ++out
)
725 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
727 for (i
= 0; i
< nzones
; ++i
) {
729 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
732 zp
->z_nrules
= out
- base
;
735 for (i
= 0; i
< nzones
; ++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"),
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"));
757 infile(const char *name
)
762 const struct lookup
*lp
;
768 if (strcmp(name
, "-") == 0) {
769 name
= _("standard input");
771 } else if ((fp
= fopen(name
, "r")) == NULL
)
772 err(EXIT_FAILURE
, _("can't open %s"), name
);
774 for (num
= 1; ; ++num
) {
776 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
778 cp
= strchr(buf
, '\n');
780 error(_("line too long"));
784 fields
= getfields(buf
);
786 while (fields
[nfields
] != NULL
) {
789 if (strcmp(fields
[nfields
], "-") == 0)
790 fields
[nfields
] = &nada
;
795 } else if (wantcont
) {
796 wantcont
= inzcont(fields
, nfields
);
798 lp
= byword(fields
[0], line_codes
);
800 error(_("input line of unknown type"));
801 else switch ((int) (lp
->l_value
)) {
803 inrule(fields
, nfields
);
807 wantcont
= inzone(fields
, nfields
);
810 inlink(fields
, nfields
);
816 _("leap line in non leap seconds file %s"), name
);
817 else inleap(fields
, nfields
);
820 default: /* "cannot happen" */
822 _("panic: invalid l_value %d"), lp
->l_value
);
825 ifree((char *) fields
);
828 errx(EXIT_FAILURE
, _("error reading %s"), filename
);
829 if (fp
!= stdin
&& fclose(fp
))
830 err(EXIT_FAILURE
, _("error closing %s"), filename
);
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.
844 gethms(const char *string
, const char * const errstring
, const int signable
)
846 int hh
, mm
, ss
, sign
;
848 if (string
== NULL
|| *string
== '\0')
852 else if (*string
== '-') {
856 if (sscanf(string
, scheck(string
, "%d"), &hh
) == 1)
858 else if (sscanf(string
, scheck(string
, "%d:%d"), &hh
, &mm
) == 2)
860 else if (sscanf(string
, scheck(string
, "%d:%d:%d"),
861 &hh
, &mm
, &ss
) != 3) {
865 if ((hh
< 0 || hh
>= HOURSPERDAY
||
866 mm
< 0 || mm
>= MINSPERHOUR
||
867 ss
< 0 || ss
> SECSPERMIN
) &&
868 !(hh
== HOURSPERDAY
&& mm
== 0 && ss
== 0)) {
873 (eitol(hh
* MINSPERHOUR
+ mm
) *
874 eitol(SECSPERMIN
) + eitol(ss
));
878 inrule(char ** const fields
, const int nfields
)
880 static struct rule r
;
882 if (nfields
!= RULE_FIELDS
) {
883 error(_("wrong number of fields on Rule line"));
886 if (*fields
[RF_NAME
] == '\0') {
887 error(_("nameless rule"));
890 r
.r_filename
= filename
;
891 r
.r_linenum
= linenum
;
892 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
893 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
894 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
895 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
896 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
897 rules
= (struct rule
*) (void *) erealloc((char *) rules
,
898 (int) ((nrules
+ 1) * sizeof *rules
));
903 inzone(char ** const fields
, const int nfields
)
908 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
909 error(_("wrong number of fields on Zone line"));
912 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
913 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFAULT
)));
915 _("\"Zone %s\" line and -l option are mutually exclusive"),
920 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
921 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFRULES
)));
923 _("\"Zone %s\" line and -p option are mutually exclusive"),
928 for (i
= 0; i
< nzones
; ++i
)
929 if (zones
[i
].z_name
!= NULL
&&
930 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
931 buf
= erealloc(buf
, (int) (132 +
932 strlen(fields
[ZF_NAME
]) +
933 strlen(zones
[i
].z_filename
)));
935 _("duplicate zone name %s (file \"%s\", line %d)"),
942 return inzsub(fields
, nfields
, FALSE
);
946 inzcont(char ** const fields
, const int nfields
)
948 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
949 error(_("wrong number of fields on Zone continuation line"));
952 return inzsub(fields
, nfields
, TRUE
);
956 inzsub(char ** const fields
, const int nfields
, const int iscont
)
959 static struct zone z
;
960 int i_gmtoff
, i_rule
, i_format
;
961 int i_untilyear
, i_untilmonth
;
962 int i_untilday
, i_untiltime
;
966 i_gmtoff
= ZFC_GMTOFF
;
968 i_format
= ZFC_FORMAT
;
969 i_untilyear
= ZFC_TILYEAR
;
970 i_untilmonth
= ZFC_TILMONTH
;
971 i_untilday
= ZFC_TILDAY
;
972 i_untiltime
= ZFC_TILTIME
;
975 i_gmtoff
= ZF_GMTOFF
;
977 i_format
= ZF_FORMAT
;
978 i_untilyear
= ZF_TILYEAR
;
979 i_untilmonth
= ZF_TILMONTH
;
980 i_untilday
= ZF_TILDAY
;
981 i_untiltime
= ZF_TILTIME
;
982 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
984 z
.z_filename
= filename
;
985 z
.z_linenum
= linenum
;
986 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid UTC offset"), TRUE
);
987 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
988 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
989 error(_("invalid abbreviation format"));
993 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
994 z
.z_format
= ecpyalloc(fields
[i_format
]);
995 hasuntil
= nfields
> i_untilyear
;
997 z
.z_untilrule
.r_filename
= filename
;
998 z
.z_untilrule
.r_linenum
= linenum
;
999 rulesub(&z
.z_untilrule
,
1000 fields
[i_untilyear
],
1003 (nfields
> i_untilmonth
) ?
1004 fields
[i_untilmonth
] : "Jan",
1005 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1006 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1007 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1008 z
.z_untilrule
.r_loyear
);
1009 if (iscont
&& nzones
> 0 &&
1010 z
.z_untiltime
> min_time
&&
1011 z
.z_untiltime
< max_time
&&
1012 zones
[nzones
- 1].z_untiltime
> min_time
&&
1013 zones
[nzones
- 1].z_untiltime
< max_time
&&
1014 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1015 error(_("Zone continuation line end time is not after end time of previous line"));
1019 zones
= (struct zone
*) (void *) erealloc((char *) zones
,
1020 (int) ((nzones
+ 1) * sizeof *zones
));
1021 zones
[nzones
++] = z
;
1023 ** If there was an UNTIL field on this line,
1024 ** there's more information about the zone on the next line.
1030 inleap(char ** const fields
, const int nfields
)
1033 const struct lookup
*lp
;
1035 int year
, month
, day
;
1039 if (nfields
!= LEAP_FIELDS
) {
1040 error(_("wrong number of fields on Leap line"));
1044 cp
= fields
[LP_YEAR
];
1045 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
1049 error(_("invalid leaping year"));
1055 i
= len_years
[isleap(j
)];
1059 i
= -len_years
[isleap(j
)];
1061 dayoff
= oadd(dayoff
, eitol(i
));
1063 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1064 error(_("invalid month name"));
1067 month
= lp
->l_value
;
1069 while (j
!= month
) {
1070 i
= len_months
[isleap(year
)][j
];
1071 dayoff
= oadd(dayoff
, eitol(i
));
1074 cp
= fields
[LP_DAY
];
1075 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1076 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1077 error(_("invalid day of month"));
1080 dayoff
= oadd(dayoff
, eitol(day
- 1));
1081 if (dayoff
< 0 && !TYPE_SIGNED(time_t)) {
1082 error(_("time before zero"));
1085 t
= (time_t) dayoff
* SECSPERDAY
;
1087 ** Cheap overflow check.
1089 if (t
/ SECSPERDAY
!= dayoff
) {
1090 error(_("time overflow"));
1093 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1094 cp
= fields
[LP_CORR
];
1099 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1102 } else if (strcmp(cp
, "--") == 0) {
1105 } else if (strcmp(cp
, "+") == 0) {
1108 } else if (strcmp(cp
, "++") == 0) {
1112 error(_("illegal CORRECTION field on Leap line"));
1115 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1116 error(_("illegal Rolling/Stationary field on Leap line"));
1119 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1124 inlink(char ** const fields
, const int nfields
)
1128 if (nfields
!= LINK_FIELDS
) {
1129 error(_("wrong number of fields on Link line"));
1132 if (*fields
[LF_FROM
] == '\0') {
1133 error(_("blank FROM field on Link line"));
1136 if (*fields
[LF_TO
] == '\0') {
1137 error(_("blank TO field on Link line"));
1140 l
.l_filename
= filename
;
1141 l
.l_linenum
= linenum
;
1142 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1143 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1144 links
= (struct link
*) (void *) erealloc((char *) links
,
1145 (int) ((nlinks
+ 1) * sizeof *links
));
1146 links
[nlinks
++] = l
;
1150 rulesub(struct rule
* const rp
,
1151 const char * const loyearp
,
1152 const char * const hiyearp
,
1153 const char * const typep
,
1154 const char * const monthp
,
1155 const char * const dayp
,
1156 const char * const timep
)
1158 const struct lookup
*lp
;
1163 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1164 error(_("invalid month name"));
1167 rp
->r_month
= lp
->l_value
;
1168 rp
->r_todisstd
= FALSE
;
1169 rp
->r_todisgmt
= FALSE
;
1170 dp
= ecpyalloc(timep
);
1172 ep
= dp
+ strlen(dp
) - 1;
1173 switch (lowerit(*ep
)) {
1174 case 's': /* Standard */
1175 rp
->r_todisstd
= TRUE
;
1176 rp
->r_todisgmt
= FALSE
;
1179 case 'w': /* Wall */
1180 rp
->r_todisstd
= FALSE
;
1181 rp
->r_todisgmt
= FALSE
;
1184 case 'g': /* Greenwich */
1185 case 'u': /* Universal */
1186 case 'z': /* Zulu */
1187 rp
->r_todisstd
= TRUE
;
1188 rp
->r_todisgmt
= TRUE
;
1193 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1199 lp
= byword(cp
, begin_years
);
1200 if (lp
!= NULL
) switch ((int) lp
->l_value
) {
1202 rp
->r_loyear
= INT_MIN
;
1205 rp
->r_loyear
= INT_MAX
;
1207 default: /* "cannot happen" */
1209 _("panic: invalid l_value %d"), lp
->l_value
);
1210 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1211 error(_("invalid starting year"));
1214 if (rp
->r_loyear
< min_year_representable
)
1215 warning(_("starting year too low to be represented"));
1216 else if (rp
->r_loyear
> max_year_representable
)
1217 warning(_("starting year too high to be represented"));
1220 if ((lp
= byword(cp
, end_years
)) != NULL
) switch ((int) lp
->l_value
) {
1222 rp
->r_hiyear
= INT_MIN
;
1225 rp
->r_hiyear
= INT_MAX
;
1228 rp
->r_hiyear
= rp
->r_loyear
;
1230 default: /* "cannot happen" */
1232 _("panic: invalid l_value %d"), lp
->l_value
);
1233 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1234 error(_("invalid ending year"));
1237 if (rp
->r_loyear
< min_year_representable
)
1238 warning(_("starting year too low to be represented"));
1239 else if (rp
->r_loyear
> max_year_representable
)
1240 warning(_("starting year too high to be represented"));
1242 if (rp
->r_loyear
> rp
->r_hiyear
) {
1243 error(_("starting year greater than ending year"));
1247 rp
->r_yrtype
= NULL
;
1249 if (rp
->r_loyear
== rp
->r_hiyear
) {
1250 error(_("typed single year"));
1253 rp
->r_yrtype
= ecpyalloc(typep
);
1255 if (rp
->r_loyear
< min_year
&& rp
->r_loyear
> 0)
1256 min_year
= rp
->r_loyear
;
1259 ** Accept things such as:
1265 dp
= ecpyalloc(dayp
);
1266 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1267 rp
->r_dycode
= DC_DOWLEQ
;
1268 rp
->r_wday
= lp
->l_value
;
1269 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1271 if ((ep
= strchr(dp
, '<')) != 0)
1272 rp
->r_dycode
= DC_DOWLEQ
;
1273 else if ((ep
= strchr(dp
, '>')) != 0)
1274 rp
->r_dycode
= DC_DOWGEQ
;
1277 rp
->r_dycode
= DC_DOM
;
1279 if (rp
->r_dycode
!= DC_DOM
) {
1282 error(_("invalid day of month"));
1286 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1287 error(_("invalid weekday name"));
1291 rp
->r_wday
= lp
->l_value
;
1293 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1294 rp
->r_dayofmonth
<= 0 ||
1295 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1296 error(_("invalid day of month"));
1305 convert(const long val
, char * const buf
)
1310 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1311 buf
[i
] = val
>> shift
;
1315 puttzcode(const long val
, FILE * const fp
)
1320 fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1324 atcomp(const void *avp
, const void *bvp
)
1326 const struct attype
*ap
= avp
;
1327 const struct attype
*bp
= bvp
;
1329 if (ap
->at
< bp
->at
)
1331 else if (ap
->at
> bp
->at
)
1337 writezone(const char * const name
)
1341 static char *fullname
;
1342 static struct tzhead tzh
;
1343 time_t ats
[TZ_MAX_TIMES
];
1344 unsigned char types
[TZ_MAX_TIMES
];
1350 qsort((void *) attypes
, (size_t) timecnt
,
1351 (size_t) sizeof *attypes
, atcomp
);
1361 while (fromi
< timecnt
&& attypes
[fromi
].at
< min_time
)
1364 while (fromi
< timecnt
&& attypes
[fromi
].type
== 0)
1365 ++fromi
; /* handled by default rule */
1366 for ( ; fromi
< timecnt
; ++fromi
) {
1368 && ((attypes
[fromi
].at
1369 + gmtoffs
[attypes
[toi
- 1].type
])
1370 <= (attypes
[toi
- 1].at
1371 + gmtoffs
[toi
== 1 ? 0
1372 : attypes
[toi
- 2].type
]))) {
1373 attypes
[toi
- 1].type
= attypes
[fromi
].type
;
1377 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1378 attypes
[toi
++] = attypes
[fromi
];
1385 for (i
= 0; i
< timecnt
; ++i
) {
1386 ats
[i
] = attypes
[i
].at
;
1387 types
[i
] = attypes
[i
].type
;
1389 fullname
= erealloc(fullname
,
1390 (int) (strlen(directory
) + 1 + strlen(name
) + 1));
1391 sprintf(fullname
, "%s/%s", directory
, name
);
1394 * Remove old file, if any, to snap links.
1396 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
)
1397 err(EXIT_FAILURE
, _("can't remove %s"), fullname
);
1399 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1400 if (mkdirs(fullname
) != 0)
1402 if ((fp
= fopen(fullname
, "wb")) == NULL
)
1403 err(EXIT_FAILURE
, _("can't create %s"), fullname
);
1405 convert(eitol(typecnt
), tzh
.tzh_ttisgmtcnt
);
1406 convert(eitol(typecnt
), tzh
.tzh_ttisstdcnt
);
1407 convert(eitol(leapcnt
), tzh
.tzh_leapcnt
);
1408 convert(eitol(timecnt
), tzh
.tzh_timecnt
);
1409 convert(eitol(typecnt
), tzh
.tzh_typecnt
);
1410 convert(eitol(charcnt
), tzh
.tzh_charcnt
);
1411 strncpy(tzh
.tzh_magic
, TZ_MAGIC
, sizeof tzh
.tzh_magic
);
1412 #define DO(field) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp)
1422 for (i
= 0; i
< timecnt
; ++i
) {
1425 if (ats
[i
] >= trans
[j
]) {
1426 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1429 puttzcode((long) ats
[i
], fp
);
1432 fwrite((void *) types
, (size_t) sizeof types
[0],
1433 (size_t) timecnt
, fp
);
1434 for (i
= 0; i
< typecnt
; ++i
) {
1435 puttzcode((long) gmtoffs
[i
], fp
);
1436 putc(isdsts
[i
], fp
);
1437 putc(abbrinds
[i
], fp
);
1440 fwrite((void *) chars
, (size_t) sizeof chars
[0],
1441 (size_t) charcnt
, fp
);
1442 for (i
= 0; i
< leapcnt
; ++i
) {
1444 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1447 if (++j
>= typecnt
) {
1453 while (j
< timecnt
&& trans
[i
] >= ats
[j
])
1457 puttzcode((long) tadd(trans
[i
], -gmtoffs
[j
]), fp
);
1458 } else puttzcode((long) trans
[i
], fp
);
1459 puttzcode((long) corr
[i
], fp
);
1461 for (i
= 0; i
< typecnt
; ++i
)
1462 putc(ttisstds
[i
], fp
);
1463 for (i
= 0; i
< typecnt
; ++i
)
1464 putc(ttisgmts
[i
], fp
);
1465 if (ferror(fp
) || fclose(fp
))
1466 errx(EXIT_FAILURE
, _("error writing %s"), fullname
);
1467 if (chmod(fullname
, mflag
) < 0)
1468 err(EXIT_FAILURE
, _("cannot change mode of %s to %03o"),
1469 fullname
, (unsigned)mflag
);
1470 if ((uflag
!= (uid_t
)-1 || gflag
!= (gid_t
)-1)
1471 && chown(fullname
, uflag
, gflag
) < 0)
1472 err(EXIT_FAILURE
, _("cannot change ownership of %s"),
1477 doabbr(char * const abbr
, const char * const format
,
1478 const char * const letters
, const int isdst
)
1480 if (strchr(format
, '/') == NULL
) {
1481 if (letters
== NULL
)
1482 strcpy(abbr
, format
);
1483 else sprintf(abbr
, format
, letters
);
1485 strcpy(abbr
, strchr(format
, '/') + 1);
1487 strcpy(abbr
, format
);
1488 *strchr(abbr
, '/') = '\0';
1493 outzone(const struct zone
* const zpfirst
, const int zonecount
)
1495 const struct zone
*zp
;
1498 int usestart
, useuntil
;
1499 time_t starttime
, untiltime
;
1507 char startbuf
[BUFSIZ
];
1509 INITIALIZE(untiltime
);
1510 INITIALIZE(starttime
);
1512 ** Now. . .finally. . .generate some useful data!
1518 ** A guess that may well be corrected later.
1522 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1523 ** for noting the need to unconditionally initialize startttisstd.
1525 startttisstd
= FALSE
;
1526 startttisgmt
= FALSE
;
1527 for (i
= 0; i
< zonecount
; ++i
) {
1529 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
1530 useuntil
= i
< (zonecount
- 1);
1531 if (useuntil
&& zp
->z_untiltime
<= min_time
)
1533 gmtoff
= zp
->z_gmtoff
;
1534 eat(zp
->z_filename
, zp
->z_linenum
);
1536 startoff
= zp
->z_gmtoff
;
1537 if (zp
->z_nrules
== 0) {
1538 stdoff
= zp
->z_stdoff
;
1539 doabbr(startbuf
, zp
->z_format
,
1540 (char *) NULL
, stdoff
!= 0);
1541 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
1542 startbuf
, stdoff
!= 0, startttisstd
,
1545 addtt(starttime
, type
);
1548 else if (stdoff
!= 0)
1549 addtt(min_time
, type
);
1550 } else for (year
= min_year
; year
<= max_year
; ++year
) {
1551 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
1554 ** Mark which rules to do in the current year.
1555 ** For those to do, calculate rpytime(rp, year);
1557 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1558 rp
= &zp
->z_rules
[j
];
1559 eats(zp
->z_filename
, zp
->z_linenum
,
1560 rp
->r_filename
, rp
->r_linenum
);
1561 rp
->r_todo
= year
>= rp
->r_loyear
&&
1562 year
<= rp
->r_hiyear
&&
1563 yearistype(year
, rp
->r_yrtype
);
1565 rp
->r_temp
= rpytime(rp
, year
);
1569 time_t jtime
, ktime
;
1576 ** Turn untiltime into UTC
1577 ** assuming the current gmtoff and
1580 untiltime
= zp
->z_untiltime
;
1581 if (!zp
->z_untilrule
.r_todisgmt
)
1582 untiltime
= tadd(untiltime
,
1584 if (!zp
->z_untilrule
.r_todisstd
)
1585 untiltime
= tadd(untiltime
,
1589 ** Find the rule (of those to do, if any)
1590 ** that takes effect earliest in the year.
1593 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1594 rp
= &zp
->z_rules
[j
];
1597 eats(zp
->z_filename
, zp
->z_linenum
,
1598 rp
->r_filename
, rp
->r_linenum
);
1599 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
1600 if (!rp
->r_todisstd
)
1601 offset
= oadd(offset
, stdoff
);
1603 if (jtime
== min_time
||
1606 jtime
= tadd(jtime
, -offset
);
1607 if (k
< 0 || jtime
< ktime
) {
1613 break; /* go on to next year */
1614 rp
= &zp
->z_rules
[k
];
1616 if (useuntil
&& ktime
>= untiltime
)
1618 stdoff
= rp
->r_stdoff
;
1619 if (usestart
&& ktime
== starttime
)
1622 if (ktime
< starttime
) {
1623 startoff
= oadd(zp
->z_gmtoff
,
1625 doabbr(startbuf
, zp
->z_format
,
1630 if (*startbuf
== '\0' &&
1631 startoff
== oadd(zp
->z_gmtoff
,
1633 doabbr(startbuf
, zp
->z_format
,
1638 eats(zp
->z_filename
, zp
->z_linenum
,
1639 rp
->r_filename
, rp
->r_linenum
);
1640 doabbr(buf
, zp
->z_format
, rp
->r_abbrvar
,
1642 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
1643 type
= addtype(offset
, buf
, rp
->r_stdoff
!= 0,
1644 rp
->r_todisstd
, rp
->r_todisgmt
);
1649 if (*startbuf
== '\0' &&
1650 zp
->z_format
!= NULL
&&
1651 strchr(zp
->z_format
, '%') == NULL
&&
1652 strchr(zp
->z_format
, '/') == NULL
)
1653 strcpy(startbuf
, zp
->z_format
);
1654 eat(zp
->z_filename
, zp
->z_linenum
);
1655 if (*startbuf
== '\0')
1656 error(_("can't determine time zone abbreviation to use just after until time"));
1657 else addtt(starttime
,
1658 addtype(startoff
, startbuf
,
1659 startoff
!= zp
->z_gmtoff
,
1664 ** Now we may get to set starttime for the next zone line.
1667 startttisstd
= zp
->z_untilrule
.r_todisstd
;
1668 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
1669 starttime
= zp
->z_untiltime
;
1671 starttime
= tadd(starttime
, -stdoff
);
1673 starttime
= tadd(starttime
, -gmtoff
);
1676 writezone(zpfirst
->z_name
);
1680 addtt(const time_t starttime
, int type
)
1682 if (starttime
<= min_time
||
1683 (timecnt
== 1 && attypes
[0].at
< min_time
)) {
1684 gmtoffs
[0] = gmtoffs
[type
];
1685 isdsts
[0] = isdsts
[type
];
1686 ttisstds
[0] = ttisstds
[type
];
1687 ttisgmts
[0] = ttisgmts
[type
];
1688 if (abbrinds
[type
] != 0)
1689 strcpy(chars
, &chars
[abbrinds
[type
]]);
1691 charcnt
= strlen(chars
) + 1;
1696 if (timecnt
>= TZ_MAX_TIMES
) {
1697 error(_("too many transitions?!"));
1700 attypes
[timecnt
].at
= starttime
;
1701 attypes
[timecnt
].type
= type
;
1706 addtype(const long gmtoff
, const char * const abbr
, const int isdst
,
1707 const int ttisstd
, const int ttisgmt
)
1711 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
1712 error(_("internal error - addtype called with bad isdst"));
1715 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
1716 error(_("internal error - addtype called with bad ttisstd"));
1719 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
1720 error(_("internal error - addtype called with bad ttisgmt"));
1724 ** See if there's already an entry for this zone type.
1725 ** If so, just return its index.
1727 for (i
= 0; i
< typecnt
; ++i
) {
1728 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
1729 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
1730 ttisstd
== ttisstds
[i
] &&
1731 ttisgmt
== ttisgmts
[i
])
1735 ** There isn't one; add a new one, unless there are already too
1738 if (typecnt
>= TZ_MAX_TYPES
) {
1739 error(_("too many local time types"));
1742 gmtoffs
[i
] = gmtoff
;
1744 ttisstds
[i
] = ttisstd
;
1745 ttisgmts
[i
] = ttisgmt
;
1747 for (j
= 0; j
< charcnt
; ++j
)
1748 if (strcmp(&chars
[j
], abbr
) == 0)
1758 leapadd(const time_t t
, const int positive
, const int rolling
, int count
)
1762 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
1763 error(_("too many leap seconds"));
1766 for (i
= 0; i
< leapcnt
; ++i
)
1767 if (t
<= trans
[i
]) {
1768 if (t
== trans
[i
]) {
1769 error(_("repeated leap second moment"));
1775 for (j
= leapcnt
; j
> i
; --j
) {
1776 trans
[j
] = trans
[j
- 1];
1777 corr
[j
] = corr
[j
- 1];
1778 roll
[j
] = roll
[j
- 1];
1781 corr
[i
] = positive
? 1L : eitol(-count
);
1784 } while (positive
&& --count
!= 0);
1794 ** propagate leap seconds forward
1796 for (i
= 0; i
< leapcnt
; ++i
) {
1797 trans
[i
] = tadd(trans
[i
], last
);
1798 last
= corr
[i
] += last
;
1803 yearistype(const int year
, const char * const type
)
1808 if (type
== NULL
|| *type
== '\0')
1810 buf
= erealloc(buf
, (int) (132 + strlen(yitcommand
) + strlen(type
)));
1811 sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
1812 result
= system(buf
);
1815 if (result
== (1 << 8))
1817 error(_("wild result from command execution"));
1818 warnx(_("command was '%s', result was %d"), buf
, result
);
1826 a
= (unsigned char) a
;
1827 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
1831 ciequal(const char *ap
, const char *bp
) /* case-insensitive equality */
1833 while (lowerit(*ap
) == lowerit(*bp
++))
1840 itsabbr(const char *abbr
, const char *word
)
1842 if (lowerit(*abbr
) != lowerit(*word
))
1845 while (*++abbr
!= '\0')
1849 } while (lowerit(*word
++) != lowerit(*abbr
));
1853 static const struct lookup
*
1854 byword(const char * const word
, const struct lookup
* const table
)
1856 const struct lookup
*foundlp
;
1857 const struct lookup
*lp
;
1859 if (word
== NULL
|| table
== NULL
)
1862 ** Look for exact match.
1864 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1865 if (ciequal(word
, lp
->l_word
))
1868 ** Look for inexact match.
1871 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1872 if (itsabbr(word
, lp
->l_word
)) {
1873 if (foundlp
== NULL
)
1875 else return NULL
; /* multiple inexact matches */
1889 array
= (char **) (void *)
1890 emalloc((int) ((strlen(cp
) + 1) * sizeof *array
));
1893 while (isascii(*cp
) && isspace((unsigned char) *cp
))
1895 if (*cp
== '\0' || *cp
== '#')
1897 array
[nsubs
++] = dp
= cp
;
1899 if ((*dp
= *cp
++) != '"')
1901 else while ((*dp
= *cp
++) != '"')
1904 else error(_("odd number of quotation marks"));
1905 } while (*cp
!= '\0' && *cp
!= '#' &&
1906 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
1907 if (isascii(*cp
) && isspace((unsigned char) *cp
))
1911 array
[nsubs
] = NULL
;
1916 oadd(const long t1
, const long t2
)
1921 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
1922 error(_("time overflow"));
1929 tadd(const time_t t1
, const long t2
)
1933 if (t1
== max_time
&& t2
> 0)
1935 if (t1
== min_time
&& t2
< 0)
1938 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
1939 error(_("time overflow"));
1946 ** Given a rule, and a year, compute the date - in seconds since January 1,
1947 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
1951 rpytime(const struct rule
* const rp
, const int wantedy
)
1954 long dayoff
; /* with a nod to Margaret O. */
1957 if (wantedy
== INT_MIN
)
1959 if (wantedy
== INT_MAX
)
1964 while (wantedy
!= y
) {
1966 i
= len_years
[isleap(y
)];
1970 i
= -len_years
[isleap(y
)];
1972 dayoff
= oadd(dayoff
, eitol(i
));
1974 while (m
!= rp
->r_month
) {
1975 i
= len_months
[isleap(y
)][m
];
1976 dayoff
= oadd(dayoff
, eitol(i
));
1979 i
= rp
->r_dayofmonth
;
1980 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
1981 if (rp
->r_dycode
== DC_DOWLEQ
)
1984 error(_("use of 2/29 in non leap-year"));
1989 dayoff
= oadd(dayoff
, eitol(i
));
1990 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
1993 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
1994 wday
= eitol(EPOCH_WDAY
);
1996 ** Don't trust mod of negative numbers.
1999 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2001 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2003 wday
+= LDAYSPERWEEK
;
2005 while (wday
!= eitol(rp
->r_wday
))
2006 if (rp
->r_dycode
== DC_DOWGEQ
) {
2007 dayoff
= oadd(dayoff
, (long) 1);
2008 if (++wday
>= LDAYSPERWEEK
)
2012 dayoff
= oadd(dayoff
, (long) -1);
2014 wday
= LDAYSPERWEEK
- 1;
2017 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2018 error(_("no day in month matches rule"));
2022 if (dayoff
< 0 && !TYPE_SIGNED(time_t))
2024 t
= (time_t) dayoff
* SECSPERDAY
;
2026 ** Cheap overflow check.
2028 if (t
/ SECSPERDAY
!= dayoff
)
2029 return (dayoff
> 0) ? max_time
: min_time
;
2030 return tadd(t
, rp
->r_tod
);
2034 newabbr(const char * const string
)
2038 i
= strlen(string
) + 1;
2039 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2040 error(_("too many, or too long, time zone abbreviations"));
2043 strcpy(&chars
[charcnt
], string
);
2044 charcnt
+= eitol(i
);
2048 mkdirs(char * const argname
)
2053 if (argname
== NULL
|| *argname
== '\0' || Dflag
)
2055 cp
= name
= ecpyalloc(argname
);
2056 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2060 ** DOS drive specifier?
2062 if (isalpha((unsigned char) name
[0]) &&
2063 name
[1] == ':' && name
[2] == '\0') {
2067 #endif /* !defined unix */
2068 if (!itsdir(name
)) {
2070 ** It doesn't seem to exist, so we try to create it.
2071 ** Creation may fail because of the directory being
2072 ** created by some other multiprocessor, so we get
2073 ** to do extra checking.
2075 if (mkdir(name
, (S_IRUSR
| S_IWUSR
| S_IXUSR
2076 | S_IRGRP
| S_IXGRP
| S_IROTH
2078 && (errno
!= EEXIST
|| !itsdir(name
))) {
2079 warn(_("can't create directory %s"), name
);
2096 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0))
2097 errx(EXIT_FAILURE
, _("%d did not sign extend correctly"), i
);
2105 setgroup(gid_t
*flag
, const char *name
)
2109 if (*flag
!= (gid_t
)-1)
2110 errx(EXIT_FAILURE
, _("multiple -g flags specified"));
2112 gr
= getgrnam(name
);
2117 ul
= strtoul(name
, &ep
, 10);
2118 if (ul
== (unsigned long)(gid_t
)ul
&& *ep
== '\0') {
2122 errx(EXIT_FAILURE
, _("group `%s' not found"), name
);
2128 setuser(uid_t
*flag
, const char *name
)
2132 if (*flag
!= (gid_t
)-1)
2133 errx(EXIT_FAILURE
, _("multiple -u flags specified"));
2135 pw
= getpwnam(name
);
2140 ul
= strtoul(name
, &ep
, 10);
2141 if (ul
== (unsigned long)(gid_t
)ul
&& *ep
== '\0') {
2145 errx(EXIT_FAILURE
, _("user `%s' not found"), name
);
2151 ** UNIX was a registered trademark of UNIX System Laboratories in 1993.