2010-05-11 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / eglib / src / vasprintf.c
blob8699df289bc98612a9fcc8602d747345cbbde459
1 #include <stdarg.h>
2 #include <stdio.h>
3 #include <stdlib.h>
5 int vasprintf(char **ret, const char *fmt, va_list ap)
7 char *buf;
8 int len;
9 size_t buflen;
10 va_list ap2;
12 #ifdef _MSC_VER
13 ap2 = ap;
14 len = _vscprintf(fmt, ap2); // NOTE MS specific extension ( :-( )
15 #else
16 va_copy(ap2, ap);
17 len = vsnprintf(NULL, 0, fmt, ap2);
18 #endif
20 if (len > 0 && (buf = malloc((buflen = (size_t) (len + 1)))) != NULL) {
21 len = vsnprintf(buf, buflen, fmt, ap);
22 *ret = buf;
23 } else {
24 *ret = NULL;
25 len = -1;
28 va_end(ap2);
29 return len;