summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2025-11-06 12:11:48 -0800
committerPaul Eggert <eggert@cs.ucla.edu>2025-11-06 12:12:24 -0800
commit50a1929f6c0a8509b0b695b3aac25fbf70b8ffd6 (patch)
tree9e747c68f69dc3d4f6b38fa2e87c09867264485e /lib
parent7654ec5e953f25499cfabe7da08e10d94379781f (diff)
Update from Gnulib by running admin/merge-gnulib
Diffstat (limited to 'lib')
-rw-r--r--lib/stdio-consolesafe.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/stdio-consolesafe.c b/lib/stdio-consolesafe.c
index fbea20be224..b5ca8cc0125 100644
--- a/lib/stdio-consolesafe.c
+++ b/lib/stdio-consolesafe.c
@@ -75,6 +75,56 @@ gl_consolesafe_fwrite (const void *ptr, size_t size, size_t nmemb, FILE *fp)
# include "fseterr.h"
+# if !HAVE_VASPRINTF
+
+# include <errno.h>
+# include <stdarg.h>
+
+/* The old mingw (before mingw-w64) does not have the vasprintf function.
+ Define a suitable replacement here, that supports the same format
+ specifiers as the mingw *printf functions. */
+
+static int
+vasprintf (char **resultp, const char *format, va_list args)
+{
+ /* First try: Use a stack-allocated buffer. */
+ char buf[2048];
+ size_t bufsize = sizeof (buf);
+ int ret = __mingw_vsnprintf (buf, bufsize, format, args);
+ if (ret < 0)
+ return -1;
+ size_t nbytes = ret;
+ char *mem = (char *) malloc (nbytes + 1);
+ if (mem == NULL)
+ {
+ errno = ENOMEM;
+ return -1;
+ }
+ if (ret < bufsize)
+ {
+ /* The buffer was sufficiently large. */
+ memcpy (mem, buf, nbytes + 1);
+ }
+ else
+ {
+ /* Second try: Use the heap-allocated memory. */
+ ret = __mingw_vsnprintf (mem, nbytes + 1, format, args);
+ if (ret < 0)
+ {
+ int saved_errno = errno;
+ free (mem);
+ errno = saved_errno;
+ return -1;
+ }
+ if (ret != nbytes)
+ abort ();
+ }
+ *resultp = mem;
+ return nbytes;
+}
+
+# endif
+
/* Bypass the functions __mingw_[v][f]printf, that trigger a bug in msvcrt,
but without losing the support for modern format specifiers added by
__mingw_*printf. */