From 2be312c112a5ccfcd165f7ba3276e3aaf33ee92c Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Tue, 24 Nov 2009 09:37:53 -0500 Subject: [PATCH] Update snprintf to return the number of bytes used. --- src/output.c | 10 +++++++--- src/util.h | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/output.c b/src/output.c index 7f74a69..7e91bfe 100644 --- a/src/output.c +++ b/src/output.c @@ -376,12 +376,15 @@ putc_str(struct putcinfo *info, char c) sinfo->str++; } -void +// Build a formatted string. Note, this function returns the actual +// number of bytes used (not including null) even in the overflow +// case. +int snprintf(char *str, size_t size, const char *fmt, ...) { ASSERT32(); if (!size) - return; + return 0; struct snprintfinfo sinfo = { { putc_str }, str, str + size }; va_list args; va_start(args, fmt); @@ -389,8 +392,9 @@ snprintf(char *str, size_t size, const char *fmt, ...) va_end(args); char *end = sinfo.str; if (end >= sinfo.end) - end--; + end = sinfo.end - 1; *end = '\0'; + return end - str; } diff --git a/src/util.h b/src/util.h index 5432685..c3ecb41 100644 --- a/src/util.h +++ b/src/util.h @@ -187,7 +187,7 @@ void printf(const char *fmt, ...) __attribute__ ((format (printf, 1, 2))); void __dprintf(const char *fmt, ...) __attribute__ ((format (printf, 1, 2))); -void snprintf(char *str, size_t size, const char *fmt, ...) +int snprintf(char *str, size_t size, const char *fmt, ...) __attribute__ ((format (printf, 3, 4))); #define dprintf(lvl, fmt, args...) do { \ if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL) \ -- 2.39.5