Reworked scanf() testing. General cleanups.
[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 #define _PDCLIB_FILEID "stdio/vsnprintf.c"
52 #define _PDCLIB_STRINGIO
53
54 #include <_PDCLIB_test.h>
55
56 static int testprintf( char * s, const char * format, ... )
57 {
58     int i;
59     va_list arg;
60     va_start( arg, format );
61     i = vsnprintf( s, 100, format, arg );
62     va_end( arg );
63     return i;
64 }
65
66 int main( void )
67 {
68     char target[100];
69 #include "printf_testcases.h"
70     return TEST_RESULTS;
71 }
72
73 #endif
74