Import of libarchive and bsdtar 1.3.1
[dragonfly.git] / contrib / libarchive-1.3.1 / libarchive / archive_string_sprintf.c
blob63a06d724869f70fead5dee1ab3e17ed39d0f739
1 /*-
2 * Copyright (c) 2003-2004 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 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: src/lib/libarchive/archive_string_sprintf.c,v 1.7 2005/01/16 22:13:51 kientzle Exp $");
31 * The use of printf()-family functions can be troublesome
32 * for space-constrained applications. In addition, correctly
33 * implementing this function in terms of vsnprintf() requires
34 * two calls (one to determine the size, another to format the
35 * result), which in turn requires duplicating the argument list
36 * using va_copy, which isn't yet universally available.
38 * So, I've implemented a bare minimum of printf()-like capability
39 * here. This is only used to format error messages, so doesn't
40 * require any floating-point support or field-width handling.
43 #include <stdio.h>
45 #include "archive_string.h"
48 * Like 'vsprintf', but ensures the target is big enough, resizing if
49 * necessary.
51 void
52 __archive_string_vsprintf(struct archive_string *as, const char *fmt,
53 va_list ap)
55 char long_flag;
56 intmax_t s; /* Signed integer temp. */
57 uintmax_t u; /* Unsigned integer temp. */
58 const char *p, *p2;
60 __archive_string_ensure(as, 64);
62 if (fmt == NULL) {
63 as->s[0] = 0;
64 return;
67 long_flag = '\0';
68 for (p = fmt; *p != '\0'; p++) {
69 const char *saved_p = p;
71 if (*p != '%') {
72 archive_strappend_char(as, *p);
73 continue;
76 p++;
78 switch(*p) {
79 case 'j':
80 long_flag = 'j';
81 p++;
82 break;
83 case 'l':
84 long_flag = 'l';
85 p++;
86 break;
89 switch (*p) {
90 case '%':
91 __archive_strappend_char(as, '%');
92 break;
93 case 'c':
94 s = va_arg(ap, int);
95 __archive_strappend_char(as, s);
96 break;
97 case 'd':
98 switch(long_flag) {
99 case 'j': s = va_arg(ap, intmax_t); break;
100 case 'l': s = va_arg(ap, long); break;
101 default: s = va_arg(ap, int); break;
103 archive_strappend_int(as, s, 10);
104 break;
105 case 's':
106 p2 = va_arg(ap, char *);
107 archive_strcat(as, p2);
108 break;
109 case 'o': case 'u': case 'x': case 'X':
110 /* Common handling for unsigned integer formats. */
111 switch(long_flag) {
112 case 'j': u = va_arg(ap, uintmax_t); break;
113 case 'l': u = va_arg(ap, unsigned long); break;
114 default: u = va_arg(ap, unsigned int); break;
116 /* Format it in the correct base. */
117 switch (*p) {
118 case 'o': archive_strappend_int(as, u, 8); break;
119 case 'u': archive_strappend_int(as, u, 10); break;
120 default: archive_strappend_int(as, u, 16); break;
122 break;
123 default:
124 /* Rewind and print the initial '%' literally. */
125 p = saved_p;
126 archive_strappend_char(as, *p);