2024-01-25 23:03:58 +05:30
|
|
|
/*
|
|
|
|
* asprintf.c
|
|
|
|
*
|
|
|
|
* Author: Intel A80486DX2-66
|
|
|
|
* License: Creative Commons Zero 1.0 Universal
|
|
|
|
*/
|
|
|
|
|
2024-01-15 22:12:17 +05:30
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2024-01-28 16:11:49 +05:30
|
|
|
*strp = malloc(++size);
|
2024-01-15 22:12:17 +05:30
|
|
|
if (*strp == NULL) {
|
|
|
|
va_end(args);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-01-28 16:11:49 +05:30
|
|
|
ssize_t result = (ssize_t) vsnprintf(*strp, size, format, args);
|
2024-01-15 22:12:17 +05:30
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (result < 0) {
|
|
|
|
free(*strp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|