]> pd.if.org Git - pdclib/blobdiff - functions/stdio/vsnprintf.c
PDCLIB-20 #resolve Add support for "unusual" cases. Ammend test suite to verify support.
[pdclib] / functions / stdio / vsnprintf.c
index fd9b4ac3fc34e5cb57477395371164b6536e3eaf..cda7ab6579d4f45ef9d47580fb5ef540e070d094 100644 (file)
 #include <stdarg.h>
 
 #ifndef REGTEST
+#include <_PDCLIB_io.h>
 
-int vsnprintf( char * _PDCLIB_restrict s, size_t n, const char * _PDCLIB_restrict format, _PDCLIB_va_list arg )
+int vsnprintf( char * _PDCLIB_restrict s, 
+               size_t n, 
+               const char * _PDCLIB_restrict format, 
+               _PDCLIB_va_list arg )
 {
     /* TODO: This function should interpret format as multibyte characters.  */
     struct _PDCLIB_status_t status;
@@ -32,7 +36,12 @@ int vsnprintf( char * _PDCLIB_restrict s, size_t n, const char * _PDCLIB_restric
         if ( ( *format != '%' ) || ( ( rc = _PDCLIB_print( format, &status ) ) == format ) )
         {
             /* No conversion specifier, print verbatim */
-            s[ status.i++ ] = *(format++);
+            if ( status.i < n )
+            {
+                s[ status.i ] = *format;
+            }
+            status.i++;
+            format++;
         }
         else
         {
@@ -40,7 +49,10 @@ int vsnprintf( char * _PDCLIB_restrict s, size_t n, const char * _PDCLIB_restric
             format = rc;
         }
     }
-    s[ status.i ] = '\0';
+    if ( status.i  < n )
+    {
+        s[ status.i ] = '\0';
+    }
     va_end( status.arg );
     return status.i;
 }
@@ -48,28 +60,26 @@ int vsnprintf( char * _PDCLIB_restrict s, size_t n, const char * _PDCLIB_restric
 #endif
 
 #ifdef TEST
-#include <_PDCLIB_test.h>
-
-#include <limits.h>
+#define _PDCLIB_FILEID "stdio/vsnprintf.c"
+#define _PDCLIB_STRINGIO
 #include <stdint.h>
-#include <string.h>
+#include <stddef.h>
+#include <_PDCLIB_test.h>
 
-static int testprintf( char * s, size_t n, const char * format, ... )
+static int testprintf( char * s, const char * format, ... )
 {
     int i;
     va_list arg;
     va_start( arg, format );
-    i = vsnprintf( s, n, format, arg );
+    i = vsnprintf( s, 100, format, arg );
     va_end( arg );
     return i;
 }
 
-#define TESTCASE_SPRINTF( x ) TESTCASE( x )
-
 int main( void )
 {
-    char buffer[100];
-#include "printf_testcases.incl"
+    char target[100];
+#include "printf_testcases.h"
     return TEST_RESULTS;
 }