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