asprintf.c

asprintf polyfill, regardless of _GNU_SOURCE
git clone git://git.finwo.net/lib/asprintf.c
Log | Files | Refs

asprintf.c (425B)


      1 #ifndef _GNU_SOURCE
      2 #include <stdarg.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 
      6 // Polyfill asprintf, comes from stdio when _GNU_SOURCE is defined
      7 int asprintf(char **restrict strp, const char *restrict fmt, ...) {
      8   va_list argptr;
      9   va_start(argptr, fmt);
     10   int size = vsnprintf(NULL, 0, fmt, argptr);
     11   *strp = realloc(*strp, size + 1);
     12   vsnprintf(*strp, size + 1, fmt, argptr);
     13   va_end(argptr);
     14   return size;
     15 }
     16 #endif