1 #include <trusty/string.h>
2 
3 #include <stdio.h>
4 #include <stdlib.h>
5 
scnprintf(char * buf,size_t size,const char * fmt,...)6 int scnprintf(char* buf, size_t size, const char* fmt, ...) {
7     va_list args;
8 
9     va_start(args, fmt);
10     int vscn_ret = vscnprintf(buf, size, fmt, args);
11     va_end(args);
12 
13     return vscn_ret;
14 }
15 
vscnprintf(char * buf,size_t size,const char * fmt,va_list args)16 int vscnprintf(char* buf, size_t size, const char* fmt, va_list args) {
17     if (size == 0) {
18         return 0;
19     }
20     int would_write = vsnprintf(buf, size, fmt, args);
21     if (would_write < 0) {
22         return would_write;
23     }
24     size_t max_write = size - 1;
25     if ((size_t)would_write > max_write) {
26         return (int)max_write;
27     }
28     return would_write;
29 }
30