vsnprintf.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Copyright (C) 1994-2019 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. <http://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 "strfile.h"
  24. static int _IO_strn_overflow (FILE *fp, int c) __THROW;
  25. static int
  26. _IO_strn_overflow (FILE *fp, int c)
  27. {
  28. /* When we come to here this means the user supplied buffer is
  29. filled. But since we must return the number of characters which
  30. would have been written in total we must provide a buffer for
  31. further use. We can do this by writing on and on in the overflow
  32. buffer in the _IO_strnfile structure. */
  33. _IO_strnfile *snf = (_IO_strnfile *) fp;
  34. if (fp->_IO_buf_base != snf->overflow_buf)
  35. {
  36. /* Terminate the string. We know that there is room for at
  37. least one more character since we initialized the stream with
  38. a size to make this possible. */
  39. *fp->_IO_write_ptr = '\0';
  40. _IO_setb (fp, snf->overflow_buf,
  41. snf->overflow_buf + sizeof (snf->overflow_buf), 0);
  42. fp->_IO_write_base = snf->overflow_buf;
  43. fp->_IO_read_base = snf->overflow_buf;
  44. fp->_IO_read_ptr = snf->overflow_buf;
  45. fp->_IO_read_end = snf->overflow_buf + sizeof (snf->overflow_buf);
  46. }
  47. fp->_IO_write_ptr = snf->overflow_buf;
  48. fp->_IO_write_end = snf->overflow_buf;
  49. /* Since we are not really interested in storing the characters
  50. which do not fit in the buffer we simply ignore it. */
  51. return c;
  52. }
  53. const struct _IO_jump_t _IO_strn_jumps libio_vtable attribute_hidden =
  54. {
  55. JUMP_INIT_DUMMY,
  56. JUMP_INIT(finish, _IO_str_finish),
  57. JUMP_INIT(overflow, _IO_strn_overflow),
  58. JUMP_INIT(underflow, _IO_str_underflow),
  59. JUMP_INIT(uflow, _IO_default_uflow),
  60. JUMP_INIT(pbackfail, _IO_str_pbackfail),
  61. JUMP_INIT(xsputn, _IO_default_xsputn),
  62. JUMP_INIT(xsgetn, _IO_default_xsgetn),
  63. JUMP_INIT(seekoff, _IO_str_seekoff),
  64. JUMP_INIT(seekpos, _IO_default_seekpos),
  65. JUMP_INIT(setbuf, _IO_default_setbuf),
  66. JUMP_INIT(sync, _IO_default_sync),
  67. JUMP_INIT(doallocate, _IO_default_doallocate),
  68. JUMP_INIT(read, _IO_default_read),
  69. JUMP_INIT(write, _IO_default_write),
  70. JUMP_INIT(seek, _IO_default_seek),
  71. JUMP_INIT(close, _IO_default_close),
  72. JUMP_INIT(stat, _IO_default_stat),
  73. JUMP_INIT(showmanyc, _IO_default_showmanyc),
  74. JUMP_INIT(imbue, _IO_default_imbue)
  75. };
  76. int
  77. __vsnprintf_internal (char *string, size_t maxlen, const char *format,
  78. va_list args, unsigned int mode_flags)
  79. {
  80. _IO_strnfile sf;
  81. int ret;
  82. #ifdef _IO_MTSAFE_IO
  83. sf.f._sbf._f._lock = NULL;
  84. #endif
  85. /* We need to handle the special case where MAXLEN is 0. Use the
  86. overflow buffer right from the start. */
  87. if (maxlen == 0)
  88. {
  89. string = sf.overflow_buf;
  90. maxlen = sizeof (sf.overflow_buf);
  91. }
  92. _IO_no_init (&sf.f._sbf._f, _IO_USER_LOCK, -1, NULL, NULL);
  93. _IO_JUMPS (&sf.f._sbf) = &_IO_strn_jumps;
  94. string[0] = '\0';
  95. _IO_str_init_static_internal (&sf.f, string, maxlen - 1, string);
  96. ret = __vfprintf_internal (&sf.f._sbf._f, format, args, mode_flags);
  97. if (sf.f._sbf._f._IO_buf_base != sf.overflow_buf)
  98. *sf.f._sbf._f._IO_write_ptr = '\0';
  99. return ret;
  100. }
  101. int
  102. ___vsnprintf (char *string, size_t maxlen, const char *format, va_list args)
  103. {
  104. return __vsnprintf_internal (string, maxlen, format, args, 0);
  105. }
  106. ldbl_weak_alias (___vsnprintf, __vsnprintf)
  107. ldbl_weak_alias (___vsnprintf, vsnprintf)