0cccf7a0842d5814953d10c5b53d825104029a2b
[fw/pdclib] / 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
14 int vsnprintf( char * _PDCLIB_restrict s, size_t n, const char * _PDCLIB_restrict format, _PDCLIB_va_list arg )
15 {
16     /* TODO: This function should interpret format as multibyte characters.  */
17     struct _PDCLIB_status_t status;
18     status.base = 0;
19     status.flags = 0;
20     status.n = n;
21     status.i = 0;
22     status.current = 0;
23     status.s = s;
24     status.width = 0;
25     status.prec = 0;
26     status.stream = NULL;
27     va_copy( status.arg, arg );
28
29     while ( *format != '\0' )
30     {
31         const char * rc;
32         if ( ( *format != '%' ) || ( ( rc = _PDCLIB_print( format, &status ) ) == format ) )
33         {
34             /* No conversion specifier, print verbatim */
35             s[ status.i++ ] = *(format++);
36         }
37         else
38         {
39             /* Continue parsing after conversion specifier */
40             format = rc;
41         }
42     }
43     s[ status.i ] = '\0';
44     va_end( status.arg );
45     return status.i;
46 }
47
48 #endif
49
50 #ifdef TEST
51 #include <_PDCLIB_test.h>
52
53 #include <limits.h>
54 #include <stdint.h>
55 #include <string.h>
56
57
58 static int testprintf( char * s, size_t n, const char * format, ... )
59 {
60     int i;
61     va_list arg;
62     va_start( arg, format );
63     i = vsnprintf( s, n, format, arg );
64     va_end( arg );
65     return i;
66 }
67
68 int main( void )
69 {
70     char buffer[100];
71 #include "printf_testcases.incl"
72     return TEST_RESULTS;
73 }
74
75 #endif
76