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