1/* Copyright (C) 1994-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>.
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. */
26
27#include "libioP.h"
28#include "strfile.h"
29
30static int _IO_strn_overflow (_IO_FILE *fp, int c) __THROW;
31
32static int
33_IO_strn_overflow (_IO_FILE *fp, int c)
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
49 _IO_setb (fp, snf->overflow_buf,
50 snf->overflow_buf + sizeof (snf->overflow_buf), 0);
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
67const struct _IO_jump_t _IO_strn_jumps libio_vtable attribute_hidden =
68{
69 JUMP_INIT_DUMMY,
70 JUMP_INIT(finish, _IO_str_finish),
71 JUMP_INIT(overflow, _IO_strn_overflow),
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),
78 JUMP_INIT(seekpos, _IO_default_seekpos),
79 JUMP_INIT(setbuf, _IO_default_setbuf),
80 JUMP_INIT(sync, _IO_default_sync),
81 JUMP_INIT(doallocate, _IO_default_doallocate),
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),
86 JUMP_INIT(stat, _IO_default_stat),
87 JUMP_INIT(showmanyc, _IO_default_showmanyc),
88 JUMP_INIT(imbue, _IO_default_imbue)
89};
90
91
92int
93_IO_vsnprintf (char *string, _IO_size_t maxlen, const char *format,
94 _IO_va_list args)
95{
96 _IO_strnfile sf;
97 int ret;
98#ifdef _IO_MTSAFE_IO
99 sf.f._sbf._f._lock = NULL;
100#endif
101
102 /* We need to handle the special case where MAXLEN is 0. Use the
103 overflow buffer right from the start. */
104 if (maxlen == 0)
105 {
106 string = sf.overflow_buf;
107 maxlen = sizeof (sf.overflow_buf);
108 }
109
110 _IO_no_init (&sf.f._sbf._f, _IO_USER_LOCK, -1, NULL, NULL);
111 _IO_JUMPS (&sf.f._sbf) = &_IO_strn_jumps;
112 string[0] = '\0';
113 _IO_str_init_static_internal (&sf.f, string, maxlen - 1, string);
114 ret = _IO_vfprintf (&sf.f._sbf._f, format, args);
115
116 if (sf.f._sbf._f._IO_buf_base != sf.overflow_buf)
117 *sf.f._sbf._f._IO_write_ptr = '\0';
118 return ret;
119}
120ldbl_weak_alias (_IO_vsnprintf, __vsnprintf)
121ldbl_weak_alias (_IO_vsnprintf, vsnprintf)
122