win_asprintf.c revision 356341
1#include <stdio.h>
2#include <stdlib.h>
3#include <stdarg.h>
4
5#include "portability.h"
6
7int
8pcap_vasprintf(char **strp, const char *format, va_list args)
9{
10	int len;
11	size_t str_size;
12	char *str;
13	int ret;
14
15	len = _vscprintf(format, args);
16	if (len == -1) {
17		*strp = NULL;
18		return (-1);
19	}
20	str_size = len + 1;
21	str = malloc(str_size);
22	if (str == NULL) {
23		*strp = NULL;
24		return (-1);
25	}
26	ret = pcap_vsnprintf(str, str_size, format, args);
27	if (ret == -1) {
28		free(str);
29		*strp = NULL;
30		return (-1);
31	}
32	*strp = str;
33	/*
34	 * pcap_vsnprintf() shouldn't truncate the string, as we have
35	 * allocated a buffer large enough to hold the string, so its
36	 * return value should be the number of characters printed.
37	 */
38	return (ret);
39}
40
41int
42pcap_asprintf(char **strp, const char *format, ...)
43{
44	va_list args;
45	int ret;
46
47	va_start(args, format);
48	ret = pcap_vasprintf(strp, format, args);
49	va_end(args);
50	return (ret);
51}
52