zic.c revision 1.46.2.1 1 1.46.2.1 martin /* $NetBSD: zic.c,v 1.46.2.1 2015/01/25 09:11:03 martin Exp $ */
2 1.25 mlelstv /*
3 1.25 mlelstv ** This file is in the public domain, so clarified as of
4 1.25 mlelstv ** 2006-07-17 by Arthur David Olson.
5 1.25 mlelstv */
6 1.2 jtc
7 1.26 tsutsui #if HAVE_NBTOOL_CONFIG_H
8 1.26 tsutsui #include "nbtool_config.h"
9 1.26 tsutsui #endif
10 1.26 tsutsui
11 1.9 christos #include <sys/cdefs.h>
12 1.1 jtc #ifndef lint
13 1.46.2.1 martin __RCSID("$NetBSD: zic.c,v 1.46.2.1 2015/01/25 09:11:03 martin Exp $");
14 1.1 jtc #endif /* !defined lint */
15 1.1 jtc
16 1.1 jtc #include "private.h"
17 1.5 jtc #include "locale.h"
18 1.1 jtc #include "tzfile.h"
19 1.19 kleink
20 1.43 christos #include <stdarg.h>
21 1.43 christos #include <unistd.h>
22 1.43 christos
23 1.43 christos #define ZIC_VERSION_PRE_2013 '2'
24 1.43 christos #define ZIC_VERSION '3'
25 1.25 mlelstv
26 1.41 christos typedef int_fast64_t zic_t;
27 1.41 christos #define ZIC_MIN INT_FAST64_MIN
28 1.41 christos #define ZIC_MAX INT_FAST64_MAX
29 1.41 christos #define SCNdZIC SCNdFAST64
30 1.25 mlelstv
31 1.25 mlelstv #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
32 1.25 mlelstv #define ZIC_MAX_ABBR_LEN_WO_WARN 6
33 1.25 mlelstv #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
34 1.25 mlelstv
35 1.19 kleink #if HAVE_SYS_STAT_H
36 1.46.2.1 martin #include <sys/stat.h>
37 1.19 kleink #endif
38 1.19 kleink #ifdef S_IRUSR
39 1.19 kleink #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
40 1.19 kleink #else
41 1.19 kleink #define MKDIR_UMASK 0755
42 1.19 kleink #endif
43 1.19 kleink
44 1.25 mlelstv #define end(cp) (strchr((cp), '\0'))
45 1.25 mlelstv
46 1.1 jtc struct rule {
47 1.1 jtc const char * r_filename;
48 1.1 jtc int r_linenum;
49 1.1 jtc const char * r_name;
50 1.1 jtc
51 1.41 christos zic_t r_loyear; /* for example, 1986 */
52 1.41 christos zic_t r_hiyear; /* for example, 1986 */
53 1.1 jtc const char * r_yrtype;
54 1.46.2.1 martin bool r_lowasnum;
55 1.46.2.1 martin bool r_hiwasnum;
56 1.1 jtc
57 1.1 jtc int r_month; /* 0..11 */
58 1.1 jtc
59 1.1 jtc int r_dycode; /* see below */
60 1.1 jtc int r_dayofmonth;
61 1.1 jtc int r_wday;
62 1.1 jtc
63 1.38 christos zic_t r_tod; /* time from midnight */
64 1.46.2.1 martin bool r_todisstd; /* above is standard time if 1 */
65 1.46.2.1 martin /* or wall clock time if 0 */
66 1.46.2.1 martin bool r_todisgmt; /* above is GMT if 1 */
67 1.46.2.1 martin /* or local time if 0 */
68 1.38 christos zic_t r_stdoff; /* offset from standard time */
69 1.1 jtc const char * r_abbrvar; /* variable part of abbreviation */
70 1.1 jtc
71 1.1 jtc int r_todo; /* a rule to do (used in outzone) */
72 1.25 mlelstv zic_t r_temp; /* used in outzone */
73 1.1 jtc };
74 1.1 jtc
75 1.1 jtc /*
76 1.1 jtc ** r_dycode r_dayofmonth r_wday
77 1.1 jtc */
78 1.1 jtc
79 1.1 jtc #define DC_DOM 0 /* 1..31 */ /* unused */
80 1.1 jtc #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
81 1.1 jtc #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
82 1.1 jtc
83 1.1 jtc struct zone {
84 1.1 jtc const char * z_filename;
85 1.1 jtc int z_linenum;
86 1.1 jtc
87 1.1 jtc const char * z_name;
88 1.38 christos zic_t z_gmtoff;
89 1.1 jtc const char * z_rule;
90 1.1 jtc const char * z_format;
91 1.1 jtc
92 1.38 christos zic_t z_stdoff;
93 1.1 jtc
94 1.1 jtc struct rule * z_rules;
95 1.1 jtc int z_nrules;
96 1.1 jtc
97 1.1 jtc struct rule z_untilrule;
98 1.25 mlelstv zic_t z_untiltime;
99 1.1 jtc };
100 1.1 jtc
101 1.25 mlelstv extern int getopt(int argc, char * const argv[],
102 1.25 mlelstv const char * options);
103 1.25 mlelstv extern int link(const char * fromname, const char * toname);
104 1.1 jtc extern char * optarg;
105 1.1 jtc extern int optind;
106 1.1 jtc
107 1.44 christos #if ! HAVE_LINK
108 1.44 christos # define link(from, to) (-1)
109 1.44 christos #endif
110 1.44 christos #if ! HAVE_SYMLINK
111 1.44 christos # define symlink(from, to) (-1)
112 1.44 christos #endif
113 1.44 christos
114 1.25 mlelstv static void addtt(zic_t starttime, int type);
115 1.46.2.1 martin static int addtype(zic_t, char * const, bool, bool, bool);
116 1.46.2.1 martin static void leapadd(zic_t, bool, int, int);
117 1.25 mlelstv static void adjleap(void);
118 1.25 mlelstv static void associate(void);
119 1.25 mlelstv static void dolink(const char * fromfield, const char * tofield);
120 1.25 mlelstv static char ** getfields(char * buf);
121 1.46.2.1 martin static zic_t gethms(const char * string, const char * errstring,
122 1.46.2.1 martin bool);
123 1.25 mlelstv static void infile(const char * filename);
124 1.25 mlelstv static void inleap(char ** fields, int nfields);
125 1.25 mlelstv static void inlink(char ** fields, int nfields);
126 1.25 mlelstv static void inrule(char ** fields, int nfields);
127 1.46.2.1 martin static bool inzcont(char ** fields, int nfields);
128 1.46.2.1 martin static bool inzone(char ** fields, int nfields);
129 1.46.2.1 martin static bool inzsub(char ** fields, int nfields, int iscont);
130 1.25 mlelstv static int itsdir(const char * name);
131 1.46.2.1 martin static bool is_alpha(char a);
132 1.46.2.1 martin static char lowerit(char);
133 1.46.2.1 martin static bool mkdirs(char * filename);
134 1.25 mlelstv static void newabbr(const char * abbr);
135 1.38 christos static zic_t oadd(zic_t t1, zic_t t2);
136 1.25 mlelstv static void outzone(const struct zone * zp, int ntzones);
137 1.25 mlelstv static int rcomp(const void * leftp, const void * rightp);
138 1.41 christos static zic_t rpytime(const struct rule * rp, zic_t wantedy);
139 1.25 mlelstv static void rulesub(struct rule * rp,
140 1.1 jtc const char * loyearp, const char * hiyearp,
141 1.1 jtc const char * typep, const char * monthp,
142 1.25 mlelstv const char * dayp, const char * timep);
143 1.38 christos static zic_t tadd(zic_t t1, zic_t t2);
144 1.46.2.1 martin static bool yearistype(int year, const char * type);
145 1.25 mlelstv static int atcomp(const void *avp, const void *bvp);
146 1.41 christos static void updateminmax(zic_t x);
147 1.5 jtc
148 1.1 jtc static int charcnt;
149 1.46.2.1 martin static bool errors;
150 1.46.2.1 martin static bool warnings;
151 1.1 jtc static const char * filename;
152 1.1 jtc static int leapcnt;
153 1.46.2.1 martin static bool leapseen;
154 1.41 christos static zic_t leapminyear;
155 1.41 christos static zic_t leapmaxyear;
156 1.1 jtc static int linenum;
157 1.36 christos static size_t max_abbrvar_len;
158 1.36 christos static size_t max_format_len;
159 1.41 christos static zic_t max_year;
160 1.41 christos static zic_t min_year;
161 1.46.2.1 martin static bool noise;
162 1.1 jtc static const char * rfilename;
163 1.1 jtc static int rlinenum;
164 1.1 jtc static const char * progname;
165 1.1 jtc static int timecnt;
166 1.45 christos static int timecnt_alloc;
167 1.1 jtc static int typecnt;
168 1.1 jtc
169 1.1 jtc /*
170 1.1 jtc ** Line codes.
171 1.1 jtc */
172 1.1 jtc
173 1.1 jtc #define LC_RULE 0
174 1.1 jtc #define LC_ZONE 1
175 1.1 jtc #define LC_LINK 2
176 1.1 jtc #define LC_LEAP 3
177 1.1 jtc
178 1.1 jtc /*
179 1.1 jtc ** Which fields are which on a Zone line.
180 1.1 jtc */
181 1.1 jtc
182 1.1 jtc #define ZF_NAME 1
183 1.1 jtc #define ZF_GMTOFF 2
184 1.1 jtc #define ZF_RULE 3
185 1.1 jtc #define ZF_FORMAT 4
186 1.1 jtc #define ZF_TILYEAR 5
187 1.1 jtc #define ZF_TILMONTH 6
188 1.1 jtc #define ZF_TILDAY 7
189 1.1 jtc #define ZF_TILTIME 8
190 1.1 jtc #define ZONE_MINFIELDS 5
191 1.1 jtc #define ZONE_MAXFIELDS 9
192 1.1 jtc
193 1.1 jtc /*
194 1.1 jtc ** Which fields are which on a Zone continuation line.
195 1.1 jtc */
196 1.1 jtc
197 1.1 jtc #define ZFC_GMTOFF 0
198 1.1 jtc #define ZFC_RULE 1
199 1.1 jtc #define ZFC_FORMAT 2
200 1.1 jtc #define ZFC_TILYEAR 3
201 1.1 jtc #define ZFC_TILMONTH 4
202 1.1 jtc #define ZFC_TILDAY 5
203 1.1 jtc #define ZFC_TILTIME 6
204 1.1 jtc #define ZONEC_MINFIELDS 3
205 1.1 jtc #define ZONEC_MAXFIELDS 7
206 1.1 jtc
207 1.1 jtc /*
208 1.1 jtc ** Which files are which on a Rule line.
209 1.1 jtc */
210 1.1 jtc
211 1.1 jtc #define RF_NAME 1
212 1.1 jtc #define RF_LOYEAR 2
213 1.1 jtc #define RF_HIYEAR 3
214 1.1 jtc #define RF_COMMAND 4
215 1.1 jtc #define RF_MONTH 5
216 1.1 jtc #define RF_DAY 6
217 1.1 jtc #define RF_TOD 7
218 1.1 jtc #define RF_STDOFF 8
219 1.1 jtc #define RF_ABBRVAR 9
220 1.1 jtc #define RULE_FIELDS 10
221 1.1 jtc
222 1.1 jtc /*
223 1.1 jtc ** Which fields are which on a Link line.
224 1.1 jtc */
225 1.1 jtc
226 1.1 jtc #define LF_FROM 1
227 1.1 jtc #define LF_TO 2
228 1.1 jtc #define LINK_FIELDS 3
229 1.1 jtc
230 1.1 jtc /*
231 1.1 jtc ** Which fields are which on a Leap line.
232 1.1 jtc */
233 1.1 jtc
234 1.1 jtc #define LP_YEAR 1
235 1.1 jtc #define LP_MONTH 2
236 1.1 jtc #define LP_DAY 3
237 1.1 jtc #define LP_TIME 4
238 1.1 jtc #define LP_CORR 5
239 1.1 jtc #define LP_ROLL 6
240 1.1 jtc #define LEAP_FIELDS 7
241 1.1 jtc
242 1.1 jtc /*
243 1.1 jtc ** Year synonyms.
244 1.1 jtc */
245 1.1 jtc
246 1.1 jtc #define YR_MINIMUM 0
247 1.1 jtc #define YR_MAXIMUM 1
248 1.1 jtc #define YR_ONLY 2
249 1.1 jtc
250 1.1 jtc static struct rule * rules;
251 1.1 jtc static int nrules; /* number of rules */
252 1.45 christos static int nrules_alloc;
253 1.1 jtc
254 1.1 jtc static struct zone * zones;
255 1.1 jtc static int nzones; /* number of zones */
256 1.45 christos static int nzones_alloc;
257 1.1 jtc
258 1.1 jtc struct link {
259 1.1 jtc const char * l_filename;
260 1.1 jtc int l_linenum;
261 1.1 jtc const char * l_from;
262 1.1 jtc const char * l_to;
263 1.1 jtc };
264 1.1 jtc
265 1.1 jtc static struct link * links;
266 1.1 jtc static int nlinks;
267 1.45 christos static int nlinks_alloc;
268 1.1 jtc
269 1.1 jtc struct lookup {
270 1.1 jtc const char * l_word;
271 1.1 jtc const int l_value;
272 1.1 jtc };
273 1.1 jtc
274 1.25 mlelstv static struct lookup const * byword(const char * string,
275 1.25 mlelstv const struct lookup * lp);
276 1.1 jtc
277 1.1 jtc static struct lookup const line_codes[] = {
278 1.1 jtc { "Rule", LC_RULE },
279 1.1 jtc { "Zone", LC_ZONE },
280 1.1 jtc { "Link", LC_LINK },
281 1.1 jtc { "Leap", LC_LEAP },
282 1.1 jtc { NULL, 0}
283 1.1 jtc };
284 1.1 jtc
285 1.1 jtc static struct lookup const mon_names[] = {
286 1.1 jtc { "January", TM_JANUARY },
287 1.1 jtc { "February", TM_FEBRUARY },
288 1.1 jtc { "March", TM_MARCH },
289 1.1 jtc { "April", TM_APRIL },
290 1.1 jtc { "May", TM_MAY },
291 1.1 jtc { "June", TM_JUNE },
292 1.1 jtc { "July", TM_JULY },
293 1.1 jtc { "August", TM_AUGUST },
294 1.1 jtc { "September", TM_SEPTEMBER },
295 1.1 jtc { "October", TM_OCTOBER },
296 1.1 jtc { "November", TM_NOVEMBER },
297 1.1 jtc { "December", TM_DECEMBER },
298 1.1 jtc { NULL, 0 }
299 1.1 jtc };
300 1.1 jtc
301 1.1 jtc static struct lookup const wday_names[] = {
302 1.1 jtc { "Sunday", TM_SUNDAY },
303 1.1 jtc { "Monday", TM_MONDAY },
304 1.1 jtc { "Tuesday", TM_TUESDAY },
305 1.1 jtc { "Wednesday", TM_WEDNESDAY },
306 1.1 jtc { "Thursday", TM_THURSDAY },
307 1.1 jtc { "Friday", TM_FRIDAY },
308 1.1 jtc { "Saturday", TM_SATURDAY },
309 1.1 jtc { NULL, 0 }
310 1.1 jtc };
311 1.1 jtc
312 1.1 jtc static struct lookup const lasts[] = {
313 1.1 jtc { "last-Sunday", TM_SUNDAY },
314 1.1 jtc { "last-Monday", TM_MONDAY },
315 1.1 jtc { "last-Tuesday", TM_TUESDAY },
316 1.1 jtc { "last-Wednesday", TM_WEDNESDAY },
317 1.1 jtc { "last-Thursday", TM_THURSDAY },
318 1.1 jtc { "last-Friday", TM_FRIDAY },
319 1.1 jtc { "last-Saturday", TM_SATURDAY },
320 1.1 jtc { NULL, 0 }
321 1.1 jtc };
322 1.1 jtc
323 1.1 jtc static struct lookup const begin_years[] = {
324 1.1 jtc { "minimum", YR_MINIMUM },
325 1.1 jtc { "maximum", YR_MAXIMUM },
326 1.1 jtc { NULL, 0 }
327 1.1 jtc };
328 1.1 jtc
329 1.1 jtc static struct lookup const end_years[] = {
330 1.1 jtc { "minimum", YR_MINIMUM },
331 1.1 jtc { "maximum", YR_MAXIMUM },
332 1.1 jtc { "only", YR_ONLY },
333 1.1 jtc { NULL, 0 }
334 1.1 jtc };
335 1.1 jtc
336 1.1 jtc static struct lookup const leap_types[] = {
337 1.46.2.1 martin { "Rolling", true },
338 1.46.2.1 martin { "Stationary", false },
339 1.1 jtc { NULL, 0 }
340 1.1 jtc };
341 1.1 jtc
342 1.1 jtc static const int len_months[2][MONSPERYEAR] = {
343 1.1 jtc { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
344 1.1 jtc { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
345 1.1 jtc };
346 1.1 jtc
347 1.1 jtc static const int len_years[2] = {
348 1.1 jtc DAYSPERNYEAR, DAYSPERLYEAR
349 1.1 jtc };
350 1.1 jtc
351 1.5 jtc static struct attype {
352 1.25 mlelstv zic_t at;
353 1.5 jtc unsigned char type;
354 1.45 christos } * attypes;
355 1.38 christos static zic_t gmtoffs[TZ_MAX_TYPES];
356 1.1 jtc static char isdsts[TZ_MAX_TYPES];
357 1.1 jtc static unsigned char abbrinds[TZ_MAX_TYPES];
358 1.46.2.1 martin static bool ttisstds[TZ_MAX_TYPES];
359 1.46.2.1 martin static bool ttisgmts[TZ_MAX_TYPES];
360 1.1 jtc static char chars[TZ_MAX_CHARS];
361 1.25 mlelstv static zic_t trans[TZ_MAX_LEAPS];
362 1.38 christos static zic_t corr[TZ_MAX_LEAPS];
363 1.1 jtc static char roll[TZ_MAX_LEAPS];
364 1.1 jtc
365 1.1 jtc /*
366 1.1 jtc ** Memory allocation.
367 1.1 jtc */
368 1.1 jtc
369 1.45 christos static _Noreturn void
370 1.45 christos memory_exhausted(const char *msg)
371 1.45 christos {
372 1.45 christos fprintf(stderr, _("%s: Memory exhausted: %s\n"), progname, msg);
373 1.45 christos exit(EXIT_FAILURE);
374 1.45 christos }
375 1.45 christos
376 1.45 christos static ATTRIBUTE_PURE size_t
377 1.45 christos size_product(size_t nitems, size_t itemsize)
378 1.45 christos {
379 1.45 christos if (SIZE_MAX / itemsize < nitems)
380 1.46.2.1 martin memory_exhausted(_("size overflow"));
381 1.45 christos return nitems * itemsize;
382 1.45 christos }
383 1.45 christos
384 1.41 christos static ATTRIBUTE_PURE void *
385 1.31 christos memcheck(void *const ptr)
386 1.1 jtc {
387 1.45 christos if (ptr == NULL)
388 1.45 christos memory_exhausted(strerror(errno));
389 1.1 jtc return ptr;
390 1.1 jtc }
391 1.1 jtc
392 1.31 christos #define emalloc(size) memcheck(malloc(size))
393 1.31 christos #define erealloc(ptr, size) memcheck(realloc((ptr), (size)))
394 1.1 jtc #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
395 1.1 jtc #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
396 1.1 jtc
397 1.45 christos static void *
398 1.45 christos growalloc(void *ptr, size_t itemsize, int nitems, int *nitems_alloc)
399 1.45 christos {
400 1.45 christos if (nitems < *nitems_alloc)
401 1.45 christos return ptr;
402 1.45 christos else {
403 1.45 christos int amax = INT_MAX < SIZE_MAX ? INT_MAX : SIZE_MAX;
404 1.45 christos if ((amax - 1) / 3 * 2 < *nitems_alloc)
405 1.46.2.1 martin memory_exhausted(_("int overflow"));
406 1.45 christos *nitems_alloc = *nitems_alloc + (*nitems_alloc >> 1) + 1;
407 1.45 christos return erealloc(ptr, size_product(*nitems_alloc, itemsize));
408 1.45 christos }
409 1.45 christos }
410 1.45 christos
411 1.1 jtc /*
412 1.1 jtc ** Error handling.
413 1.1 jtc */
414 1.1 jtc
415 1.1 jtc static void
416 1.31 christos eats(const char *const name, const int num, const char *const rname,
417 1.31 christos const int rnum)
418 1.1 jtc {
419 1.1 jtc filename = name;
420 1.1 jtc linenum = num;
421 1.1 jtc rfilename = rname;
422 1.1 jtc rlinenum = rnum;
423 1.1 jtc }
424 1.1 jtc
425 1.1 jtc static void
426 1.31 christos eat(const char *const name, const int num)
427 1.1 jtc {
428 1.31 christos eats(name, num, NULL, -1);
429 1.1 jtc }
430 1.1 jtc
431 1.43 christos static void ATTRIBUTE_FORMAT((printf, 1, 0))
432 1.43 christos verror(const char *const string, va_list args)
433 1.1 jtc {
434 1.1 jtc /*
435 1.1 jtc ** Match the format of "cc" to allow sh users to
436 1.1 jtc ** zic ... 2>&1 | error -t "*" -v
437 1.1 jtc ** on BSD systems.
438 1.1 jtc */
439 1.43 christos fprintf(stderr, _("\"%s\", line %d: "), filename, linenum);
440 1.43 christos vfprintf(stderr, string, args);
441 1.1 jtc if (rfilename != NULL)
442 1.46.2.1 martin fprintf(stderr, _(" (rule from \"%s\", line %d)"),
443 1.1 jtc rfilename, rlinenum);
444 1.46.2.1 martin fprintf(stderr, "\n");
445 1.1 jtc }
446 1.1 jtc
447 1.43 christos static void ATTRIBUTE_FORMAT((printf, 1, 2))
448 1.43 christos error(const char *const string, ...)
449 1.5 jtc {
450 1.43 christos va_list args;
451 1.43 christos va_start(args, string);
452 1.43 christos verror(string, args);
453 1.43 christos va_end(args);
454 1.46.2.1 martin errors = true;
455 1.43 christos }
456 1.43 christos
457 1.43 christos static void ATTRIBUTE_FORMAT((printf, 1, 2))
458 1.43 christos warning(const char *const string, ...)
459 1.43 christos {
460 1.43 christos va_list args;
461 1.43 christos fprintf(stderr, _("warning: "));
462 1.43 christos va_start(args, string);
463 1.43 christos verror(string, args);
464 1.43 christos va_end(args);
465 1.46.2.1 martin warnings = true;
466 1.46.2.1 martin }
467 1.46.2.1 martin
468 1.46.2.1 martin static void
469 1.46.2.1 martin close_file(FILE *stream, char const *name)
470 1.46.2.1 martin {
471 1.46.2.1 martin char const *e = (ferror(stream) ? _("I/O error")
472 1.46.2.1 martin : fclose(stream) != 0 ? strerror(errno) : NULL);
473 1.46.2.1 martin if (e) {
474 1.46.2.1 martin fprintf(stderr, "%s: ", progname);
475 1.46.2.1 martin if (name)
476 1.46.2.1 martin fprintf(stderr, "%s: ", name);
477 1.46.2.1 martin fprintf(stderr, "%s\n", e);
478 1.46.2.1 martin exit(EXIT_FAILURE);
479 1.46.2.1 martin }
480 1.5 jtc }
481 1.5 jtc
482 1.41 christos static _Noreturn void
483 1.25 mlelstv usage(FILE *stream, int status)
484 1.1 jtc {
485 1.46.2.1 martin fprintf(stream,
486 1.46.2.1 martin _("%s: usage is %s [ --version ] [ --help ] [ -v ] \\\n"
487 1.46.2.1 martin "\t[ -l localtime ] [ -p posixrules ] [ -d directory ] \\\n"
488 1.46.2.1 martin "\t[ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n\n"
489 1.46.2.1 martin "Report bugs to %s.\n"),
490 1.46.2.1 martin progname, progname, REPORT_BUGS_TO);
491 1.46.2.1 martin if (status == EXIT_SUCCESS)
492 1.46.2.1 martin close_file(stream, NULL);
493 1.46.2.1 martin exit(status);
494 1.1 jtc }
495 1.1 jtc
496 1.1 jtc static const char * psxrules;
497 1.1 jtc static const char * lcltime;
498 1.1 jtc static const char * directory;
499 1.1 jtc static const char * leapsec;
500 1.1 jtc static const char * yitcommand;
501 1.1 jtc
502 1.1 jtc int
503 1.31 christos main(int argc, char *argv[])
504 1.31 christos {
505 1.31 christos int i;
506 1.31 christos int j;
507 1.31 christos int c;
508 1.1 jtc
509 1.41 christos #ifdef S_IWGRP
510 1.1 jtc (void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
511 1.41 christos #endif
512 1.5 jtc #if HAVE_GETTEXT - 0
513 1.5 jtc (void) setlocale(LC_MESSAGES, "");
514 1.5 jtc #ifdef TZ_DOMAINDIR
515 1.5 jtc (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
516 1.5 jtc #endif /* defined TEXTDOMAINDIR */
517 1.5 jtc (void) textdomain(TZ_DOMAIN);
518 1.25 mlelstv #endif /* HAVE_GETTEXT */
519 1.1 jtc progname = argv[0];
520 1.25 mlelstv if (TYPE_BIT(zic_t) < 64) {
521 1.25 mlelstv (void) fprintf(stderr, "%s: %s\n", progname,
522 1.25 mlelstv _("wild compilation-time specification of zic_t"));
523 1.46.2.1 martin return EXIT_FAILURE;
524 1.25 mlelstv }
525 1.20 kleink for (i = 1; i < argc; ++i)
526 1.20 kleink if (strcmp(argv[i], "--version") == 0) {
527 1.35 christos (void) printf("zic %s%s\n", PKGVERSION, TZVERSION);
528 1.46.2.1 martin close_file(stdout, NULL);
529 1.46.2.1 martin return EXIT_SUCCESS;
530 1.25 mlelstv } else if (strcmp(argv[i], "--help") == 0) {
531 1.25 mlelstv usage(stdout, EXIT_SUCCESS);
532 1.20 kleink }
533 1.7 jtc while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF && c != -1)
534 1.1 jtc switch (c) {
535 1.1 jtc default:
536 1.25 mlelstv usage(stderr, EXIT_FAILURE);
537 1.1 jtc case 'd':
538 1.1 jtc if (directory == NULL)
539 1.1 jtc directory = optarg;
540 1.1 jtc else {
541 1.46.2.1 martin fprintf(stderr,
542 1.5 jtc _("%s: More than one -d option specified\n"),
543 1.1 jtc progname);
544 1.46.2.1 martin return EXIT_FAILURE;
545 1.1 jtc }
546 1.1 jtc break;
547 1.1 jtc case 'l':
548 1.1 jtc if (lcltime == NULL)
549 1.1 jtc lcltime = optarg;
550 1.1 jtc else {
551 1.46.2.1 martin fprintf(stderr,
552 1.5 jtc _("%s: More than one -l option specified\n"),
553 1.1 jtc progname);
554 1.46.2.1 martin return EXIT_FAILURE;
555 1.1 jtc }
556 1.1 jtc break;
557 1.1 jtc case 'p':
558 1.1 jtc if (psxrules == NULL)
559 1.1 jtc psxrules = optarg;
560 1.1 jtc else {
561 1.46.2.1 martin fprintf(stderr,
562 1.5 jtc _("%s: More than one -p option specified\n"),
563 1.1 jtc progname);
564 1.46.2.1 martin return EXIT_FAILURE;
565 1.1 jtc }
566 1.1 jtc break;
567 1.1 jtc case 'y':
568 1.1 jtc if (yitcommand == NULL)
569 1.1 jtc yitcommand = optarg;
570 1.1 jtc else {
571 1.46.2.1 martin fprintf(stderr,
572 1.5 jtc _("%s: More than one -y option specified\n"),
573 1.1 jtc progname);
574 1.46.2.1 martin return EXIT_FAILURE;
575 1.1 jtc }
576 1.1 jtc break;
577 1.1 jtc case 'L':
578 1.1 jtc if (leapsec == NULL)
579 1.1 jtc leapsec = optarg;
580 1.1 jtc else {
581 1.46.2.1 martin fprintf(stderr,
582 1.5 jtc _("%s: More than one -L option specified\n"),
583 1.1 jtc progname);
584 1.46.2.1 martin return EXIT_FAILURE;
585 1.1 jtc }
586 1.1 jtc break;
587 1.1 jtc case 'v':
588 1.46.2.1 martin noise = true;
589 1.1 jtc break;
590 1.1 jtc case 's':
591 1.46.2.1 martin warning(_("-s ignored\n"));
592 1.1 jtc break;
593 1.1 jtc }
594 1.1 jtc if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
595 1.25 mlelstv usage(stderr, EXIT_FAILURE); /* usage message by request */
596 1.1 jtc if (directory == NULL)
597 1.1 jtc directory = TZDIR;
598 1.1 jtc if (yitcommand == NULL)
599 1.1 jtc yitcommand = "yearistype";
600 1.1 jtc
601 1.1 jtc if (optind < argc && leapsec != NULL) {
602 1.1 jtc infile(leapsec);
603 1.1 jtc adjleap();
604 1.1 jtc }
605 1.1 jtc
606 1.1 jtc for (i = optind; i < argc; ++i)
607 1.1 jtc infile(argv[i]);
608 1.1 jtc if (errors)
609 1.46.2.1 martin return EXIT_FAILURE;
610 1.1 jtc associate();
611 1.1 jtc for (i = 0; i < nzones; i = j) {
612 1.1 jtc /*
613 1.1 jtc ** Find the next non-continuation zone entry.
614 1.1 jtc */
615 1.1 jtc for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
616 1.1 jtc continue;
617 1.1 jtc outzone(&zones[i], j - i);
618 1.1 jtc }
619 1.1 jtc /*
620 1.1 jtc ** Make links.
621 1.1 jtc */
622 1.15 kleink for (i = 0; i < nlinks; ++i) {
623 1.15 kleink eat(links[i].l_filename, links[i].l_linenum);
624 1.1 jtc dolink(links[i].l_from, links[i].l_to);
625 1.25 mlelstv if (noise)
626 1.25 mlelstv for (j = 0; j < nlinks; ++j)
627 1.25 mlelstv if (strcmp(links[i].l_to,
628 1.25 mlelstv links[j].l_from) == 0)
629 1.25 mlelstv warning(_("link to link"));
630 1.15 kleink }
631 1.15 kleink if (lcltime != NULL) {
632 1.46.2.1 martin eat(_("command line"), 1);
633 1.1 jtc dolink(lcltime, TZDEFAULT);
634 1.15 kleink }
635 1.15 kleink if (psxrules != NULL) {
636 1.46.2.1 martin eat(_("command line"), 1);
637 1.1 jtc dolink(psxrules, TZDEFRULES);
638 1.15 kleink }
639 1.46.2.1 martin if (warnings && (ferror(stderr) || fclose(stderr) != 0))
640 1.46.2.1 martin return EXIT_FAILURE;
641 1.46.2.1 martin return errors ? EXIT_FAILURE : EXIT_SUCCESS;
642 1.46.2.1 martin }
643 1.46.2.1 martin
644 1.46.2.1 martin static void
645 1.46.2.1 martin componentcheck(char const *name, char const *component,
646 1.46.2.1 martin char const *component_end)
647 1.46.2.1 martin {
648 1.46.2.1 martin enum { component_len_max = 14 };
649 1.46.2.1 martin size_t component_len = component_end - component;
650 1.46.2.1 martin if (0 < component_len && component_len <= 2
651 1.46.2.1 martin && component[0] == '.' && component_end[-1] == '.') {
652 1.46.2.1 martin fprintf(stderr, _("%s: file name '%s' contains"
653 1.46.2.1 martin " '%.*s' component"),
654 1.46.2.1 martin progname, name, (int) component_len, component);
655 1.46.2.1 martin exit(EXIT_FAILURE);
656 1.46.2.1 martin }
657 1.46.2.1 martin if (!noise)
658 1.46.2.1 martin return;
659 1.46.2.1 martin if (0 < component_len && component[0] == '-')
660 1.46.2.1 martin warning(_("file name '%s' component contains leading '-'"),
661 1.46.2.1 martin name);
662 1.46.2.1 martin if (component_len_max < component_len)
663 1.46.2.1 martin warning(_("file name '%s' contains overlength component"
664 1.46.2.1 martin " '%.*s...'"),
665 1.46.2.1 martin name, component_len_max, component);
666 1.46.2.1 martin }
667 1.46.2.1 martin
668 1.46.2.1 martin static void
669 1.46.2.1 martin namecheck(const char *name)
670 1.46.2.1 martin {
671 1.46.2.1 martin char const *cp;
672 1.46.2.1 martin
673 1.46.2.1 martin /* Benign characters in a portable file name. */
674 1.46.2.1 martin static char const benign[] =
675 1.46.2.1 martin "-/_"
676 1.46.2.1 martin "abcdefghijklmnopqrstuvwxyz"
677 1.46.2.1 martin "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
678 1.46.2.1 martin
679 1.46.2.1 martin /* Non-control chars in the POSIX portable character set,
680 1.46.2.1 martin excluding the benign characters. */
681 1.46.2.1 martin static char const printable_and_not_benign[] =
682 1.46.2.1 martin " !\"#$%&'()*+,.0123456789:;<=>?@[\\]^`{|}~";
683 1.46.2.1 martin
684 1.46.2.1 martin char const *component = name;
685 1.46.2.1 martin for (cp = name; *cp; cp++) {
686 1.46.2.1 martin unsigned char c = *cp;
687 1.46.2.1 martin if (noise && !strchr(benign, c)) {
688 1.46.2.1 martin warning((strchr(printable_and_not_benign, c)
689 1.46.2.1 martin ? _("file name '%s' contains byte '%c'")
690 1.46.2.1 martin : _("file name '%s' contains byte '\\%o'")),
691 1.46.2.1 martin name, c);
692 1.46.2.1 martin return;
693 1.46.2.1 martin }
694 1.46.2.1 martin if (c == '/') {
695 1.46.2.1 martin componentcheck(name, component, cp);
696 1.46.2.1 martin component = cp + 1;
697 1.46.2.1 martin }
698 1.46.2.1 martin }
699 1.46.2.1 martin componentcheck(name, component, cp);
700 1.1 jtc }
701 1.1 jtc
702 1.1 jtc static void
703 1.31 christos dolink(const char *const fromfield, const char *const tofield)
704 1.1 jtc {
705 1.31 christos char * fromname;
706 1.31 christos char * toname;
707 1.46.2.1 martin int fromisdir;
708 1.1 jtc
709 1.46.2.1 martin namecheck(tofield);
710 1.25 mlelstv if (fromfield[0] == '/')
711 1.25 mlelstv fromname = ecpyalloc(fromfield);
712 1.1 jtc else {
713 1.1 jtc fromname = ecpyalloc(directory);
714 1.1 jtc fromname = ecatalloc(fromname, "/");
715 1.25 mlelstv fromname = ecatalloc(fromname, fromfield);
716 1.1 jtc }
717 1.25 mlelstv if (tofield[0] == '/')
718 1.25 mlelstv toname = ecpyalloc(tofield);
719 1.1 jtc else {
720 1.1 jtc toname = ecpyalloc(directory);
721 1.1 jtc toname = ecatalloc(toname, "/");
722 1.25 mlelstv toname = ecatalloc(toname, tofield);
723 1.1 jtc }
724 1.1 jtc /*
725 1.1 jtc ** We get to be careful here since
726 1.1 jtc ** there's a fair chance of root running us.
727 1.1 jtc */
728 1.46.2.1 martin fromisdir = itsdir(fromname);
729 1.46.2.1 martin if (fromisdir) {
730 1.46.2.1 martin char const *e = strerror(fromisdir < 0 ? errno : EPERM);
731 1.46.2.1 martin fprintf(stderr, _("%s: link from %s failed: %s"),
732 1.46.2.1 martin progname, fromname, e);
733 1.46.2.1 martin exit(EXIT_FAILURE);
734 1.46.2.1 martin }
735 1.46.2.1 martin if (itsdir(toname) <= 0)
736 1.46.2.1 martin remove(toname);
737 1.46.2.1 martin if (link(fromname, toname) != 0) {
738 1.12 kleink int result;
739 1.12 kleink
740 1.46.2.1 martin if (! mkdirs(toname))
741 1.25 mlelstv exit(EXIT_FAILURE);
742 1.15 kleink
743 1.12 kleink result = link(fromname, toname);
744 1.44 christos if (result != 0) {
745 1.44 christos const char *s = fromfield;
746 1.44 christos const char *t;
747 1.31 christos char * symlinkcontents = NULL;
748 1.25 mlelstv
749 1.44 christos do
750 1.44 christos t = s;
751 1.44 christos while ((s = strchr(s, '/'))
752 1.44 christos && ! strncmp (fromfield, tofield,
753 1.44 christos ++s - fromfield));
754 1.44 christos
755 1.44 christos for (s = tofield + (t - fromfield);
756 1.44 christos (s = strchr(s, '/'));
757 1.44 christos s++)
758 1.25 mlelstv symlinkcontents =
759 1.25 mlelstv ecatalloc(symlinkcontents,
760 1.25 mlelstv "../");
761 1.44 christos symlinkcontents = ecatalloc(symlinkcontents, t);
762 1.44 christos result = symlink(symlinkcontents, toname);
763 1.25 mlelstv if (result == 0)
764 1.12 kleink warning(_("hard link failed, symbolic link used"));
765 1.31 christos free(symlinkcontents);
766 1.12 kleink }
767 1.12 kleink if (result != 0) {
768 1.44 christos FILE *fp, *tp;
769 1.44 christos int c;
770 1.44 christos fp = fopen(fromname, "rb");
771 1.44 christos if (!fp) {
772 1.44 christos const char *e = strerror(errno);
773 1.46.2.1 martin fprintf(stderr,
774 1.44 christos _("%s: Can't read %s: %s\n"),
775 1.44 christos progname, fromname, e);
776 1.44 christos exit(EXIT_FAILURE);
777 1.44 christos }
778 1.44 christos tp = fopen(toname, "wb");
779 1.44 christos if (!tp) {
780 1.44 christos const char *e = strerror(errno);
781 1.46.2.1 martin fprintf(stderr,
782 1.44 christos _("%s: Can't create %s: %s\n"),
783 1.44 christos progname, toname, e);
784 1.44 christos exit(EXIT_FAILURE);
785 1.44 christos }
786 1.44 christos while ((c = getc(fp)) != EOF)
787 1.44 christos putc(c, tp);
788 1.46.2.1 martin close_file(fp, fromname);
789 1.46.2.1 martin close_file(tp, toname);
790 1.44 christos warning(_("link failed, copy used"));
791 1.1 jtc }
792 1.1 jtc }
793 1.31 christos free(fromname);
794 1.31 christos free(toname);
795 1.1 jtc }
796 1.1 jtc
797 1.41 christos #define TIME_T_BITS_IN_FILE 64
798 1.41 christos
799 1.41 christos static const zic_t min_time = (zic_t) -1 << (TIME_T_BITS_IN_FILE - 1);
800 1.41 christos static const zic_t max_time = -1 - ((zic_t) -1 << (TIME_T_BITS_IN_FILE - 1));
801 1.41 christos
802 1.46 christos /* Estimated time of the Big Bang, in seconds since the POSIX epoch.
803 1.46 christos rounded downward to the negation of a power of two that is
804 1.46 christos comfortably outside the error bounds.
805 1.46 christos
806 1.46 christos zic does not output time stamps before this, partly because they
807 1.46 christos are physically suspect, and partly because GNOME mishandles them; see
808 1.46 christos GNOME bug 730332 <https://bugzilla.gnome.org/show_bug.cgi?id=730332>.
809 1.46 christos
810 1.46 christos For the time of the Big Bang, see:
811 1.46 christos
812 1.46 christos Ade PAR, Aghanim N, Armitage-Caplan C et al. Planck 2013 results.
813 1.46 christos I. Overview of products and scientific results.
814 1.46 christos arXiv:1303.5062 2013-03-20 20:10:01 UTC
815 1.46 christos <http://arxiv.org/pdf/1303.5062v1> [PDF]
816 1.46 christos
817 1.46 christos Page 36, Table 9, row Age/Gyr, column Planck+WP+highL+BAO 68% limits
818 1.46 christos gives the value 13.798 plus-or-minus 0.037 billion years.
819 1.46 christos Multiplying this by 1000000000 and then by 31557600 (the number of
820 1.46 christos seconds in an astronomical year) gives a value that is comfortably
821 1.46 christos less than 2**59, so BIG_BANG is - 2**59.
822 1.46 christos
823 1.46 christos BIG_BANG is approximate, and may change in future versions.
824 1.46 christos Please do not rely on its exact value. */
825 1.46 christos
826 1.46 christos #ifndef BIG_BANG
827 1.46 christos #define BIG_BANG (- (1LL << 59))
828 1.46 christos #endif
829 1.46 christos
830 1.46 christos static const zic_t big_bang_time = BIG_BANG;
831 1.46 christos
832 1.46.2.1 martin /* Return 1 if NAME is a directory, 0 if it's something else, -1 if trouble. */
833 1.1 jtc static int
834 1.31 christos itsdir(const char *const name)
835 1.1 jtc {
836 1.46.2.1 martin struct stat st;
837 1.46.2.1 martin int res = stat(name, &st);
838 1.46.2.1 martin if (res != 0)
839 1.46.2.1 martin return res;
840 1.46.2.1 martin #ifdef S_ISDIR
841 1.46.2.1 martin return S_ISDIR(st.st_mode) != 0;
842 1.46.2.1 martin #else
843 1.46.2.1 martin {
844 1.46.2.1 martin char *nameslashdot = ecatalloc(ecpyalloc(name), "/.");
845 1.46.2.1 martin res = stat(nameslashdot, &st);
846 1.46.2.1 martin free(nameslashdot);
847 1.46.2.1 martin return res == 0;
848 1.46.2.1 martin }
849 1.46.2.1 martin #endif
850 1.1 jtc }
851 1.1 jtc
852 1.1 jtc /*
853 1.1 jtc ** Associate sets of rules with zones.
854 1.1 jtc */
855 1.1 jtc
856 1.1 jtc /*
857 1.1 jtc ** Sort by rule name.
858 1.1 jtc */
859 1.1 jtc
860 1.1 jtc static int
861 1.31 christos rcomp(const void *cp1, const void *cp2)
862 1.1 jtc {
863 1.1 jtc return strcmp(((const struct rule *) cp1)->r_name,
864 1.1 jtc ((const struct rule *) cp2)->r_name);
865 1.1 jtc }
866 1.1 jtc
867 1.1 jtc static void
868 1.25 mlelstv associate(void)
869 1.1 jtc {
870 1.31 christos struct zone * zp;
871 1.31 christos struct rule * rp;
872 1.31 christos int base, out;
873 1.31 christos int i, j;
874 1.1 jtc
875 1.5 jtc if (nrules != 0) {
876 1.31 christos (void) qsort(rules, (size_t)nrules, sizeof *rules, rcomp);
877 1.5 jtc for (i = 0; i < nrules - 1; ++i) {
878 1.5 jtc if (strcmp(rules[i].r_name,
879 1.5 jtc rules[i + 1].r_name) != 0)
880 1.5 jtc continue;
881 1.5 jtc if (strcmp(rules[i].r_filename,
882 1.5 jtc rules[i + 1].r_filename) == 0)
883 1.5 jtc continue;
884 1.5 jtc eat(rules[i].r_filename, rules[i].r_linenum);
885 1.5 jtc warning(_("same rule name in multiple files"));
886 1.5 jtc eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
887 1.5 jtc warning(_("same rule name in multiple files"));
888 1.5 jtc for (j = i + 2; j < nrules; ++j) {
889 1.5 jtc if (strcmp(rules[i].r_name,
890 1.5 jtc rules[j].r_name) != 0)
891 1.5 jtc break;
892 1.5 jtc if (strcmp(rules[i].r_filename,
893 1.5 jtc rules[j].r_filename) == 0)
894 1.5 jtc continue;
895 1.5 jtc if (strcmp(rules[i + 1].r_filename,
896 1.5 jtc rules[j].r_filename) == 0)
897 1.5 jtc continue;
898 1.5 jtc break;
899 1.5 jtc }
900 1.5 jtc i = j - 1;
901 1.5 jtc }
902 1.5 jtc }
903 1.1 jtc for (i = 0; i < nzones; ++i) {
904 1.1 jtc zp = &zones[i];
905 1.1 jtc zp->z_rules = NULL;
906 1.1 jtc zp->z_nrules = 0;
907 1.1 jtc }
908 1.1 jtc for (base = 0; base < nrules; base = out) {
909 1.1 jtc rp = &rules[base];
910 1.1 jtc for (out = base + 1; out < nrules; ++out)
911 1.1 jtc if (strcmp(rp->r_name, rules[out].r_name) != 0)
912 1.1 jtc break;
913 1.1 jtc for (i = 0; i < nzones; ++i) {
914 1.1 jtc zp = &zones[i];
915 1.1 jtc if (strcmp(zp->z_rule, rp->r_name) != 0)
916 1.1 jtc continue;
917 1.1 jtc zp->z_rules = rp;
918 1.1 jtc zp->z_nrules = out - base;
919 1.1 jtc }
920 1.1 jtc }
921 1.1 jtc for (i = 0; i < nzones; ++i) {
922 1.1 jtc zp = &zones[i];
923 1.1 jtc if (zp->z_nrules == 0) {
924 1.1 jtc /*
925 1.1 jtc ** Maybe we have a local standard time offset.
926 1.1 jtc */
927 1.1 jtc eat(zp->z_filename, zp->z_linenum);
928 1.5 jtc zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
929 1.46.2.1 martin true);
930 1.1 jtc /*
931 1.1 jtc ** Note, though, that if there's no rule,
932 1.1 jtc ** a '%s' in the format is a bad thing.
933 1.1 jtc */
934 1.1 jtc if (strchr(zp->z_format, '%') != 0)
935 1.43 christos error("%s", _("%s in ruleless zone"));
936 1.1 jtc }
937 1.1 jtc }
938 1.1 jtc if (errors)
939 1.25 mlelstv exit(EXIT_FAILURE);
940 1.1 jtc }
941 1.1 jtc
942 1.1 jtc static void
943 1.31 christos infile(const char *name)
944 1.1 jtc {
945 1.31 christos FILE * fp;
946 1.31 christos char ** fields;
947 1.31 christos char * cp;
948 1.31 christos const struct lookup * lp;
949 1.31 christos int nfields;
950 1.46.2.1 martin bool wantcont;
951 1.31 christos int num;
952 1.1 jtc char buf[BUFSIZ];
953 1.1 jtc
954 1.1 jtc if (strcmp(name, "-") == 0) {
955 1.5 jtc name = _("standard input");
956 1.1 jtc fp = stdin;
957 1.1 jtc } else if ((fp = fopen(name, "r")) == NULL) {
958 1.5 jtc const char *e = strerror(errno);
959 1.7 jtc
960 1.46.2.1 martin fprintf(stderr, _("%s: Can't open %s: %s\n"),
961 1.5 jtc progname, name, e);
962 1.25 mlelstv exit(EXIT_FAILURE);
963 1.1 jtc }
964 1.46.2.1 martin wantcont = false;
965 1.1 jtc for (num = 1; ; ++num) {
966 1.1 jtc eat(name, num);
967 1.1 jtc if (fgets(buf, (int) sizeof buf, fp) != buf)
968 1.1 jtc break;
969 1.1 jtc cp = strchr(buf, '\n');
970 1.1 jtc if (cp == NULL) {
971 1.40 christos error(_("line too long"));
972 1.25 mlelstv exit(EXIT_FAILURE);
973 1.1 jtc }
974 1.1 jtc *cp = '\0';
975 1.1 jtc fields = getfields(buf);
976 1.1 jtc nfields = 0;
977 1.1 jtc while (fields[nfields] != NULL) {
978 1.1 jtc static char nada;
979 1.1 jtc
980 1.3 jtc if (strcmp(fields[nfields], "-") == 0)
981 1.1 jtc fields[nfields] = &nada;
982 1.1 jtc ++nfields;
983 1.1 jtc }
984 1.1 jtc if (nfields == 0) {
985 1.1 jtc /* nothing to do */
986 1.1 jtc } else if (wantcont) {
987 1.1 jtc wantcont = inzcont(fields, nfields);
988 1.1 jtc } else {
989 1.1 jtc lp = byword(fields[0], line_codes);
990 1.1 jtc if (lp == NULL)
991 1.5 jtc error(_("input line of unknown type"));
992 1.1 jtc else switch ((int) (lp->l_value)) {
993 1.1 jtc case LC_RULE:
994 1.1 jtc inrule(fields, nfields);
995 1.46.2.1 martin wantcont = false;
996 1.1 jtc break;
997 1.1 jtc case LC_ZONE:
998 1.1 jtc wantcont = inzone(fields, nfields);
999 1.1 jtc break;
1000 1.1 jtc case LC_LINK:
1001 1.1 jtc inlink(fields, nfields);
1002 1.46.2.1 martin wantcont = false;
1003 1.1 jtc break;
1004 1.1 jtc case LC_LEAP:
1005 1.1 jtc if (name != leapsec)
1006 1.46.2.1 martin warning(
1007 1.5 jtc _("%s: Leap line in non leap seconds file %s\n"),
1008 1.1 jtc progname, name);
1009 1.1 jtc else inleap(fields, nfields);
1010 1.46.2.1 martin wantcont = false;
1011 1.1 jtc break;
1012 1.1 jtc default: /* "cannot happen" */
1013 1.46.2.1 martin fprintf(stderr,
1014 1.5 jtc _("%s: panic: Invalid l_value %d\n"),
1015 1.1 jtc progname, lp->l_value);
1016 1.25 mlelstv exit(EXIT_FAILURE);
1017 1.1 jtc }
1018 1.1 jtc }
1019 1.31 christos free(fields);
1020 1.1 jtc }
1021 1.46.2.1 martin close_file(fp, filename);
1022 1.1 jtc if (wantcont)
1023 1.5 jtc error(_("expected continuation line not found"));
1024 1.1 jtc }
1025 1.1 jtc
1026 1.1 jtc /*
1027 1.1 jtc ** Convert a string of one of the forms
1028 1.1 jtc ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
1029 1.1 jtc ** into a number of seconds.
1030 1.1 jtc ** A null string maps to zero.
1031 1.1 jtc ** Call error with errstring and return zero on errors.
1032 1.1 jtc */
1033 1.1 jtc
1034 1.38 christos static zic_t
1035 1.46.2.1 martin gethms(char const *string, char const *errstring, bool signable)
1036 1.1 jtc {
1037 1.38 christos zic_t hh;
1038 1.25 mlelstv int mm, ss, sign;
1039 1.1 jtc
1040 1.1 jtc if (string == NULL || *string == '\0')
1041 1.1 jtc return 0;
1042 1.1 jtc if (!signable)
1043 1.1 jtc sign = 1;
1044 1.1 jtc else if (*string == '-') {
1045 1.1 jtc sign = -1;
1046 1.1 jtc ++string;
1047 1.1 jtc } else sign = 1;
1048 1.41 christos if (sscanf(string, scheck(string, "%"SCNdZIC), &hh) == 1)
1049 1.1 jtc mm = ss = 0;
1050 1.41 christos else if (sscanf(string, scheck(string, "%"SCNdZIC":%d"), &hh, &mm) == 2)
1051 1.1 jtc ss = 0;
1052 1.41 christos else if (sscanf(string, scheck(string, "%"SCNdZIC":%d:%d"),
1053 1.39 christos &hh, &mm, &ss) != 3) {
1054 1.43 christos error("%s", errstring);
1055 1.1 jtc return 0;
1056 1.1 jtc }
1057 1.25 mlelstv if (hh < 0 ||
1058 1.1 jtc mm < 0 || mm >= MINSPERHOUR ||
1059 1.25 mlelstv ss < 0 || ss > SECSPERMIN) {
1060 1.43 christos error("%s", errstring);
1061 1.1 jtc return 0;
1062 1.1 jtc }
1063 1.41 christos if (ZIC_MAX / SECSPERHOUR < hh) {
1064 1.25 mlelstv error(_("time overflow"));
1065 1.25 mlelstv return 0;
1066 1.25 mlelstv }
1067 1.25 mlelstv if (noise && (hh > HOURSPERDAY ||
1068 1.25 mlelstv (hh == HOURSPERDAY && (mm != 0 || ss != 0))))
1069 1.25 mlelstv warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
1070 1.41 christos return oadd(sign * hh * SECSPERHOUR,
1071 1.41 christos sign * (mm * SECSPERMIN + ss));
1072 1.1 jtc }
1073 1.1 jtc
1074 1.1 jtc static void
1075 1.31 christos inrule(char **const fields, const int nfields)
1076 1.1 jtc {
1077 1.1 jtc static struct rule r;
1078 1.1 jtc
1079 1.1 jtc if (nfields != RULE_FIELDS) {
1080 1.5 jtc error(_("wrong number of fields on Rule line"));
1081 1.1 jtc return;
1082 1.1 jtc }
1083 1.1 jtc if (*fields[RF_NAME] == '\0') {
1084 1.5 jtc error(_("nameless rule"));
1085 1.1 jtc return;
1086 1.1 jtc }
1087 1.1 jtc r.r_filename = filename;
1088 1.1 jtc r.r_linenum = linenum;
1089 1.46.2.1 martin r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), true);
1090 1.1 jtc rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
1091 1.1 jtc fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
1092 1.1 jtc r.r_name = ecpyalloc(fields[RF_NAME]);
1093 1.1 jtc r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
1094 1.25 mlelstv if (max_abbrvar_len < strlen(r.r_abbrvar))
1095 1.25 mlelstv max_abbrvar_len = strlen(r.r_abbrvar);
1096 1.45 christos rules = growalloc(rules, sizeof *rules, nrules, &nrules_alloc);
1097 1.1 jtc rules[nrules++] = r;
1098 1.1 jtc }
1099 1.1 jtc
1100 1.46.2.1 martin static bool
1101 1.31 christos inzone(char **const fields, const int nfields)
1102 1.1 jtc {
1103 1.31 christos int i;
1104 1.1 jtc
1105 1.1 jtc if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
1106 1.5 jtc error(_("wrong number of fields on Zone line"));
1107 1.46.2.1 martin return false;
1108 1.1 jtc }
1109 1.1 jtc if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
1110 1.43 christos error(
1111 1.5 jtc _("\"Zone %s\" line and -l option are mutually exclusive"),
1112 1.1 jtc TZDEFAULT);
1113 1.46.2.1 martin return false;
1114 1.1 jtc }
1115 1.1 jtc if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
1116 1.43 christos error(
1117 1.5 jtc _("\"Zone %s\" line and -p option are mutually exclusive"),
1118 1.1 jtc TZDEFRULES);
1119 1.46.2.1 martin return false;
1120 1.1 jtc }
1121 1.1 jtc for (i = 0; i < nzones; ++i)
1122 1.1 jtc if (zones[i].z_name != NULL &&
1123 1.1 jtc strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
1124 1.43 christos error(
1125 1.5 jtc _("duplicate zone name %s (file \"%s\", line %d)"),
1126 1.1 jtc fields[ZF_NAME],
1127 1.1 jtc zones[i].z_filename,
1128 1.1 jtc zones[i].z_linenum);
1129 1.46.2.1 martin return false;
1130 1.1 jtc }
1131 1.46.2.1 martin return inzsub(fields, nfields, false);
1132 1.1 jtc }
1133 1.1 jtc
1134 1.46.2.1 martin static bool
1135 1.31 christos inzcont(char **const fields, const int nfields)
1136 1.1 jtc {
1137 1.1 jtc if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
1138 1.5 jtc error(_("wrong number of fields on Zone continuation line"));
1139 1.46.2.1 martin return false;
1140 1.1 jtc }
1141 1.46.2.1 martin return inzsub(fields, nfields, true);
1142 1.1 jtc }
1143 1.1 jtc
1144 1.46.2.1 martin static bool
1145 1.31 christos inzsub(char **const fields, const int nfields, const int iscont)
1146 1.1 jtc {
1147 1.31 christos char * cp;
1148 1.1 jtc static struct zone z;
1149 1.31 christos int i_gmtoff, i_rule, i_format;
1150 1.31 christos int i_untilyear, i_untilmonth;
1151 1.31 christos int i_untilday, i_untiltime;
1152 1.46.2.1 martin bool hasuntil;
1153 1.1 jtc
1154 1.1 jtc if (iscont) {
1155 1.1 jtc i_gmtoff = ZFC_GMTOFF;
1156 1.1 jtc i_rule = ZFC_RULE;
1157 1.1 jtc i_format = ZFC_FORMAT;
1158 1.1 jtc i_untilyear = ZFC_TILYEAR;
1159 1.1 jtc i_untilmonth = ZFC_TILMONTH;
1160 1.1 jtc i_untilday = ZFC_TILDAY;
1161 1.1 jtc i_untiltime = ZFC_TILTIME;
1162 1.1 jtc z.z_name = NULL;
1163 1.1 jtc } else {
1164 1.1 jtc i_gmtoff = ZF_GMTOFF;
1165 1.1 jtc i_rule = ZF_RULE;
1166 1.1 jtc i_format = ZF_FORMAT;
1167 1.1 jtc i_untilyear = ZF_TILYEAR;
1168 1.1 jtc i_untilmonth = ZF_TILMONTH;
1169 1.1 jtc i_untilday = ZF_TILDAY;
1170 1.1 jtc i_untiltime = ZF_TILTIME;
1171 1.1 jtc z.z_name = ecpyalloc(fields[ZF_NAME]);
1172 1.1 jtc }
1173 1.1 jtc z.z_filename = filename;
1174 1.1 jtc z.z_linenum = linenum;
1175 1.46.2.1 martin z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UT offset"), true);
1176 1.1 jtc if ((cp = strchr(fields[i_format], '%')) != 0) {
1177 1.1 jtc if (*++cp != 's' || strchr(cp, '%') != 0) {
1178 1.5 jtc error(_("invalid abbreviation format"));
1179 1.46.2.1 martin return false;
1180 1.1 jtc }
1181 1.1 jtc }
1182 1.1 jtc z.z_rule = ecpyalloc(fields[i_rule]);
1183 1.1 jtc z.z_format = ecpyalloc(fields[i_format]);
1184 1.25 mlelstv if (max_format_len < strlen(z.z_format))
1185 1.25 mlelstv max_format_len = strlen(z.z_format);
1186 1.1 jtc hasuntil = nfields > i_untilyear;
1187 1.1 jtc if (hasuntil) {
1188 1.1 jtc z.z_untilrule.r_filename = filename;
1189 1.1 jtc z.z_untilrule.r_linenum = linenum;
1190 1.1 jtc rulesub(&z.z_untilrule,
1191 1.1 jtc fields[i_untilyear],
1192 1.1 jtc "only",
1193 1.1 jtc "",
1194 1.1 jtc (nfields > i_untilmonth) ?
1195 1.1 jtc fields[i_untilmonth] : "Jan",
1196 1.1 jtc (nfields > i_untilday) ? fields[i_untilday] : "1",
1197 1.1 jtc (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1198 1.1 jtc z.z_untiltime = rpytime(&z.z_untilrule,
1199 1.1 jtc z.z_untilrule.r_loyear);
1200 1.1 jtc if (iscont && nzones > 0 &&
1201 1.1 jtc z.z_untiltime > min_time &&
1202 1.1 jtc z.z_untiltime < max_time &&
1203 1.1 jtc zones[nzones - 1].z_untiltime > min_time &&
1204 1.1 jtc zones[nzones - 1].z_untiltime < max_time &&
1205 1.1 jtc zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1206 1.25 mlelstv error(_(
1207 1.25 mlelstv "Zone continuation line end time is not after end time of previous line"
1208 1.25 mlelstv ));
1209 1.46.2.1 martin return false;
1210 1.1 jtc }
1211 1.1 jtc }
1212 1.45 christos zones = growalloc(zones, sizeof *zones, nzones, &nzones_alloc);
1213 1.1 jtc zones[nzones++] = z;
1214 1.1 jtc /*
1215 1.1 jtc ** If there was an UNTIL field on this line,
1216 1.1 jtc ** there's more information about the zone on the next line.
1217 1.1 jtc */
1218 1.1 jtc return hasuntil;
1219 1.1 jtc }
1220 1.1 jtc
1221 1.1 jtc static void
1222 1.31 christos inleap(char **const fields, const int nfields)
1223 1.31 christos {
1224 1.31 christos const char * cp;
1225 1.31 christos const struct lookup * lp;
1226 1.31 christos int i, j;
1227 1.41 christos zic_t year;
1228 1.41 christos int month, day;
1229 1.41 christos zic_t dayoff, tod;
1230 1.41 christos zic_t t;
1231 1.1 jtc
1232 1.1 jtc if (nfields != LEAP_FIELDS) {
1233 1.5 jtc error(_("wrong number of fields on Leap line"));
1234 1.1 jtc return;
1235 1.1 jtc }
1236 1.1 jtc dayoff = 0;
1237 1.1 jtc cp = fields[LP_YEAR];
1238 1.41 christos if (sscanf(cp, scheck(cp, "%"SCNdZIC), &year) != 1) {
1239 1.25 mlelstv /*
1240 1.25 mlelstv ** Leapin' Lizards!
1241 1.25 mlelstv */
1242 1.25 mlelstv error(_("invalid leaping year"));
1243 1.25 mlelstv return;
1244 1.1 jtc }
1245 1.25 mlelstv if (!leapseen || leapmaxyear < year)
1246 1.25 mlelstv leapmaxyear = year;
1247 1.25 mlelstv if (!leapseen || leapminyear > year)
1248 1.25 mlelstv leapminyear = year;
1249 1.46.2.1 martin leapseen = true;
1250 1.1 jtc j = EPOCH_YEAR;
1251 1.1 jtc while (j != year) {
1252 1.1 jtc if (year > j) {
1253 1.1 jtc i = len_years[isleap(j)];
1254 1.1 jtc ++j;
1255 1.1 jtc } else {
1256 1.1 jtc --j;
1257 1.1 jtc i = -len_years[isleap(j)];
1258 1.1 jtc }
1259 1.41 christos dayoff = oadd(dayoff, i);
1260 1.1 jtc }
1261 1.1 jtc if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1262 1.5 jtc error(_("invalid month name"));
1263 1.1 jtc return;
1264 1.1 jtc }
1265 1.1 jtc month = lp->l_value;
1266 1.1 jtc j = TM_JANUARY;
1267 1.1 jtc while (j != month) {
1268 1.1 jtc i = len_months[isleap(year)][j];
1269 1.41 christos dayoff = oadd(dayoff, i);
1270 1.1 jtc ++j;
1271 1.1 jtc }
1272 1.1 jtc cp = fields[LP_DAY];
1273 1.1 jtc if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1274 1.1 jtc day <= 0 || day > len_months[isleap(year)][month]) {
1275 1.5 jtc error(_("invalid day of month"));
1276 1.1 jtc return;
1277 1.1 jtc }
1278 1.41 christos dayoff = oadd(dayoff, day - 1);
1279 1.34 martin if (dayoff < min_time / SECSPERDAY) {
1280 1.20 kleink error(_("time too small"));
1281 1.20 kleink return;
1282 1.20 kleink }
1283 1.34 martin if (dayoff > max_time / SECSPERDAY) {
1284 1.20 kleink error(_("time too large"));
1285 1.1 jtc return;
1286 1.1 jtc }
1287 1.46 christos t = dayoff * SECSPERDAY;
1288 1.46.2.1 martin tod = gethms(fields[LP_TIME], _("invalid time of day"), false);
1289 1.1 jtc cp = fields[LP_CORR];
1290 1.1 jtc {
1291 1.46.2.1 martin bool positive;
1292 1.46.2.1 martin int count;
1293 1.1 jtc
1294 1.1 jtc if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1295 1.46.2.1 martin positive = false;
1296 1.1 jtc count = 1;
1297 1.1 jtc } else if (strcmp(cp, "--") == 0) {
1298 1.46.2.1 martin positive = false;
1299 1.1 jtc count = 2;
1300 1.1 jtc } else if (strcmp(cp, "+") == 0) {
1301 1.46.2.1 martin positive = true;
1302 1.1 jtc count = 1;
1303 1.1 jtc } else if (strcmp(cp, "++") == 0) {
1304 1.46.2.1 martin positive = true;
1305 1.1 jtc count = 2;
1306 1.1 jtc } else {
1307 1.5 jtc error(_("illegal CORRECTION field on Leap line"));
1308 1.1 jtc return;
1309 1.1 jtc }
1310 1.1 jtc if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1311 1.25 mlelstv error(_(
1312 1.25 mlelstv "illegal Rolling/Stationary field on Leap line"
1313 1.25 mlelstv ));
1314 1.1 jtc return;
1315 1.1 jtc }
1316 1.46 christos t = tadd(t, tod);
1317 1.46 christos if (t < big_bang_time) {
1318 1.46 christos error(_("leap second precedes Big Bang"));
1319 1.46 christos return;
1320 1.46 christos }
1321 1.46 christos leapadd(t, positive, lp->l_value, count);
1322 1.1 jtc }
1323 1.1 jtc }
1324 1.1 jtc
1325 1.1 jtc static void
1326 1.31 christos inlink(char **const fields, const int nfields)
1327 1.1 jtc {
1328 1.1 jtc struct link l;
1329 1.1 jtc
1330 1.1 jtc if (nfields != LINK_FIELDS) {
1331 1.5 jtc error(_("wrong number of fields on Link line"));
1332 1.1 jtc return;
1333 1.1 jtc }
1334 1.1 jtc if (*fields[LF_FROM] == '\0') {
1335 1.5 jtc error(_("blank FROM field on Link line"));
1336 1.1 jtc return;
1337 1.1 jtc }
1338 1.1 jtc if (*fields[LF_TO] == '\0') {
1339 1.5 jtc error(_("blank TO field on Link line"));
1340 1.1 jtc return;
1341 1.1 jtc }
1342 1.1 jtc l.l_filename = filename;
1343 1.1 jtc l.l_linenum = linenum;
1344 1.1 jtc l.l_from = ecpyalloc(fields[LF_FROM]);
1345 1.1 jtc l.l_to = ecpyalloc(fields[LF_TO]);
1346 1.45 christos links = growalloc(links, sizeof *links, nlinks, &nlinks_alloc);
1347 1.1 jtc links[nlinks++] = l;
1348 1.1 jtc }
1349 1.1 jtc
1350 1.1 jtc static void
1351 1.31 christos rulesub(struct rule *const rp, const char *const loyearp,
1352 1.31 christos const char *const hiyearp, const char *const typep,
1353 1.31 christos const char *const monthp, const char *const dayp, const char *const timep)
1354 1.31 christos {
1355 1.31 christos const struct lookup * lp;
1356 1.31 christos const char * cp;
1357 1.31 christos char * dp;
1358 1.31 christos char * ep;
1359 1.1 jtc
1360 1.1 jtc if ((lp = byword(monthp, mon_names)) == NULL) {
1361 1.5 jtc error(_("invalid month name"));
1362 1.1 jtc return;
1363 1.1 jtc }
1364 1.1 jtc rp->r_month = lp->l_value;
1365 1.46.2.1 martin rp->r_todisstd = false;
1366 1.46.2.1 martin rp->r_todisgmt = false;
1367 1.1 jtc dp = ecpyalloc(timep);
1368 1.1 jtc if (*dp != '\0') {
1369 1.1 jtc ep = dp + strlen(dp) - 1;
1370 1.1 jtc switch (lowerit(*ep)) {
1371 1.1 jtc case 's': /* Standard */
1372 1.46.2.1 martin rp->r_todisstd = true;
1373 1.46.2.1 martin rp->r_todisgmt = false;
1374 1.1 jtc *ep = '\0';
1375 1.1 jtc break;
1376 1.1 jtc case 'w': /* Wall */
1377 1.46.2.1 martin rp->r_todisstd = false;
1378 1.46.2.1 martin rp->r_todisgmt = false;
1379 1.1 jtc *ep = '\0';
1380 1.7 jtc break;
1381 1.1 jtc case 'g': /* Greenwich */
1382 1.1 jtc case 'u': /* Universal */
1383 1.1 jtc case 'z': /* Zulu */
1384 1.46.2.1 martin rp->r_todisstd = true;
1385 1.46.2.1 martin rp->r_todisgmt = true;
1386 1.1 jtc *ep = '\0';
1387 1.1 jtc break;
1388 1.1 jtc }
1389 1.1 jtc }
1390 1.46.2.1 martin rp->r_tod = gethms(dp, _("invalid time of day"), false);
1391 1.31 christos free(dp);
1392 1.1 jtc /*
1393 1.1 jtc ** Year work.
1394 1.1 jtc */
1395 1.1 jtc cp = loyearp;
1396 1.1 jtc lp = byword(cp, begin_years);
1397 1.25 mlelstv rp->r_lowasnum = lp == NULL;
1398 1.25 mlelstv if (!rp->r_lowasnum) switch ((int) lp->l_value) {
1399 1.1 jtc case YR_MINIMUM:
1400 1.41 christos rp->r_loyear = ZIC_MIN;
1401 1.1 jtc break;
1402 1.1 jtc case YR_MAXIMUM:
1403 1.41 christos rp->r_loyear = ZIC_MAX;
1404 1.1 jtc break;
1405 1.1 jtc default: /* "cannot happen" */
1406 1.46.2.1 martin fprintf(stderr,
1407 1.5 jtc _("%s: panic: Invalid l_value %d\n"),
1408 1.1 jtc progname, lp->l_value);
1409 1.25 mlelstv exit(EXIT_FAILURE);
1410 1.41 christos } else if (sscanf(cp, scheck(cp, "%"SCNdZIC), &rp->r_loyear) != 1) {
1411 1.5 jtc error(_("invalid starting year"));
1412 1.1 jtc return;
1413 1.11 jtc }
1414 1.1 jtc cp = hiyearp;
1415 1.25 mlelstv lp = byword(cp, end_years);
1416 1.25 mlelstv rp->r_hiwasnum = lp == NULL;
1417 1.25 mlelstv if (!rp->r_hiwasnum) switch ((int) lp->l_value) {
1418 1.1 jtc case YR_MINIMUM:
1419 1.41 christos rp->r_hiyear = ZIC_MIN;
1420 1.1 jtc break;
1421 1.1 jtc case YR_MAXIMUM:
1422 1.41 christos rp->r_hiyear = ZIC_MAX;
1423 1.1 jtc break;
1424 1.1 jtc case YR_ONLY:
1425 1.1 jtc rp->r_hiyear = rp->r_loyear;
1426 1.1 jtc break;
1427 1.1 jtc default: /* "cannot happen" */
1428 1.46.2.1 martin fprintf(stderr,
1429 1.5 jtc _("%s: panic: Invalid l_value %d\n"),
1430 1.1 jtc progname, lp->l_value);
1431 1.25 mlelstv exit(EXIT_FAILURE);
1432 1.41 christos } else if (sscanf(cp, scheck(cp, "%"SCNdZIC), &rp->r_hiyear) != 1) {
1433 1.5 jtc error(_("invalid ending year"));
1434 1.1 jtc return;
1435 1.11 jtc }
1436 1.1 jtc if (rp->r_loyear > rp->r_hiyear) {
1437 1.5 jtc error(_("starting year greater than ending year"));
1438 1.1 jtc return;
1439 1.1 jtc }
1440 1.1 jtc if (*typep == '\0')
1441 1.1 jtc rp->r_yrtype = NULL;
1442 1.1 jtc else {
1443 1.1 jtc if (rp->r_loyear == rp->r_hiyear) {
1444 1.5 jtc error(_("typed single year"));
1445 1.1 jtc return;
1446 1.1 jtc }
1447 1.1 jtc rp->r_yrtype = ecpyalloc(typep);
1448 1.1 jtc }
1449 1.1 jtc /*
1450 1.1 jtc ** Day work.
1451 1.1 jtc ** Accept things such as:
1452 1.1 jtc ** 1
1453 1.1 jtc ** last-Sunday
1454 1.1 jtc ** Sun<=20
1455 1.1 jtc ** Sun>=7
1456 1.1 jtc */
1457 1.1 jtc dp = ecpyalloc(dayp);
1458 1.1 jtc if ((lp = byword(dp, lasts)) != NULL) {
1459 1.1 jtc rp->r_dycode = DC_DOWLEQ;
1460 1.1 jtc rp->r_wday = lp->l_value;
1461 1.1 jtc rp->r_dayofmonth = len_months[1][rp->r_month];
1462 1.1 jtc } else {
1463 1.1 jtc if ((ep = strchr(dp, '<')) != 0)
1464 1.1 jtc rp->r_dycode = DC_DOWLEQ;
1465 1.1 jtc else if ((ep = strchr(dp, '>')) != 0)
1466 1.1 jtc rp->r_dycode = DC_DOWGEQ;
1467 1.1 jtc else {
1468 1.1 jtc ep = dp;
1469 1.1 jtc rp->r_dycode = DC_DOM;
1470 1.1 jtc }
1471 1.1 jtc if (rp->r_dycode != DC_DOM) {
1472 1.1 jtc *ep++ = 0;
1473 1.1 jtc if (*ep++ != '=') {
1474 1.5 jtc error(_("invalid day of month"));
1475 1.31 christos free(dp);
1476 1.1 jtc return;
1477 1.1 jtc }
1478 1.1 jtc if ((lp = byword(dp, wday_names)) == NULL) {
1479 1.5 jtc error(_("invalid weekday name"));
1480 1.31 christos free(dp);
1481 1.1 jtc return;
1482 1.1 jtc }
1483 1.1 jtc rp->r_wday = lp->l_value;
1484 1.1 jtc }
1485 1.1 jtc if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1486 1.1 jtc rp->r_dayofmonth <= 0 ||
1487 1.1 jtc (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1488 1.5 jtc error(_("invalid day of month"));
1489 1.31 christos free(dp);
1490 1.1 jtc return;
1491 1.1 jtc }
1492 1.1 jtc }
1493 1.31 christos free(dp);
1494 1.1 jtc }
1495 1.1 jtc
1496 1.1 jtc static void
1497 1.38 christos convert(const zic_t val, char *const buf)
1498 1.1 jtc {
1499 1.31 christos int i;
1500 1.31 christos int shift;
1501 1.31 christos unsigned char *const b = (unsigned char *) buf;
1502 1.1 jtc
1503 1.1 jtc for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1504 1.31 christos b[i] = val >> shift;
1505 1.1 jtc }
1506 1.1 jtc
1507 1.1 jtc static void
1508 1.31 christos convert64(const zic_t val, char *const buf)
1509 1.25 mlelstv {
1510 1.31 christos int i;
1511 1.31 christos int shift;
1512 1.31 christos unsigned char *const b = (unsigned char *) buf;
1513 1.25 mlelstv
1514 1.25 mlelstv for (i = 0, shift = 56; i < 8; ++i, shift -= 8)
1515 1.31 christos b[i] = val >> shift;
1516 1.25 mlelstv }
1517 1.25 mlelstv
1518 1.25 mlelstv static void
1519 1.38 christos puttzcode(const zic_t val, FILE *const fp)
1520 1.1 jtc {
1521 1.1 jtc char buf[4];
1522 1.1 jtc
1523 1.1 jtc convert(val, buf);
1524 1.31 christos (void) fwrite(buf, sizeof buf, (size_t) 1, fp);
1525 1.1 jtc }
1526 1.1 jtc
1527 1.25 mlelstv static void
1528 1.31 christos puttzcode64(const zic_t val, FILE *const fp)
1529 1.25 mlelstv {
1530 1.25 mlelstv char buf[8];
1531 1.25 mlelstv
1532 1.25 mlelstv convert64(val, buf);
1533 1.31 christos (void) fwrite(buf, sizeof buf, (size_t) 1, fp);
1534 1.25 mlelstv }
1535 1.25 mlelstv
1536 1.5 jtc static int
1537 1.31 christos atcomp(const void *avp, const void *bvp)
1538 1.5 jtc {
1539 1.25 mlelstv const zic_t a = ((const struct attype *) avp)->at;
1540 1.25 mlelstv const zic_t b = ((const struct attype *) bvp)->at;
1541 1.25 mlelstv
1542 1.25 mlelstv return (a < b) ? -1 : (a > b);
1543 1.25 mlelstv }
1544 1.25 mlelstv
1545 1.46.2.1 martin static bool
1546 1.31 christos is32(const zic_t x)
1547 1.25 mlelstv {
1548 1.25 mlelstv return INT32_MIN <= x && x <= INT32_MAX;
1549 1.5 jtc }
1550 1.5 jtc
1551 1.1 jtc static void
1552 1.43 christos writezone(const char *const name, const char *const string, char version)
1553 1.31 christos {
1554 1.31 christos FILE * fp;
1555 1.31 christos int i, j;
1556 1.31 christos int leapcnt32, leapi32;
1557 1.31 christos int timecnt32, timei32;
1558 1.31 christos int pass;
1559 1.25 mlelstv static char * fullname;
1560 1.25 mlelstv static const struct tzhead tzh0;
1561 1.25 mlelstv static struct tzhead tzh;
1562 1.45 christos zic_t *ats = emalloc(size_product(timecnt, sizeof *ats + 1));
1563 1.45 christos void *typesptr = ats + timecnt;
1564 1.45 christos unsigned char *types = typesptr;
1565 1.5 jtc
1566 1.46.2.1 martin namecheck(name);
1567 1.5 jtc /*
1568 1.5 jtc ** Sort.
1569 1.5 jtc */
1570 1.5 jtc if (timecnt > 1)
1571 1.31 christos (void) qsort(attypes, (size_t) timecnt, sizeof *attypes,
1572 1.31 christos atcomp);
1573 1.5 jtc /*
1574 1.5 jtc ** Optimize.
1575 1.5 jtc */
1576 1.5 jtc {
1577 1.5 jtc int fromi;
1578 1.5 jtc int toi;
1579 1.1 jtc
1580 1.5 jtc toi = 0;
1581 1.5 jtc fromi = 0;
1582 1.46 christos while (fromi < timecnt && attypes[fromi].at < big_bang_time)
1583 1.7 jtc ++fromi;
1584 1.5 jtc for ( ; fromi < timecnt; ++fromi) {
1585 1.45 christos if (toi > 1 && ((attypes[fromi].at +
1586 1.25 mlelstv gmtoffs[attypes[toi - 1].type]) <=
1587 1.45 christos (attypes[toi - 1].at +
1588 1.45 christos gmtoffs[attypes[toi - 2].type]))) {
1589 1.25 mlelstv attypes[toi - 1].type =
1590 1.25 mlelstv attypes[fromi].type;
1591 1.25 mlelstv continue;
1592 1.5 jtc }
1593 1.5 jtc if (toi == 0 ||
1594 1.5 jtc attypes[toi - 1].type != attypes[fromi].type)
1595 1.5 jtc attypes[toi++] = attypes[fromi];
1596 1.5 jtc }
1597 1.5 jtc timecnt = toi;
1598 1.5 jtc }
1599 1.45 christos if (noise && timecnt > 1200)
1600 1.45 christos warning(_("pre-2014 clients may mishandle"
1601 1.45 christos " more than 1200 transition times"));
1602 1.5 jtc /*
1603 1.5 jtc ** Transfer.
1604 1.5 jtc */
1605 1.5 jtc for (i = 0; i < timecnt; ++i) {
1606 1.5 jtc ats[i] = attypes[i].at;
1607 1.5 jtc types[i] = attypes[i].type;
1608 1.5 jtc }
1609 1.25 mlelstv /*
1610 1.25 mlelstv ** Correct for leap seconds.
1611 1.25 mlelstv */
1612 1.25 mlelstv for (i = 0; i < timecnt; ++i) {
1613 1.25 mlelstv j = leapcnt;
1614 1.25 mlelstv while (--j >= 0)
1615 1.25 mlelstv if (ats[i] > trans[j] - corr[j]) {
1616 1.25 mlelstv ats[i] = tadd(ats[i], corr[j]);
1617 1.25 mlelstv break;
1618 1.25 mlelstv }
1619 1.25 mlelstv }
1620 1.25 mlelstv /*
1621 1.25 mlelstv ** Figure out 32-bit-limited starts and counts.
1622 1.25 mlelstv */
1623 1.25 mlelstv timecnt32 = timecnt;
1624 1.25 mlelstv timei32 = 0;
1625 1.25 mlelstv leapcnt32 = leapcnt;
1626 1.25 mlelstv leapi32 = 0;
1627 1.25 mlelstv while (timecnt32 > 0 && !is32(ats[timecnt32 - 1]))
1628 1.25 mlelstv --timecnt32;
1629 1.25 mlelstv while (timecnt32 > 0 && !is32(ats[timei32])) {
1630 1.25 mlelstv --timecnt32;
1631 1.25 mlelstv ++timei32;
1632 1.25 mlelstv }
1633 1.45 christos /*
1634 1.46.2.1 martin ** Output an INT32_MIN "transition" if appropriate; see below.
1635 1.45 christos */
1636 1.45 christos if (timei32 > 0 && ats[timei32] > INT32_MIN) {
1637 1.45 christos --timei32;
1638 1.45 christos ++timecnt32;
1639 1.45 christos }
1640 1.25 mlelstv while (leapcnt32 > 0 && !is32(trans[leapcnt32 - 1]))
1641 1.25 mlelstv --leapcnt32;
1642 1.25 mlelstv while (leapcnt32 > 0 && !is32(trans[leapi32])) {
1643 1.25 mlelstv --leapcnt32;
1644 1.25 mlelstv ++leapi32;
1645 1.25 mlelstv }
1646 1.1 jtc fullname = erealloc(fullname,
1647 1.31 christos strlen(directory) + 1 + strlen(name) + 1);
1648 1.7 jtc (void) sprintf(fullname, "%s/%s", directory, name); /* XXX: sprintf is safe */
1649 1.7 jtc /*
1650 1.7 jtc ** Remove old file, if any, to snap links.
1651 1.7 jtc */
1652 1.46.2.1 martin if (itsdir(fullname) <= 0 && remove(fullname) != 0 && errno != ENOENT) {
1653 1.7 jtc const char *e = strerror(errno);
1654 1.7 jtc
1655 1.7 jtc (void) fprintf(stderr, _("%s: Can't remove %s: %s\n"),
1656 1.7 jtc progname, fullname, e);
1657 1.25 mlelstv exit(EXIT_FAILURE);
1658 1.7 jtc }
1659 1.1 jtc if ((fp = fopen(fullname, "wb")) == NULL) {
1660 1.46.2.1 martin if (! mkdirs(fullname))
1661 1.25 mlelstv exit(EXIT_FAILURE);
1662 1.1 jtc if ((fp = fopen(fullname, "wb")) == NULL) {
1663 1.5 jtc const char *e = strerror(errno);
1664 1.7 jtc
1665 1.5 jtc (void) fprintf(stderr, _("%s: Can't create %s: %s\n"),
1666 1.5 jtc progname, fullname, e);
1667 1.25 mlelstv exit(EXIT_FAILURE);
1668 1.1 jtc }
1669 1.1 jtc }
1670 1.25 mlelstv for (pass = 1; pass <= 2; ++pass) {
1671 1.31 christos int thistimei, thistimecnt;
1672 1.31 christos int thisleapi, thisleapcnt;
1673 1.31 christos int thistimelim, thisleaplim;
1674 1.45 christos int writetype[TZ_MAX_TYPES];
1675 1.25 mlelstv int typemap[TZ_MAX_TYPES];
1676 1.31 christos int thistypecnt;
1677 1.25 mlelstv char thischars[TZ_MAX_CHARS];
1678 1.25 mlelstv char thischarcnt;
1679 1.46.2.1 martin int indmap[TZ_MAX_CHARS];
1680 1.25 mlelstv
1681 1.25 mlelstv if (pass == 1) {
1682 1.25 mlelstv thistimei = timei32;
1683 1.25 mlelstv thistimecnt = timecnt32;
1684 1.25 mlelstv thisleapi = leapi32;
1685 1.25 mlelstv thisleapcnt = leapcnt32;
1686 1.25 mlelstv } else {
1687 1.25 mlelstv thistimei = 0;
1688 1.25 mlelstv thistimecnt = timecnt;
1689 1.25 mlelstv thisleapi = 0;
1690 1.25 mlelstv thisleapcnt = leapcnt;
1691 1.25 mlelstv }
1692 1.25 mlelstv thistimelim = thistimei + thistimecnt;
1693 1.25 mlelstv thisleaplim = thisleapi + thisleapcnt;
1694 1.45 christos for (i = 0; i < typecnt; ++i)
1695 1.25 mlelstv writetype[i] = thistimecnt == timecnt;
1696 1.25 mlelstv if (thistimecnt == 0) {
1697 1.25 mlelstv /*
1698 1.25 mlelstv ** No transition times fall in the current
1699 1.25 mlelstv ** (32- or 64-bit) window.
1700 1.25 mlelstv */
1701 1.25 mlelstv if (typecnt != 0)
1702 1.46.2.1 martin writetype[typecnt - 1] = true;
1703 1.25 mlelstv } else {
1704 1.25 mlelstv for (i = thistimei - 1; i < thistimelim; ++i)
1705 1.25 mlelstv if (i >= 0)
1706 1.46.2.1 martin writetype[types[i]] = true;
1707 1.25 mlelstv /*
1708 1.25 mlelstv ** For America/Godthab and Antarctica/Palmer
1709 1.25 mlelstv */
1710 1.25 mlelstv if (thistimei == 0)
1711 1.46.2.1 martin writetype[0] = true;
1712 1.25 mlelstv }
1713 1.29 christos #ifndef LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH
1714 1.29 christos /*
1715 1.29 christos ** For some pre-2011 systems: if the last-to-be-written
1716 1.29 christos ** standard (or daylight) type has an offset different from the
1717 1.29 christos ** most recently used offset,
1718 1.29 christos ** append an (unused) copy of the most recently used type
1719 1.29 christos ** (to help get global "altzone" and "timezone" variables
1720 1.29 christos ** set correctly).
1721 1.29 christos */
1722 1.29 christos {
1723 1.31 christos int mrudst, mrustd, hidst, histd, type;
1724 1.29 christos
1725 1.29 christos hidst = histd = mrudst = mrustd = -1;
1726 1.42 christos for (i = thistimei; i < thistimelim; ++i) {
1727 1.42 christos if (i < 0)
1728 1.42 christos continue;
1729 1.29 christos if (isdsts[types[i]])
1730 1.29 christos mrudst = types[i];
1731 1.29 christos else mrustd = types[i];
1732 1.42 christos }
1733 1.29 christos for (i = 0; i < typecnt; ++i)
1734 1.29 christos if (writetype[i]) {
1735 1.29 christos if (isdsts[i])
1736 1.29 christos hidst = i;
1737 1.29 christos else histd = i;
1738 1.29 christos }
1739 1.29 christos if (hidst >= 0 && mrudst >= 0 && hidst != mrudst &&
1740 1.29 christos gmtoffs[hidst] != gmtoffs[mrudst]) {
1741 1.29 christos isdsts[mrudst] = -1;
1742 1.29 christos type = addtype(gmtoffs[mrudst],
1743 1.29 christos &chars[abbrinds[mrudst]],
1744 1.46.2.1 martin true,
1745 1.29 christos ttisstds[mrudst],
1746 1.29 christos ttisgmts[mrudst]);
1747 1.46.2.1 martin isdsts[mrudst] = 1;
1748 1.46.2.1 martin writetype[type] = true;
1749 1.29 christos }
1750 1.29 christos if (histd >= 0 && mrustd >= 0 && histd != mrustd &&
1751 1.29 christos gmtoffs[histd] != gmtoffs[mrustd]) {
1752 1.29 christos isdsts[mrustd] = -1;
1753 1.29 christos type = addtype(gmtoffs[mrustd],
1754 1.29 christos &chars[abbrinds[mrustd]],
1755 1.46.2.1 martin false,
1756 1.29 christos ttisstds[mrustd],
1757 1.29 christos ttisgmts[mrustd]);
1758 1.46.2.1 martin isdsts[mrustd] = 0;
1759 1.46.2.1 martin writetype[type] = true;
1760 1.29 christos }
1761 1.29 christos }
1762 1.29 christos #endif /* !defined LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH */
1763 1.25 mlelstv thistypecnt = 0;
1764 1.25 mlelstv for (i = 0; i < typecnt; ++i)
1765 1.45 christos typemap[i] = writetype[i] ? thistypecnt++ : -1;
1766 1.36 christos for (i = 0; i < (int)(sizeof indmap / sizeof indmap[0]); ++i)
1767 1.25 mlelstv indmap[i] = -1;
1768 1.25 mlelstv thischarcnt = 0;
1769 1.25 mlelstv for (i = 0; i < typecnt; ++i) {
1770 1.31 christos char * thisabbr;
1771 1.25 mlelstv
1772 1.25 mlelstv if (!writetype[i])
1773 1.25 mlelstv continue;
1774 1.25 mlelstv if (indmap[abbrinds[i]] >= 0)
1775 1.25 mlelstv continue;
1776 1.25 mlelstv thisabbr = &chars[abbrinds[i]];
1777 1.25 mlelstv for (j = 0; j < thischarcnt; ++j)
1778 1.25 mlelstv if (strcmp(&thischars[j], thisabbr) == 0)
1779 1.25 mlelstv break;
1780 1.25 mlelstv if (j == thischarcnt) {
1781 1.25 mlelstv (void) strcpy(&thischars[(int) thischarcnt],
1782 1.25 mlelstv thisabbr);
1783 1.25 mlelstv thischarcnt += strlen(thisabbr) + 1;
1784 1.25 mlelstv }
1785 1.25 mlelstv indmap[abbrinds[i]] = j;
1786 1.25 mlelstv }
1787 1.31 christos #define DO(field) (void) fwrite(tzh.field, \
1788 1.31 christos sizeof tzh.field, (size_t) 1, fp)
1789 1.25 mlelstv tzh = tzh0;
1790 1.25 mlelstv (void) strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1791 1.43 christos tzh.tzh_version[0] = version;
1792 1.41 christos convert(thistypecnt, tzh.tzh_ttisgmtcnt);
1793 1.41 christos convert(thistypecnt, tzh.tzh_ttisstdcnt);
1794 1.41 christos convert(thisleapcnt, tzh.tzh_leapcnt);
1795 1.41 christos convert(thistimecnt, tzh.tzh_timecnt);
1796 1.41 christos convert(thistypecnt, tzh.tzh_typecnt);
1797 1.41 christos convert(thischarcnt, tzh.tzh_charcnt);
1798 1.25 mlelstv DO(tzh_magic);
1799 1.25 mlelstv DO(tzh_version);
1800 1.25 mlelstv DO(tzh_reserved);
1801 1.25 mlelstv DO(tzh_ttisgmtcnt);
1802 1.25 mlelstv DO(tzh_ttisstdcnt);
1803 1.25 mlelstv DO(tzh_leapcnt);
1804 1.25 mlelstv DO(tzh_timecnt);
1805 1.25 mlelstv DO(tzh_typecnt);
1806 1.25 mlelstv DO(tzh_charcnt);
1807 1.1 jtc #undef DO
1808 1.25 mlelstv for (i = thistimei; i < thistimelim; ++i)
1809 1.25 mlelstv if (pass == 1)
1810 1.45 christos /*
1811 1.45 christos ** Output an INT32_MIN "transition"
1812 1.46.2.1 martin ** if appropriate; see above.
1813 1.45 christos */
1814 1.45 christos puttzcode(((ats[i] < INT32_MIN) ?
1815 1.45 christos INT32_MIN : ats[i]), fp);
1816 1.25 mlelstv else puttzcode64(ats[i], fp);
1817 1.25 mlelstv for (i = thistimei; i < thistimelim; ++i) {
1818 1.25 mlelstv unsigned char uc;
1819 1.25 mlelstv
1820 1.25 mlelstv uc = typemap[types[i]];
1821 1.31 christos (void) fwrite(&uc, sizeof uc, (size_t) 1, fp);
1822 1.25 mlelstv }
1823 1.25 mlelstv for (i = 0; i < typecnt; ++i)
1824 1.25 mlelstv if (writetype[i]) {
1825 1.25 mlelstv puttzcode(gmtoffs[i], fp);
1826 1.25 mlelstv (void) putc(isdsts[i], fp);
1827 1.25 mlelstv (void) putc((unsigned char) indmap[abbrinds[i]], fp);
1828 1.1 jtc }
1829 1.25 mlelstv if (thischarcnt != 0)
1830 1.31 christos (void) fwrite(thischars, sizeof thischars[0],
1831 1.25 mlelstv (size_t) thischarcnt, fp);
1832 1.25 mlelstv for (i = thisleapi; i < thisleaplim; ++i) {
1833 1.31 christos zic_t todo;
1834 1.25 mlelstv
1835 1.25 mlelstv if (roll[i]) {
1836 1.25 mlelstv if (timecnt == 0 || trans[i] < ats[0]) {
1837 1.25 mlelstv j = 0;
1838 1.25 mlelstv while (isdsts[j])
1839 1.25 mlelstv if (++j >= typecnt) {
1840 1.25 mlelstv j = 0;
1841 1.25 mlelstv break;
1842 1.25 mlelstv }
1843 1.25 mlelstv } else {
1844 1.25 mlelstv j = 1;
1845 1.25 mlelstv while (j < timecnt &&
1846 1.25 mlelstv trans[i] >= ats[j])
1847 1.25 mlelstv ++j;
1848 1.25 mlelstv j = types[j - 1];
1849 1.25 mlelstv }
1850 1.25 mlelstv todo = tadd(trans[i], -gmtoffs[j]);
1851 1.25 mlelstv } else todo = trans[i];
1852 1.25 mlelstv if (pass == 1)
1853 1.41 christos puttzcode(todo, fp);
1854 1.25 mlelstv else puttzcode64(todo, fp);
1855 1.25 mlelstv puttzcode(corr[i], fp);
1856 1.25 mlelstv }
1857 1.25 mlelstv for (i = 0; i < typecnt; ++i)
1858 1.25 mlelstv if (writetype[i])
1859 1.25 mlelstv (void) putc(ttisstds[i], fp);
1860 1.25 mlelstv for (i = 0; i < typecnt; ++i)
1861 1.25 mlelstv if (writetype[i])
1862 1.25 mlelstv (void) putc(ttisgmts[i], fp);
1863 1.1 jtc }
1864 1.25 mlelstv (void) fprintf(fp, "\n%s\n", string);
1865 1.46.2.1 martin close_file(fp, fullname);
1866 1.45 christos free(ats);
1867 1.1 jtc }
1868 1.1 jtc
1869 1.1 jtc static void
1870 1.31 christos doabbr(char *const abbr, const int abbrlen, const char *const format,
1871 1.46.2.1 martin const char *const letters, bool isdst, bool doquotes)
1872 1.31 christos {
1873 1.31 christos char * cp;
1874 1.31 christos char * slashp;
1875 1.31 christos int len;
1876 1.25 mlelstv
1877 1.25 mlelstv slashp = strchr(format, '/');
1878 1.25 mlelstv if (slashp == NULL) {
1879 1.1 jtc if (letters == NULL)
1880 1.25 mlelstv (void) strlcpy(abbr, format, abbrlen);
1881 1.25 mlelstv else (void) snprintf(abbr, abbrlen, format, letters);
1882 1.25 mlelstv } else if (isdst) {
1883 1.25 mlelstv (void) strlcpy(abbr, slashp + 1, abbrlen);
1884 1.25 mlelstv } else {
1885 1.46.2.1 martin (void) memcpy(abbr, format, slashp - format);
1886 1.25 mlelstv abbr[slashp - format] = '\0';
1887 1.25 mlelstv }
1888 1.25 mlelstv if (!doquotes)
1889 1.25 mlelstv return;
1890 1.46.2.1 martin for (cp = abbr; is_alpha(*cp); cp++)
1891 1.46.2.1 martin continue;
1892 1.25 mlelstv len = strlen(abbr);
1893 1.25 mlelstv if (len > 0 && *cp == '\0')
1894 1.25 mlelstv return;
1895 1.25 mlelstv abbr[len + 2] = '\0';
1896 1.25 mlelstv abbr[len + 1] = '>';
1897 1.25 mlelstv for ( ; len > 0; --len)
1898 1.25 mlelstv abbr[len] = abbr[len - 1];
1899 1.25 mlelstv abbr[0] = '<';
1900 1.25 mlelstv }
1901 1.25 mlelstv
1902 1.25 mlelstv static void
1903 1.41 christos updateminmax(const zic_t x)
1904 1.25 mlelstv {
1905 1.25 mlelstv if (min_year > x)
1906 1.25 mlelstv min_year = x;
1907 1.25 mlelstv if (max_year < x)
1908 1.25 mlelstv max_year = x;
1909 1.25 mlelstv }
1910 1.25 mlelstv
1911 1.46.2.1 martin static bool
1912 1.38 christos stringoffset(char *result, zic_t offset)
1913 1.31 christos {
1914 1.31 christos int hours;
1915 1.31 christos int minutes;
1916 1.31 christos int seconds;
1917 1.25 mlelstv
1918 1.25 mlelstv result[0] = '\0';
1919 1.25 mlelstv if (offset < 0) {
1920 1.46.2.1 martin strcpy(result, "-");
1921 1.25 mlelstv offset = -offset;
1922 1.25 mlelstv }
1923 1.25 mlelstv seconds = offset % SECSPERMIN;
1924 1.25 mlelstv offset /= SECSPERMIN;
1925 1.25 mlelstv minutes = offset % MINSPERHOUR;
1926 1.25 mlelstv offset /= MINSPERHOUR;
1927 1.25 mlelstv hours = offset;
1928 1.43 christos if (hours >= HOURSPERDAY * DAYSPERWEEK) {
1929 1.25 mlelstv result[0] = '\0';
1930 1.46.2.1 martin return false;
1931 1.25 mlelstv }
1932 1.46.2.1 martin sprintf(end(result), "%d", hours);
1933 1.25 mlelstv if (minutes != 0 || seconds != 0) {
1934 1.46.2.1 martin sprintf(end(result), ":%02d", minutes);
1935 1.25 mlelstv if (seconds != 0)
1936 1.46.2.1 martin sprintf(end(result), ":%02d", seconds);
1937 1.25 mlelstv }
1938 1.46.2.1 martin return true;
1939 1.25 mlelstv }
1940 1.25 mlelstv
1941 1.25 mlelstv static int
1942 1.38 christos stringrule(char *result, const struct rule *const rp, const zic_t dstoff,
1943 1.38 christos const zic_t gmtoff)
1944 1.25 mlelstv {
1945 1.43 christos zic_t tod = rp->r_tod;
1946 1.43 christos int compat = 0;
1947 1.25 mlelstv
1948 1.25 mlelstv result = end(result);
1949 1.25 mlelstv if (rp->r_dycode == DC_DOM) {
1950 1.31 christos int month, total;
1951 1.25 mlelstv
1952 1.25 mlelstv if (rp->r_dayofmonth == 29 && rp->r_month == TM_FEBRUARY)
1953 1.25 mlelstv return -1;
1954 1.25 mlelstv total = 0;
1955 1.25 mlelstv for (month = 0; month < rp->r_month; ++month)
1956 1.25 mlelstv total += len_months[0][month];
1957 1.43 christos /* Omit the "J" in Jan and Feb, as that's shorter. */
1958 1.43 christos if (rp->r_month <= 1)
1959 1.46.2.1 martin sprintf(result, "%d", total + rp->r_dayofmonth - 1);
1960 1.43 christos else
1961 1.46.2.1 martin sprintf(result, "J%d", total + rp->r_dayofmonth);
1962 1.25 mlelstv } else {
1963 1.31 christos int week;
1964 1.43 christos int wday = rp->r_wday;
1965 1.43 christos int wdayoff;
1966 1.25 mlelstv
1967 1.25 mlelstv if (rp->r_dycode == DC_DOWGEQ) {
1968 1.43 christos wdayoff = (rp->r_dayofmonth - 1) % DAYSPERWEEK;
1969 1.43 christos if (wdayoff)
1970 1.43 christos compat = 2013;
1971 1.43 christos wday -= wdayoff;
1972 1.43 christos tod += wdayoff * SECSPERDAY;
1973 1.43 christos week = 1 + (rp->r_dayofmonth - 1) / DAYSPERWEEK;
1974 1.25 mlelstv } else if (rp->r_dycode == DC_DOWLEQ) {
1975 1.25 mlelstv if (rp->r_dayofmonth == len_months[1][rp->r_month])
1976 1.25 mlelstv week = 5;
1977 1.25 mlelstv else {
1978 1.43 christos wdayoff = rp->r_dayofmonth % DAYSPERWEEK;
1979 1.43 christos if (wdayoff)
1980 1.43 christos compat = 2013;
1981 1.43 christos wday -= wdayoff;
1982 1.43 christos tod += wdayoff * SECSPERDAY;
1983 1.29 christos week = rp->r_dayofmonth / DAYSPERWEEK;
1984 1.25 mlelstv }
1985 1.25 mlelstv } else return -1; /* "cannot happen" */
1986 1.43 christos if (wday < 0)
1987 1.43 christos wday += DAYSPERWEEK;
1988 1.46.2.1 martin sprintf(result, "M%d.%d.%d",
1989 1.43 christos rp->r_month + 1, week, wday);
1990 1.25 mlelstv }
1991 1.25 mlelstv if (rp->r_todisgmt)
1992 1.25 mlelstv tod += gmtoff;
1993 1.25 mlelstv if (rp->r_todisstd && rp->r_stdoff == 0)
1994 1.25 mlelstv tod += dstoff;
1995 1.25 mlelstv if (tod != 2 * SECSPERMIN * MINSPERHOUR) {
1996 1.46.2.1 martin strcat(result, "/");
1997 1.46.2.1 martin if (! stringoffset(end(result), tod))
1998 1.25 mlelstv return -1;
1999 1.43 christos if (tod < 0) {
2000 1.43 christos if (compat < 2013)
2001 1.43 christos compat = 2013;
2002 1.43 christos } else if (SECSPERDAY <= tod) {
2003 1.43 christos if (compat < 1994)
2004 1.43 christos compat = 1994;
2005 1.43 christos }
2006 1.25 mlelstv }
2007 1.43 christos return compat;
2008 1.43 christos }
2009 1.43 christos
2010 1.43 christos static int
2011 1.43 christos rule_cmp(struct rule const *a, struct rule const *b)
2012 1.43 christos {
2013 1.43 christos if (!a)
2014 1.43 christos return -!!b;
2015 1.43 christos if (!b)
2016 1.43 christos return 1;
2017 1.43 christos if (a->r_hiyear != b->r_hiyear)
2018 1.43 christos return a->r_hiyear < b->r_hiyear ? -1 : 1;
2019 1.43 christos if (a->r_month - b->r_month != 0)
2020 1.43 christos return a->r_month - b->r_month;
2021 1.43 christos return a->r_dayofmonth - b->r_dayofmonth;
2022 1.25 mlelstv }
2023 1.25 mlelstv
2024 1.43 christos enum { YEAR_BY_YEAR_ZONE = 1 };
2025 1.43 christos
2026 1.43 christos static int
2027 1.31 christos stringzone(char *result, const int resultlen, const struct zone *const zpfirst,
2028 1.31 christos const int zonecount)
2029 1.31 christos {
2030 1.31 christos const struct zone * zp;
2031 1.31 christos struct rule * rp;
2032 1.31 christos struct rule * stdrp;
2033 1.31 christos struct rule * dstrp;
2034 1.31 christos int i;
2035 1.31 christos const char * abbrvar;
2036 1.43 christos int compat = 0;
2037 1.43 christos int c;
2038 1.43 christos struct rule stdr, dstr;
2039 1.25 mlelstv
2040 1.25 mlelstv result[0] = '\0';
2041 1.25 mlelstv zp = zpfirst + zonecount - 1;
2042 1.25 mlelstv stdrp = dstrp = NULL;
2043 1.25 mlelstv for (i = 0; i < zp->z_nrules; ++i) {
2044 1.25 mlelstv rp = &zp->z_rules[i];
2045 1.41 christos if (rp->r_hiwasnum || rp->r_hiyear != ZIC_MAX)
2046 1.25 mlelstv continue;
2047 1.25 mlelstv if (rp->r_yrtype != NULL)
2048 1.25 mlelstv continue;
2049 1.25 mlelstv if (rp->r_stdoff == 0) {
2050 1.25 mlelstv if (stdrp == NULL)
2051 1.25 mlelstv stdrp = rp;
2052 1.43 christos else return -1;
2053 1.25 mlelstv } else {
2054 1.25 mlelstv if (dstrp == NULL)
2055 1.25 mlelstv dstrp = rp;
2056 1.43 christos else return -1;
2057 1.25 mlelstv }
2058 1.25 mlelstv }
2059 1.25 mlelstv if (stdrp == NULL && dstrp == NULL) {
2060 1.25 mlelstv /*
2061 1.25 mlelstv ** There are no rules running through "max".
2062 1.43 christos ** Find the latest std rule in stdabbrrp
2063 1.43 christos ** and latest rule of any type in stdrp.
2064 1.25 mlelstv */
2065 1.43 christos struct rule *stdabbrrp = NULL;
2066 1.25 mlelstv for (i = 0; i < zp->z_nrules; ++i) {
2067 1.25 mlelstv rp = &zp->z_rules[i];
2068 1.43 christos if (rp->r_stdoff == 0 && rule_cmp(stdabbrrp, rp) < 0)
2069 1.43 christos stdabbrrp = rp;
2070 1.43 christos if (rule_cmp(stdrp, rp) < 0)
2071 1.43 christos stdrp = rp;
2072 1.25 mlelstv }
2073 1.25 mlelstv /*
2074 1.25 mlelstv ** Horrid special case: if year is 2037,
2075 1.25 mlelstv ** presume this is a zone handled on a year-by-year basis;
2076 1.25 mlelstv ** do not try to apply a rule to the zone.
2077 1.25 mlelstv */
2078 1.25 mlelstv if (stdrp != NULL && stdrp->r_hiyear == 2037)
2079 1.43 christos return YEAR_BY_YEAR_ZONE;
2080 1.43 christos
2081 1.43 christos if (stdrp != NULL && stdrp->r_stdoff != 0) {
2082 1.43 christos /* Perpetual DST. */
2083 1.43 christos dstr.r_month = TM_JANUARY;
2084 1.43 christos dstr.r_dycode = DC_DOM;
2085 1.43 christos dstr.r_dayofmonth = 1;
2086 1.43 christos dstr.r_tod = 0;
2087 1.46.2.1 martin dstr.r_todisstd = dstr.r_todisgmt = false;
2088 1.43 christos dstr.r_stdoff = stdrp->r_stdoff;
2089 1.43 christos dstr.r_abbrvar = stdrp->r_abbrvar;
2090 1.43 christos stdr.r_month = TM_DECEMBER;
2091 1.43 christos stdr.r_dycode = DC_DOM;
2092 1.43 christos stdr.r_dayofmonth = 31;
2093 1.43 christos stdr.r_tod = SECSPERDAY + stdrp->r_stdoff;
2094 1.46.2.1 martin stdr.r_todisstd = stdr.r_todisgmt = false;
2095 1.43 christos stdr.r_stdoff = 0;
2096 1.43 christos stdr.r_abbrvar
2097 1.43 christos = (stdabbrrp ? stdabbrrp->r_abbrvar : "");
2098 1.43 christos dstrp = &dstr;
2099 1.43 christos stdrp = &stdr;
2100 1.43 christos }
2101 1.25 mlelstv }
2102 1.25 mlelstv if (stdrp == NULL && (zp->z_nrules != 0 || zp->z_stdoff != 0))
2103 1.43 christos return -1;
2104 1.25 mlelstv abbrvar = (stdrp == NULL) ? "" : stdrp->r_abbrvar;
2105 1.46.2.1 martin doabbr(result, resultlen, zp->z_format, abbrvar, false, true);
2106 1.46.2.1 martin if (! stringoffset(end(result), -zp->z_gmtoff)) {
2107 1.25 mlelstv result[0] = '\0';
2108 1.43 christos return -1;
2109 1.25 mlelstv }
2110 1.25 mlelstv if (dstrp == NULL)
2111 1.43 christos return compat;
2112 1.25 mlelstv doabbr(end(result), resultlen - strlen(result),
2113 1.46.2.1 martin zp->z_format, dstrp->r_abbrvar, true, true);
2114 1.25 mlelstv if (dstrp->r_stdoff != SECSPERMIN * MINSPERHOUR)
2115 1.46.2.1 martin if (! stringoffset(end(result),
2116 1.46.2.1 martin -(zp->z_gmtoff + dstrp->r_stdoff))) {
2117 1.46.2.1 martin result[0] = '\0';
2118 1.46.2.1 martin return -1;
2119 1.25 mlelstv }
2120 1.25 mlelstv (void) strcat(result, ",");
2121 1.43 christos c = stringrule(result, dstrp, dstrp->r_stdoff, zp->z_gmtoff);
2122 1.43 christos if (c < 0) {
2123 1.25 mlelstv result[0] = '\0';
2124 1.43 christos return -1;
2125 1.25 mlelstv }
2126 1.43 christos if (compat < c)
2127 1.43 christos compat = c;
2128 1.46.2.1 martin strcat(result, ",");
2129 1.43 christos c = stringrule(result, stdrp, dstrp->r_stdoff, zp->z_gmtoff);
2130 1.43 christos if (c < 0) {
2131 1.25 mlelstv result[0] = '\0';
2132 1.43 christos return -1;
2133 1.1 jtc }
2134 1.43 christos if (compat < c)
2135 1.43 christos compat = c;
2136 1.43 christos return compat;
2137 1.1 jtc }
2138 1.1 jtc
2139 1.1 jtc static void
2140 1.31 christos outzone(const struct zone *const zpfirst, const int zonecount)
2141 1.31 christos {
2142 1.31 christos const struct zone * zp;
2143 1.31 christos struct rule * rp;
2144 1.31 christos int i, j;
2145 1.46.2.1 martin bool usestart, useuntil;
2146 1.31 christos zic_t starttime, untiltime;
2147 1.38 christos zic_t gmtoff;
2148 1.38 christos zic_t stdoff;
2149 1.41 christos zic_t year;
2150 1.38 christos zic_t startoff;
2151 1.46.2.1 martin bool startttisstd;
2152 1.46.2.1 martin bool startttisgmt;
2153 1.31 christos int type;
2154 1.31 christos char * startbuf;
2155 1.31 christos char * ab;
2156 1.31 christos char * envvar;
2157 1.36 christos size_t max_abbr_len;
2158 1.36 christos size_t max_envvar_len;
2159 1.46.2.1 martin bool prodstic; /* all rules are min to max */
2160 1.43 christos int compat;
2161 1.46.2.1 martin bool do_extend;
2162 1.43 christos int version;
2163 1.25 mlelstv
2164 1.25 mlelstv max_abbr_len = 2 + max_format_len + max_abbrvar_len;
2165 1.25 mlelstv max_envvar_len = 2 * max_abbr_len + 5 * 9;
2166 1.25 mlelstv startbuf = emalloc(max_abbr_len + 1);
2167 1.25 mlelstv ab = emalloc(max_abbr_len + 1);
2168 1.25 mlelstv envvar = emalloc(max_envvar_len + 1);
2169 1.1 jtc INITIALIZE(untiltime);
2170 1.1 jtc INITIALIZE(starttime);
2171 1.1 jtc /*
2172 1.1 jtc ** Now. . .finally. . .generate some useful data!
2173 1.1 jtc */
2174 1.1 jtc timecnt = 0;
2175 1.1 jtc typecnt = 0;
2176 1.1 jtc charcnt = 0;
2177 1.29 christos prodstic = zonecount == 1;
2178 1.1 jtc /*
2179 1.25 mlelstv ** Thanks to Earl Chew
2180 1.1 jtc ** for noting the need to unconditionally initialize startttisstd.
2181 1.1 jtc */
2182 1.46.2.1 martin startttisstd = false;
2183 1.46.2.1 martin startttisgmt = false;
2184 1.25 mlelstv min_year = max_year = EPOCH_YEAR;
2185 1.25 mlelstv if (leapseen) {
2186 1.25 mlelstv updateminmax(leapminyear);
2187 1.41 christos updateminmax(leapmaxyear + (leapmaxyear < ZIC_MAX));
2188 1.25 mlelstv }
2189 1.25 mlelstv for (i = 0; i < zonecount; ++i) {
2190 1.25 mlelstv zp = &zpfirst[i];
2191 1.25 mlelstv if (i < zonecount - 1)
2192 1.25 mlelstv updateminmax(zp->z_untilrule.r_loyear);
2193 1.25 mlelstv for (j = 0; j < zp->z_nrules; ++j) {
2194 1.25 mlelstv rp = &zp->z_rules[j];
2195 1.25 mlelstv if (rp->r_lowasnum)
2196 1.25 mlelstv updateminmax(rp->r_loyear);
2197 1.25 mlelstv if (rp->r_hiwasnum)
2198 1.25 mlelstv updateminmax(rp->r_hiyear);
2199 1.41 christos if (rp->r_lowasnum || rp->r_hiwasnum)
2200 1.46.2.1 martin prodstic = false;
2201 1.25 mlelstv }
2202 1.25 mlelstv }
2203 1.25 mlelstv /*
2204 1.25 mlelstv ** Generate lots of data if a rule can't cover all future times.
2205 1.25 mlelstv */
2206 1.43 christos compat = stringzone(envvar, max_envvar_len + 1, zpfirst, zonecount);
2207 1.43 christos version = compat < 2013 ? ZIC_VERSION_PRE_2013 : ZIC_VERSION;
2208 1.43 christos do_extend = compat < 0 || compat == YEAR_BY_YEAR_ZONE;
2209 1.44 christos if (noise) {
2210 1.44 christos if (!*envvar)
2211 1.43 christos warning("%s %s",
2212 1.43 christos _("no POSIX environment variable for zone"),
2213 1.43 christos zpfirst->z_name);
2214 1.44 christos else if (compat != 0 && compat != YEAR_BY_YEAR_ZONE) {
2215 1.43 christos /* Circa-COMPAT clients, and earlier clients, might
2216 1.43 christos not work for this zone when given dates before
2217 1.43 christos 1970 or after 2038. */
2218 1.43 christos warning(_("%s: pre-%d clients may mishandle"
2219 1.43 christos " distant timestamps"),
2220 1.43 christos zpfirst->z_name, compat);
2221 1.43 christos }
2222 1.43 christos }
2223 1.43 christos if (do_extend) {
2224 1.43 christos /*
2225 1.43 christos ** Search through a couple of extra years past the obvious
2226 1.43 christos ** 400, to avoid edge cases. For example, suppose a non-POSIX
2227 1.43 christos ** rule applies from 2012 onwards and has transitions in March
2228 1.43 christos ** and September, plus some one-off transitions in November
2229 1.43 christos ** 2013. If zic looked only at the last 400 years, it would
2230 1.43 christos ** set max_year=2413, with the intent that the 400 years 2014
2231 1.43 christos ** through 2413 will be repeated. The last transition listed
2232 1.43 christos ** in the tzfile would be in 2413-09, less than 400 years
2233 1.43 christos ** after the last one-off transition in 2013-11. Two years
2234 1.43 christos ** might be overkill, but with the kind of edge cases
2235 1.43 christos ** available we're not sure that one year would suffice.
2236 1.43 christos */
2237 1.43 christos enum { years_of_observations = YEARSPERREPEAT + 2 };
2238 1.43 christos
2239 1.43 christos if (min_year >= ZIC_MIN + years_of_observations)
2240 1.43 christos min_year -= years_of_observations;
2241 1.41 christos else min_year = ZIC_MIN;
2242 1.43 christos if (max_year <= ZIC_MAX - years_of_observations)
2243 1.43 christos max_year += years_of_observations;
2244 1.41 christos else max_year = ZIC_MAX;
2245 1.29 christos /*
2246 1.29 christos ** Regardless of any of the above,
2247 1.29 christos ** for a "proDSTic" zone which specifies that its rules
2248 1.29 christos ** always have and always will be in effect,
2249 1.29 christos ** we only need one cycle to define the zone.
2250 1.29 christos */
2251 1.29 christos if (prodstic) {
2252 1.29 christos min_year = 1900;
2253 1.43 christos max_year = min_year + years_of_observations;
2254 1.29 christos }
2255 1.25 mlelstv }
2256 1.25 mlelstv /*
2257 1.25 mlelstv ** For the benefit of older systems,
2258 1.25 mlelstv ** generate data from 1900 through 2037.
2259 1.25 mlelstv */
2260 1.25 mlelstv if (min_year > 1900)
2261 1.25 mlelstv min_year = 1900;
2262 1.25 mlelstv if (max_year < 2037)
2263 1.25 mlelstv max_year = 2037;
2264 1.1 jtc for (i = 0; i < zonecount; ++i) {
2265 1.19 kleink /*
2266 1.19 kleink ** A guess that may well be corrected later.
2267 1.19 kleink */
2268 1.19 kleink stdoff = 0;
2269 1.1 jtc zp = &zpfirst[i];
2270 1.46 christos usestart = i > 0 && (zp - 1)->z_untiltime > big_bang_time;
2271 1.1 jtc useuntil = i < (zonecount - 1);
2272 1.46 christos if (useuntil && zp->z_untiltime <= big_bang_time)
2273 1.1 jtc continue;
2274 1.1 jtc gmtoff = zp->z_gmtoff;
2275 1.1 jtc eat(zp->z_filename, zp->z_linenum);
2276 1.5 jtc *startbuf = '\0';
2277 1.5 jtc startoff = zp->z_gmtoff;
2278 1.1 jtc if (zp->z_nrules == 0) {
2279 1.1 jtc stdoff = zp->z_stdoff;
2280 1.25 mlelstv doabbr(startbuf, max_abbr_len + 1, zp->z_format,
2281 1.46.2.1 martin NULL, stdoff != 0, false);
2282 1.1 jtc type = addtype(oadd(zp->z_gmtoff, stdoff),
2283 1.1 jtc startbuf, stdoff != 0, startttisstd,
2284 1.1 jtc startttisgmt);
2285 1.5 jtc if (usestart) {
2286 1.1 jtc addtt(starttime, type);
2287 1.46.2.1 martin usestart = false;
2288 1.46 christos } else addtt(big_bang_time, type);
2289 1.1 jtc } else for (year = min_year; year <= max_year; ++year) {
2290 1.1 jtc if (useuntil && year > zp->z_untilrule.r_hiyear)
2291 1.1 jtc break;
2292 1.1 jtc /*
2293 1.1 jtc ** Mark which rules to do in the current year.
2294 1.1 jtc ** For those to do, calculate rpytime(rp, year);
2295 1.1 jtc */
2296 1.1 jtc for (j = 0; j < zp->z_nrules; ++j) {
2297 1.1 jtc rp = &zp->z_rules[j];
2298 1.1 jtc eats(zp->z_filename, zp->z_linenum,
2299 1.1 jtc rp->r_filename, rp->r_linenum);
2300 1.1 jtc rp->r_todo = year >= rp->r_loyear &&
2301 1.1 jtc year <= rp->r_hiyear &&
2302 1.1 jtc yearistype(year, rp->r_yrtype);
2303 1.1 jtc if (rp->r_todo)
2304 1.1 jtc rp->r_temp = rpytime(rp, year);
2305 1.1 jtc }
2306 1.1 jtc for ( ; ; ) {
2307 1.31 christos int k;
2308 1.31 christos zic_t jtime, ktime;
2309 1.38 christos zic_t offset;
2310 1.1 jtc
2311 1.1 jtc INITIALIZE(ktime);
2312 1.1 jtc if (useuntil) {
2313 1.1 jtc /*
2314 1.43 christos ** Turn untiltime into UT
2315 1.1 jtc ** assuming the current gmtoff and
2316 1.1 jtc ** stdoff values.
2317 1.1 jtc */
2318 1.1 jtc untiltime = zp->z_untiltime;
2319 1.1 jtc if (!zp->z_untilrule.r_todisgmt)
2320 1.1 jtc untiltime = tadd(untiltime,
2321 1.1 jtc -gmtoff);
2322 1.1 jtc if (!zp->z_untilrule.r_todisstd)
2323 1.1 jtc untiltime = tadd(untiltime,
2324 1.1 jtc -stdoff);
2325 1.1 jtc }
2326 1.1 jtc /*
2327 1.1 jtc ** Find the rule (of those to do, if any)
2328 1.1 jtc ** that takes effect earliest in the year.
2329 1.1 jtc */
2330 1.1 jtc k = -1;
2331 1.1 jtc for (j = 0; j < zp->z_nrules; ++j) {
2332 1.1 jtc rp = &zp->z_rules[j];
2333 1.1 jtc if (!rp->r_todo)
2334 1.1 jtc continue;
2335 1.1 jtc eats(zp->z_filename, zp->z_linenum,
2336 1.1 jtc rp->r_filename, rp->r_linenum);
2337 1.1 jtc offset = rp->r_todisgmt ? 0 : gmtoff;
2338 1.1 jtc if (!rp->r_todisstd)
2339 1.1 jtc offset = oadd(offset, stdoff);
2340 1.1 jtc jtime = rp->r_temp;
2341 1.1 jtc if (jtime == min_time ||
2342 1.1 jtc jtime == max_time)
2343 1.1 jtc continue;
2344 1.1 jtc jtime = tadd(jtime, -offset);
2345 1.1 jtc if (k < 0 || jtime < ktime) {
2346 1.1 jtc k = j;
2347 1.1 jtc ktime = jtime;
2348 1.1 jtc }
2349 1.1 jtc }
2350 1.1 jtc if (k < 0)
2351 1.1 jtc break; /* go on to next year */
2352 1.1 jtc rp = &zp->z_rules[k];
2353 1.46.2.1 martin rp->r_todo = false;
2354 1.1 jtc if (useuntil && ktime >= untiltime)
2355 1.1 jtc break;
2356 1.5 jtc stdoff = rp->r_stdoff;
2357 1.5 jtc if (usestart && ktime == starttime)
2358 1.46.2.1 martin usestart = false;
2359 1.1 jtc if (usestart) {
2360 1.5 jtc if (ktime < starttime) {
2361 1.5 jtc startoff = oadd(zp->z_gmtoff,
2362 1.5 jtc stdoff);
2363 1.25 mlelstv doabbr(startbuf,
2364 1.25 mlelstv max_abbr_len + 1,
2365 1.6 mrg zp->z_format,
2366 1.5 jtc rp->r_abbrvar,
2367 1.25 mlelstv rp->r_stdoff != 0,
2368 1.46.2.1 martin false);
2369 1.5 jtc continue;
2370 1.5 jtc }
2371 1.5 jtc if (*startbuf == '\0' &&
2372 1.25 mlelstv startoff == oadd(zp->z_gmtoff,
2373 1.25 mlelstv stdoff)) {
2374 1.25 mlelstv doabbr(startbuf,
2375 1.25 mlelstv max_abbr_len + 1,
2376 1.25 mlelstv zp->z_format,
2377 1.25 mlelstv rp->r_abbrvar,
2378 1.25 mlelstv rp->r_stdoff !=
2379 1.25 mlelstv 0,
2380 1.46.2.1 martin false);
2381 1.1 jtc }
2382 1.1 jtc }
2383 1.1 jtc eats(zp->z_filename, zp->z_linenum,
2384 1.1 jtc rp->r_filename, rp->r_linenum);
2385 1.25 mlelstv doabbr(ab, max_abbr_len+1, zp->z_format, rp->r_abbrvar,
2386 1.46.2.1 martin rp->r_stdoff != 0, false);
2387 1.1 jtc offset = oadd(zp->z_gmtoff, rp->r_stdoff);
2388 1.25 mlelstv type = addtype(offset, ab, rp->r_stdoff != 0,
2389 1.1 jtc rp->r_todisstd, rp->r_todisgmt);
2390 1.1 jtc addtt(ktime, type);
2391 1.1 jtc }
2392 1.1 jtc }
2393 1.5 jtc if (usestart) {
2394 1.5 jtc if (*startbuf == '\0' &&
2395 1.5 jtc zp->z_format != NULL &&
2396 1.5 jtc strchr(zp->z_format, '%') == NULL &&
2397 1.5 jtc strchr(zp->z_format, '/') == NULL)
2398 1.6 mrg (void)strncpy(startbuf, zp->z_format,
2399 1.25 mlelstv max_abbr_len + 1 - 1);
2400 1.5 jtc eat(zp->z_filename, zp->z_linenum);
2401 1.5 jtc if (*startbuf == '\0')
2402 1.7 jtc error(_("can't determine time zone abbreviation to use just after until time"));
2403 1.5 jtc else addtt(starttime,
2404 1.5 jtc addtype(startoff, startbuf,
2405 1.5 jtc startoff != zp->z_gmtoff,
2406 1.5 jtc startttisstd,
2407 1.5 jtc startttisgmt));
2408 1.5 jtc }
2409 1.1 jtc /*
2410 1.1 jtc ** Now we may get to set starttime for the next zone line.
2411 1.1 jtc */
2412 1.1 jtc if (useuntil) {
2413 1.1 jtc startttisstd = zp->z_untilrule.r_todisstd;
2414 1.1 jtc startttisgmt = zp->z_untilrule.r_todisgmt;
2415 1.5 jtc starttime = zp->z_untiltime;
2416 1.1 jtc if (!startttisstd)
2417 1.1 jtc starttime = tadd(starttime, -stdoff);
2418 1.5 jtc if (!startttisgmt)
2419 1.5 jtc starttime = tadd(starttime, -gmtoff);
2420 1.1 jtc }
2421 1.1 jtc }
2422 1.43 christos if (do_extend) {
2423 1.43 christos /*
2424 1.43 christos ** If we're extending the explicitly listed observations
2425 1.43 christos ** for 400 years because we can't fill the POSIX-TZ field,
2426 1.43 christos ** check whether we actually ended up explicitly listing
2427 1.43 christos ** observations through that period. If there aren't any
2428 1.43 christos ** near the end of the 400-year period, add a redundant
2429 1.43 christos ** one at the end of the final year, to make it clear
2430 1.43 christos ** that we are claiming to have definite knowledge of
2431 1.43 christos ** the lack of transitions up to that point.
2432 1.43 christos */
2433 1.43 christos struct rule xr;
2434 1.43 christos struct attype *lastat;
2435 1.43 christos xr.r_month = TM_JANUARY;
2436 1.43 christos xr.r_dycode = DC_DOM;
2437 1.43 christos xr.r_dayofmonth = 1;
2438 1.43 christos xr.r_tod = 0;
2439 1.43 christos for (lastat = &attypes[0], i = 1; i < timecnt; i++)
2440 1.43 christos if (attypes[i].at > lastat->at)
2441 1.43 christos lastat = &attypes[i];
2442 1.43 christos if (lastat->at < rpytime(&xr, max_year - 1)) {
2443 1.43 christos /*
2444 1.43 christos ** Create new type code for the redundant entry,
2445 1.46.2.1 martin ** to prevent it being optimized away.
2446 1.43 christos */
2447 1.43 christos if (typecnt >= TZ_MAX_TYPES) {
2448 1.43 christos error(_("too many local time types"));
2449 1.43 christos exit(EXIT_FAILURE);
2450 1.43 christos }
2451 1.43 christos gmtoffs[typecnt] = gmtoffs[lastat->type];
2452 1.43 christos isdsts[typecnt] = isdsts[lastat->type];
2453 1.43 christos ttisstds[typecnt] = ttisstds[lastat->type];
2454 1.43 christos ttisgmts[typecnt] = ttisgmts[lastat->type];
2455 1.43 christos abbrinds[typecnt] = abbrinds[lastat->type];
2456 1.43 christos ++typecnt;
2457 1.43 christos addtt(rpytime(&xr, max_year + 1), typecnt-1);
2458 1.43 christos }
2459 1.43 christos }
2460 1.43 christos writezone(zpfirst->z_name, envvar, version);
2461 1.31 christos free(startbuf);
2462 1.31 christos free(ab);
2463 1.31 christos free(envvar);
2464 1.1 jtc }
2465 1.1 jtc
2466 1.1 jtc static void
2467 1.31 christos addtt(const zic_t starttime, int type)
2468 1.1 jtc {
2469 1.46 christos if (starttime <= big_bang_time ||
2470 1.46 christos (timecnt == 1 && attypes[0].at < big_bang_time)) {
2471 1.7 jtc gmtoffs[0] = gmtoffs[type];
2472 1.7 jtc isdsts[0] = isdsts[type];
2473 1.7 jtc ttisstds[0] = ttisstds[type];
2474 1.7 jtc ttisgmts[0] = ttisgmts[type];
2475 1.7 jtc if (abbrinds[type] != 0)
2476 1.46.2.1 martin strcpy(chars, &chars[abbrinds[type]]);
2477 1.7 jtc abbrinds[0] = 0;
2478 1.7 jtc charcnt = strlen(chars) + 1;
2479 1.7 jtc typecnt = 1;
2480 1.7 jtc timecnt = 0;
2481 1.7 jtc type = 0;
2482 1.7 jtc }
2483 1.45 christos attypes = growalloc(attypes, sizeof *attypes, timecnt, &timecnt_alloc);
2484 1.5 jtc attypes[timecnt].at = starttime;
2485 1.5 jtc attypes[timecnt].type = type;
2486 1.1 jtc ++timecnt;
2487 1.1 jtc }
2488 1.1 jtc
2489 1.1 jtc static int
2490 1.46.2.1 martin addtype(zic_t gmtoff, char *const abbr, bool isdst, bool ttisstd, bool ttisgmt)
2491 1.1 jtc {
2492 1.31 christos int i, j;
2493 1.1 jtc
2494 1.1 jtc /*
2495 1.1 jtc ** See if there's already an entry for this zone type.
2496 1.1 jtc ** If so, just return its index.
2497 1.1 jtc */
2498 1.1 jtc for (i = 0; i < typecnt; ++i) {
2499 1.1 jtc if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
2500 1.1 jtc strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
2501 1.1 jtc ttisstd == ttisstds[i] &&
2502 1.1 jtc ttisgmt == ttisgmts[i])
2503 1.1 jtc return i;
2504 1.1 jtc }
2505 1.1 jtc /*
2506 1.1 jtc ** There isn't one; add a new one, unless there are already too
2507 1.1 jtc ** many.
2508 1.1 jtc */
2509 1.1 jtc if (typecnt >= TZ_MAX_TYPES) {
2510 1.5 jtc error(_("too many local time types"));
2511 1.25 mlelstv exit(EXIT_FAILURE);
2512 1.25 mlelstv }
2513 1.25 mlelstv if (! (-1L - 2147483647L <= gmtoff && gmtoff <= 2147483647L)) {
2514 1.43 christos error(_("UT offset out of range"));
2515 1.25 mlelstv exit(EXIT_FAILURE);
2516 1.1 jtc }
2517 1.1 jtc gmtoffs[i] = gmtoff;
2518 1.1 jtc isdsts[i] = isdst;
2519 1.1 jtc ttisstds[i] = ttisstd;
2520 1.1 jtc ttisgmts[i] = ttisgmt;
2521 1.1 jtc
2522 1.1 jtc for (j = 0; j < charcnt; ++j)
2523 1.1 jtc if (strcmp(&chars[j], abbr) == 0)
2524 1.1 jtc break;
2525 1.1 jtc if (j == charcnt)
2526 1.1 jtc newabbr(abbr);
2527 1.1 jtc abbrinds[i] = j;
2528 1.1 jtc ++typecnt;
2529 1.1 jtc return i;
2530 1.1 jtc }
2531 1.1 jtc
2532 1.1 jtc static void
2533 1.46.2.1 martin leapadd(zic_t t, bool positive, int rolling, int count)
2534 1.1 jtc {
2535 1.31 christos int i, j;
2536 1.1 jtc
2537 1.1 jtc if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
2538 1.5 jtc error(_("too many leap seconds"));
2539 1.25 mlelstv exit(EXIT_FAILURE);
2540 1.1 jtc }
2541 1.1 jtc for (i = 0; i < leapcnt; ++i)
2542 1.1 jtc if (t <= trans[i]) {
2543 1.1 jtc if (t == trans[i]) {
2544 1.5 jtc error(_("repeated leap second moment"));
2545 1.25 mlelstv exit(EXIT_FAILURE);
2546 1.1 jtc }
2547 1.1 jtc break;
2548 1.1 jtc }
2549 1.1 jtc do {
2550 1.1 jtc for (j = leapcnt; j > i; --j) {
2551 1.1 jtc trans[j] = trans[j - 1];
2552 1.1 jtc corr[j] = corr[j - 1];
2553 1.1 jtc roll[j] = roll[j - 1];
2554 1.1 jtc }
2555 1.1 jtc trans[i] = t;
2556 1.41 christos corr[i] = positive ? 1 : -count;
2557 1.1 jtc roll[i] = rolling;
2558 1.1 jtc ++leapcnt;
2559 1.1 jtc } while (positive && --count != 0);
2560 1.1 jtc }
2561 1.1 jtc
2562 1.1 jtc static void
2563 1.25 mlelstv adjleap(void)
2564 1.1 jtc {
2565 1.31 christos int i;
2566 1.38 christos zic_t last = 0;
2567 1.1 jtc
2568 1.1 jtc /*
2569 1.1 jtc ** propagate leap seconds forward
2570 1.1 jtc */
2571 1.1 jtc for (i = 0; i < leapcnt; ++i) {
2572 1.1 jtc trans[i] = tadd(trans[i], last);
2573 1.1 jtc last = corr[i] += last;
2574 1.1 jtc }
2575 1.1 jtc }
2576 1.1 jtc
2577 1.46.2.1 martin static bool
2578 1.31 christos yearistype(const int year, const char *const type)
2579 1.1 jtc {
2580 1.1 jtc static char * buf;
2581 1.1 jtc int result;
2582 1.1 jtc
2583 1.1 jtc if (type == NULL || *type == '\0')
2584 1.46.2.1 martin return true;
2585 1.31 christos buf = erealloc(buf, 132 + strlen(yitcommand) + strlen(type));
2586 1.6 mrg (void)sprintf(buf, "%s %d %s", yitcommand, year, type); /* XXX: sprintf is safe */
2587 1.1 jtc result = system(buf);
2588 1.16 kleink if (WIFEXITED(result)) switch (WEXITSTATUS(result)) {
2589 1.16 kleink case 0:
2590 1.46.2.1 martin return true;
2591 1.16 kleink case 1:
2592 1.46.2.1 martin return false;
2593 1.16 kleink }
2594 1.5 jtc error(_("Wild result from command execution"));
2595 1.5 jtc (void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
2596 1.1 jtc progname, buf, result);
2597 1.1 jtc for ( ; ; )
2598 1.25 mlelstv exit(EXIT_FAILURE);
2599 1.1 jtc }
2600 1.1 jtc
2601 1.46.2.1 martin /* Is A a space character in the C locale? */
2602 1.46.2.1 martin static bool
2603 1.46.2.1 martin is_space(char a)
2604 1.1 jtc {
2605 1.46.2.1 martin switch (a) {
2606 1.46.2.1 martin default:
2607 1.46.2.1 martin return false;
2608 1.46.2.1 martin case ' ': case '\f': case '\n': case '\r': case '\t': case '\v':
2609 1.46.2.1 martin return true;
2610 1.46.2.1 martin }
2611 1.46.2.1 martin }
2612 1.46.2.1 martin
2613 1.46.2.1 martin /* Is A an alphabetic character in the C locale? */
2614 1.46.2.1 martin static bool
2615 1.46.2.1 martin is_alpha(char a)
2616 1.46.2.1 martin {
2617 1.46.2.1 martin switch (a) {
2618 1.46.2.1 martin default:
2619 1.46.2.1 martin return 0;
2620 1.46.2.1 martin case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
2621 1.46.2.1 martin case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
2622 1.46.2.1 martin case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
2623 1.46.2.1 martin case 'V': case 'W': case 'X': case 'Y': case 'Z':
2624 1.46.2.1 martin case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2625 1.46.2.1 martin case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
2626 1.46.2.1 martin case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
2627 1.46.2.1 martin case 'v': case 'w': case 'x': case 'y': case 'z':
2628 1.46.2.1 martin return true;
2629 1.46.2.1 martin }
2630 1.46.2.1 martin }
2631 1.46.2.1 martin
2632 1.46.2.1 martin /* If A is an uppercase character in the C locale, return its lowercase
2633 1.46.2.1 martin counterpart. Otherwise, return A. */
2634 1.46.2.1 martin static char
2635 1.46.2.1 martin lowerit(char a)
2636 1.46.2.1 martin {
2637 1.46.2.1 martin switch (a) {
2638 1.46.2.1 martin default: return a;
2639 1.46.2.1 martin case 'A': return 'a'; case 'B': return 'b'; case 'C': return 'c';
2640 1.46.2.1 martin case 'D': return 'd'; case 'E': return 'e'; case 'F': return 'f';
2641 1.46.2.1 martin case 'G': return 'g'; case 'H': return 'h'; case 'I': return 'i';
2642 1.46.2.1 martin case 'J': return 'j'; case 'K': return 'k'; case 'L': return 'l';
2643 1.46.2.1 martin case 'M': return 'm'; case 'N': return 'n'; case 'O': return 'o';
2644 1.46.2.1 martin case 'P': return 'p'; case 'Q': return 'q'; case 'R': return 'r';
2645 1.46.2.1 martin case 'S': return 's'; case 'T': return 't'; case 'U': return 'u';
2646 1.46.2.1 martin case 'V': return 'v'; case 'W': return 'w'; case 'X': return 'x';
2647 1.46.2.1 martin case 'Y': return 'y'; case 'Z': return 'z';
2648 1.46.2.1 martin }
2649 1.1 jtc }
2650 1.1 jtc
2651 1.31 christos /* case-insensitive equality */
2652 1.46.2.1 martin static ATTRIBUTE_PURE bool
2653 1.31 christos ciequal(const char *ap, const char *bp)
2654 1.1 jtc {
2655 1.1 jtc while (lowerit(*ap) == lowerit(*bp++))
2656 1.1 jtc if (*ap++ == '\0')
2657 1.46.2.1 martin return true;
2658 1.46.2.1 martin return false;
2659 1.1 jtc }
2660 1.1 jtc
2661 1.46.2.1 martin static ATTRIBUTE_PURE bool
2662 1.31 christos itsabbr(const char *abbr, const char *word)
2663 1.1 jtc {
2664 1.1 jtc if (lowerit(*abbr) != lowerit(*word))
2665 1.46.2.1 martin return false;
2666 1.1 jtc ++word;
2667 1.1 jtc while (*++abbr != '\0')
2668 1.3 jtc do {
2669 1.3 jtc if (*word == '\0')
2670 1.46.2.1 martin return false;
2671 1.3 jtc } while (lowerit(*word++) != lowerit(*abbr));
2672 1.46.2.1 martin return true;
2673 1.1 jtc }
2674 1.1 jtc
2675 1.41 christos static ATTRIBUTE_PURE const struct lookup *
2676 1.31 christos byword(const char *const word, const struct lookup *const table)
2677 1.1 jtc {
2678 1.31 christos const struct lookup * foundlp;
2679 1.31 christos const struct lookup * lp;
2680 1.1 jtc
2681 1.1 jtc if (word == NULL || table == NULL)
2682 1.1 jtc return NULL;
2683 1.1 jtc /*
2684 1.1 jtc ** Look for exact match.
2685 1.1 jtc */
2686 1.1 jtc for (lp = table; lp->l_word != NULL; ++lp)
2687 1.1 jtc if (ciequal(word, lp->l_word))
2688 1.1 jtc return lp;
2689 1.1 jtc /*
2690 1.1 jtc ** Look for inexact match.
2691 1.1 jtc */
2692 1.1 jtc foundlp = NULL;
2693 1.1 jtc for (lp = table; lp->l_word != NULL; ++lp)
2694 1.11 jtc if (itsabbr(word, lp->l_word)) {
2695 1.1 jtc if (foundlp == NULL)
2696 1.1 jtc foundlp = lp;
2697 1.1 jtc else return NULL; /* multiple inexact matches */
2698 1.11 jtc }
2699 1.1 jtc return foundlp;
2700 1.1 jtc }
2701 1.1 jtc
2702 1.1 jtc static char **
2703 1.31 christos getfields(char *cp)
2704 1.1 jtc {
2705 1.31 christos char * dp;
2706 1.31 christos char ** array;
2707 1.31 christos int nsubs;
2708 1.1 jtc
2709 1.1 jtc if (cp == NULL)
2710 1.1 jtc return NULL;
2711 1.45 christos array = emalloc(size_product(strlen(cp) + 1, sizeof *array));
2712 1.1 jtc nsubs = 0;
2713 1.1 jtc for ( ; ; ) {
2714 1.46.2.1 martin while (is_space(*cp))
2715 1.25 mlelstv ++cp;
2716 1.1 jtc if (*cp == '\0' || *cp == '#')
2717 1.1 jtc break;
2718 1.1 jtc array[nsubs++] = dp = cp;
2719 1.1 jtc do {
2720 1.1 jtc if ((*dp = *cp++) != '"')
2721 1.1 jtc ++dp;
2722 1.1 jtc else while ((*dp = *cp++) != '"')
2723 1.1 jtc if (*dp != '\0')
2724 1.1 jtc ++dp;
2725 1.25 mlelstv else {
2726 1.25 mlelstv error(_(
2727 1.25 mlelstv "Odd number of quotation marks"
2728 1.25 mlelstv ));
2729 1.25 mlelstv exit(1);
2730 1.25 mlelstv }
2731 1.46.2.1 martin } while (*cp && *cp != '#' && !is_space(*cp));
2732 1.46.2.1 martin if (is_space(*cp))
2733 1.1 jtc ++cp;
2734 1.1 jtc *dp = '\0';
2735 1.1 jtc }
2736 1.1 jtc array[nsubs] = NULL;
2737 1.1 jtc return array;
2738 1.1 jtc }
2739 1.1 jtc
2740 1.41 christos static ATTRIBUTE_PURE zic_t
2741 1.38 christos oadd(const zic_t t1, const zic_t t2)
2742 1.1 jtc {
2743 1.41 christos if (t1 < 0 ? t2 < ZIC_MIN - t1 : ZIC_MAX - t1 < t2) {
2744 1.5 jtc error(_("time overflow"));
2745 1.25 mlelstv exit(EXIT_FAILURE);
2746 1.1 jtc }
2747 1.31 christos return t1 + t2;
2748 1.1 jtc }
2749 1.1 jtc
2750 1.43 christos static ATTRIBUTE_PURE zic_t
2751 1.38 christos tadd(const zic_t t1, const zic_t t2)
2752 1.1 jtc {
2753 1.1 jtc if (t1 == max_time && t2 > 0)
2754 1.1 jtc return max_time;
2755 1.1 jtc if (t1 == min_time && t2 < 0)
2756 1.1 jtc return min_time;
2757 1.31 christos if (t1 < 0 ? t2 < min_time - t1 : max_time - t1 < t2) {
2758 1.5 jtc error(_("time overflow"));
2759 1.25 mlelstv exit(EXIT_FAILURE);
2760 1.1 jtc }
2761 1.31 christos return t1 + t2;
2762 1.1 jtc }
2763 1.1 jtc
2764 1.1 jtc /*
2765 1.46.2.1 martin ** Given a rule, and a year, compute the date (in seconds since January 1,
2766 1.46.2.1 martin ** 1970, 00:00 LOCAL time) in that year that the rule refers to.
2767 1.1 jtc */
2768 1.1 jtc
2769 1.25 mlelstv static zic_t
2770 1.41 christos rpytime(const struct rule *const rp, const zic_t wantedy)
2771 1.31 christos {
2772 1.41 christos int m, i;
2773 1.38 christos zic_t dayoff; /* with a nod to Margaret O. */
2774 1.41 christos zic_t t, y;
2775 1.1 jtc
2776 1.41 christos if (wantedy == ZIC_MIN)
2777 1.1 jtc return min_time;
2778 1.41 christos if (wantedy == ZIC_MAX)
2779 1.1 jtc return max_time;
2780 1.1 jtc dayoff = 0;
2781 1.1 jtc m = TM_JANUARY;
2782 1.1 jtc y = EPOCH_YEAR;
2783 1.1 jtc while (wantedy != y) {
2784 1.1 jtc if (wantedy > y) {
2785 1.1 jtc i = len_years[isleap(y)];
2786 1.1 jtc ++y;
2787 1.1 jtc } else {
2788 1.1 jtc --y;
2789 1.1 jtc i = -len_years[isleap(y)];
2790 1.1 jtc }
2791 1.41 christos dayoff = oadd(dayoff, i);
2792 1.1 jtc }
2793 1.1 jtc while (m != rp->r_month) {
2794 1.1 jtc i = len_months[isleap(y)][m];
2795 1.41 christos dayoff = oadd(dayoff, i);
2796 1.1 jtc ++m;
2797 1.1 jtc }
2798 1.1 jtc i = rp->r_dayofmonth;
2799 1.1 jtc if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
2800 1.1 jtc if (rp->r_dycode == DC_DOWLEQ)
2801 1.1 jtc --i;
2802 1.1 jtc else {
2803 1.5 jtc error(_("use of 2/29 in non leap-year"));
2804 1.25 mlelstv exit(EXIT_FAILURE);
2805 1.1 jtc }
2806 1.1 jtc }
2807 1.1 jtc --i;
2808 1.41 christos dayoff = oadd(dayoff, i);
2809 1.1 jtc if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
2810 1.38 christos zic_t wday;
2811 1.1 jtc
2812 1.38 christos #define LDAYSPERWEEK ((zic_t) DAYSPERWEEK)
2813 1.41 christos wday = EPOCH_WDAY;
2814 1.1 jtc /*
2815 1.1 jtc ** Don't trust mod of negative numbers.
2816 1.1 jtc */
2817 1.1 jtc if (dayoff >= 0)
2818 1.1 jtc wday = (wday + dayoff) % LDAYSPERWEEK;
2819 1.1 jtc else {
2820 1.1 jtc wday -= ((-dayoff) % LDAYSPERWEEK);
2821 1.1 jtc if (wday < 0)
2822 1.1 jtc wday += LDAYSPERWEEK;
2823 1.1 jtc }
2824 1.41 christos while (wday != rp->r_wday)
2825 1.1 jtc if (rp->r_dycode == DC_DOWGEQ) {
2826 1.38 christos dayoff = oadd(dayoff, (zic_t) 1);
2827 1.1 jtc if (++wday >= LDAYSPERWEEK)
2828 1.1 jtc wday = 0;
2829 1.1 jtc ++i;
2830 1.1 jtc } else {
2831 1.38 christos dayoff = oadd(dayoff, (zic_t) -1);
2832 1.1 jtc if (--wday < 0)
2833 1.1 jtc wday = LDAYSPERWEEK - 1;
2834 1.1 jtc --i;
2835 1.1 jtc }
2836 1.1 jtc if (i < 0 || i >= len_months[isleap(y)][m]) {
2837 1.23 kleink if (noise)
2838 1.46.2.1 martin warning(_("rule goes past start/end of month; \
2839 1.25 mlelstv will not work with pre-2004 versions of zic"));
2840 1.1 jtc }
2841 1.1 jtc }
2842 1.34 martin if (dayoff < min_time / SECSPERDAY)
2843 1.20 kleink return min_time;
2844 1.34 martin if (dayoff > max_time / SECSPERDAY)
2845 1.20 kleink return max_time;
2846 1.25 mlelstv t = (zic_t) dayoff * SECSPERDAY;
2847 1.1 jtc return tadd(t, rp->r_tod);
2848 1.1 jtc }
2849 1.1 jtc
2850 1.1 jtc static void
2851 1.31 christos newabbr(const char *const string)
2852 1.1 jtc {
2853 1.31 christos int i;
2854 1.1 jtc
2855 1.25 mlelstv if (strcmp(string, GRANDPARENTED) != 0) {
2856 1.31 christos const char * cp;
2857 1.31 christos const char * mp;
2858 1.25 mlelstv
2859 1.25 mlelstv /*
2860 1.25 mlelstv ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2861 1.25 mlelstv ** optionally followed by a + or - and a number from 1 to 14.
2862 1.25 mlelstv */
2863 1.25 mlelstv cp = string;
2864 1.31 christos mp = NULL;
2865 1.46.2.1 martin while (is_alpha(*cp))
2866 1.25 mlelstv ++cp;
2867 1.25 mlelstv if (cp - string == 0)
2868 1.31 christos mp = _("time zone abbreviation lacks alphabetic at start");
2869 1.35 christos if (noise && cp - string < 3)
2870 1.35 christos mp = _("time zone abbreviation has fewer than 3 alphabetics");
2871 1.25 mlelstv if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
2872 1.31 christos mp = _("time zone abbreviation has too many alphabetics");
2873 1.31 christos if (mp == NULL && (*cp == '+' || *cp == '-')) {
2874 1.25 mlelstv ++cp;
2875 1.46.2.1 martin if (is_digit(*cp))
2876 1.25 mlelstv if (*cp++ == '1' &&
2877 1.25 mlelstv *cp >= '0' && *cp <= '4')
2878 1.25 mlelstv ++cp;
2879 1.25 mlelstv }
2880 1.25 mlelstv if (*cp != '\0')
2881 1.31 christos mp = _("time zone abbreviation differs from POSIX standard");
2882 1.43 christos if (mp != NULL)
2883 1.43 christos warning("%s (%s)", mp, string);
2884 1.25 mlelstv }
2885 1.1 jtc i = strlen(string) + 1;
2886 1.1 jtc if (charcnt + i > TZ_MAX_CHARS) {
2887 1.40 christos error(_("too many, or too long, time zone abbreviations"));
2888 1.25 mlelstv exit(EXIT_FAILURE);
2889 1.1 jtc }
2890 1.6 mrg (void)strncpy(&chars[charcnt], string, sizeof(chars) - charcnt - 1);
2891 1.41 christos charcnt += i;
2892 1.1 jtc }
2893 1.1 jtc
2894 1.46.2.1 martin static bool
2895 1.31 christos mkdirs(char *argname)
2896 1.1 jtc {
2897 1.31 christos char * name;
2898 1.31 christos char * cp;
2899 1.1 jtc
2900 1.1 jtc if (argname == NULL || *argname == '\0')
2901 1.46.2.1 martin return true;
2902 1.1 jtc cp = name = ecpyalloc(argname);
2903 1.1 jtc while ((cp = strchr(cp + 1, '/')) != 0) {
2904 1.1 jtc *cp = '\0';
2905 1.41 christos #ifdef HAVE_DOS_FILE_NAMES
2906 1.1 jtc /*
2907 1.1 jtc ** DOS drive specifier?
2908 1.1 jtc */
2909 1.46.2.1 martin if (is_alpha(name[0]) && name[1] == ':' && name[2] == '\0') {
2910 1.1 jtc *cp = '/';
2911 1.1 jtc continue;
2912 1.1 jtc }
2913 1.41 christos #endif
2914 1.46.2.1 martin /*
2915 1.46.2.1 martin ** Try to create it. It's OK if creation fails because
2916 1.46.2.1 martin ** the directory already exists, perhaps because some
2917 1.46.2.1 martin ** other process just created it.
2918 1.46.2.1 martin */
2919 1.46.2.1 martin if (mkdir(name, MKDIR_UMASK) != 0) {
2920 1.46.2.1 martin int err = errno;
2921 1.46.2.1 martin if (itsdir(name) <= 0) {
2922 1.46.2.1 martin char const *e = strerror(err);
2923 1.46.2.1 martin warning(_("%s: Can't create directory"
2924 1.46.2.1 martin " %s: %s\n"),
2925 1.46.2.1 martin progname, name, e);
2926 1.46.2.1 martin free(name);
2927 1.46.2.1 martin return false;
2928 1.1 jtc }
2929 1.1 jtc }
2930 1.1 jtc *cp = '/';
2931 1.1 jtc }
2932 1.31 christos free(name);
2933 1.46.2.1 martin return true;
2934 1.1 jtc }
2935