archive_string_sprintf.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #include "archive_platform.h"
  26. /*
  27. * The use of printf()-family functions can be troublesome
  28. * for space-constrained applications. In addition, correctly
  29. * implementing this function in terms of vsnprintf() requires
  30. * two calls (one to determine the size, another to format the
  31. * result), which in turn requires duplicating the argument list
  32. * using va_copy, which isn't yet universally available. <sigh>
  33. *
  34. * So, I've implemented a bare minimum of printf()-like capability
  35. * here. This is only used to format error messages, so doesn't
  36. * require any floating-point support or field-width handling.
  37. */
  38. #ifdef HAVE_ERRNO_H
  39. #include <errno.h>
  40. #endif
  41. #include <stdio.h>
  42. #include "archive_string.h"
  43. #include "archive_private.h"
  44. /*
  45. * Utility functions to format signed/unsigned integers and append
  46. * them to an archive_string.
  47. */
  48. static void
  49. append_uint(struct archive_string *as, uintmax_t d, unsigned base)
  50. {
  51. static const char digits[] = "0123456789abcdef";
  52. if (d >= base)
  53. append_uint(as, d/base, base);
  54. archive_strappend_char(as, digits[d % base]);
  55. }
  56. static void
  57. append_int(struct archive_string *as, intmax_t d, unsigned base)
  58. {
  59. uintmax_t ud;
  60. if (d < 0) {
  61. archive_strappend_char(as, '-');
  62. ud = (d == INTMAX_MIN) ? (uintmax_t)(INTMAX_MAX) + 1 : (uintmax_t)(-d);
  63. } else
  64. ud = d;
  65. append_uint(as, ud, base);
  66. }
  67. void
  68. archive_string_sprintf(struct archive_string *as, const char *fmt, ...)
  69. {
  70. va_list ap;
  71. va_start(ap, fmt);
  72. archive_string_vsprintf(as, fmt, ap);
  73. va_end(ap);
  74. }
  75. /*
  76. * Like 'vsprintf', but ensures the target is big enough, resizing if
  77. * necessary.
  78. */
  79. void
  80. archive_string_vsprintf(struct archive_string *as, const char *fmt,
  81. va_list ap)
  82. {
  83. char long_flag;
  84. intmax_t s; /* Signed integer temp. */
  85. uintmax_t u; /* Unsigned integer temp. */
  86. const char *p, *p2;
  87. const wchar_t *pw;
  88. if (archive_string_ensure(as, 64) == NULL)
  89. __archive_errx(1, "Out of memory");
  90. if (fmt == NULL) {
  91. as->s[0] = 0;
  92. return;
  93. }
  94. for (p = fmt; *p != '\0'; p++) {
  95. const char *saved_p = p;
  96. if (*p != '%') {
  97. archive_strappend_char(as, *p);
  98. continue;
  99. }
  100. p++;
  101. long_flag = '\0';
  102. switch(*p) {
  103. case 'j':
  104. case 'l':
  105. case 'z':
  106. long_flag = *p;
  107. p++;
  108. break;
  109. }
  110. switch (*p) {
  111. case '%':
  112. archive_strappend_char(as, '%');
  113. break;
  114. case 'c':
  115. s = va_arg(ap, int);
  116. archive_strappend_char(as, (char)s);
  117. break;
  118. case 'd':
  119. switch(long_flag) {
  120. case 'j': s = va_arg(ap, intmax_t); break;
  121. case 'l': s = va_arg(ap, long); break;
  122. case 'z': s = va_arg(ap, ssize_t); break;
  123. default: s = va_arg(ap, int); break;
  124. }
  125. append_int(as, s, 10);
  126. break;
  127. case 's':
  128. switch(long_flag) {
  129. case 'l':
  130. pw = va_arg(ap, wchar_t *);
  131. if (pw == NULL)
  132. pw = L"(null)";
  133. if (archive_string_append_from_wcs(as, pw,
  134. wcslen(pw)) != 0 && errno == ENOMEM)
  135. __archive_errx(1, "Out of memory");
  136. break;
  137. default:
  138. p2 = va_arg(ap, char *);
  139. if (p2 == NULL)
  140. p2 = "(null)";
  141. archive_strcat(as, p2);
  142. break;
  143. }
  144. break;
  145. case 'S':
  146. pw = va_arg(ap, wchar_t *);
  147. if (pw == NULL)
  148. pw = L"(null)";
  149. if (archive_string_append_from_wcs(as, pw,
  150. wcslen(pw)) != 0 && errno == ENOMEM)
  151. __archive_errx(1, "Out of memory");
  152. break;
  153. case 'o': case 'u': case 'x': case 'X':
  154. /* Common handling for unsigned integer formats. */
  155. switch(long_flag) {
  156. case 'j': u = va_arg(ap, uintmax_t); break;
  157. case 'l': u = va_arg(ap, unsigned long); break;
  158. case 'z': u = va_arg(ap, size_t); break;
  159. default: u = va_arg(ap, unsigned int); break;
  160. }
  161. /* Format it in the correct base. */
  162. switch (*p) {
  163. case 'o': append_uint(as, u, 8); break;
  164. case 'u': append_uint(as, u, 10); break;
  165. default: append_uint(as, u, 16); break;
  166. }
  167. break;
  168. default:
  169. /* Rewind and print the initial '%' literally. */
  170. p = saved_p;
  171. archive_strappend_char(as, *p);
  172. }
  173. }
  174. }