From 92bd70fb85bce57ac47ba5d8af008736832c955a Mon Sep 17 00:00:00 2001 From: Joseph Myers Date: Fri, 16 Jun 2017 11:09:21 +0000 Subject: Update timezone code from tzcode 2017b. This patch updates files coming from tzcode to the versions in tzcode 2017b. A couple of changes to other glibc code are needed. time/tzset.c was using the SECSPERDAY macro from tzfile.h, which no longer defines that macro, so a local definition is added to tzset.c. Because timezone/private.h now defines the _ macro whenever HAVE_GETTEXT is true, even if it was previously defined, it is also necessary to avoid a conflict with the definition in include/libintl.h. Defining _ISOMAC is the obvious way to avoid such internal definitions being visible, together with defining TZ_DOMAIN so that zic and zdump continue to get the messages from the libc domain as desired. However, zic and zdump rely on PKGVERSION and REPORT_BUGS_TO from config.h, which is not included by default with _ISOMAC, so -include config.h needs adding to the options for these programs as well. Together those changes allow unmodified tzcode 2017b sources to work in glibc. Tested for x86_64. * timezone/private.h: Update from tzcode 2017b. * timezone/tzfile.h: Likewise. * timezone/tzselect.ksh: Likewise. * timezone/zdump.c: Likewise. * timezone/zic.c: Likewise. * timezone/Makefile (tz-cflags): Add -D_ISOMAC -DTZ_DOMAIN='"libc"' -include $(common-objpfx)config.h. * time/tzset.c (SECSPERDAY): New macro. --- timezone/zic.c | 684 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 419 insertions(+), 265 deletions(-) (limited to 'timezone/zic.c') diff --git a/timezone/zic.c b/timezone/zic.c index 78ab870941..068fb43318 100644 --- a/timezone/zic.c +++ b/timezone/zic.c @@ -5,10 +5,12 @@ #include "version.h" #include "private.h" -#include "locale.h" #include "tzfile.h" +#include +#include #include +#include #define ZIC_VERSION_PRE_2013 '2' #define ZIC_VERSION '3' @@ -16,12 +18,20 @@ typedef int_fast64_t zic_t; #define ZIC_MIN INT_FAST64_MIN #define ZIC_MAX INT_FAST64_MAX +#define PRIdZIC PRIdFAST64 #define SCNdZIC SCNdFAST64 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN #define ZIC_MAX_ABBR_LEN_WO_WARN 6 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */ +#ifdef HAVE_DIRECT_H +# include +# include +# undef mkdir +# define mkdir(name, mode) _mkdir(name) +#endif + #if HAVE_SYS_STAT_H #include #endif @@ -31,9 +41,18 @@ typedef int_fast64_t zic_t; #define MKDIR_UMASK 0755 #endif +/* The maximum ptrdiff_t value, for pre-C99 platforms. */ +#ifndef PTRDIFF_MAX +static ptrdiff_t const PTRDIFF_MAX = MAXVAL(ptrdiff_t, TYPE_BIT(ptrdiff_t)); +#endif + +/* The type and printf format for line numbers. */ +typedef intmax_t lineno; +#define PRIdLINENO PRIdMAX + struct rule { const char * r_filename; - int r_linenum; + lineno r_linenum; const char * r_name; zic_t r_loyear; /* for example, 1986 */ @@ -56,7 +75,7 @@ struct rule { zic_t r_stdoff; /* offset from standard time */ const char * r_abbrvar; /* variable part of abbreviation */ - int r_todo; /* a rule to do (used in outzone) */ + bool r_todo; /* a rule to do (used in outzone) */ zic_t r_temp; /* used in outzone */ }; @@ -70,7 +89,7 @@ struct rule { struct zone { const char * z_filename; - int z_linenum; + lineno z_linenum; const char * z_name; zic_t z_gmtoff; @@ -81,23 +100,31 @@ struct zone { zic_t z_stdoff; struct rule * z_rules; - int z_nrules; + ptrdiff_t z_nrules; struct rule z_untilrule; zic_t z_untiltime; }; +#if !HAVE_POSIX_DECLS extern int getopt(int argc, char * const argv[], const char * options); extern int link(const char * fromname, const char * toname); extern char * optarg; extern int optind; +#endif #if ! HAVE_LINK -# define link(from, to) (-1) +# define link(from, to) (errno = ENOTSUP, -1) #endif #if ! HAVE_SYMLINK -# define symlink(from, to) (-1) +# define readlink(file, buf, size) (errno = ENOTSUP, -1) +# define symlink(from, to) (errno = ENOTSUP, -1) +# define S_ISLNK(m) 0 +#endif +#ifndef AT_SYMLINK_FOLLOW +# define linkat(fromdir, from, todir, to, flag) \ + (itssymlink(from) ? (errno = ENOTSUP, -1) : link(from, to)) #endif static void addtt(zic_t starttime, int type); @@ -105,7 +132,7 @@ static int addtype(zic_t, char const *, bool, bool, bool); static void leapadd(zic_t, bool, int, int); static void adjleap(void); static void associate(void); -static void dolink(const char * fromfield, const char * tofield); +static void dolink(const char *, const char *, bool); static char ** getfields(char * buf); static zic_t gethms(const char * string, const char * errstring, bool); @@ -116,24 +143,32 @@ 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 int itsdir(const char * name); +static bool itsdir(char const *); +static bool itssymlink(char const *); static bool is_alpha(char a); static char lowerit(char); -static bool mkdirs(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, int ntzones); +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(int year, const char * type); +static bool yearistype(zic_t year, const char * type); /* Bound on length of what %z can expand to. */ enum { PERCENT_Z_LEN_BOUND = sizeof "+995959" - 1 }; +/* If true, work around a bug in Qt 5.6.1 and earlier, which mishandles + tz binary files whose POSIX-TZ-style strings contain '<'; see + QTBUG-53071 . This + workaround will no longer be needed when Qt 5.6.1 and earlier are + obsolete, say in the year 2021. */ +enum { WORK_AROUND_QTBUG_53071 = true }; + static int charcnt; static bool errors; static bool warnings; @@ -142,17 +177,17 @@ static int leapcnt; static bool leapseen; static zic_t leapminyear; static zic_t leapmaxyear; -static int linenum; +static lineno 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 const char * rfilename; -static int rlinenum; +static lineno rlinenum; static const char * progname; -static int timecnt; -static int timecnt_alloc; +static ptrdiff_t timecnt; +static ptrdiff_t timecnt_alloc; static int typecnt; /* @@ -237,23 +272,23 @@ static int typecnt; #define YR_ONLY 2 static struct rule * rules; -static int nrules; /* number of rules */ -static int nrules_alloc; +static ptrdiff_t nrules; /* number of rules */ +static ptrdiff_t nrules_alloc; static struct zone * zones; -static int nzones; /* number of zones */ -static int nzones_alloc; +static ptrdiff_t nzones; /* number of zones */ +static ptrdiff_t nzones_alloc; struct link { const char * l_filename; - int l_linenum; + lineno l_linenum; const char * l_from; const char * l_to; }; static struct link * links; -static int nlinks; -static int nlinks_alloc; +static ptrdiff_t nlinks; +static ptrdiff_t nlinks_alloc; struct lookup { const char * l_word; @@ -339,6 +374,7 @@ static const int len_years[2] = { static struct attype { zic_t at; + bool dontmerge; unsigned char type; } * attypes; static zic_t gmtoffs[TZ_MAX_TYPES]; @@ -406,15 +442,16 @@ ecpyalloc (char const *str) } static void * -growalloc(void *ptr, size_t itemsize, int nitems, int *nitems_alloc) +growalloc(void *ptr, size_t itemsize, ptrdiff_t nitems, ptrdiff_t *nitems_alloc) { if (nitems < *nitems_alloc) return ptr; else { - int amax = INT_MAX < SIZE_MAX ? INT_MAX : SIZE_MAX; + ptrdiff_t nitems_max = PTRDIFF_MAX - WORK_AROUND_QTBUG_53071; + ptrdiff_t amax = nitems_max < SIZE_MAX ? nitems_max : SIZE_MAX; if ((amax - 1) / 3 * 2 < *nitems_alloc) - memory_exhausted(_("int overflow")); - *nitems_alloc = *nitems_alloc + (*nitems_alloc >> 1) + 1; + memory_exhausted(_("integer overflow")); + *nitems_alloc += (*nitems_alloc >> 1) + 1; return erealloc(ptr, size_product(*nitems_alloc, itemsize)); } } @@ -424,8 +461,7 @@ growalloc(void *ptr, size_t itemsize, int nitems, int *nitems_alloc) */ static void -eats(const char *const name, const int num, const char *const rname, - const int rnum) +eats(char const *name, lineno num, char const *rname, lineno rnum) { filename = name; linenum = num; @@ -434,7 +470,7 @@ eats(const char *const name, const int num, const char *const rname, } static void -eat(const char *const name, const int num) +eat(char const *name, lineno num) { eats(name, num, NULL, -1); } @@ -448,10 +484,10 @@ verror(const char *const string, va_list args) ** on BSD systems. */ if (filename) - fprintf(stderr, _("\"%s\", line %d: "), filename, linenum); + fprintf(stderr, _("\"%s\", line %"PRIdLINENO": "), filename, linenum); vfprintf(stderr, string, args); if (rfilename != NULL) - fprintf(stderr, _(" (rule from \"%s\", line %d)"), + fprintf(stderr, _(" (rule from \"%s\", line %"PRIdLINENO")"), rfilename, rlinenum); fprintf(stderr, "\n"); } @@ -478,15 +514,15 @@ warning(const char *const string, ...) } static void -close_file(FILE *stream, char const *name) +close_file(FILE *stream, char const *dir, char const *name) { char const *e = (ferror(stream) ? _("I/O error") : fclose(stream) != 0 ? strerror(errno) : NULL); if (e) { - fprintf(stderr, "%s: ", progname); - if (name) - fprintf(stderr, "%s: ", name); - fprintf(stderr, "%s\n", e); + fprintf(stderr, "%s: %s%s%s%s%s\n", progname, + dir ? dir : "", dir ? "/" : "", + name ? name : "", name ? ": " : "", + e); exit(EXIT_FAILURE); } } @@ -501,10 +537,30 @@ usage(FILE *stream, int status) "Report bugs to %s.\n"), progname, progname, REPORT_BUGS_TO); if (status == EXIT_SUCCESS) - close_file(stream, NULL); + close_file(stream, NULL, NULL); exit(status); } +/* Change the working directory to DIR, possibly creating DIR and its + ancestors. After this is done, all files are accessed with names + relative to DIR. */ +static void +change_directory (char const *dir) +{ + if (chdir(dir) != 0) { + int chdir_errno = errno; + if (chdir_errno == ENOENT) { + mkdirs(dir, false); + chdir_errno = chdir(dir) == 0 ? 0 : errno; + } + if (chdir_errno != 0) { + fprintf(stderr, _("%s: Can't chdir to %s: %s\n"), + progname, dir, strerror(chdir_errno)); + exit(EXIT_FAILURE); + } + } +} + static const char * psxrules; static const char * lcltime; static const char * directory; @@ -514,9 +570,8 @@ static const char * yitcommand; int main(int argc, char **argv) { - register int i; - register int j; - register int c; + register int c, k; + register ptrdiff_t i, j; #ifdef S_IWGRP umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH)); @@ -534,12 +589,12 @@ main(int argc, char **argv) _("wild compilation-time specification of zic_t")); return EXIT_FAILURE; } - for (i = 1; i < argc; ++i) - if (strcmp(argv[i], "--version") == 0) { + for (k = 1; k < argc; k++) + if (strcmp(argv[k], "--version") == 0) { printf("zic %s%s\n", PKGVERSION, TZVERSION); - close_file(stdout, NULL); + close_file(stdout, NULL, NULL); return EXIT_SUCCESS; - } else if (strcmp(argv[i], "--help") == 0) { + } else if (strcmp(argv[k], "--help") == 0) { usage(stdout, EXIT_SUCCESS); } while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF && c != -1) @@ -615,11 +670,12 @@ _("%s: More than one -L option specified\n"), adjleap(); } - for (i = optind; i < argc; ++i) - infile(argv[i]); + for (k = optind; k < argc; k++) + infile(argv[k]); if (errors) return EXIT_FAILURE; associate(); + change_directory(directory); for (i = 0; i < nzones; i = j) { /* ** Find the next non-continuation zone entry. @@ -633,7 +689,7 @@ _("%s: More than one -L option specified\n"), */ for (i = 0; i < nlinks; ++i) { eat(links[i].l_filename, links[i].l_linenum); - dolink(links[i].l_from, links[i].l_to); + dolink(links[i].l_from, links[i].l_to, false); if (noise) for (j = 0; j < nlinks; ++j) if (strcmp(links[i].l_to, @@ -642,11 +698,11 @@ _("%s: More than one -L option specified\n"), } if (lcltime != NULL) { eat(_("command line"), 1); - dolink(lcltime, TZDEFAULT); + dolink(lcltime, TZDEFAULT, true); } if (psxrules != NULL) { eat(_("command line"), 1); - dolink(psxrules, TZDEFRULES); + dolink(psxrules, TZDEFRULES, true); } if (warnings && (ferror(stderr) || fclose(stderr) != 0)) return EXIT_FAILURE; @@ -658,7 +714,7 @@ componentcheck(char const *name, char const *component, char const *component_end) { enum { component_len_max = 14 }; - size_t component_len = component_end - component; + ptrdiff_t component_len = component_end - component; if (component_len == 0) { if (!*name) error (_("empty file name")); @@ -673,8 +729,9 @@ componentcheck(char const *name, char const *component, } if (0 < component_len && component_len <= 2 && component[0] == '.' && component_end[-1] == '.') { + int len = component_len; error(_("file name '%s' contains '%.*s' component"), - name, (int) component_len, component); + name, len, component); return false; } if (noise) { @@ -723,118 +780,138 @@ namecheck(const char *name) return componentcheck(name, component, cp); } +/* Create symlink contents suitable for symlinking FROM to TO, as a + freshly allocated string. FROM should be a relative file name, and + is relative to the global variable DIRECTORY. TO can be either + relative or absolute. */ static char * -relname(char const *dir, char const *base) -{ - if (*base == '/') - return ecpyalloc(base); - else { - size_t dir_len = strlen(dir); - bool needs_slash = dir_len && dir[dir_len - 1] != '/'; - char *result = emalloc(dir_len + needs_slash + strlen(base) + 1); - result[dir_len] = '/'; - strcpy(result + dir_len + needs_slash, base); - return memcpy(result, dir, dir_len); +relname(char const *from, char const *to) +{ + size_t i, taillen, dotdotetcsize; + size_t dir_len = 0, dotdots = 0, linksize = SIZE_MAX; + char const *f = from; + char *result = NULL; + if (*to == '/') { + /* Make F absolute too. */ + size_t len = strlen(directory); + bool needslash = len && directory[len - 1] != '/'; + linksize = len + needslash + strlen(from) + 1; + f = result = emalloc(linksize); + strcpy(result, directory); + result[len] = '/'; + strcpy(result + len + needslash, from); + } + for (i = 0; f[i] && f[i] == to[i]; i++) + if (f[i] == '/') + dir_len = i + 1; + for (; to[i]; i++) + dotdots += to[i] == '/' && to[i - 1] != '/'; + taillen = strlen(f + dir_len); + dotdotetcsize = 3 * dotdots + taillen + 1; + if (dotdotetcsize <= linksize) { + if (!result) + result = emalloc(dotdotetcsize); + for (i = 0; i < dotdots; i++) + memcpy(result + 3 * i, "../", 3); + memmove(result + 3 * dotdots, f + dir_len, taillen + 1); } + return result; +} + +/* Hard link FROM to TO, following any symbolic links. + Return 0 if successful, an error number otherwise. */ +static int +hardlinkerr(char const *from, char const *to) +{ + int r = linkat(AT_FDCWD, from, AT_FDCWD, to, AT_SYMLINK_FOLLOW); + return r == 0 ? 0 : errno; } static void -dolink(char const *fromfield, char const *tofield) +dolink(char const *fromfield, char const *tofield, bool staysymlink) { - register char * fromname; - register char * toname; - register int fromisdir; + bool todirs_made = false; + int link_errno; - fromname = relname(directory, fromfield); - toname = relname(directory, tofield); /* ** We get to be careful here since ** there's a fair chance of root running us. */ - fromisdir = itsdir(fromname); - if (fromisdir) { - char const *e = strerror(fromisdir < 0 ? errno : EPERM); - fprintf(stderr, _("%s: link from %s failed: %s"), - progname, fromname, e); + if (itsdir(fromfield)) { + fprintf(stderr, _("%s: link from %s/%s failed: %s\n"), + progname, directory, fromfield, strerror(EPERM)); exit(EXIT_FAILURE); } - if (itsdir(toname) <= 0) - remove(toname); - if (link(fromname, toname) != 0) { - int result; - - if (! mkdirs(toname)) - exit(EXIT_FAILURE); - - result = link(fromname, toname); - if (result != 0) { - const char *s = fromfield; - const char *t; - char *p; - size_t dotdots = 0; - register char * symlinkcontents = NULL; - - do - t = s; - while ((s = strchr(s, '/')) - && ! strncmp (fromfield, tofield, - ++s - fromfield)); - - for (s = tofield + (t - fromfield); *s; s++) - dotdots += *s == '/'; - symlinkcontents - = emalloc(3 * dotdots + strlen(t) + 1); - for (p = symlinkcontents; dotdots-- != 0; p += 3) - memcpy(p, "../", 3); - strcpy(p, t); - result = symlink(symlinkcontents, toname); - if (result == 0) -warning(_("hard link failed, symbolic link used")); - free(symlinkcontents); - } - if (result != 0) { - FILE *fp, *tp; - int c; - fp = fopen(fromname, "rb"); - if (!fp) { - const char *e = strerror(errno); - fprintf(stderr, - _("%s: Can't read %s: %s\n"), - progname, fromname, e); - exit(EXIT_FAILURE); - } - tp = fopen(toname, "wb"); - if (!tp) { - const char *e = strerror(errno); - fprintf(stderr, - _("%s: Can't create %s: %s\n"), - progname, toname, e); - exit(EXIT_FAILURE); - } - while ((c = getc(fp)) != EOF) - putc(c, tp); - close_file(fp, fromname); - close_file(tp, toname); - warning(_("link failed, copy used")); - } + if (staysymlink) + staysymlink = itssymlink(tofield); + if (remove(tofield) == 0) + todirs_made = true; + else if (errno != ENOENT) { + char const *e = strerror(errno); + fprintf(stderr, _("%s: Can't remove %s/%s: %s\n"), + progname, directory, tofield, e); + exit(EXIT_FAILURE); + } + link_errno = staysymlink ? ENOTSUP : hardlinkerr(fromfield, tofield); + if (link_errno == ENOENT && !todirs_made) { + mkdirs(tofield, true); + todirs_made = true; + link_errno = hardlinkerr(fromfield, tofield); + } + if (link_errno != 0) { + bool absolute = *fromfield == '/'; + char *linkalloc = absolute ? NULL : relname(fromfield, tofield); + char const *contents = absolute ? fromfield : linkalloc; + int symlink_errno = symlink(contents, tofield) == 0 ? 0 : errno; + if (symlink_errno == ENOENT && !todirs_made) { + mkdirs(tofield, true); + symlink_errno = symlink(contents, tofield) == 0 ? 0 : errno; + } + free(linkalloc); + if (symlink_errno == 0) { + if (link_errno != ENOTSUP) + warning(_("symbolic link used because hard link failed: %s"), + strerror(link_errno)); + } else { + FILE *fp, *tp; + int c; + fp = fopen(fromfield, "rb"); + if (!fp) { + char const *e = strerror(errno); + fprintf(stderr, _("%s: Can't read %s/%s: %s\n"), + progname, directory, fromfield, e); + exit(EXIT_FAILURE); + } + tp = fopen(tofield, "wb"); + if (!tp) { + char const *e = strerror(errno); + fprintf(stderr, _("%s: Can't create %s/%s: %s\n"), + progname, directory, tofield, e); + exit(EXIT_FAILURE); + } + while ((c = getc(fp)) != EOF) + putc(c, tp); + close_file(fp, directory, fromfield); + close_file(tp, directory, tofield); + if (link_errno != ENOTSUP) + warning(_("copy used because hard link failed: %s"), + strerror(link_errno)); + else if (symlink_errno != ENOTSUP) + warning(_("copy used because symbolic link failed: %s"), + strerror(symlink_errno)); + } } - free(fromname); - free(toname); } #define TIME_T_BITS_IN_FILE 64 -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 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); /* Estimated time of the Big Bang, in seconds since the POSIX epoch. rounded downward to the negation of a power of two that is comfortably outside the error bounds. - zic does not output time stamps before this, partly because they - are physically suspect, and partly because GNOME mishandles them; see - GNOME bug 730332 . - For the time of the Big Bang, see: Ade PAR, Aghanim N, Armitage-Caplan C et al. Planck 2013 results. @@ -855,26 +932,49 @@ static zic_t const max_time = MAXVAL (zic_t, TIME_T_BITS_IN_FILE); #define BIG_BANG (- (1LL << 59)) #endif -static const zic_t big_bang_time = BIG_BANG; +/* If true, work around GNOME bug 730332 + + by refusing to output time stamps before BIG_BANG. + Such time stamps are physically suspect anyway. -/* Return 1 if NAME is a directory, 0 if it's something else, -1 if trouble. */ -static int + The GNOME bug is scheduled to be fixed in GNOME 3.22, and if so + this workaround will no longer be needed when GNOME 3.21 and + earlier are obsolete, say in the year 2021. */ +enum { WORK_AROUND_GNOME_BUG_730332 = true }; + +static const zic_t early_time = (WORK_AROUND_GNOME_BUG_730332 + ? BIG_BANG + : MINVAL(zic_t, TIME_T_BITS_IN_FILE)); + +/* Return true if NAME is a directory. */ +static bool itsdir(char const *name) { struct stat st; int res = stat(name, &st); - if (res != 0) - return res; #ifdef S_ISDIR - return S_ISDIR(st.st_mode) != 0; -#else - { - char *nameslashdot = relname(name, "."); - res = stat(nameslashdot, &st); + if (res == 0) + return S_ISDIR(st.st_mode) != 0; +#endif + if (res == 0 || errno == EOVERFLOW) { + size_t n = strlen(name); + char *nameslashdot = emalloc(n + 3); + bool dir; + memcpy(nameslashdot, name, n); + strcpy(&nameslashdot[n], &"/."[! (n && name[n - 1] != '/')]); + dir = stat(nameslashdot, &st) == 0 || errno == EOVERFLOW; free(nameslashdot); - return res == 0; + return dir; } -#endif + return false; +} + +/* Return true if NAME is a symbolic link. */ +static bool +itssymlink(char const *name) +{ + char c; + return 0 <= readlink(name, &c, 1); } /* @@ -897,8 +997,7 @@ associate(void) { register struct zone * zp; register struct rule * rp; - register int base, out; - register int i, j; + register ptrdiff_t i, j, base, out; if (nrules != 0) { qsort(rules, nrules, sizeof *rules, rcomp); @@ -976,7 +1075,7 @@ infile(const char *name) register const struct lookup * lp; register int nfields; register bool wantcont; - register int num; + register lineno num; char buf[BUFSIZ]; if (strcmp(name, "-") == 0) { @@ -1017,7 +1116,7 @@ infile(const char *name) lp = byword(fields[0], line_codes); if (lp == NULL) error(_("input line of unknown type")); - else switch ((int) (lp->l_value)) { + else switch (lp->l_value) { case LC_RULE: inrule(fields, nfields); wantcont = false; @@ -1046,7 +1145,7 @@ _("%s: panic: Invalid l_value %d\n"), } free(fields); } - close_file(fp, filename); + close_file(fp, NULL, filename); if (wantcont) error(_("expected continuation line not found")); } @@ -1129,7 +1228,7 @@ inrule(char **fields, int nfields) static bool inzone(char **fields, int nfields) { - register int i; + register ptrdiff_t i; if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) { error(_("wrong number of fields on Zone line")); @@ -1150,8 +1249,8 @@ _("\"Zone %s\" line and -p option are mutually exclusive"), for (i = 0; i < nzones; ++i) if (zones[i].z_name != NULL && strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) { - error( -_("duplicate zone name %s (file \"%s\", line %d)"), + error(_("duplicate zone name %s" + " (file \"%s\", line %"PRIdLINENO")"), fields[ZF_NAME], zones[i].z_filename, zones[i].z_linenum); @@ -1263,7 +1362,7 @@ inleap(char **fields, int nfields) { register const char * cp; register const struct lookup * lp; - register int i, j; + register zic_t i, j; zic_t year; int month, day; zic_t dayoff, tod; @@ -1355,7 +1454,7 @@ inleap(char **fields, int nfields) return; } t = tadd(t, tod); - if (t < big_bang_time) { + if (t < early_time) { error(_("leap second precedes Big Bang")); return; } @@ -1435,7 +1534,7 @@ rulesub(struct rule *rp, const char *loyearp, const char *hiyearp, cp = loyearp; lp = byword(cp, begin_years); rp->r_lowasnum = lp == NULL; - if (!rp->r_lowasnum) switch ((int) lp->l_value) { + if (!rp->r_lowasnum) switch (lp->l_value) { case YR_MINIMUM: rp->r_loyear = ZIC_MIN; break; @@ -1454,7 +1553,7 @@ rulesub(struct rule *rp, const char *loyearp, const char *hiyearp, cp = hiyearp; lp = byword(cp, end_years); rp->r_hiwasnum = lp == NULL; - if (!rp->r_hiwasnum) switch ((int) lp->l_value) { + if (!rp->r_hiwasnum) switch (lp->l_value) { case YR_MINIMUM: rp->r_hiyear = ZIC_MIN; break; @@ -1592,15 +1691,18 @@ static void writezone(const char *const name, const char *const string, char version) { register FILE * fp; - register int i, j; + register ptrdiff_t i, j; register int leapcnt32, leapi32; - register int timecnt32, timei32; + register ptrdiff_t timecnt32, timei32; register int pass; - char * fullname; static const struct tzhead tzh0; static struct tzhead tzh; - zic_t *ats = emalloc(size_product(timecnt, sizeof *ats + 1)); - void *typesptr = ats + timecnt; + bool dir_checked = false; + zic_t one = 1; + zic_t y2038_boundary = one << 31; + ptrdiff_t nats = timecnt + WORK_AROUND_QTBUG_53071; + zic_t *ats = emalloc(size_product(nats, sizeof *ats + 1)); + void *typesptr = ats + nats; unsigned char *types = typesptr; /* @@ -1612,12 +1714,11 @@ writezone(const char *const name, const char *const string, char version) ** Optimize. */ { - int fromi; - int toi; + ptrdiff_t fromi, toi; toi = 0; fromi = 0; - while (fromi < timecnt && attypes[fromi].at < big_bang_time) + while (fromi < timecnt && attypes[fromi].at < early_time) ++fromi; for ( ; fromi < timecnt; ++fromi) { if (toi > 1 && ((attypes[fromi].at + @@ -1628,15 +1729,23 @@ writezone(const char *const name, const char *const string, char version) attypes[fromi].type; continue; } - if (toi == 0 || - attypes[toi - 1].type != attypes[fromi].type) + if (toi == 0 + || attypes[fromi].dontmerge + || attypes[toi - 1].type != attypes[fromi].type) attypes[toi++] = attypes[fromi]; } timecnt = toi; } - if (noise && timecnt > 1200) + + if (noise && timecnt > 1200) { + if (timecnt > TZ_MAX_TIMES) + warning(_("reference clients mishandle" + " more than %d transition times"), + TZ_MAX_TIMES); + else warning(_("pre-2014 clients may mishandle" " more than 1200 transition times")); + } /* ** Transfer. */ @@ -1644,6 +1753,19 @@ writezone(const char *const name, const char *const string, char version) ats[i] = attypes[i].at; types[i] = attypes[i].type; } + + /* Work around QTBUG-53071 for time stamps less than y2038_boundary - 1, + by inserting a no-op transition at time y2038_boundary - 1. + This works only for timestamps before the boundary, which + should be good enough in practice as QTBUG-53071 should be + long-dead by 2038. */ + if (WORK_AROUND_QTBUG_53071 && timecnt != 0 + && ats[timecnt - 1] < y2038_boundary - 1 && strchr(string, '<')) { + ats[timecnt] = y2038_boundary - 1; + types[timecnt] = types[timecnt - 1]; + timecnt++; + } + /* ** Correct for leap seconds. */ @@ -1681,50 +1803,58 @@ writezone(const char *const name, const char *const string, char version) --leapcnt32; ++leapi32; } - fullname = relname(directory, name); /* ** Remove old file, if any, to snap links. */ - if (itsdir(fullname) <= 0 && remove(fullname) != 0 && errno != ENOENT) { + if (remove(name) == 0) + dir_checked = true; + else if (errno != ENOENT) { const char *e = strerror(errno); - fprintf(stderr, _("%s: Can't remove %s: %s\n"), - progname, fullname, e); + fprintf(stderr, _("%s: Can't remove %s/%s: %s\n"), + progname, directory, name, e); exit(EXIT_FAILURE); } - if ((fp = fopen(fullname, "wb")) == NULL) { - if (! mkdirs(fullname)) - exit(EXIT_FAILURE); - if ((fp = fopen(fullname, "wb")) == NULL) { - const char *e = strerror(errno); - - fprintf(stderr, _("%s: Can't create %s: %s\n"), - progname, fullname, e); - exit(EXIT_FAILURE); - } + fp = fopen(name, "wb"); + if (!fp) { + int fopen_errno = errno; + if (fopen_errno == ENOENT && !dir_checked) { + mkdirs(name, true); + fp = fopen(name, "wb"); + fopen_errno = errno; + } + if (!fp) { + fprintf(stderr, _("%s: Can't create %s/%s: %s\n"), + progname, directory, name, strerror(fopen_errno)); + exit(EXIT_FAILURE); + } } for (pass = 1; pass <= 2; ++pass) { - register int thistimei, thistimecnt; - register int thisleapi, thisleapcnt; - register int thistimelim, thisleaplim; + register ptrdiff_t thistimei, thistimecnt, thistimelim; + register int thisleapi, thisleapcnt, thisleaplim; int writetype[TZ_MAX_TYPES]; int typemap[TZ_MAX_TYPES]; register int thistypecnt; char thischars[TZ_MAX_CHARS]; - char thischarcnt; + int thischarcnt; + bool toomanytimes; int indmap[TZ_MAX_CHARS]; if (pass == 1) { thistimei = timei32; thistimecnt = timecnt32; + toomanytimes = thistimecnt >> 31 >> 1 != 0; thisleapi = leapi32; thisleapcnt = leapcnt32; } else { thistimei = 0; thistimecnt = timecnt; + toomanytimes = thistimecnt >> 31 >> 31 >> 2 != 0; thisleapi = 0; thisleapcnt = leapcnt; } + if (toomanytimes) + error(_("too many transition times")); thistimelim = thistimei + thistimecnt; thisleaplim = thisleapi + thisleapcnt; for (i = 0; i < typecnt; ++i) @@ -1811,8 +1941,7 @@ writezone(const char *const name, const char *const string, char version) if (strcmp(&thischars[j], thisabbr) == 0) break; if (j == thischarcnt) { - strcpy(&thischars[(int) thischarcnt], - thisabbr); + strcpy(&thischars[thischarcnt], thisabbr); thischarcnt += strlen(thisabbr) + 1; } indmap[abbrinds[i]] = j; @@ -1894,9 +2023,8 @@ writezone(const char *const name, const char *const string, char version) putc(ttisgmts[i], fp); } fprintf(fp, "\n%s\n", string); - close_file(fp, fullname); + close_file(fp, directory, name); free(ats); - free(fullname); } static char const * @@ -2097,13 +2225,13 @@ rule_cmp(struct rule const *a, struct rule const *b) enum { YEAR_BY_YEAR_ZONE = 1 }; static int -stringzone(char *result, const struct zone *const zpfirst, const int zonecount) +stringzone(char *result, struct zone const *zpfirst, ptrdiff_t zonecount) { register const struct zone * zp; register struct rule * rp; register struct rule * stdrp; register struct rule * dstrp; - register int i; + register ptrdiff_t i; register const char * abbrvar; register int compat = 0; register int c; @@ -2216,11 +2344,11 @@ stringzone(char *result, const struct zone *const zpfirst, const int zonecount) } static void -outzone(const struct zone *zpfirst, int zonecount) +outzone(const struct zone *zpfirst, ptrdiff_t zonecount) { register const struct zone * zp; register struct rule * rp; - register int i, j; + register ptrdiff_t i, j; register bool usestart, useuntil; register zic_t starttime, untiltime; register zic_t gmtoff; @@ -2239,6 +2367,10 @@ outzone(const struct zone *zpfirst, int zonecount) register int compat; register bool do_extend; register char version; + ptrdiff_t lastatmax = -1; + zic_t one = 1; + zic_t y2038_boundary = one << 31; + zic_t max_year0; max_abbr_len = 2 + max_format_len + max_abbrvar_len; max_envvar_len = 2 * max_abbr_len + 5 * 9; @@ -2334,21 +2466,22 @@ outzone(const struct zone *zpfirst, int zonecount) } /* ** For the benefit of older systems, - ** generate data from 1900 through 2037. + ** generate data from 1900 through 2038. */ if (min_year > 1900) min_year = 1900; - if (max_year < 2037) - max_year = 2037; + max_year0 = max_year; + if (max_year < 2038) + max_year = 2038; for (i = 0; i < zonecount; ++i) { /* ** A guess that may well be corrected later. */ stdoff = 0; zp = &zpfirst[i]; - usestart = i > 0 && (zp - 1)->z_untiltime > big_bang_time; + usestart = i > 0 && (zp - 1)->z_untiltime > early_time; useuntil = i < (zonecount - 1); - if (useuntil && zp->z_untiltime <= big_bang_time) + if (useuntil && zp->z_untiltime <= early_time) continue; gmtoff = zp->z_gmtoff; eat(zp->z_filename, zp->z_linenum); @@ -2363,7 +2496,7 @@ outzone(const struct zone *zpfirst, int zonecount) if (usestart) { addtt(starttime, type); usestart = false; - } else addtt(big_bang_time, type); + } else addtt(early_time, type); } else for (year = min_year; year <= max_year; ++year) { if (useuntil && year > zp->z_untilrule.r_hiyear) break; @@ -2378,11 +2511,15 @@ outzone(const struct zone *zpfirst, int zonecount) rp->r_todo = year >= rp->r_loyear && year <= rp->r_hiyear && yearistype(year, rp->r_yrtype); - if (rp->r_todo) + if (rp->r_todo) { rp->r_temp = rpytime(rp, year); + rp->r_todo + = (rp->r_temp < y2038_boundary + || year <= max_year0); + } } for ( ; ; ) { - register int k; + register ptrdiff_t k; register zic_t jtime, ktime; register zic_t offset; @@ -2471,6 +2608,10 @@ outzone(const struct zone *zpfirst, int zonecount) offset = oadd(zp->z_gmtoff, rp->r_stdoff); type = addtype(offset, ab, rp->r_stdoff != 0, rp->r_todisstd, rp->r_todisgmt); + if (rp->r_hiyear == ZIC_MAX + && ! (0 <= lastatmax + && ktime < attypes[lastatmax].at)) + lastatmax = timecnt; addtt(ktime, type); } } @@ -2502,6 +2643,8 @@ error(_("can't determine time zone abbreviation to use just after until time")); starttime = tadd(starttime, -gmtoff); } } + if (0 <= lastatmax) + attypes[lastatmax].dontmerge = true; if (do_extend) { /* ** If we're extending the explicitly listed observations @@ -2523,21 +2666,8 @@ error(_("can't determine time zone abbreviation to use just after until time")); if (attypes[i].at > lastat->at) lastat = &attypes[i]; if (lastat->at < rpytime(&xr, max_year - 1)) { - /* - ** Create new type code for the redundant entry, - ** to prevent it being optimized away. - */ - if (typecnt >= TZ_MAX_TYPES) { - error(_("too many local time types")); - exit(EXIT_FAILURE); - } - gmtoffs[typecnt] = gmtoffs[lastat->type]; - isdsts[typecnt] = isdsts[lastat->type]; - ttisstds[typecnt] = ttisstds[lastat->type]; - ttisgmts[typecnt] = ttisgmts[lastat->type]; - abbrinds[typecnt] = abbrinds[lastat->type]; - ++typecnt; addtt(rpytime(&xr, max_year + 1), typecnt-1); + attypes[timecnt - 1].dontmerge = true; } } writezone(zpfirst->z_name, envvar, version); @@ -2549,8 +2679,8 @@ error(_("can't determine time zone abbreviation to use just after until time")); static void addtt(zic_t starttime, int type) { - if (starttime <= big_bang_time || - (timecnt == 1 && attypes[0].at < big_bang_time)) { + if (starttime <= early_time + || (timecnt == 1 && attypes[0].at < early_time)) { gmtoffs[0] = gmtoffs[type]; isdsts[0] = isdsts[type]; ttisstds[0] = ttisstds[type]; @@ -2565,6 +2695,7 @@ addtt(zic_t starttime, int type) } attypes = growalloc(attypes, sizeof *attypes, timecnt, &timecnt_alloc); attypes[timecnt].at = starttime; + attypes[timecnt].dontmerge = false; attypes[timecnt].type = type; ++timecnt; } @@ -2657,28 +2788,48 @@ adjleap(void) } } +static char * +shellquote(char *b, char const *s) +{ + *b++ = '\''; + while (*s) { + if (*s == '\'') + *b++ = '\'', *b++ = '\\', *b++ = '\''; + *b++ = *s++; + } + *b++ = '\''; + return b; +} + static bool -yearistype(int year, const char *type) +yearistype(zic_t year, const char *type) { - static char * buf; - int result; + char *buf; + char *b; + int result; if (type == NULL || *type == '\0') return true; - buf = erealloc(buf, 132 + strlen(yitcommand) + strlen(type)); - sprintf(buf, "%s %d %s", yitcommand, year, type); + buf = emalloc(1 + 4 * strlen(yitcommand) + 2 + + INT_STRLEN_MAXIMUM(zic_t) + 2 + 4 * strlen(type) + 2); + b = shellquote(buf, yitcommand); + *b++ = ' '; + b += sprintf(b, "%"PRIdZIC, year); + *b++ = ' '; + b = shellquote(b, type); + *b = '\0'; result = system(buf); - if (WIFEXITED(result)) switch (WEXITSTATUS(result)) { - case 0: - return true; - case 1: - return false; + if (WIFEXITED(result)) { + int status = WEXITSTATUS(result); + if (status <= 1) { + free(buf); + return status == 0; + } } error(_("Wild result from command execution")); fprintf(stderr, _("%s: command was '%s', result was %d\n"), progname, buf, result); - for ( ; ; ) - exit(EXIT_FAILURE); + exit(EXIT_FAILURE); } /* Is A a space character in the C locale? */ @@ -2806,10 +2957,8 @@ getfields(register char *cp) if (*dp != '\0') ++dp; else { - error(_( - "Odd number of quotation marks" - )); - exit(1); + error(_("Odd number of quotation marks")); + exit(EXIT_FAILURE); } } while (*cp && *cp != '#' && !is_space(*cp)); if (is_space(*cp)) @@ -2972,44 +3121,49 @@ mp = _("time zone abbreviation differs from POSIX standard"); charcnt += i; } -static bool -mkdirs(char *argname) +/* Ensure that the directories of ARGNAME exist, by making any missing + ones. If ANCESTORS, do this only for ARGNAME's ancestors; otherwise, + do it for ARGNAME too. Exit with failure if there is trouble. + Do not consider an existing non-directory to be trouble. */ +static void +mkdirs(char const *argname, bool ancestors) { register char * name; register char * cp; - if (argname == NULL || *argname == '\0') - return true; cp = name = ecpyalloc(argname); - while ((cp = strchr(cp + 1, '/')) != 0) { - *cp = '\0'; + + /* Do not mkdir a root directory, as it must exist. */ #ifdef HAVE_DOS_FILE_NAMES - /* - ** DOS drive specifier? - */ - if (is_alpha(name[0]) && name[1] == ':' && name[2] == '\0') { - *cp = '/'; - continue; - } + if (is_alpha(name[0]) && name[1] == ':') + cp += 2; #endif + while (*cp == '/') + cp++; + + while (cp && ((cp = strchr(cp, '/')) || !ancestors)) { + if (cp) + *cp = '\0'; /* ** Try to create it. It's OK if creation fails because ** the directory already exists, perhaps because some - ** other process just created it. + ** other process just created it. For simplicity do + ** not check first whether it already exists, as that + ** is checked anyway if the mkdir fails. */ if (mkdir(name, MKDIR_UMASK) != 0) { + /* For speed, skip itsdir if errno == EEXIST. Since + mkdirs is called only after open fails with ENOENT + on a subfile, EEXIST implies itsdir here. */ int err = errno; - if (itsdir(name) <= 0) { - char const *e = strerror(err); - warning(_("%s: Can't create directory" - " %s: %s"), - progname, name, e); - free(name); - return false; + if (err != EEXIST && !itsdir(name)) { + error(_("%s: Can't create directory %s: %s"), + progname, name, strerror(err)); + exit(EXIT_FAILURE); } } - *cp = '/'; + if (cp) + *cp++ = '/'; } free(name); - return true; } -- cgit 1.4.1