gitrev.sh: search harder for the git dir
[dragonfly.git] / contrib / libarchive / libarchive / archive_string_sprintf.c
blob067c79bf64635ae07d097824ae27e99359428151
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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: src/lib/libarchive/archive_string_sprintf.c,v 1.10 2008/03/14 22:00:09 kientzle Exp $");
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.
42 #include <stdio.h>
44 #include "archive_string.h"
45 #include "archive_private.h"
48 * Utility functions to format signed/unsigned integers and append
49 * them to an archive_string.
51 static void
52 append_uint(struct archive_string *as, uintmax_t d, unsigned base)
54 static const char *digits = "0123456789abcdef";
55 if (d >= base)
56 append_uint(as, d/base, base);
57 archive_strappend_char(as, digits[d % base]);
60 static void
61 append_int(struct archive_string *as, intmax_t d, unsigned base)
63 if (d < 0) {
64 archive_strappend_char(as, '-');
65 d = -d;
67 append_uint(as, d, base);
71 void
72 __archive_string_sprintf(struct archive_string *as, const char *fmt, ...)
74 va_list ap;
76 va_start(ap, fmt);
77 archive_string_vsprintf(as, fmt, ap);
78 va_end(ap);
82 * Like 'vsprintf', but ensures the target is big enough, resizing if
83 * necessary.
85 void
86 __archive_string_vsprintf(struct archive_string *as, const char *fmt,
87 va_list ap)
89 char long_flag;
90 intmax_t s; /* Signed integer temp. */
91 uintmax_t u; /* Unsigned integer temp. */
92 const char *p, *p2;
94 if (__archive_string_ensure(as, 64) == NULL)
95 __archive_errx(1, "Out of memory");
97 if (fmt == NULL) {
98 as->s[0] = 0;
99 return;
102 for (p = fmt; *p != '\0'; p++) {
103 const char *saved_p = p;
105 if (*p != '%') {
106 archive_strappend_char(as, *p);
107 continue;
110 p++;
112 long_flag = '\0';
113 switch(*p) {
114 case 'j':
115 long_flag = 'j';
116 p++;
117 break;
118 case 'l':
119 long_flag = 'l';
120 p++;
121 break;
124 switch (*p) {
125 case '%':
126 __archive_strappend_char(as, '%');
127 break;
128 case 'c':
129 s = va_arg(ap, int);
130 __archive_strappend_char(as, s);
131 break;
132 case 'd':
133 switch(long_flag) {
134 case 'j': s = va_arg(ap, intmax_t); break;
135 case 'l': s = va_arg(ap, long); break;
136 default: s = va_arg(ap, int); break;
138 append_int(as, s, 10);
139 break;
140 case 's':
141 p2 = va_arg(ap, char *);
142 archive_strcat(as, p2);
143 break;
144 case 'o': case 'u': case 'x': case 'X':
145 /* Common handling for unsigned integer formats. */
146 switch(long_flag) {
147 case 'j': u = va_arg(ap, uintmax_t); break;
148 case 'l': u = va_arg(ap, unsigned long); break;
149 default: u = va_arg(ap, unsigned int); break;
151 /* Format it in the correct base. */
152 switch (*p) {
153 case 'o': append_uint(as, u, 8); break;
154 case 'u': append_uint(as, u, 10); break;
155 default: append_uint(as, u, 16); break;
157 break;
158 default:
159 /* Rewind and print the initial '%' literally. */
160 p = saved_p;
161 archive_strappend_char(as, *p);