| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include "stdio-shim.h" |
| 8 |
#include <stdlib.h> |
| 9 |
#include <limits.h> |
| 10 |
|
| 11 |
#ifdef _MSC_VER |
| 12 |
int vasprintf(char** strp, const char* fmt, va_list ap) |
| 13 |
{ |
| 14 |
#ifdef _WIN32 |
| 15 |
int size = _vscprintf(fmt, ap); |
| 16 |
#else |
| 17 |
|
| 18 |
va_list apcopy; |
| 19 |
va_copy(apcopy, ap); |
| 20 |
int size = vsnprintf(NULL, 0, fmt, apcopy); |
| 21 |
va_end(apcopy); |
| 22 |
#endif |
| 23 |
if ((size < 0) || (size >= INT_MAX)) { |
| 24 |
goto fail; |
| 25 |
} |
| 26 |
|
| 27 |
char *str = malloc(size + 1); |
| 28 |
if (str == NULL) { |
| 29 |
goto fail; |
| 30 |
} |
| 31 |
|
| 32 |
int r = vsnprintf(str, size + 1, fmt, ap); |
| 33 |
if ((r < 0) || (r > size)) { |
| 34 |
free(str); |
| 35 |
goto fail; |
| 36 |
} |
| 37 |
|
| 38 |
*strp = str; |
| 39 |
return r; |
| 40 |
|
| 41 |
fail: |
| 42 |
*strp = NULL; |
| 43 |
return -1; |
| 44 |
} |
| 45 |
|
| 46 |
int asprintf(char** strp, const char* fmt, ...) |
| 47 |
{ |
| 48 |
int r; |
| 49 |
va_list ap; |
| 50 |
va_start(ap, fmt); |
| 51 |
r = vasprintf(strp, fmt, ap); |
| 52 |
va_end(ap); |
| 53 |
return r; |
| 54 |
} |
| 55 |
#endif |