X-Git-Url: https://pd.if.org/git/?p=pdclib;a=blobdiff_plain;f=functions%2Fstdio%2Fvsnprintf.c;h=761853ff8f627e38a7766e4b79184887f0b666ac;hp=9eb869e78cb2b7dbb52c9eab259b0fba1670283c;hb=da0f3f353d417fed71f358a48d5d5394145e460d;hpb=a7a8d2f1c85c2d7760d4d3479e90466cc3a81b04 diff --git a/functions/stdio/vsnprintf.c b/functions/stdio/vsnprintf.c index 9eb869e..761853f 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. @@ -10,39 +8,39 @@ #include #ifndef REGTEST +#include "_PDCLIB_io.h" +#include + +struct state { + size_t bufrem; + char *bufp; +}; -int vsnprintf( char * _PDCLIB_restrict s, size_t n, const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) +static size_t strout( void *p, const char *buf, size_t sz ) { - /* TODO: This function should interpret format as multibyte characters. */ - struct _PDCLIB_status_t status; - status.base = 0; - status.flags = 0; - status.n = n; - status.i = 0; - status.current = 0; - status.s = s; - status.width = 0; - status.prec = 0; - status.stream = NULL; - va_copy( status.arg, arg ); + struct state *s = p; + size_t copy = s->bufrem >= sz ? sz : s->bufrem; + memcpy( s->bufp, buf, copy ); + s->bufrem -= copy; + s->bufp += copy; + return sz; +} - while ( *format != '\0' ) +int vsnprintf( char * _PDCLIB_restrict s, + size_t n, + const char * _PDCLIB_restrict format, + _PDCLIB_va_list arg ) +{ + struct state st; + st.bufrem = n; + st.bufp = s; + int r = _vcbprintf( &st, strout, format, arg ); + if ( st.bufrem ) { - const char * rc; - if ( ( *format != '%' ) || ( ( rc = _PDCLIB_print( format, &status ) ) == format ) ) - { - /* No conversion specifier, print verbatim */ - s[ status.i++ ] = *(format++); - } - else - { - /* Continue parsing after conversion specifier */ - format = rc; - } + *st.bufp = 0; } - s[ status.i ] = '\0'; - va_end( status.arg ); - return status.i; + + return r; } #endif @@ -50,8 +48,9 @@ int vsnprintf( char * _PDCLIB_restrict s, size_t n, const char * _PDCLIB_restric #ifdef TEST #define _PDCLIB_FILEID "stdio/vsnprintf.c" #define _PDCLIB_STRINGIO - -#include <_PDCLIB_test.h> +#include +#include +#include "_PDCLIB_test.h" static int testprintf( char * s, const char * format, ... ) {