]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Fix %ju handling in archive_string_sprintf()
authorTim Kientzle <kientzle@gmail.com>
Thu, 29 Jan 2009 03:08:40 +0000 (22:08 -0500)
committerTim Kientzle <kientzle@gmail.com>
Thu, 29 Jan 2009 03:08:40 +0000 (22:08 -0500)
SVN-Revision: 505

libarchive/archive_string.c
libarchive/archive_string.h
libarchive/archive_string_sprintf.c

index 57df4e874c9d2c0eccdade2f0a146b894c2b4f79..0fc42cf807925015c684b51d7d1184c60f9297f5 100644 (file)
@@ -163,21 +163,6 @@ __archive_strappend_char(struct archive_string *as, char c)
        return (__archive_string_append(as, &c, 1));
 }
 
-struct archive_string *
-__archive_strappend_int(struct archive_string *as, int d, int base)
-{
-       static const char *digits = "0123456789abcdef";
-
-       if (d < 0) {
-               __archive_strappend_char(as, '-');
-               d = -d;
-       }
-       if (d >= base)
-               __archive_strappend_int(as, d/base, base);
-       __archive_strappend_char(as, digits[d % base]);
-       return (as);
-}
-
 #ifndef _WIN32
 /*
  * Home-grown wctomb for UTF-8.
index 8e80b32c35a81a799acb218a5f566aae900cb04b..87c3b03cc69002894f55c8cd05b96e849e93df72 100644 (file)
@@ -66,11 +66,6 @@ struct archive_string *
 __archive_strappend_char(struct archive_string *, char);
 #define        archive_strappend_char __archive_strappend_char
 
-/* Append an integer in the specified base (2 <= base <= 16). */
-struct archive_string *
-__archive_strappend_int(struct archive_string *as, int d, int base);
-#define        archive_strappend_int __archive_strappend_int
-
 /* Convert a wide-char string to UTF-8 and append the result. */
 struct archive_string *
 __archive_strappend_w_utf8(struct archive_string *, const wchar_t *);
index 6f77a36a1b8cc251624c032d36cc59d24430908b..067c79bf64635ae07d097824ae27e99359428151 100644 (file)
@@ -32,7 +32,7 @@ __FBSDID("$FreeBSD: src/lib/libarchive/archive_string_sprintf.c,v 1.10 2008/03/1
  * implementing this function in terms of vsnprintf() requires
  * two calls (one to determine the size, another to format the
  * result), which in turn requires duplicating the argument list
- * using va_copy, which isn't yet universally available.
+ * using va_copy, which isn't yet universally available. <sigh>
  *
  * So, I've implemented a bare minimum of printf()-like capability
  * here.  This is only used to format error messages, so doesn't
@@ -44,6 +44,30 @@ __FBSDID("$FreeBSD: src/lib/libarchive/archive_string_sprintf.c,v 1.10 2008/03/1
 #include "archive_string.h"
 #include "archive_private.h"
 
+/*
+ * Utility functions to format signed/unsigned integers and append
+ * them to an archive_string.
+ */
+static void
+append_uint(struct archive_string *as, uintmax_t d, unsigned base)
+{
+       static const char *digits = "0123456789abcdef";
+       if (d >= base)
+               append_uint(as, d/base, base);
+       archive_strappend_char(as, digits[d % base]);
+}
+
+static void
+append_int(struct archive_string *as, intmax_t d, unsigned base)
+{
+       if (d < 0) {
+               archive_strappend_char(as, '-');
+               d = -d;
+       }
+       append_uint(as, d, base);
+}
+
+
 void
 __archive_string_sprintf(struct archive_string *as, const char *fmt, ...)
 {
@@ -75,7 +99,6 @@ __archive_string_vsprintf(struct archive_string *as, const char *fmt,
                return;
        }
 
-       long_flag = '\0';
        for (p = fmt; *p != '\0'; p++) {
                const char *saved_p = p;
 
@@ -86,6 +109,7 @@ __archive_string_vsprintf(struct archive_string *as, const char *fmt,
 
                p++;
 
+               long_flag = '\0';
                switch(*p) {
                case 'j':
                        long_flag = 'j';
@@ -111,7 +135,7 @@ __archive_string_vsprintf(struct archive_string *as, const char *fmt,
                        case 'l': s = va_arg(ap, long); break;
                        default:  s = va_arg(ap, int); break;
                        }
-                       archive_strappend_int(as, s, 10);
+                       append_int(as, s, 10);
                        break;
                case 's':
                        p2 = va_arg(ap, char *);
@@ -126,9 +150,9 @@ __archive_string_vsprintf(struct archive_string *as, const char *fmt,
                        }
                        /* Format it in the correct base. */
                        switch (*p) {
-                       case 'o': archive_strappend_int(as, u, 8); break;
-                       case 'u': archive_strappend_int(as, u, 10); break;
-                       default: archive_strappend_int(as, u, 16); break;
+                       case 'o': append_uint(as, u, 8); break;
+                       case 'u': append_uint(as, u, 10); break;
+                       default: append_uint(as, u, 16); break;
                        }
                        break;
                default: