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