diff --git a/src/stdio.c b/src/stdio.c index 687686b..6b4c60d 100644 --- a/src/stdio.c +++ b/src/stdio.c @@ -7,11 +7,11 @@ -static int32_t rtstring_output_handler(void *user, const char *data, size_t size) +static int32_t rtstring_output_handler(void* user, const char* data, size_t size) { - string_descriptor_t* str_descriptor = (string_descriptor_t*)user; + string_descriptor_t* const str_descriptor = (string_descriptor_t*)user; - size_t len = size < (str_descriptor->len - 1) ? size : (str_descriptor->len - 1); + const size_t len = size < (str_descriptor->len - 1) ? size : (str_descriptor->len - 1); if (len) { @@ -27,7 +27,7 @@ -int32_t sprintf(char *str, const char *format, ...) +int32_t sprintf(char* str, const char* format, ...) { va_list args; va_start(args, format); @@ -40,20 +40,27 @@ } -int32_t snprintf(char *str, size_t n, const char *format, ...) +int32_t snprintf(char* str, size_t n, const char* format, ...) { - va_list args; - va_start(args, format); + if (n > 0) + { + va_list args; + va_start(args, format); - string_descriptor_t str_descriptor = {str, n}; - const int32_t rv = rtprintf(rtstring_output_handler, &str_descriptor, format, args); + string_descriptor_t str_descriptor = {str, n}; + const int32_t rv = rtprintf(rtstring_output_handler, &str_descriptor, format, args); - va_end(args); - return rv; + va_end(args); + return rv; + } + else + { + return 0; + } } -int32_t vsprintf(char *str, const char *format, va_list args) +int32_t vsprintf(char* str, const char* format, va_list args) { string_descriptor_t str_descriptor = {str, -1}; return rtprintf(rtstring_output_handler, &str_descriptor, format, args); @@ -61,10 +68,17 @@ -int32_t vsnprintf(char *str, size_t n, const char *format, va_list args) +int32_t vsnprintf(char* str, size_t n, const char* format, va_list args) { - string_descriptor_t str_descriptor = {str, n}; - return rtprintf(rtstring_output_handler, &str_descriptor, format, args); + if (n > 0) + { + string_descriptor_t str_descriptor = {str, n}; + return rtprintf(rtstring_output_handler, &str_descriptor, format, args); + } + else + { + return 0; + } } @@ -85,14 +99,21 @@ int32_t isnprintf(char *str, size_t n, const char *format, ...) { - va_list args; - va_start(args, format); + if (n > 0) + { + va_list args; + va_start(args, format); - string_descriptor_t str_descriptor = {str, n}; - int32_t rv = rtiprintf(rtstring_output_handler, &str_descriptor, format, args); + string_descriptor_t str_descriptor = {str, n}; + int32_t rv = rtiprintf(rtstring_output_handler, &str_descriptor, format, args); - va_end(args); - return rv; + va_end(args); + return rv; + } + else + { + return 0; + } } @@ -103,10 +124,16 @@ } - int32_t ivsnprintf(char *str, size_t n, const char *format, va_list args) { - string_descriptor_t str_descriptor = {str, n}; - return rtiprintf(rtstring_output_handler, &str_descriptor, format, args); + if (n > 0) + { + string_descriptor_t str_descriptor = {str, n}; + return rtiprintf(rtstring_output_handler, &str_descriptor, format, args); + } + else + { + return 0; + } }