Import libarchive-2.5.5.
[dragonfly.git] / contrib / libarchive-2.0 / libarchive / archive_string_sprintf.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
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.
13  *
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.
24  */
25
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_string_sprintf.c,v 1.8 2007/01/09 08:05:55 kientzle Exp $");
28
29 /*
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.
36  *
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.
40  */
41
42 #include <stdio.h>
43
44 #include "archive_string.h"
45
46 /*
47  * Like 'vsprintf', but ensures the target is big enough, resizing if
48  * necessary.
49  */
50 void
51 __archive_string_vsprintf(struct archive_string *as, const char *fmt,
52     va_list ap)
53 {
54         char long_flag;
55         intmax_t s; /* Signed integer temp. */
56         uintmax_t u; /* Unsigned integer temp. */
57         const char *p, *p2;
58
59         __archive_string_ensure(as, 64);
60
61         if (fmt == NULL) {
62                 as->s[0] = 0;
63                 return;
64         }
65
66         long_flag = '\0';
67         for (p = fmt; *p != '\0'; p++) {
68                 const char *saved_p = p;
69
70                 if (*p != '%') {
71                         archive_strappend_char(as, *p);
72                         continue;
73                 }
74
75                 p++;
76
77                 switch(*p) {
78                 case 'j':
79                         long_flag = 'j';
80                         p++;
81                         break;
82                 case 'l':
83                         long_flag = 'l';
84                         p++;
85                         break;
86                 }
87
88                 switch (*p) {
89                 case '%':
90                         __archive_strappend_char(as, '%');
91                         break;
92                 case 'c':
93                         s = va_arg(ap, int);
94                         __archive_strappend_char(as, s);
95                         break;
96                 case 'd':
97                         switch(long_flag) {
98                         case 'j': s = va_arg(ap, intmax_t); break;
99                         case 'l': s = va_arg(ap, long); break;
100                         default:  s = va_arg(ap, int); break;
101                         }
102                         archive_strappend_int(as, s, 10);
103                         break;
104                 case 's':
105                         p2 = va_arg(ap, char *);
106                         archive_strcat(as, p2);
107                         break;
108                 case 'o': case 'u': case 'x': case 'X':
109                         /* Common handling for unsigned integer formats. */
110                         switch(long_flag) {
111                         case 'j': u = va_arg(ap, uintmax_t); break;
112                         case 'l': u = va_arg(ap, unsigned long); break;
113                         default:  u = va_arg(ap, unsigned int); break;
114                         }
115                         /* Format it in the correct base. */
116                         switch (*p) {
117                         case 'o': archive_strappend_int(as, u, 8); break;
118                         case 'u': archive_strappend_int(as, u, 10); break;
119                         default: archive_strappend_int(as, u, 16); break;
120                         }
121                         break;
122                 default:
123                         /* Rewind and print the initial '%' literally. */
124                         p = saved_p;
125                         archive_strappend_char(as, *p);
126                 }
127         }
128 }