From ad9dd8dc1a41af734a3dae4b6ff0e38821227959 Mon Sep 17 00:00:00 2001
From: Dimitri Papadopoulos
 <3234522+DimitriPapadopoulos@users.noreply.github.com>
Date: Mon, 3 Jun 2024 22:01:42 +0200
Subject: [PATCH] Support older mac0S versions that lack vdprintf()

Define and call an alternative vdprintf(), only
if the genuine vdprintf() does not exist.
---
 configure.ac    |  6 +++++-
 src/userinput.c | 16 ++++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/configure.ac b/configure.ac
index 81ed3247..f031aa7a 100644
--- configure.ac
+++ configure.ac
@@ -166,7 +166,11 @@ write \
 ], [], AC_MSG_ERROR([Required function not found]))
 
 # Checks for optional functions.
-AC_CHECK_FUNCS([pthread_mutexattr_setrobust])
+AC_CHECK_FUNCS([ \
+pthread_mutexattr_setrobust \
+vdprintf \
+])
+
 # Use PKG_CHECK_MODULES compiler/linker flags
 save_openssl_CPPFLAGS="${CPPFLAGS}"
 save_openssl_LIBS="${LIBS}"
diff --git a/src/userinput.c b/src/userinput.c
index ec19dbe2..53a50255 100644
--- src/userinput.c
+++ src/userinput.c
@@ -167,6 +167,22 @@ static int pinentry_read(int from, char **retstr)
 	return -1;
 }
 
+#ifndef HAVE_VDPRINTF
+static int vdprintf(int fd, const char *format, va_list ap)
+{
+	char buffer[2049];
+	int size = vsnprintf(buffer, sizeof(buffer), format, ap);
+
+	if (size < 0)
+		return size;
+
+	if (size >= sizeof(buffer)) // silently discard beyond the buffer size
+		size = sizeof(buffer) - 1;
+
+	return (int) write(fd, buffer, size);
+}
+#endif
+
 static int pinentry_exchange(int to, int from, char **retstr,
                              const char *format, ...)
 {
