>From 4c2f5b49c965271b263007f56657ac8c21f3c2aa Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Fri, 26 Jan 2018 18:41:40 -0800
Subject: [PROPOSEDC] Port better to native MS-Windows

Inspired by a patch proposed by Manuela Friedrich in:
https://mm.icann.org/pipermail/tz/2018-January/026016.html
* NEWS: Mention this.
* private.h (HAVE_STRTOLL): Default to 1.
(strtoimax) [!HAVE_STRTOLL && !defined strtoimax]:
Define to strtol, not strtoll.
* zic.c (dolink): If the call to symlink fails with errno ==
ENOTSUP, create the parent directory of the destination anyway,
since it is needed before the later call to fopen.
---
 Makefile  |  1 +
 NEWS      |  5 +++++
 private.h | 12 ++++++++++--
 zic.c     |  6 ++++--
 4 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/Makefile b/Makefile
index fad1dbe..8c84cd9 100644
--- a/Makefile
+++ b/Makefile
@@ -189,6 +189,7 @@ LDLIBS=
 #  -DHAVE_STDINT_H if you have a non-C99 compiler with <stdint.h>
 #  -DHAVE_STRFTIME_L if <time.h> declares locale_t and strftime_l
 #  -DHAVE_STRDUP=0 if your system lacks the strdup function
+#  -DHAVE_STRTOLL=0 if your system lacks the strtoll function
 #  -DHAVE_SYMLINK=0 if your system lacks the symlink function
 #  -DHAVE_SYS_STAT_H=0 if your compiler lacks a <sys/stat.h>
 #  -DHAVE_SYS_WAIT_H=0 if your compiler lacks a <sys/wait.h>
diff --git a/NEWS b/NEWS
index 36dba18..4f763c0 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,11 @@ News for the tz database
 
 Unreleased, experimental changes
 
+  Changes to code
+
+    The code is a bit more portable to MS-Windows.  (Thanks to Manuela
+    Friedrich).
+
   Changes to documentation and commentary
 
     theory.html now has a section "POSIX features no longer needed"
diff --git a/private.h b/private.h
index 2e8415e..ffb8121 100644
--- a/private.h
+++ b/private.h
@@ -66,6 +66,10 @@
 #define HAVE_STRDUP 1
 #endif
 
+#ifndef HAVE_STRTOLL
+#define HAVE_STRTOLL 1
+#endif
+
 #ifndef HAVE_SYMLINK
 #define HAVE_SYMLINK		1
 #endif /* !defined HAVE_SYMLINK */
@@ -266,15 +270,19 @@ typedef int int_fast32_t;
 #ifndef INTMAX_MAX
 # ifdef LLONG_MAX
 typedef long long intmax_t;
-#  define strtoimax strtoll
+#  if HAVE_STRTOLL
+#   define strtoimax strtoll
+#  endif
 #  define INTMAX_MAX LLONG_MAX
 #  define INTMAX_MIN LLONG_MIN
 # else
 typedef long intmax_t;
-#  define strtoimax strtol
 #  define INTMAX_MAX LONG_MAX
 #  define INTMAX_MIN LONG_MIN
 # endif
+# ifndef strtoimax
+#  define strtoimax strtol
+# endif
 #endif
 
 #ifndef PRIdMAX
diff --git a/zic.c b/zic.c
index 304410f..dc1a612 100644
--- a/zic.c
+++ b/zic.c
@@ -893,9 +893,11 @@ dolink(char const *fromfield, char const *tofield, bool staysymlink)
 	  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) {
+	  if (!todirs_made
+	      && (symlink_errno == ENOENT || symlink_errno == ENOTSUP)) {
 	    mkdirs(tofield, true);
-	    symlink_errno = symlink(contents, tofield) == 0 ? 0 : errno;
+	    if (symlink_errno == ENOENT)
+	      symlink_errno = symlink(contents, tofield) == 0 ? 0 : errno;
 	  }
 	  free(linkalloc);
 	  if (symlink_errno == 0) {
-- 
2.14.3

