diff options
Diffstat (limited to 'contrib/libpcap/missing/win_asprintf.c')
| -rw-r--r-- | contrib/libpcap/missing/win_asprintf.c | 51 | 
1 files changed, 51 insertions, 0 deletions
diff --git a/contrib/libpcap/missing/win_asprintf.c b/contrib/libpcap/missing/win_asprintf.c new file mode 100644 index 000000000000..076bc562da82 --- /dev/null +++ b/contrib/libpcap/missing/win_asprintf.c @@ -0,0 +1,51 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> + +#include "portability.h" + +int +pcapint_vasprintf(char **strp, const char *format, va_list args) +{ +	int len; +	size_t str_size; +	char *str; +	int ret; + +	len = _vscprintf(format, args); +	if (len == -1) { +		*strp = NULL; +		return (-1); +	} +	str_size = len + 1; +	str = malloc(str_size); +	if (str == NULL) { +		*strp = NULL; +		return (-1); +	} +	ret = vsnprintf(str, str_size, format, args); +	if (ret == -1) { +		free(str); +		*strp = NULL; +		return (-1); +	} +	*strp = str; +	/* +	 * vsnprintf() shouldn't truncate the string, as we have +	 * allocated a buffer large enough to hold the string, so its +	 * return value should be the number of characters printed. +	 */ +	return (ret); +} + +int +pcapint_asprintf(char **strp, const char *format, ...) +{ +	va_list args; +	int ret; + +	va_start(args, format); +	ret = pcapint_vasprintf(strp, format, args); +	va_end(args); +	return (ret); +}  | 
