From: Kevin O'Connor Date: Tue, 24 Nov 2009 14:37:53 +0000 (-0500) Subject: Update snprintf to return the number of bytes used. X-Git-Tag: rel-0.5.0~31 X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=2be312c112a5ccfcd165f7ba3276e3aaf33ee92c;p=seabios.git Update snprintf to return the number of bytes used. --- 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) \