1 #ifndef __vasprintf_compat_h
2 #define __vasprintf_compat_h
3
4 /**
5 * @file
6 * @brief Do not use, json-c internal, may be changed or removed at any time.
7 */
8
9 #include "snprintf_compat.h"
10
11 /* CAW: compliant version of vasprintf */
json_vasprintf(char ** buf,const char * fmt,va_list ap)12 static int json_vasprintf(char **buf, const char *fmt, va_list ap)
13 {
14 #ifndef WIN32
15 static char _T_emptybuffer = '\0';
16 #endif /* !defined(WIN32) */
17 int chars;
18 char *b;
19 va_list ap2;
20
21 if(!buf) { return -1; }
22
23 va_copy(ap2, ap);
24 #ifdef WIN32
25 chars = _vscprintf(fmt, ap2)+1;
26 #else /* !defined(WIN32) */
27 /* CAW: RAWR! We have to hope to god here that vsnprintf doesn't overwrite
28 our buffer like on some 64bit sun systems.... but hey, its time to move on */
29 chars = vsnprintf(&_T_emptybuffer, 0, fmt, ap2)+1;
30 if(chars < 0) { chars *= -1; } /* CAW: old glibc versions have this problem */
31 #endif /* defined(WIN32) */
32 va_end(ap2);
33
34 b = (char*)malloc(sizeof(char)*chars);
35 if(!b) { return -1; }
36
37 if((chars = vsprintf(b, fmt, ap)) < 0)
38 {
39 free(b);
40 } else {
41 *buf = b;
42 }
43
44 return chars;
45 }
46
47 #endif /* __vasprintf_compat_h */
48