]>
git.saurik.com Git - apple/system_cmds.git/blob - zic.tproj/zic.c
1 static const char elsieid
[] = "@(#)zic.c 7.116";
5 __unused
static const char rcsid
[] =
6 "$FreeBSD: src/usr.sbin/zic/zic.c,v 1.18 2007/12/03 10:45:44 kevlo Exp $";
13 #include <sys/stat.h> /* for umask manifest constants */
14 #include <sys/types.h>
17 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
20 ** On some ancient hosts, predicates like `isspace(C)' are defined
21 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
22 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
23 ** Neither the C Standard nor POSIX require that `isascii' exist.
24 ** For portability, we check both ancient and modern requirements.
25 ** If isascii is not defined, the isascii check succeeds trivially.
33 const char * r_filename
;
37 int r_loyear
; /* for example, 1986 */
38 int r_hiyear
; /* for example, 1986 */
39 const char * r_yrtype
;
41 int r_month
; /* 0..11 */
43 int r_dycode
; /* see below */
47 long r_tod
; /* time from midnight */
48 int r_todisstd
; /* above is standard time if TRUE */
49 /* or wall clock time if FALSE */
50 int r_todisgmt
; /* above is GMT if TRUE */
51 /* or local time if FALSE */
52 long r_stdoff
; /* offset from standard time */
53 const char * r_abbrvar
; /* variable part of abbreviation */
55 int r_todo
; /* a rule to do (used in outzone) */
56 time_t r_temp
; /* used in outzone */
60 ** r_dycode r_dayofmonth r_wday
63 #define DC_DOM 0 /* 1..31 */ /* unused */
64 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
65 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
68 const char * z_filename
;
74 const char * z_format
;
78 struct rule
* z_rules
;
81 struct rule z_untilrule
;
85 static void addtt
P((time_t starttime
, int type
));
86 static int addtype
P((long gmtoff
, const char * abbr
, int isdst
,
87 int ttisstd
, int ttisgmt
));
88 static void leapadd
P((time_t t
, int positive
, int rolling
, int count
));
89 static void adjleap
P((void));
90 static void associate
P((void));
91 static int ciequal
P((const char * ap
, const char * bp
));
92 static void convert
P((long val
, char * buf
));
93 static void dolink
P((const char * fromfile
, const char * tofile
));
94 static void doabbr
P((char * abbr
, const char * format
,
95 const char * letters
, int isdst
));
96 static void eat
P((const char * name
, int num
));
97 static void eats
P((const char * name
, int num
,
98 const char * rname
, int rnum
));
99 static long eitol
P((int i
));
100 static void error
P((const char * message
));
101 static char ** getfields
P((char * buf
));
102 static long gethms
P((const char * string
, const char * errstrng
,
104 static void infile
P((const char * filename
));
105 static void inleap
P((char ** fields
, int nfields
));
106 static void inlink
P((char ** fields
, int nfields
));
107 static void inrule
P((char ** fields
, int nfields
));
108 static int inzcont
P((char ** fields
, int nfields
));
109 static int inzone
P((char ** fields
, int nfields
));
110 static int inzsub
P((char ** fields
, int nfields
, int iscont
));
111 static int itsabbr
P((const char * abbr
, const char * word
));
112 static int itsdir
P((const char * name
));
113 static int lowerit
P((int c
));
114 static char * memcheck
P((char * tocheck
));
115 static int mkdirs
P((char * filename
));
116 static void newabbr
P((const char * abbr
));
117 static long oadd
P((long t1
, long t2
));
118 static void outzone
P((const struct zone
* zp
, int ntzones
));
119 static void puttzcode
P((long code
, FILE * fp
));
120 static int rcomp
P((const void * leftp
, const void * rightp
));
121 static time_t rpytime
P((const struct rule
* rp
, int wantedy
));
122 static void rulesub
P((struct rule
* rp
,
123 const char * loyearp
, const char * hiyearp
,
124 const char * typep
, const char * monthp
,
125 const char * dayp
, const char * timep
));
126 static void setboundaries
P((void));
127 static void setgroup
P((gid_t
*flag
, const char *name
));
128 static void setuser
P((uid_t
*flag
, const char *name
));
129 static time_t tadd
P((time_t t1
, long t2
));
130 static void usage
P((void));
131 static void writezone
P((const char * name
));
132 static int yearistype
P((int year
, const char * type
));
134 #if !(HAVE_STRERROR - 0)
135 static char * strerror
P((int));
136 #endif /* !(HAVE_STRERROR - 0) */
140 static const char * filename
;
143 static time_t max_time
;
145 static int max_year_representable
;
146 static time_t min_time
;
148 static int min_year_representable
;
150 static const char * rfilename
;
165 ** Which fields are which on a Zone line.
173 #define ZF_TILMONTH 6
176 #define ZONE_MINFIELDS 5
177 #define ZONE_MAXFIELDS 9
180 ** Which fields are which on a Zone continuation line.
186 #define ZFC_TILYEAR 3
187 #define ZFC_TILMONTH 4
189 #define ZFC_TILTIME 6
190 #define ZONEC_MINFIELDS 3
191 #define ZONEC_MAXFIELDS 7
194 ** Which files are which on a Rule line.
206 #define RULE_FIELDS 10
209 ** Which fields are which on a Link line.
214 #define LINK_FIELDS 3
217 ** Which fields are which on a Leap line.
226 #define LEAP_FIELDS 7
236 static struct rule
* rules
;
237 static int nrules
; /* number of rules */
239 static struct zone
* zones
;
240 static int nzones
; /* number of zones */
243 const char * l_filename
;
249 static struct link
* links
;
257 static struct lookup
const * byword
P((const char * string
,
258 const struct lookup
* lp
));
260 static struct lookup
const line_codes
[] = {
268 static struct lookup
const mon_names
[] = {
269 { "January", TM_JANUARY
},
270 { "February", TM_FEBRUARY
},
271 { "March", TM_MARCH
},
272 { "April", TM_APRIL
},
276 { "August", TM_AUGUST
},
277 { "September", TM_SEPTEMBER
},
278 { "October", TM_OCTOBER
},
279 { "November", TM_NOVEMBER
},
280 { "December", TM_DECEMBER
},
284 static struct lookup
const wday_names
[] = {
285 { "Sunday", TM_SUNDAY
},
286 { "Monday", TM_MONDAY
},
287 { "Tuesday", TM_TUESDAY
},
288 { "Wednesday", TM_WEDNESDAY
},
289 { "Thursday", TM_THURSDAY
},
290 { "Friday", TM_FRIDAY
},
291 { "Saturday", TM_SATURDAY
},
295 static struct lookup
const lasts
[] = {
296 { "last-Sunday", TM_SUNDAY
},
297 { "last-Monday", TM_MONDAY
},
298 { "last-Tuesday", TM_TUESDAY
},
299 { "last-Wednesday", TM_WEDNESDAY
},
300 { "last-Thursday", TM_THURSDAY
},
301 { "last-Friday", TM_FRIDAY
},
302 { "last-Saturday", TM_SATURDAY
},
306 static struct lookup
const begin_years
[] = {
307 { "minimum", YR_MINIMUM
},
308 { "maximum", YR_MAXIMUM
},
312 static struct lookup
const end_years
[] = {
313 { "minimum", YR_MINIMUM
},
314 { "maximum", YR_MAXIMUM
},
319 static struct lookup
const leap_types
[] = {
321 { "Stationary", FALSE
},
325 static const int len_months
[2][MONSPERYEAR
] = {
326 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
327 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
330 static const int len_years
[2] = {
331 DAYSPERNYEAR
, DAYSPERLYEAR
334 static struct attype
{
337 } attypes
[TZ_MAX_TIMES
];
338 static long gmtoffs
[TZ_MAX_TYPES
];
339 static char isdsts
[TZ_MAX_TYPES
];
340 static unsigned char abbrinds
[TZ_MAX_TYPES
];
341 static char ttisstds
[TZ_MAX_TYPES
];
342 static char ttisgmts
[TZ_MAX_TYPES
];
343 static char chars
[TZ_MAX_CHARS
];
344 static time_t trans
[TZ_MAX_LEAPS
];
345 static long corr
[TZ_MAX_LEAPS
];
346 static char roll
[TZ_MAX_LEAPS
];
349 ** Memory allocation.
353 memcheck(char * const ptr
)
356 errx(EXIT_FAILURE
, _("memory exhausted"));
360 #define emalloc(size) memcheck(imalloc(size))
361 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
362 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
363 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
369 #if !(HAVE_STRERROR - 0)
373 extern char * sys_errlist
[];
376 return (errnum
> 0 && errnum
<= sys_nerr
) ?
377 sys_errlist
[errnum
] : _("Unknown system error");
379 #endif /* !(HAVE_STRERROR - 0) */
382 eats(const char * const name
, const int num
, const char * const rname
,
392 eat(const char * const name
, const int num
)
394 eats(name
, num
, (char *) NULL
, -1);
398 error(const char * const string
)
401 ** Match the format of "cc" to allow sh users to
402 ** zic ... 2>&1 | error -t "*" -v
405 (void) fprintf(stderr
, _("\"%s\", line %d: %s"),
406 filename
, linenum
, string
);
407 if (rfilename
!= NULL
)
408 (void) fprintf(stderr
, _(" (rule from \"%s\", line %d)"),
409 rfilename
, rlinenum
);
410 (void) fprintf(stderr
, "\n");
415 warning(const char * const string
)
419 cp
= ecpyalloc(_("warning: "));
420 cp
= ecatalloc(cp
, string
);
429 (void) fprintf(stderr
, "%s\n%s\n",
430 _("usage: zic [--version] [-s] [-v] [-l localtime] [-p posixrules] [-d directory]"),
431 _(" [-L leapseconds] [-y yearistype] [filename ... ]"));
432 (void) exit(EXIT_FAILURE
);
435 static const char * psxrules
;
436 static const char * lcltime
;
437 static const char * directory
;
438 static const char * leapsec
;
439 static const char * yitcommand
;
440 static int sflag
= FALSE
;
442 static uid_t uflag
= (uid_t
)-1;
443 static gid_t gflag
= (gid_t
)-1;
444 static mode_t mflag
= (S_IRUSR
| S_IRGRP
| S_IROTH
448 main(int argc
, char * argv
[])
455 (void) umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
456 #endif /* defined unix */
458 (void) setlocale(LC_MESSAGES
, "");
460 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
461 #endif /* defined TEXTDOMAINDIR */
462 (void) textdomain(TZ_DOMAIN
);
463 #endif /* HAVE_GETTEXT - 0 */
464 for (i
= 1; i
< argc
; ++i
)
465 if (strcmp(argv
[i
], "--version") == 0) {
466 errx(EXIT_SUCCESS
, "%s", elsieid
);
468 while ((c
= getopt(argc
, argv
, "Dd:g:l:m:p:L:u:vsy:")) != -1)
476 if (directory
== NULL
)
480 _("more than one -d option specified"));
483 setgroup(&gflag
, optarg
);
490 _("more than one -l option specified"));
494 void *set
= setmode(optarg
);
497 _("invalid file mode"));
498 mflag
= getmode(set
, mflag
);
503 if (psxrules
== NULL
)
507 _("more than one -p option specified"));
510 setuser(&uflag
, optarg
);
513 if (yitcommand
== NULL
)
517 _("more than one -y option specified"));
524 _("more than one -L option specified"));
533 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
534 usage(); /* usage message by request */
535 if (directory
== NULL
)
537 if (yitcommand
== NULL
)
538 yitcommand
= "yearistype";
542 if (optind
< argc
&& leapsec
!= NULL
) {
547 for (i
= optind
; i
< argc
; ++i
)
550 (void) exit(EXIT_FAILURE
);
552 for (i
= 0; i
< nzones
; i
= j
) {
554 ** Find the next non-continuation zone entry.
556 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
558 outzone(&zones
[i
], j
- i
);
563 for (i
= 0; i
< nlinks
; ++i
) {
564 eat(links
[i
].l_filename
, links
[i
].l_linenum
);
565 dolink(links
[i
].l_from
, links
[i
].l_to
);
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 fromfile
, const char * const tofile
)
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
);
610 result
= link(fromname
, toname
);
611 #if (HAVE_SYMLINK - 0)
613 access(fromname
, F_OK
) == 0 &&
615 const char *s
= tofile
;
616 char * symlinkcontents
= NULL
;
617 while ((s
= strchr(s
+1, '/')) != NULL
)
618 symlinkcontents
= ecatalloc(symlinkcontents
, "../");
619 symlinkcontents
= ecatalloc(symlinkcontents
, fromfile
);
621 result
= symlink(symlinkcontents
, toname
);
623 warning(_("hard link failed, symbolic link used"));
624 ifree(symlinkcontents
);
628 err(EXIT_FAILURE
, _("can't link from %s to %s"),
637 #define INT_MAX ((int) (((unsigned)~0)>>1))
638 #endif /* !defined INT_MAX */
641 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
642 #endif /* !defined INT_MIN */
645 ** The tz file format currently allows at most 32-bit quantities.
646 ** This restriction should be removed before signed 32-bit values
647 ** wrap around in 2038, but unfortunately this will require a
648 ** change to the tz file format.
651 #define MAX_BITS_IN_FILE 32
652 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(time_t) < MAX_BITS_IN_FILE) ? TYPE_BIT(time_t) : MAX_BITS_IN_FILE)
655 setboundaries
P((void))
657 if (TYPE_SIGNED(time_t)) {
658 min_time
= ~ (time_t) 0;
659 min_time
<<= TIME_T_BITS_IN_FILE
- 1;
660 max_time
= ~ (time_t) 0 - min_time
;
665 max_time
= 2 - sflag
;
666 max_time
<<= TIME_T_BITS_IN_FILE
- 1;
669 min_year
= TM_YEAR_BASE
+ gmtime(&min_time
)->tm_year
;
670 max_year
= TM_YEAR_BASE
+ gmtime(&max_time
)->tm_year
;
671 min_year_representable
= min_year
;
672 max_year_representable
= max_year
;
676 itsdir(const char * const name
)
681 myname
= ecpyalloc(name
);
682 myname
= ecatalloc(myname
, "/.");
683 accres
= access(myname
, F_OK
);
689 ** Associate sets of rules with zones.
693 ** Sort by rule name.
697 rcomp(const void *cp1
, const void *cp2
)
699 return strcmp(((const struct rule
*) cp1
)->r_name
,
700 ((const struct rule
*) cp2
)->r_name
);
712 (void) qsort((void *) rules
, (size_t) nrules
,
713 (size_t) sizeof *rules
, rcomp
);
714 for (i
= 0; i
< nrules
- 1; ++i
) {
715 if (strcmp(rules
[i
].r_name
,
716 rules
[i
+ 1].r_name
) != 0)
718 if (strcmp(rules
[i
].r_filename
,
719 rules
[i
+ 1].r_filename
) == 0)
721 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
722 warning(_("same rule name in multiple files"));
723 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
724 warning(_("same rule name in multiple files"));
725 for (j
= i
+ 2; j
< nrules
; ++j
) {
726 if (strcmp(rules
[i
].r_name
,
727 rules
[j
].r_name
) != 0)
729 if (strcmp(rules
[i
].r_filename
,
730 rules
[j
].r_filename
) == 0)
732 if (strcmp(rules
[i
+ 1].r_filename
,
733 rules
[j
].r_filename
) == 0)
740 for (i
= 0; i
< nzones
; ++i
) {
745 for (base
= 0; base
< nrules
; base
= out
) {
747 for (out
= base
+ 1; out
< nrules
; ++out
)
748 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
750 for (i
= 0; i
< nzones
; ++i
) {
752 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
755 zp
->z_nrules
= out
- base
;
758 for (i
= 0; i
< nzones
; ++i
) {
760 if (zp
->z_nrules
== 0) {
762 ** Maybe we have a local standard time offset.
764 eat(zp
->z_filename
, zp
->z_linenum
);
765 zp
->z_stdoff
= gethms(zp
->z_rule
, _("unruly zone"),
768 ** Note, though, that if there's no rule,
769 ** a '%s' in the format is a bad thing.
771 if (strchr(zp
->z_format
, '%') != 0)
772 error(_("%s in ruleless zone"));
776 (void) exit(EXIT_FAILURE
);
780 infile(const char * name
)
785 const struct lookup
* lp
;
791 if (strcmp(name
, "-") == 0) {
792 name
= _("standard input");
794 } else if ((fp
= fopen(name
, "r")) == NULL
)
795 err(EXIT_FAILURE
, _("can't open %s"), name
);
797 for (num
= 1; ; ++num
) {
799 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
801 cp
= strchr(buf
, '\n');
803 error(_("line too long"));
804 (void) exit(EXIT_FAILURE
);
807 fields
= getfields(buf
);
809 while (fields
[nfields
] != NULL
) {
812 if (strcmp(fields
[nfields
], "-") == 0)
813 fields
[nfields
] = &nada
;
818 } else if (wantcont
) {
819 wantcont
= inzcont(fields
, nfields
);
821 lp
= byword(fields
[0], line_codes
);
823 error(_("input line of unknown type"));
824 else switch ((int) (lp
->l_value
)) {
826 inrule(fields
, nfields
);
830 wantcont
= inzone(fields
, nfields
);
833 inlink(fields
, nfields
);
839 _("leap line in non leap seconds file %s"), name
);
840 else inleap(fields
, nfields
);
843 default: /* "cannot happen" */
845 _("panic: invalid l_value %d"), lp
->l_value
);
848 ifree((char *) fields
);
851 errx(EXIT_FAILURE
, _("error reading %s"), filename
);
852 if (fp
!= stdin
&& fclose(fp
))
853 err(EXIT_FAILURE
, _("error closing %s"), filename
);
855 error(_("expected continuation line not found"));
859 ** Convert a string of one of the forms
860 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
861 ** into a number of seconds.
862 ** A null string maps to zero.
863 ** Call error with errstring and return zero on errors.
867 gethms(const char *string
, const char * const errstring
, const int signable
)
869 int hh
, mm
, ss
, sign
;
871 if (string
== NULL
|| *string
== '\0')
875 else if (*string
== '-') {
879 if (sscanf(string
, scheck(string
, "%d"), &hh
) == 1)
881 else if (sscanf(string
, scheck(string
, "%d:%d"), &hh
, &mm
) == 2)
883 else if (sscanf(string
, scheck(string
, "%d:%d:%d"),
884 &hh
, &mm
, &ss
) != 3) {
888 if ((hh
< 0 || hh
>= HOURSPERDAY
||
889 mm
< 0 || mm
>= MINSPERHOUR
||
890 ss
< 0 || ss
> SECSPERMIN
) &&
891 !(hh
== HOURSPERDAY
&& mm
== 0 && ss
== 0)) {
895 if (noise
&& hh
== HOURSPERDAY
)
896 warning(_("24:00 not handled by pre-1998 versions of zic"));
898 (eitol(hh
* MINSPERHOUR
+ mm
) *
899 eitol(SECSPERMIN
) + eitol(ss
));
903 inrule(char ** const fields
, const int nfields
)
905 static struct rule r
;
907 if (nfields
!= RULE_FIELDS
) {
908 error(_("wrong number of fields on Rule line"));
911 if (*fields
[RF_NAME
] == '\0') {
912 error(_("nameless rule"));
915 r
.r_filename
= filename
;
916 r
.r_linenum
= linenum
;
917 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
918 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
919 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
920 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
921 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
922 rules
= (struct rule
*) (void *) erealloc((char *) rules
,
923 (int) ((nrules
+ 1) * sizeof *rules
));
928 inzone(char ** const fields
, const int nfields
)
933 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
934 error(_("wrong number of fields on Zone line"));
937 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
938 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFAULT
)));
940 _("\"Zone %s\" line and -l option are mutually exclusive"),
945 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
946 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFRULES
)));
948 _("\"Zone %s\" line and -p option are mutually exclusive"),
953 for (i
= 0; i
< nzones
; ++i
)
954 if (zones
[i
].z_name
!= NULL
&&
955 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
956 buf
= erealloc(buf
, (int) (132 +
957 strlen(fields
[ZF_NAME
]) +
958 strlen(zones
[i
].z_filename
)));
960 _("duplicate zone name %s (file \"%s\", line %d)"),
967 return inzsub(fields
, nfields
, FALSE
);
971 inzcont(char ** const fields
, const int nfields
)
973 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
974 error(_("wrong number of fields on Zone continuation line"));
977 return inzsub(fields
, nfields
, TRUE
);
981 inzsub(char ** const fields
, const int nfields
, const int iscont
)
984 static struct zone z
;
985 int i_gmtoff
, i_rule
, i_format
;
986 int i_untilyear
, i_untilmonth
;
987 int i_untilday
, i_untiltime
;
991 i_gmtoff
= ZFC_GMTOFF
;
993 i_format
= ZFC_FORMAT
;
994 i_untilyear
= ZFC_TILYEAR
;
995 i_untilmonth
= ZFC_TILMONTH
;
996 i_untilday
= ZFC_TILDAY
;
997 i_untiltime
= ZFC_TILTIME
;
1000 i_gmtoff
= ZF_GMTOFF
;
1002 i_format
= ZF_FORMAT
;
1003 i_untilyear
= ZF_TILYEAR
;
1004 i_untilmonth
= ZF_TILMONTH
;
1005 i_untilday
= ZF_TILDAY
;
1006 i_untiltime
= ZF_TILTIME
;
1007 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1009 z
.z_filename
= filename
;
1010 z
.z_linenum
= linenum
;
1011 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid UTC offset"), TRUE
);
1012 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1013 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1014 error(_("invalid abbreviation format"));
1018 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1019 z
.z_format
= ecpyalloc(fields
[i_format
]);
1020 hasuntil
= nfields
> i_untilyear
;
1022 z
.z_untilrule
.r_filename
= filename
;
1023 z
.z_untilrule
.r_linenum
= linenum
;
1024 rulesub(&z
.z_untilrule
,
1025 fields
[i_untilyear
],
1028 (nfields
> i_untilmonth
) ?
1029 fields
[i_untilmonth
] : "Jan",
1030 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1031 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1032 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1033 z
.z_untilrule
.r_loyear
);
1034 if (iscont
&& nzones
> 0 &&
1035 z
.z_untiltime
> min_time
&&
1036 z
.z_untiltime
< max_time
&&
1037 zones
[nzones
- 1].z_untiltime
> min_time
&&
1038 zones
[nzones
- 1].z_untiltime
< max_time
&&
1039 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1040 error(_("Zone continuation line end time is not after end time of previous line"));
1044 zones
= (struct zone
*) (void *) erealloc((char *) zones
,
1045 (int) ((nzones
+ 1) * sizeof *zones
));
1046 zones
[nzones
++] = z
;
1048 ** If there was an UNTIL field on this line,
1049 ** there's more information about the zone on the next line.
1055 inleap(char ** const fields
, const int nfields
)
1058 const struct lookup
* lp
;
1060 int year
, month
, day
;
1064 if (nfields
!= LEAP_FIELDS
) {
1065 error(_("wrong number of fields on Leap line"));
1069 cp
= fields
[LP_YEAR
];
1070 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
1074 error(_("invalid leaping year"));
1080 i
= len_years
[isleap(j
)];
1084 i
= -len_years
[isleap(j
)];
1086 dayoff
= oadd(dayoff
, eitol(i
));
1088 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1089 error(_("invalid month name"));
1092 month
= lp
->l_value
;
1094 while (j
!= month
) {
1095 i
= len_months
[isleap(year
)][j
];
1096 dayoff
= oadd(dayoff
, eitol(i
));
1099 cp
= fields
[LP_DAY
];
1100 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1101 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1102 error(_("invalid day of month"));
1105 dayoff
= oadd(dayoff
, eitol(day
- 1));
1106 if (dayoff
< 0 && !TYPE_SIGNED(time_t)) {
1107 error(_("time before zero"));
1110 if (dayoff
< min_time
/ SECSPERDAY
) {
1111 error(_("time too small"));
1114 if (dayoff
> max_time
/ SECSPERDAY
) {
1115 error(_("time too large"));
1118 t
= (time_t) dayoff
* SECSPERDAY
;
1119 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1120 cp
= fields
[LP_CORR
];
1125 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1128 } else if (strcmp(cp
, "--") == 0) {
1131 } else if (strcmp(cp
, "+") == 0) {
1134 } else if (strcmp(cp
, "++") == 0) {
1138 error(_("illegal CORRECTION field on Leap line"));
1141 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1142 error(_("illegal Rolling/Stationary field on Leap line"));
1145 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1150 inlink(char ** const fields
, const int nfields
)
1154 if (nfields
!= LINK_FIELDS
) {
1155 error(_("wrong number of fields on Link line"));
1158 if (*fields
[LF_FROM
] == '\0') {
1159 error(_("blank FROM field on Link line"));
1162 if (*fields
[LF_TO
] == '\0') {
1163 error(_("blank TO field on Link line"));
1166 l
.l_filename
= filename
;
1167 l
.l_linenum
= linenum
;
1168 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1169 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1170 links
= (struct link
*) (void *) erealloc((char *) links
,
1171 (int) ((nlinks
+ 1) * sizeof *links
));
1172 links
[nlinks
++] = l
;
1176 rulesub(struct rule
* const rp
, const char * const loyearp
,
1177 const char * const hiyearp
, const char * const typep
,
1178 const char * const monthp
, const char * const dayp
,
1179 const char * const timep
)
1181 const struct lookup
* lp
;
1186 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1187 error(_("invalid month name"));
1190 rp
->r_month
= lp
->l_value
;
1191 rp
->r_todisstd
= FALSE
;
1192 rp
->r_todisgmt
= FALSE
;
1193 dp
= ecpyalloc(timep
);
1195 ep
= dp
+ strlen(dp
) - 1;
1196 switch (lowerit(*ep
)) {
1197 case 's': /* Standard */
1198 rp
->r_todisstd
= TRUE
;
1199 rp
->r_todisgmt
= FALSE
;
1202 case 'w': /* Wall */
1203 rp
->r_todisstd
= FALSE
;
1204 rp
->r_todisgmt
= FALSE
;
1207 case 'g': /* Greenwich */
1208 case 'u': /* Universal */
1209 case 'z': /* Zulu */
1210 rp
->r_todisstd
= TRUE
;
1211 rp
->r_todisgmt
= TRUE
;
1216 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1222 lp
= byword(cp
, begin_years
);
1223 if (lp
!= NULL
) switch ((int) lp
->l_value
) {
1225 rp
->r_loyear
= INT_MIN
;
1228 rp
->r_loyear
= INT_MAX
;
1230 default: /* "cannot happen" */
1232 _("panic: invalid l_value %d"), lp
->l_value
);
1233 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1234 error(_("invalid starting year"));
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"));
1243 if ((lp
= byword(cp
, end_years
)) != NULL
) switch ((int) lp
->l_value
) {
1245 rp
->r_hiyear
= INT_MIN
;
1248 rp
->r_hiyear
= INT_MAX
;
1251 rp
->r_hiyear
= rp
->r_loyear
;
1253 default: /* "cannot happen" */
1255 _("panic: invalid l_value %d"), lp
->l_value
);
1256 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1257 error(_("invalid ending year"));
1260 if (rp
->r_loyear
< min_year_representable
)
1261 warning(_("ending year too low to be represented"));
1262 else if (rp
->r_loyear
> max_year_representable
)
1263 warning(_("ending year too high to be represented"));
1265 if (rp
->r_loyear
> rp
->r_hiyear
) {
1266 error(_("starting year greater than ending year"));
1270 rp
->r_yrtype
= NULL
;
1272 if (rp
->r_loyear
== rp
->r_hiyear
) {
1273 error(_("typed single year"));
1276 rp
->r_yrtype
= ecpyalloc(typep
);
1278 if (rp
->r_loyear
< min_year
&& rp
->r_loyear
> 0)
1279 min_year
= rp
->r_loyear
;
1282 ** Accept things such as:
1288 dp
= ecpyalloc(dayp
);
1289 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1290 rp
->r_dycode
= DC_DOWLEQ
;
1291 rp
->r_wday
= lp
->l_value
;
1292 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1294 if ((ep
= strchr(dp
, '<')) != 0)
1295 rp
->r_dycode
= DC_DOWLEQ
;
1296 else if ((ep
= strchr(dp
, '>')) != 0)
1297 rp
->r_dycode
= DC_DOWGEQ
;
1300 rp
->r_dycode
= DC_DOM
;
1302 if (rp
->r_dycode
!= DC_DOM
) {
1305 error(_("invalid day of month"));
1309 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1310 error(_("invalid weekday name"));
1314 rp
->r_wday
= lp
->l_value
;
1316 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1317 rp
->r_dayofmonth
<= 0 ||
1318 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1319 error(_("invalid day of month"));
1328 convert(const long val
, char * const buf
)
1333 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1334 buf
[i
] = val
>> shift
;
1338 puttzcode(const long val
, FILE * const fp
)
1343 (void) fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1347 atcomp(const void *avp
, const void *bvp
)
1349 if (((const struct attype
*) avp
)->at
< ((const struct attype
*) bvp
)->at
)
1351 else if (((const struct attype
*) avp
)->at
> ((const struct attype
*) bvp
)->at
)
1357 writezone(const char * const name
)
1361 static char * fullname
;
1362 static struct tzhead tzh
;
1363 time_t ats
[TZ_MAX_TIMES
];
1364 unsigned char types
[TZ_MAX_TIMES
];
1370 (void) qsort((void *) attypes
, (size_t) timecnt
,
1371 (size_t) sizeof *attypes
, atcomp
);
1381 while (fromi
< timecnt
&& attypes
[fromi
].at
< min_time
)
1384 while (fromi
< timecnt
&& attypes
[fromi
].type
== 0)
1385 ++fromi
; /* handled by default rule */
1386 for ( ; fromi
< timecnt
; ++fromi
) {
1388 && ((attypes
[fromi
].at
1389 + gmtoffs
[attypes
[toi
- 1].type
])
1390 <= (attypes
[toi
- 1].at
1391 + gmtoffs
[toi
== 1 ? 0
1392 : attypes
[toi
- 2].type
]))) {
1393 attypes
[toi
- 1].type
= attypes
[fromi
].type
;
1397 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1398 attypes
[toi
++] = attypes
[fromi
];
1405 for (i
= 0; i
< timecnt
; ++i
) {
1406 ats
[i
] = attypes
[i
].at
;
1407 types
[i
] = attypes
[i
].type
;
1409 fullname
= erealloc(fullname
,
1410 (int) (strlen(directory
) + 1 + strlen(name
) + 1));
1411 (void) sprintf(fullname
, "%s/%s", directory
, name
);
1414 * Remove old file, if any, to snap links.
1416 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
)
1417 err(EXIT_FAILURE
, _("can't remove %s"), fullname
);
1419 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1420 if (mkdirs(fullname
) != 0)
1421 (void) exit(EXIT_FAILURE
);
1422 if ((fp
= fopen(fullname
, "wb")) == NULL
)
1423 err(EXIT_FAILURE
, _("can't create %s"), fullname
);
1425 convert(eitol(typecnt
), tzh
.tzh_ttisgmtcnt
);
1426 convert(eitol(typecnt
), tzh
.tzh_ttisstdcnt
);
1427 convert(eitol(leapcnt
), tzh
.tzh_leapcnt
);
1428 convert(eitol(timecnt
), tzh
.tzh_timecnt
);
1429 convert(eitol(typecnt
), tzh
.tzh_typecnt
);
1430 convert(eitol(charcnt
), tzh
.tzh_charcnt
);
1431 (void) strncpy(tzh
.tzh_magic
, TZ_MAGIC
, sizeof tzh
.tzh_magic
);
1432 #define DO(field) (void) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp)
1442 for (i
= 0; i
< timecnt
; ++i
) {
1445 if (ats
[i
] >= trans
[j
]) {
1446 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1449 puttzcode((long) ats
[i
], fp
);
1452 (void) fwrite((void *) types
, (size_t) sizeof types
[0],
1453 (size_t) timecnt
, fp
);
1454 for (i
= 0; i
< typecnt
; ++i
) {
1455 puttzcode((long) gmtoffs
[i
], fp
);
1456 (void) putc(isdsts
[i
], fp
);
1457 (void) putc(abbrinds
[i
], fp
);
1460 (void) fwrite((void *) chars
, (size_t) sizeof chars
[0],
1461 (size_t) charcnt
, fp
);
1462 for (i
= 0; i
< leapcnt
; ++i
) {
1464 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1467 if (++j
>= typecnt
) {
1473 while (j
< timecnt
&& trans
[i
] >= ats
[j
])
1477 puttzcode((long) tadd(trans
[i
], -gmtoffs
[j
]), fp
);
1478 } else puttzcode((long) trans
[i
], fp
);
1479 puttzcode((long) corr
[i
], fp
);
1481 for (i
= 0; i
< typecnt
; ++i
)
1482 (void) putc(ttisstds
[i
], fp
);
1483 for (i
= 0; i
< typecnt
; ++i
)
1484 (void) putc(ttisgmts
[i
], fp
);
1485 if (ferror(fp
) || fclose(fp
))
1486 errx(EXIT_FAILURE
, _("error writing %s"), fullname
);
1487 if (chmod(fullname
, mflag
) < 0)
1488 err(EXIT_FAILURE
, _("cannot change mode of %s to %03o"),
1489 fullname
, (unsigned)mflag
);
1490 if ((uflag
!= (uid_t
)-1 || gflag
!= (gid_t
)-1)
1491 && chown(fullname
, uflag
, gflag
) < 0)
1492 err(EXIT_FAILURE
, _("cannot change ownership of %s"),
1497 doabbr(char * const abbr
, const char * const format
, const char * const letters
,
1500 if (strchr(format
, '/') == NULL
) {
1501 if (letters
== NULL
)
1502 (void) strcpy(abbr
, format
);
1503 else (void) sprintf(abbr
, format
, letters
);
1505 (void) strcpy(abbr
, strchr(format
, '/') + 1);
1507 (void) strcpy(abbr
, format
);
1508 *strchr(abbr
, '/') = '\0';
1513 outzone(const struct zone
* const zpfirst
, const int zonecount
)
1515 const struct zone
* zp
;
1518 int usestart
, useuntil
;
1519 time_t starttime
, untiltime
;
1527 char startbuf
[BUFSIZ
];
1529 INITIALIZE(untiltime
);
1530 INITIALIZE(starttime
);
1532 ** Now. . .finally. . .generate some useful data!
1538 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1539 ** for noting the need to unconditionally initialize startttisstd.
1541 startttisstd
= FALSE
;
1542 startttisgmt
= FALSE
;
1543 for (i
= 0; i
< zonecount
; ++i
) {
1545 ** A guess that may well be corrected later.
1549 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
1550 useuntil
= i
< (zonecount
- 1);
1551 if (useuntil
&& zp
->z_untiltime
<= min_time
)
1553 gmtoff
= zp
->z_gmtoff
;
1554 eat(zp
->z_filename
, zp
->z_linenum
);
1556 startoff
= zp
->z_gmtoff
;
1557 if (zp
->z_nrules
== 0) {
1558 stdoff
= zp
->z_stdoff
;
1559 doabbr(startbuf
, zp
->z_format
,
1560 (char *) NULL
, stdoff
!= 0);
1561 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
1562 startbuf
, stdoff
!= 0, startttisstd
,
1565 addtt(starttime
, type
);
1567 } else if (stdoff
!= 0)
1568 addtt(min_time
, type
);
1569 } else for (year
= min_year
; year
<= max_year
; ++year
) {
1570 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
1573 ** Mark which rules to do in the current year.
1574 ** For those to do, calculate rpytime(rp, year);
1576 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1577 rp
= &zp
->z_rules
[j
];
1578 eats(zp
->z_filename
, zp
->z_linenum
,
1579 rp
->r_filename
, rp
->r_linenum
);
1580 rp
->r_todo
= year
>= rp
->r_loyear
&&
1581 year
<= rp
->r_hiyear
&&
1582 yearistype(year
, rp
->r_yrtype
);
1584 rp
->r_temp
= rpytime(rp
, year
);
1588 time_t jtime
, ktime
;
1595 ** Turn untiltime into UTC
1596 ** assuming the current gmtoff and
1599 untiltime
= zp
->z_untiltime
;
1600 if (!zp
->z_untilrule
.r_todisgmt
)
1601 untiltime
= tadd(untiltime
,
1603 if (!zp
->z_untilrule
.r_todisstd
)
1604 untiltime
= tadd(untiltime
,
1608 ** Find the rule (of those to do, if any)
1609 ** that takes effect earliest in the year.
1612 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1613 rp
= &zp
->z_rules
[j
];
1616 eats(zp
->z_filename
, zp
->z_linenum
,
1617 rp
->r_filename
, rp
->r_linenum
);
1618 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
1619 if (!rp
->r_todisstd
)
1620 offset
= oadd(offset
, stdoff
);
1622 if (jtime
== min_time
||
1625 jtime
= tadd(jtime
, -offset
);
1626 if (k
< 0 || jtime
< ktime
) {
1632 break; /* go on to next year */
1633 rp
= &zp
->z_rules
[k
];
1635 if (useuntil
&& ktime
>= untiltime
)
1637 stdoff
= rp
->r_stdoff
;
1638 if (usestart
&& ktime
== starttime
)
1641 if (ktime
< starttime
) {
1642 startoff
= oadd(zp
->z_gmtoff
,
1644 doabbr(startbuf
, zp
->z_format
,
1649 if (*startbuf
== '\0' &&
1650 startoff
== oadd(zp
->z_gmtoff
,
1652 doabbr(startbuf
, zp
->z_format
,
1657 eats(zp
->z_filename
, zp
->z_linenum
,
1658 rp
->r_filename
, rp
->r_linenum
);
1659 doabbr(buf
, zp
->z_format
, rp
->r_abbrvar
,
1661 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
1662 type
= addtype(offset
, buf
, rp
->r_stdoff
!= 0,
1663 rp
->r_todisstd
, rp
->r_todisgmt
);
1668 if (*startbuf
== '\0' &&
1669 zp
->z_format
!= NULL
&&
1670 strchr(zp
->z_format
, '%') == NULL
&&
1671 strchr(zp
->z_format
, '/') == NULL
)
1672 (void) strcpy(startbuf
, zp
->z_format
);
1673 eat(zp
->z_filename
, zp
->z_linenum
);
1674 if (*startbuf
== '\0')
1675 error(_("can't determine time zone abbreviation to use just after until time"));
1676 else addtt(starttime
,
1677 addtype(startoff
, startbuf
,
1678 startoff
!= zp
->z_gmtoff
,
1683 ** Now we may get to set starttime for the next zone line.
1686 startttisstd
= zp
->z_untilrule
.r_todisstd
;
1687 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
1688 starttime
= zp
->z_untiltime
;
1690 starttime
= tadd(starttime
, -stdoff
);
1692 starttime
= tadd(starttime
, -gmtoff
);
1695 writezone(zpfirst
->z_name
);
1699 addtt(const time_t starttime
, int type
)
1701 if (starttime
<= min_time
||
1702 (timecnt
== 1 && attypes
[0].at
< min_time
)) {
1703 gmtoffs
[0] = gmtoffs
[type
];
1704 isdsts
[0] = isdsts
[type
];
1705 ttisstds
[0] = ttisstds
[type
];
1706 ttisgmts
[0] = ttisgmts
[type
];
1707 if (abbrinds
[type
] != 0)
1708 (void) strcpy(chars
, &chars
[abbrinds
[type
]]);
1710 charcnt
= (int)strlen(chars
) + 1;
1715 if (timecnt
>= TZ_MAX_TIMES
) {
1716 error(_("too many transitions?!"));
1717 (void) exit(EXIT_FAILURE
);
1719 attypes
[timecnt
].at
= starttime
;
1720 attypes
[timecnt
].type
= type
;
1725 addtype(const long gmtoff
, const char * const abbr
, const int isdst
,
1726 const int ttisstd
, const int ttisgmt
)
1730 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
1731 error(_("internal error - addtype called with bad isdst"));
1732 (void) exit(EXIT_FAILURE
);
1734 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
1735 error(_("internal error - addtype called with bad ttisstd"));
1736 (void) exit(EXIT_FAILURE
);
1738 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
1739 error(_("internal error - addtype called with bad ttisgmt"));
1740 (void) exit(EXIT_FAILURE
);
1743 ** See if there's already an entry for this zone type.
1744 ** If so, just return its index.
1746 for (i
= 0; i
< typecnt
; ++i
) {
1747 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
1748 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
1749 ttisstd
== ttisstds
[i
] &&
1750 ttisgmt
== ttisgmts
[i
])
1754 ** There isn't one; add a new one, unless there are already too
1757 if (typecnt
>= TZ_MAX_TYPES
) {
1758 error(_("too many local time types"));
1759 (void) exit(EXIT_FAILURE
);
1761 gmtoffs
[i
] = gmtoff
;
1763 ttisstds
[i
] = ttisstd
;
1764 ttisgmts
[i
] = ttisgmt
;
1766 for (j
= 0; j
< charcnt
; ++j
)
1767 if (strcmp(&chars
[j
], abbr
) == 0)
1777 leapadd(const time_t t
, const int positive
, const int rolling
, int count
)
1781 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
1782 error(_("too many leap seconds"));
1783 (void) exit(EXIT_FAILURE
);
1785 for (i
= 0; i
< leapcnt
; ++i
)
1786 if (t
<= trans
[i
]) {
1787 if (t
== trans
[i
]) {
1788 error(_("repeated leap second moment"));
1789 (void) exit(EXIT_FAILURE
);
1794 for (j
= leapcnt
; j
> i
; --j
) {
1795 trans
[j
] = trans
[j
- 1];
1796 corr
[j
] = corr
[j
- 1];
1797 roll
[j
] = roll
[j
- 1];
1800 corr
[i
] = positive
? 1L : eitol(-count
);
1803 } while (positive
&& --count
!= 0);
1813 ** propagate leap seconds forward
1815 for (i
= 0; i
< leapcnt
; ++i
) {
1816 trans
[i
] = tadd(trans
[i
], last
);
1817 last
= corr
[i
] += last
;
1822 yearistype(const int year
, const char * const type
)
1827 if (type
== NULL
|| *type
== '\0')
1829 buf
= erealloc(buf
, (int) (132 + strlen(yitcommand
) + strlen(type
)));
1830 (void) sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
1831 result
= system(buf
);
1832 if (WIFEXITED(result
)) switch (WEXITSTATUS(result
)) {
1838 error(_("wild result from command execution"));
1839 warnx(_("command was '%s', result was %d"), buf
, result
);
1841 (void) exit(EXIT_FAILURE
);
1847 a
= (unsigned char) a
;
1848 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
1851 /* case-insensitive equality */
1853 ciequal(const char *ap
, const char *bp
)
1855 while (lowerit(*ap
) == lowerit(*bp
++))
1862 itsabbr(const char *abbr
, const char *word
)
1864 if (lowerit(*abbr
) != lowerit(*word
))
1867 while (*++abbr
!= '\0')
1871 } while (lowerit(*word
++) != lowerit(*abbr
));
1875 static const struct lookup
*
1876 byword(const char * const word
, const struct lookup
* const table
)
1878 const struct lookup
* foundlp
;
1879 const struct lookup
* lp
;
1881 if (word
== NULL
|| table
== NULL
)
1884 ** Look for exact match.
1886 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1887 if (ciequal(word
, lp
->l_word
))
1890 ** Look for inexact match.
1893 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1894 if (itsabbr(word
, lp
->l_word
)) {
1895 if (foundlp
== NULL
)
1897 else return NULL
; /* multiple inexact matches */
1911 array
= (char **) (void *)
1912 emalloc((int) ((strlen(cp
) + 1) * sizeof *array
));
1915 while (isascii(*cp
) && isspace((unsigned char) *cp
))
1917 if (*cp
== '\0' || *cp
== '#')
1919 array
[nsubs
++] = dp
= cp
;
1921 if ((*dp
= *cp
++) != '"')
1923 else while ((*dp
= *cp
++) != '"')
1927 error(_("odd number of quotation marks"));
1930 } while (*cp
!= '\0' && *cp
!= '#' &&
1931 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
1932 if (isascii(*cp
) && isspace((unsigned char) *cp
))
1936 array
[nsubs
] = NULL
;
1941 oadd(const long t1
, const long t2
)
1946 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
1947 error(_("time overflow"));
1948 (void) exit(EXIT_FAILURE
);
1954 tadd(const time_t t1
, const long t2
)
1958 if (t1
== max_time
&& t2
> 0)
1960 if (t1
== min_time
&& t2
< 0)
1963 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
1964 error(_("time overflow"));
1965 (void) exit(EXIT_FAILURE
);
1971 ** Given a rule, and a year, compute the date - in seconds since January 1,
1972 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
1976 rpytime(const struct rule
* const rp
, const int wantedy
)
1979 long dayoff
; /* with a nod to Margaret O. */
1982 if (wantedy
== INT_MIN
)
1984 if (wantedy
== INT_MAX
)
1989 while (wantedy
!= y
) {
1991 i
= len_years
[isleap(y
)];
1995 i
= -len_years
[isleap(y
)];
1997 dayoff
= oadd(dayoff
, eitol(i
));
1999 while (m
!= rp
->r_month
) {
2000 i
= len_months
[isleap(y
)][m
];
2001 dayoff
= oadd(dayoff
, eitol(i
));
2004 i
= rp
->r_dayofmonth
;
2005 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2006 if (rp
->r_dycode
== DC_DOWLEQ
)
2009 error(_("use of 2/29 in non leap-year"));
2010 (void) exit(EXIT_FAILURE
);
2014 dayoff
= oadd(dayoff
, eitol(i
));
2015 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2018 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2019 wday
= eitol(EPOCH_WDAY
);
2021 ** Don't trust mod of negative numbers.
2024 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2026 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2028 wday
+= LDAYSPERWEEK
;
2030 while (wday
!= eitol(rp
->r_wday
))
2031 if (rp
->r_dycode
== DC_DOWGEQ
) {
2032 dayoff
= oadd(dayoff
, (long) 1);
2033 if (++wday
>= LDAYSPERWEEK
)
2037 dayoff
= oadd(dayoff
, (long) -1);
2039 wday
= LDAYSPERWEEK
- 1;
2042 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2044 warning(_("rule goes past start/end of month--will not work with pre-2004 versions of zic"));
2047 if (dayoff
< 0 && !TYPE_SIGNED(time_t))
2049 if (dayoff
< min_time
/ SECSPERDAY
)
2051 if (dayoff
> max_time
/ SECSPERDAY
)
2053 t
= (time_t) dayoff
* SECSPERDAY
;
2054 return tadd(t
, rp
->r_tod
);
2058 newabbr(const char * const string
)
2062 i
= (int)strlen(string
) + 1;
2063 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2064 error(_("too many, or too long, time zone abbreviations"));
2065 (void) exit(EXIT_FAILURE
);
2067 (void) strcpy(&chars
[charcnt
], string
);
2068 charcnt
+= eitol(i
);
2072 mkdirs(char * const argname
)
2077 if (argname
== NULL
|| *argname
== '\0' || Dflag
)
2079 cp
= name
= ecpyalloc(argname
);
2080 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2084 ** DOS drive specifier?
2086 if (isalpha((unsigned char) name
[0]) &&
2087 name
[1] == ':' && name
[2] == '\0') {
2091 #endif /* !defined unix */
2092 if (!itsdir(name
)) {
2094 ** It doesn't seem to exist, so we try to create it.
2095 ** Creation may fail because of the directory being
2096 ** created by some other multiprocessor, so we get
2097 ** to do extra checking.
2099 if (mkdir(name
, MKDIR_UMASK
) != 0
2100 && (errno
!= EEXIST
|| !itsdir(name
))) {
2101 warn(_("can't create directory %s"), name
);
2118 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0))
2119 errx(EXIT_FAILURE
, _("%d did not sign extend correctly"), i
);
2127 setgroup(gid_t
*flag
, const char *name
)
2131 if (*flag
!= (gid_t
)-1)
2132 errx(EXIT_FAILURE
, _("multiple -g flags specified"));
2134 gr
= getgrnam(name
);
2139 ul
= strtoul(name
, &ep
, 10);
2140 if (ul
== (unsigned long)(gid_t
)ul
&& *ep
== '\0') {
2144 errx(EXIT_FAILURE
, _("group `%s' not found"), name
);
2150 setuser(uid_t
*flag
, const char *name
)
2154 if (*flag
!= (gid_t
)-1)
2155 errx(EXIT_FAILURE
, _("multiple -u flags specified"));
2157 pw
= getpwnam(name
);
2162 ul
= strtoul(name
, &ep
, 10);
2163 if (ul
== (unsigned long)(gid_t
)ul
&& *ep
== '\0') {
2167 errx(EXIT_FAILURE
, _("user `%s' not found"), name
);
2173 ** UNIX was a registered trademark of The Open Group in 2003.