]> xenbits.xensource.com Git - seabios.git/commitdiff
Update snprintf to return the number of bytes used.
authorKevin O'Connor <kevin@koconnor.net>
Tue, 24 Nov 2009 14:37:53 +0000 (09:37 -0500)
committerKevin O'Connor <kevin@koconnor.net>
Tue, 24 Nov 2009 14:37:53 +0000 (09:37 -0500)
src/output.c
src/util.h

index 7f74a69ec44c2024a8e6dfb59158f2d603c15307..7e91bfe168dd31dda08dc15712cce9be64941cfc 100644 (file)
@@ -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;
 }
 
 
index 54326853e7944935302a578aa27ec746e87db24f..c3ecb41079e6b6210e6bb15974f5094516dbe41e 100644 (file)
@@ -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)  \