]> pd.if.org Git - pdclib/blob - functions/stdio/vsnprintf.c
Fixing #47 (*snprint() crash).
[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             if ( status.i < n )
36             {
37                 s[ status.i ] = *format;
38             }
39             status.i++;
40             format++;
41         }
42         else
43         {
44             /* Continue parsing after conversion specifier */
45             format = rc;
46         }
47     }
48     if ( status.i  < n )
49     {
50         s[ status.i ] = '\0';
51     }
52     va_end( status.arg );
53     return status.i;
54 }
55
56 #endif
57
58 #ifdef TEST
59 #define _PDCLIB_FILEID "stdio/vsnprintf.c"
60 #define _PDCLIB_STRINGIO
61
62 #include <_PDCLIB_test.h>
63
64 static int testprintf( char * s, const char * format, ... )
65 {
66     int i;
67     va_list arg;
68     va_start( arg, format );
69     i = vsnprintf( s, 100, format, arg );
70     va_end( arg );
71     return i;
72 }
73
74 int main( void )
75 {
76     char target[100];
77 #include "printf_testcases.h"
78     return TEST_RESULTS;
79 }
80
81 #endif
82