>From 399180580f65bcad63cf996997068366cadaf530 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Mon, 12 Jun 2017 22:14:22 -0700
Subject: [PATCH 3/4] Port zdump.c back to C89

Do not assume snprintf, since it was not standardized until C99.
* NEWS, Makefile: Mention this
* asctime.c: Resurrect comment about snprintf.
* zdump.c (HAVE_SNPRINTF): New symbol.
(snprintf) [!HAVE_SNPRINTF]: New function.
---
 Makefile  |  1 +
 NEWS      |  2 ++
 asctime.c |  3 +++
 zdump.c   | 36 ++++++++++++++++++++++++++++++++++++
 4 files changed, 42 insertions(+)

diff --git a/Makefile b/Makefile
index 65915ea..c02e149 100644
--- a/Makefile
+++ b/Makefile
@@ -147,6 +147,7 @@ LDLIBS=
 #	localtime_rz can make zdump significantly faster, but is nonstandard.
 #  -DHAVE_POSIX_DECLS=0 if your system's include files do not declare
 #	functions like 'link' or variables like 'tzname' required by POSIX
+#  -DHAVE_SNPRINTF=0 if your system lacks the snprintf function
 #  -DHAVE_STDBOOL_H if you have a non-C99 compiler with <stdbool.h>
 #  -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
diff --git a/NEWS b/NEWS
index 0aa2e3f..81d702d 100644
--- a/NEWS
+++ b/NEWS
@@ -86,6 +86,8 @@ Unreleased, experimental changes
     localtime.c and difftime.c no longer require stdio.h, and .c files
     other than zic.c no longer require sys/wait.h.
 
+    zdump.c no longer assumes snprintf.  (
+
     Several minor changes have been made to the code to make it a
     bit easier to port to MS-Windows and Solaris.  (Thanks to Kees
     Dekker for reporting the problems.)
diff --git a/asctime.c b/asctime.c
index edd6875..a55f785 100644
--- a/asctime.c
+++ b/asctime.c
@@ -100,6 +100,9 @@ asctime_r(register const struct tm *timeptr, char *buf)
 	** (e.g., timeptr->tm_mday) when processing "%Y".
 	*/
 	strftime(year, sizeof year, "%Y", timeptr);
+	/*
+	** We avoid using snprintf since it's not available on all systems.
+	*/
 	sprintf(result,
 		((strlen(year) <= 4) ? ASCTIME_FMT : ASCTIME_FMT_B),
 		wn, mn,
diff --git a/zdump.c b/zdump.c
index 38a13bc..8e3bf3e 100644
--- a/zdump.c
+++ b/zdump.c
@@ -22,6 +22,10 @@
 #include "private.h"
 #include <stdio.h>
 
+#ifndef HAVE_SNPRINTF
+# define HAVE_SNPRINTF (199901 <= __STDC_VERSION__)
+#endif
+
 #ifndef HAVE_LOCALTIME_R
 # define HAVE_LOCALTIME_R 1
 #endif
@@ -791,6 +795,38 @@ show(timezone_t tz, char *zone, time_t t, bool v)
 		abbrok(abbr(tmp), zone);
 }
 
+#if !HAVE_SNPRINTF
+# include <stdarg.h>
+
+/* A substitute for snprintf that is good enough for zdump.  */
+static int
+snprintf(char *s, size_t size, char const *format, ...)
+{
+  int n;
+  va_list args;
+  char const *arg;
+  size_t arglen, slen;
+  char buf[1024];
+  va_start(args, format);
+  if (strcmp(format, "%s") == 0) {
+    arg = va_arg(args, char const *);
+    arglen = strlen(arg);
+  } else {
+    n = vsprintf(buf, format, args);
+    if (n < 0)
+      return n;
+    arg = buf;
+    arglen = n;
+  }
+  slen = arglen < size ? arglen : size - 1;
+  memcpy(s, arg, slen);
+  s[slen] = '\0';
+  n = arglen <= INT_MAX ? arglen : -1;
+  va_end(args);
+  return n;
+}
+#endif
+
 /* Store into BUF, of size SIZE, a formatted local time taken from *TM.
    Use ISO 8601 format +HH:MM:SS.  Omit :SS if SS is zero, and omit
    :MM too if MM is also zero.
-- 
2.9.4

