vswprintf.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 wint_t _IO_wstrn_overflow (FILE *fp, wint_t c) __THROW;
  25. static wint_t
  26. _IO_wstrn_overflow (FILE *fp, wint_t 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_wstrnfile structure. */
  33. _IO_wstrnfile *snf = (_IO_wstrnfile *) fp;
  34. if (fp->_wide_data->_IO_buf_base != snf->overflow_buf)
  35. {
  36. _IO_wsetb (fp, snf->overflow_buf,
  37. snf->overflow_buf + (sizeof (snf->overflow_buf)
  38. / sizeof (wchar_t)), 0);
  39. fp->_wide_data->_IO_write_base = snf->overflow_buf;
  40. fp->_wide_data->_IO_read_base = snf->overflow_buf;
  41. fp->_wide_data->_IO_read_ptr = snf->overflow_buf;
  42. fp->_wide_data->_IO_read_end = (snf->overflow_buf
  43. + (sizeof (snf->overflow_buf)
  44. / sizeof (wchar_t)));
  45. }
  46. fp->_wide_data->_IO_write_ptr = snf->overflow_buf;
  47. fp->_wide_data->_IO_write_end = snf->overflow_buf;
  48. /* Since we are not really interested in storing the characters
  49. which do not fit in the buffer we simply ignore it. */
  50. return c;
  51. }
  52. const struct _IO_jump_t _IO_wstrn_jumps libio_vtable attribute_hidden =
  53. {
  54. JUMP_INIT_DUMMY,
  55. JUMP_INIT(finish, _IO_wstr_finish),
  56. JUMP_INIT(overflow, (_IO_overflow_t) _IO_wstrn_overflow),
  57. JUMP_INIT(underflow, (_IO_underflow_t) _IO_wstr_underflow),
  58. JUMP_INIT(uflow, (_IO_underflow_t) _IO_wdefault_uflow),
  59. JUMP_INIT(pbackfail, (_IO_pbackfail_t) _IO_wstr_pbackfail),
  60. JUMP_INIT(xsputn, _IO_wdefault_xsputn),
  61. JUMP_INIT(xsgetn, _IO_wdefault_xsgetn),
  62. JUMP_INIT(seekoff, _IO_wstr_seekoff),
  63. JUMP_INIT(seekpos, _IO_default_seekpos),
  64. JUMP_INIT(setbuf, _IO_default_setbuf),
  65. JUMP_INIT(sync, _IO_default_sync),
  66. JUMP_INIT(doallocate, _IO_wdefault_doallocate),
  67. JUMP_INIT(read, _IO_default_read),
  68. JUMP_INIT(write, _IO_default_write),
  69. JUMP_INIT(seek, _IO_default_seek),
  70. JUMP_INIT(close, _IO_default_close),
  71. JUMP_INIT(stat, _IO_default_stat),
  72. JUMP_INIT(showmanyc, _IO_default_showmanyc),
  73. JUMP_INIT(imbue, _IO_default_imbue)
  74. };
  75. int
  76. __vswprintf_internal (wchar_t *string, size_t maxlen, const wchar_t *format,
  77. va_list args, unsigned int mode_flags)
  78. {
  79. _IO_wstrnfile sf;
  80. int ret;
  81. struct _IO_wide_data wd;
  82. #ifdef _IO_MTSAFE_IO
  83. sf.f._sbf._f._lock = NULL;
  84. #endif
  85. if (maxlen == 0)
  86. /* Since we have to write at least the terminating L'\0' a buffer
  87. length of zero always makes the function fail. */
  88. return -1;
  89. _IO_no_init (&sf.f._sbf._f, _IO_USER_LOCK, 0, &wd, &_IO_wstrn_jumps);
  90. _IO_fwide (&sf.f._sbf._f, 1);
  91. string[0] = L'\0';
  92. _IO_wstr_init_static (&sf.f._sbf._f, string, maxlen - 1, string);
  93. ret = __vfwprintf_internal ((FILE *) &sf.f._sbf, format, args, mode_flags);
  94. if (sf.f._sbf._f._wide_data->_IO_buf_base == sf.overflow_buf)
  95. /* ISO C99 requires swprintf/vswprintf to return an error if the
  96. output does not fit in the provided buffer. */
  97. return -1;
  98. /* Terminate the string. */
  99. *sf.f._sbf._f._wide_data->_IO_write_ptr = '\0';
  100. return ret;
  101. }
  102. int
  103. __vswprintf (wchar_t *string, size_t maxlen, const wchar_t *format,
  104. va_list args)
  105. {
  106. return __vswprintf_internal (string, maxlen, format, args, 0);
  107. }
  108. ldbl_weak_alias (__vswprintf, vswprintf)