vsnprintf.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* Copyright (C) 1994-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>.
  14. As a special exception, if you link the code in this file with
  15. files compiled with a GNU compiler to produce an executable,
  16. that does not cause the resulting executable to be covered by
  17. the GNU Lesser General Public License. This exception does not
  18. however invalidate any other reasons why the executable file
  19. might be covered by the GNU Lesser General Public License.
  20. This exception applies to code released by its copyright holders
  21. in files containing the exception. */
  22. #include "libioP.h"
  23. #include <array_length.h>
  24. #include <printf.h>
  25. #include <printf_buffer.h>
  26. void
  27. __printf_buffer_flush_snprintf (struct __printf_buffer_snprintf *buf)
  28. {
  29. /* Record the bytes written so far, before switching buffers. */
  30. buf->base.written += buf->base.write_ptr - buf->base.write_base;
  31. if (buf->base.write_base != buf->discard)
  32. {
  33. /* We just finished writing the caller-supplied buffer. Force
  34. NUL termination if the string length is not zero. */
  35. if (buf->base.write_base != buf->base.write_end)
  36. buf->base.write_end[-1] = '\0';
  37. /* Switch to the discard buffer. */
  38. buf->base.write_base = buf->discard;
  39. buf->base.write_ptr = buf->discard;
  40. buf->base.write_end = array_end (buf->discard);
  41. }
  42. buf->base.write_base = buf->discard;
  43. buf->base.write_ptr = buf->discard;
  44. }
  45. void
  46. __printf_buffer_snprintf_init (struct __printf_buffer_snprintf *buf,
  47. char *buffer, size_t length)
  48. {
  49. __printf_buffer_init (&buf->base, buffer, length,
  50. __printf_buffer_mode_snprintf);
  51. if (length > 0)
  52. /* Historic behavior for trivially overlapping buffers (checked by
  53. the test suite). */
  54. *buffer = '\0';
  55. }
  56. int
  57. __printf_buffer_snprintf_done (struct __printf_buffer_snprintf *buf)
  58. {
  59. /* NB: Do not check for buf->base.fail here. Write the null
  60. terminator even in case of errors. */
  61. if (buf->base.write_ptr < buf->base.write_end)
  62. *buf->base.write_ptr = '\0';
  63. else if (buf->base.write_ptr > buf->base.write_base)
  64. /* If write_ptr == write_base, nothing has been written. No null
  65. termination is needed because of the early truncation in
  66. __printf_buffer_snprintf_init (the historic behavior).
  67. We might also be at the start of the discard buffer, but in
  68. this case __printf_buffer_flush_snprintf has already written
  69. the NUL terminator. */
  70. buf->base.write_ptr[-1] = '\0';
  71. return __printf_buffer_done (&buf->base);
  72. }
  73. int
  74. __vsnprintf_internal (char *string, size_t maxlen, const char *format,
  75. va_list args, unsigned int mode_flags)
  76. {
  77. struct __printf_buffer_snprintf buf;
  78. __printf_buffer_snprintf_init (&buf, string, maxlen);
  79. __printf_buffer (&buf.base, format, args, mode_flags);
  80. return __printf_buffer_snprintf_done (&buf);
  81. }
  82. int
  83. ___vsnprintf (char *string, size_t maxlen, const char *format, va_list args)
  84. {
  85. return __vsnprintf_internal (string, maxlen, format, args, 0);
  86. }
  87. ldbl_weak_alias (___vsnprintf, __vsnprintf)
  88. ldbl_weak_alias (___vsnprintf, vsnprintf)