PostgreSQL Source Code  git master
zic.c
Go to the documentation of this file.
1 /* Compile .zi time zone data into TZif binary files. */
2 
3 /*
4  * This file is in the public domain, so clarified as of
5  * 2006-07-17 by Arthur David Olson.
6  *
7  * IDENTIFICATION
8  * src/timezone/zic.c
9  */
10 
11 #include "postgres_fe.h"
12 
13 #include <fcntl.h>
14 #include <sys/stat.h>
15 #include <time.h>
16 
17 #include "pg_getopt.h"
18 
19 #include "private.h"
20 #include "tzfile.h"
21 
22 #define ZIC_VERSION_PRE_2013 '2'
23 #define ZIC_VERSION '3'
24 
25 typedef int64 zic_t;
26 #define ZIC_MIN PG_INT64_MIN
27 #define ZIC_MAX PG_INT64_MAX
28 
29 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
30 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
31 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
32 
33 #ifndef WIN32
34 #ifdef S_IRUSR
35 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
36 #else
37 #define MKDIR_UMASK 0755
38 #endif
39 #endif
40 #ifndef AT_SYMLINK_FOLLOW
41 #define linkat(fromdir, from, todir, to, flag) \
42  (itssymlink(from) ? (errno = ENOTSUP, -1) : link(from, to))
43 #endif
44 /* Port to native MS-Windows and to ancient UNIX. */
45 #if !defined S_ISDIR && defined S_IFDIR && defined S_IFMT
46 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
47 #endif
48 
49 /* The maximum ptrdiff_t value, for pre-C99 platforms. */
50 #ifndef PTRDIFF_MAX
51 static ptrdiff_t const PTRDIFF_MAX = MAXVAL(ptrdiff_t, TYPE_BIT(ptrdiff_t));
52 #endif
53 
54 /*
55  * The type for line numbers. In Postgres, use %d to format them; upstream
56  * uses PRIdMAX but we prefer not to rely on that, not least because it
57  * results in platform-dependent strings to be translated.
58  */
59 typedef int lineno_t;
60 
61 struct rule
62 {
63  const char *r_filename;
64  lineno_t r_linenum;
65  const char *r_name;
66 
67  zic_t r_loyear; /* for example, 1986 */
68  zic_t r_hiyear; /* for example, 1986 */
69  const char *r_yrtype;
70  bool r_lowasnum;
71  bool r_hiwasnum;
72 
73  int r_month; /* 0..11 */
74 
75  int r_dycode; /* see below */
77  int r_wday;
78 
79  zic_t r_tod; /* time from midnight */
80  bool r_todisstd; /* is r_tod standard time? */
81  bool r_todisut; /* is r_tod UT? */
82  bool r_isdst; /* is this daylight saving time? */
83  zic_t r_save; /* offset from standard time */
84  const char *r_abbrvar; /* variable part of abbreviation */
85 
86  bool r_todo; /* a rule to do (used in outzone) */
87  zic_t r_temp; /* used in outzone */
88 };
89 
90 /*
91  * r_dycode r_dayofmonth r_wday
92  */
93 
94 #define DC_DOM 0 /* 1..31 */ /* unused */
95 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
96 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
97 
98 struct zone
99 {
100  const char *z_filename;
101  lineno_t z_linenum;
102 
103  const char *z_name;
105  char *z_rule;
106  const char *z_format;
108 
109  bool z_isdst;
111 
112  struct rule *z_rules;
113  ptrdiff_t z_nrules;
114 
115  struct rule z_untilrule;
117 };
118 
119 extern int link(const char *fromname, const char *toname);
120 
121 static void memory_exhausted(const char *msg) pg_attribute_noreturn();
122 static void verror(const char *string, va_list args) pg_attribute_printf(1, 0);
123 static void error(const char *string,...) pg_attribute_printf(1, 2);
124 static void warning(const char *string,...) pg_attribute_printf(1, 2);
125 static void usage(FILE *stream, int status) pg_attribute_noreturn();
126 static void addtt(zic_t starttime, int type);
127 static int addtype(zic_t, char const *, bool, bool, bool);
128 static void leapadd(zic_t, int, int);
129 static void adjleap(void);
130 static void associate(void);
131 static void dolink(const char *, const char *, bool);
132 static char **getfields(char *buf);
133 static zic_t gethms(const char *string, const char *errstring);
134 static zic_t getsave(char *, bool *);
135 static void inexpires(char **, int);
136 static void infile(const char *filename);
137 static void inleap(char **fields, int nfields);
138 static void inlink(char **fields, int nfields);
139 static void inrule(char **fields, int nfields);
140 static bool inzcont(char **fields, int nfields);
141 static bool inzone(char **fields, int nfields);
142 static bool inzsub(char **, int, bool);
143 static bool itsdir(char const *);
144 static bool itssymlink(char const *);
145 static bool is_alpha(char a);
146 static char lowerit(char);
147 static void mkdirs(char const *, bool);
148 static void newabbr(const char *abbr);
149 static zic_t oadd(zic_t t1, zic_t t2);
150 static void outzone(const struct zone *zp, ptrdiff_t ntzones);
151 static zic_t rpytime(const struct rule *rp, zic_t wantedy);
152 static void rulesub(struct rule *rp,
153  const char *loyearp, const char *hiyearp,
154  const char *typep, const char *monthp,
155  const char *dayp, const char *timep);
156 static zic_t tadd(zic_t t1, zic_t t2);
157 static bool yearistype(zic_t year, const char *type);
158 
159 /* Bound on length of what %z can expand to. */
160 enum
161 {
162 PERCENT_Z_LEN_BOUND = sizeof "+995959" - 1};
163 
164 /* If true, work around a bug in Qt 5.6.1 and earlier, which mishandles
165  TZif files whose POSIX-TZ-style strings contain '<'; see
166  QTBUG-53071 <https://bugreports.qt.io/browse/QTBUG-53071>. This
167  workaround will no longer be needed when Qt 5.6.1 and earlier are
168  obsolete, say in the year 2021. */
169 #ifndef WORK_AROUND_QTBUG_53071
170 enum
171 {
173 #endif
174 
175 static int charcnt;
176 static bool errors;
177 static bool warnings;
178 static const char *filename;
179 static int leapcnt;
180 static bool leapseen;
183 static lineno_t linenum;
185 static int max_format_len;
188 static bool noise;
189 static bool print_abbrevs;
191 static const char *rfilename;
192 static lineno_t rlinenum;
193 static const char *progname;
194 static ptrdiff_t timecnt;
195 static ptrdiff_t timecnt_alloc;
196 static int typecnt;
197 
198 /*
199  * Line codes.
200  */
201 
202 #define LC_RULE 0
203 #define LC_ZONE 1
204 #define LC_LINK 2
205 #define LC_LEAP 3
206 #define LC_EXPIRES 4
207 
208 /*
209  * Which fields are which on a Zone line.
210  */
211 
212 #define ZF_NAME 1
213 #define ZF_STDOFF 2
214 #define ZF_RULE 3
215 #define ZF_FORMAT 4
216 #define ZF_TILYEAR 5
217 #define ZF_TILMONTH 6
218 #define ZF_TILDAY 7
219 #define ZF_TILTIME 8
220 #define ZONE_MINFIELDS 5
221 #define ZONE_MAXFIELDS 9
222 
223 /*
224  * Which fields are which on a Zone continuation line.
225  */
226 
227 #define ZFC_STDOFF 0
228 #define ZFC_RULE 1
229 #define ZFC_FORMAT 2
230 #define ZFC_TILYEAR 3
231 #define ZFC_TILMONTH 4
232 #define ZFC_TILDAY 5
233 #define ZFC_TILTIME 6
234 #define ZONEC_MINFIELDS 3
235 #define ZONEC_MAXFIELDS 7
236 
237 /*
238  * Which files are which on a Rule line.
239  */
240 
241 #define RF_NAME 1
242 #define RF_LOYEAR 2
243 #define RF_HIYEAR 3
244 #define RF_COMMAND 4
245 #define RF_MONTH 5
246 #define RF_DAY 6
247 #define RF_TOD 7
248 #define RF_SAVE 8
249 #define RF_ABBRVAR 9
250 #define RULE_FIELDS 10
251 
252 /*
253  * Which fields are which on a Link line.
254  */
255 
256 #define LF_FROM 1
257 #define LF_TO 2
258 #define LINK_FIELDS 3
259 
260 /*
261  * Which fields are which on a Leap line.
262  */
263 
264 #define LP_YEAR 1
265 #define LP_MONTH 2
266 #define LP_DAY 3
267 #define LP_TIME 4
268 #define LP_CORR 5
269 #define LP_ROLL 6
270 #define LEAP_FIELDS 7
271 
272 /* Expires lines are like Leap lines, except without CORR and ROLL fields. */
273 #define EXPIRES_FIELDS 5
274 
275 /*
276  * Year synonyms.
277  */
278 
279 #define YR_MINIMUM 0
280 #define YR_MAXIMUM 1
281 #define YR_ONLY 2
282 
283 static struct rule *rules;
284 static ptrdiff_t nrules; /* number of rules */
285 static ptrdiff_t nrules_alloc;
286 
287 static struct zone *zones;
288 static ptrdiff_t nzones; /* number of zones */
289 static ptrdiff_t nzones_alloc;
290 
291 struct link
292 {
293  const char *l_filename;
294  lineno_t l_linenum;
295  const char *l_from;
296  const char *l_to;
297 };
298 
299 static struct link *links;
300 static ptrdiff_t nlinks;
301 static ptrdiff_t nlinks_alloc;
302 
303 struct lookup
304 {
305  const char *l_word;
306  const int l_value;
307 };
308 
309 static struct lookup const *byword(const char *string,
310  const struct lookup *lp);
311 
312 static struct lookup const zi_line_codes[] = {
313  {"Rule", LC_RULE},
314  {"Zone", LC_ZONE},
315  {"Link", LC_LINK},
316  {NULL, 0}
317 };
318 static struct lookup const leap_line_codes[] = {
319  {"Leap", LC_LEAP},
320  {"Expires", LC_EXPIRES},
321  {NULL, 0}
322 };
323 
324 static struct lookup const mon_names[] = {
325  {"January", TM_JANUARY},
326  {"February", TM_FEBRUARY},
327  {"March", TM_MARCH},
328  {"April", TM_APRIL},
329  {"May", TM_MAY},
330  {"June", TM_JUNE},
331  {"July", TM_JULY},
332  {"August", TM_AUGUST},
333  {"September", TM_SEPTEMBER},
334  {"October", TM_OCTOBER},
335  {"November", TM_NOVEMBER},
336  {"December", TM_DECEMBER},
337  {NULL, 0}
338 };
339 
340 static struct lookup const wday_names[] = {
341  {"Sunday", TM_SUNDAY},
342  {"Monday", TM_MONDAY},
343  {"Tuesday", TM_TUESDAY},
344  {"Wednesday", TM_WEDNESDAY},
345  {"Thursday", TM_THURSDAY},
346  {"Friday", TM_FRIDAY},
347  {"Saturday", TM_SATURDAY},
348  {NULL, 0}
349 };
350 
351 static struct lookup const lasts[] = {
352  {"last-Sunday", TM_SUNDAY},
353  {"last-Monday", TM_MONDAY},
354  {"last-Tuesday", TM_TUESDAY},
355  {"last-Wednesday", TM_WEDNESDAY},
356  {"last-Thursday", TM_THURSDAY},
357  {"last-Friday", TM_FRIDAY},
358  {"last-Saturday", TM_SATURDAY},
359  {NULL, 0}
360 };
361 
362 static struct lookup const begin_years[] = {
363  {"minimum", YR_MINIMUM},
364  {"maximum", YR_MAXIMUM},
365  {NULL, 0}
366 };
367 
368 static struct lookup const end_years[] = {
369  {"minimum", YR_MINIMUM},
370  {"maximum", YR_MAXIMUM},
371  {"only", YR_ONLY},
372  {NULL, 0}
373 };
374 
375 static struct lookup const leap_types[] = {
376  {"Rolling", true},
377  {"Stationary", false},
378  {NULL, 0}
379 };
380 
381 static const int len_months[2][MONSPERYEAR] = {
382  {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
383  {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
384 };
385 
386 static const int len_years[2] = {
388 };
389 
390 static struct attype
391 {
393  bool dontmerge;
394  unsigned char type;
395 } *attypes;
397 static char isdsts[TZ_MAX_TYPES];
398 static unsigned char desigidx[TZ_MAX_TYPES];
399 static bool ttisstds[TZ_MAX_TYPES];
400 static bool ttisuts[TZ_MAX_TYPES];
401 static char chars[TZ_MAX_CHARS];
404 static char roll[TZ_MAX_LEAPS];
405 
406 /*
407  * Memory allocation.
408  */
409 
410 static void
411 memory_exhausted(const char *msg)
412 {
413  fprintf(stderr, _("%s: Memory exhausted: %s\n"), progname, msg);
414  exit(EXIT_FAILURE);
415 }
416 
417 static size_t
418 size_product(size_t nitems, size_t itemsize)
419 {
420  if (SIZE_MAX / itemsize < nitems)
421  memory_exhausted(_("size overflow"));
422  return nitems * itemsize;
423 }
424 
425 static void *
426 memcheck(void *ptr)
427 {
428  if (ptr == NULL)
429  memory_exhausted(strerror(errno));
430  return ptr;
431 }
432 
433 static void *
434 emalloc(size_t size)
435 {
436  return memcheck(malloc(size));
437 }
438 
439 static void *
440 erealloc(void *ptr, size_t size)
441 {
442  return memcheck(realloc(ptr, size));
443 }
444 
445 static char *
446 ecpyalloc(char const *str)
447 {
448  return memcheck(strdup(str));
449 }
450 
451 static void *
452 growalloc(void *ptr, size_t itemsize, ptrdiff_t nitems, ptrdiff_t *nitems_alloc)
453 {
454  if (nitems < *nitems_alloc)
455  return ptr;
456  else
457  {
458  ptrdiff_t nitems_max = PTRDIFF_MAX - WORK_AROUND_QTBUG_53071;
459  ptrdiff_t amax = nitems_max < SIZE_MAX ? nitems_max : SIZE_MAX;
460 
461  if ((amax - 1) / 3 * 2 < *nitems_alloc)
462  memory_exhausted(_("integer overflow"));
463  *nitems_alloc += (*nitems_alloc >> 1) + 1;
464  return erealloc(ptr, size_product(*nitems_alloc, itemsize));
465  }
466 }
467 
468 /*
469  * Error handling.
470  */
471 
472 static void
473 eats(char const *name, lineno_t num, char const *rname, lineno_t rnum)
474 {
475  filename = name;
476  linenum = num;
477  rfilename = rname;
478  rlinenum = rnum;
479 }
480 
481 static void
482 eat(char const *name, lineno_t num)
483 {
484  eats(name, num, NULL, -1);
485 }
486 
487 static void
488 verror(const char *string, va_list args)
489 {
490  /*
491  * Match the format of "cc" to allow sh users to zic ... 2>&1 | error -t
492  * "*" -v on BSD systems.
493  */
494  if (filename)
495  fprintf(stderr, _("\"%s\", line %d: "), filename, linenum);
496  vfprintf(stderr, string, args);
497  if (rfilename != NULL)
498  fprintf(stderr, _(" (rule from \"%s\", line %d)"),
499  rfilename, rlinenum);
500  fprintf(stderr, "\n");
501 }
502 
503 static void
504 error(const char *string,...)
505 {
506  va_list args;
507 
508  va_start(args, string);
509  verror(string, args);
510  va_end(args);
511  errors = true;
512 }
513 
514 static void
515 warning(const char *string,...)
516 {
517  va_list args;
518 
519  fprintf(stderr, _("warning: "));
520  va_start(args, string);
521  verror(string, args);
522  va_end(args);
523  warnings = true;
524 }
525 
526 static void
527 close_file(FILE *stream, char const *dir, char const *name)
528 {
529  char const *e = (ferror(stream) ? _("I/O error")
530  : fclose(stream) != 0 ? strerror(errno) : NULL);
531 
532  if (e)
533  {
534  fprintf(stderr, "%s: %s%s%s%s%s\n", progname,
535  dir ? dir : "", dir ? "/" : "",
536  name ? name : "", name ? ": " : "",
537  e);
538  exit(EXIT_FAILURE);
539  }
540 }
541 
542 static void
543 usage(FILE *stream, int status)
544 {
545  fprintf(stream,
546  _("%s: usage is %s [ --version ] [ --help ] [ -v ] [ -P ] \\\n"
547  "\t[ -b {slim|fat} ] [ -d directory ] [ -l localtime ]"
548  " [ -L leapseconds ] \\\n"
549  "\t[ -p posixrules ] [ -r '[@lo][/@hi]' ] [ -t localtime-link ] \\\n"
550  "\t[ filename ... ]\n\n"
551  "Report bugs to %s.\n"),
552  progname, progname, PACKAGE_BUGREPORT);
553  if (status == EXIT_SUCCESS)
554  close_file(stream, NULL, NULL);
555  exit(status);
556 }
557 
558 /* Change the working directory to DIR, possibly creating DIR and its
559  ancestors. After this is done, all files are accessed with names
560  relative to DIR. */
561 static void
562 change_directory(char const *dir)
563 {
564  if (chdir(dir) != 0)
565  {
566  int chdir_errno = errno;
567 
568  if (chdir_errno == ENOENT)
569  {
570  mkdirs(dir, false);
571  chdir_errno = chdir(dir) == 0 ? 0 : errno;
572  }
573  if (chdir_errno != 0)
574  {
575  fprintf(stderr, _("%s: Can't chdir to %s: %s\n"),
576  progname, dir, strerror(chdir_errno));
577  exit(EXIT_FAILURE);
578  }
579  }
580 }
581 
582 #define TIME_T_BITS_IN_FILE 64
583 
584 /* The minimum and maximum values representable in a TZif file. */
587 
588 /* The minimum, and one less than the maximum, values specified by
589  the -r option. These default to MIN_TIME and MAX_TIME. */
592 
593 /* The time specified by an Expires line, or negative if no such line. */
594 static zic_t leapexpires = -1;
595 
596 /* The time specified by an #expires comment, or negative if no such line. */
598 
599 /* Set the time range of the output to TIMERANGE.
600  Return true if successful. */
601 static bool
603 {
604  int64 lo = min_time,
605  hi = max_time;
606  char *lo_end = timerange,
607  *hi_end;
608 
609  if (*timerange == '@')
610  {
611  errno = 0;
612  lo = strtoimax(timerange + 1, &lo_end, 10);
613  if (lo_end == timerange + 1 || (lo == PG_INT64_MAX && errno == ERANGE))
614  return false;
615  }
616  hi_end = lo_end;
617  if (lo_end[0] == '/' && lo_end[1] == '@')
618  {
619  errno = 0;
620  hi = strtoimax(lo_end + 2, &hi_end, 10);
621  if (hi_end == lo_end + 2 || hi == PG_INT64_MIN)
622  return false;
623  hi -= !(hi == PG_INT64_MAX && errno == ERANGE);
624  }
625  if (*hi_end || hi < lo || max_time < lo || hi < min_time)
626  return false;
627  lo_time = lo < min_time ? min_time : lo;
628  hi_time = max_time < hi ? max_time : hi;
629  return true;
630 }
631 
632 static const char *psxrules;
633 static const char *lcltime;
634 static const char *directory;
635 static const char *leapsec;
636 static const char *tzdefault;
637 static const char *yitcommand;
638 
639 /* -1 if the TZif output file should be slim, 0 if default, 1 if the
640  output should be fat for backward compatibility. Currently the
641  default is fat, although this may change. */
642 static int bloat;
643 
644 static bool
646 {
647  return 0 <= bloat;
648 }
649 
650 #ifndef ZIC_BLOAT_DEFAULT
651 #define ZIC_BLOAT_DEFAULT "fat"
652 #endif
653 
654 int
655 main(int argc, char **argv)
656 {
657  int c,
658  k;
659  ptrdiff_t i,
660  j;
661  bool timerange_given = false;
662 
663 #ifndef WIN32
664  umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
665 #endif
666  progname = argv[0];
667  if (TYPE_BIT(zic_t) < 64)
668  {
669  fprintf(stderr, "%s: %s\n", progname,
670  _("wild compilation-time specification of zic_t"));
671  return EXIT_FAILURE;
672  }
673  for (k = 1; k < argc; k++)
674  if (strcmp(argv[k], "--version") == 0)
675  {
676  printf("zic %s\n", PG_VERSION);
677  close_file(stdout, NULL, NULL);
678  return EXIT_SUCCESS;
679  }
680  else if (strcmp(argv[k], "--help") == 0)
681  {
683  }
684  while ((c = getopt(argc, argv, "b:d:l:L:p:Pr:st:vy:")) != EOF && c != -1)
685  switch (c)
686  {
687  default:
688  usage(stderr, EXIT_FAILURE);
689  case 'b':
690  if (strcmp(optarg, "slim") == 0)
691  {
692  if (0 < bloat)
693  error(_("incompatible -b options"));
694  bloat = -1;
695  }
696  else if (strcmp(optarg, "fat") == 0)
697  {
698  if (bloat < 0)
699  error(_("incompatible -b options"));
700  bloat = 1;
701  }
702  else
703  error(_("invalid option: -b '%s'"), optarg);
704  break;
705  case 'd':
706  if (directory == NULL)
707  directory = strdup(optarg);
708  else
709  {
710  fprintf(stderr,
711  _("%s: More than one -d option specified\n"),
712  progname);
713  return EXIT_FAILURE;
714  }
715  break;
716  case 'l':
717  if (lcltime == NULL)
718  lcltime = strdup(optarg);
719  else
720  {
721  fprintf(stderr,
722  _("%s: More than one -l option specified\n"),
723  progname);
724  return EXIT_FAILURE;
725  }
726  break;
727  case 'p':
728  if (psxrules == NULL)
729  psxrules = strdup(optarg);
730  else
731  {
732  fprintf(stderr,
733  _("%s: More than one -p option specified\n"),
734  progname);
735  return EXIT_FAILURE;
736  }
737  break;
738  case 't':
739  if (tzdefault != NULL)
740  {
741  fprintf(stderr,
742  _("%s: More than one -t option"
743  " specified\n"),
744  progname);
745  return EXIT_FAILURE;
746  }
747  tzdefault = optarg;
748  break;
749  case 'y':
750  if (yitcommand == NULL)
751  {
752  warning(_("-y is obsolescent"));
753  yitcommand = strdup(optarg);
754  }
755  else
756  {
757  fprintf(stderr,
758  _("%s: More than one -y option specified\n"),
759  progname);
760  return EXIT_FAILURE;
761  }
762  break;
763  case 'L':
764  if (leapsec == NULL)
765  leapsec = strdup(optarg);
766  else
767  {
768  fprintf(stderr,
769  _("%s: More than one -L option specified\n"),
770  progname);
771  return EXIT_FAILURE;
772  }
773  break;
774  case 'v':
775  noise = true;
776  break;
777  case 'P':
778  print_abbrevs = true;
779  print_cutoff = time(NULL);
780  break;
781  case 'r':
782  if (timerange_given)
783  {
784  fprintf(stderr,
785  _("%s: More than one -r option specified\n"),
786  progname);
787  return EXIT_FAILURE;
788  }
789  if (!timerange_option(optarg))
790  {
791  fprintf(stderr,
792  _("%s: invalid time range: %s\n"),
793  progname, optarg);
794  return EXIT_FAILURE;
795  }
796  timerange_given = true;
797  break;
798  case 's':
799  warning(_("-s ignored"));
800  break;
801  }
802  if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
803  usage(stderr, EXIT_FAILURE); /* usage message by request */
804  if (bloat == 0)
805  bloat = strcmp(ZIC_BLOAT_DEFAULT, "slim") == 0 ? -1 : 1;
806  if (directory == NULL)
807  directory = "data";
808  if (tzdefault == NULL)
809  tzdefault = TZDEFAULT;
810  if (yitcommand == NULL)
811  yitcommand = "yearistype";
812 
813  if (optind < argc && leapsec != NULL)
814  {
815  infile(leapsec);
816  adjleap();
817  }
818 
819  for (k = optind; k < argc; k++)
820  infile(argv[k]);
821  if (errors)
822  return EXIT_FAILURE;
823  associate();
824  change_directory(directory);
825  for (i = 0; i < nzones; i = j)
826  {
827  /*
828  * Find the next non-continuation zone entry.
829  */
830  for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
831  continue;
832  outzone(&zones[i], j - i);
833  }
834 
835  /*
836  * Make links.
837  */
838  for (i = 0; i < nlinks; ++i)
839  {
840  eat(links[i].l_filename, links[i].l_linenum);
841  dolink(links[i].l_from, links[i].l_to, false);
842  if (noise)
843  for (j = 0; j < nlinks; ++j)
844  if (strcmp(links[i].l_to,
845  links[j].l_from) == 0)
846  warning(_("link to link"));
847  }
848  if (lcltime != NULL)
849  {
850  eat(_("command line"), 1);
851  dolink(lcltime, tzdefault, true);
852  }
853  if (psxrules != NULL)
854  {
855  eat(_("command line"), 1);
856  dolink(psxrules, TZDEFRULES, true);
857  }
858  if (warnings && (ferror(stderr) || fclose(stderr) != 0))
859  return EXIT_FAILURE;
860  return errors ? EXIT_FAILURE : EXIT_SUCCESS;
861 }
862 
863 static bool
864 componentcheck(char const *name, char const *component,
865  char const *component_end)
866 {
867  enum
868  {
869  component_len_max = 14};
870  ptrdiff_t component_len = component_end - component;
871 
872  if (component_len == 0)
873  {
874  if (!*name)
875  error(_("empty file name"));
876  else
877  error(_(component == name
878  ? "file name '%s' begins with '/'"
879  : *component_end
880  ? "file name '%s' contains '//'"
881  : "file name '%s' ends with '/'"),
882  name);
883  return false;
884  }
885  if (0 < component_len && component_len <= 2
886  && component[0] == '.' && component_end[-1] == '.')
887  {
888  int len = component_len;
889 
890  error(_("file name '%s' contains '%.*s' component"),
891  name, len, component);
892  return false;
893  }
894  if (noise)
895  {
896  if (0 < component_len && component[0] == '-')
897  warning(_("file name '%s' component contains leading '-'"),
898  name);
899  if (component_len_max < component_len)
900  warning(_("file name '%s' contains overlength component"
901  " '%.*s...'"),
902  name, component_len_max, component);
903  }
904  return true;
905 }
906 
907 static bool
908 namecheck(const char *name)
909 {
910  char const *cp;
911 
912  /* Benign characters in a portable file name. */
913  static char const benign[] =
914  "-/_"
915  "abcdefghijklmnopqrstuvwxyz"
916  "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
917 
918  /*
919  * Non-control chars in the POSIX portable character set, excluding the
920  * benign characters.
921  */
922  static char const printable_and_not_benign[] =
923  " !\"#$%&'()*+,.0123456789:;<=>?@[\\]^`{|}~";
924 
925  char const *component = name;
926 
927  for (cp = name; *cp; cp++)
928  {
929  unsigned char c = *cp;
930 
931  if (noise && !strchr(benign, c))
932  {
933  warning((strchr(printable_and_not_benign, c)
934  ? _("file name '%s' contains byte '%c'")
935  : _("file name '%s' contains byte '\\%o'")),
936  name, c);
937  }
938  if (c == '/')
939  {
940  if (!componentcheck(name, component, cp))
941  return false;
942  component = cp + 1;
943  }
944  }
945  return componentcheck(name, component, cp);
946 }
947 
948 /*
949  * Create symlink contents suitable for symlinking FROM to TO, as a
950  * freshly allocated string. FROM should be a relative file name, and
951  * is relative to the global variable DIRECTORY. TO can be either
952  * relative or absolute.
953  */
954 #ifdef HAVE_SYMLINK
955 static char *
956 relname(char const *from, char const *to)
957 {
958  size_t i,
959  taillen,
960  dotdotetcsize;
961  size_t dir_len = 0,
962  dotdots = 0,
963  linksize = SIZE_MAX;
964  char const *f = from;
965  char *result = NULL;
966 
967  if (*to == '/')
968  {
969  /* Make F absolute too. */
970  size_t len = strlen(directory);
971  bool needslash = len && directory[len - 1] != '/';
972 
973  linksize = len + needslash + strlen(from) + 1;
974  f = result = emalloc(linksize);
975  strcpy(result, directory);
976  result[len] = '/';
977  strcpy(result + len + needslash, from);
978  }
979  for (i = 0; f[i] && f[i] == to[i]; i++)
980  if (f[i] == '/')
981  dir_len = i + 1;
982  for (; to[i]; i++)
983  dotdots += to[i] == '/' && to[i - 1] != '/';
984  taillen = strlen(f + dir_len);
985  dotdotetcsize = 3 * dotdots + taillen + 1;
986  if (dotdotetcsize <= linksize)
987  {
988  if (!result)
989  result = emalloc(dotdotetcsize);
990  for (i = 0; i < dotdots; i++)
991  memcpy(result + 3 * i, "../", 3);
992  memmove(result + 3 * dotdots, f + dir_len, taillen + 1);
993  }
994  return result;
995 }
996 #endif /* HAVE_SYMLINK */
997 
998 /* Hard link FROM to TO, following any symbolic links.
999  Return 0 if successful, an error number otherwise. */
1000 static int
1001 hardlinkerr(char const *from, char const *to)
1002 {
1003  int r = linkat(AT_FDCWD, from, AT_FDCWD, to, AT_SYMLINK_FOLLOW);
1004 
1005  return r == 0 ? 0 : errno;
1006 }
1007 
1008 static void
1009 dolink(char const *fromfield, char const *tofield, bool staysymlink)
1010 {
1011  bool todirs_made = false;
1012  int link_errno;
1013 
1014  /*
1015  * We get to be careful here since there's a fair chance of root running
1016  * us.
1017  */
1018  if (itsdir(fromfield))
1019  {
1020  fprintf(stderr, _("%s: link from %s/%s failed: %s\n"),
1021  progname, directory, fromfield, strerror(EPERM));
1022  exit(EXIT_FAILURE);
1023  }
1024  if (staysymlink)
1025  staysymlink = itssymlink(tofield);
1026  if (remove(tofield) == 0)
1027  todirs_made = true;
1028  else if (errno != ENOENT)
1029  {
1030  char const *e = strerror(errno);
1031 
1032  fprintf(stderr, _("%s: Can't remove %s/%s: %s\n"),
1033  progname, directory, tofield, e);
1034  exit(EXIT_FAILURE);
1035  }
1036  link_errno = staysymlink ? ENOTSUP : hardlinkerr(fromfield, tofield);
1037  if (link_errno == ENOENT && !todirs_made)
1038  {
1039  mkdirs(tofield, true);
1040  todirs_made = true;
1041  link_errno = hardlinkerr(fromfield, tofield);
1042  }
1043  if (link_errno != 0)
1044  {
1045 #ifdef HAVE_SYMLINK
1046  bool absolute = *fromfield == '/';
1047  char *linkalloc = absolute ? NULL : relname(fromfield, tofield);
1048  char const *contents = absolute ? fromfield : linkalloc;
1049  int symlink_errno = symlink(contents, tofield) == 0 ? 0 : errno;
1050 
1051  if (!todirs_made
1052  && (symlink_errno == ENOENT || symlink_errno == ENOTSUP))
1053  {
1054  mkdirs(tofield, true);
1055  if (symlink_errno == ENOENT)
1056  symlink_errno = symlink(contents, tofield) == 0 ? 0 : errno;
1057  }
1058  free(linkalloc);
1059  if (symlink_errno == 0)
1060  {
1061  if (link_errno != ENOTSUP)
1062  warning(_("symbolic link used because hard link failed: %s"),
1063  strerror(link_errno));
1064  }
1065  else
1066 #endif /* HAVE_SYMLINK */
1067  {
1068  FILE *fp,
1069  *tp;
1070  int c;
1071 
1072  fp = fopen(fromfield, "rb");
1073  if (!fp)
1074  {
1075  char const *e = strerror(errno);
1076 
1077  fprintf(stderr, _("%s: Can't read %s/%s: %s\n"),
1078  progname, directory, fromfield, e);
1079  exit(EXIT_FAILURE);
1080  }
1081  tp = fopen(tofield, "wb");
1082  if (!tp)
1083  {
1084  char const *e = strerror(errno);
1085 
1086  fprintf(stderr, _("%s: Can't create %s/%s: %s\n"),
1087  progname, directory, tofield, e);
1088  exit(EXIT_FAILURE);
1089  }
1090  while ((c = getc(fp)) != EOF)
1091  putc(c, tp);
1092  close_file(fp, directory, fromfield);
1093  close_file(tp, directory, tofield);
1094  if (link_errno != ENOTSUP)
1095  warning(_("copy used because hard link failed: %s"),
1096  strerror(link_errno));
1097 #ifdef HAVE_SYMLINK
1098  else if (symlink_errno != ENOTSUP)
1099  warning(_("copy used because symbolic link failed: %s"),
1100  strerror(symlink_errno));
1101 #endif
1102  }
1103  }
1104 }
1105 
1106 /* Return true if NAME is a directory. */
1107 static bool
1108 itsdir(char const *name)
1109 {
1110  struct stat st;
1111  int res = stat(name, &st);
1112 #ifdef S_ISDIR
1113  if (res == 0)
1114  return S_ISDIR(st.st_mode) != 0;
1115 #endif
1116  if (res == 0 || errno == EOVERFLOW)
1117  {
1118  size_t n = strlen(name);
1119  char *nameslashdot = emalloc(n + 3);
1120  bool dir;
1121 
1122  memcpy(nameslashdot, name, n);
1123  strcpy(&nameslashdot[n], &"/."[!(n && name[n - 1] != '/')]);
1124  dir = stat(nameslashdot, &st) == 0 || errno == EOVERFLOW;
1125  free(nameslashdot);
1126  return dir;
1127  }
1128  return false;
1129 }
1130 
1131 /* Return true if NAME is a symbolic link. */
1132 static bool
1133 itssymlink(char const *name)
1134 {
1135 #ifdef HAVE_SYMLINK
1136  char c;
1137 
1138  return 0 <= readlink(name, &c, 1);
1139 #else
1140  return false;
1141 #endif
1142 }
1143 
1144 /*
1145  * Associate sets of rules with zones.
1146  */
1147 
1148 /*
1149  * Sort by rule name.
1150  */
1151 
1152 static int
1153 rcomp(const void *cp1, const void *cp2)
1154 {
1155  return strcmp(((const struct rule *) cp1)->r_name,
1156  ((const struct rule *) cp2)->r_name);
1157 }
1158 
1159 static void
1161 {
1162  struct zone *zp;
1163  struct rule *rp;
1164  ptrdiff_t i,
1165  j,
1166  base,
1167  out;
1168 
1169  if (nrules != 0)
1170  {
1171  qsort(rules, nrules, sizeof *rules, rcomp);
1172  for (i = 0; i < nrules - 1; ++i)
1173  {
1174  if (strcmp(rules[i].r_name,
1175  rules[i + 1].r_name) != 0)
1176  continue;
1177  if (strcmp(rules[i].r_filename,
1178  rules[i + 1].r_filename) == 0)
1179  continue;
1180  eat(rules[i].r_filename, rules[i].r_linenum);
1181  warning(_("same rule name in multiple files"));
1182  eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
1183  warning(_("same rule name in multiple files"));
1184  for (j = i + 2; j < nrules; ++j)
1185  {
1186  if (strcmp(rules[i].r_name,
1187  rules[j].r_name) != 0)
1188  break;
1189  if (strcmp(rules[i].r_filename,
1190  rules[j].r_filename) == 0)
1191  continue;
1192  if (strcmp(rules[i + 1].r_filename,
1193  rules[j].r_filename) == 0)
1194  continue;
1195  break;
1196  }
1197  i = j - 1;
1198  }
1199  }
1200  for (i = 0; i < nzones; ++i)
1201  {
1202  zp = &zones[i];
1203  zp->z_rules = NULL;
1204  zp->z_nrules = 0;
1205  }
1206  for (base = 0; base < nrules; base = out)
1207  {
1208  rp = &rules[base];
1209  for (out = base + 1; out < nrules; ++out)
1210  if (strcmp(rp->r_name, rules[out].r_name) != 0)
1211  break;
1212  for (i = 0; i < nzones; ++i)
1213  {
1214  zp = &zones[i];
1215  if (strcmp(zp->z_rule, rp->r_name) != 0)
1216  continue;
1217  zp->z_rules = rp;
1218  zp->z_nrules = out - base;
1219  }
1220  }
1221  for (i = 0; i < nzones; ++i)
1222  {
1223  zp = &zones[i];
1224  if (zp->z_nrules == 0)
1225  {
1226  /*
1227  * Maybe we have a local standard time offset.
1228  */
1229  eat(zp->z_filename, zp->z_linenum);
1230  zp->z_save = getsave(zp->z_rule, &zp->z_isdst);
1231 
1232  /*
1233  * Note, though, that if there's no rule, a '%s' in the format is
1234  * a bad thing.
1235  */
1236  if (zp->z_format_specifier == 's')
1237  error("%s", _("%s in ruleless zone"));
1238  }
1239  }
1240  if (errors)
1241  exit(EXIT_FAILURE);
1242 }
1243 
1244 static void
1245 infile(const char *name)
1246 {
1247  FILE *fp;
1248  char **fields;
1249  char *cp;
1250  const struct lookup *lp;
1251  int nfields;
1252  bool wantcont;
1253  lineno_t num;
1254  char buf[BUFSIZ];
1255 
1256  if (strcmp(name, "-") == 0)
1257  {
1258  name = _("standard input");
1259  fp = stdin;
1260  }
1261  else if ((fp = fopen(name, "r")) == NULL)
1262  {
1263  const char *e = strerror(errno);
1264 
1265  fprintf(stderr, _("%s: Cannot open %s: %s\n"),
1266  progname, name, e);
1267  exit(EXIT_FAILURE);
1268  }
1269  wantcont = false;
1270  for (num = 1;; ++num)
1271  {
1272  eat(name, num);
1273  if (fgets(buf, sizeof buf, fp) != buf)
1274  break;
1275  cp = strchr(buf, '\n');
1276  if (cp == NULL)
1277  {
1278  error(_("line too long"));
1279  exit(EXIT_FAILURE);
1280  }
1281  *cp = '\0';
1282  fields = getfields(buf);
1283  nfields = 0;
1284  while (fields[nfields] != NULL)
1285  {
1286  static char nada;
1287 
1288  if (strcmp(fields[nfields], "-") == 0)
1289  fields[nfields] = &nada;
1290  ++nfields;
1291  }
1292  if (nfields == 0)
1293  {
1294  if (name == leapsec && *buf == '#')
1295  {
1296  /*
1297  * PG: INT64_FORMAT isn't portable for sscanf, so be content
1298  * with scanning a "long". Once we are requiring C99 in all
1299  * live branches, it'd be sensible to adopt upstream's
1300  * practice of using the <inttypes.h> macros. But for now, we
1301  * don't actually use this code, and it won't overflow before
1302  * 2038 anyway.
1303  */
1304  long cl_tmp;
1305 
1306  sscanf(buf, "#expires %ld", &cl_tmp);
1307  comment_leapexpires = cl_tmp;
1308  }
1309  }
1310  else if (wantcont)
1311  {
1312  wantcont = inzcont(fields, nfields);
1313  }
1314  else
1315  {
1316  struct lookup const *line_codes
1317  = name == leapsec ? leap_line_codes : zi_line_codes;
1318 
1319  lp = byword(fields[0], line_codes);
1320  if (lp == NULL)
1321  error(_("input line of unknown type"));
1322  else
1323  switch (lp->l_value)
1324  {
1325  case LC_RULE:
1326  inrule(fields, nfields);
1327  wantcont = false;
1328  break;
1329  case LC_ZONE:
1330  wantcont = inzone(fields, nfields);
1331  break;
1332  case LC_LINK:
1333  inlink(fields, nfields);
1334  wantcont = false;
1335  break;
1336  case LC_LEAP:
1337  inleap(fields, nfields);
1338  wantcont = false;
1339  break;
1340  case LC_EXPIRES:
1341  inexpires(fields, nfields);
1342  wantcont = false;
1343  break;
1344  default: /* "cannot happen" */
1345  fprintf(stderr,
1346  _("%s: panic: Invalid l_value %d\n"),
1347  progname, lp->l_value);
1348  exit(EXIT_FAILURE);
1349  }
1350  }
1351  free(fields);
1352  }
1353  close_file(fp, NULL, filename);
1354  if (wantcont)
1355  error(_("expected continuation line not found"));
1356 }
1357 
1358 /*
1359  * Convert a string of one of the forms
1360  * h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
1361  * into a number of seconds.
1362  * A null string maps to zero.
1363  * Call error with errstring and return zero on errors.
1364  */
1365 
1366 static zic_t
1367 gethms(char const *string, char const *errstring)
1368 {
1369  /* PG: make hh be int not zic_t to avoid sscanf portability issues */
1370  int hh;
1371  int sign,
1372  mm = 0,
1373  ss = 0;
1374  char hhx,
1375  mmx,
1376  ssx,
1377  xr = '0',
1378  xs;
1379  int tenths = 0;
1380  bool ok = true;
1381 
1382  if (string == NULL || *string == '\0')
1383  return 0;
1384  if (*string == '-')
1385  {
1386  sign = -1;
1387  ++string;
1388  }
1389  else
1390  sign = 1;
1391  switch (sscanf(string,
1392  "%d%c%d%c%d%c%1d%*[0]%c%*[0123456789]%c",
1393  &hh, &hhx, &mm, &mmx, &ss, &ssx, &tenths, &xr, &xs))
1394  {
1395  default:
1396  ok = false;
1397  break;
1398  case 8:
1399  ok = '0' <= xr && xr <= '9';
1400  /* fallthrough */
1401  case 7:
1402  ok &= ssx == '.';
1403  if (ok && noise)
1404  warning(_("fractional seconds rejected by"
1405  " pre-2018 versions of zic"));
1406  /* fallthrough */
1407  case 5:
1408  ok &= mmx == ':';
1409  /* fallthrough */
1410  case 3:
1411  ok &= hhx == ':';
1412  /* fallthrough */
1413  case 1:
1414  break;
1415  }
1416  if (!ok)
1417  {
1418  error("%s", errstring);
1419  return 0;
1420  }
1421  if (hh < 0 ||
1422  mm < 0 || mm >= MINSPERHOUR ||
1423  ss < 0 || ss > SECSPERMIN)
1424  {
1425  error("%s", errstring);
1426  return 0;
1427  }
1428  /* Some compilers warn that this test is unsatisfiable for 32-bit ints */
1429 #if INT_MAX > PG_INT32_MAX
1430  if (ZIC_MAX / SECSPERHOUR < hh)
1431  {
1432  error(_("time overflow"));
1433  return 0;
1434  }
1435 #endif
1436  ss += 5 + ((ss ^ 1) & (xr == '0')) <= tenths; /* Round to even. */
1437  if (noise && (hh > HOURSPERDAY ||
1438  (hh == HOURSPERDAY && (mm != 0 || ss != 0))))
1439  warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
1440  return oadd(sign * (zic_t) hh * SECSPERHOUR,
1441  sign * (mm * SECSPERMIN + ss));
1442 }
1443 
1444 static zic_t
1445 getsave(char *field, bool *isdst)
1446 {
1447  int dst = -1;
1448  zic_t save;
1449  size_t fieldlen = strlen(field);
1450 
1451  if (fieldlen != 0)
1452  {
1453  char *ep = field + fieldlen - 1;
1454 
1455  switch (*ep)
1456  {
1457  case 'd':
1458  dst = 1;
1459  *ep = '\0';
1460  break;
1461  case 's':
1462  dst = 0;
1463  *ep = '\0';
1464  break;
1465  }
1466  }
1467  save = gethms(field, _("invalid saved time"));
1468  *isdst = dst < 0 ? save != 0 : dst;
1469  return save;
1470 }
1471 
1472 static void
1473 inrule(char **fields, int nfields)
1474 {
1475  static struct rule r;
1476 
1477  if (nfields != RULE_FIELDS)
1478  {
1479  error(_("wrong number of fields on Rule line"));
1480  return;
1481  }
1482  switch (*fields[RF_NAME])
1483  {
1484  case '\0':
1485  case ' ':
1486  case '\f':
1487  case '\n':
1488  case '\r':
1489  case '\t':
1490  case '\v':
1491  case '+':
1492  case '-':
1493  case '0':
1494  case '1':
1495  case '2':
1496  case '3':
1497  case '4':
1498  case '5':
1499  case '6':
1500  case '7':
1501  case '8':
1502  case '9':
1503  error(_("Invalid rule name \"%s\""), fields[RF_NAME]);
1504  return;
1505  }
1506  r.r_filename = filename;
1507  r.r_linenum = linenum;
1508  r.r_save = getsave(fields[RF_SAVE], &r.r_isdst);
1509  rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
1510  fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
1511  r.r_name = ecpyalloc(fields[RF_NAME]);
1512  r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
1513  if (max_abbrvar_len < strlen(r.r_abbrvar))
1514  max_abbrvar_len = strlen(r.r_abbrvar);
1515  rules = growalloc(rules, sizeof *rules, nrules, &nrules_alloc);
1516  rules[nrules++] = r;
1517 }
1518 
1519 static bool
1520 inzone(char **fields, int nfields)
1521 {
1522  ptrdiff_t i;
1523 
1524  if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS)
1525  {
1526  error(_("wrong number of fields on Zone line"));
1527  return false;
1528  }
1529  if (lcltime != NULL && strcmp(fields[ZF_NAME], tzdefault) == 0)
1530  {
1531  error(
1532  _("\"Zone %s\" line and -l option are mutually exclusive"),
1533  tzdefault);
1534  return false;
1535  }
1536  if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL)
1537  {
1538  error(
1539  _("\"Zone %s\" line and -p option are mutually exclusive"),
1540  TZDEFRULES);
1541  return false;
1542  }
1543  for (i = 0; i < nzones; ++i)
1544  if (zones[i].z_name != NULL &&
1545  strcmp(zones[i].z_name, fields[ZF_NAME]) == 0)
1546  {
1547  error(_("duplicate zone name %s"
1548  " (file \"%s\", line %d)"),
1549  fields[ZF_NAME],
1550  zones[i].z_filename,
1551  zones[i].z_linenum);
1552  return false;
1553  }
1554  return inzsub(fields, nfields, false);
1555 }
1556 
1557 static bool
1558 inzcont(char **fields, int nfields)
1559 {
1560  if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS)
1561  {
1562  error(_("wrong number of fields on Zone continuation line"));
1563  return false;
1564  }
1565  return inzsub(fields, nfields, true);
1566 }
1567 
1568 static bool
1569 inzsub(char **fields, int nfields, bool iscont)
1570 {
1571  char *cp;
1572  char *cp1;
1573  static struct zone z;
1574  int i_stdoff,
1575  i_rule,
1576  i_format;
1577  int i_untilyear,
1578  i_untilmonth;
1579  int i_untilday,
1580  i_untiltime;
1581  bool hasuntil;
1582 
1583  if (iscont)
1584  {
1585  i_stdoff = ZFC_STDOFF;
1586  i_rule = ZFC_RULE;
1587  i_format = ZFC_FORMAT;
1588  i_untilyear = ZFC_TILYEAR;
1589  i_untilmonth = ZFC_TILMONTH;
1590  i_untilday = ZFC_TILDAY;
1591  i_untiltime = ZFC_TILTIME;
1592  z.z_name = NULL;
1593  }
1594  else if (!namecheck(fields[ZF_NAME]))
1595  return false;
1596  else
1597  {
1598  i_stdoff = ZF_STDOFF;
1599  i_rule = ZF_RULE;
1600  i_format = ZF_FORMAT;
1601  i_untilyear = ZF_TILYEAR;
1602  i_untilmonth = ZF_TILMONTH;
1603  i_untilday = ZF_TILDAY;
1604  i_untiltime = ZF_TILTIME;
1605  z.z_name = ecpyalloc(fields[ZF_NAME]);
1606  }
1607  z.z_filename = filename;
1608  z.z_linenum = linenum;
1609  z.z_stdoff = gethms(fields[i_stdoff], _("invalid UT offset"));
1610  if ((cp = strchr(fields[i_format], '%')) != NULL)
1611  {
1612  if ((*++cp != 's' && *cp != 'z') || strchr(cp, '%')
1613  || strchr(fields[i_format], '/'))
1614  {
1615  error(_("invalid abbreviation format"));
1616  return false;
1617  }
1618  }
1619  z.z_rule = ecpyalloc(fields[i_rule]);
1620  z.z_format = cp1 = ecpyalloc(fields[i_format]);
1621  z.z_format_specifier = cp ? *cp : '\0';
1622  if (z.z_format_specifier == 'z')
1623  {
1624  if (noise)
1625  warning(_("format '%s' not handled by pre-2015 versions of zic"),
1626  z.z_format);
1627  cp1[cp - fields[i_format]] = 's';
1628  }
1629  if (max_format_len < strlen(z.z_format))
1630  max_format_len = strlen(z.z_format);
1631  hasuntil = nfields > i_untilyear;
1632  if (hasuntil)
1633  {
1636  rulesub(&z.z_untilrule,
1637  fields[i_untilyear],
1638  "only",
1639  "",
1640  (nfields > i_untilmonth) ?
1641  fields[i_untilmonth] : "Jan",
1642  (nfields > i_untilday) ? fields[i_untilday] : "1",
1643  (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1645  z.z_untilrule.r_loyear);
1646  if (iscont && nzones > 0 &&
1647  z.z_untiltime > min_time &&
1648  z.z_untiltime < max_time &&
1649  zones[nzones - 1].z_untiltime > min_time &&
1650  zones[nzones - 1].z_untiltime < max_time &&
1651  zones[nzones - 1].z_untiltime >= z.z_untiltime)
1652  {
1653  error(_("Zone continuation line end time is not after end time of previous line"));
1654  return false;
1655  }
1656  }
1657  zones = growalloc(zones, sizeof *zones, nzones, &nzones_alloc);
1658  zones[nzones++] = z;
1659 
1660  /*
1661  * If there was an UNTIL field on this line, there's more information
1662  * about the zone on the next line.
1663  */
1664  return hasuntil;
1665 }
1666 
1667 static zic_t
1668 getleapdatetime(char **fields, int nfields, bool expire_line)
1669 {
1670  const char *cp;
1671  const struct lookup *lp;
1672  zic_t i,
1673  j;
1674 
1675  /* PG: make year be int not zic_t to avoid sscanf portability issues */
1676  int year;
1677  int month,
1678  day;
1679  zic_t dayoff,
1680  tod;
1681  zic_t t;
1682  char xs;
1683 
1684  dayoff = 0;
1685  cp = fields[LP_YEAR];
1686  if (sscanf(cp, "%d%c", &year, &xs) != 1)
1687  {
1688  /*
1689  * Leapin' Lizards!
1690  */
1691  error(_("invalid leaping year"));
1692  return -1;
1693  }
1694  if (!expire_line)
1695  {
1696  if (!leapseen || leapmaxyear < year)
1697  leapmaxyear = year;
1698  if (!leapseen || leapminyear > year)
1699  leapminyear = year;
1700  leapseen = true;
1701  }
1702  j = EPOCH_YEAR;
1703  while (j != year)
1704  {
1705  if (year > j)
1706  {
1707  i = len_years[isleap(j)];
1708  ++j;
1709  }
1710  else
1711  {
1712  --j;
1713  i = -len_years[isleap(j)];
1714  }
1715  dayoff = oadd(dayoff, i);
1716  }
1717  if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL)
1718  {
1719  error(_("invalid month name"));
1720  return -1;
1721  }
1722  month = lp->l_value;
1723  j = TM_JANUARY;
1724  while (j != month)
1725  {
1726  i = len_months[isleap(year)][j];
1727  dayoff = oadd(dayoff, i);
1728  ++j;
1729  }
1730  cp = fields[LP_DAY];
1731  if (sscanf(cp, "%d%c", &day, &xs) != 1 ||
1732  day <= 0 || day > len_months[isleap(year)][month])
1733  {
1734  error(_("invalid day of month"));
1735  return -1;
1736  }
1737  dayoff = oadd(dayoff, day - 1);
1738  if (dayoff < min_time / SECSPERDAY)
1739  {
1740  error(_("time too small"));
1741  return -1;
1742  }
1743  if (dayoff > max_time / SECSPERDAY)
1744  {
1745  error(_("time too large"));
1746  return -1;
1747  }
1748  t = dayoff * SECSPERDAY;
1749  tod = gethms(fields[LP_TIME], _("invalid time of day"));
1750  t = tadd(t, tod);
1751  if (t < 0)
1752  error(_("leap second precedes Epoch"));
1753  return t;
1754 }
1755 
1756 static void
1757 inleap(char **fields, int nfields)
1758 {
1759  if (nfields != LEAP_FIELDS)
1760  error(_("wrong number of fields on Leap line"));
1761  else
1762  {
1763  zic_t t = getleapdatetime(fields, nfields, false);
1764 
1765  if (0 <= t)
1766  {
1767  struct lookup const *lp = byword(fields[LP_ROLL], leap_types);
1768 
1769  if (!lp)
1770  error(_("invalid Rolling/Stationary field on Leap line"));
1771  else
1772  {
1773  int correction = 0;
1774 
1775  if (!fields[LP_CORR][0]) /* infile() turns "-" into "". */
1776  correction = -1;
1777  else if (strcmp(fields[LP_CORR], "+") == 0)
1778  correction = 1;
1779  else
1780  error(_("invalid CORRECTION field on Leap line"));
1781  if (correction)
1782  leapadd(t, correction, lp->l_value);
1783  }
1784  }
1785  }
1786 }
1787 
1788 static void
1789 inexpires(char **fields, int nfields)
1790 {
1791  if (nfields != EXPIRES_FIELDS)
1792  error(_("wrong number of fields on Expires line"));
1793  else if (0 <= leapexpires)
1794  error(_("multiple Expires lines"));
1795  else
1796  leapexpires = getleapdatetime(fields, nfields, true);
1797 }
1798 
1799 static void
1800 inlink(char **fields, int nfields)
1801 {
1802  struct link l;
1803 
1804  if (nfields != LINK_FIELDS)
1805  {
1806  error(_("wrong number of fields on Link line"));
1807  return;
1808  }
1809  if (*fields[LF_FROM] == '\0')
1810  {
1811  error(_("blank FROM field on Link line"));
1812  return;
1813  }
1814  if (!namecheck(fields[LF_TO]))
1815  return;
1816  l.l_filename = filename;
1817  l.l_linenum = linenum;
1818  l.l_from = ecpyalloc(fields[LF_FROM]);
1819  l.l_to = ecpyalloc(fields[LF_TO]);
1820  links = growalloc(links, sizeof *links, nlinks, &nlinks_alloc);
1821  links[nlinks++] = l;
1822 }
1823 
1824 static void
1825 rulesub(struct rule *rp, const char *loyearp, const char *hiyearp,
1826  const char *typep, const char *monthp, const char *dayp,
1827  const char *timep)
1828 {
1829  const struct lookup *lp;
1830  const char *cp;
1831  char *dp;
1832  char *ep;
1833  char xs;
1834 
1835  /* PG: year_tmp is to avoid sscanf portability issues */
1836  int year_tmp;
1837 
1838  if ((lp = byword(monthp, mon_names)) == NULL)
1839  {
1840  error(_("invalid month name"));
1841  return;
1842  }
1843  rp->r_month = lp->l_value;
1844  rp->r_todisstd = false;
1845  rp->r_todisut = false;
1846  dp = ecpyalloc(timep);
1847  if (*dp != '\0')
1848  {
1849  ep = dp + strlen(dp) - 1;
1850  switch (lowerit(*ep))
1851  {
1852  case 's': /* Standard */
1853  rp->r_todisstd = true;
1854  rp->r_todisut = false;
1855  *ep = '\0';
1856  break;
1857  case 'w': /* Wall */
1858  rp->r_todisstd = false;
1859  rp->r_todisut = false;
1860  *ep = '\0';
1861  break;
1862  case 'g': /* Greenwich */
1863  case 'u': /* Universal */
1864  case 'z': /* Zulu */
1865  rp->r_todisstd = true;
1866  rp->r_todisut = true;
1867  *ep = '\0';
1868  break;
1869  }
1870  }
1871  rp->r_tod = gethms(dp, _("invalid time of day"));
1872  free(dp);
1873 
1874  /*
1875  * Year work.
1876  */
1877  cp = loyearp;
1878  lp = byword(cp, begin_years);
1879  rp->r_lowasnum = lp == NULL;
1880  if (!rp->r_lowasnum)
1881  switch (lp->l_value)
1882  {
1883  case YR_MINIMUM:
1884  rp->r_loyear = ZIC_MIN;
1885  break;
1886  case YR_MAXIMUM:
1887  rp->r_loyear = ZIC_MAX;
1888  break;
1889  default: /* "cannot happen" */
1890  fprintf(stderr,
1891  _("%s: panic: Invalid l_value %d\n"),
1892  progname, lp->l_value);
1893  exit(EXIT_FAILURE);
1894  }
1895  else if (sscanf(cp, "%d%c", &year_tmp, &xs) == 1)
1896  rp->r_loyear = year_tmp;
1897  else
1898  {
1899  error(_("invalid starting year"));
1900  return;
1901  }
1902  cp = hiyearp;
1903  lp = byword(cp, end_years);
1904  rp->r_hiwasnum = lp == NULL;
1905  if (!rp->r_hiwasnum)
1906  switch (lp->l_value)
1907  {
1908  case YR_MINIMUM:
1909  rp->r_hiyear = ZIC_MIN;
1910  break;
1911  case YR_MAXIMUM:
1912  rp->r_hiyear = ZIC_MAX;
1913  break;
1914  case YR_ONLY:
1915  rp->r_hiyear = rp->r_loyear;
1916  break;
1917  default: /* "cannot happen" */
1918  fprintf(stderr,
1919  _("%s: panic: Invalid l_value %d\n"),
1920  progname, lp->l_value);
1921  exit(EXIT_FAILURE);
1922  }
1923  else if (sscanf(cp, "%d%c", &year_tmp, &xs) == 1)
1924  rp->r_hiyear = year_tmp;
1925  else
1926  {
1927  error(_("invalid ending year"));
1928  return;
1929  }
1930  if (rp->r_loyear > rp->r_hiyear)
1931  {
1932  error(_("starting year greater than ending year"));
1933  return;
1934  }
1935  if (*typep == '\0')
1936  rp->r_yrtype = NULL;
1937  else
1938  {
1939  if (rp->r_loyear == rp->r_hiyear)
1940  {
1941  error(_("typed single year"));
1942  return;
1943  }
1944  warning(_("year type \"%s\" is obsolete; use \"-\" instead"),
1945  typep);
1946  rp->r_yrtype = ecpyalloc(typep);
1947  }
1948 
1949  /*
1950  * Day work. Accept things such as: 1 lastSunday last-Sunday
1951  * (undocumented; warn about this) Sun<=20 Sun>=7
1952  */
1953  dp = ecpyalloc(dayp);
1954  if ((lp = byword(dp, lasts)) != NULL)
1955  {
1956  rp->r_dycode = DC_DOWLEQ;
1957  rp->r_wday = lp->l_value;
1958  rp->r_dayofmonth = len_months[1][rp->r_month];
1959  }
1960  else
1961  {
1962  if ((ep = strchr(dp, '<')) != NULL)
1963  rp->r_dycode = DC_DOWLEQ;
1964  else if ((ep = strchr(dp, '>')) != NULL)
1965  rp->r_dycode = DC_DOWGEQ;
1966  else
1967  {
1968  ep = dp;
1969  rp->r_dycode = DC_DOM;
1970  }
1971  if (rp->r_dycode != DC_DOM)
1972  {
1973  *ep++ = 0;
1974  if (*ep++ != '=')
1975  {
1976  error(_("invalid day of month"));
1977  free(dp);
1978  return;
1979  }
1980  if ((lp = byword(dp, wday_names)) == NULL)
1981  {
1982  error(_("invalid weekday name"));
1983  free(dp);
1984  return;
1985  }
1986  rp->r_wday = lp->l_value;
1987  }
1988  if (sscanf(ep, "%d%c", &rp->r_dayofmonth, &xs) != 1 ||
1989  rp->r_dayofmonth <= 0 ||
1990  (rp->r_dayofmonth > len_months[1][rp->r_month]))
1991  {
1992  error(_("invalid day of month"));
1993  free(dp);
1994  return;
1995  }
1996  }
1997  free(dp);
1998 }
1999 
2000 static void
2001 convert(const int32 val, char *const buf)
2002 {
2003  int i;
2004  int shift;
2005  unsigned char *const b = (unsigned char *) buf;
2006 
2007  for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
2008  b[i] = val >> shift;
2009 }
2010 
2011 static void
2012 convert64(const zic_t val, char *const buf)
2013 {
2014  int i;
2015  int shift;
2016  unsigned char *const b = (unsigned char *) buf;
2017 
2018  for (i = 0, shift = 56; i < 8; ++i, shift -= 8)
2019  b[i] = val >> shift;
2020 }
2021 
2022 static void
2023 puttzcode(const int32 val, FILE *const fp)
2024 {
2025  char buf[4];
2026 
2027  convert(val, buf);
2028  fwrite(buf, sizeof buf, 1, fp);
2029 }
2030 
2031 static void
2032 puttzcodepass(zic_t val, FILE *fp, int pass)
2033 {
2034  if (pass == 1)
2035  puttzcode(val, fp);
2036  else
2037  {
2038  char buf[8];
2039 
2040  convert64(val, buf);
2041  fwrite(buf, sizeof buf, 1, fp);
2042  }
2043 }
2044 
2045 static int
2046 atcomp(const void *avp, const void *bvp)
2047 {
2048  const zic_t a = ((const struct attype *) avp)->at;
2049  const zic_t b = ((const struct attype *) bvp)->at;
2050 
2051  return (a < b) ? -1 : (a > b);
2052 }
2053 
2055 {
2057  ptrdiff_t base,
2058  count;
2059  int leapbase,
2060  leapcount;
2061 };
2062 
2063 static struct timerange
2064 limitrange(struct timerange r, zic_t lo, zic_t hi,
2065  zic_t const *ats, unsigned char const *types)
2066 {
2067  while (0 < r.count && ats[r.base] < lo)
2068  {
2069  r.defaulttype = types[r.base];
2070  r.count--;
2071  r.base++;
2072  }
2073  while (0 < r.leapcount && trans[r.leapbase] < lo)
2074  {
2075  r.leapcount--;
2076  r.leapbase++;
2077  }
2078 
2079  if (hi < ZIC_MAX)
2080  {
2081  while (0 < r.count && hi + 1 < ats[r.base + r.count - 1])
2082  r.count--;
2083  while (0 < r.leapcount && hi + 1 < trans[r.leapbase + r.leapcount - 1])
2084  r.leapcount--;
2085  }
2086 
2087  return r;
2088 }
2089 
2090 static void
2091 writezone(const char *const name, const char *const string, char version,
2092  int defaulttype)
2093 {
2094  FILE *fp;
2095  ptrdiff_t i,
2096  j;
2097  int pass;
2098  static const struct tzhead tzh0;
2099  static struct tzhead tzh;
2100  bool dir_checked = false;
2101  zic_t one = 1;
2102  zic_t y2038_boundary = one << 31;
2103  ptrdiff_t nats = timecnt + WORK_AROUND_QTBUG_53071;
2104 
2105  /*
2106  * Allocate the ATS and TYPES arrays via a single malloc, as this is a bit
2107  * faster.
2108  */
2109  zic_t *ats = emalloc(MAXALIGN(size_product(nats, sizeof *ats + 1)));
2110  void *typesptr = ats + nats;
2111  unsigned char *types = typesptr;
2112  struct timerange rangeall,
2113  range32,
2114  range64;
2115 
2116  /*
2117  * Sort.
2118  */
2119  if (timecnt > 1)
2120  qsort(attypes, timecnt, sizeof *attypes, atcomp);
2121 
2122  /*
2123  * Optimize.
2124  */
2125  {
2126  ptrdiff_t fromi,
2127  toi;
2128 
2129  toi = 0;
2130  fromi = 0;
2131  for (; fromi < timecnt; ++fromi)
2132  {
2133  if (toi != 0
2134  && ((attypes[fromi].at
2135  + utoffs[attypes[toi - 1].type])
2136  <= (attypes[toi - 1].at
2137  + utoffs[toi == 1 ? 0
2138  : attypes[toi - 2].type])))
2139  {
2140  attypes[toi - 1].type =
2141  attypes[fromi].type;
2142  continue;
2143  }
2144  if (toi == 0
2145  || attypes[fromi].dontmerge
2146  || (utoffs[attypes[toi - 1].type]
2147  != utoffs[attypes[fromi].type])
2148  || (isdsts[attypes[toi - 1].type]
2149  != isdsts[attypes[fromi].type])
2150  || (desigidx[attypes[toi - 1].type]
2151  != desigidx[attypes[fromi].type]))
2152  attypes[toi++] = attypes[fromi];
2153  }
2154  timecnt = toi;
2155  }
2156 
2157  if (noise && timecnt > 1200)
2158  {
2159  if (timecnt > TZ_MAX_TIMES)
2160  warning(_("reference clients mishandle"
2161  " more than %d transition times"),
2162  TZ_MAX_TIMES);
2163  else
2164  warning(_("pre-2014 clients may mishandle"
2165  " more than 1200 transition times"));
2166  }
2167 
2168  /*
2169  * Transfer.
2170  */
2171  for (i = 0; i < timecnt; ++i)
2172  {
2173  ats[i] = attypes[i].at;
2174  types[i] = attypes[i].type;
2175  }
2176 
2177  /*
2178  * Correct for leap seconds.
2179  */
2180  for (i = 0; i < timecnt; ++i)
2181  {
2182  j = leapcnt;
2183  while (--j >= 0)
2184  if (ats[i] > trans[j] - corr[j])
2185  {
2186  ats[i] = tadd(ats[i], corr[j]);
2187  break;
2188  }
2189  }
2190 
2191  /*
2192  * Work around QTBUG-53071 for timestamps less than y2038_boundary - 1, by
2193  * inserting a no-op transition at time y2038_boundary - 1. This works
2194  * only for timestamps before the boundary, which should be good enough in
2195  * practice as QTBUG-53071 should be long-dead by 2038. Do this after
2196  * correcting for leap seconds, as the idea is to insert a transition just
2197  * before 32-bit pg_time_t rolls around, and this occurs at a slightly
2198  * different moment if transitions are leap-second corrected.
2199  */
2200  if (WORK_AROUND_QTBUG_53071 && timecnt != 0 && want_bloat()
2201  && ats[timecnt - 1] < y2038_boundary - 1 && strchr(string, '<'))
2202  {
2203  ats[timecnt] = y2038_boundary - 1;
2204  types[timecnt] = types[timecnt - 1];
2205  timecnt++;
2206  }
2207 
2208  rangeall.defaulttype = defaulttype;
2209  rangeall.base = rangeall.leapbase = 0;
2210  rangeall.count = timecnt;
2211  rangeall.leapcount = leapcnt;
2212  range64 = limitrange(rangeall, lo_time, hi_time, ats, types);
2213  range32 = limitrange(range64, PG_INT32_MIN, PG_INT32_MAX, ats, types);
2214 
2215  /*
2216  * Remove old file, if any, to snap links.
2217  */
2218  if (remove(name) == 0)
2219  dir_checked = true;
2220  else if (errno != ENOENT)
2221  {
2222  const char *e = strerror(errno);
2223 
2224  fprintf(stderr, _("%s: Cannot remove %s/%s: %s\n"),
2225  progname, directory, name, e);
2226  exit(EXIT_FAILURE);
2227  }
2228  fp = fopen(name, "wb");
2229  if (!fp)
2230  {
2231  int fopen_errno = errno;
2232 
2233  if (fopen_errno == ENOENT && !dir_checked)
2234  {
2235  mkdirs(name, true);
2236  fp = fopen(name, "wb");
2237  fopen_errno = errno;
2238  }
2239  if (!fp)
2240  {
2241  fprintf(stderr, _("%s: Cannot create %s/%s: %s\n"),
2242  progname, directory, name, strerror(fopen_errno));
2243  exit(EXIT_FAILURE);
2244  }
2245  }
2246  for (pass = 1; pass <= 2; ++pass)
2247  {
2248  ptrdiff_t thistimei,
2249  thistimecnt,
2250  thistimelim;
2251  int thisleapi,
2252  thisleapcnt,
2253  thisleaplim;
2254  int currenttype,
2255  thisdefaulttype;
2256  bool locut,
2257  hicut;
2258  zic_t lo;
2259  int old0;
2260  char omittype[TZ_MAX_TYPES];
2261  int typemap[TZ_MAX_TYPES];
2262  int thistypecnt,
2263  stdcnt,
2264  utcnt;
2265  char thischars[TZ_MAX_CHARS];
2266  int thischarcnt;
2267  bool toomanytimes;
2268  int indmap[TZ_MAX_CHARS];
2269 
2270  if (pass == 1)
2271  {
2272  /*
2273  * Arguably the default time type in the 32-bit data should be
2274  * range32.defaulttype, which is suited for timestamps just before
2275  * PG_INT32_MIN. However, zic traditionally used the time type of
2276  * the indefinite past instead. Internet RFC 8532 says readers
2277  * should ignore 32-bit data, so this discrepancy matters only to
2278  * obsolete readers where the traditional type might be more
2279  * appropriate even if it's "wrong". So, use the historical zic
2280  * value, unless -r specifies a low cutoff that excludes some
2281  * 32-bit timestamps.
2282  */
2283  thisdefaulttype = (lo_time <= PG_INT32_MIN
2284  ? range64.defaulttype
2285  : range32.defaulttype);
2286 
2287  thistimei = range32.base;
2288  thistimecnt = range32.count;
2289  toomanytimes = thistimecnt >> 31 >> 1 != 0;
2290  thisleapi = range32.leapbase;
2291  thisleapcnt = range32.leapcount;
2292  locut = PG_INT32_MIN < lo_time;
2293  hicut = hi_time < PG_INT32_MAX;
2294  }
2295  else
2296  {
2297  thisdefaulttype = range64.defaulttype;
2298  thistimei = range64.base;
2299  thistimecnt = range64.count;
2300  toomanytimes = thistimecnt >> 31 >> 31 >> 2 != 0;
2301  thisleapi = range64.leapbase;
2302  thisleapcnt = range64.leapcount;
2303  locut = min_time < lo_time;
2304  hicut = hi_time < max_time;
2305  }
2306  if (toomanytimes)
2307  error(_("too many transition times"));
2308 
2309  /*
2310  * Keep the last too-low transition if no transition is exactly at LO.
2311  * The kept transition will be output as a LO "transition"; see
2312  * "Output a LO_TIME transition" below. This is needed when the
2313  * output is truncated at the start, and is also useful when catering
2314  * to buggy 32-bit clients that do not use time type 0 for timestamps
2315  * before the first transition.
2316  */
2317  if (0 < thistimei && ats[thistimei] != lo_time)
2318  {
2319  thistimei--;
2320  thistimecnt++;
2321  locut = false;
2322  }
2323 
2324  thistimelim = thistimei + thistimecnt;
2325  thisleaplim = thisleapi + thisleapcnt;
2326  if (thistimecnt != 0)
2327  {
2328  if (ats[thistimei] == lo_time)
2329  locut = false;
2330  if (hi_time < ZIC_MAX && ats[thistimelim - 1] == hi_time + 1)
2331  hicut = false;
2332  }
2333  memset(omittype, true, typecnt);
2334  omittype[thisdefaulttype] = false;
2335  for (i = thistimei; i < thistimelim; i++)
2336  omittype[types[i]] = false;
2337 
2338  /*
2339  * Reorder types to make THISDEFAULTTYPE type 0. Use TYPEMAP to swap
2340  * OLD0 and THISDEFAULTTYPE so that THISDEFAULTTYPE appears as type 0
2341  * in the output instead of OLD0. TYPEMAP also omits unused types.
2342  */
2343  old0 = strlen(omittype);
2344 
2345 #ifndef LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH
2346 
2347  /*
2348  * For some pre-2011 systems: if the last-to-be-written standard (or
2349  * daylight) type has an offset different from the most recently used
2350  * offset, append an (unused) copy of the most recently used type (to
2351  * help get global "altzone" and "timezone" variables set correctly).
2352  */
2353  if (want_bloat())
2354  {
2355  int mrudst,
2356  mrustd,
2357  hidst,
2358  histd,
2359  type;
2360 
2361  hidst = histd = mrudst = mrustd = -1;
2362  for (i = thistimei; i < thistimelim; ++i)
2363  if (isdsts[types[i]])
2364  mrudst = types[i];
2365  else
2366  mrustd = types[i];
2367  for (i = old0; i < typecnt; i++)
2368  {
2369  int h = (i == old0 ? thisdefaulttype
2370  : i == thisdefaulttype ? old0 : i);
2371 
2372  if (!omittype[h])
2373  {
2374  if (isdsts[h])
2375  hidst = i;
2376  else
2377  histd = i;
2378  }
2379  }
2380  if (hidst >= 0 && mrudst >= 0 && hidst != mrudst &&
2381  utoffs[hidst] != utoffs[mrudst])
2382  {
2383  isdsts[mrudst] = -1;
2384  type = addtype(utoffs[mrudst],
2385  &chars[desigidx[mrudst]],
2386  true,
2387  ttisstds[mrudst],
2388  ttisuts[mrudst]);
2389  isdsts[mrudst] = 1;
2390  omittype[type] = false;
2391  }
2392  if (histd >= 0 && mrustd >= 0 && histd != mrustd &&
2393  utoffs[histd] != utoffs[mrustd])
2394  {
2395  isdsts[mrustd] = -1;
2396  type = addtype(utoffs[mrustd],
2397  &chars[desigidx[mrustd]],
2398  false,
2399  ttisstds[mrustd],
2400  ttisuts[mrustd]);
2401  isdsts[mrustd] = 0;
2402  omittype[type] = false;
2403  }
2404  }
2405 #endif /* !defined
2406  * LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH */
2407  thistypecnt = 0;
2408  for (i = old0; i < typecnt; i++)
2409  if (!omittype[i])
2410  typemap[i == old0 ? thisdefaulttype
2411  : i == thisdefaulttype ? old0 : i]
2412  = thistypecnt++;
2413 
2414  for (i = 0; i < sizeof indmap / sizeof indmap[0]; ++i)
2415  indmap[i] = -1;
2416  thischarcnt = stdcnt = utcnt = 0;
2417  for (i = old0; i < typecnt; i++)
2418  {
2419  char *thisabbr;
2420 
2421  if (omittype[i])
2422  continue;
2423  if (ttisstds[i])
2424  stdcnt = thistypecnt;
2425  if (ttisuts[i])
2426  utcnt = thistypecnt;
2427  if (indmap[desigidx[i]] >= 0)
2428  continue;
2429  thisabbr = &chars[desigidx[i]];
2430  for (j = 0; j < thischarcnt; ++j)
2431  if (strcmp(&thischars[j], thisabbr) == 0)
2432  break;
2433  if (j == thischarcnt)
2434  {
2435  strcpy(&thischars[thischarcnt], thisabbr);
2436  thischarcnt += strlen(thisabbr) + 1;
2437  }
2438  indmap[desigidx[i]] = j;
2439  }
2440  if (pass == 1 && !want_bloat())
2441  {
2442  utcnt = stdcnt = thisleapcnt = 0;
2443  thistimecnt = -(locut + hicut);
2444  thistypecnt = thischarcnt = 1;
2445  thistimelim = thistimei;
2446  }
2447 #define DO(field) fwrite(tzh.field, sizeof tzh.field, 1, fp)
2448  tzh = tzh0;
2449  memcpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
2450  tzh.tzh_version[0] = version;
2451  convert(utcnt, tzh.tzh_ttisutcnt);
2452  convert(stdcnt, tzh.tzh_ttisstdcnt);
2453  convert(thisleapcnt, tzh.tzh_leapcnt);
2454  convert(locut + thistimecnt + hicut, tzh.tzh_timecnt);
2455  convert(thistypecnt, tzh.tzh_typecnt);
2456  convert(thischarcnt, tzh.tzh_charcnt);
2457  DO(tzh_magic);
2458  DO(tzh_version);
2459  DO(tzh_reserved);
2460  DO(tzh_ttisutcnt);
2461  DO(tzh_ttisstdcnt);
2462  DO(tzh_leapcnt);
2463  DO(tzh_timecnt);
2464  DO(tzh_typecnt);
2465  DO(tzh_charcnt);
2466 #undef DO
2467  if (pass == 1 && !want_bloat())
2468  {
2469  /* Output a minimal data block with just one time type. */
2470  puttzcode(0, fp); /* utoff */
2471  putc(0, fp); /* dst */
2472  putc(0, fp); /* index of abbreviation */
2473  putc(0, fp); /* empty-string abbreviation */
2474  continue;
2475  }
2476 
2477  /* PG: print current timezone abbreviations if requested */
2478  if (print_abbrevs && pass == 2)
2479  {
2480  /* Print "type" data for periods ending after print_cutoff */
2481  for (i = thistimei; i < thistimelim; ++i)
2482  {
2483  if (i == thistimelim - 1 || ats[i + 1] > print_cutoff)
2484  {
2485  unsigned char tm = types[i];
2486  char *thisabbrev = &thischars[indmap[desigidx[tm]]];
2487 
2488  fprintf(stdout, "%s\t" INT64_FORMAT "%s\n",
2489  thisabbrev,
2490  utoffs[tm],
2491  isdsts[tm] ? "\tD" : "");
2492  }
2493  }
2494  /* Print the default type if we have no transitions at all */
2495  if (thistimei >= thistimelim)
2496  {
2497  unsigned char tm = defaulttype;
2498  char *thisabbrev = &thischars[indmap[desigidx[tm]]];
2499 
2500  fprintf(stdout, "%s\t" INT64_FORMAT "%s\n",
2501  thisabbrev,
2502  utoffs[tm],
2503  isdsts[tm] ? "\tD" : "");
2504  }
2505  }
2506 
2507  /*
2508  * Output a LO_TIME transition if needed; see limitrange. But do not
2509  * go below the minimum representable value for this pass.
2510  */
2511  lo = pass == 1 && lo_time < PG_INT32_MIN ? PG_INT32_MIN : lo_time;
2512 
2513  if (locut)
2514  puttzcodepass(lo, fp, pass);
2515  for (i = thistimei; i < thistimelim; ++i)
2516  {
2517  zic_t at = ats[i] < lo ? lo : ats[i];
2518 
2519  puttzcodepass(at, fp, pass);
2520  }
2521  if (hicut)
2522  puttzcodepass(hi_time + 1, fp, pass);
2523  currenttype = 0;
2524  if (locut)
2525  putc(currenttype, fp);
2526  for (i = thistimei; i < thistimelim; ++i)
2527  {
2528  currenttype = typemap[types[i]];
2529  putc(currenttype, fp);
2530  }
2531  if (hicut)
2532  putc(currenttype, fp);
2533 
2534  for (i = old0; i < typecnt; i++)
2535  {
2536  int h = (i == old0 ? thisdefaulttype
2537  : i == thisdefaulttype ? old0 : i);
2538 
2539  if (!omittype[h])
2540  {
2541  puttzcode(utoffs[h], fp);
2542  putc(isdsts[h], fp);
2543  putc(indmap[desigidx[h]], fp);
2544  }
2545  }
2546  if (thischarcnt != 0)
2547  fwrite(thischars, sizeof thischars[0],
2548  thischarcnt, fp);
2549  for (i = thisleapi; i < thisleaplim; ++i)
2550  {
2551  zic_t todo;
2552 
2553  if (roll[i])
2554  {
2555  if (timecnt == 0 || trans[i] < ats[0])
2556  {
2557  j = 0;
2558  while (isdsts[j])
2559  if (++j >= typecnt)
2560  {
2561  j = 0;
2562  break;
2563  }
2564  }
2565  else
2566  {
2567  j = 1;
2568  while (j < timecnt &&
2569  trans[i] >= ats[j])
2570  ++j;
2571  j = types[j - 1];
2572  }
2573  todo = tadd(trans[i], -utoffs[j]);
2574  }
2575  else
2576  todo = trans[i];
2577  puttzcodepass(todo, fp, pass);
2578  puttzcode(corr[i], fp);
2579  }
2580  if (stdcnt != 0)
2581  for (i = old0; i < typecnt; i++)
2582  if (!omittype[i])
2583  putc(ttisstds[i], fp);
2584  if (utcnt != 0)
2585  for (i = old0; i < typecnt; i++)
2586  if (!omittype[i])
2587  putc(ttisuts[i], fp);
2588  }
2589  fprintf(fp, "\n%s\n", string);
2590  close_file(fp, directory, name);
2591  free(ats);
2592 }
2593 
2594 static char const *
2595 abbroffset(char *buf, zic_t offset)
2596 {
2597  char sign = '+';
2598  int seconds,
2599  minutes;
2600 
2601  if (offset < 0)
2602  {
2603  offset = -offset;
2604  sign = '-';
2605  }
2606 
2607  seconds = offset % SECSPERMIN;
2608  offset /= SECSPERMIN;
2609  minutes = offset % MINSPERHOUR;
2610  offset /= MINSPERHOUR;
2611  if (100 <= offset)
2612  {
2613  error(_("%%z UT offset magnitude exceeds 99:59:59"));
2614  return "%z";
2615  }
2616  else
2617  {
2618  char *p = buf;
2619 
2620  *p++ = sign;
2621  *p++ = '0' + offset / 10;
2622  *p++ = '0' + offset % 10;
2623  if (minutes | seconds)
2624  {
2625  *p++ = '0' + minutes / 10;
2626  *p++ = '0' + minutes % 10;
2627  if (seconds)
2628  {
2629  *p++ = '0' + seconds / 10;
2630  *p++ = '0' + seconds % 10;
2631  }
2632  }
2633  *p = '\0';
2634  return buf;
2635  }
2636 }
2637 
2638 static size_t
2639 doabbr(char *abbr, struct zone const *zp, char const *letters,
2640  bool isdst, zic_t save, bool doquotes)
2641 {
2642  char *cp;
2643  char *slashp;
2644  size_t len;
2645  char const *format = zp->z_format;
2646 
2647  slashp = strchr(format, '/');
2648  if (slashp == NULL)
2649  {
2650  char letterbuf[PERCENT_Z_LEN_BOUND + 1];
2651 
2652  if (zp->z_format_specifier == 'z')
2653  letters = abbroffset(letterbuf, zp->z_stdoff + save);
2654  else if (!letters)
2655  letters = "%s";
2656  sprintf(abbr, format, letters);
2657  }
2658  else if (isdst)
2659  {
2660  strcpy(abbr, slashp + 1);
2661  }
2662  else
2663  {
2664  memcpy(abbr, format, slashp - format);
2665  abbr[slashp - format] = '\0';
2666  }
2667  len = strlen(abbr);
2668  if (!doquotes)
2669  return len;
2670  for (cp = abbr; is_alpha(*cp); cp++)
2671  continue;
2672  if (len > 0 && *cp == '\0')
2673  return len;
2674  abbr[len + 2] = '\0';
2675  abbr[len + 1] = '>';
2676  memmove(abbr + 1, abbr, len);
2677  abbr[0] = '<';
2678  return len + 2;
2679 }
2680 
2681 static void
2683 {
2684  if (min_year > x)
2685  min_year = x;
2686  if (max_year < x)
2687  max_year = x;
2688 }
2689 
2690 static int
2691 stringoffset(char *result, zic_t offset)
2692 {
2693  int hours;
2694  int minutes;
2695  int seconds;
2696  bool negative = offset < 0;
2697  int len = negative;
2698 
2699  if (negative)
2700  {
2701  offset = -offset;
2702  result[0] = '-';
2703  }
2704  seconds = offset % SECSPERMIN;
2705  offset /= SECSPERMIN;
2706  minutes = offset % MINSPERHOUR;
2707  offset /= MINSPERHOUR;
2708  hours = offset;
2709  if (hours >= HOURSPERDAY * DAYSPERWEEK)
2710  {
2711  result[0] = '\0';
2712  return 0;
2713  }
2714  len += sprintf(result + len, "%d", hours);
2715  if (minutes != 0 || seconds != 0)
2716  {
2717  len += sprintf(result + len, ":%02d", minutes);
2718  if (seconds != 0)
2719  len += sprintf(result + len, ":%02d", seconds);
2720  }
2721  return len;
2722 }
2723 
2724 static int
2725 stringrule(char *result, struct rule *const rp, zic_t save, zic_t stdoff)
2726 {
2727  zic_t tod = rp->r_tod;
2728  int compat = 0;
2729 
2730  if (rp->r_dycode == DC_DOM)
2731  {
2732  int month,
2733  total;
2734 
2735  if (rp->r_dayofmonth == 29 && rp->r_month == TM_FEBRUARY)
2736  return -1;
2737  total = 0;
2738  for (month = 0; month < rp->r_month; ++month)
2739  total += len_months[0][month];
2740  /* Omit the "J" in Jan and Feb, as that's shorter. */
2741  if (rp->r_month <= 1)
2742  result += sprintf(result, "%d", total + rp->r_dayofmonth - 1);
2743  else
2744  result += sprintf(result, "J%d", total + rp->r_dayofmonth);
2745  }
2746  else
2747  {
2748  int week;
2749  int wday = rp->r_wday;
2750  int wdayoff;
2751 
2752  if (rp->r_dycode == DC_DOWGEQ)
2753  {
2754  wdayoff = (rp->r_dayofmonth - 1) % DAYSPERWEEK;
2755  if (wdayoff)
2756  compat = 2013;
2757  wday -= wdayoff;
2758  tod += wdayoff * SECSPERDAY;
2759  week = 1 + (rp->r_dayofmonth - 1) / DAYSPERWEEK;
2760  }
2761  else if (rp->r_dycode == DC_DOWLEQ)
2762  {
2763  if (rp->r_dayofmonth == len_months[1][rp->r_month])
2764  week = 5;
2765  else
2766  {
2767  wdayoff = rp->r_dayofmonth % DAYSPERWEEK;
2768  if (wdayoff)
2769  compat = 2013;
2770  wday -= wdayoff;
2771  tod += wdayoff * SECSPERDAY;
2772  week = rp->r_dayofmonth / DAYSPERWEEK;
2773  }
2774  }
2775  else
2776  return -1; /* "cannot happen" */
2777  if (wday < 0)
2778  wday += DAYSPERWEEK;
2779  result += sprintf(result, "M%d.%d.%d",
2780  rp->r_month + 1, week, wday);
2781  }
2782  if (rp->r_todisut)
2783  tod += stdoff;
2784  if (rp->r_todisstd && !rp->r_isdst)
2785  tod += save;
2786  if (tod != 2 * SECSPERMIN * MINSPERHOUR)
2787  {
2788  *result++ = '/';
2789  if (!stringoffset(result, tod))
2790  return -1;
2791  if (tod < 0)
2792  {
2793  if (compat < 2013)
2794  compat = 2013;
2795  }
2796  else if (SECSPERDAY <= tod)
2797  {
2798  if (compat < 1994)
2799  compat = 1994;
2800  }
2801  }
2802  return compat;
2803 }
2804 
2805 static int
2806 rule_cmp(struct rule const *a, struct rule const *b)
2807 {
2808  if (!a)
2809  return -!!b;
2810  if (!b)
2811  return 1;
2812  if (a->r_hiyear != b->r_hiyear)
2813  return a->r_hiyear < b->r_hiyear ? -1 : 1;
2814  if (a->r_month - b->r_month != 0)
2815  return a->r_month - b->r_month;
2816  return a->r_dayofmonth - b->r_dayofmonth;
2817 }
2818 
2819 static int
2820 stringzone(char *result, struct zone const *zpfirst, ptrdiff_t zonecount)
2821 {
2822  const struct zone *zp;
2823  struct rule *rp;
2824  struct rule *stdrp;
2825  struct rule *dstrp;
2826  ptrdiff_t i;
2827  const char *abbrvar;
2828  int compat = 0;
2829  int c;
2830  size_t len;
2831  int offsetlen;
2832  struct rule stdr,
2833  dstr;
2834 
2835  result[0] = '\0';
2836 
2837  /*
2838  * Internet RFC 8536 section 5.1 says to use an empty TZ string if future
2839  * timestamps are truncated.
2840  */
2841  if (hi_time < max_time)
2842  return -1;
2843 
2844  zp = zpfirst + zonecount - 1;
2845  stdrp = dstrp = NULL;
2846  for (i = 0; i < zp->z_nrules; ++i)
2847  {
2848  rp = &zp->z_rules[i];
2849  if (rp->r_hiwasnum || rp->r_hiyear != ZIC_MAX)
2850  continue;
2851  if (rp->r_yrtype != NULL)
2852  continue;
2853  if (!rp->r_isdst)
2854  {
2855  if (stdrp == NULL)
2856  stdrp = rp;
2857  else
2858  return -1;
2859  }
2860  else
2861  {
2862  if (dstrp == NULL)
2863  dstrp = rp;
2864  else
2865  return -1;
2866  }
2867  }
2868  if (stdrp == NULL && dstrp == NULL)
2869  {
2870  /*
2871  * There are no rules running through "max". Find the latest std rule
2872  * in stdabbrrp and latest rule of any type in stdrp.
2873  */
2874  struct rule *stdabbrrp = NULL;
2875 
2876  for (i = 0; i < zp->z_nrules; ++i)
2877  {
2878  rp = &zp->z_rules[i];
2879  if (!rp->r_isdst && rule_cmp(stdabbrrp, rp) < 0)
2880  stdabbrrp = rp;
2881  if (rule_cmp(stdrp, rp) < 0)
2882  stdrp = rp;
2883  }
2884  if (stdrp != NULL && stdrp->r_isdst)
2885  {
2886  /* Perpetual DST. */
2887  dstr.r_month = TM_JANUARY;
2888  dstr.r_dycode = DC_DOM;
2889  dstr.r_dayofmonth = 1;
2890  dstr.r_tod = 0;
2891  dstr.r_todisstd = dstr.r_todisut = false;
2892  dstr.r_isdst = stdrp->r_isdst;
2893  dstr.r_save = stdrp->r_save;
2894  dstr.r_abbrvar = stdrp->r_abbrvar;
2895  stdr.r_month = TM_DECEMBER;
2896  stdr.r_dycode = DC_DOM;
2897  stdr.r_dayofmonth = 31;
2898  stdr.r_tod = SECSPERDAY + stdrp->r_save;
2899  stdr.r_todisstd = stdr.r_todisut = false;
2900  stdr.r_isdst = false;
2901  stdr.r_save = 0;
2902  stdr.r_abbrvar
2903  = (stdabbrrp ? stdabbrrp->r_abbrvar : "");
2904  dstrp = &dstr;
2905  stdrp = &stdr;
2906  }
2907  }
2908  if (stdrp == NULL && (zp->z_nrules != 0 || zp->z_isdst))
2909  return -1;
2910  abbrvar = (stdrp == NULL) ? "" : stdrp->r_abbrvar;
2911  len = doabbr(result, zp, abbrvar, false, 0, true);
2912  offsetlen = stringoffset(result + len, -zp->z_stdoff);
2913  if (!offsetlen)
2914  {
2915  result[0] = '\0';
2916  return -1;
2917  }
2918  len += offsetlen;
2919  if (dstrp == NULL)
2920  return compat;
2921  len += doabbr(result + len, zp, dstrp->r_abbrvar,
2922  dstrp->r_isdst, dstrp->r_save, true);
2923  if (dstrp->r_save != SECSPERMIN * MINSPERHOUR)
2924  {
2925  offsetlen = stringoffset(result + len,
2926  -(zp->z_stdoff + dstrp->r_save));
2927  if (!offsetlen)
2928  {
2929  result[0] = '\0';
2930  return -1;
2931  }
2932  len += offsetlen;
2933  }
2934  result[len++] = ',';
2935  c = stringrule(result + len, dstrp, dstrp->r_save, zp->z_stdoff);
2936  if (c < 0)
2937  {
2938  result[0] = '\0';
2939  return -1;
2940  }
2941  if (compat < c)
2942  compat = c;
2943  len += strlen(result + len);
2944  result[len++] = ',';
2945  c = stringrule(result + len, stdrp, dstrp->r_save, zp->z_stdoff);
2946  if (c < 0)
2947  {
2948  result[0] = '\0';
2949  return -1;
2950  }
2951  if (compat < c)
2952  compat = c;
2953  return compat;
2954 }
2955 
2956 static void
2957 outzone(const struct zone *zpfirst, ptrdiff_t zonecount)
2958 {
2959  const struct zone *zp;
2960  struct rule *rp;
2961  ptrdiff_t i,
2962  j;
2963  bool usestart,
2964  useuntil;
2965  zic_t starttime,
2966  untiltime;
2967  zic_t stdoff;
2968  zic_t save;
2969  zic_t year;
2970  zic_t startoff;
2971  bool startttisstd;
2972  bool startttisut;
2973  int type;
2974  char *startbuf;
2975  char *ab;
2976  char *envvar;
2977  int max_abbr_len;
2978  int max_envvar_len;
2979  bool prodstic; /* all rules are min to max */
2980  int compat;
2981  bool do_extend;
2982  char version;
2983  ptrdiff_t lastatmax = -1;
2984  zic_t one = 1;
2985  zic_t y2038_boundary = one << 31;
2986  zic_t max_year0;
2987  int defaulttype = -1;
2988 
2989  max_abbr_len = 2 + max_format_len + max_abbrvar_len;
2990  max_envvar_len = 2 * max_abbr_len + 5 * 9;
2991  startbuf = emalloc(max_abbr_len + 1);
2992  ab = emalloc(max_abbr_len + 1);
2993  envvar = emalloc(max_envvar_len + 1);
2994  INITIALIZE(untiltime);
2995  INITIALIZE(starttime);
2996 
2997  /*
2998  * Now. . .finally. . .generate some useful data!
2999  */
3000  timecnt = 0;
3001  typecnt = 0;
3002  charcnt = 0;
3003  prodstic = zonecount == 1;
3004 
3005  /*
3006  * Thanks to Earl Chew for noting the need to unconditionally initialize
3007  * startttisstd.
3008  */
3009  startttisstd = false;
3010  startttisut = false;
3011  min_year = max_year = EPOCH_YEAR;
3012  if (leapseen)
3013  {
3014  updateminmax(leapminyear);
3015  updateminmax(leapmaxyear + (leapmaxyear < ZIC_MAX));
3016  }
3017  for (i = 0; i < zonecount; ++i)
3018  {
3019  zp = &zpfirst[i];
3020  if (i < zonecount - 1)
3022  for (j = 0; j < zp->z_nrules; ++j)
3023  {
3024  rp = &zp->z_rules[j];
3025  if (rp->r_lowasnum)
3026  updateminmax(rp->r_loyear);
3027  if (rp->r_hiwasnum)
3028  updateminmax(rp->r_hiyear);
3029  if (rp->r_lowasnum || rp->r_hiwasnum)
3030  prodstic = false;
3031  }
3032  }
3033 
3034  /*
3035  * Generate lots of data if a rule can't cover all future times.
3036  */
3037  compat = stringzone(envvar, zpfirst, zonecount);
3038  version = compat < 2013 ? ZIC_VERSION_PRE_2013 : ZIC_VERSION;
3039  do_extend = compat < 0;
3040  if (noise)
3041  {
3042  if (!*envvar)
3043  warning("%s %s",
3044  _("no POSIX environment variable for zone"),
3045  zpfirst->z_name);
3046  else if (compat != 0)
3047  {
3048  /*
3049  * Circa-COMPAT clients, and earlier clients, might not work for
3050  * this zone when given dates before 1970 or after 2038.
3051  */
3052  warning(_("%s: pre-%d clients may mishandle"
3053  " distant timestamps"),
3054  zpfirst->z_name, compat);
3055  }
3056  }
3057  if (do_extend)
3058  {
3059  /*
3060  * Search through a couple of extra years past the obvious 400, to
3061  * avoid edge cases. For example, suppose a non-POSIX rule applies
3062  * from 2012 onwards and has transitions in March and September, plus
3063  * some one-off transitions in November 2013. If zic looked only at
3064  * the last 400 years, it would set max_year=2413, with the intent
3065  * that the 400 years 2014 through 2413 will be repeated. The last
3066  * transition listed in the tzfile would be in 2413-09, less than 400
3067  * years after the last one-off transition in 2013-11. Two years
3068  * might be overkill, but with the kind of edge cases available we're
3069  * not sure that one year would suffice.
3070  */
3071  enum
3072  {
3073  years_of_observations = YEARSPERREPEAT + 2};
3074 
3075  if (min_year >= ZIC_MIN + years_of_observations)
3076  min_year -= years_of_observations;
3077  else
3078  min_year = ZIC_MIN;
3079  if (max_year <= ZIC_MAX - years_of_observations)
3080  max_year += years_of_observations;
3081  else
3082  max_year = ZIC_MAX;
3083 
3084  /*
3085  * Regardless of any of the above, for a "proDSTic" zone which
3086  * specifies that its rules always have and always will be in effect,
3087  * we only need one cycle to define the zone.
3088  */
3089  if (prodstic)
3090  {
3091  min_year = 1900;
3092  max_year = min_year + years_of_observations;
3093  }
3094  }
3095  max_year0 = max_year;
3096  if (want_bloat())
3097  {
3098  /*
3099  * For the benefit of older systems, generate data from 1900 through
3100  * 2038.
3101  */
3102  if (min_year > 1900)
3103  min_year = 1900;
3104  if (max_year < 2038)
3105  max_year = 2038;
3106  }
3107 
3108  for (i = 0; i < zonecount; ++i)
3109  {
3110  struct rule *prevrp = NULL;
3111 
3112  /*
3113  * A guess that may well be corrected later.
3114  */
3115  save = 0;
3116  zp = &zpfirst[i];
3117  usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
3118  useuntil = i < (zonecount - 1);
3119  if (useuntil && zp->z_untiltime <= min_time)
3120  continue;
3121  stdoff = zp->z_stdoff;
3122  eat(zp->z_filename, zp->z_linenum);
3123  *startbuf = '\0';
3124  startoff = zp->z_stdoff;
3125  if (zp->z_nrules == 0)
3126  {
3127  save = zp->z_save;
3128  doabbr(startbuf, zp, NULL, zp->z_isdst, save, false);
3129  type = addtype(oadd(zp->z_stdoff, save),
3130  startbuf, zp->z_isdst, startttisstd,
3131  startttisut);
3132  if (usestart)
3133  {
3134  addtt(starttime, type);
3135  usestart = false;
3136  }
3137  else
3138  defaulttype = type;
3139  }
3140  else
3141  for (year = min_year; year <= max_year; ++year)
3142  {
3143  if (useuntil && year > zp->z_untilrule.r_hiyear)
3144  break;
3145 
3146  /*
3147  * Mark which rules to do in the current year. For those to
3148  * do, calculate rpytime(rp, year);
3149  */
3150  for (j = 0; j < zp->z_nrules; ++j)
3151  {
3152  rp = &zp->z_rules[j];
3153  eats(zp->z_filename, zp->z_linenum,
3154  rp->r_filename, rp->r_linenum);
3155  rp->r_todo = year >= rp->r_loyear &&
3156  year <= rp->r_hiyear &&
3157  yearistype(year, rp->r_yrtype);
3158  if (rp->r_todo)
3159  {
3160  rp->r_temp = rpytime(rp, year);
3161  rp->r_todo
3162  = (rp->r_temp < y2038_boundary
3163  || year <= max_year0);
3164  }
3165  }
3166  for (;;)
3167  {
3168  ptrdiff_t k;
3169  zic_t jtime,
3170  ktime;
3171  zic_t offset;
3172 
3173  INITIALIZE(ktime);
3174  if (useuntil)
3175  {
3176  /*
3177  * Turn untiltime into UT assuming the current stdoff
3178  * and save values.
3179  */
3180  untiltime = zp->z_untiltime;
3181  if (!zp->z_untilrule.r_todisut)
3182  untiltime = tadd(untiltime,
3183  -stdoff);
3184  if (!zp->z_untilrule.r_todisstd)
3185  untiltime = tadd(untiltime,
3186  -save);
3187  }
3188 
3189  /*
3190  * Find the rule (of those to do, if any) that takes
3191  * effect earliest in the year.
3192  */
3193  k = -1;
3194  for (j = 0; j < zp->z_nrules; ++j)
3195  {
3196  rp = &zp->z_rules[j];
3197  if (!rp->r_todo)
3198  continue;
3199  eats(zp->z_filename, zp->z_linenum,
3200  rp->r_filename, rp->r_linenum);
3201  offset = rp->r_todisut ? 0 : stdoff;
3202  if (!rp->r_todisstd)
3203  offset = oadd(offset, save);
3204  jtime = rp->r_temp;
3205  if (jtime == min_time ||
3206  jtime == max_time)
3207  continue;
3208  jtime = tadd(jtime, -offset);
3209  if (k < 0 || jtime < ktime)
3210  {
3211  k = j;
3212  ktime = jtime;
3213  }
3214  else if (jtime == ktime)
3215  {
3216  char const *dup_rules_msg =
3217  _("two rules for same instant");
3218 
3219  eats(zp->z_filename, zp->z_linenum,
3220  rp->r_filename, rp->r_linenum);
3221  warning("%s", dup_rules_msg);
3222  rp = &zp->z_rules[k];
3223  eats(zp->z_filename, zp->z_linenum,
3224  rp->r_filename, rp->r_linenum);
3225  error("%s", dup_rules_msg);
3226  }
3227  }
3228  if (k < 0)
3229  break; /* go on to next year */
3230  rp = &zp->z_rules[k];
3231  rp->r_todo = false;
3232  if (useuntil && ktime >= untiltime)
3233  break;
3234  save = rp->r_save;
3235  if (usestart && ktime == starttime)
3236  usestart = false;
3237  if (usestart)
3238  {
3239  if (ktime < starttime)
3240  {
3241  startoff = oadd(zp->z_stdoff,
3242  save);
3243  doabbr(startbuf, zp,
3244  rp->r_abbrvar,
3245  rp->r_isdst,
3246  rp->r_save,
3247  false);
3248  continue;
3249  }
3250  if (*startbuf == '\0'
3251  && startoff == oadd(zp->z_stdoff,
3252  save))
3253  {
3254  doabbr(startbuf,
3255  zp,
3256  rp->r_abbrvar,
3257  rp->r_isdst,
3258  rp->r_save,
3259  false);
3260  }
3261  }
3262  eats(zp->z_filename, zp->z_linenum,
3263  rp->r_filename, rp->r_linenum);
3264  doabbr(ab, zp, rp->r_abbrvar,
3265  rp->r_isdst, rp->r_save, false);
3266  offset = oadd(zp->z_stdoff, rp->r_save);
3267  if (!want_bloat() && !useuntil && !do_extend
3268  && prevrp
3269  && rp->r_hiyear == ZIC_MAX
3270  && prevrp->r_hiyear == ZIC_MAX)
3271  break;
3272  type = addtype(offset, ab, rp->r_isdst,
3273  rp->r_todisstd, rp->r_todisut);
3274  if (defaulttype < 0 && !rp->r_isdst)
3275  defaulttype = type;
3276  if (rp->r_hiyear == ZIC_MAX
3277  && !(0 <= lastatmax
3278  && ktime < attypes[lastatmax].at))
3279  lastatmax = timecnt;
3280  addtt(ktime, type);
3281  prevrp = rp;
3282  }
3283  }
3284  if (usestart)
3285  {
3286  if (*startbuf == '\0' &&
3287  zp->z_format != NULL &&
3288  strchr(zp->z_format, '%') == NULL &&
3289  strchr(zp->z_format, '/') == NULL)
3290  strcpy(startbuf, zp->z_format);
3291  eat(zp->z_filename, zp->z_linenum);
3292  if (*startbuf == '\0')
3293  error(_("cannot determine time zone abbreviation to use just after until time"));
3294  else
3295  {
3296  bool isdst = startoff != zp->z_stdoff;
3297 
3298  type = addtype(startoff, startbuf, isdst,
3299  startttisstd, startttisut);
3300  if (defaulttype < 0 && !isdst)
3301  defaulttype = type;
3302  addtt(starttime, type);
3303  }
3304  }
3305 
3306  /*
3307  * Now we may get to set starttime for the next zone line.
3308  */
3309  if (useuntil)
3310  {
3311  startttisstd = zp->z_untilrule.r_todisstd;
3312  startttisut = zp->z_untilrule.r_todisut;
3313  starttime = zp->z_untiltime;
3314  if (!startttisstd)
3315  starttime = tadd(starttime, -save);
3316  if (!startttisut)
3317  starttime = tadd(starttime, -stdoff);
3318  }
3319  }
3320  if (defaulttype < 0)
3321  defaulttype = 0;
3322  if (0 <= lastatmax)
3323  attypes[lastatmax].dontmerge = true;
3324  if (do_extend)
3325  {
3326  /*
3327  * If we're extending the explicitly listed observations for 400 years
3328  * because we can't fill the POSIX-TZ field, check whether we actually
3329  * ended up explicitly listing observations through that period. If
3330  * there aren't any near the end of the 400-year period, add a
3331  * redundant one at the end of the final year, to make it clear that
3332  * we are claiming to have definite knowledge of the lack of
3333  * transitions up to that point.
3334  */
3335  struct rule xr;
3336  struct attype *lastat;
3337 
3338  xr.r_month = TM_JANUARY;
3339  xr.r_dycode = DC_DOM;
3340  xr.r_dayofmonth = 1;
3341  xr.r_tod = 0;
3342  for (lastat = attypes, i = 1; i < timecnt; i++)
3343  if (attypes[i].at > lastat->at)
3344  lastat = &attypes[i];
3345  if (!lastat || lastat->at < rpytime(&xr, max_year - 1))
3346  {
3347  addtt(rpytime(&xr, max_year + 1),
3348  lastat ? lastat->type : defaulttype);
3349  attypes[timecnt - 1].dontmerge = true;
3350  }
3351  }
3352  writezone(zpfirst->z_name, envvar, version, defaulttype);
3353  free(startbuf);
3354  free(ab);
3355  free(envvar);
3356 }
3357 
3358 static void
3359 addtt(zic_t starttime, int type)
3360 {
3361  attypes = growalloc(attypes, sizeof *attypes, timecnt, &timecnt_alloc);
3362  attypes[timecnt].at = starttime;
3363  attypes[timecnt].dontmerge = false;
3364  attypes[timecnt].type = type;
3365  ++timecnt;
3366 }
3367 
3368 static int
3369 addtype(zic_t utoff, char const *abbr, bool isdst, bool ttisstd, bool ttisut)
3370 {
3371  int i,
3372  j;
3373 
3374  if (!(-1L - 2147483647L <= utoff && utoff <= 2147483647L))
3375  {
3376  error(_("UT offset out of range"));
3377  exit(EXIT_FAILURE);
3378  }
3379  if (!want_bloat())
3380  ttisstd = ttisut = false;
3381 
3382  for (j = 0; j < charcnt; ++j)
3383  if (strcmp(&chars[j], abbr) == 0)
3384  break;
3385  if (j == charcnt)
3386  newabbr(abbr);
3387  else
3388  {
3389  /* If there's already an entry, return its index. */
3390  for (i = 0; i < typecnt; i++)
3391  if (utoff == utoffs[i] && isdst == isdsts[i] && j == desigidx[i]
3392  && ttisstd == ttisstds[i] && ttisut == ttisuts[i])
3393  return i;
3394  }
3395 
3396  /*
3397  * There isn't one; add a new one, unless there are already too many.
3398  */
3399  if (typecnt >= TZ_MAX_TYPES)
3400  {
3401  error(_("too many local time types"));
3402  exit(EXIT_FAILURE);
3403  }
3404  i = typecnt++;
3405  utoffs[i] = utoff;
3406  isdsts[i] = isdst;
3407  ttisstds[i] = ttisstd;
3408  ttisuts[i] = ttisut;
3409  desigidx[i] = j;
3410  return i;
3411 }
3412 
3413 static void
3414 leapadd(zic_t t, int correction, int rolling)
3415 {
3416  int i;
3417 
3418  if (TZ_MAX_LEAPS <= leapcnt)
3419  {
3420  error(_("too many leap seconds"));
3421  exit(EXIT_FAILURE);
3422  }
3423  for (i = 0; i < leapcnt; ++i)
3424  if (t <= trans[i])
3425  break;
3426  memmove(&trans[i + 1], &trans[i], (leapcnt - i) * sizeof *trans);
3427  memmove(&corr[i + 1], &corr[i], (leapcnt - i) * sizeof *corr);
3428  memmove(&roll[i + 1], &roll[i], (leapcnt - i) * sizeof *roll);
3429  trans[i] = t;
3430  corr[i] = correction;
3431  roll[i] = rolling;
3432  ++leapcnt;
3433 }
3434 
3435 static void
3436 adjleap(void)
3437 {
3438  int i;
3439  zic_t last = 0;
3440  zic_t prevtrans = 0;
3441 
3442  /*
3443  * propagate leap seconds forward
3444  */
3445  for (i = 0; i < leapcnt; ++i)
3446  {
3447  if (trans[i] - prevtrans < 28 * SECSPERDAY)
3448  {
3449  error(_("Leap seconds too close together"));
3450  exit(EXIT_FAILURE);
3451  }
3452  prevtrans = trans[i];
3453  trans[i] = tadd(trans[i], last);
3454  last = corr[i] += last;
3455  }
3456 
3457  if (leapexpires < 0)
3458  {
3459  leapexpires = comment_leapexpires;
3460  if (0 <= leapexpires)
3461  warning(_("\"#expires\" is obsolescent; use \"Expires\""));
3462  }
3463 
3464  if (0 <= leapexpires)
3465  {
3466  leapexpires = oadd(leapexpires, last);
3467  if (!(leapcnt == 0 || (trans[leapcnt - 1] < leapexpires)))
3468  {
3469  error(_("last Leap time does not precede Expires time"));
3470  exit(EXIT_FAILURE);
3471  }
3472  if (leapexpires <= hi_time)
3473  hi_time = leapexpires - 1;
3474  }
3475 }
3476 
3477 static char *
3478 shellquote(char *b, char const *s)
3479 {
3480  *b++ = '\'';
3481  while (*s)
3482  {
3483  if (*s == '\'')
3484  *b++ = '\'', *b++ = '\\', *b++ = '\'';
3485  *b++ = *s++;
3486  }
3487  *b++ = '\'';
3488  return b;
3489 }
3490 
3491 static bool
3492 yearistype(zic_t year, const char *type)
3493 {
3494  char *buf;
3495  char *b;
3496  int result;
3497 
3498  if (type == NULL || *type == '\0')
3499  return true;
3500  buf = emalloc(1 + 4 * strlen(yitcommand) + 2
3501  + INT_STRLEN_MAXIMUM(zic_t) + 2 + 4 * strlen(type) + 2);
3502  b = shellquote(buf, yitcommand);
3503  *b++ = ' ';
3504  b += sprintf(b, INT64_FORMAT, year);
3505  *b++ = ' ';
3506  b = shellquote(b, type);
3507  *b = '\0';
3508  result = system(buf);
3509  if (WIFEXITED(result))
3510  {
3511  int status = WEXITSTATUS(result);
3512 
3513  if (status <= 1)
3514  {
3515  free(buf);
3516  return status == 0;
3517  }
3518  }
3519  error(_("Wild result from command execution"));
3520  fprintf(stderr, _("%s: command was '%s', result was %d\n"),
3521  progname, buf, result);
3522  exit(EXIT_FAILURE);
3523 }
3524 
3525 /* Is A a space character in the C locale? */
3526 static bool
3527 is_space(char a)
3528 {
3529  switch (a)
3530  {
3531  default:
3532  return false;
3533  case ' ':
3534  case '\f':
3535  case '\n':
3536  case '\r':
3537  case '\t':
3538  case '\v':
3539  return true;
3540  }
3541 }
3542 
3543 /* Is A an alphabetic character in the C locale? */
3544 static bool
3545 is_alpha(char a)
3546 {
3547  switch (a)
3548  {
3549  default:
3550  return false;
3551  case 'A':
3552  case 'B':
3553  case 'C':
3554  case 'D':
3555  case 'E':
3556  case 'F':
3557  case 'G':
3558  case 'H':
3559  case 'I':
3560  case 'J':
3561  case 'K':
3562  case 'L':
3563  case 'M':
3564  case 'N':
3565  case 'O':
3566  case 'P':
3567  case 'Q':
3568  case 'R':
3569  case 'S':
3570  case 'T':
3571  case 'U':
3572  case 'V':
3573  case 'W':
3574  case 'X':
3575  case 'Y':
3576  case 'Z':
3577  case 'a':
3578  case 'b':
3579  case 'c':
3580  case 'd':
3581  case 'e':
3582  case 'f':
3583  case 'g':
3584  case 'h':
3585  case 'i':
3586  case 'j':
3587  case 'k':
3588  case 'l':
3589  case 'm':
3590  case 'n':
3591  case 'o':
3592  case 'p':
3593  case 'q':
3594  case 'r':
3595  case 's':
3596  case 't':
3597  case 'u':
3598  case 'v':
3599  case 'w':
3600  case 'x':
3601  case 'y':
3602  case 'z':
3603  return true;
3604  }
3605 }
3606 
3607 /* If A is an uppercase character in the C locale, return its lowercase
3608  counterpart. Otherwise, return A. */
3609 static char
3610 lowerit(char a)
3611 {
3612  switch (a)
3613  {
3614  default:
3615  return a;
3616  case 'A':
3617  return 'a';
3618  case 'B':
3619  return 'b';
3620  case 'C':
3621  return 'c';
3622  case 'D':
3623  return 'd';
3624  case 'E':
3625  return 'e';
3626  case 'F':
3627  return 'f';
3628  case 'G':
3629  return 'g';
3630  case 'H':
3631  return 'h';
3632  case 'I':
3633  return 'i';
3634  case 'J':
3635  return 'j';
3636  case 'K':
3637  return 'k';
3638  case 'L':
3639  return 'l';
3640  case 'M':
3641  return 'm';
3642  case 'N':
3643  return 'n';
3644  case 'O':
3645  return 'o';
3646  case 'P':
3647  return 'p';
3648  case 'Q':
3649  return 'q';
3650  case 'R':
3651  return 'r';
3652  case 'S':
3653  return 's';
3654  case 'T':
3655  return 't';
3656  case 'U':
3657  return 'u';
3658  case 'V':
3659  return 'v';
3660  case 'W':
3661  return 'w';
3662  case 'X':
3663  return 'x';
3664  case 'Y':
3665  return 'y';
3666  case 'Z':
3667  return 'z';
3668  }
3669 }
3670 
3671 /* case-insensitive equality */
3672 static bool
3673 ciequal(const char *ap, const char *bp)
3674 {
3675  while (lowerit(*ap) == lowerit(*bp++))
3676  if (*ap++ == '\0')
3677  return true;
3678  return false;
3679 }
3680 
3681 static bool
3682 itsabbr(const char *abbr, const char *word)
3683 {
3684  if (lowerit(*abbr) != lowerit(*word))
3685  return false;
3686  ++word;
3687  while (*++abbr != '\0')
3688  do
3689  {
3690  if (*word == '\0')
3691  return false;
3692  } while (lowerit(*word++) != lowerit(*abbr));
3693  return true;
3694 }
3695 
3696 /* Return true if ABBR is an initial prefix of WORD, ignoring ASCII case. */
3697 
3698 static bool
3699 ciprefix(char const *abbr, char const *word)
3700 {
3701  do
3702  if (!*abbr)
3703  return true;
3704  while (lowerit(*abbr++) == lowerit(*word++));
3705 
3706  return false;
3707 }
3708 
3709 static const struct lookup *
3710 byword(const char *word, const struct lookup *table)
3711 {
3712  const struct lookup *foundlp;
3713  const struct lookup *lp;
3714 
3715  if (word == NULL || table == NULL)
3716  return NULL;
3717 
3718  /*
3719  * If TABLE is LASTS and the word starts with "last" followed by a
3720  * non-'-', skip the "last" and look in WDAY_NAMES instead. Warn about any
3721  * usage of the undocumented prefix "last-".
3722  */
3723  if (table == lasts && ciprefix("last", word) && word[4])
3724  {
3725  if (word[4] == '-')
3726  warning(_("\"%s\" is undocumented; use \"last%s\" instead"),
3727  word, word + 5);
3728  else
3729  {
3730  word += 4;
3731  table = wday_names;
3732  }
3733  }
3734 
3735  /*
3736  * Look for exact match.
3737  */
3738  for (lp = table; lp->l_word != NULL; ++lp)
3739  if (ciequal(word, lp->l_word))
3740  return lp;
3741 
3742  /*
3743  * Look for inexact match.
3744  */
3745  foundlp = NULL;
3746  for (lp = table; lp->l_word != NULL; ++lp)
3747  if (ciprefix(word, lp->l_word))
3748  {
3749  if (foundlp == NULL)
3750  foundlp = lp;
3751  else
3752  return NULL; /* multiple inexact matches */
3753  }
3754 
3755  if (foundlp && noise)
3756  {
3757  /* Warn about any backward-compatibility issue with pre-2017c zic. */
3758  bool pre_2017c_match = false;
3759 
3760  for (lp = table; lp->l_word; lp++)
3761  if (itsabbr(word, lp->l_word))
3762  {
3763  if (pre_2017c_match)
3764  {
3765  warning(_("\"%s\" is ambiguous in pre-2017c zic"), word);
3766  break;
3767  }
3768  pre_2017c_match = true;
3769  }
3770  }
3771 
3772  return foundlp;
3773 }
3774 
3775 static char **
3776 getfields(char *cp)
3777 {
3778  char *dp;
3779  char **array;
3780  int nsubs;
3781 
3782  if (cp == NULL)
3783  return NULL;
3784  array = emalloc(size_product(strlen(cp) + 1, sizeof *array));
3785  nsubs = 0;
3786  for (;;)
3787  {
3788  while (is_space(*cp))
3789  ++cp;
3790  if (*cp == '\0' || *cp == '#')
3791  break;
3792  array[nsubs++] = dp = cp;
3793  do
3794  {
3795  if ((*dp = *cp++) != '"')
3796  ++dp;
3797  else
3798  while ((*dp = *cp++) != '"')
3799  if (*dp != '\0')
3800  ++dp;
3801  else
3802  {
3803  error(_("Odd number of quotation marks"));
3804  exit(EXIT_FAILURE);
3805  }
3806  } while (*cp && *cp != '#' && !is_space(*cp));
3807  if (is_space(*cp))
3808  ++cp;
3809  *dp = '\0';
3810  }
3811  array[nsubs] = NULL;
3812  return array;
3813 }
3814 
3815 static void
3817 {
3818  error(_("time overflow"));
3819  exit(EXIT_FAILURE);
3820 }
3821 
3822 static zic_t
3824 {
3825  if (t1 < 0 ? t2 < ZIC_MIN - t1 : ZIC_MAX - t1 < t2)
3826  time_overflow();
3827  return t1 + t2;
3828 }
3829 
3830 static zic_t
3832 {
3833  if (t1 < 0)
3834  {
3835  if (t2 < min_time - t1)
3836  {
3837  if (t1 != min_time)
3838  time_overflow();
3839  return min_time;
3840  }
3841  }
3842  else
3843  {
3844  if (max_time - t1 < t2)
3845  {
3846  if (t1 != max_time)
3847  time_overflow();
3848  return max_time;
3849  }
3850  }
3851  return t1 + t2;
3852 }
3853 
3854 /*
3855  * Given a rule, and a year, compute the date (in seconds since January 1,
3856  * 1970, 00:00 LOCAL time) in that year that the rule refers to.
3857  */
3858 
3859 static zic_t
3860 rpytime(const struct rule *rp, zic_t wantedy)
3861 {
3862  int m,
3863  i;
3864  zic_t dayoff; /* with a nod to Margaret O. */
3865  zic_t t,
3866  y;
3867 
3868  if (wantedy == ZIC_MIN)
3869  return min_time;
3870  if (wantedy == ZIC_MAX)
3871  return max_time;
3872  dayoff = 0;
3873  m = TM_JANUARY;
3874  y = EPOCH_YEAR;
3875  if (y < wantedy)
3876  {
3877  wantedy -= y;
3878  dayoff = (wantedy / YEARSPERREPEAT) * (SECSPERREPEAT / SECSPERDAY);
3879  wantedy %= YEARSPERREPEAT;
3880  wantedy += y;
3881  }
3882  else if (wantedy < 0)
3883  {
3884  dayoff = (wantedy / YEARSPERREPEAT) * (SECSPERREPEAT / SECSPERDAY);
3885  wantedy %= YEARSPERREPEAT;
3886  }
3887  while (wantedy != y)
3888  {
3889  if (wantedy > y)
3890  {
3891  i = len_years[isleap(y)];
3892  ++y;
3893  }
3894  else
3895  {
3896  --y;
3897  i = -len_years[isleap(y)];
3898  }
3899  dayoff = oadd(dayoff, i);
3900  }
3901  while (m != rp->r_month)
3902  {
3903  i = len_months[isleap(y)][m];
3904  dayoff = oadd(dayoff, i);
3905  ++m;
3906  }
3907  i = rp->r_dayofmonth;
3908  if (m == TM_FEBRUARY && i == 29 && !isleap(y))
3909  {
3910  if (rp->r_dycode == DC_DOWLEQ)
3911  --i;
3912  else
3913  {
3914  error(_("use of 2/29 in non leap-year"));
3915  exit(EXIT_FAILURE);
3916  }
3917  }
3918  --i;
3919  dayoff = oadd(dayoff, i);
3920  if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ)
3921  {
3922  zic_t wday;
3923 
3924 #define LDAYSPERWEEK ((zic_t) DAYSPERWEEK)
3925  wday = EPOCH_WDAY;
3926 
3927  /*
3928  * Don't trust mod of negative numbers.
3929  */
3930  if (dayoff >= 0)
3931  wday = (wday + dayoff) % LDAYSPERWEEK;
3932  else
3933  {
3934  wday -= ((-dayoff) % LDAYSPERWEEK);
3935  if (wday < 0)
3936  wday += LDAYSPERWEEK;
3937  }
3938  while (wday != rp->r_wday)
3939  if (rp->r_dycode == DC_DOWGEQ)
3940  {
3941  dayoff = oadd(dayoff, 1);
3942  if (++wday >= LDAYSPERWEEK)
3943  wday = 0;
3944  ++i;
3945  }
3946  else
3947  {
3948  dayoff = oadd(dayoff, -1);
3949  if (--wday < 0)
3950  wday = LDAYSPERWEEK - 1;
3951  --i;
3952  }
3953  if (i < 0 || i >= len_months[isleap(y)][m])
3954  {
3955  if (noise)
3956  warning(_("rule goes past start/end of month; \
3957 will not work with pre-2004 versions of zic"));
3958  }
3959  }
3960  if (dayoff < min_time / SECSPERDAY)
3961  return min_time;
3962  if (dayoff > max_time / SECSPERDAY)
3963  return max_time;
3964  t = (zic_t) dayoff * SECSPERDAY;
3965  return tadd(t, rp->r_tod);
3966 }
3967 
3968 static void
3969 newabbr(const char *string)
3970 {
3971  int i;
3972 
3973  if (strcmp(string, GRANDPARENTED) != 0)
3974  {
3975  const char *cp;
3976  const char *mp;
3977 
3978  cp = string;
3979  mp = NULL;
3980  while (is_alpha(*cp) || ('0' <= *cp && *cp <= '9')
3981  || *cp == '-' || *cp == '+')
3982  ++cp;
3983  if (noise && cp - string < 3)
3984  mp = _("time zone abbreviation has fewer than 3 characters");
3985  if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
3986  mp = _("time zone abbreviation has too many characters");
3987  if (*cp != '\0')
3988  mp = _("time zone abbreviation differs from POSIX standard");
3989  if (mp != NULL)
3990  warning("%s (%s)", mp, string);
3991  }
3992  i = strlen(string) + 1;
3993  if (charcnt + i > TZ_MAX_CHARS)
3994  {
3995  error(_("too many, or too long, time zone abbreviations"));
3996  exit(EXIT_FAILURE);
3997  }
3998  strcpy(&chars[charcnt], string);
3999  charcnt += i;
4000 }
4001 
4002 /* Ensure that the directories of ARGNAME exist, by making any missing
4003  ones. If ANCESTORS, do this only for ARGNAME's ancestors; otherwise,
4004  do it for ARGNAME too. Exit with failure if there is trouble.
4005  Do not consider an existing non-directory to be trouble. */
4006 static void
4007 mkdirs(char const *argname, bool ancestors)
4008 {
4009  char *name;
4010  char *cp;
4011 
4012  cp = name = ecpyalloc(argname);
4013 
4014  /*
4015  * On MS-Windows systems, do not worry about drive letters or backslashes,
4016  * as this should suffice in practice. Time zone names do not use drive
4017  * letters and backslashes. If the -d option of zic does not name an
4018  * already-existing directory, it can use slashes to separate the
4019  * already-existing ancestor prefix from the to-be-created subdirectories.
4020  */
4021 
4022  /* Do not mkdir a root directory, as it must exist. */
4023  while (*cp == '/')
4024  cp++;
4025 
4026  while (cp && ((cp = strchr(cp, '/')) || !ancestors))
4027  {
4028  if (cp)
4029  *cp = '\0';
4030 
4031  /*
4032  * Try to create it. It's OK if creation fails because the directory
4033  * already exists, perhaps because some other process just created it.
4034  * For simplicity do not check first whether it already exists, as
4035  * that is checked anyway if the mkdir fails.
4036  */
4037  if (mkdir(name, MKDIR_UMASK) != 0)
4038  {
4039  /*
4040  * For speed, skip itsdir if errno == EEXIST. Since mkdirs is
4041  * called only after open fails with ENOENT on a subfile, EEXIST
4042  * implies itsdir here.
4043  */
4044  int err = errno;
4045 
4046  if (err != EEXIST && !itsdir(name))
4047  {
4048  error(_("%s: Cannot create directory %s: %s"),
4049  progname, name, strerror(err));
4050  exit(EXIT_FAILURE);
4051  }
4052  }
4053  if (cp)
4054  *cp++ = '/';
4055  }
4056  free(name);
4057 }
4058 
4059 
4060 #ifdef WIN32
4061 /*
4062  * To run on win32
4063  */
4064 int
4065 link(const char *oldpath, const char *newpath)
4066 {
4067  if (!CopyFile(oldpath, newpath, false))
4068  {
4069  _dosmaperr(GetLastError());
4070  return -1;
4071  }
4072  return 0;
4073 }
4074 #endif
#define TZDEFAULT
Definition: tzfile.h:27
static bool ciprefix(char const *abbr, char const *word)
Definition: zic.c:3699
static bool yearistype(zic_t year, const char *type)
Definition: zic.c:3492
static bool ttisstds[TZ_MAX_TYPES]
Definition: zic.c:399
#define LP_CORR
Definition: zic.c:268
static ptrdiff_t nlinks
Definition: zic.c:300
#define ZF_TILMONTH
Definition: zic.c:217
static zic_t leapmaxyear
Definition: zic.c:182
#define pg_attribute_noreturn()
Definition: c.h:145
lineno_t r_linenum
Definition: zic.c:64
static int rcomp(const void *cp1, const void *cp2)
Definition: zic.c:1153
#define SECSPERMIN
Definition: private.h:101
static int hardlinkerr(char const *from, char const *to)
Definition: zic.c:1001
static bool print_abbrevs
Definition: zic.c:189
#define YR_MINIMUM
Definition: zic.c:279
static zic_t print_cutoff
Definition: zic.c:190
static int typecnt
Definition: zic.c:196
zic_t z_save
Definition: zic.c:110
static void change_directory(char const *dir)
Definition: zic.c:562
#define PG_INT64_MAX
Definition: c.h:460
#define TM_MARCH
Definition: private.h:121
static void inexpires(char **, int)
Definition: zic.c:1789
static struct lookup const zi_line_codes[]
Definition: zic.c:312
zic_t r_tod
Definition: zic.c:79
static int stringzone(char *result, struct zone const *zpfirst, ptrdiff_t zonecount)
Definition: zic.c:2820
#define TM_THURSDAY
Definition: private.h:115
#define EXIT_SUCCESS
Definition: settings.h:150
static struct lookup const leap_line_codes[]
Definition: zic.c:318
static int bloat
Definition: zic.c:642
#define ZF_TILTIME
Definition: zic.c:219
Definition: zic.c:390
#define ZF_TILYEAR
Definition: zic.c:216
struct typedefs * types
Definition: ecpg.c:29
#define ZF_NAME
Definition: zic.c:212
#define TM_FRIDAY
Definition: private.h:116
unsigned char type
Definition: zic.c:394
#define ZFC_TILMONTH
Definition: zic.c:231
bool r_todisstd
Definition: zic.c:80
static zic_t lo_time
Definition: zic.c:590
static char isdsts[TZ_MAX_TYPES]
Definition: zic.c:397
#define INT_STRLEN_MAXIMUM(type)
Definition: private.h:85
ptrdiff_t count
Definition: zic.c:2057
static char * ecpyalloc(char const *str)
Definition: zic.c:446
#define ZF_RULE
Definition: zic.c:214
static void static void static void warning(const char *string,...) pg_attribute_printf(1
Definition: zic.c:515
#define LF_TO
Definition: zic.c:257
static void inrule(char **fields, int nfields)
Definition: zic.c:1473
static zic_t corr[TZ_MAX_LEAPS]
Definition: zic.c:403
int r_month
Definition: zic.c:73
#define DC_DOWGEQ
Definition: zic.c:95
static struct lookup const begin_years[]
Definition: zic.c:362
#define TM_APRIL
Definition: private.h:122
#define TM_SATURDAY
Definition: private.h:117
#define TM_JULY
Definition: private.h:125
static void addtt(zic_t starttime, int type)
Definition: zic.c:3359
#define RF_COMMAND
Definition: zic.c:244
static bool errors
Definition: zic.c:176
#define ZF_STDOFF
Definition: zic.c:213
#define SECSPERDAY
Definition: private.h:108
#define isleap(y)
Definition: datetime.h:271
#define ENOTSUP
Definition: private.h:38
static void associate(void)
Definition: zic.c:1160
#define INITIALIZE(x)
Definition: private.h:92
#define S_IWOTH
Definition: win32_port.h:287
const char * z_format
Definition: zic.c:106
zic_t r_save
Definition: zic.c:83
#define LC_LEAP
Definition: zic.c:205
void _dosmaperr(unsigned long)
Definition: win32error.c:171
#define TM_DECEMBER
Definition: private.h:130
static int rule_cmp(struct rule const *a, struct rule const *b)
Definition: zic.c:2806
#define SECSPERHOUR
Definition: private.h:107
#define DC_DOM
Definition: zic.c:94
#define MONSPERYEAR
Definition: private.h:109
static zic_t hi_time
Definition: zic.c:591
zic_t z_untiltime
Definition: zic.c:116
#define HOURSPERDAY
Definition: private.h:103
#define TZ_MAX_TIMES
Definition: tzfile.h:100
#define printf(...)
Definition: port.h:199
#define DAYSPERNYEAR
Definition: private.h:105
#define ZIC_BLOAT_DEFAULT
Definition: zic.c:651
static ptrdiff_t nzones
Definition: zic.c:288
struct rule z_untilrule
Definition: zic.c:115
static zic_t min_year
Definition: zic.c:187
static const int len_months[2][MONSPERYEAR]
Definition: zic.c:381
char tzh_timecnt[4]
Definition: tzfile.h:47
static const char * yitcommand
Definition: zic.c:637
NameData relname
Definition: pg_class.h:38
static void updateminmax(const zic_t x)
Definition: zic.c:2682
#define ZONEC_MAXFIELDS
Definition: zic.c:235
#define ZFC_FORMAT
Definition: zic.c:229
static bool warnings
Definition: zic.c:177
int leapbase
Definition: zic.c:2059
static const char * leapsec
Definition: zic.c:635
#define fprintf
Definition: port.h:197
static int max_format_len
Definition: zic.c:185
static int stringoffset(char *result, zic_t offset)
Definition: zic.c:2691
const int l_value
Definition: zic.c:306
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition: getopt.c:71
static void mkdirs(char const *, bool)
Definition: zic.c:4007
bool z_isdst
Definition: zic.c:109
char tzh_ttisutcnt[4]
Definition: tzfile.h:44
#define TM_AUGUST
Definition: private.h:126
#define LP_ROLL
Definition: zic.c:269
static lineno_t rlinenum
Definition: zic.c:192
Definition: localtime.c:72
signed int int32
Definition: c.h:362
static const char * filename
Definition: zic.c:178
#define pg_attribute_printf(f, a)
Definition: c.h:130
#define GRANDPARENTED
Definition: private.h:30
bool r_lowasnum
Definition: zic.c:70
static void inleap(char **fields, int nfields)
Definition: zic.c:1757
char z_format_specifier
Definition: zic.c:107
static char ** getfields(char *buf)
Definition: zic.c:3776
#define DAYSPERLYEAR
Definition: private.h:106
char * z_rule
Definition: zic.c:105
static struct pg_tm tm
Definition: localtime.c:102
static const char * psxrules
Definition: zic.c:632
#define TM_SEPTEMBER
Definition: private.h:127
#define malloc(a)
Definition: header.h:50
#define ZIC_MAX
Definition: zic.c:27
static bool itsdir(char const *)
Definition: zic.c:1108
const char * r_name
Definition: zic.c:65
static char const * abbroffset(char *buf, zic_t offset)
Definition: zic.c:2595
#define TM_MAY
Definition: private.h:123
#define ZF_FORMAT
Definition: zic.c:215
static void static void static void static void usage(FILE *stream, int status) pg_attribute_noreturn()
Definition: zic.c:543
#define LDAYSPERWEEK
#define ZFC_STDOFF
Definition: zic.c:227
#define TZ_MAX_LEAPS
Definition: tzfile.h:108
static void verror(const char *string, va_list args) pg_attribute_printf(1
Definition: zic.c:488
#define sprintf
Definition: port.h:195
static void leapadd(zic_t, int, int)
Definition: zic.c:3414
static bool is_alpha(char a)
Definition: zic.c:3545
const char * z_filename
Definition: zic.c:100
#define YR_ONLY
Definition: zic.c:281
static void dolink(const char *, const char *, bool)
Definition: zic.c:1009
static zic_t tadd(zic_t t1, zic_t t2)
Definition: zic.c:3831
static bool is_space(char a)
Definition: zic.c:3527
static void convert(const int32 val, char *const buf)
Definition: zic.c:2001
static zic_t comment_leapexpires
Definition: zic.c:597
int optind
Definition: getopt.c:50
#define TIME_T_BITS_IN_FILE
Definition: zic.c:582
static zic_t gethms(const char *string, const char *errstring)
Definition: zic.c:1367
static lineno_t linenum
Definition: zic.c:183
static bool inzcont(char **fields, int nfields)
Definition: zic.c:1558
static int addtype(zic_t, char const *, bool, bool, bool)
Definition: zic.c:3369
static char * shellquote(char *b, char const *s)
Definition: zic.c:3478
char tzh_magic[4]
Definition: tzfile.h:41
#define LEAP_FIELDS
Definition: zic.c:270
static bool inzsub(char **, int, bool)
Definition: zic.c:1569
bool r_todo
Definition: zic.c:86
#define TM_OCTOBER
Definition: private.h:128
static void writezone(const char *const name, const char *const string, char version, int defaulttype)
Definition: zic.c:2091
int defaulttype
Definition: zic.c:2056
static size_t doabbr(char *abbr, struct zone const *zp, char const *letters, bool isdst, zic_t save, bool doquotes)
Definition: zic.c:2639
#define DAYSPERWEEK
Definition: private.h:104
static const char * rfilename
Definition: zic.c:191
static struct lookup const mon_names[]
Definition: zic.c:324
#define ZONE_MAXFIELDS
Definition: zic.c:221
static bool componentcheck(char const *name, char const *component, char const *component_end)
Definition: zic.c:864
char sign
Definition: informix.c:668
#define TZ_MAGIC
Definition: tzfile.h:37
#define LP_DAY
Definition: zic.c:266
#define ZIC_VERSION_PRE_2013
Definition: zic.c:22
bool r_isdst
Definition: zic.c:82
#define linkat(fromdir, from, todir, to, flag)
Definition: zic.c:41
#define MINVAL(t, b)
Definition: private.h:72
char * c
static bool leapseen
Definition: zic.c:180
static char * buf
Definition: pg_test_fsync.c:67
#define symlink(oldpath, newpath)
Definition: win32_port.h:221
#define PG_INT64_MIN
Definition: c.h:459
#define ZIC_VERSION
Definition: zic.c:23
static void adjleap(void)
Definition: zic.c:3436
static int stringrule(char *result, struct rule *const rp, zic_t save, zic_t stdoff)
Definition: zic.c:2725
#define readlink(path, buf, size)
Definition: win32_port.h:222
static const char * lcltime
Definition: zic.c:633
char string[11]
Definition: preproc-type.c:46
static int leapcnt
Definition: zic.c:179
static ptrdiff_t nzones_alloc
Definition: zic.c:289
static int max_abbrvar_len
Definition: zic.c:184
#define ZFC_TILTIME
Definition: zic.c:233
int64 zic_t
Definition: zic.c:25
static struct rule * rules
Definition: zic.c:283
Definition: zic.c:303
zic_t r_hiyear
Definition: zic.c:68
#define S_IWGRP
Definition: win32_port.h:275
static bool ciequal(const char *ap, const char *bp)
Definition: zic.c:3673
static zic_t getleapdatetime(char **fields, int nfields, bool expire_line)
Definition: zic.c:1668
const char * r_yrtype
Definition: zic.c:69
zic_t r_temp
Definition: zic.c:87
#define ZIC_MIN
Definition: zic.c:26
static zic_t leapexpires
Definition: zic.c:594
char tzh_leapcnt[4]
Definition: tzfile.h:46
static void * growalloc(void *ptr, size_t itemsize, ptrdiff_t nitems, ptrdiff_t *nitems_alloc)
Definition: zic.c:452
bool r_todisut
Definition: zic.c:81
#define PG_INT32_MIN
Definition: c.h:456
static struct lookup const end_years[]
Definition: zic.c:368
const char * r_filename
Definition: zic.c:63
#define RF_SAVE
Definition: zic.c:248
int link(const char *fromname, const char *toname)
#define ZFC_RULE
Definition: zic.c:228
enum COMPAT_MODE compat
Definition: ecpg.c:25
int r_dycode
Definition: zic.c:75
#define ZIC_MAX_ABBR_LEN_WO_WARN
Definition: zic.c:30
static zic_t utoffs[TZ_MAX_TYPES]
Definition: zic.c:396
const char * l_word
Definition: zic.c:305
#define TZ_MAX_CHARS
Definition: tzfile.h:105
#define TM_SUNDAY
Definition: private.h:111
#define stat(a, b)
Definition: win32_port.h:255
zic_t at
Definition: zic.c:392
#define EPOCH_YEAR
Definition: private.h:134
#define DC_DOWLEQ
Definition: zic.c:96
#define LP_TIME
Definition: zic.c:267
static zic_t rpytime(const struct rule *rp, zic_t wantedy)
Definition: zic.c:3860
static ptrdiff_t timecnt_alloc
Definition: zic.c:195
#define WIFEXITED(w)
Definition: win32_port.h:137
#define strtoimax
Definition: private.h:52
static ptrdiff_t nlinks_alloc
Definition: zic.c:301
static bool itssymlink(char const *)
Definition: zic.c:1133
#define LC_LINK
Definition: zic.c:204
static void time_overflow(void)
Definition: zic.c:3816
static void eat(char const *name, lineno_t num)
Definition: zic.c:482
#define ZF_TILDAY
Definition: zic.c:218
const char * r_abbrvar
Definition: zic.c:84
#define RF_HIYEAR
Definition: zic.c:243
#define LF_FROM
Definition: zic.c:256
static struct attype * attypes
#define LC_ZONE
Definition: zic.c:203
static zic_t const max_time
Definition: zic.c:586
int leapcount
Definition: zic.c:2059
static bool noise
Definition: zic.c:188
static struct link * links
Definition: zic.c:299
ptrdiff_t z_nrules
Definition: zic.c:113
Definition: tzfile.h:39
#define TM_JANUARY
Definition: private.h:119
static void puttzcodepass(zic_t val, FILE *fp, int pass)
Definition: zic.c:2032
static void infile(const char *filename)
Definition: zic.c:1245
int main(int argc, char **argv)
Definition: zic.c:655
lineno_t z_linenum
Definition: zic.c:101
char tzh_typecnt[4]
Definition: tzfile.h:48
#define TM_FEBRUARY
Definition: private.h:120
static const char * progname
Definition: zic.c:193
static zic_t max_year
Definition: zic.c:186
#define free(a)
Definition: header.h:65
#define TM_JUNE
Definition: private.h:124
static bool inzone(char **fields, int nfields)
Definition: zic.c:1520
#define LINK_FIELDS
Definition: zic.c:258
char tzh_version[1]
Definition: tzfile.h:42
int r_dayofmonth
Definition: zic.c:76
int r_wday
Definition: zic.c:77
char tzh_charcnt[4]
Definition: tzfile.h:49
ptrdiff_t base
Definition: zic.c:2057
#define TZDEFRULES
Definition: tzfile.h:28
#define MKDIR_UMASK
Definition: zic.c:37
#define LC_EXPIRES
Definition: zic.c:206
static ptrdiff_t timecnt
Definition: zic.c:194
#define RF_NAME
Definition: zic.c:241
static ptrdiff_t nrules_alloc
Definition: zic.c:285
static void rulesub(struct rule *rp, const char *loyearp, const char *hiyearp, const char *typep, const char *monthp, const char *dayp, const char *timep)
Definition: zic.c:1825
static void inlink(char **fields, int nfields)
Definition: zic.c:1800
static zic_t getsave(char *, bool *)
Definition: zic.c:1445
#define EPOCH_WDAY
Definition: private.h:135
#define strerror
Definition: port.h:206
static const char * directory
Definition: zic.c:634
static int charcnt
Definition: zic.c:175
const char * z_name
Definition: zic.c:103
static void close_file(FILE *stream, char const *dir, char const *name)
Definition: zic.c:527
static struct lookup const * byword(const char *string, const struct lookup *lp)
Definition: zic.c:3710
static ptrdiff_t const PTRDIFF_MAX
Definition: zic.c:51
#define MAXALIGN(LEN)
Definition: c.h:698
#define realloc(a, b)
Definition: header.h:60
#define INT64_FORMAT
Definition: c.h:416
const char * name
Definition: encode.c:561
static int atcomp(const void *avp, const void *bvp)
Definition: zic.c:2046
static char lowerit(char)
Definition: zic.c:3610
#define TM_TUESDAY
Definition: private.h:113
#define EXPIRES_FIELDS
Definition: zic.c:273
static zic_t leapminyear
Definition: zic.c:181
static ptrdiff_t nrules
Definition: zic.c:284
#define S_ISDIR(m)
Definition: win32_port.h:296
static void word(struct vars *, int, struct state *, struct state *)
Definition: regcomp.c:1246
#define YEARSPERREPEAT
Definition: private.h:99
static struct zone * zones
Definition: zic.c:287
struct rule * z_rules
Definition: zic.c:112
static struct timerange limitrange(struct timerange r, zic_t lo, zic_t hi, zic_t const *ats, unsigned char const *types)
Definition: zic.c:2064
static void outzone(const struct zone *zp, ptrdiff_t ntzones)
Definition: zic.c:2957
#define LP_YEAR
Definition: zic.c:264
#define MAXVAL(t, b)
Definition: private.h:69
Definition: zic.c:98
#define YR_MAXIMUM
Definition: zic.c:280
e
Definition: preproc-init.c:82
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:402
#define TZ_MAX_TYPES
Definition: tzfile.h:103
static struct lookup const lasts[]
Definition: zic.c:351
bool r_hiwasnum
Definition: zic.c:71
#define RF_TOD
Definition: zic.c:247
static struct lookup const wday_names[]
Definition: zic.c:340
#define PG_INT32_MAX
Definition: c.h:457
char * optarg
Definition: getopt.c:52
static void * memcheck(void *ptr)
Definition: zic.c:426
static void newabbr(const char *abbr)
Definition: zic.c:3969
#define TM_MONDAY
Definition: private.h:112
char tzh_ttisstdcnt[4]
Definition: tzfile.h:45
#define ZFC_TILDAY
Definition: zic.c:232
#define RF_ABBRVAR
Definition: zic.c:249
#define TYPE_BIT(type)
Definition: private.h:60
int i
static struct lookup const leap_types[]
Definition: zic.c:375
#define EXIT_FAILURE
Definition: settings.h:154
static const char * tzdefault
Definition: zic.c:636
static const int len_years[2]
Definition: zic.c:386
int lineno_t
Definition: zic.c:59
static char format
static bool timerange_option(char *timerange)
Definition: zic.c:602
#define mkdir(a, b)
Definition: win32_port.h:57
#define SECSPERREPEAT
Definition: private.h:159
#define vfprintf
Definition: port.h:196
#define qsort(a, b, c, d)
Definition: port.h:479
#define DO(field)
#define EOVERFLOW
Definition: private.h:41
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
static bool ttisuts[TZ_MAX_TYPES]
Definition: zic.c:400
zic_t r_loyear
Definition: zic.c:67
static zic_t const min_time
Definition: zic.c:585
#define TM_WEDNESDAY
Definition: private.h:114
#define LP_MONTH
Definition: zic.c:265
static void memory_exhausted(const char *msg) pg_attribute_noreturn()
Definition: zic.c:411
#define ZFC_TILYEAR
Definition: zic.c:230
bool dontmerge
Definition: zic.c:393
static void eats(char const *name, lineno_t num, char const *rname, lineno_t rnum)
Definition: zic.c:473
static char chars[TZ_MAX_CHARS]
Definition: zic.c:401
#define TM_NOVEMBER
Definition: private.h:129
#define RF_LOYEAR
Definition: zic.c:242
#define LC_RULE
Definition: zic.c:202
#define _(x)
Definition: elog.c:88
long val
Definition: informix.c:664
static zic_t oadd(zic_t t1, zic_t t2)
Definition: zic.c:3823
static size_t size_product(size_t nitems, size_t itemsize)
Definition: zic.c:418
#define MINSPERHOUR
Definition: private.h:102
static bool want_bloat(void)
Definition: zic.c:645
static void puttzcode(const int32 val, FILE *const fp)
Definition: zic.c:2023
static void * erealloc(void *ptr, size_t size)
Definition: zic.c:440
#define RULE_FIELDS
Definition: zic.c:250
static unsigned char desigidx[TZ_MAX_TYPES]
Definition: zic.c:398
#define RF_MONTH
Definition: zic.c:245
zic_t z_stdoff
Definition: zic.c:104
#define WEXITSTATUS(w)
Definition: win32_port.h:139
static void convert64(const zic_t val, char *const buf)
Definition: zic.c:2012
static bool namecheck(const char *name)
Definition: zic.c:908
#define RF_DAY
Definition: zic.c:246
static char roll[TZ_MAX_LEAPS]
Definition: zic.c:404
static bool itsabbr(const char *abbr, const char *word)
Definition: zic.c:3682
static void * emalloc(size_t size)
Definition: zic.c:434