| 1 | // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors. |
| 2 | // SPDX-License-Identifier: mit |
| 3 | |
| 4 | #include <stdarg.h> |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | |
| 8 | #include "compat.h" |
| 9 | |
| 10 | int asprintf(char **strp, const char *fmt, ...) { |
| 11 | va_list ap; |
| 12 | int len; |
| 13 | |
| 14 | va_start(ap, fmt); |
| 15 | len = _vscprintf(fmt, ap); |
| 16 | va_end(ap); |
| 17 | |
| 18 | *strp = malloc(len + 1); |
| 19 | if (!*strp) { |
| 20 | return -1; |
| 21 | } |
| 22 | |
| 23 | va_start(ap, fmt); |
| 24 | _vsnprintf(*strp, len + 1, fmt, ap); |
| 25 | va_end(ap); |
| 26 | |
| 27 | (*strp)[len] = 0; |
| 28 | return len; |
| 29 | } |