Enabled g_mem_set_vtable through the configure option --with-overridable-allocators...
[mono.git] / eglib / src / vasprintf.c
1 #include <stdarg.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <glib.h>
5
6 gint g_vasprintf (gchar **ret, const gchar *fmt, va_list ap)
7 {
8         char *buf;
9         int len;
10         size_t buflen;
11         va_list ap2;
12         
13 #if defined(_MSC_VER) || defined(__MINGW64_VERSION_MAJOR)
14         ap2 = ap;
15         len = _vscprintf(fmt, ap2); // NOTE MS specific extension ( :-( )
16 #else
17         va_copy(ap2, ap);
18         len = vsnprintf(NULL, 0, fmt, ap2);
19 #endif
20         
21         if (len >= 0 && (buf = g_malloc ((buflen = (size_t) (len + 1)))) != NULL) {
22                 len = vsnprintf(buf, buflen, fmt, ap);
23                 *ret = buf;
24         } else {
25                 *ret = NULL;
26                 len = -1;
27         }
28         
29         va_end(ap2);
30         return len;
31 }
32