PostgreSQL Source Code  git master
zic.c File Reference
#include "postgres_fe.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <time.h>
#include "pg_getopt.h"
#include "private.h"
#include "tzfile.h"
Include dependency graph for zic.c:

Go to the source code of this file.

Data Structures

struct  rule
 
struct  zone
 
struct  link
 
struct  lookup
 
struct  attype
 
struct  timerange
 

Macros

#define ZIC_VERSION_PRE_2013   '2'
 
#define ZIC_VERSION   '3'
 
#define ZIC_MIN   PG_INT64_MIN
 
#define ZIC_MAX   PG_INT64_MAX
 
#define ZIC_MAX_ABBR_LEN_WO_WARN   6
 
#define MKDIR_UMASK   0755
 
#define linkat(fromdir, from, todir, to, flag)   (itssymlink(from) ? (errno = ENOTSUP, -1) : link(from, to))
 
#define DC_DOM   0 /* 1..31 */ /* unused */
 
#define DC_DOWGEQ   1 /* 1..31 */ /* 0..6 (Sun..Sat) */
 
#define DC_DOWLEQ   2 /* 1..31 */ /* 0..6 (Sun..Sat) */
 
#define LC_RULE   0
 
#define LC_ZONE   1
 
#define LC_LINK   2
 
#define LC_LEAP   3
 
#define LC_EXPIRES   4
 
#define ZF_NAME   1
 
#define ZF_STDOFF   2
 
#define ZF_RULE   3
 
#define ZF_FORMAT   4
 
#define ZF_TILYEAR   5
 
#define ZF_TILMONTH   6
 
#define ZF_TILDAY   7
 
#define ZF_TILTIME   8
 
#define ZONE_MINFIELDS   5
 
#define ZONE_MAXFIELDS   9
 
#define ZFC_STDOFF   0
 
#define ZFC_RULE   1
 
#define ZFC_FORMAT   2
 
#define ZFC_TILYEAR   3
 
#define ZFC_TILMONTH   4
 
#define ZFC_TILDAY   5
 
#define ZFC_TILTIME   6
 
#define ZONEC_MINFIELDS   3
 
#define ZONEC_MAXFIELDS   7
 
#define RF_NAME   1
 
#define RF_LOYEAR   2
 
#define RF_HIYEAR   3
 
#define RF_COMMAND   4
 
#define RF_MONTH   5
 
#define RF_DAY   6
 
#define RF_TOD   7
 
#define RF_SAVE   8
 
#define RF_ABBRVAR   9
 
#define RULE_FIELDS   10
 
#define LF_FROM   1
 
#define LF_TO   2
 
#define LINK_FIELDS   3
 
#define LP_YEAR   1
 
#define LP_MONTH   2
 
#define LP_DAY   3
 
#define LP_TIME   4
 
#define LP_CORR   5
 
#define LP_ROLL   6
 
#define LEAP_FIELDS   7
 
#define EXPIRES_FIELDS   5
 
#define YR_MINIMUM   0
 
#define YR_MAXIMUM   1
 
#define YR_ONLY   2
 
#define TIME_T_BITS_IN_FILE   64
 
#define ZIC_BLOAT_DEFAULT   "fat"
 
#define DO(field)   fwrite(tzh.field, sizeof tzh.field, 1, fp)
 
#define LDAYSPERWEEK   ((zic_t) DAYSPERWEEK)
 

Typedefs

typedef int64 zic_t
 
typedef int lineno_t
 

Enumerations

enum  { PERCENT_Z_LEN_BOUND = sizeof "+995959" - 1 }
 
enum  { WORK_AROUND_QTBUG_53071 = true }
 

Functions

int link (const char *fromname, const char *toname)
 
static void memory_exhausted (const char *msg) pg_attribute_noreturn()
 
static void verror (const char *string, va_list args) pg_attribute_printf(1
 
static void static void error (const char *string,...) pg_attribute_printf(1
 
static void static void static void warning (const char *string,...) pg_attribute_printf(1
 
static void static void static void static void usage (FILE *stream, int status) pg_attribute_noreturn()
 
static void addtt (zic_t starttime, int type)
 
static int addtype (zic_t, char const *, bool, bool, bool)
 
static void leapadd (zic_t, int, int)
 
static void adjleap (void)
 
static void associate (void)
 
static void dolink (const char *, const char *, bool)
 
static char ** getfields (char *buf)
 
static zic_t gethms (const char *string, const char *errstring)
 
static zic_t getsave (char *, bool *)
 
static void inexpires (char **, int)
 
static void infile (const char *filename)
 
static void inleap (char **fields, int nfields)
 
static void inlink (char **fields, int nfields)
 
static void inrule (char **fields, int nfields)
 
static bool inzcont (char **fields, int nfields)
 
static bool inzone (char **fields, int nfields)
 
static bool inzsub (char **, int, bool)
 
static bool itsdir (char const *)
 
static bool itssymlink (char const *)
 
static bool is_alpha (char a)
 
static char lowerit (char)
 
static void mkdirs (char const *, bool)
 
static void newabbr (const char *abbr)
 
static zic_t oadd (zic_t t1, zic_t t2)
 
static void outzone (const struct zone *zp, ptrdiff_t ntzones)
 
static zic_t rpytime (const struct rule *rp, zic_t wantedy)
 
static void rulesub (struct rule *rp, const char *loyearp, const char *hiyearp, const char *typep, const char *monthp, const char *dayp, const char *timep)
 
static zic_t tadd (zic_t t1, zic_t t2)
 
static bool yearistype (zic_t year, const char *type)
 
static struct lookup const * byword (const char *string, const struct lookup *lp)
 
static size_t size_product (size_t nitems, size_t itemsize)
 
static void * memcheck (void *ptr)
 
static void * emalloc (size_t size)
 
static void * erealloc (void *ptr, size_t size)
 
static char * ecpyalloc (char const *str)
 
static void * growalloc (void *ptr, size_t itemsize, ptrdiff_t nitems, ptrdiff_t *nitems_alloc)
 
static void eats (char const *name, lineno_t num, char const *rname, lineno_t rnum)
 
static void eat (char const *name, lineno_t num)
 
static void close_file (FILE *stream, char const *dir, char const *name)
 
static void change_directory (char const *dir)
 
static bool timerange_option (char *timerange)
 
static bool want_bloat (void)
 
int main (int argc, char **argv)
 
static bool componentcheck (char const *name, char const *component, char const *component_end)
 
static bool namecheck (const char *name)
 
static int hardlinkerr (char const *from, char const *to)
 
static int rcomp (const void *cp1, const void *cp2)
 
static zic_t getleapdatetime (char **fields, int nfields, bool expire_line)
 
static void convert (const int32 val, char *const buf)
 
static void convert64 (const zic_t val, char *const buf)
 
static void puttzcode (const int32 val, FILE *const fp)
 
static void puttzcodepass (zic_t val, FILE *fp, int pass)
 
static int atcomp (const void *avp, const void *bvp)
 
static struct timerange limitrange (struct timerange r, zic_t lo, zic_t hi, zic_t const *ats, unsigned char const *types)
 
static void writezone (const char *const name, const char *const string, char version, int defaulttype)
 
static char const * abbroffset (char *buf, zic_t offset)
 
static size_t doabbr (char *abbr, struct zone const *zp, char const *letters, bool isdst, zic_t save, bool doquotes)
 
static void updateminmax (const zic_t x)
 
static int stringoffset (char *result, zic_t offset)
 
static int stringrule (char *result, struct rule *const rp, zic_t save, zic_t stdoff)
 
static int rule_cmp (struct rule const *a, struct rule const *b)
 
static int stringzone (char *result, struct zone const *zpfirst, ptrdiff_t zonecount)
 
static char * shellquote (char *b, char const *s)
 
static bool is_space (char a)
 
static bool ciequal (const char *ap, const char *bp)
 
static bool itsabbr (const char *abbr, const char *word)
 
static bool ciprefix (char const *abbr, char const *word)
 
static void time_overflow (void)
 

Variables

static ptrdiff_t const PTRDIFF_MAX = MAXVAL(ptrdiff_t, TYPE_BIT(ptrdiff_t))
 
static int charcnt
 
static bool errors
 
static bool warnings
 
static const char * filename
 
static int leapcnt
 
static bool leapseen
 
static zic_t leapminyear
 
static zic_t leapmaxyear
 
static lineno_t linenum
 
static int max_abbrvar_len = PERCENT_Z_LEN_BOUND
 
static int max_format_len
 
static zic_t max_year
 
static zic_t min_year
 
static bool noise
 
static bool print_abbrevs
 
static zic_t print_cutoff
 
static const char * rfilename
 
static lineno_t rlinenum
 
static const char * progname
 
static ptrdiff_t timecnt
 
static ptrdiff_t timecnt_alloc
 
static int typecnt
 
static struct rulerules
 
static ptrdiff_t nrules
 
static ptrdiff_t nrules_alloc
 
static struct zonezones
 
static ptrdiff_t nzones
 
static ptrdiff_t nzones_alloc
 
static struct linklinks
 
static ptrdiff_t nlinks
 
static ptrdiff_t nlinks_alloc
 
static struct lookup const zi_line_codes []
 
static struct lookup const leap_line_codes []
 
static struct lookup const mon_names []
 
static struct lookup const wday_names []
 
static struct lookup const lasts []
 
static struct lookup const begin_years []
 
static struct lookup const end_years []
 
static struct lookup const leap_types []
 
static const int len_months [2][MONSPERYEAR]
 
static const int len_years [2]
 
static struct attypeattypes
 
static zic_t utoffs [TZ_MAX_TYPES]
 
static char isdsts [TZ_MAX_TYPES]
 
static unsigned char desigidx [TZ_MAX_TYPES]
 
static bool ttisstds [TZ_MAX_TYPES]
 
static bool ttisuts [TZ_MAX_TYPES]
 
static char chars [TZ_MAX_CHARS]
 
static zic_t trans [TZ_MAX_LEAPS]
 
static zic_t corr [TZ_MAX_LEAPS]
 
static char roll [TZ_MAX_LEAPS]
 
static zic_t const min_time = MINVAL(zic_t, TIME_T_BITS_IN_FILE)
 
static zic_t const max_time = MAXVAL(zic_t, TIME_T_BITS_IN_FILE)
 
static zic_t lo_time = MINVAL(zic_t, TIME_T_BITS_IN_FILE)
 
static zic_t hi_time = MAXVAL(zic_t, TIME_T_BITS_IN_FILE)
 
static zic_t leapexpires = -1
 
static zic_t comment_leapexpires = -1
 
static const char * psxrules
 
static const char * lcltime
 
static const char * directory
 
static const char * leapsec
 
static const char * tzdefault
 
static const char * yitcommand
 
static int bloat
 

Macro Definition Documentation

◆ DC_DOM

#define DC_DOM   0 /* 1..31 */ /* unused */

Definition at line 94 of file zic.c.

Referenced by outzone(), rulesub(), stringrule(), and stringzone().

◆ DC_DOWGEQ

#define DC_DOWGEQ   1 /* 1..31 */ /* 0..6 (Sun..Sat) */

Definition at line 95 of file zic.c.

Referenced by rpytime(), rulesub(), and stringrule().

◆ DC_DOWLEQ

#define DC_DOWLEQ   2 /* 1..31 */ /* 0..6 (Sun..Sat) */

Definition at line 96 of file zic.c.

Referenced by rpytime(), rulesub(), and stringrule().

◆ DO

#define DO (   field)    fwrite(tzh.field, sizeof tzh.field, 1, fp)

Referenced by writezone().

◆ EXPIRES_FIELDS

#define EXPIRES_FIELDS   5

Definition at line 273 of file zic.c.

Referenced by inexpires().

◆ LC_EXPIRES

#define LC_EXPIRES   4

Definition at line 206 of file zic.c.

Referenced by infile().

◆ LC_LEAP

#define LC_LEAP   3

Definition at line 205 of file zic.c.

Referenced by infile().

◆ LC_LINK

#define LC_LINK   2

Definition at line 204 of file zic.c.

Referenced by infile().

◆ LC_RULE

#define LC_RULE   0

Definition at line 202 of file zic.c.

Referenced by infile().

◆ LC_ZONE

#define LC_ZONE   1

Definition at line 203 of file zic.c.

Referenced by infile().

◆ LDAYSPERWEEK

#define LDAYSPERWEEK   ((zic_t) DAYSPERWEEK)

Referenced by rpytime().

◆ LEAP_FIELDS

#define LEAP_FIELDS   7

Definition at line 270 of file zic.c.

Referenced by inleap().

◆ LF_FROM

#define LF_FROM   1

Definition at line 256 of file zic.c.

Referenced by inlink().

◆ LF_TO

#define LF_TO   2

Definition at line 257 of file zic.c.

Referenced by inlink().

◆ LINK_FIELDS

#define LINK_FIELDS   3

Definition at line 258 of file zic.c.

Referenced by inlink().

◆ linkat

#define linkat (   fromdir,
  from,
  todir,
  to,
  flag 
)    (itssymlink(from) ? (errno = ENOTSUP, -1) : link(from, to))

Definition at line 41 of file zic.c.

Referenced by hardlinkerr().

◆ LP_CORR

#define LP_CORR   5

Definition at line 268 of file zic.c.

Referenced by inleap().

◆ LP_DAY

#define LP_DAY   3

Definition at line 266 of file zic.c.

Referenced by getleapdatetime().

◆ LP_MONTH

#define LP_MONTH   2

Definition at line 265 of file zic.c.

Referenced by getleapdatetime().

◆ LP_ROLL

#define LP_ROLL   6

Definition at line 269 of file zic.c.

Referenced by inleap().

◆ LP_TIME

#define LP_TIME   4

Definition at line 267 of file zic.c.

Referenced by getleapdatetime().

◆ LP_YEAR

#define LP_YEAR   1

Definition at line 264 of file zic.c.

Referenced by getleapdatetime().

◆ MKDIR_UMASK

#define MKDIR_UMASK   0755

Definition at line 37 of file zic.c.

Referenced by mkdirs().

◆ RF_ABBRVAR

#define RF_ABBRVAR   9

Definition at line 249 of file zic.c.

Referenced by inrule().

◆ RF_COMMAND

#define RF_COMMAND   4

Definition at line 244 of file zic.c.

Referenced by inrule().

◆ RF_DAY

#define RF_DAY   6

Definition at line 246 of file zic.c.

Referenced by inrule().

◆ RF_HIYEAR

#define RF_HIYEAR   3

Definition at line 243 of file zic.c.

Referenced by inrule().

◆ RF_LOYEAR

#define RF_LOYEAR   2

Definition at line 242 of file zic.c.

Referenced by inrule().

◆ RF_MONTH

#define RF_MONTH   5

Definition at line 245 of file zic.c.

Referenced by inrule().

◆ RF_NAME

#define RF_NAME   1

Definition at line 241 of file zic.c.

Referenced by inrule().

◆ RF_SAVE

#define RF_SAVE   8

Definition at line 248 of file zic.c.

Referenced by inrule().

◆ RF_TOD

#define RF_TOD   7

Definition at line 247 of file zic.c.

Referenced by inrule().

◆ RULE_FIELDS

#define RULE_FIELDS   10

Definition at line 250 of file zic.c.

Referenced by inrule().

◆ TIME_T_BITS_IN_FILE

#define TIME_T_BITS_IN_FILE   64

Definition at line 582 of file zic.c.

◆ YR_MAXIMUM

#define YR_MAXIMUM   1

Definition at line 280 of file zic.c.

Referenced by rulesub().

◆ YR_MINIMUM

#define YR_MINIMUM   0

Definition at line 279 of file zic.c.

Referenced by rulesub().

◆ YR_ONLY

#define YR_ONLY   2

Definition at line 281 of file zic.c.

Referenced by rulesub().

◆ ZF_FORMAT

#define ZF_FORMAT   4

Definition at line 215 of file zic.c.

Referenced by inzsub().

◆ ZF_NAME

#define ZF_NAME   1

Definition at line 212 of file zic.c.

Referenced by inzone(), and inzsub().

◆ ZF_RULE

#define ZF_RULE   3

Definition at line 214 of file zic.c.

Referenced by inzsub().

◆ ZF_STDOFF

#define ZF_STDOFF   2

Definition at line 213 of file zic.c.

Referenced by inzsub().

◆ ZF_TILDAY

#define ZF_TILDAY   7

Definition at line 218 of file zic.c.

Referenced by inzsub().

◆ ZF_TILMONTH

#define ZF_TILMONTH   6

Definition at line 217 of file zic.c.

Referenced by inzsub().

◆ ZF_TILTIME

#define ZF_TILTIME   8

Definition at line 219 of file zic.c.

Referenced by inzsub().

◆ ZF_TILYEAR

#define ZF_TILYEAR   5

Definition at line 216 of file zic.c.

Referenced by inzsub().

◆ ZFC_FORMAT

#define ZFC_FORMAT   2

Definition at line 229 of file zic.c.

Referenced by inzsub().

◆ ZFC_RULE

#define ZFC_RULE   1

Definition at line 228 of file zic.c.

Referenced by inzsub().

◆ ZFC_STDOFF

#define ZFC_STDOFF   0

Definition at line 227 of file zic.c.

Referenced by inzsub().

◆ ZFC_TILDAY

#define ZFC_TILDAY   5

Definition at line 232 of file zic.c.

Referenced by inzsub().

◆ ZFC_TILMONTH

#define ZFC_TILMONTH   4

Definition at line 231 of file zic.c.

Referenced by inzsub().

◆ ZFC_TILTIME

#define ZFC_TILTIME   6

Definition at line 233 of file zic.c.

Referenced by inzsub().

◆ ZFC_TILYEAR

#define ZFC_TILYEAR   3

Definition at line 230 of file zic.c.

Referenced by inzsub().

◆ ZIC_BLOAT_DEFAULT

#define ZIC_BLOAT_DEFAULT   "fat"

Definition at line 651 of file zic.c.

Referenced by main().

◆ ZIC_MAX

#define ZIC_MAX   PG_INT64_MAX

Definition at line 27 of file zic.c.

Referenced by gethms(), limitrange(), oadd(), outzone(), rpytime(), rulesub(), stringzone(), and writezone().

◆ ZIC_MAX_ABBR_LEN_WO_WARN

#define ZIC_MAX_ABBR_LEN_WO_WARN   6

Definition at line 30 of file zic.c.

Referenced by newabbr().

◆ ZIC_MIN

#define ZIC_MIN   PG_INT64_MIN

Definition at line 26 of file zic.c.

Referenced by oadd(), outzone(), rpytime(), and rulesub().

◆ ZIC_VERSION

#define ZIC_VERSION   '3'

Definition at line 23 of file zic.c.

Referenced by outzone().

◆ ZIC_VERSION_PRE_2013

#define ZIC_VERSION_PRE_2013   '2'

Definition at line 22 of file zic.c.

Referenced by outzone().

◆ ZONE_MAXFIELDS

#define ZONE_MAXFIELDS   9

Definition at line 221 of file zic.c.

Referenced by inzone().

◆ ZONE_MINFIELDS

#define ZONE_MINFIELDS   5

Definition at line 220 of file zic.c.

◆ ZONEC_MAXFIELDS

#define ZONEC_MAXFIELDS   7

Definition at line 235 of file zic.c.

Referenced by inzcont().

◆ ZONEC_MINFIELDS

#define ZONEC_MINFIELDS   3

Definition at line 234 of file zic.c.

Typedef Documentation

◆ lineno_t

typedef int lineno_t

Definition at line 59 of file zic.c.

◆ zic_t

typedef int64 zic_t

Definition at line 25 of file zic.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
PERCENT_Z_LEN_BOUND 

Definition at line 160 of file zic.c.

161 {
162 PERCENT_Z_LEN_BOUND = sizeof "+995959" - 1};

◆ anonymous enum

anonymous enum
Enumerator
WORK_AROUND_QTBUG_53071 

Definition at line 170 of file zic.c.

Function Documentation

◆ abbroffset()

static char const* abbroffset ( char *  buf,
zic_t  offset 
)
static

Definition at line 2595 of file zic.c.

References _, buf, error(), MINSPERHOUR, SECSPERMIN, and sign.

Referenced by doabbr().

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 }
#define SECSPERMIN
Definition: private.h:101
char sign
Definition: informix.c:668
static char * buf
Definition: pg_test_fsync.c:67
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
#define _(x)
Definition: elog.c:88
#define MINSPERHOUR
Definition: private.h:102

◆ addtt()

static void addtt ( zic_t  starttime,
int  type 
)
static

Definition at line 3359 of file zic.c.

References attype::at, attypes, attype::dontmerge, growalloc(), timecnt, generate_unaccent_rules::type, and attype::type.

Referenced by outzone().

3360 {
3362  attypes[timecnt].at = starttime;
3363  attypes[timecnt].dontmerge = false;
3364  attypes[timecnt].type = type;
3365  ++timecnt;
3366 }
unsigned char type
Definition: zic.c:394
static void * growalloc(void *ptr, size_t itemsize, ptrdiff_t nitems, ptrdiff_t *nitems_alloc)
Definition: zic.c:452
zic_t at
Definition: zic.c:392
static ptrdiff_t timecnt_alloc
Definition: zic.c:195
static struct attype * attypes
static ptrdiff_t timecnt
Definition: zic.c:194
bool dontmerge
Definition: zic.c:393

◆ addtype()

static int addtype ( zic_t  utoff,
char const *  abbr,
bool  isdst,
bool  ttisstd,
bool  ttisut 
)
static

Definition at line 3369 of file zic.c.

References _, charcnt, error(), EXIT_FAILURE, i, newabbr(), typecnt, TZ_MAX_TYPES, and want_bloat().

Referenced by outzone(), and writezone().

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 }
static bool ttisstds[TZ_MAX_TYPES]
Definition: zic.c:399
static int typecnt
Definition: zic.c:196
static char isdsts[TZ_MAX_TYPES]
Definition: zic.c:397
static zic_t utoffs[TZ_MAX_TYPES]
Definition: zic.c:396
static int charcnt
Definition: zic.c:175
#define TZ_MAX_TYPES
Definition: tzfile.h:103
static void newabbr(const char *abbr)
Definition: zic.c:3969
int i
#define EXIT_FAILURE
Definition: settings.h:154
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
static char chars[TZ_MAX_CHARS]
Definition: zic.c:401
#define _(x)
Definition: elog.c:88
static bool want_bloat(void)
Definition: zic.c:645
static unsigned char desigidx[TZ_MAX_TYPES]
Definition: zic.c:398

◆ adjleap()

static void adjleap ( void  )
static

Definition at line 3436 of file zic.c.

References _, comment_leapexpires, error(), EXIT_FAILURE, i, leapcnt, oadd(), SECSPERDAY, tadd(), and warning().

Referenced by main().

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  {
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 }
static void static void static void warning(const char *string,...) pg_attribute_printf(1
Definition: zic.c:515
static zic_t corr[TZ_MAX_LEAPS]
Definition: zic.c:403
#define SECSPERDAY
Definition: private.h:108
static zic_t hi_time
Definition: zic.c:591
static zic_t tadd(zic_t t1, zic_t t2)
Definition: zic.c:3831
static zic_t comment_leapexpires
Definition: zic.c:597
static int leapcnt
Definition: zic.c:179
int64 zic_t
Definition: zic.c:25
static zic_t leapexpires
Definition: zic.c:594
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:402
int i
#define EXIT_FAILURE
Definition: settings.h:154
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
#define _(x)
Definition: elog.c:88
static zic_t oadd(zic_t t1, zic_t t2)
Definition: zic.c:3823

◆ associate()

static void associate ( void  )
static

Definition at line 1160 of file zic.c.

References _, eat(), error(), EXIT_FAILURE, getsave(), i, nrules, nzones, qsort, rule::r_filename, rule::r_linenum, rule::r_name, rcomp(), warning(), zone::z_filename, zone::z_format_specifier, zone::z_isdst, zone::z_linenum, zone::z_nrules, zone::z_rule, zone::z_rules, and zone::z_save.

Referenced by main().

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 }
lineno_t r_linenum
Definition: zic.c:64
static int rcomp(const void *cp1, const void *cp2)
Definition: zic.c:1153
zic_t z_save
Definition: zic.c:110
static void static void static void warning(const char *string,...) pg_attribute_printf(1
Definition: zic.c:515
static bool errors
Definition: zic.c:176
static ptrdiff_t nzones
Definition: zic.c:288
bool z_isdst
Definition: zic.c:109
Definition: localtime.c:72
char z_format_specifier
Definition: zic.c:107
char * z_rule
Definition: zic.c:105
const char * r_name
Definition: zic.c:65
const char * z_filename
Definition: zic.c:100
static struct rule * rules
Definition: zic.c:283
const char * r_filename
Definition: zic.c:63
static void eat(char const *name, lineno_t num)
Definition: zic.c:482
ptrdiff_t z_nrules
Definition: zic.c:113
lineno_t z_linenum
Definition: zic.c:101
static zic_t getsave(char *, bool *)
Definition: zic.c:1445
static ptrdiff_t nrules
Definition: zic.c:284
static struct zone * zones
Definition: zic.c:287
struct rule * z_rules
Definition: zic.c:112
Definition: zic.c:98
int i
#define EXIT_FAILURE
Definition: settings.h:154
#define qsort(a, b, c, d)
Definition: port.h:479
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
#define _(x)
Definition: elog.c:88

◆ atcomp()

static int atcomp ( const void *  avp,
const void *  bvp 
)
static

Definition at line 2046 of file zic.c.

Referenced by writezone().

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 }
Definition: zic.c:390
int64 zic_t
Definition: zic.c:25

◆ byword()

static const struct lookup * byword ( const char *  string,
const struct lookup lp 
)
static

Definition at line 3710 of file zic.c.

References _, ciequal(), ciprefix(), itsabbr(), lookup::l_word, warning(), and wday_names.

Referenced by getleapdatetime(), infile(), inleap(), and rulesub().

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 }
static bool ciprefix(char const *abbr, char const *word)
Definition: zic.c:3699
static void static void static void warning(const char *string,...) pg_attribute_printf(1
Definition: zic.c:515
Definition: zic.c:303
static bool ciequal(const char *ap, const char *bp)
Definition: zic.c:3673
const char * l_word
Definition: zic.c:305
static bool noise
Definition: zic.c:188
static void word(struct vars *, int, struct state *, struct state *)
Definition: regcomp.c:1246
static struct lookup const lasts[]
Definition: zic.c:351
static struct lookup const wday_names[]
Definition: zic.c:340
#define _(x)
Definition: elog.c:88
static bool itsabbr(const char *abbr, const char *word)
Definition: zic.c:3682

◆ change_directory()

static void change_directory ( char const *  dir)
static

Definition at line 562 of file zic.c.

References _, EXIT_FAILURE, fprintf, mkdirs(), and strerror.

Referenced by main().

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 }
#define fprintf
Definition: port.h:197
static void mkdirs(char const *, bool)
Definition: zic.c:4007
static const char * progname
Definition: zic.c:193
#define strerror
Definition: port.h:206
#define EXIT_FAILURE
Definition: settings.h:154
#define _(x)
Definition: elog.c:88

◆ ciequal()

static bool ciequal ( const char *  ap,
const char *  bp 
)
static

Definition at line 3673 of file zic.c.

References lowerit().

Referenced by byword().

3674 {
3675  while (lowerit(*ap) == lowerit(*bp++))
3676  if (*ap++ == '\0')
3677  return true;
3678  return false;
3679 }
static char lowerit(char)
Definition: zic.c:3610

◆ ciprefix()

static bool ciprefix ( char const *  abbr,
char const *  word 
)
static

Definition at line 3699 of file zic.c.

References lowerit().

Referenced by byword().

3700 {
3701  do
3702  if (!*abbr)
3703  return true;
3704  while (lowerit(*abbr++) == lowerit(*word++));
3705 
3706  return false;
3707 }
static char lowerit(char)
Definition: zic.c:3610
static void word(struct vars *, int, struct state *, struct state *)
Definition: regcomp.c:1246

◆ close_file()

static void close_file ( FILE *  stream,
char const *  dir,
char const *  name 
)
static

Definition at line 527 of file zic.c.

References _, EXIT_FAILURE, fprintf, and strerror.

Referenced by dolink(), infile(), main(), usage(), and writezone().

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 }
#define fprintf
Definition: port.h:197
static const char * progname
Definition: zic.c:193
#define strerror
Definition: port.h:206
const char * name
Definition: encode.c:561
e
Definition: preproc-init.c:82
#define EXIT_FAILURE
Definition: settings.h:154
#define _(x)
Definition: elog.c:88

◆ componentcheck()

static bool componentcheck ( char const *  name,
char const *  component,
char const *  component_end 
)
static

Definition at line 864 of file zic.c.

References _, error(), and warning().

Referenced by namecheck().

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 }
static void static void static void warning(const char *string,...) pg_attribute_printf(1
Definition: zic.c:515
static bool noise
Definition: zic.c:188
const char * name
Definition: encode.c:561
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
#define _(x)
Definition: elog.c:88

◆ convert()

static void convert ( const int32  val,
char *const  buf 
)
static

Definition at line 2001 of file zic.c.

References i.

Referenced by ExecEvalConvertRowtype(), ExecInitExprRec(), fmtfloat(), fmtint(), fmtptr(), get_rule_expr(), pg_strfromd(), print_double(), puttzcode(), and writezone().

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 }
static char * buf
Definition: pg_test_fsync.c:67
int i
long val
Definition: informix.c:664

◆ convert64()

static void convert64 ( const zic_t  val,
char *const  buf 
)
static

Definition at line 2012 of file zic.c.

References i.

Referenced by puttzcodepass().

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 }
static char * buf
Definition: pg_test_fsync.c:67
int i
long val
Definition: informix.c:664

◆ doabbr()

static size_t doabbr ( char *  abbr,
struct zone const *  zp,
char const *  letters,
bool  isdst,
zic_t  save,
bool  doquotes 
)
static

Definition at line 2639 of file zic.c.

References abbroffset(), format, is_alpha(), PERCENT_Z_LEN_BOUND, sprintf, zone::z_format, zone::z_format_specifier, and zone::z_stdoff.

Referenced by outzone(), and stringzone().

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 }
static char const * abbroffset(char *buf, zic_t offset)
Definition: zic.c:2595
#define sprintf
Definition: port.h:195
static bool is_alpha(char a)
Definition: zic.c:3545
static char format

◆ dolink()

static void dolink ( const char *  fromfield,
const char *  tofield,
bool  staysymlink 
)
static

Definition at line 1009 of file zic.c.

References _, close_file(), ENOTSUP, EXIT_FAILURE, fprintf, free, hardlinkerr(), itsdir(), itssymlink(), mkdirs(), relname, strerror, symlink, and warning().

Referenced by main().

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 }
static int hardlinkerr(char const *from, char const *to)
Definition: zic.c:1001
static void static void static void warning(const char *string,...) pg_attribute_printf(1
Definition: zic.c:515
#define ENOTSUP
Definition: private.h:38
NameData relname
Definition: pg_class.h:38
#define fprintf
Definition: port.h:197
static void mkdirs(char const *, bool)
Definition: zic.c:4007
static bool itsdir(char const *)
Definition: zic.c:1108
char * c
#define symlink(oldpath, newpath)
Definition: win32_port.h:221
static bool itssymlink(char const *)
Definition: zic.c:1133
static const char * progname
Definition: zic.c:193
#define free(a)
Definition: header.h:65
#define strerror
Definition: port.h:206
static const char * directory
Definition: zic.c:634
static void close_file(FILE *stream, char const *dir, char const *name)
Definition: zic.c:527
e
Definition: preproc-init.c:82
#define EXIT_FAILURE
Definition: settings.h:154
#define _(x)
Definition: elog.c:88

◆ eat()

static void eat ( char const *  name,
lineno_t  num 
)
static

Definition at line 482 of file zic.c.

References eats().

Referenced by associate(), infile(), main(), and outzone().

483 {
484  eats(name, num, NULL, -1);
485 }
const char * name
Definition: encode.c:561
static void eats(char const *name, lineno_t num, char const *rname, lineno_t rnum)
Definition: zic.c:473

◆ eats()

static void eats ( char const *  name,
lineno_t  num,
char const *  rname,
lineno_t  rnum 
)
static

Definition at line 473 of file zic.c.

References name.

Referenced by eat(), and outzone().

474 {
475  filename = name;
476  linenum = num;
477  rfilename = rname;
478  rlinenum = rnum;
479 }
static lineno_t rlinenum
Definition: zic.c:192
static const char * filename
Definition: zic.c:178
static lineno_t linenum
Definition: zic.c:183
static const char * rfilename
Definition: zic.c:191
const char * name
Definition: encode.c:561

◆ ecpyalloc()

static char* ecpyalloc ( char const *  str)
static

Definition at line 446 of file zic.c.

References memcheck().

Referenced by inlink(), inrule(), inzsub(), mkdirs(), and rulesub().

447 {
448  return memcheck(strdup(str));
449 }
static void * memcheck(void *ptr)
Definition: zic.c:426

◆ emalloc()

static void* emalloc ( size_t  size)
static

Definition at line 434 of file zic.c.

References malloc, and memcheck().

Referenced by getfields(), itsdir(), namecheck(), outzone(), writezone(), and yearistype().

435 {
436  return memcheck(malloc(size));
437 }
#define malloc(a)
Definition: header.h:50
static void * memcheck(void *ptr)
Definition: zic.c:426

◆ erealloc()

static void* erealloc ( void *  ptr,
size_t  size 
)
static

Definition at line 440 of file zic.c.

References memcheck(), and realloc.

Referenced by growalloc().

441 {
442  return memcheck(realloc(ptr, size));
443 }
#define realloc(a, b)
Definition: header.h:60
static void * memcheck(void *ptr)
Definition: zic.c:426

◆ error()

static void error ( const char *  string,
  ... 
)
static

Definition at line 504 of file zic.c.

References generate_unaccent_rules::args, and verror().

Referenced by abbroffset(), addtype(), adjleap(), associate(), componentcheck(), getfields(), gethms(), getleapdatetime(), inexpires(), infile(), inleap(), inlink(), inrule(), inzcont(), inzone(), inzsub(), leapadd(), main(), mkdirs(), newabbr(), outzone(), rpytime(), rulesub(), time_overflow(), writezone(), and yearistype().

505 {
506  va_list args;
507 
508  va_start(args, string);
509  verror(string, args);
510  va_end(args);
511  errors = true;
512 }
static bool errors
Definition: zic.c:176
static void verror(const char *string, va_list args) pg_attribute_printf(1
Definition: zic.c:488

◆ getfields()

static char ** getfields ( char *  buf)
static

Definition at line 3776 of file zic.c.

References _, emalloc(), error(), EXIT_FAILURE, is_space(), and size_product().

Referenced by infile().

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 }
static bool is_space(char a)
Definition: zic.c:3527
#define EXIT_FAILURE
Definition: settings.h:154
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
#define _(x)
Definition: elog.c:88
static size_t size_product(size_t nitems, size_t itemsize)
Definition: zic.c:418
static void * emalloc(size_t size)
Definition: zic.c:434

◆ gethms()

static zic_t gethms ( const char *  string,
const char *  errstring 
)
static

Definition at line 1367 of file zic.c.

References _, error(), HOURSPERDAY, MINSPERHOUR, oadd(), SECSPERHOUR, SECSPERMIN, sign, warning(), and ZIC_MAX.

Referenced by getleapdatetime(), getsave(), inzsub(), and rulesub().

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 }
#define SECSPERMIN
Definition: private.h:101
static void static void static void warning(const char *string,...) pg_attribute_printf(1
Definition: zic.c:515
#define SECSPERHOUR
Definition: private.h:107
#define HOURSPERDAY
Definition: private.h:103
#define ZIC_MAX
Definition: zic.c:27
char sign
Definition: informix.c:668
char string[11]
Definition: preproc-type.c:46
int64 zic_t
Definition: zic.c:25
static bool noise
Definition: zic.c:188
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
#define _(x)
Definition: elog.c:88
static zic_t oadd(zic_t t1, zic_t t2)
Definition: zic.c:3823
#define MINSPERHOUR
Definition: private.h:102

◆ getleapdatetime()

static zic_t getleapdatetime ( char **  fields,
int  nfields,
bool  expire_line 
)
static

Definition at line 1668 of file zic.c.

References _, byword(), EPOCH_YEAR, error(), gethms(), i, isleap, lookup::l_value, LP_DAY, LP_MONTH, LP_TIME, LP_YEAR, oadd(), SECSPERDAY, tadd(), and TM_JANUARY.

Referenced by inexpires(), and inleap().

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 }
static zic_t leapmaxyear
Definition: zic.c:182
#define SECSPERDAY
Definition: private.h:108
#define isleap(y)
Definition: datetime.h:271
static const int len_months[2][MONSPERYEAR]
Definition: zic.c:381
const int l_value
Definition: zic.c:306
static zic_t tadd(zic_t t1, zic_t t2)
Definition: zic.c:3831
static zic_t gethms(const char *string, const char *errstring)
Definition: zic.c:1367
static struct lookup const mon_names[]
Definition: zic.c:324
#define LP_DAY
Definition: zic.c:266
static bool leapseen
Definition: zic.c:180
int64 zic_t
Definition: zic.c:25
Definition: zic.c:303
#define EPOCH_YEAR
Definition: private.h:134
#define LP_TIME
Definition: zic.c:267
static zic_t const max_time
Definition: zic.c:586
#define TM_JANUARY
Definition: private.h:119
static struct lookup const * byword(const char *string, const struct lookup *lp)
Definition: zic.c:3710
static zic_t leapminyear
Definition: zic.c:181
#define LP_YEAR
Definition: zic.c:264
int i
static const int len_years[2]
Definition: zic.c:386
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
static zic_t const min_time
Definition: zic.c:585
#define LP_MONTH
Definition: zic.c:265
#define _(x)
Definition: elog.c:88
static zic_t oadd(zic_t t1, zic_t t2)
Definition: zic.c:3823

◆ getsave()

static zic_t getsave ( char *  field,
bool isdst 
)
static

Definition at line 1445 of file zic.c.

References _, and gethms().

Referenced by associate(), and inrule().

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 }
static zic_t gethms(const char *string, const char *errstring)
Definition: zic.c:1367
int64 zic_t
Definition: zic.c:25
#define _(x)
Definition: elog.c:88

◆ growalloc()

static void* growalloc ( void *  ptr,
size_t  itemsize,
ptrdiff_t  nitems,
ptrdiff_t *  nitems_alloc 
)
static

Definition at line 452 of file zic.c.

References _, erealloc(), memory_exhausted(), PTRDIFF_MAX, size_product(), and WORK_AROUND_QTBUG_53071.

Referenced by addtt(), inlink(), inrule(), and inzsub().

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 }
static ptrdiff_t const PTRDIFF_MAX
Definition: zic.c:51
static void memory_exhausted(const char *msg) pg_attribute_noreturn()
Definition: zic.c:411
#define _(x)
Definition: elog.c:88
static size_t size_product(size_t nitems, size_t itemsize)
Definition: zic.c:418
static void * erealloc(void *ptr, size_t size)
Definition: zic.c:440

◆ hardlinkerr()

static int hardlinkerr ( char const *  from,
char const *  to 
)
static

Definition at line 1001 of file zic.c.

References linkat.

Referenced by dolink().

1002 {
1003  int r = linkat(AT_FDCWD, from, AT_FDCWD, to, AT_SYMLINK_FOLLOW);
1004 
1005  return r == 0 ? 0 : errno;
1006 }
#define linkat(fromdir, from, todir, to, flag)
Definition: zic.c:41

◆ inexpires()

static void inexpires ( char **  fields,
int  nfields 
)
static

Definition at line 1789 of file zic.c.

References _, error(), EXPIRES_FIELDS, and getleapdatetime().

Referenced by infile().

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 }
static zic_t getleapdatetime(char **fields, int nfields, bool expire_line)
Definition: zic.c:1668
static zic_t leapexpires
Definition: zic.c:594
#define EXPIRES_FIELDS
Definition: zic.c:273
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
#define _(x)
Definition: elog.c:88

◆ infile()

static void infile ( const char *  filename)
static

Definition at line 1245 of file zic.c.

References _, buf, byword(), close_file(), eat(), error(), EXIT_FAILURE, fprintf, free, getfields(), inexpires(), inleap(), inlink(), inrule(), inzcont(), inzone(), lookup::l_value, LC_EXPIRES, LC_LEAP, LC_LINK, LC_RULE, LC_ZONE, leap_line_codes, strerror, and zi_line_codes.

Referenced by AlterSystemSetConfigFile(), convert_sourcefiles_in(), isolation_start_test(), main(), psql_start_test(), and readfile().

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
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 }
static void inexpires(char **, int)
Definition: zic.c:1789
static struct lookup const zi_line_codes[]
Definition: zic.c:312
static struct lookup const leap_line_codes[]
Definition: zic.c:318
static void inrule(char **fields, int nfields)
Definition: zic.c:1473
#define LC_LEAP
Definition: zic.c:205
static const char * leapsec
Definition: zic.c:635
#define fprintf
Definition: port.h:197
const int l_value
Definition: zic.c:306
static const char * filename
Definition: zic.c:178
static void inleap(char **fields, int nfields)
Definition: zic.c:1757
static char ** getfields(char *buf)
Definition: zic.c:3776
static zic_t comment_leapexpires
Definition: zic.c:597
static bool inzcont(char **fields, int nfields)
Definition: zic.c:1558
static char * buf
Definition: pg_test_fsync.c:67
Definition: zic.c:303
#define LC_LINK
Definition: zic.c:204
static void eat(char const *name, lineno_t num)
Definition: zic.c:482
#define LC_ZONE
Definition: zic.c:203
static const char * progname
Definition: zic.c:193
#define free(a)
Definition: header.h:65
static bool inzone(char **fields, int nfields)
Definition: zic.c:1520
#define LC_EXPIRES
Definition: zic.c:206
static void inlink(char **fields, int nfields)
Definition: zic.c:1800
#define strerror
Definition: port.h:206
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
const char * name
Definition: encode.c:561
e
Definition: preproc-init.c:82
#define EXIT_FAILURE
Definition: settings.h:154
int lineno_t
Definition: zic.c:59
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
#define LC_RULE
Definition: zic.c:202
#define _(x)
Definition: elog.c:88

◆ inleap()

static void inleap ( char **  fields,
int  nfields 
)
static

Definition at line 1757 of file zic.c.

References _, byword(), error(), getleapdatetime(), lookup::l_value, LEAP_FIELDS, leapadd(), LP_CORR, and LP_ROLL.

Referenced by infile().

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 }
#define LP_CORR
Definition: zic.c:268
const int l_value
Definition: zic.c:306
#define LP_ROLL
Definition: zic.c:269
static void leapadd(zic_t, int, int)
Definition: zic.c:3414
#define LEAP_FIELDS
Definition: zic.c:270
int64 zic_t
Definition: zic.c:25
Definition: zic.c:303
static zic_t getleapdatetime(char **fields, int nfields, bool expire_line)
Definition: zic.c:1668
static struct lookup const * byword(const char *string, const struct lookup *lp)
Definition: zic.c:3710
static struct lookup const leap_types[]
Definition: zic.c:375
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
#define _(x)
Definition: elog.c:88

◆ inlink()

static void inlink ( char **  fields,
int  nfields 
)
static

Definition at line 1800 of file zic.c.

References _, ecpyalloc(), error(), filename, growalloc(), link::l_filename, link::l_from, link::l_linenum, link::l_to, LF_FROM, LF_TO, linenum, LINK_FIELDS, and namecheck().

Referenced by infile().

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 }
static ptrdiff_t nlinks
Definition: zic.c:300
static char * ecpyalloc(char const *str)
Definition: zic.c:446
#define LF_TO
Definition: zic.c:257
static const char * filename
Definition: zic.c:178
static lineno_t linenum
Definition: zic.c:183
static void * growalloc(void *ptr, size_t itemsize, ptrdiff_t nitems, ptrdiff_t *nitems_alloc)
Definition: zic.c:452
static ptrdiff_t nlinks_alloc
Definition: zic.c:301
#define LF_FROM
Definition: zic.c:256
static struct link * links
Definition: zic.c:299
#define LINK_FIELDS
Definition: zic.c:258
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
#define _(x)
Definition: elog.c:88
static bool namecheck(const char *name)
Definition: zic.c:908

◆ inrule()

static void inrule ( char **  fields,
int  nfields 
)
static

Definition at line 1473 of file zic.c.

References _, ecpyalloc(), error(), filename, getsave(), growalloc(), linenum, rule::r_abbrvar, rule::r_filename, rule::r_isdst, rule::r_linenum, rule::r_name, rule::r_save, RF_ABBRVAR, RF_COMMAND, RF_DAY, RF_HIYEAR, RF_LOYEAR, RF_MONTH, RF_NAME, RF_SAVE, RF_TOD, RULE_FIELDS, and rulesub().

Referenced by infile().

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 }
static char * ecpyalloc(char const *str)
Definition: zic.c:446
#define RF_COMMAND
Definition: zic.c:244
Definition: localtime.c:72
static const char * filename
Definition: zic.c:178
static lineno_t linenum
Definition: zic.c:183
static int max_abbrvar_len
Definition: zic.c:184
static struct rule * rules
Definition: zic.c:283
static void * growalloc(void *ptr, size_t itemsize, ptrdiff_t nitems, ptrdiff_t *nitems_alloc)
Definition: zic.c:452
#define RF_SAVE
Definition: zic.c:248
#define RF_HIYEAR
Definition: zic.c:243
#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 zic_t getsave(char *, bool *)
Definition: zic.c:1445
static ptrdiff_t nrules
Definition: zic.c:284
#define RF_TOD
Definition: zic.c:247
#define RF_ABBRVAR
Definition: zic.c:249
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
#define RF_LOYEAR
Definition: zic.c:242
#define _(x)
Definition: elog.c:88
#define RULE_FIELDS
Definition: zic.c:250
#define RF_MONTH
Definition: zic.c:245
#define RF_DAY
Definition: zic.c:246

◆ inzcont()

static bool inzcont ( char **  fields,
int  nfields 
)
static

Definition at line 1558 of file zic.c.

References _, error(), inzsub(), and ZONEC_MAXFIELDS.

Referenced by infile().

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 }
#define ZONEC_MAXFIELDS
Definition: zic.c:235
static bool inzsub(char **, int, bool)
Definition: zic.c:1569
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
#define _(x)
Definition: elog.c:88

◆ inzone()

static bool inzone ( char **  fields,
int  nfields 
)
static

Definition at line 1520 of file zic.c.

References _, error(), i, inzsub(), nzones, TZDEFRULES, ZF_NAME, and ZONE_MAXFIELDS.

Referenced by infile().

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 }
#define ZF_NAME
Definition: zic.c:212
static ptrdiff_t nzones
Definition: zic.c:288
static const char * psxrules
Definition: zic.c:632
static bool inzsub(char **, int, bool)
Definition: zic.c:1569
#define ZONE_MAXFIELDS
Definition: zic.c:221
static const char * lcltime
Definition: zic.c:633
#define TZDEFRULES
Definition: tzfile.h:28
static struct zone * zones
Definition: zic.c:287
int i
static const char * tzdefault
Definition: zic.c:636
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
#define _(x)
Definition: elog.c:88

◆ inzsub()

static bool inzsub ( char **  fields,
int  nfields,
bool  iscont 
)
static

Definition at line 1569 of file zic.c.

References _, ecpyalloc(), error(), filename, gethms(), growalloc(), linenum, namecheck(), rule::r_filename, rule::r_linenum, rule::r_loyear, rpytime(), rulesub(), warning(), zone::z_filename, zone::z_format, zone::z_format_specifier, zone::z_linenum, zone::z_name, zone::z_rule, zone::z_stdoff, zone::z_untilrule, zone::z_untiltime, ZF_FORMAT, ZF_NAME, ZF_RULE, ZF_STDOFF, ZF_TILDAY, ZF_TILMONTH, ZF_TILTIME, ZF_TILYEAR, ZFC_FORMAT, ZFC_RULE, ZFC_STDOFF, ZFC_TILDAY, ZFC_TILMONTH, ZFC_TILTIME, and ZFC_TILYEAR.

Referenced by inzcont(), and inzone().

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  {
1634  z.z_untilrule.r_filename = filename;
1635  z.z_untilrule.r_linenum = linenum;
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");
1644  z.z_untiltime = rpytime(&z.z_untilrule,
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 }
#define ZF_TILMONTH
Definition: zic.c:217
#define ZF_TILTIME
Definition: zic.c:219
#define ZF_TILYEAR
Definition: zic.c:216
#define ZF_NAME
Definition: zic.c:212
#define ZFC_TILMONTH
Definition: zic.c:231
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 ZF_STDOFF
Definition: zic.c:213
zic_t z_untiltime
Definition: zic.c:116
static ptrdiff_t nzones
Definition: zic.c:288
#define ZFC_FORMAT
Definition: zic.c:229
static int max_format_len
Definition: zic.c:185
static const char * filename
Definition: zic.c:178
#define ZF_FORMAT
Definition: zic.c:215
#define ZFC_STDOFF
Definition: zic.c:227
static zic_t gethms(const char *string, const char *errstring)
Definition: zic.c:1367
static lineno_t linenum
Definition: zic.c:183
static ptrdiff_t nzones_alloc
Definition: zic.c:289
#define ZFC_TILTIME
Definition: zic.c:233
static void * growalloc(void *ptr, size_t itemsize, ptrdiff_t nitems, ptrdiff_t *nitems_alloc)
Definition: zic.c:452
#define ZFC_RULE
Definition: zic.c:228
static zic_t rpytime(const struct rule *rp, zic_t wantedy)
Definition: zic.c:3860
#define ZF_TILDAY
Definition: zic.c:218
static zic_t const max_time
Definition: zic.c:586
static bool noise
Definition: zic.c:188
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 struct zone * zones
Definition: zic.c:287
Definition: zic.c:98
#define ZFC_TILDAY
Definition: zic.c:232
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
static zic_t const min_time
Definition: zic.c:585
#define ZFC_TILYEAR
Definition: zic.c:230
#define _(x)
Definition: elog.c:88
static bool namecheck(const char *name)
Definition: zic.c:908

◆ is_alpha()

static bool is_alpha ( char  a)
static

Definition at line 3545 of file zic.c.

Referenced by doabbr(), and newabbr().

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 }

◆ is_space()

static bool is_space ( char  a)
static

Definition at line 3527 of file zic.c.

Referenced by getfields().

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 }

◆ itsabbr()

static bool itsabbr ( const char *  abbr,
const char *  word 
)
static

Definition at line 3682 of file zic.c.

References lowerit(), and word().

Referenced by byword().

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 }
static char lowerit(char)
Definition: zic.c:3610
static void word(struct vars *, int, struct state *, struct state *)
Definition: regcomp.c:1246

◆ itsdir()

static bool itsdir ( char const *  name)
static

Definition at line 1108 of file zic.c.

References emalloc(), EOVERFLOW, free, S_ISDIR, and stat.

Referenced by dolink(), and mkdirs().

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 }
#define stat(a, b)
Definition: win32_port.h:255
#define free(a)
Definition: header.h:65
const char * name
Definition: encode.c:561
#define S_ISDIR(m)
Definition: win32_port.h:296
#define EOVERFLOW
Definition: private.h:41
static void * emalloc(size_t size)
Definition: zic.c:434

◆ itssymlink()

static bool itssymlink ( char const *  name)
static

Definition at line 1133 of file zic.c.

References readlink.

Referenced by dolink().

1134 {
1135 #ifdef HAVE_SYMLINK
1136  char c;
1137 
1138  return 0 <= readlink(name, &c, 1);
1139 #else
1140  return false;
1141 #endif
1142 }
char * c
#define readlink(path, buf, size)
Definition: win32_port.h:222
const char * name
Definition: encode.c:561

◆ leapadd()

static void leapadd ( zic_t  t,
int  correction,
int  rolling 
)
static

Definition at line 3414 of file zic.c.

References _, error(), EXIT_FAILURE, i, leapcnt, and TZ_MAX_LEAPS.

Referenced by inleap().

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 }
static zic_t corr[TZ_MAX_LEAPS]
Definition: zic.c:403
#define TZ_MAX_LEAPS
Definition: tzfile.h:108
static int leapcnt
Definition: zic.c:179
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:402
int i
#define EXIT_FAILURE
Definition: settings.h:154
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
#define _(x)
Definition: elog.c:88
static char roll[TZ_MAX_LEAPS]
Definition: zic.c:404

◆ limitrange()

static struct timerange limitrange ( struct timerange  r,
zic_t  lo,
zic_t  hi,
zic_t const *  ats,
unsigned char const *  types 
)
static

Definition at line 2064 of file zic.c.

References timerange::defaulttype, types, and ZIC_MAX.

Referenced by writezone().

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 }
struct typedefs * types
Definition: ecpg.c:29
ptrdiff_t count
Definition: zic.c:2057
int leapbase
Definition: zic.c:2059
#define ZIC_MAX
Definition: zic.c:27
int defaulttype
Definition: zic.c:2056
int leapcount
Definition: zic.c:2059
ptrdiff_t base
Definition: zic.c:2057
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:402

◆ link()

int link ( const char *  fromname,
const char *  toname 
)

Referenced by mkdirs().

◆ lowerit()

static char lowerit ( char  a)
static

Definition at line 3610 of file zic.c.

Referenced by ciequal(), ciprefix(), itsabbr(), and rulesub().

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 }

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 655 of file zic.c.

References _, adjleap(), associate(), change_directory(), close_file(), dolink(), eat(), error(), EXIT_FAILURE, EXIT_SUCCESS, fprintf, getopt(), i, infile(), nlinks, nzones, optarg, optind, outzone(), printf, S_IWGRP, S_IWOTH, generate_unaccent_rules::stdout, timerange_option(), TYPE_BIT, TZDEFAULT, TZDEFRULES, usage(), warning(), zone::z_name, and ZIC_BLOAT_DEFAULT.

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)
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();
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 }
#define TZDEFAULT
Definition: tzfile.h:27
static ptrdiff_t nlinks
Definition: zic.c:300
static bool print_abbrevs
Definition: zic.c:189
static zic_t print_cutoff
Definition: zic.c:190
static void change_directory(char const *dir)
Definition: zic.c:562
#define EXIT_SUCCESS
Definition: settings.h:150
static int bloat
Definition: zic.c:642
static void static void static void warning(const char *string,...) pg_attribute_printf(1
Definition: zic.c:515
static bool errors
Definition: zic.c:176
static void associate(void)
Definition: zic.c:1160
#define S_IWOTH
Definition: win32_port.h:287
#define printf(...)
Definition: port.h:199
#define ZIC_BLOAT_DEFAULT
Definition: zic.c:651
static ptrdiff_t nzones
Definition: zic.c:288
static const char * yitcommand
Definition: zic.c:637
static bool warnings
Definition: zic.c:177
static const char * leapsec
Definition: zic.c:635
#define fprintf
Definition: port.h:197
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition: getopt.c:71
static const char * psxrules
Definition: zic.c:632
static void static void static void static void usage(FILE *stream, int status) pg_attribute_noreturn()
Definition: zic.c:543
static void dolink(const char *, const char *, bool)
Definition: zic.c:1009
int optind
Definition: getopt.c:50
char * c
static void adjleap(void)
Definition: zic.c:3436
static const char * lcltime
Definition: zic.c:633
int64 zic_t
Definition: zic.c:25
#define S_IWGRP
Definition: win32_port.h:275
static void eat(char const *name, lineno_t num)
Definition: zic.c:482
static bool noise
Definition: zic.c:188
static struct link * links
Definition: zic.c:299
static void infile(const char *filename)
Definition: zic.c:1245
static const char * progname
Definition: zic.c:193
#define TZDEFRULES
Definition: tzfile.h:28
static const char * directory
Definition: zic.c:634
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 zone * zones
Definition: zic.c:287
static void outzone(const struct zone *zp, ptrdiff_t ntzones)
Definition: zic.c:2957
char * optarg
Definition: getopt.c:52
#define TYPE_BIT(type)
Definition: private.h:60
int i
#define EXIT_FAILURE
Definition: settings.h:154
static const char * tzdefault
Definition: zic.c:636
static bool timerange_option(char *timerange)
Definition: zic.c:602
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
#define _(x)
Definition: elog.c:88

◆ memcheck()

static void* memcheck ( void *  ptr)
static

Definition at line 426 of file zic.c.

References memory_exhausted(), and strerror.

Referenced by ecpyalloc(), emalloc(), and erealloc().

427 {
428  if (ptr == NULL)
429  memory_exhausted(strerror(errno));
430  return ptr;
431 }
#define strerror
Definition: port.h:206
static void memory_exhausted(const char *msg) pg_attribute_noreturn()
Definition: zic.c:411

◆ memory_exhausted()

static void memory_exhausted ( const char *  msg)
static

Definition at line 411 of file zic.c.

References _, EXIT_FAILURE, and fprintf.

Referenced by growalloc(), memcheck(), and size_product().

412 {
413  fprintf(stderr, _("%s: Memory exhausted: %s\n"), progname, msg);
414  exit(EXIT_FAILURE);
415 }
#define fprintf
Definition: port.h:197
static const char * progname
Definition: zic.c:193
#define EXIT_FAILURE
Definition: settings.h:154
#define _(x)
Definition: elog.c:88

◆ mkdirs()

static void mkdirs ( char const *  argname,
bool  ancestors 
)
static

Definition at line 4007 of file zic.c.

References _, _dosmaperr(), ecpyalloc(), error(), EXIT_FAILURE, free, itsdir(), link(), mkdir, MKDIR_UMASK, name, and strerror.

Referenced by change_directory(), dolink(), and writezone().

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 }
static char * ecpyalloc(char const *str)
Definition: zic.c:446
static bool itsdir(char const *)
Definition: zic.c:1108
static const char * progname
Definition: zic.c:193
#define free(a)
Definition: header.h:65
#define MKDIR_UMASK
Definition: zic.c:37
#define strerror
Definition: port.h:206
const char * name
Definition: encode.c:561
#define EXIT_FAILURE
Definition: settings.h:154
#define mkdir(a, b)
Definition: win32_port.h:57
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
#define _(x)
Definition: elog.c:88

◆ namecheck()

static bool namecheck ( const char *  name)
static

Definition at line 908 of file zic.c.

References _, componentcheck(), emalloc(), i, name, relname, and warning().

Referenced by inlink(), and inzsub().

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 }
static void static void static void warning(const char *string,...) pg_attribute_printf(1
Definition: zic.c:515
static bool componentcheck(char const *name, char const *component, char const *component_end)
Definition: zic.c:864
char * c
static bool noise
Definition: zic.c:188
const char * name
Definition: encode.c:561
#define _(x)
Definition: elog.c:88

◆ newabbr()

static void newabbr ( const char *  abbr)
static

Definition at line 3969 of file zic.c.

References _, error(), EXIT_FAILURE, GRANDPARENTED, i, is_alpha(), TZ_MAX_CHARS, warning(), and ZIC_MAX_ABBR_LEN_WO_WARN.

Referenced by addtype().

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 }
static void static void static void warning(const char *string,...) pg_attribute_printf(1
Definition: zic.c:515
#define GRANDPARENTED
Definition: private.h:30
static bool is_alpha(char a)
Definition: zic.c:3545
char string[11]
Definition: preproc-type.c:46
#define ZIC_MAX_ABBR_LEN_WO_WARN
Definition: zic.c:30
#define TZ_MAX_CHARS
Definition: tzfile.h:105
static bool noise
Definition: zic.c:188
static int charcnt
Definition: zic.c:175
int i
#define EXIT_FAILURE
Definition: settings.h:154
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
static char chars[TZ_MAX_CHARS]
Definition: zic.c:401
#define _(x)
Definition: elog.c:88

◆ oadd()

static zic_t oadd ( zic_t  t1,
zic_t  t2 
)
static

Definition at line 3823 of file zic.c.

References time_overflow(), ZIC_MAX, and ZIC_MIN.

Referenced by adjleap(), gethms(), getleapdatetime(), outzone(), and rpytime().

3824 {
3825  if (t1 < 0 ? t2 < ZIC_MIN - t1 : ZIC_MAX - t1 < t2)
3826  time_overflow();
3827  return t1 + t2;
3828 }
#define ZIC_MAX
Definition: zic.c:27
#define ZIC_MIN
Definition: zic.c:26
static void time_overflow(void)
Definition: zic.c:3816

◆ outzone()

static void outzone ( const struct zone zp,
ptrdiff_t  ntzones 
)
static

Definition at line 2957 of file zic.c.

References _, addtt(), addtype(), attype::at, attypes, compat, DC_DOM, doabbr(), attype::dontmerge, eat(), eats(), emalloc(), EPOCH_YEAR, error(), free, i, INITIALIZE, max_abbrvar_len, max_year, oadd(), rule::r_abbrvar, rule::r_dayofmonth, rule::r_dycode, rule::r_filename, rule::r_hiwasnum, rule::r_hiyear, rule::r_isdst, rule::r_linenum, rule::r_lowasnum, rule::r_loyear, rule::r_month, rule::r_save, rule::r_temp, rule::r_tod, rule::r_todisstd, rule::r_todisut, rule::r_todo, rule::r_yrtype, rpytime(), stringzone(), tadd(), timecnt, TM_JANUARY, generate_unaccent_rules::type, attype::type, updateminmax(), want_bloat(), warning(), writezone(), yearistype(), YEARSPERREPEAT, zone::z_filename, zone::z_format, zone::z_isdst, zone::z_linenum, zone::z_name, zone::z_nrules, zone::z_rules, zone::z_save, zone::z_stdoff, zone::z_untilrule, zone::z_untiltime, ZIC_MAX, ZIC_MIN, ZIC_VERSION, and ZIC_VERSION_PRE_2013.

Referenced by main().

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;
3012  if (leapseen)
3013  {
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 }
static bool yearistype(zic_t year, const char *type)
Definition: zic.c:3492
static zic_t leapmaxyear
Definition: zic.c:182
lineno_t r_linenum
Definition: zic.c:64
static int typecnt
Definition: zic.c:196
zic_t z_save
Definition: zic.c:110
static int stringzone(char *result, struct zone const *zpfirst, ptrdiff_t zonecount)
Definition: zic.c:2820
Definition: zic.c:390
unsigned char type
Definition: zic.c:394
bool r_todisstd
Definition: zic.c:80
static void static void static void warning(const char *string,...) pg_attribute_printf(1
Definition: zic.c:515
static void addtt(zic_t starttime, int type)
Definition: zic.c:3359
#define INITIALIZE(x)
Definition: private.h:92
const char * z_format
Definition: zic.c:106
zic_t r_save
Definition: zic.c:83
#define DC_DOM
Definition: zic.c:94
zic_t z_untiltime
Definition: zic.c:116
struct rule z_untilrule
Definition: zic.c:115
static zic_t min_year
Definition: zic.c:187
static void updateminmax(const zic_t x)
Definition: zic.c:2682
static int max_format_len
Definition: zic.c:185
bool z_isdst
Definition: zic.c:109
Definition: localtime.c:72
bool r_lowasnum
Definition: zic.c:70
#define ZIC_MAX
Definition: zic.c:27
const char * z_filename
Definition: zic.c:100
static zic_t tadd(zic_t t1, zic_t t2)
Definition: zic.c:3831
static int addtype(zic_t, char const *, bool, bool, bool)
Definition: zic.c:3369
bool r_todo
Definition: zic.c:86
static void writezone(const char *const name, const char *const string, char version, int defaulttype)
Definition: zic.c:2091
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 ZIC_VERSION_PRE_2013
Definition: zic.c:22
bool r_isdst
Definition: zic.c:82
static bool leapseen
Definition: zic.c:180
#define ZIC_VERSION
Definition: zic.c:23
static int max_abbrvar_len
Definition: zic.c:184
int64 zic_t
Definition: zic.c:25
zic_t r_hiyear
Definition: zic.c:68
const char * r_yrtype
Definition: zic.c:69
zic_t r_temp
Definition: zic.c:87
#define ZIC_MIN
Definition: zic.c:26
bool r_todisut
Definition: zic.c:81
const char * r_filename
Definition: zic.c:63
enum COMPAT_MODE compat
Definition: ecpg.c:25
zic_t at
Definition: zic.c:392
#define EPOCH_YEAR
Definition: private.h:134
static zic_t rpytime(const struct rule *rp, zic_t wantedy)
Definition: zic.c:3860
static void eat(char const *name, lineno_t num)
Definition: zic.c:482
const char * r_abbrvar
Definition: zic.c:84
static struct attype * attypes
static zic_t const max_time
Definition: zic.c:586
static bool noise
Definition: zic.c:188
ptrdiff_t z_nrules
Definition: zic.c:113
#define TM_JANUARY
Definition: private.h:119
lineno_t z_linenum
Definition: zic.c:101
static zic_t max_year
Definition: zic.c:186
#define free(a)
Definition: header.h:65
static ptrdiff_t timecnt
Definition: zic.c:194
static int charcnt
Definition: zic.c:175
static zic_t leapminyear
Definition: zic.c:181
#define YEARSPERREPEAT
Definition: private.h:99
struct rule * z_rules
Definition: zic.c:112
Definition: zic.c:98
bool r_hiwasnum
Definition: zic.c:71
int i
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
zic_t r_loyear
Definition: zic.c:67
static zic_t const min_time
Definition: zic.c:585
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
#define _(x)
Definition: elog.c:88
static zic_t oadd(zic_t t1, zic_t t2)
Definition: zic.c:3823
static bool want_bloat(void)
Definition: zic.c:645
zic_t z_stdoff
Definition: zic.c:104
static void * emalloc(size_t size)
Definition: zic.c:434

◆ puttzcode()

static void puttzcode ( const int32  val,
FILE *const  fp 
)
static

Definition at line 2023 of file zic.c.

References buf, and convert().

Referenced by puttzcodepass(), and writezone().

2024 {
2025  char buf[4];
2026 
2027  convert(val, buf);
2028  fwrite(buf, sizeof buf, 1, fp);
2029 }
static void convert(const int32 val, char *const buf)
Definition: zic.c:2001
static char * buf
Definition: pg_test_fsync.c:67
long val
Definition: informix.c:664

◆ puttzcodepass()

static void puttzcodepass ( zic_t  val,
FILE *  fp,
int  pass 
)
static

Definition at line 2032 of file zic.c.

References buf, convert64(), and puttzcode().

Referenced by writezone().

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 }
static char * buf
Definition: pg_test_fsync.c:67
long val
Definition: informix.c:664
static void puttzcode(const int32 val, FILE *const fp)
Definition: zic.c:2023
static void convert64(const zic_t val, char *const buf)
Definition: zic.c:2012

◆ rcomp()

static int rcomp ( const void *  cp1,
const void *  cp2 
)
static

Definition at line 1153 of file zic.c.

References rule::r_name.

Referenced by associate().

1154 {
1155  return strcmp(((const struct rule *) cp1)->r_name,
1156  ((const struct rule *) cp2)->r_name);
1157 }
Definition: localtime.c:72

◆ rpytime()

static zic_t rpytime ( const struct rule rp,
zic_t  wantedy 
)
static

Definition at line 3860 of file zic.c.

References _, DC_DOWGEQ, DC_DOWLEQ, EPOCH_WDAY, EPOCH_YEAR, error(), EXIT_FAILURE, i, isleap, LDAYSPERWEEK, max_time, min_time, oadd(), rule::r_dayofmonth, rule::r_dycode, rule::r_month, rule::r_tod, rule::r_wday, SECSPERDAY, SECSPERREPEAT, tadd(), TM_FEBRUARY, TM_JANUARY, warning(), YEARSPERREPEAT, ZIC_MAX, and ZIC_MIN.

Referenced by inzsub(), and outzone().

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 }
zic_t r_tod
Definition: zic.c:79
static void static void static void warning(const char *string,...) pg_attribute_printf(1
Definition: zic.c:515
int r_month
Definition: zic.c:73
#define DC_DOWGEQ
Definition: zic.c:95
#define SECSPERDAY
Definition: private.h:108
#define isleap(y)
Definition: datetime.h:271
static const int len_months[2][MONSPERYEAR]
Definition: zic.c:381
#define ZIC_MAX
Definition: zic.c:27
#define LDAYSPERWEEK
static zic_t tadd(zic_t t1, zic_t t2)
Definition: zic.c:3831
int64 zic_t
Definition: zic.c:25
#define ZIC_MIN
Definition: zic.c:26
int r_dycode
Definition: zic.c:75
#define EPOCH_YEAR
Definition: private.h:134
#define DC_DOWLEQ
Definition: zic.c:96
static zic_t const max_time
Definition: zic.c:586
static bool noise
Definition: zic.c:188
#define TM_JANUARY
Definition: private.h:119
#define TM_FEBRUARY
Definition: private.h:120
int r_dayofmonth
Definition: zic.c:76
int r_wday
Definition: zic.c:77
#define EPOCH_WDAY
Definition: private.h:135
#define YEARSPERREPEAT
Definition: private.h:99
int i
#define EXIT_FAILURE
Definition: settings.h:154
static const int len_years[2]
Definition: zic.c:386
#define SECSPERREPEAT
Definition: private.h:159
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
static zic_t const min_time
Definition: zic.c:585
#define _(x)
Definition: elog.c:88
static zic_t oadd(zic_t t1, zic_t t2)
Definition: zic.c:3823

◆ rule_cmp()

static int rule_cmp ( struct rule const *  a,
struct rule const *  b 
)
static

Definition at line 2806 of file zic.c.

References rule::r_dayofmonth, rule::r_hiyear, and rule::r_month.

Referenced by stringzone().

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 }

◆ rulesub()

static void rulesub ( struct rule rp,
const char *  loyearp,
const char *  hiyearp,
const char *  typep,
const char *  monthp,
const char *  dayp,
const char *  timep 
)
static

Definition at line 1825 of file zic.c.

References _, byword(), DC_DOM, DC_DOWGEQ, DC_DOWLEQ, ecpyalloc(), error(), EXIT_FAILURE, fprintf, free, gethms(), lookup::l_value, lowerit(), rule::r_dayofmonth, rule::r_dycode, rule::r_hiwasnum, rule::r_hiyear, rule::r_lowasnum, rule::r_loyear, rule::r_month, rule::r_tod, rule::r_todisstd, rule::r_todisut, rule::r_wday, rule::r_yrtype, warning(), YR_MAXIMUM, YR_MINIMUM, YR_ONLY, ZIC_MAX, and ZIC_MIN.

Referenced by inrule(), and inzsub().

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 }
#define YR_MINIMUM
Definition: zic.c:279
zic_t r_tod
Definition: zic.c:79
bool r_todisstd
Definition: zic.c:80
static char * ecpyalloc(char const *str)
Definition: zic.c:446
static void static void static void warning(const char *string,...) pg_attribute_printf(1
Definition: zic.c:515
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 DC_DOM
Definition: zic.c:94
static const int len_months[2][MONSPERYEAR]
Definition: zic.c:381
#define fprintf
Definition: port.h:197
const int l_value
Definition: zic.c:306
bool r_lowasnum
Definition: zic.c:70
#define ZIC_MAX
Definition: zic.c:27
#define YR_ONLY
Definition: zic.c:281
static zic_t gethms(const char *string, const char *errstring)
Definition: zic.c:1367
static struct lookup const mon_names[]
Definition: zic.c:324
Definition: zic.c:303
zic_t r_hiyear
Definition: zic.c:68
const char * r_yrtype
Definition: zic.c:69
#define ZIC_MIN
Definition: zic.c:26
bool r_todisut
Definition: zic.c:81
static struct lookup const end_years[]
Definition: zic.c:368
int r_dycode
Definition: zic.c:75
#define DC_DOWLEQ
Definition: zic.c:96
static const char * progname
Definition: zic.c:193
#define free(a)
Definition: header.h:65
int r_dayofmonth
Definition: zic.c:76
int r_wday
Definition: zic.c:77
static struct lookup const * byword(const char *string, const struct lookup *lp)
Definition: zic.c:3710
static char lowerit(char)
Definition: zic.c:3610
#define YR_MAXIMUM
Definition: zic.c:280
static struct lookup const lasts[]
Definition: zic.c:351
bool r_hiwasnum
Definition: zic.c:71
static struct lookup const wday_names[]
Definition: zic.c:340
#define EXIT_FAILURE
Definition: settings.h:154
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
zic_t r_loyear
Definition: zic.c:67
#define _(x)
Definition: elog.c:88

◆ shellquote()

static char* shellquote ( char *  b,
char const *  s 
)
static

Definition at line 3478 of file zic.c.

Referenced by yearistype().

3479 {
3480  *b++ = '\'';
3481  while (*s)
3482  {
3483  if (*s == '\'')
3484  *b++ = '\'', *b++ = '\\', *b++ = '\'';
3485  *b++ = *s++;
3486  }
3487  *b++ = '\'';
3488  return b;
3489 }

◆ size_product()

static size_t size_product ( size_t  nitems,
size_t  itemsize 
)
static

Definition at line 418 of file zic.c.

References _, and memory_exhausted().

Referenced by getfields(), growalloc(), and writezone().

419 {
420  if (SIZE_MAX / itemsize < nitems)
421  memory_exhausted(_("size overflow"));
422  return nitems * itemsize;
423 }
static void memory_exhausted(const char *msg) pg_attribute_noreturn()
Definition: zic.c:411
#define _(x)
Definition: elog.c:88

◆ stringoffset()

static int stringoffset ( char *  result,
zic_t  offset 
)
static

Definition at line 2691 of file zic.c.

References DAYSPERWEEK, HOURSPERDAY, MINSPERHOUR, SECSPERMIN, and sprintf.

Referenced by stringrule(), and stringzone().

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 }
#define SECSPERMIN
Definition: private.h:101
#define HOURSPERDAY
Definition: private.h:103
#define sprintf
Definition: port.h:195
#define DAYSPERWEEK
Definition: private.h:104
#define MINSPERHOUR
Definition: private.h:102

◆ stringrule()

static int stringrule ( char *  result,
struct rule *const  rp,
zic_t  save,
zic_t  stdoff 
)
static

Definition at line 2725 of file zic.c.

References compat, DAYSPERWEEK, DC_DOM, DC_DOWGEQ, DC_DOWLEQ, MINSPERHOUR, rule::r_dayofmonth, rule::r_dycode, rule::r_isdst, rule::r_month, rule::r_tod, rule::r_todisstd, rule::r_todisut, rule::r_wday, SECSPERDAY, SECSPERMIN, sprintf, stringoffset(), and TM_FEBRUARY.

Referenced by stringzone().

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 }
#define SECSPERMIN
Definition: private.h:101
zic_t r_tod
Definition: zic.c:79
bool r_todisstd
Definition: zic.c:80
int r_month
Definition: zic.c:73
#define DC_DOWGEQ
Definition: zic.c:95
#define SECSPERDAY
Definition: private.h:108
#define DC_DOM
Definition: zic.c:94
static const int len_months[2][MONSPERYEAR]
Definition: zic.c:381
static int stringoffset(char *result, zic_t offset)
Definition: zic.c:2691
#define sprintf
Definition: port.h:195
#define DAYSPERWEEK
Definition: private.h:104
bool r_isdst
Definition: zic.c:82
int64 zic_t
Definition: zic.c:25
bool r_todisut
Definition: zic.c:81
enum COMPAT_MODE compat
Definition: ecpg.c:25
int r_dycode
Definition: zic.c:75
#define DC_DOWLEQ
Definition: zic.c:96
#define TM_FEBRUARY
Definition: private.h:120
int r_dayofmonth
Definition: zic.c:76
int r_wday
Definition: zic.c:77
#define MINSPERHOUR
Definition: private.h:102

◆ stringzone()

static int stringzone ( char *  result,
struct zone const *  zpfirst,
ptrdiff_t  zonecount 
)
static

Definition at line 2820 of file zic.c.

References compat, DC_DOM, doabbr(), i, MINSPERHOUR, rule::r_abbrvar, rule::r_dayofmonth, rule::r_dycode, rule::r_hiwasnum, rule::r_hiyear, rule::r_isdst, rule::r_month, rule::r_save, rule::r_tod, rule::r_todisstd, rule::r_todisut, rule::r_yrtype, rule_cmp(), SECSPERDAY, SECSPERMIN, stringoffset(), stringrule(), TM_DECEMBER, TM_JANUARY, zone::z_isdst, zone::z_nrules, zone::z_rules, zone::z_stdoff, and ZIC_MAX.

Referenced by outzone().

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 }
#define SECSPERMIN
Definition: private.h:101
int r_month
Definition: zic.c:73
#define SECSPERDAY
Definition: private.h:108
zic_t r_save
Definition: zic.c:83
#define TM_DECEMBER
Definition: private.h:130
static int rule_cmp(struct rule const *a, struct rule const *b)
Definition: zic.c:2806
#define DC_DOM
Definition: zic.c:94
static zic_t hi_time
Definition: zic.c:591
static int stringoffset(char *result, zic_t offset)
Definition: zic.c:2691
bool z_isdst
Definition: zic.c:109
Definition: localtime.c:72
#define ZIC_MAX
Definition: zic.c:27
static size_t doabbr(char *abbr, struct zone const *zp, char const *letters, bool isdst, zic_t save, bool doquotes)
Definition: zic.c:2639
bool r_isdst
Definition: zic.c:82
char * c
static int stringrule(char *result, struct rule *const rp, zic_t save, zic_t stdoff)
Definition: zic.c:2725
zic_t r_hiyear
Definition: zic.c:68
const char * r_yrtype
Definition: zic.c:69
enum COMPAT_MODE compat
Definition: ecpg.c:25
const char * r_abbrvar
Definition: zic.c:84
static zic_t const max_time
Definition: zic.c:586
ptrdiff_t z_nrules
Definition: zic.c:113
#define TM_JANUARY
Definition: private.h:119
struct rule * z_rules
Definition: zic.c:112
Definition: zic.c:98
bool r_hiwasnum
Definition: zic.c:71
int i
#define MINSPERHOUR
Definition: private.h:102
zic_t z_stdoff
Definition: zic.c:104

◆ tadd()

static zic_t tadd ( zic_t  t1,
zic_t  t2 
)
static

Definition at line 3831 of file zic.c.

References max_time, min_time, and time_overflow().

Referenced by adjleap(), getleapdatetime(), outzone(), rpytime(), and writezone().

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 }
static void time_overflow(void)
Definition: zic.c:3816
static zic_t const max_time
Definition: zic.c:586
static zic_t const min_time
Definition: zic.c:585

◆ time_overflow()

static void time_overflow ( void  )
static

Definition at line 3816 of file zic.c.

References _, error(), and EXIT_FAILURE.

Referenced by oadd(), and tadd().

3817 {
3818  error(_("time overflow"));
3819  exit(EXIT_FAILURE);
3820 }
#define EXIT_FAILURE
Definition: settings.h:154
static void static void error(const char *string,...) pg_attribute_printf(1
Definition: zic.c:504
#define _(x)
Definition: elog.c:88

◆ timerange_option()

static bool timerange_option ( char *  timerange)
static

Definition at line 602 of file zic.c.

References max_time, min_time, PG_INT64_MAX, PG_INT64_MIN, and strtoimax.

Referenced by main().

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 }
#define PG_INT64_MAX
Definition: c.h:460
static zic_t lo_time
Definition: zic.c:590
static zic_t hi_time
Definition: zic.c:591
#define PG_INT64_MIN
Definition: c.h:459
#define strtoimax
Definition: private.h:52
static zic_t const max_time
Definition: zic.c:586
static zic_t const min_time
Definition: zic.c:585

◆ updateminmax()

static void updateminmax ( const zic_t  x)
static

Definition at line 2682 of file zic.c.

Referenced by outzone().

2683 {
2684  if (min_year > x)
2685  min_year = x;
2686  if (max_year < x)
2687  max_year = x;
2688 }
static zic_t min_year
Definition: zic.c:187
static zic_t max_year
Definition: zic.c:186

◆ usage()

static void usage ( FILE *  stream,
int  status 
)
static

Definition at line 543 of file zic.c.

References _, close_file(), EXIT_SUCCESS, and fprintf.

Referenced by main().

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 }
#define EXIT_SUCCESS
Definition: settings.h:150
#define fprintf
Definition: port.h:197
static const char * progname
Definition: zic.c:193
static void close_file(FILE *stream, char const *dir, char const *name)
Definition: zic.c:527
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
#define _(x)
Definition: elog.c:88

◆ verror()

static void verror ( const char *  string,
va_list  args 
)
static

Definition at line 488 of file zic.c.

References _, fprintf, and vfprintf.

Referenced by error(), and warning().

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)"),
500  fprintf(stderr, "\n");
501 }
#define fprintf
Definition: port.h:197
static lineno_t rlinenum
Definition: zic.c:192
static const char * filename
Definition: zic.c:178
static lineno_t linenum
Definition: zic.c:183
static const char * rfilename
Definition: zic.c:191
#define vfprintf
Definition: port.h:196
#define _(x)
Definition: elog.c:88

◆ want_bloat()

static bool want_bloat ( void  )
static

Definition at line 645 of file zic.c.

References bloat.

Referenced by addtype(), outzone(), and writezone().

646 {
647  return 0 <= bloat;
648 }
static int bloat
Definition: zic.c:642

◆ warning()

static void warning ( const char *  string,
  ... 
)
static

Definition at line 515 of file zic.c.

References _, generate_unaccent_rules::args, fprintf, and verror().

Referenced by adjleap(), associate(), byword(), componentcheck(), dolink(), gethms(), inzsub(), main(), namecheck(), newabbr(), outzone(), rpytime(), rulesub(), and writezone().

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 }
static bool warnings
Definition: zic.c:177
#define fprintf
Definition: port.h:197
static void verror(const char *string, va_list args) pg_attribute_printf(1
Definition: zic.c:488
#define _(x)
Definition: elog.c:88

◆ writezone()

static void writezone ( const char *const  name,
const char *const  string,
char  version,
int  defaulttype 
)
static

Definition at line 2091 of file zic.c.

References _, addtype(), attype::at, atcomp(), attypes, timerange::base, close_file(), convert(), timerange::count, timerange::defaulttype, DO, emalloc(), error(), EXIT_FAILURE, fprintf, free, i, INT64_FORMAT, timerange::leapbase, leapcnt, timerange::leapcount, limitrange(), lo_time, max_time, MAXALIGN, mkdirs(), PG_INT32_MAX, PG_INT32_MIN, puttzcode(), puttzcodepass(), qsort, size_product(), generate_unaccent_rules::stdout, strerror, tadd(), timecnt, tm, generate_unaccent_rules::type, attype::type, typecnt, types, TZ_MAGIC, TZ_MAX_CHARS, TZ_MAX_TIMES, TZ_MAX_TYPES, tzhead::tzh_charcnt, tzhead::tzh_leapcnt, tzhead::tzh_magic, tzhead::tzh_timecnt, tzhead::tzh_ttisstdcnt, tzhead::tzh_ttisutcnt, tzhead::tzh_typecnt, tzhead::tzh_version, want_bloat(), warning(), WORK_AROUND_QTBUG_53071, and ZIC_MAX.

Referenced by outzone().

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)