vsnprintf.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* Implement the vsnprintf function.
  2. Copyright (C) 2003, 2004, 2005, 2011, 2013 Free Software Foundation, Inc.
  3. Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>.
  4. This file is part of the libiberty library. This library is free
  5. software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. This library 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
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU CC; see the file COPYING. If not, write to
  15. the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  16. As a special exception, if you link this library with files
  17. compiled with a GNU compiler to produce an executable, this does not cause
  18. the resulting executable to be covered by the GNU General Public License.
  19. This exception does not however invalidate any other reasons why
  20. the executable file might be covered by the GNU General Public License. */
  21. /*
  22. @deftypefn Supplemental int vsnprintf (char *@var{buf}, size_t @var{n}, @
  23. const char *@var{format}, va_list @var{ap})
  24. This function is similar to @code{vsprintf}, but it will write to
  25. @var{buf} at most @code{@var{n}-1} bytes of text, followed by a
  26. terminating null byte, for a total of @var{n} bytes. On error the
  27. return value is -1, otherwise it returns the number of characters that
  28. would have been printed had @var{n} been sufficiently large,
  29. regardless of the actual value of @var{n}. Note some pre-C99 system
  30. libraries do not implement this correctly so users cannot generally
  31. rely on the return value if the system version of this function is
  32. used.
  33. @end deftypefn
  34. */
  35. #include "config.h"
  36. #include "ansidecl.h"
  37. #include <stdarg.h>
  38. #ifdef HAVE_STRING_H
  39. #include <string.h>
  40. #endif
  41. #ifdef HAVE_STDLIB_H
  42. #include <stdlib.h>
  43. #endif
  44. #include "libiberty.h"
  45. /* This implementation relies on a working vasprintf. */
  46. int
  47. vsnprintf (char *s, size_t n, const char *format, va_list ap)
  48. {
  49. char *buf = 0;
  50. int result = vasprintf (&buf, format, ap);
  51. if (!buf)
  52. return -1;
  53. if (result < 0)
  54. {
  55. free (buf);
  56. return -1;
  57. }
  58. result = strlen (buf);
  59. if (n > 0)
  60. {
  61. if ((long) n > result)
  62. memcpy (s, buf, result+1);
  63. else
  64. {
  65. memcpy (s, buf, n-1);
  66. s[n - 1] = 0;
  67. }
  68. }
  69. free (buf);
  70. return result;
  71. }
  72. #ifdef TEST
  73. /* Set the buffer to a known state. */
  74. #define CLEAR(BUF) do { memset ((BUF), 'X', sizeof (BUF)); (BUF)[14] = '\0'; } while (0)
  75. /* For assertions. */
  76. #define VERIFY(P) do { if (!(P)) abort(); } while (0)
  77. static int ATTRIBUTE_PRINTF_3
  78. checkit (char *s, size_t n, const char *format, ...)
  79. {
  80. int result;
  81. va_list ap;
  82. va_start (ap, format);
  83. result = vsnprintf (s, n, format, ap);
  84. va_end (ap);
  85. return result;
  86. }
  87. extern int main (void);
  88. int
  89. main (void)
  90. {
  91. char buf[128];
  92. int status;
  93. CLEAR (buf);
  94. status = checkit (buf, 10, "%s:%d", "foobar", 9);
  95. VERIFY (status==8 && memcmp (buf, "foobar:9\0XXXXX\0", 15) == 0);
  96. CLEAR (buf);
  97. status = checkit (buf, 9, "%s:%d", "foobar", 9);
  98. VERIFY (status==8 && memcmp (buf, "foobar:9\0XXXXX\0", 15) == 0);
  99. CLEAR (buf);
  100. status = checkit (buf, 8, "%s:%d", "foobar", 9);
  101. VERIFY (status==8 && memcmp (buf, "foobar:\0XXXXXX\0", 15) == 0);
  102. CLEAR (buf);
  103. status = checkit (buf, 7, "%s:%d", "foobar", 9);
  104. VERIFY (status==8 && memcmp (buf, "foobar\0XXXXXXX\0", 15) == 0);
  105. CLEAR (buf);
  106. status = checkit (buf, 6, "%s:%d", "foobar", 9);
  107. VERIFY (status==8 && memcmp (buf, "fooba\0XXXXXXXX\0", 15) == 0);
  108. CLEAR (buf);
  109. status = checkit (buf, 2, "%s:%d", "foobar", 9);
  110. VERIFY (status==8 && memcmp (buf, "f\0XXXXXXXXXXXX\0", 15) == 0);
  111. CLEAR (buf);
  112. status = checkit (buf, 1, "%s:%d", "foobar", 9);
  113. VERIFY (status==8 && memcmp (buf, "\0XXXXXXXXXXXXX\0", 15) == 0);
  114. CLEAR (buf);
  115. status = checkit (buf, 0, "%s:%d", "foobar", 9);
  116. VERIFY (status==8 && memcmp (buf, "XXXXXXXXXXXXXX\0", 15) == 0);
  117. return 0;
  118. }
  119. #endif /* TEST */