scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

vsnprintf.c (407B)


      1 #include <stdarg.h>
      2 #include <stdio.h>
      3 
      4 #undef vsnprintf
      5 
      6 int
      7 vsnprintf(char *restrict buf, size_t siz, const char *restrict fmt, va_list ap)
      8 {
      9 	FILE f;
     10 	int r;
     11 	unsigned char *s = (unsigned char *) buf;
     12 
     13 	f.flags = _IORW | _IOSTRG;
     14 	f.len = siz;
     15 	f.buf = s;
     16 	f.wp = s;
     17 	f.rp = s + siz;
     18 
     19 	r = vfprintf(&f, fmt, ap);
     20 	if (s) {
     21 		if (f.wp == f.rp)
     22 			--f.wp;
     23 		if (siz != 0)
     24 			*f.wp = '\0';
     25 	}
     26 
     27 	return r;
     28 }