#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>


#ifdef __cplusplus
extern "C" {
#endif
int vasprintf(char** strp, const char* fmt, va_list ap);
#ifdef __cplusplus
}
#endif


int vasprintf( char** strp, const char* fmt, va_list ap ) { 
	int len; char *buf; 
	len = vsnprintf(NULL, 0, fmt, ap);  /* get needed size */ 
	if (len < 0) return -1; 
	buf = malloc(len + 1);  /* reserve 1 byte for trailing \0 */
	*strp = buf; 	
	if( vsnprintf(buf, len + 1, fmt, ap) == len) 
		return len; 
	free(buf);  /* something went wrong in second vsnprintf() */ 
	return -1; 
}