]> git.ipfire.org Git - thirdparty/glibc.git/blame - libio/vsnprintf.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / libio / vsnprintf.c
CommitLineData
bfff8b1b 1/* Copyright (C) 1994-2017 Free Software Foundation, Inc.
41bdb6e2 2 This file is part of the GNU C Library.
40a55d20 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.
40a55d20 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
40a55d20 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 12 Lesser General Public License for more details.
40a55d20 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>.
40a55d20 17
41bdb6e2
AJ
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. */
96aa2d94
RM
26
27#include "libioP.h"
28#include "strfile.h"
29
79937577 30static int _IO_strn_overflow (_IO_FILE *fp, int c) __THROW;
40a55d20 31
4cca6b86 32static int
9d46370c 33_IO_strn_overflow (_IO_FILE *fp, int c)
4cca6b86
UD
34{
35 /* When we come to here this means the user supplied buffer is
36 filled. But since we must return the number of characters which
37 would have been written in total we must provide a buffer for
38 further use. We can do this by writing on and on in the overflow
39 buffer in the _IO_strnfile structure. */
40 _IO_strnfile *snf = (_IO_strnfile *) fp;
41
42 if (fp->_IO_buf_base != snf->overflow_buf)
43 {
44 /* Terminate the string. We know that there is room for at
45 least one more character since we initialized the stream with
46 a size to make this possible. */
47 *fp->_IO_write_ptr = '\0';
48
d18ea0c5
AS
49 _IO_setb (fp, snf->overflow_buf,
50 snf->overflow_buf + sizeof (snf->overflow_buf), 0);
4cca6b86
UD
51
52 fp->_IO_write_base = snf->overflow_buf;
53 fp->_IO_read_base = snf->overflow_buf;
54 fp->_IO_read_ptr = snf->overflow_buf;
55 fp->_IO_read_end = snf->overflow_buf + sizeof (snf->overflow_buf);
56 }
57
58 fp->_IO_write_ptr = snf->overflow_buf;
59 fp->_IO_write_end = snf->overflow_buf;
60
61 /* Since we are not really interested in storing the characters
62 which do not fit in the buffer we simply ignore it. */
63 return c;
64}
65
66
db3476af 67const struct _IO_jump_t _IO_strn_jumps libio_vtable attribute_hidden =
40a55d20 68{
4cca6b86
UD
69 JUMP_INIT_DUMMY,
70 JUMP_INIT(finish, _IO_str_finish),
71 JUMP_INIT(overflow, _IO_strn_overflow),
d18ea0c5
AS
72 JUMP_INIT(underflow, _IO_str_underflow),
73 JUMP_INIT(uflow, _IO_default_uflow),
74 JUMP_INIT(pbackfail, _IO_str_pbackfail),
75 JUMP_INIT(xsputn, _IO_default_xsputn),
76 JUMP_INIT(xsgetn, _IO_default_xsgetn),
77 JUMP_INIT(seekoff, _IO_str_seekoff),
4cca6b86
UD
78 JUMP_INIT(seekpos, _IO_default_seekpos),
79 JUMP_INIT(setbuf, _IO_default_setbuf),
80 JUMP_INIT(sync, _IO_default_sync),
d18ea0c5 81 JUMP_INIT(doallocate, _IO_default_doallocate),
4cca6b86
UD
82 JUMP_INIT(read, _IO_default_read),
83 JUMP_INIT(write, _IO_default_write),
84 JUMP_INIT(seek, _IO_default_seek),
85 JUMP_INIT(close, _IO_default_close),
dfd2257a
UD
86 JUMP_INIT(stat, _IO_default_stat),
87 JUMP_INIT(showmanyc, _IO_default_showmanyc),
88 JUMP_INIT(imbue, _IO_default_imbue)
4cca6b86
UD
89};
90
91
96aa2d94 92int
f63f2bfd
JM
93_IO_vsnprintf (char *string, _IO_size_t maxlen, const char *format,
94 _IO_va_list args)
96aa2d94 95{
4cca6b86 96 _IO_strnfile sf;
96aa2d94 97 int ret;
499e7464 98#ifdef _IO_MTSAFE_IO
c020d48c 99 sf.f._sbf._f._lock = NULL;
499e7464 100#endif
7cc27f44 101
4cca6b86
UD
102 /* We need to handle the special case where MAXLEN is 0. Use the
103 overflow buffer right from the start. */
7cc27f44 104 if (maxlen == 0)
4cca6b86
UD
105 {
106 string = sf.overflow_buf;
107 maxlen = sizeof (sf.overflow_buf);
108 }
7cc27f44 109
c020d48c 110 _IO_no_init (&sf.f._sbf._f, _IO_USER_LOCK, -1, NULL, NULL);
f521be31 111 _IO_JUMPS (&sf.f._sbf) = &_IO_strn_jumps;
b259e746 112 string[0] = '\0';
40a54e4d 113 _IO_str_init_static_internal (&sf.f, string, maxlen - 1, string);
d18ea0c5 114 ret = _IO_vfprintf (&sf.f._sbf._f, format, args);
4cca6b86
UD
115
116 if (sf.f._sbf._f._IO_buf_base != sf.overflow_buf)
117 *sf.f._sbf._f._IO_write_ptr = '\0';
96aa2d94
RM
118 return ret;
119}
c6251f03
RM
120ldbl_weak_alias (_IO_vsnprintf, __vsnprintf)
121ldbl_weak_alias (_IO_vsnprintf, vsnprintf)