zig/lib/libc/mingw/stdio/asprintf.c
Andrew Kelley 49d1a4c562 move lib dirs to lib subdir
also start prefering NtDll API. so far:
 * NtQueryInformationFile
 * NtClose

adds a performance workaround for windows unicode conversion. but that
should probably be removed before merging
2019-07-15 17:54:50 -04:00

32 lines
634 B
C

#define _GNU_SOURCE
#define __CRT__NO_INLINE
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
int asprintf(char ** __restrict__ ret,
const char * __restrict__ format,
...) {
va_list ap;
int len;
va_start(ap,format);
/* Get Length */
len = _vsnprintf(NULL,0,format,ap);
if (len < 0) goto _end;
/* +1 for \0 terminator. */
*ret = malloc(len + 1);
/* Check malloc fail*/
if (!*ret) {
len = -1;
goto _end;
}
/* Write String */
_vsnprintf(*ret,len+1,format,ap);
/* Terminate explicitly */
(*ret)[len] = '\0';
_end:
va_end(ap);
return len;
}