aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stdio-consolesafe.c
diff options
context:
space:
mode:
authorPaul Eggert2025-11-06 12:11:48 -0800
committerPaul Eggert2025-11-06 12:12:24 -0800
commit50a1929f6c0a8509b0b695b3aac25fbf70b8ffd6 (patch)
tree9e747c68f69dc3d4f6b38fa2e87c09867264485e /lib/stdio-consolesafe.c
parent7654ec5e953f25499cfabe7da08e10d94379781f (diff)
downloademacs-50a1929f6c0a8509b0b695b3aac25fbf70b8ffd6.tar.gz
emacs-50a1929f6c0a8509b0b695b3aac25fbf70b8ffd6.zip
Update from Gnulib by running admin/merge-gnulib
Diffstat (limited to 'lib/stdio-consolesafe.c')
-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)
75 75
76# include "fseterr.h" 76# include "fseterr.h"
77 77
78# if !HAVE_VASPRINTF
79
80# include <errno.h>
81# include <stdarg.h>
82
83/* The old mingw (before mingw-w64) does not have the vasprintf function.
84 Define a suitable replacement here, that supports the same format
85 specifiers as the mingw *printf functions. */
86
87static int
88vasprintf (char **resultp, const char *format, va_list args)
89{
90 /* First try: Use a stack-allocated buffer. */
91 char buf[2048];
92 size_t bufsize = sizeof (buf);
93 int ret = __mingw_vsnprintf (buf, bufsize, format, args);
94 if (ret < 0)
95 return -1;
96 size_t nbytes = ret;
97 char *mem = (char *) malloc (nbytes + 1);
98 if (mem == NULL)
99 {
100 errno = ENOMEM;
101 return -1;
102 }
103 if (ret < bufsize)
104 {
105 /* The buffer was sufficiently large. */
106 memcpy (mem, buf, nbytes + 1);
107 }
108 else
109 {
110 /* Second try: Use the heap-allocated memory. */
111 ret = __mingw_vsnprintf (mem, nbytes + 1, format, args);
112 if (ret < 0)
113 {
114 int saved_errno = errno;
115 free (mem);
116 errno = saved_errno;
117 return -1;
118 }
119 if (ret != nbytes)
120 abort ();
121 }
122 *resultp = mem;
123 return nbytes;
124}
125
126# endif
127
78/* Bypass the functions __mingw_[v][f]printf, that trigger a bug in msvcrt, 128/* Bypass the functions __mingw_[v][f]printf, that trigger a bug in msvcrt,
79 but without losing the support for modern format specifiers added by 129 but without losing the support for modern format specifiers added by
80 __mingw_*printf. */ 130 __mingw_*printf. */