1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2025-01-10 17:32:05 +05:30
gists/c-programming/asprintf.c
2024-01-28 13:43:56 +03:00

36 lines
559 B
C

/*
* asprintf.c
*
* Author: Intel A80486DX2-66
* License: Creative Commons Zero 1.0 Universal
*/
#include "asprintf.h"
ssize_t asprintf(char** strp, char* format, ...) {
va_list args;
va_start(args, format);
ssize_t size = (ssize_t) vsnprintf(NULL, 0, format, args);
if (size < 0) {
va_end(args);
return -1;
}
*strp = malloc(++size);
if (*strp == NULL) {
va_end(args);
return -1;
}
ssize_t result = (ssize_t) vsnprintf(*strp, size, format, args);
va_end(args);
if (result < 0) {
free(*strp);
return -1;
}
return result;
}