• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #define _GNU_SOURCE
2 #define __CRT__NO_INLINE
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdarg.h>
7 
asprintf(char ** __restrict__ ret,const char * __restrict__ format,...)8 int asprintf(char ** __restrict__ ret,
9                      const char * __restrict__ format,
10                      ...) {
11   va_list ap;
12   int len;
13   va_start(ap,format);
14   /* Get Length */
15   len = _vsnprintf(NULL,0,format,ap);
16   if (len < 0) goto _end;
17   /* +1 for \0 terminator. */
18   *ret = malloc(len + 1);
19   /* Check malloc fail*/
20   if (!*ret) {
21     len = -1;
22     goto _end;
23   }
24   /* Write String */
25   _vsnprintf(*ret,len+1,format,ap);
26   /* Terminate explicitly */
27   (*ret)[len] = '\0';
28   _end:
29   va_end(ap);
30   return len;
31 }
32