| 1 | #define _GNU_SOURCE |
| 2 | #define __CRT__NO_INLINE |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <stdarg.h> |
| 7 | |
| 8 | int vasprintf(char ** __restrict__ ret, |
| 9 | const char * __restrict__ format, |
| 10 | va_list ap) { |
| 11 | int len; |
| 12 | /* Get Length */ |
| 13 | len = _vscprintf(format,ap); |
| 14 | if (len < 0) return -1; |
| 15 | /* +1 for \0 terminator. */ |
| 16 | *ret = malloc(len + 1); |
| 17 | /* Check malloc fail*/ |
| 18 | if (!*ret) return -1; |
| 19 | /* Write String */ |
| 20 | _vsnprintf(*ret,len+1,format,ap); |
| 21 | /* Terminate explicitly */ |
| 22 | (*ret)[len] = '\0'; |
| 23 | return len; |
| 24 | } |
| 25 | |