2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: head/lib/libarchive/archive_string_sprintf.c 189435 2009-03-06 05:14:55Z kientzle $");
30 * The use of printf()-family functions can be troublesome
31 * for space-constrained applications. In addition, correctly
32 * implementing this function in terms of vsnprintf() requires
33 * two calls (one to determine the size, another to format the
34 * result), which in turn requires duplicating the argument list
35 * using va_copy, which isn't yet universally available. <sigh>
37 * So, I've implemented a bare minimum of printf()-like capability
38 * here. This is only used to format error messages, so doesn't
39 * require any floating-point support or field-width handling.
46 #include "archive_string.h"
47 #include "archive_private.h"
50 * Utility functions to format signed/unsigned integers and append
51 * them to an archive_string.
54 append_uint(struct archive_string *as, uintmax_t d, unsigned base)
56 static const char digits[] = "0123456789abcdef";
58 append_uint(as, d/base, base);
59 archive_strappend_char(as, digits[d % base]);
63 append_int(struct archive_string *as, intmax_t d, unsigned base)
68 archive_strappend_char(as, '-');
69 ud = (d == INTMAX_MIN) ? (uintmax_t)(INTMAX_MAX) + 1 : (uintmax_t)(-d);
72 append_uint(as, ud, base);
77 archive_string_sprintf(struct archive_string *as, const char *fmt, ...)
82 archive_string_vsprintf(as, fmt, ap);
87 * Like 'vsprintf', but ensures the target is big enough, resizing if
91 archive_string_vsprintf(struct archive_string *as, const char *fmt,
95 intmax_t s; /* Signed integer temp. */
96 uintmax_t u; /* Unsigned integer temp. */
100 if (archive_string_ensure(as, 64) == NULL)
101 __archive_errx(1, "Out of memory");
108 for (p = fmt; *p != '\0'; p++) {
109 const char *saved_p = p;
112 archive_strappend_char(as, *p);
130 archive_strappend_char(as, '%');
134 archive_strappend_char(as, (char)s);
138 case 'j': s = va_arg(ap, intmax_t); break;
139 case 'l': s = va_arg(ap, long); break;
140 case 'z': s = va_arg(ap, ssize_t); break;
141 default: s = va_arg(ap, int); break;
143 append_int(as, s, 10);
148 pw = va_arg(ap, wchar_t *);
151 if (archive_string_append_from_wcs(as, pw,
152 wcslen(pw)) != 0 && errno == ENOMEM)
153 __archive_errx(1, "Out of memory");
156 p2 = va_arg(ap, char *);
159 archive_strcat(as, p2);
164 pw = va_arg(ap, wchar_t *);
167 if (archive_string_append_from_wcs(as, pw,
168 wcslen(pw)) != 0 && errno == ENOMEM)
169 __archive_errx(1, "Out of memory");
171 case 'o': case 'u': case 'x': case 'X':
172 /* Common handling for unsigned integer formats. */
174 case 'j': u = va_arg(ap, uintmax_t); break;
175 case 'l': u = va_arg(ap, unsigned long); break;
176 case 'z': u = va_arg(ap, size_t); break;
177 default: u = va_arg(ap, unsigned int); break;
179 /* Format it in the correct base. */
181 case 'o': append_uint(as, u, 8); break;
182 case 'u': append_uint(as, u, 10); break;
183 default: append_uint(as, u, 16); break;
187 /* Rewind and print the initial '%' literally. */
189 archive_strappend_char(as, *p);