1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <errno.h>
4 
5 // C function that calls `vsnprintf` with a POINTER to a va_list.
6 //
7 // We have to write this in C because there is no way to know
8 // the size of a va_list in Rust and so we couldn't pass it
9 // by-value as required by vsnprintf.
vsnprintf_wrapper(char * buffer,size_t size,const char * format,va_list orig_list)10 int vsnprintf_wrapper(char *buffer,
11                       size_t size,
12                       const char *format,
13                       va_list orig_list) {
14   va_list list;
15   va_copy(list, orig_list);
16 
17   // C does not require vsprintf to set errno, but POSIX does.
18   // Here we clear the errno and so we know that if this function
19   // fails AND there is an error set, then it must have been triggered
20   // by the sprintf.
21   errno = 0;
22   return vsnprintf(buffer, size, format, list);
23 }
24 
25