157e22627SCy Schubert #include <stdio.h> 257e22627SCy Schubert #include <stdlib.h> 357e22627SCy Schubert #include <stdarg.h> 457e22627SCy Schubert 557e22627SCy Schubert #include "portability.h" 657e22627SCy Schubert 757e22627SCy Schubert int 8*afdbf109SJoseph Mingrone pcapint_vasprintf(char **strp, const char *format, va_list args) 957e22627SCy Schubert { 1057e22627SCy Schubert int len; 1157e22627SCy Schubert size_t str_size; 1257e22627SCy Schubert char *str; 1357e22627SCy Schubert int ret; 1457e22627SCy Schubert 1557e22627SCy Schubert len = _vscprintf(format, args); 1657e22627SCy Schubert if (len == -1) { 1757e22627SCy Schubert *strp = NULL; 1857e22627SCy Schubert return (-1); 1957e22627SCy Schubert } 2057e22627SCy Schubert str_size = len + 1; 2157e22627SCy Schubert str = malloc(str_size); 2257e22627SCy Schubert if (str == NULL) { 2357e22627SCy Schubert *strp = NULL; 2457e22627SCy Schubert return (-1); 2557e22627SCy Schubert } 266f9cba8fSJoseph Mingrone ret = vsnprintf(str, str_size, format, args); 2757e22627SCy Schubert if (ret == -1) { 2857e22627SCy Schubert free(str); 2957e22627SCy Schubert *strp = NULL; 3057e22627SCy Schubert return (-1); 3157e22627SCy Schubert } 3257e22627SCy Schubert *strp = str; 3357e22627SCy Schubert /* 346f9cba8fSJoseph Mingrone * vsnprintf() shouldn't truncate the string, as we have 3557e22627SCy Schubert * allocated a buffer large enough to hold the string, so its 3657e22627SCy Schubert * return value should be the number of characters printed. 3757e22627SCy Schubert */ 3857e22627SCy Schubert return (ret); 3957e22627SCy Schubert } 4057e22627SCy Schubert 4157e22627SCy Schubert int 42*afdbf109SJoseph Mingrone pcapint_asprintf(char **strp, const char *format, ...) 4357e22627SCy Schubert { 4457e22627SCy Schubert va_list args; 4557e22627SCy Schubert int ret; 4657e22627SCy Schubert 4757e22627SCy Schubert va_start(args, format); 48*afdbf109SJoseph Mingrone ret = pcapint_vasprintf(strp, format, args); 4957e22627SCy Schubert va_end(args); 5057e22627SCy Schubert return (ret); 5157e22627SCy Schubert } 52