]>
Commit | Line | Data |
---|---|---|
b168057a | 1 | /* Copyright (C) 1994-2015 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 | 30 | static int _IO_strn_overflow (_IO_FILE *fp, int c) __THROW; |
40a55d20 | 31 | |
4cca6b86 | 32 | static int |
40a55d20 UD |
33 | _IO_strn_overflow (fp, c) |
34 | _IO_FILE *fp; | |
35 | int c; | |
4cca6b86 UD |
36 | { |
37 | /* When we come to here this means the user supplied buffer is | |
38 | filled. But since we must return the number of characters which | |
39 | would have been written in total we must provide a buffer for | |
40 | further use. We can do this by writing on and on in the overflow | |
41 | buffer in the _IO_strnfile structure. */ | |
42 | _IO_strnfile *snf = (_IO_strnfile *) fp; | |
43 | ||
44 | if (fp->_IO_buf_base != snf->overflow_buf) | |
45 | { | |
46 | /* Terminate the string. We know that there is room for at | |
47 | least one more character since we initialized the stream with | |
48 | a size to make this possible. */ | |
49 | *fp->_IO_write_ptr = '\0'; | |
50 | ||
d18ea0c5 AS |
51 | _IO_setb (fp, snf->overflow_buf, |
52 | snf->overflow_buf + sizeof (snf->overflow_buf), 0); | |
4cca6b86 UD |
53 | |
54 | fp->_IO_write_base = snf->overflow_buf; | |
55 | fp->_IO_read_base = snf->overflow_buf; | |
56 | fp->_IO_read_ptr = snf->overflow_buf; | |
57 | fp->_IO_read_end = snf->overflow_buf + sizeof (snf->overflow_buf); | |
58 | } | |
59 | ||
60 | fp->_IO_write_ptr = snf->overflow_buf; | |
61 | fp->_IO_write_end = snf->overflow_buf; | |
62 | ||
63 | /* Since we are not really interested in storing the characters | |
64 | which do not fit in the buffer we simply ignore it. */ | |
65 | return c; | |
66 | } | |
67 | ||
68 | ||
b5cc329c | 69 | const struct _IO_jump_t _IO_strn_jumps attribute_hidden = |
40a55d20 | 70 | { |
4cca6b86 UD |
71 | JUMP_INIT_DUMMY, |
72 | JUMP_INIT(finish, _IO_str_finish), | |
73 | JUMP_INIT(overflow, _IO_strn_overflow), | |
d18ea0c5 AS |
74 | JUMP_INIT(underflow, _IO_str_underflow), |
75 | JUMP_INIT(uflow, _IO_default_uflow), | |
76 | JUMP_INIT(pbackfail, _IO_str_pbackfail), | |
77 | JUMP_INIT(xsputn, _IO_default_xsputn), | |
78 | JUMP_INIT(xsgetn, _IO_default_xsgetn), | |
79 | JUMP_INIT(seekoff, _IO_str_seekoff), | |
4cca6b86 UD |
80 | JUMP_INIT(seekpos, _IO_default_seekpos), |
81 | JUMP_INIT(setbuf, _IO_default_setbuf), | |
82 | JUMP_INIT(sync, _IO_default_sync), | |
d18ea0c5 | 83 | JUMP_INIT(doallocate, _IO_default_doallocate), |
4cca6b86 UD |
84 | JUMP_INIT(read, _IO_default_read), |
85 | JUMP_INIT(write, _IO_default_write), | |
86 | JUMP_INIT(seek, _IO_default_seek), | |
87 | JUMP_INIT(close, _IO_default_close), | |
dfd2257a UD |
88 | JUMP_INIT(stat, _IO_default_stat), |
89 | JUMP_INIT(showmanyc, _IO_default_showmanyc), | |
90 | JUMP_INIT(imbue, _IO_default_imbue) | |
4cca6b86 UD |
91 | }; |
92 | ||
93 | ||
96aa2d94 RM |
94 | int |
95 | _IO_vsnprintf (string, maxlen, format, args) | |
96 | char *string; | |
97 | _IO_size_t maxlen; | |
98 | const char *format; | |
99 | _IO_va_list args; | |
100 | { | |
4cca6b86 | 101 | _IO_strnfile sf; |
96aa2d94 | 102 | int ret; |
499e7464 | 103 | #ifdef _IO_MTSAFE_IO |
c020d48c | 104 | sf.f._sbf._f._lock = NULL; |
499e7464 | 105 | #endif |
7cc27f44 | 106 | |
4cca6b86 UD |
107 | /* We need to handle the special case where MAXLEN is 0. Use the |
108 | overflow buffer right from the start. */ | |
7cc27f44 | 109 | if (maxlen == 0) |
4cca6b86 UD |
110 | { |
111 | string = sf.overflow_buf; | |
112 | maxlen = sizeof (sf.overflow_buf); | |
113 | } | |
7cc27f44 | 114 | |
c020d48c | 115 | _IO_no_init (&sf.f._sbf._f, _IO_USER_LOCK, -1, NULL, NULL); |
f521be31 | 116 | _IO_JUMPS (&sf.f._sbf) = &_IO_strn_jumps; |
b259e746 | 117 | string[0] = '\0'; |
40a54e4d | 118 | _IO_str_init_static_internal (&sf.f, string, maxlen - 1, string); |
d18ea0c5 | 119 | ret = _IO_vfprintf (&sf.f._sbf._f, format, args); |
4cca6b86 UD |
120 | |
121 | if (sf.f._sbf._f._IO_buf_base != sf.overflow_buf) | |
122 | *sf.f._sbf._f._IO_write_ptr = '\0'; | |
96aa2d94 RM |
123 | return ret; |
124 | } | |
c6251f03 RM |
125 | ldbl_weak_alias (_IO_vsnprintf, __vsnprintf) |
126 | ldbl_weak_alias (_IO_vsnprintf, vsnprintf) |