]> git.ipfire.org Git - thirdparty/glibc.git/blame - libio/vswprintf.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / libio / vswprintf.c
CommitLineData
04277e02 1/* Copyright (C) 1994-2019 Free Software Foundation, Inc.
41bdb6e2 2 This file is part of the GNU C Library.
d64b6ad0 3
41bdb6e2
AJ
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
d64b6ad0 8
41bdb6e2
AJ
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
d64b6ad0 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2
AJ
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
59ba27a6 15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>.
41bdb6e2
AJ
17
18 As a special exception, if you link the code in this file with
19 files compiled with a GNU compiler to produce an executable,
20 that does not cause the resulting executable to be covered by
21 the GNU Lesser General Public License. This exception does not
22 however invalidate any other reasons why the executable file
23 might be covered by the GNU Lesser General Public License.
24 This exception applies to code released by its copyright holders
25 in files containing the exception. */
d64b6ad0
UD
26
27#include "libioP.h"
28#include "strfile.h"
29
30
9964a145 31static wint_t _IO_wstrn_overflow (FILE *fp, wint_t c) __THROW;
d64b6ad0
UD
32
33static wint_t
9964a145 34_IO_wstrn_overflow (FILE *fp, wint_t c)
d64b6ad0
UD
35{
36 /* When we come to here this means the user supplied buffer is
37 filled. But since we must return the number of characters which
38 would have been written in total we must provide a buffer for
39 further use. We can do this by writing on and on in the overflow
b5cc329c
UD
40 buffer in the _IO_wstrnfile structure. */
41 _IO_wstrnfile *snf = (_IO_wstrnfile *) fp;
d64b6ad0
UD
42
43 if (fp->_wide_data->_IO_buf_base != snf->overflow_buf)
44 {
d18ea0c5
AS
45 _IO_wsetb (fp, snf->overflow_buf,
46 snf->overflow_buf + (sizeof (snf->overflow_buf)
47 / sizeof (wchar_t)), 0);
d64b6ad0
UD
48
49 fp->_wide_data->_IO_write_base = snf->overflow_buf;
50 fp->_wide_data->_IO_read_base = snf->overflow_buf;
51 fp->_wide_data->_IO_read_ptr = snf->overflow_buf;
52 fp->_wide_data->_IO_read_end = (snf->overflow_buf
53 + (sizeof (snf->overflow_buf)
54 / sizeof (wchar_t)));
55 }
56
57 fp->_wide_data->_IO_write_ptr = snf->overflow_buf;
58 fp->_wide_data->_IO_write_end = snf->overflow_buf;
59
60 /* Since we are not really interested in storing the characters
61 which do not fit in the buffer we simply ignore it. */
62 return c;
63}
64
65
db3476af 66const struct _IO_jump_t _IO_wstrn_jumps libio_vtable attribute_hidden =
d64b6ad0
UD
67{
68 JUMP_INIT_DUMMY,
69 JUMP_INIT(finish, _IO_wstr_finish),
70 JUMP_INIT(overflow, (_IO_overflow_t) _IO_wstrn_overflow),
71 JUMP_INIT(underflow, (_IO_underflow_t) _IO_wstr_underflow),
d18ea0c5 72 JUMP_INIT(uflow, (_IO_underflow_t) _IO_wdefault_uflow),
d64b6ad0 73 JUMP_INIT(pbackfail, (_IO_pbackfail_t) _IO_wstr_pbackfail),
d18ea0c5
AS
74 JUMP_INIT(xsputn, _IO_wdefault_xsputn),
75 JUMP_INIT(xsgetn, _IO_wdefault_xsgetn),
d64b6ad0
UD
76 JUMP_INIT(seekoff, _IO_wstr_seekoff),
77 JUMP_INIT(seekpos, _IO_default_seekpos),
bff334e0 78 JUMP_INIT(setbuf, _IO_default_setbuf),
d64b6ad0 79 JUMP_INIT(sync, _IO_default_sync),
d18ea0c5 80 JUMP_INIT(doallocate, _IO_wdefault_doallocate),
d64b6ad0
UD
81 JUMP_INIT(read, _IO_default_read),
82 JUMP_INIT(write, _IO_default_write),
83 JUMP_INIT(seek, _IO_default_seek),
84 JUMP_INIT(close, _IO_default_close),
85 JUMP_INIT(stat, _IO_default_stat),
86 JUMP_INIT(showmanyc, _IO_default_showmanyc),
87 JUMP_INIT(imbue, _IO_default_imbue)
88};
89
90
91int
698fb75b
ZW
92__vswprintf_internal (wchar_t *string, size_t maxlen, const wchar_t *format,
93 va_list args, unsigned int mode_flags)
d64b6ad0 94{
b5cc329c 95 _IO_wstrnfile sf;
d64b6ad0
UD
96 int ret;
97 struct _IO_wide_data wd;
98#ifdef _IO_MTSAFE_IO
c020d48c 99 sf.f._sbf._f._lock = NULL;
d64b6ad0
UD
100#endif
101
d64b6ad0 102 if (maxlen == 0)
5569e0a6
UD
103 /* Since we have to write at least the terminating L'\0' a buffer
104 length of zero always makes the function fail. */
105 return -1;
d64b6ad0 106
c020d48c 107 _IO_no_init (&sf.f._sbf._f, _IO_USER_LOCK, 0, &wd, &_IO_wstrn_jumps);
d64b6ad0
UD
108 _IO_fwide (&sf.f._sbf._f, 1);
109 string[0] = L'\0';
e1b13a63 110 _IO_wstr_init_static (&sf.f._sbf._f, string, maxlen - 1, string);
698fb75b 111 ret = __vfwprintf_internal ((FILE *) &sf.f._sbf, format, args, mode_flags);
d64b6ad0 112
5569e0a6
UD
113 if (sf.f._sbf._f._wide_data->_IO_buf_base == sf.overflow_buf)
114 /* ISO C99 requires swprintf/vswprintf to return an error if the
11bf311e 115 output does not fit in the provided buffer. */
5569e0a6
UD
116 return -1;
117
118 /* Terminate the string. */
119 *sf.f._sbf._f._wide_data->_IO_write_ptr = '\0';
120
d64b6ad0
UD
121 return ret;
122}
698fb75b
ZW
123
124int
125__vswprintf (wchar_t *string, size_t maxlen, const wchar_t *format,
126 va_list args)
127{
128 return __vswprintf_internal (string, maxlen, format, args, 0);
129}
130ldbl_weak_alias (__vswprintf, vswprintf)