Skip to content

Instantly share code, notes, and snippets.

@spedru
Created May 16, 2015 02:50
Show Gist options
  • Save spedru/d2460411cab52580a733 to your computer and use it in GitHub Desktop.
Save spedru/d2460411cab52580a733 to your computer and use it in GitHub Desktop.
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
int vasprintf(char **strp, const char *fmt, va_list ap)
{
char buf;
va_list ap2;
va_copy(ap2, ap);
int len = vsnprintf(&buf, 1, fmt, ap) + 1;
return len < 1 || !(*strp = malloc(len))
? (*strp = NULL), -1
: vsprintf(*strp, fmt, ap2);
}
int asprintf(char **strp, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
int ret = vasprintf(strp, fmt, args);
va_end(args);
return ret;
}
int main()
{
char *benis;
asprintf(&benis, "%d %d %d", 1, 1, 1);
puts(benis);
free(benis);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment