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