]> pd.if.org Git - pdclib/blob - functions/stdio/vsnprintf.c
PDCLib includes with quotes, not <>.
[pdclib] / functions / stdio / vsnprintf.c
1 /* vsnprintf( char *, size_t, const char *, va_list )
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include <stdio.h>
8 #include <stdarg.h>
9
10 #ifndef REGTEST
11 #include "_PDCLIB_io.h"
12 #include <string.h>
13
14 struct state {
15     size_t bufrem;
16     char *bufp;
17 };
18
19 static size_t strout( void *p, const char *buf, size_t sz )
20 {
21     struct state *s = p;
22     size_t copy = s->bufrem >= sz ? sz : s->bufrem;
23     memcpy( s->bufp, buf, copy );
24     s->bufrem -= copy;
25     s->bufp   += copy;
26     return sz;
27 }
28
29 int vsnprintf( char * _PDCLIB_restrict s,
30                size_t n,
31                const char * _PDCLIB_restrict format,
32                _PDCLIB_va_list arg )
33 {
34     struct state st;
35     st.bufrem = n;
36     st.bufp   = s;
37     int r = _vcbprintf( &st, strout, format, arg );
38     if ( st.bufrem )
39     {
40         *st.bufp = 0;
41     }
42
43     return r;
44 }
45
46 #endif
47
48 #ifdef TEST
49 #define _PDCLIB_FILEID "stdio/vsnprintf.c"
50 #define _PDCLIB_STRINGIO
51 #include <stdint.h>
52 #include <stddef.h>
53 #include "_PDCLIB_test.h"
54
55 static int testprintf( char * s, const char * format, ... )
56 {
57     int i;
58     va_list arg;
59     va_start( arg, format );
60     i = vsnprintf( s, 100, format, arg );
61     va_end( arg );
62     return i;
63 }
64
65 int main( void )
66 {
67     char target[100];
68 #include "printf_testcases.h"
69     return TEST_RESULTS;
70 }
71
72 #endif
73