X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdio%2Fvsnprintf.c;h=dccd90e719d5ccdf67713df42fcad3e27ed3f8e3;hb=b1fc26afebd4d557ff89a44bc21767a8704c3809;hp=fd9b4ac3fc34e5cb57477395371164b6536e3eaf;hpb=92177644f19b596c593293cb20c4f6677186f3b6;p=pdclib diff --git a/functions/stdio/vsnprintf.c b/functions/stdio/vsnprintf.c index fd9b4ac..dccd90e 100644 --- a/functions/stdio/vsnprintf.c +++ b/functions/stdio/vsnprintf.c @@ -1,6 +1,4 @@ -/* $Id$ */ - -/* vsnprintf( char *, size_t, const char *, va_list ap ) +/* vsnprintf( char *, size_t, const char *, va_list ) This file is part of the Public Domain C Library (PDCLib). Permission is granted to use, modify, and / or redistribute at will. @@ -32,7 +30,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 +43,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 +54,25 @@ int vsnprintf( char * _PDCLIB_restrict s, size_t n, const char * _PDCLIB_restric #endif #ifdef TEST -#include <_PDCLIB_test.h> +#define _PDCLIB_FILEID "stdio/vsnprintf.c" +#define _PDCLIB_STRINGIO -#include -#include -#include +#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; }