1 #include <stdio.h>
2 #include <stdarg.h>
3 
4 #include "portability.h"
5 
6 int
pcap_vsnprintf(char * str,size_t str_size,const char * format,va_list args)7 pcap_vsnprintf(char *str, size_t str_size, const char *format, va_list args)
8 {
9 	int ret;
10 
11 	ret = _vsnprintf_s(str, str_size, _TRUNCATE, format, args);
12 
13 	/*
14 	 * XXX - _vsnprintf() and _snprintf() do *not* guarantee
15 	 * that str is null-terminated, but C99's vsnprintf()
16 	 * and snprintf() do, and we want to offer C99 behavior,
17 	 * so forcibly null-terminate the string.
18 	 *
19 	 * We don't, however, offer C99 behavior for the return
20 	 * value; _vsnprintf_s() returns -1, not the number of
21 	 * characters that would have been put into the buffer
22 	 * had it been large enough, if the string is truncated.
23 	 * The only way to get that value is to use _vscprintf();
24 	 * getting that count isn't worth the re-formatting.
25 	 *
26 	 * XXX - does _vsnprintf_s() return -1 on a formatting
27 	 * error?
28 	 */
29 	str[str_size - 1] = '\0';
30 	return (ret);
31 }
32 
33 int
pcap_snprintf(char * str,size_t str_size,const char * format,...)34 pcap_snprintf(char *str, size_t str_size, const char *format, ...)
35 {
36 	va_list args;
37 	int ret;
38 
39 	va_start(args, format);
40 	ret = pcap_vsnprintf(str, str_size, format, args);
41 	va_end(args);
42 	return (ret);
43 }
44