1 #include "stdio_impl.h"
7 static size_t sn_write(FILE *f, const unsigned char *s, size_t l)
9 size_t k = f->wend - f->wpos;
11 memcpy(f->wpos, s, k);
13 /* pretend to succeed, but discard extra data */
17 int vsnprintf(char *restrict s, size_t n, const char *restrict fmt, va_list ap)
21 FILE f = { .lbf = EOF, .write = sn_write, .lock = -1 };
23 if (n-1 > INT_MAX-1) {
32 /* Ensure pointers don't wrap if "infinite" n is passed in */
33 if (n > (char *)0+SIZE_MAX-s-1) n = (char *)0+SIZE_MAX-s-1;
35 f.buf = f.wpos = (void *)s;
36 f.wbase = f.wend = (void *)(s+n);
37 r = vfprintf(&f, fmt, ap);
39 /* Null-terminate, overwriting last char if dest buffer is full */
40 if (n) f.wpos[-(f.wpos == f.wend)] = 0;