X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdio%2Fvsnprintf.c;h=cda7ab6579d4f45ef9d47580fb5ef540e070d094;hb=0711226145f12c37217681b57944860bdeb6ace0;hp=165ed54ce3a1fb600e317a383c7075edbd955a6f;hpb=e72505f84aa8d8730e46d955a2722af361112e4f;p=pdclib diff --git a/functions/stdio/vsnprintf.c b/functions/stdio/vsnprintf.c index 165ed54..cda7ab6 100644 --- a/functions/stdio/vsnprintf.c +++ b/functions/stdio/vsnprintf.c @@ -10,8 +10,12 @@ #include #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,11 +60,11 @@ int vsnprintf( char * _PDCLIB_restrict s, size_t n, const char * _PDCLIB_restric #endif #ifdef TEST -#include <_PDCLIB_test.h> - -#include +#define _PDCLIB_FILEID "stdio/vsnprintf.c" +#define _PDCLIB_STRINGIO #include -#include +#include +#include <_PDCLIB_test.h> static int testprintf( char * s, const char * format, ... ) { @@ -64,13 +76,10 @@ static int testprintf( char * s, const char * format, ... ) return i; } -#define TESTCASE_SPRINTF( x ) if ( strcmp( target, x ) == 0 ) {} \ - else { TEST_RESULTS += 1; printf( "FAILED: " __FILE__ ", line %d - \"%s\" != %s\n", __LINE__, target, #x ); } - int main( void ) { char target[100]; -#include "printf_testcases.incl" +#include "printf_testcases.h" return TEST_RESULTS; }