aboutsummaryrefslogtreecommitdiff
path: root/lib/libarchive/archive_string_sprintf.c
diff options
context:
space:
mode:
authorTim Kientzle <kientzle@FreeBSD.org>2009-03-06 05:14:55 +0000
committerTim Kientzle <kientzle@FreeBSD.org>2009-03-06 05:14:55 +0000
commitaf176e930c843269a17564b6006026e12dd9f8c4 (patch)
tree05876ae8fd18c0cdb5b8e5844dacf40a7bd41276 /lib/libarchive/archive_string_sprintf.c
parentc07eec64597c11cbcc0afd1edbe5ba5b6d82e609 (diff)
downloadsrc-af176e930c843269a17564b6006026e12dd9f8c4.tar.gz
src-af176e930c843269a17564b6006026e12dd9f8c4.zip
Merge r505 from libarchive.googlecode.com: Fix %ju support. Simplify
the code here a bit by making the int formatting functions static to archive_string_sprintf.c, which is the only place this has ever been used.
Notes
Notes: svn path=/head/; revision=189435
Diffstat (limited to 'lib/libarchive/archive_string_sprintf.c')
-rw-r--r--lib/libarchive/archive_string_sprintf.c36
1 files changed, 30 insertions, 6 deletions
diff --git a/lib/libarchive/archive_string_sprintf.c b/lib/libarchive/archive_string_sprintf.c
index 44b116eac0af..9199106b21fd 100644
--- a/lib/libarchive/archive_string_sprintf.c
+++ b/lib/libarchive/archive_string_sprintf.c
@@ -32,7 +32,7 @@ __FBSDID("$FreeBSD$");
* 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$");
#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: