rework langinfo code for ABI compat and for use by time code
[musl] / src / stdio / vasprintf.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <stdarg.h>
4 #include <stdlib.h>
5
6 #define GUESS 240U
7
8 int vasprintf(char **s, const char *fmt, va_list ap)
9 {
10         va_list ap2;
11         char *a;
12         int l=GUESS;
13
14         if (!(a=malloc(GUESS))) return -1;
15
16         va_copy(ap2, ap);
17         l=vsnprintf(a, GUESS, fmt, ap2);
18         va_end(ap2);
19
20         if (l<GUESS) {
21                 char *b = realloc(a, l+1U);
22                 *s = b ? b : a;
23                 return l;
24         }
25         free(a);
26         if (l<0 || !(*s=malloc(l+1U))) return -1;
27         return vsnprintf(*s, l+1U, fmt, ap);
28 }