From cd4481a6f29893fb0fff0c0ff0ff9c8f152af7dc Mon Sep 17 00:00:00 2001 From: Marco Paland Date: Fri, 20 Apr 2018 00:08:41 +0200 Subject: [PATCH] feat(printf): add vsnprintf function fixes #5 --- printf.c | 35 ++++++++++++++++++++++------------- printf.h | 14 ++++++++------ 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/printf.c b/printf.c index 21d5620..ed6e745 100644 --- a/printf.c +++ b/printf.c @@ -30,19 +30,21 @@ // /////////////////////////////////////////////////////////////////////////////// -#include #include +#include #include #include "printf.h" -// buffer size used for printf (created on stack) +// buffer size used omly for printf (created on stack) #define PRINTF_BUFFER_SIZE 128U -// ntoa conversion buffer size, this must be big enough to hold one converted numeric number (created on stack) +// ntoa conversion buffer size, this must be big enough to hold +// one converted numeric number including padded zeros (created on stack) #define PRINTF_NTOA_BUFFER_SIZE 32U -// ftoa conversion buffer size, this must be big enough to hold one converted float number (created on stack) +// ftoa conversion buffer size, this must be big enough to hold +// one converted float number including padded zeros (created on stack) #define PRINTF_FTOA_BUFFER_SIZE 32U // define this to support floating point (%f) @@ -349,14 +351,14 @@ static size_t _ftoa(double value, char* buffer, size_t maxlen, unsigned int prec // internal vsnprintf -static size_t _vsnprintf(char* buffer, size_t buffer_len, const char* format, va_list va) +static int _vsnprintf(char* buffer, size_t buffer_len, const char* format, va_list va) { unsigned int flags, width, precision, n; size_t idx = 0U; // check if buffer is valid if (!buffer) { - return 0U; + return -1; } while ((idx < buffer_len) && *format) @@ -587,7 +589,7 @@ static size_t _vsnprintf(char* buffer, size_t buffer_len, const char* format, va } // return written chars without terminating \0 - return idx; + return (int)idx; } @@ -598,12 +600,12 @@ int printf(const char* format, ...) va_list va; va_start(va, format); char buffer[PRINTF_BUFFER_SIZE]; - size_t ret = _vsnprintf(buffer, PRINTF_BUFFER_SIZE, format, va); + int ret = _vsnprintf(buffer, PRINTF_BUFFER_SIZE, format, va); va_end(va); for (size_t i = 0U; i < ret; ++i) { _putchar(buffer[i]); } - return (int)ret; + return ret; } @@ -611,9 +613,9 @@ int sprintf(char* buffer, const char* format, ...) { va_list va; va_start(va, format); - size_t ret = _vsnprintf(buffer, (size_t)-1, format, va); + int ret = _vsnprintf(buffer, (size_t)-1, format, va); va_end(va); - return (int)ret; + return ret; } @@ -621,7 +623,14 @@ int snprintf(char* buffer, size_t count, const char* format, ...) { va_list va; va_start(va, format); - size_t ret = _vsnprintf(buffer, count, format, va); + int ret = _vsnprintf(buffer, count, format, va); va_end(va); - return (int)ret; + return ret; } + + +inline int vsnprintf(char* buffer, size_t count, const char* format, va_list va) +{ + return _vsnprintf(buffer, count, format, va); +} + diff --git a/printf.h b/printf.h index d90ff45..e39bbb2 100644 --- a/printf.h +++ b/printf.h @@ -32,7 +32,7 @@ #ifndef _PRINTF_H_ #define _PRINTF_H_ -#include +#include #ifdef __cplusplus @@ -62,19 +62,21 @@ int printf(const char* format, ...); * Due to security reasons (buffer overflow) YOU SHOULD CONSIDER USING SNPRINTF INSTEAD! * \param buffer A pointer to the buffer where to store the formatted string. MUST be big enough to store the output! * \param format A string that specifies the format of the output - * \return The number of characters that are written into the array, not counting the terminating null character + * \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character */ int sprintf(char* buffer, const char* format, ...); /** - * Tiny snprintf implementation + * Tiny snprintf/vsnprintf implementation * \param buffer A pointer to the buffer where to store the formatted string - * \param count The maximum number of characters to store in the buffer, INCLUDING the terminating null character + * \param count The maximum number of characters to store in the buffer, including a terminating null character * \param format A string that specifies the format of the output - * \return The number of characters that are written into the array, not counting the terminating null character + * \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character + * If the formatted string is truncated the buffer size (count) is returned */ -int snprintf(char* buffer, size_t count, const char* format, ...); +int snprintf(char* buffer, size_t count, const char* format, ...); +int vsnprintf(char* buffer, size_t count, const char* format, va_list va); #ifdef __cplusplus