vprintf-support.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Estimate the length of the string generated by a vprintf-like
  2. function. Used by vasprintf and xvasprintf.
  3. Copyright (C) 1994, 2003, 2011, 2013, 2014 Free Software Foundation, Inc.
  4. This file is part of the libiberty library.
  5. Libiberty is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. Libiberty is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with libiberty; see the file COPYING.LIB. If not, write
  15. to the Free Software Foundation, Inc., 51 Franklin Street - Fifth
  16. Floor, Boston, MA 02110-1301, USA. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include <ansidecl.h>
  21. #include <stdarg.h>
  22. #if !defined (va_copy) && defined (__va_copy)
  23. # define va_copy(d,s) __va_copy((d),(s))
  24. #endif
  25. #include <stdio.h>
  26. #ifdef HAVE_STRING_H
  27. #include <string.h>
  28. #endif
  29. #ifdef HAVE_STDLIB_H
  30. #include <stdlib.h>
  31. #else
  32. extern unsigned long strtoul ();
  33. #endif
  34. #include "libiberty.h"
  35. int
  36. libiberty_vprintf_buffer_size (const char *format, va_list args)
  37. {
  38. const char *p = format;
  39. /* Add one to make sure that it is never zero, which might cause malloc
  40. to return NULL. */
  41. int total_width = strlen (format) + 1;
  42. va_list ap;
  43. #ifdef va_copy
  44. va_copy (ap, args);
  45. #else
  46. memcpy ((PTR) &ap, (PTR) &args, sizeof (va_list));
  47. #endif
  48. while (*p != '\0')
  49. {
  50. if (*p++ == '%')
  51. {
  52. while (strchr ("-+ #0", *p))
  53. ++p;
  54. if (*p == '*')
  55. {
  56. ++p;
  57. total_width += abs (va_arg (ap, int));
  58. }
  59. else
  60. total_width += strtoul (p, (char **) &p, 10);
  61. if (*p == '.')
  62. {
  63. ++p;
  64. if (*p == '*')
  65. {
  66. ++p;
  67. total_width += abs (va_arg (ap, int));
  68. }
  69. else
  70. total_width += strtoul (p, (char **) &p, 10);
  71. }
  72. while (strchr ("hlL", *p))
  73. ++p;
  74. /* Should be big enough for any format specifier except %s and floats. */
  75. total_width += 30;
  76. switch (*p)
  77. {
  78. case 'd':
  79. case 'i':
  80. case 'o':
  81. case 'u':
  82. case 'x':
  83. case 'X':
  84. case 'c':
  85. (void) va_arg (ap, int);
  86. break;
  87. case 'f':
  88. case 'e':
  89. case 'E':
  90. case 'g':
  91. case 'G':
  92. (void) va_arg (ap, double);
  93. /* Since an ieee double can have an exponent of 307, we'll
  94. make the buffer wide enough to cover the gross case. */
  95. total_width += 307;
  96. break;
  97. case 's':
  98. total_width += strlen (va_arg (ap, char *));
  99. break;
  100. case 'p':
  101. case 'n':
  102. (void) va_arg (ap, char *);
  103. break;
  104. }
  105. p++;
  106. }
  107. }
  108. #ifdef va_copy
  109. va_end (ap);
  110. #endif
  111. return total_width;
  112. }