]>
Commit | Line | Data |
---|---|---|
2c6fe0bd | 1 | /* Print output of stream to given obstack. |
581c785b | 2 | Copyright (C) 1996-2022 Free Software Foundation, Inc. |
2c6fe0bd | 3 | This file is part of the GNU C Library. |
2c6fe0bd UD |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
2c6fe0bd UD |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
2c6fe0bd | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
2c6fe0bd UD |
18 | |
19 | ||
2c6fe0bd | 20 | #include <stdlib.h> |
2c6fe0bd | 21 | #include "libioP.h" |
0317eaec | 22 | #include "strfile.h" |
a211d174 | 23 | #include <assert.h> |
2c6fe0bd UD |
24 | #include <string.h> |
25 | #include <errno.h> | |
26 | #include <obstack.h> | |
27 | #include <stdarg.h> | |
0d875352 | 28 | #include <stdio_ext.h> |
2c6fe0bd UD |
29 | |
30 | ||
31 | struct _IO_obstack_file | |
32 | { | |
27995311 | 33 | struct _IO_FILE_plus file; |
2c6fe0bd UD |
34 | struct obstack *obstack; |
35 | }; | |
36 | ||
37 | ||
38 | static int | |
9964a145 | 39 | _IO_obstack_overflow (FILE *fp, int c) |
2c6fe0bd UD |
40 | { |
41 | struct obstack *obstack = ((struct _IO_obstack_file *) fp)->obstack; | |
a211d174 | 42 | int size; |
2c6fe0bd UD |
43 | |
44 | /* Make room for another character. This might as well allocate a | |
45 | new chunk a memory and moves the old contents over. */ | |
a211d174 UD |
46 | assert (c != EOF); |
47 | obstack_1grow (obstack, c); | |
2c6fe0bd UD |
48 | |
49 | /* Setup the buffer pointers again. */ | |
50 | fp->_IO_write_base = obstack_base (obstack); | |
51 | fp->_IO_write_ptr = obstack_next_free (obstack); | |
a211d174 UD |
52 | size = obstack_room (obstack); |
53 | fp->_IO_write_end = fp->_IO_write_ptr + size; | |
2c6fe0bd | 54 | /* Now allocate the rest of the current chunk. */ |
a211d174 | 55 | obstack_blank_fast (obstack, size); |
2c6fe0bd UD |
56 | |
57 | return c; | |
58 | } | |
59 | ||
60 | ||
9964a145 ZW |
61 | static size_t |
62 | _IO_obstack_xsputn (FILE *fp, const void *data, size_t n) | |
2c6fe0bd UD |
63 | { |
64 | struct obstack *obstack = ((struct _IO_obstack_file *) fp)->obstack; | |
65 | ||
66 | if (fp->_IO_write_ptr + n > fp->_IO_write_end) | |
67 | { | |
a211d174 UD |
68 | int size; |
69 | ||
2c6fe0bd UD |
70 | /* We need some more memory. First shrink the buffer to the |
71 | space we really currently need. */ | |
27995311 | 72 | obstack_blank_fast (obstack, fp->_IO_write_ptr - fp->_IO_write_end); |
2c6fe0bd | 73 | |
27995311 UD |
74 | /* Now grow for N bytes, and put the data there. */ |
75 | obstack_grow (obstack, data, n); | |
2c6fe0bd UD |
76 | |
77 | /* Setup the buffer pointers again. */ | |
78 | fp->_IO_write_base = obstack_base (obstack); | |
79 | fp->_IO_write_ptr = obstack_next_free (obstack); | |
a211d174 UD |
80 | size = obstack_room (obstack); |
81 | fp->_IO_write_end = fp->_IO_write_ptr + size; | |
2c6fe0bd | 82 | /* Now allocate the rest of the current chunk. */ |
a211d174 | 83 | obstack_blank_fast (obstack, size); |
2c6fe0bd UD |
84 | } |
85 | else | |
86187531 | 86 | fp->_IO_write_ptr = __mempcpy (fp->_IO_write_ptr, data, n); |
2c6fe0bd UD |
87 | |
88 | return n; | |
89 | } | |
90 | ||
91 | ||
92 | /* the jump table. */ | |
db3476af | 93 | const struct _IO_jump_t _IO_obstack_jumps libio_vtable attribute_hidden = |
2c6fe0bd UD |
94 | { |
95 | JUMP_INIT_DUMMY, | |
96 | JUMP_INIT(finish, NULL), | |
97 | JUMP_INIT(overflow, _IO_obstack_overflow), | |
98 | JUMP_INIT(underflow, NULL), | |
99 | JUMP_INIT(uflow, NULL), | |
100 | JUMP_INIT(pbackfail, NULL), | |
101 | JUMP_INIT(xsputn, _IO_obstack_xsputn), | |
102 | JUMP_INIT(xsgetn, NULL), | |
103 | JUMP_INIT(seekoff, NULL), | |
104 | JUMP_INIT(seekpos, NULL), | |
105 | JUMP_INIT(setbuf, NULL), | |
106 | JUMP_INIT(sync, NULL), | |
107 | JUMP_INIT(doallocate, NULL), | |
108 | JUMP_INIT(read, NULL), | |
109 | JUMP_INIT(write, NULL), | |
110 | JUMP_INIT(seek, NULL), | |
111 | JUMP_INIT(close, NULL), | |
dfd2257a UD |
112 | JUMP_INIT(stat, NULL), |
113 | JUMP_INIT(showmanyc, NULL), | |
114 | JUMP_INIT(imbue, NULL) | |
2c6fe0bd UD |
115 | }; |
116 | ||
117 | ||
118 | int | |
698fb75b ZW |
119 | __obstack_vprintf_internal (struct obstack *obstack, const char *format, |
120 | va_list args, unsigned int mode_flags) | |
2c6fe0bd UD |
121 | { |
122 | struct obstack_FILE | |
123 | { | |
124 | struct _IO_obstack_file ofile; | |
2c6fe0bd UD |
125 | } new_f; |
126 | int result; | |
a211d174 UD |
127 | int size; |
128 | int room; | |
2c6fe0bd UD |
129 | |
130 | #ifdef _IO_MTSAFE_IO | |
c020d48c | 131 | new_f.ofile.file.file._lock = NULL; |
2c6fe0bd UD |
132 | #endif |
133 | ||
c020d48c | 134 | _IO_no_init (&new_f.ofile.file.file, _IO_USER_LOCK, -1, NULL, NULL); |
d64b6ad0 | 135 | _IO_JUMPS (&new_f.ofile.file) = &_IO_obstack_jumps; |
a211d174 UD |
136 | room = obstack_room (obstack); |
137 | size = obstack_object_size (obstack) + room; | |
138 | if (size == 0) | |
139 | { | |
140 | /* We have to handle the allocation a bit different since the | |
141 | `_IO_str_init_static' function would handle a size of zero | |
142 | different from what we expect. */ | |
a211d174 UD |
143 | |
144 | /* Get more memory. */ | |
b420597e | 145 | obstack_make_room (obstack, 64); |
a211d174 | 146 | |
b420597e | 147 | /* Recompute how much room we have. */ |
a211d174 | 148 | room = obstack_room (obstack); |
b420597e | 149 | size = room; |
a211d174 UD |
150 | |
151 | assert (size != 0); | |
152 | } | |
153 | ||
40a54e4d UD |
154 | _IO_str_init_static_internal ((struct _IO_strfile_ *) &new_f.ofile, |
155 | obstack_base (obstack), | |
156 | size, obstack_next_free (obstack)); | |
2c6fe0bd | 157 | /* Now allocate the rest of the current chunk. */ |
a211d174 UD |
158 | assert (size == (new_f.ofile.file.file._IO_write_end |
159 | - new_f.ofile.file.file._IO_write_base)); | |
160 | assert (new_f.ofile.file.file._IO_write_ptr | |
161 | == (new_f.ofile.file.file._IO_write_base | |
162 | + obstack_object_size (obstack))); | |
163 | obstack_blank_fast (obstack, room); | |
164 | ||
2c6fe0bd UD |
165 | new_f.ofile.obstack = obstack; |
166 | ||
698fb75b ZW |
167 | result = __vfprintf_internal (&new_f.ofile.file.file, format, args, |
168 | mode_flags); | |
2c6fe0bd UD |
169 | |
170 | /* Shrink the buffer to the space we really currently need. */ | |
27995311 UD |
171 | obstack_blank_fast (obstack, (new_f.ofile.file.file._IO_write_ptr |
172 | - new_f.ofile.file.file._IO_write_end)); | |
2c6fe0bd UD |
173 | |
174 | return result; | |
175 | } | |
2c6fe0bd | 176 | |
698fb75b ZW |
177 | int |
178 | __obstack_vprintf (struct obstack *obstack, const char *format, va_list ap) | |
179 | { | |
180 | return __obstack_vprintf_internal (obstack, format, ap, 0); | |
181 | } | |
182 | ldbl_weak_alias (__obstack_vprintf, obstack_vprintf) | |
2c6fe0bd UD |
183 | |
184 | int | |
698fb75b | 185 | __obstack_printf (struct obstack *obstack, const char *format, ...) |
2c6fe0bd UD |
186 | { |
187 | int result; | |
188 | va_list ap; | |
189 | va_start (ap, format); | |
698fb75b | 190 | result = __obstack_vprintf_internal (obstack, format, ap, 0); |
2c6fe0bd UD |
191 | va_end (ap); |
192 | return result; | |
193 | } | |
698fb75b | 194 | ldbl_weak_alias (__obstack_printf, obstack_printf) |