]>
Commit | Line | Data |
---|---|---|
b168057a | 1 | /* Copyright (C) 1993-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 AJ |
12 | Lesser General Public License for more details. |
13 | ||
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/>. | |
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. */ | |
96aa2d94 RM |
26 | |
27 | /* Generic or default I/O operations. */ | |
28 | ||
29 | #include "libioP.h" | |
96aa2d94 | 30 | #include <stdlib.h> |
96aa2d94 | 31 | #include <string.h> |
8d24de8b | 32 | #include <stdbool.h> |
e2c7e1de RM |
33 | #ifdef _LIBC |
34 | #include <sched.h> | |
35 | #endif | |
96aa2d94 | 36 | |
51e176c2 | 37 | #ifdef _IO_MTSAFE_IO |
118bad87 | 38 | static _IO_lock_t list_all_lock = _IO_lock_initializer; |
51e176c2 | 39 | #endif |
118bad87 | 40 | |
3afd9491 UD |
41 | /* Used to signal modifications to the list of FILE decriptors. */ |
42 | static int _IO_list_all_stamp; | |
43 | ||
beafb752 UD |
44 | |
45 | static _IO_FILE *run_fp; | |
46 | ||
bea9b193 | 47 | #ifdef _IO_MTSAFE_IO |
beafb752 UD |
48 | static void |
49 | flush_cleanup (void *not_used) | |
50 | { | |
51 | if (run_fp != NULL) | |
52 | _IO_funlockfile (run_fp); | |
53 | _IO_lock_unlock (list_all_lock); | |
54 | } | |
bea9b193 | 55 | #endif |
beafb752 | 56 | |
96aa2d94 | 57 | void |
9d46370c | 58 | _IO_un_link (struct _IO_FILE_plus *fp) |
40a55d20 | 59 | { |
2ca8b1ee | 60 | if (fp->file._flags & _IO_LINKED) |
40a55d20 | 61 | { |
cedb4109 | 62 | struct _IO_FILE **f; |
d328b80b | 63 | #ifdef _IO_MTSAFE_IO |
beafb752 | 64 | _IO_cleanup_region_start_noarg (flush_cleanup); |
118bad87 | 65 | _IO_lock_lock (list_all_lock); |
beafb752 UD |
66 | run_fp = (_IO_FILE *) fp; |
67 | _IO_flockfile ((_IO_FILE *) fp); | |
51e176c2 | 68 | #endif |
d18ea0c5 | 69 | if (_IO_list_all == NULL) |
cedb4109 | 70 | ; |
d18ea0c5 | 71 | else if (fp == _IO_list_all) |
40a55d20 | 72 | { |
d18ea0c5 | 73 | _IO_list_all = (struct _IO_FILE_plus *) _IO_list_all->file._chain; |
cedb4109 UD |
74 | ++_IO_list_all_stamp; |
75 | } | |
76 | else | |
d18ea0c5 | 77 | for (f = &_IO_list_all->file._chain; *f; f = &(*f)->_chain) |
cedb4109 | 78 | if (*f == (_IO_FILE *) fp) |
40a55d20 | 79 | { |
cedb4109 | 80 | *f = fp->file._chain; |
3afd9491 | 81 | ++_IO_list_all_stamp; |
40a55d20 UD |
82 | break; |
83 | } | |
beafb752 | 84 | fp->file._flags &= ~_IO_LINKED; |
51e176c2 | 85 | #ifdef _IO_MTSAFE_IO |
beafb752 UD |
86 | _IO_funlockfile ((_IO_FILE *) fp); |
87 | run_fp = NULL; | |
118bad87 | 88 | _IO_lock_unlock (list_all_lock); |
beafb752 | 89 | _IO_cleanup_region_end (0); |
51e176c2 | 90 | #endif |
96aa2d94 | 91 | } |
96aa2d94 | 92 | } |
d18ea0c5 | 93 | libc_hidden_def (_IO_un_link) |
96aa2d94 RM |
94 | |
95 | void | |
9d46370c | 96 | _IO_link_in (struct _IO_FILE_plus *fp) |
96aa2d94 | 97 | { |
beafb752 UD |
98 | if ((fp->file._flags & _IO_LINKED) == 0) |
99 | { | |
100 | fp->file._flags |= _IO_LINKED; | |
51e176c2 | 101 | #ifdef _IO_MTSAFE_IO |
beafb752 UD |
102 | _IO_cleanup_region_start_noarg (flush_cleanup); |
103 | _IO_lock_lock (list_all_lock); | |
104 | run_fp = (_IO_FILE *) fp; | |
105 | _IO_flockfile ((_IO_FILE *) fp); | |
51e176c2 | 106 | #endif |
d18ea0c5 AS |
107 | fp->file._chain = (_IO_FILE *) _IO_list_all; |
108 | _IO_list_all = fp; | |
beafb752 | 109 | ++_IO_list_all_stamp; |
51e176c2 | 110 | #ifdef _IO_MTSAFE_IO |
beafb752 UD |
111 | _IO_funlockfile ((_IO_FILE *) fp); |
112 | run_fp = NULL; | |
113 | _IO_lock_unlock (list_all_lock); | |
114 | _IO_cleanup_region_end (0); | |
51e176c2 | 115 | #endif |
beafb752 | 116 | } |
96aa2d94 | 117 | } |
d18ea0c5 | 118 | libc_hidden_def (_IO_link_in) |
96aa2d94 RM |
119 | |
120 | /* Return minimum _pos markers | |
121 | Assumes the current get area is the main get area. */ | |
79937577 | 122 | _IO_ssize_t _IO_least_marker (_IO_FILE *fp, char *end_p); |
96aa2d94 | 123 | |
d64b6ad0 | 124 | _IO_ssize_t |
9d46370c | 125 | _IO_least_marker (_IO_FILE *fp, char *end_p) |
96aa2d94 | 126 | { |
05f732b3 | 127 | _IO_ssize_t least_so_far = end_p - fp->_IO_read_base; |
40a55d20 | 128 | struct _IO_marker *mark; |
96aa2d94 RM |
129 | for (mark = fp->_markers; mark != NULL; mark = mark->_next) |
130 | if (mark->_pos < least_so_far) | |
131 | least_so_far = mark->_pos; | |
132 | return least_so_far; | |
133 | } | |
134 | ||
135 | /* Switch current get area from backup buffer to (start of) main get area. */ | |
136 | ||
137 | void | |
9d46370c | 138 | _IO_switch_to_main_get_area (_IO_FILE *fp) |
96aa2d94 RM |
139 | { |
140 | char *tmp; | |
141 | fp->_flags &= ~_IO_IN_BACKUP; | |
142 | /* Swap _IO_read_end and _IO_save_end. */ | |
40a55d20 UD |
143 | tmp = fp->_IO_read_end; |
144 | fp->_IO_read_end = fp->_IO_save_end; | |
145 | fp->_IO_save_end= tmp; | |
96aa2d94 | 146 | /* Swap _IO_read_base and _IO_save_base. */ |
40a55d20 UD |
147 | tmp = fp->_IO_read_base; |
148 | fp->_IO_read_base = fp->_IO_save_base; | |
149 | fp->_IO_save_base = tmp; | |
05f732b3 UD |
150 | /* Set _IO_read_ptr. */ |
151 | fp->_IO_read_ptr = fp->_IO_read_base; | |
96aa2d94 RM |
152 | } |
153 | ||
154 | /* Switch current get area from main get area to (end of) backup area. */ | |
155 | ||
156 | void | |
9d46370c | 157 | _IO_switch_to_backup_area (_IO_FILE *fp) |
96aa2d94 RM |
158 | { |
159 | char *tmp; | |
160 | fp->_flags |= _IO_IN_BACKUP; | |
161 | /* Swap _IO_read_end and _IO_save_end. */ | |
40a55d20 UD |
162 | tmp = fp->_IO_read_end; |
163 | fp->_IO_read_end = fp->_IO_save_end; | |
164 | fp->_IO_save_end = tmp; | |
05f732b3 | 165 | /* Swap _IO_read_base and _IO_save_base. */ |
40a55d20 UD |
166 | tmp = fp->_IO_read_base; |
167 | fp->_IO_read_base = fp->_IO_save_base; | |
168 | fp->_IO_save_base = tmp; | |
05f732b3 | 169 | /* Set _IO_read_ptr. */ |
96aa2d94 RM |
170 | fp->_IO_read_ptr = fp->_IO_read_end; |
171 | } | |
172 | ||
173 | int | |
9d46370c | 174 | _IO_switch_to_get_mode (_IO_FILE *fp) |
96aa2d94 RM |
175 | { |
176 | if (fp->_IO_write_ptr > fp->_IO_write_base) | |
177 | if (_IO_OVERFLOW (fp, EOF) == EOF) | |
178 | return EOF; | |
40a55d20 | 179 | if (_IO_in_backup (fp)) |
96aa2d94 RM |
180 | fp->_IO_read_base = fp->_IO_backup_base; |
181 | else | |
182 | { | |
183 | fp->_IO_read_base = fp->_IO_buf_base; | |
184 | if (fp->_IO_write_ptr > fp->_IO_read_end) | |
185 | fp->_IO_read_end = fp->_IO_write_ptr; | |
186 | } | |
187 | fp->_IO_read_ptr = fp->_IO_write_ptr; | |
188 | ||
189 | fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end = fp->_IO_read_ptr; | |
190 | ||
191 | fp->_flags &= ~_IO_CURRENTLY_PUTTING; | |
192 | return 0; | |
193 | } | |
d18ea0c5 | 194 | libc_hidden_def (_IO_switch_to_get_mode) |
96aa2d94 RM |
195 | |
196 | void | |
9d46370c | 197 | _IO_free_backup_area (_IO_FILE *fp) |
96aa2d94 RM |
198 | { |
199 | if (_IO_in_backup (fp)) | |
40a55d20 | 200 | _IO_switch_to_main_get_area (fp); /* Just in case. */ |
96aa2d94 RM |
201 | free (fp->_IO_save_base); |
202 | fp->_IO_save_base = NULL; | |
203 | fp->_IO_save_end = NULL; | |
204 | fp->_IO_backup_base = NULL; | |
205 | } | |
d18ea0c5 | 206 | libc_hidden_def (_IO_free_backup_area) |
96aa2d94 RM |
207 | |
208 | #if 0 | |
209 | int | |
9d46370c | 210 | _IO_switch_to_put_mode (_IO_FILE *fp) |
96aa2d94 RM |
211 | { |
212 | fp->_IO_write_base = fp->_IO_read_ptr; | |
213 | fp->_IO_write_ptr = fp->_IO_read_ptr; | |
214 | /* Following is wrong if line- or un-buffered? */ | |
40a55d20 UD |
215 | fp->_IO_write_end = (fp->_flags & _IO_IN_BACKUP |
216 | ? fp->_IO_read_end : fp->_IO_buf_end); | |
96aa2d94 RM |
217 | |
218 | fp->_IO_read_ptr = fp->_IO_read_end; | |
219 | fp->_IO_read_base = fp->_IO_read_end; | |
220 | ||
221 | fp->_flags |= _IO_CURRENTLY_PUTTING; | |
222 | return 0; | |
223 | } | |
224 | #endif | |
225 | ||
226 | int | |
9d46370c | 227 | __overflow (_IO_FILE *f, int ch) |
96aa2d94 | 228 | { |
6f98fd7e UD |
229 | /* This is a single-byte stream. */ |
230 | if (f->_mode == 0) | |
231 | _IO_fwide (f, -1); | |
96aa2d94 RM |
232 | return _IO_OVERFLOW (f, ch); |
233 | } | |
37ba7d66 | 234 | libc_hidden_def (__overflow) |
96aa2d94 | 235 | |
79937577 | 236 | static int save_for_backup (_IO_FILE *fp, char *end_p) |
dfd2257a UD |
237 | #ifdef _LIBC |
238 | internal_function | |
239 | #endif | |
240 | ; | |
40a55d20 | 241 | |
05f732b3 | 242 | static int |
dfd2257a | 243 | #ifdef _LIBC |
05f732b3 | 244 | internal_function |
dfd2257a | 245 | #endif |
05f732b3 | 246 | save_for_backup (fp, end_p) |
40a55d20 | 247 | _IO_FILE *fp; |
05f732b3 | 248 | char *end_p; |
96aa2d94 | 249 | { |
05f732b3 UD |
250 | /* Append [_IO_read_base..end_p] to backup area. */ |
251 | _IO_ssize_t least_mark = _IO_least_marker (fp, end_p); | |
96aa2d94 | 252 | /* needed_size is how much space we need in the backup area. */ |
05f732b3 UD |
253 | _IO_size_t needed_size = (end_p - fp->_IO_read_base) - least_mark; |
254 | /* FIXME: Dubious arithmetic if pointers are NULL */ | |
255 | _IO_size_t current_Bsize = fp->_IO_save_end - fp->_IO_save_base; | |
256 | _IO_size_t avail; /* Extra space available for future expansion. */ | |
257 | _IO_ssize_t delta; | |
96aa2d94 RM |
258 | struct _IO_marker *mark; |
259 | if (needed_size > current_Bsize) | |
260 | { | |
261 | char *new_buffer; | |
262 | avail = 100; | |
40a55d20 | 263 | new_buffer = (char *) malloc (avail + needed_size); |
96aa2d94 RM |
264 | if (new_buffer == NULL) |
265 | return EOF; /* FIXME */ | |
266 | if (least_mark < 0) | |
267 | { | |
86187531 UD |
268 | #ifdef _LIBC |
269 | __mempcpy (__mempcpy (new_buffer + avail, | |
270 | fp->_IO_save_end + least_mark, | |
271 | -least_mark), | |
272 | fp->_IO_read_base, | |
05f732b3 | 273 | end_p - fp->_IO_read_base); |
86187531 | 274 | #else |
40a55d20 UD |
275 | memcpy (new_buffer + avail, |
276 | fp->_IO_save_end + least_mark, | |
277 | -least_mark); | |
278 | memcpy (new_buffer + avail - least_mark, | |
279 | fp->_IO_read_base, | |
05f732b3 | 280 | end_p - fp->_IO_read_base); |
86187531 | 281 | #endif |
96aa2d94 RM |
282 | } |
283 | else | |
40a55d20 UD |
284 | memcpy (new_buffer + avail, |
285 | fp->_IO_read_base + least_mark, | |
286 | needed_size); | |
72e6cdfa | 287 | free (fp->_IO_save_base); |
96aa2d94 RM |
288 | fp->_IO_save_base = new_buffer; |
289 | fp->_IO_save_end = new_buffer + avail + needed_size; | |
290 | } | |
291 | else | |
292 | { | |
293 | avail = current_Bsize - needed_size; | |
294 | if (least_mark < 0) | |
295 | { | |
40a55d20 UD |
296 | memmove (fp->_IO_save_base + avail, |
297 | fp->_IO_save_end + least_mark, | |
298 | -least_mark); | |
299 | memcpy (fp->_IO_save_base + avail - least_mark, | |
300 | fp->_IO_read_base, | |
05f732b3 | 301 | end_p - fp->_IO_read_base); |
96aa2d94 RM |
302 | } |
303 | else if (needed_size > 0) | |
40a55d20 UD |
304 | memcpy (fp->_IO_save_base + avail, |
305 | fp->_IO_read_base + least_mark, | |
306 | needed_size); | |
96aa2d94 | 307 | } |
96aa2d94 RM |
308 | fp->_IO_backup_base = fp->_IO_save_base + avail; |
309 | /* Adjust all the streammarkers. */ | |
05f732b3 | 310 | delta = end_p - fp->_IO_read_base; |
96aa2d94 RM |
311 | for (mark = fp->_markers; mark != NULL; mark = mark->_next) |
312 | mark->_pos -= delta; | |
313 | return 0; | |
314 | } | |
315 | ||
316 | int | |
9d46370c | 317 | __underflow (_IO_FILE *fp) |
96aa2d94 | 318 | { |
319d719d | 319 | #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T |
bbdef797 | 320 | if (_IO_vtable_offset (fp) == 0 && _IO_fwide (fp, -1) != -1) |
d64b6ad0 | 321 | return EOF; |
319d719d | 322 | #endif |
d64b6ad0 | 323 | |
6f98fd7e UD |
324 | if (fp->_mode == 0) |
325 | _IO_fwide (fp, -1); | |
40a55d20 | 326 | if (_IO_in_put_mode (fp)) |
d18ea0c5 | 327 | if (_IO_switch_to_get_mode (fp) == EOF) |
40a55d20 | 328 | return EOF; |
96aa2d94 | 329 | if (fp->_IO_read_ptr < fp->_IO_read_end) |
40a55d20 UD |
330 | return *(unsigned char *) fp->_IO_read_ptr; |
331 | if (_IO_in_backup (fp)) | |
96aa2d94 | 332 | { |
40a55d20 | 333 | _IO_switch_to_main_get_area (fp); |
96aa2d94 | 334 | if (fp->_IO_read_ptr < fp->_IO_read_end) |
31f7410f | 335 | return *(unsigned char *) fp->_IO_read_ptr; |
96aa2d94 | 336 | } |
40a55d20 | 337 | if (_IO_have_markers (fp)) |
96aa2d94 | 338 | { |
05f732b3 | 339 | if (save_for_backup (fp, fp->_IO_read_end)) |
96aa2d94 RM |
340 | return EOF; |
341 | } | |
40a55d20 | 342 | else if (_IO_have_backup (fp)) |
d18ea0c5 | 343 | _IO_free_backup_area (fp); |
96aa2d94 RM |
344 | return _IO_UNDERFLOW (fp); |
345 | } | |
a20d8dbe | 346 | libc_hidden_def (__underflow) |
96aa2d94 RM |
347 | |
348 | int | |
9d46370c | 349 | __uflow (_IO_FILE *fp) |
96aa2d94 | 350 | { |
319d719d | 351 | #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T |
bbdef797 | 352 | if (_IO_vtable_offset (fp) == 0 && _IO_fwide (fp, -1) != -1) |
d64b6ad0 | 353 | return EOF; |
319d719d | 354 | #endif |
d64b6ad0 | 355 | |
6f98fd7e | 356 | if (fp->_mode == 0) |
1e88bd0f | 357 | _IO_fwide (fp, -1); |
40a55d20 | 358 | if (_IO_in_put_mode (fp)) |
d18ea0c5 | 359 | if (_IO_switch_to_get_mode (fp) == EOF) |
40a55d20 | 360 | return EOF; |
96aa2d94 | 361 | if (fp->_IO_read_ptr < fp->_IO_read_end) |
40a55d20 UD |
362 | return *(unsigned char *) fp->_IO_read_ptr++; |
363 | if (_IO_in_backup (fp)) | |
96aa2d94 | 364 | { |
40a55d20 | 365 | _IO_switch_to_main_get_area (fp); |
96aa2d94 | 366 | if (fp->_IO_read_ptr < fp->_IO_read_end) |
31f7410f | 367 | return *(unsigned char *) fp->_IO_read_ptr++; |
96aa2d94 | 368 | } |
40a55d20 | 369 | if (_IO_have_markers (fp)) |
96aa2d94 | 370 | { |
05f732b3 | 371 | if (save_for_backup (fp, fp->_IO_read_end)) |
96aa2d94 RM |
372 | return EOF; |
373 | } | |
40a55d20 | 374 | else if (_IO_have_backup (fp)) |
d18ea0c5 | 375 | _IO_free_backup_area (fp); |
96aa2d94 RM |
376 | return _IO_UFLOW (fp); |
377 | } | |
3ba06713 | 378 | libc_hidden_def (__uflow) |
96aa2d94 RM |
379 | |
380 | void | |
9d46370c | 381 | _IO_setb (_IO_FILE *f, char *b, char *eb, int a) |
96aa2d94 RM |
382 | { |
383 | if (f->_IO_buf_base && !(f->_flags & _IO_USER_BUF)) | |
8a29509d | 384 | free (f->_IO_buf_base); |
96aa2d94 RM |
385 | f->_IO_buf_base = b; |
386 | f->_IO_buf_end = eb; | |
387 | if (a) | |
388 | f->_flags &= ~_IO_USER_BUF; | |
389 | else | |
390 | f->_flags |= _IO_USER_BUF; | |
391 | } | |
d18ea0c5 | 392 | libc_hidden_def (_IO_setb) |
96aa2d94 RM |
393 | |
394 | void | |
9d46370c | 395 | _IO_doallocbuf (_IO_FILE *fp) |
96aa2d94 RM |
396 | { |
397 | if (fp->_IO_buf_base) | |
398 | return; | |
655de5fd | 399 | if (!(fp->_flags & _IO_UNBUFFERED) || fp->_mode > 0) |
96aa2d94 RM |
400 | if (_IO_DOALLOCATE (fp) != EOF) |
401 | return; | |
d18ea0c5 | 402 | _IO_setb (fp, fp->_shortbuf, fp->_shortbuf+1, 0); |
96aa2d94 | 403 | } |
d18ea0c5 | 404 | libc_hidden_def (_IO_doallocbuf) |
96aa2d94 RM |
405 | |
406 | int | |
9d46370c | 407 | _IO_default_underflow (_IO_FILE *fp) |
96aa2d94 RM |
408 | { |
409 | return EOF; | |
410 | } | |
411 | ||
412 | int | |
9d46370c | 413 | _IO_default_uflow (_IO_FILE *fp) |
96aa2d94 RM |
414 | { |
415 | int ch = _IO_UNDERFLOW (fp); | |
416 | if (ch == EOF) | |
417 | return EOF; | |
40a55d20 | 418 | return *(unsigned char *) fp->_IO_read_ptr++; |
96aa2d94 | 419 | } |
d18ea0c5 | 420 | libc_hidden_def (_IO_default_uflow) |
96aa2d94 RM |
421 | |
422 | _IO_size_t | |
9d46370c | 423 | _IO_default_xsputn (_IO_FILE *f, const void *data, _IO_size_t n) |
96aa2d94 | 424 | { |
40a55d20 UD |
425 | const char *s = (char *) data; |
426 | _IO_size_t more = n; | |
96aa2d94 RM |
427 | if (more <= 0) |
428 | return 0; | |
429 | for (;;) | |
430 | { | |
40a55d20 | 431 | /* Space available. */ |
f7803f51 | 432 | if (f->_IO_write_ptr < f->_IO_write_end) |
96aa2d94 | 433 | { |
f7803f51 UD |
434 | _IO_size_t count = f->_IO_write_end - f->_IO_write_ptr; |
435 | if (count > more) | |
96aa2d94 RM |
436 | count = more; |
437 | if (count > 20) | |
438 | { | |
86187531 UD |
439 | #ifdef _LIBC |
440 | f->_IO_write_ptr = __mempcpy (f->_IO_write_ptr, s, count); | |
441 | #else | |
40a55d20 | 442 | memcpy (f->_IO_write_ptr, s, count); |
96aa2d94 | 443 | f->_IO_write_ptr += count; |
86187531 UD |
444 | #endif |
445 | s += count; | |
f22e1074 | 446 | } |
f7803f51 | 447 | else if (count) |
96aa2d94 | 448 | { |
40a55d20 UD |
449 | char *p = f->_IO_write_ptr; |
450 | _IO_ssize_t i; | |
451 | for (i = count; --i >= 0; ) | |
452 | *p++ = *s++; | |
96aa2d94 | 453 | f->_IO_write_ptr = p; |
f22e1074 | 454 | } |
96aa2d94 | 455 | more -= count; |
f22e1074 | 456 | } |
9202ffe3 | 457 | if (more == 0 || _IO_OVERFLOW (f, (unsigned char) *s++) == EOF) |
96aa2d94 RM |
458 | break; |
459 | more--; | |
460 | } | |
461 | return n - more; | |
462 | } | |
d18ea0c5 | 463 | libc_hidden_def (_IO_default_xsputn) |
96aa2d94 RM |
464 | |
465 | _IO_size_t | |
9d46370c | 466 | _IO_sgetn (_IO_FILE *fp, void *data, _IO_size_t n) |
96aa2d94 RM |
467 | { |
468 | /* FIXME handle putback buffer here! */ | |
469 | return _IO_XSGETN (fp, data, n); | |
470 | } | |
d18ea0c5 | 471 | libc_hidden_def (_IO_sgetn) |
96aa2d94 RM |
472 | |
473 | _IO_size_t | |
9d46370c | 474 | _IO_default_xsgetn (_IO_FILE *fp, void *data, _IO_size_t n) |
96aa2d94 | 475 | { |
40a55d20 UD |
476 | _IO_size_t more = n; |
477 | char *s = (char*) data; | |
96aa2d94 RM |
478 | for (;;) |
479 | { | |
40a55d20 | 480 | /* Data available. */ |
f7803f51 | 481 | if (fp->_IO_read_ptr < fp->_IO_read_end) |
96aa2d94 | 482 | { |
f7803f51 UD |
483 | _IO_size_t count = fp->_IO_read_end - fp->_IO_read_ptr; |
484 | if (count > more) | |
96aa2d94 RM |
485 | count = more; |
486 | if (count > 20) | |
487 | { | |
86187531 UD |
488 | #ifdef _LIBC |
489 | s = __mempcpy (s, fp->_IO_read_ptr, count); | |
490 | #else | |
40a55d20 | 491 | memcpy (s, fp->_IO_read_ptr, count); |
96aa2d94 | 492 | s += count; |
86187531 | 493 | #endif |
96aa2d94 RM |
494 | fp->_IO_read_ptr += count; |
495 | } | |
f7803f51 | 496 | else if (count) |
96aa2d94 | 497 | { |
40a55d20 UD |
498 | char *p = fp->_IO_read_ptr; |
499 | int i = (int) count; | |
500 | while (--i >= 0) | |
501 | *s++ = *p++; | |
96aa2d94 | 502 | fp->_IO_read_ptr = p; |
f22e1074 UD |
503 | } |
504 | more -= count; | |
505 | } | |
40a55d20 | 506 | if (more == 0 || __underflow (fp) == EOF) |
96aa2d94 RM |
507 | break; |
508 | } | |
509 | return n - more; | |
510 | } | |
d18ea0c5 | 511 | libc_hidden_def (_IO_default_xsgetn) |
96aa2d94 | 512 | |
40a55d20 UD |
513 | #if 0 |
514 | /* Seems not to be needed. --drepper */ | |
96aa2d94 | 515 | int |
9d46370c | 516 | _IO_sync (_IO_FILE *fp) |
96aa2d94 RM |
517 | { |
518 | return 0; | |
519 | } | |
40a55d20 | 520 | #endif |
96aa2d94 | 521 | |
40a55d20 | 522 | _IO_FILE * |
9d46370c | 523 | _IO_default_setbuf (_IO_FILE *fp, char *p, _IO_ssize_t len) |
96aa2d94 RM |
524 | { |
525 | if (_IO_SYNC (fp) == EOF) | |
526 | return NULL; | |
527 | if (p == NULL || len == 0) | |
528 | { | |
529 | fp->_flags |= _IO_UNBUFFERED; | |
d18ea0c5 | 530 | _IO_setb (fp, fp->_shortbuf, fp->_shortbuf+1, 0); |
96aa2d94 RM |
531 | } |
532 | else | |
533 | { | |
534 | fp->_flags &= ~_IO_UNBUFFERED; | |
d18ea0c5 | 535 | _IO_setb (fp, p, p+len, 0); |
96aa2d94 RM |
536 | } |
537 | fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end = 0; | |
538 | fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_read_end = 0; | |
539 | return fp; | |
540 | } | |
541 | ||
d64b6ad0 | 542 | _IO_off64_t |
9d46370c | 543 | _IO_default_seekpos (_IO_FILE *fp, _IO_off64_t pos, int mode) |
96aa2d94 | 544 | { |
d64b6ad0 | 545 | return _IO_SEEKOFF (fp, pos, 0, mode); |
96aa2d94 RM |
546 | } |
547 | ||
548 | int | |
9d46370c | 549 | _IO_default_doallocate (_IO_FILE *fp) |
96aa2d94 | 550 | { |
f8b87ef0 UD |
551 | char *buf; |
552 | ||
8a29509d PP |
553 | buf = malloc(_IO_BUFSIZ); |
554 | if (__glibc_unlikely (buf == NULL)) | |
555 | return EOF; | |
556 | ||
d18ea0c5 | 557 | _IO_setb (fp, buf, buf+_IO_BUFSIZ, 1); |
96aa2d94 RM |
558 | return 1; |
559 | } | |
d18ea0c5 | 560 | libc_hidden_def (_IO_default_doallocate) |
96aa2d94 RM |
561 | |
562 | void | |
9d46370c | 563 | _IO_init (_IO_FILE *fp, int flags) |
d64b6ad0 UD |
564 | { |
565 | _IO_no_init (fp, flags, -1, NULL, NULL); | |
566 | } | |
d18ea0c5 | 567 | libc_hidden_def (_IO_init) |
d64b6ad0 UD |
568 | |
569 | void | |
9d46370c | 570 | _IO_old_init (_IO_FILE *fp, int flags) |
96aa2d94 RM |
571 | { |
572 | fp->_flags = _IO_MAGIC|flags; | |
dd0ee2e1 | 573 | fp->_flags2 = 0; |
96aa2d94 RM |
574 | fp->_IO_buf_base = NULL; |
575 | fp->_IO_buf_end = NULL; | |
576 | fp->_IO_read_base = NULL; | |
577 | fp->_IO_read_ptr = NULL; | |
578 | fp->_IO_read_end = NULL; | |
579 | fp->_IO_write_base = NULL; | |
580 | fp->_IO_write_ptr = NULL; | |
581 | fp->_IO_write_end = NULL; | |
582 | fp->_chain = NULL; /* Not necessary. */ | |
583 | ||
584 | fp->_IO_save_base = NULL; | |
585 | fp->_IO_backup_base = NULL; | |
586 | fp->_IO_save_end = NULL; | |
587 | fp->_markers = NULL; | |
588 | fp->_cur_column = 0; | |
c15cf13a | 589 | #if _IO_JUMPS_OFFSET |
bd355af0 UD |
590 | fp->_vtable_offset = 0; |
591 | #endif | |
7c713e28 | 592 | #ifdef _IO_MTSAFE_IO |
c020d48c UD |
593 | if (fp->_lock != NULL) |
594 | _IO_lock_init (*fp->_lock); | |
7c713e28 | 595 | #endif |
14a2bd4b UD |
596 | } |
597 | ||
598 | void | |
599 | _IO_no_init (fp, flags, orientation, wd, jmp) | |
600 | _IO_FILE *fp; | |
601 | int flags; | |
602 | int orientation; | |
603 | struct _IO_wide_data *wd; | |
b2637a22 | 604 | const struct _IO_jump_t *jmp; |
14a2bd4b UD |
605 | { |
606 | _IO_old_init (fp, flags); | |
d64b6ad0 | 607 | fp->_mode = orientation; |
319d719d | 608 | #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T |
d64b6ad0 UD |
609 | if (orientation >= 0) |
610 | { | |
611 | fp->_wide_data = wd; | |
612 | fp->_wide_data->_IO_buf_base = NULL; | |
613 | fp->_wide_data->_IO_buf_end = NULL; | |
614 | fp->_wide_data->_IO_read_base = NULL; | |
615 | fp->_wide_data->_IO_read_ptr = NULL; | |
616 | fp->_wide_data->_IO_read_end = NULL; | |
617 | fp->_wide_data->_IO_write_base = NULL; | |
618 | fp->_wide_data->_IO_write_ptr = NULL; | |
619 | fp->_wide_data->_IO_write_end = NULL; | |
620 | fp->_wide_data->_IO_save_base = NULL; | |
621 | fp->_wide_data->_IO_backup_base = NULL; | |
622 | fp->_wide_data->_IO_save_end = NULL; | |
623 | ||
624 | fp->_wide_data->_wide_vtable = jmp; | |
625 | } | |
bae143d2 OB |
626 | else |
627 | /* Cause predictable crash when a wide function is called on a byte | |
628 | stream. */ | |
629 | fp->_wide_data = (struct _IO_wide_data *) -1L; | |
319d719d | 630 | #endif |
78762723 | 631 | fp->_freeres_list = NULL; |
96aa2d94 RM |
632 | } |
633 | ||
634 | int | |
9d46370c | 635 | _IO_default_sync (_IO_FILE *fp) |
96aa2d94 RM |
636 | { |
637 | return 0; | |
638 | } | |
639 | ||
640 | /* The way the C++ classes are mapped into the C functions in the | |
641 | current implementation, this function can get called twice! */ | |
642 | ||
643 | void | |
9d46370c | 644 | _IO_default_finish (_IO_FILE *fp, int dummy) |
96aa2d94 RM |
645 | { |
646 | struct _IO_marker *mark; | |
647 | if (fp->_IO_buf_base && !(fp->_flags & _IO_USER_BUF)) | |
648 | { | |
8a29509d | 649 | free (fp->_IO_buf_base); |
96aa2d94 RM |
650 | fp->_IO_buf_base = fp->_IO_buf_end = NULL; |
651 | } | |
652 | ||
653 | for (mark = fp->_markers; mark != NULL; mark = mark->_next) | |
654 | mark->_sbuf = NULL; | |
655 | ||
656 | if (fp->_IO_save_base) | |
657 | { | |
658 | free (fp->_IO_save_base); | |
659 | fp->_IO_save_base = NULL; | |
660 | } | |
661 | ||
d18ea0c5 | 662 | _IO_un_link ((struct _IO_FILE_plus *) fp); |
993a5d66 | 663 | |
7c713e28 | 664 | #ifdef _IO_MTSAFE_IO |
c020d48c UD |
665 | if (fp->_lock != NULL) |
666 | _IO_lock_fini (*fp->_lock); | |
7c713e28 | 667 | #endif |
96aa2d94 | 668 | } |
d18ea0c5 | 669 | libc_hidden_def (_IO_default_finish) |
96aa2d94 | 670 | |
d64b6ad0 | 671 | _IO_off64_t |
9d46370c | 672 | _IO_default_seekoff (_IO_FILE *fp, _IO_off64_t offset, int dir, int mode) |
96aa2d94 | 673 | { |
c020d48c | 674 | return _IO_pos_BAD; |
96aa2d94 RM |
675 | } |
676 | ||
677 | int | |
9d46370c | 678 | _IO_sputbackc (_IO_FILE *fp, int c) |
96aa2d94 | 679 | { |
19bc17a9 | 680 | int result; |
adfa2078 | 681 | |
96aa2d94 RM |
682 | if (fp->_IO_read_ptr > fp->_IO_read_base |
683 | && (unsigned char)fp->_IO_read_ptr[-1] == (unsigned char)c) | |
684 | { | |
685 | fp->_IO_read_ptr--; | |
40a55d20 | 686 | result = (unsigned char) c; |
96aa2d94 | 687 | } |
19bc17a9 RM |
688 | else |
689 | result = _IO_PBACKFAIL (fp, c); | |
690 | ||
691 | if (result != EOF) | |
692 | fp->_flags &= ~_IO_EOF_SEEN; | |
693 | ||
694 | return result; | |
96aa2d94 | 695 | } |
d18ea0c5 | 696 | libc_hidden_def (_IO_sputbackc) |
96aa2d94 RM |
697 | |
698 | int | |
9d46370c | 699 | _IO_sungetc (_IO_FILE *fp) |
96aa2d94 | 700 | { |
19bc17a9 | 701 | int result; |
adfa2078 | 702 | |
96aa2d94 RM |
703 | if (fp->_IO_read_ptr > fp->_IO_read_base) |
704 | { | |
705 | fp->_IO_read_ptr--; | |
40a55d20 | 706 | result = (unsigned char) *fp->_IO_read_ptr; |
96aa2d94 RM |
707 | } |
708 | else | |
19bc17a9 RM |
709 | result = _IO_PBACKFAIL (fp, EOF); |
710 | ||
711 | if (result != EOF) | |
712 | fp->_flags &= ~_IO_EOF_SEEN; | |
713 | ||
714 | return result; | |
96aa2d94 RM |
715 | } |
716 | ||
717 | #if 0 /* Work in progress */ | |
40a55d20 UD |
718 | /* Seems not to be needed. */ |
719 | #if 0 | |
96aa2d94 | 720 | void |
9d46370c | 721 | _IO_set_column (_IO_FILE *fp, int c) |
96aa2d94 RM |
722 | { |
723 | if (c == -1) | |
724 | fp->_column = -1; | |
725 | else | |
726 | fp->_column = c - (fp->_IO_write_ptr - fp->_IO_write_base); | |
727 | } | |
728 | #else | |
729 | int | |
9d46370c | 730 | _IO_set_column (_IO_FILE *fp, int i) |
96aa2d94 | 731 | { |
40a55d20 | 732 | fp->_cur_column = i + 1; |
96aa2d94 RM |
733 | return 0; |
734 | } | |
735 | #endif | |
40a55d20 | 736 | #endif |
96aa2d94 RM |
737 | |
738 | ||
739 | unsigned | |
9d46370c | 740 | _IO_adjust_column (unsigned start, const char *line, int count) |
96aa2d94 | 741 | { |
40a55d20 | 742 | const char *ptr = line + count; |
96aa2d94 RM |
743 | while (ptr > line) |
744 | if (*--ptr == '\n') | |
745 | return line + count - ptr - 1; | |
746 | return start + count; | |
747 | } | |
d18ea0c5 | 748 | libc_hidden_def (_IO_adjust_column) |
96aa2d94 | 749 | |
40a55d20 UD |
750 | #if 0 |
751 | /* Seems not to be needed. --drepper */ | |
96aa2d94 | 752 | int |
9d46370c | 753 | _IO_get_column (_IO_FILE *fp) |
96aa2d94 | 754 | { |
adfa2078 | 755 | if (fp->_cur_column) |
40a55d20 | 756 | return _IO_adjust_column (fp->_cur_column - 1, |
96aa2d94 RM |
757 | fp->_IO_write_base, |
758 | fp->_IO_write_ptr - fp->_IO_write_base); | |
759 | return -1; | |
760 | } | |
40a55d20 | 761 | #endif |
96aa2d94 | 762 | |
3afd9491 | 763 | |
96aa2d94 | 764 | int |
c6baa867 | 765 | _IO_flush_all_lockp (int do_lock) |
96aa2d94 RM |
766 | { |
767 | int result = 0; | |
73c115ed | 768 | struct _IO_FILE *fp; |
3afd9491 UD |
769 | int last_stamp; |
770 | ||
771 | #ifdef _IO_MTSAFE_IO | |
341dd673 | 772 | __libc_cleanup_region_start (do_lock, flush_cleanup, NULL); |
c6baa867 UD |
773 | if (do_lock) |
774 | _IO_lock_lock (list_all_lock); | |
3afd9491 UD |
775 | #endif |
776 | ||
777 | last_stamp = _IO_list_all_stamp; | |
d18ea0c5 | 778 | fp = (_IO_FILE *) _IO_list_all; |
3afd9491 UD |
779 | while (fp != NULL) |
780 | { | |
781 | run_fp = fp; | |
c6baa867 UD |
782 | if (do_lock) |
783 | _IO_flockfile (fp); | |
3afd9491 UD |
784 | |
785 | if (((fp->_mode <= 0 && fp->_IO_write_ptr > fp->_IO_write_base) | |
319d719d | 786 | #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T |
bbdef797 | 787 | || (_IO_vtable_offset (fp) == 0 |
3afd9491 UD |
788 | && fp->_mode > 0 && (fp->_wide_data->_IO_write_ptr |
789 | > fp->_wide_data->_IO_write_base)) | |
319d719d | 790 | #endif |
3afd9491 UD |
791 | ) |
792 | && _IO_OVERFLOW (fp, EOF) == EOF) | |
793 | result = EOF; | |
794 | ||
c6baa867 UD |
795 | if (do_lock) |
796 | _IO_funlockfile (fp); | |
3afd9491 UD |
797 | run_fp = NULL; |
798 | ||
799 | if (last_stamp != _IO_list_all_stamp) | |
800 | { | |
801 | /* Something was added to the list. Start all over again. */ | |
d18ea0c5 | 802 | fp = (_IO_FILE *) _IO_list_all; |
3afd9491 UD |
803 | last_stamp = _IO_list_all_stamp; |
804 | } | |
805 | else | |
806 | fp = fp->_chain; | |
807 | } | |
808 | ||
809 | #ifdef _IO_MTSAFE_IO | |
c6baa867 UD |
810 | if (do_lock) |
811 | _IO_lock_unlock (list_all_lock); | |
7583a88d | 812 | __libc_cleanup_region_end (0); |
3afd9491 UD |
813 | #endif |
814 | ||
96aa2d94 RM |
815 | return result; |
816 | } | |
817 | ||
c6baa867 UD |
818 | |
819 | int | |
60d2f8f3 | 820 | _IO_flush_all (void) |
c6baa867 UD |
821 | { |
822 | /* We want locking. */ | |
823 | return _IO_flush_all_lockp (1); | |
824 | } | |
d18ea0c5 | 825 | libc_hidden_def (_IO_flush_all) |
c6baa867 | 826 | |
96aa2d94 | 827 | void |
60d2f8f3 | 828 | _IO_flush_all_linebuffered (void) |
96aa2d94 | 829 | { |
73c115ed | 830 | struct _IO_FILE *fp; |
3afd9491 UD |
831 | int last_stamp; |
832 | ||
833 | #ifdef _IO_MTSAFE_IO | |
834 | _IO_cleanup_region_start_noarg (flush_cleanup); | |
835 | _IO_lock_lock (list_all_lock); | |
836 | #endif | |
837 | ||
838 | last_stamp = _IO_list_all_stamp; | |
d18ea0c5 | 839 | fp = (_IO_FILE *) _IO_list_all; |
3afd9491 UD |
840 | while (fp != NULL) |
841 | { | |
842 | run_fp = fp; | |
843 | _IO_flockfile (fp); | |
844 | ||
845 | if ((fp->_flags & _IO_NO_WRITES) == 0 && fp->_flags & _IO_LINE_BUF) | |
846 | _IO_OVERFLOW (fp, EOF); | |
847 | ||
848 | _IO_funlockfile (fp); | |
849 | run_fp = NULL; | |
850 | ||
851 | if (last_stamp != _IO_list_all_stamp) | |
852 | { | |
853 | /* Something was added to the list. Start all over again. */ | |
d18ea0c5 | 854 | fp = (_IO_FILE *) _IO_list_all; |
3afd9491 UD |
855 | last_stamp = _IO_list_all_stamp; |
856 | } | |
857 | else | |
858 | fp = fp->_chain; | |
859 | } | |
860 | ||
861 | #ifdef _IO_MTSAFE_IO | |
862 | _IO_lock_unlock (list_all_lock); | |
863 | _IO_cleanup_region_end (0); | |
864 | #endif | |
96aa2d94 | 865 | } |
d18ea0c5 | 866 | libc_hidden_def (_IO_flush_all_linebuffered) |
a91d3cd3 UD |
867 | #ifdef _LIBC |
868 | weak_alias (_IO_flush_all_linebuffered, _flushlbf) | |
869 | #endif | |
96aa2d94 | 870 | |
78762723 UD |
871 | |
872 | /* The following is a bit tricky. In general, we want to unbuffer the | |
873 | streams so that all output which follows is seen. If we are not | |
874 | looking for memory leaks it does not make much sense to free the | |
875 | actual buffer because this will happen anyway once the program | |
876 | terminated. If we do want to look for memory leaks we have to free | |
877 | the buffers. Whether something is freed is determined by the | |
878 | function sin the libc_freeres section. Those are called as part of | |
879 | the atexit routine, just like _IO_cleanup. The problem is we do | |
880 | not know whether the freeres code is called first or _IO_cleanup. | |
881 | if the former is the case, we set the DEALLOC_BUFFER variable to | |
18d26750 PP |
882 | true and _IO_unbuffer_all will take care of the rest. If |
883 | _IO_unbuffer_all is called first we add the streams to a list | |
78762723 | 884 | which the freeres function later can walk through. */ |
18d26750 | 885 | static void _IO_unbuffer_all (void); |
40a55d20 | 886 | |
78762723 UD |
887 | static bool dealloc_buffers; |
888 | static _IO_FILE *freeres_list; | |
889 | ||
40a55d20 | 890 | static void |
18d26750 | 891 | _IO_unbuffer_all (void) |
96aa2d94 | 892 | { |
73c115ed | 893 | struct _IO_FILE *fp; |
d18ea0c5 | 894 | for (fp = (_IO_FILE *) _IO_list_all; fp; fp = fp->_chain) |
6906cea4 UD |
895 | { |
896 | if (! (fp->_flags & _IO_UNBUFFERED) | |
6906cea4 UD |
897 | /* Iff stream is un-orientated, it wasn't used. */ |
898 | && fp->_mode != 0) | |
78762723 | 899 | { |
b2e1c562 | 900 | #ifdef _IO_MTSAFE_IO |
af047cff UD |
901 | int cnt; |
902 | #define MAXTRIES 2 | |
903 | for (cnt = 0; cnt < MAXTRIES; ++cnt) | |
f22e1074 | 904 | if (fp->_lock == NULL || _IO_lock_trylock (*fp->_lock) == 0) |
af047cff UD |
905 | break; |
906 | else | |
907 | /* Give the other thread time to finish up its use of the | |
908 | stream. */ | |
909 | __sched_yield (); | |
b2e1c562 | 910 | #endif |
af047cff | 911 | |
78762723 UD |
912 | if (! dealloc_buffers && !(fp->_flags & _IO_USER_BUF)) |
913 | { | |
914 | fp->_flags |= _IO_USER_BUF; | |
915 | ||
916 | fp->_freeres_list = freeres_list; | |
917 | freeres_list = fp; | |
918 | fp->_freeres_buf = fp->_IO_buf_base; | |
78762723 UD |
919 | } |
920 | ||
921 | _IO_SETBUF (fp, NULL, 0); | |
af047cff | 922 | |
a601b74d PP |
923 | if (fp->_mode > 0) |
924 | _IO_wsetb (fp, NULL, NULL, 0); | |
925 | ||
b2e1c562 | 926 | #ifdef _IO_MTSAFE_IO |
f22e1074 | 927 | if (cnt < MAXTRIES && fp->_lock != NULL) |
af047cff | 928 | _IO_lock_unlock (*fp->_lock); |
b2e1c562 | 929 | #endif |
78762723 | 930 | } |
6906cea4 UD |
931 | |
932 | /* Make sure that never again the wide char functions can be | |
933 | used. */ | |
934 | fp->_mode = -1; | |
935 | } | |
96aa2d94 RM |
936 | } |
937 | ||
78762723 UD |
938 | |
939 | libc_freeres_fn (buffer_free) | |
940 | { | |
941 | dealloc_buffers = true; | |
942 | ||
943 | while (freeres_list != NULL) | |
944 | { | |
8a29509d | 945 | free (freeres_list->_freeres_buf); |
78762723 UD |
946 | |
947 | freeres_list = freeres_list->_freeres_list; | |
948 | } | |
949 | } | |
950 | ||
951 | ||
310b3460 | 952 | int |
60d2f8f3 | 953 | _IO_cleanup (void) |
96aa2d94 | 954 | { |
64f01020 | 955 | /* We do *not* want locking. Some threads might use streams but |
78762723 | 956 | that is their problem, we flush them underneath them. */ |
64f01020 | 957 | int result = _IO_flush_all_lockp (0); |
96aa2d94 RM |
958 | |
959 | /* We currently don't have a reliable mechanism for making sure that | |
960 | C++ static destructors are executed in the correct order. | |
6d52618b | 961 | So it is possible that other static destructors might want to |
96aa2d94 RM |
962 | write to cout - and they're supposed to be able to do so. |
963 | ||
adfa2078 | 964 | The following will make the standard streambufs be unbuffered, |
96aa2d94 | 965 | which forces any output from late destructors to be written out. */ |
18d26750 | 966 | _IO_unbuffer_all (); |
310b3460 UD |
967 | |
968 | return result; | |
96aa2d94 RM |
969 | } |
970 | ||
f2ea0f5b | 971 | |
96aa2d94 | 972 | void |
9d46370c | 973 | _IO_init_marker (struct _IO_marker *marker, _IO_FILE *fp) |
96aa2d94 RM |
974 | { |
975 | marker->_sbuf = fp; | |
40a55d20 | 976 | if (_IO_in_put_mode (fp)) |
d18ea0c5 | 977 | _IO_switch_to_get_mode (fp); |
40a55d20 | 978 | if (_IO_in_backup (fp)) |
96aa2d94 RM |
979 | marker->_pos = fp->_IO_read_ptr - fp->_IO_read_end; |
980 | else | |
981 | marker->_pos = fp->_IO_read_ptr - fp->_IO_read_base; | |
adfa2078 | 982 | |
96aa2d94 RM |
983 | /* Should perhaps sort the chain? */ |
984 | marker->_next = fp->_markers; | |
985 | fp->_markers = marker; | |
986 | } | |
987 | ||
988 | void | |
9d46370c | 989 | _IO_remove_marker (struct _IO_marker *marker) |
96aa2d94 RM |
990 | { |
991 | /* Unlink from sb's chain. */ | |
40a55d20 | 992 | struct _IO_marker **ptr = &marker->_sbuf->_markers; |
96aa2d94 RM |
993 | for (; ; ptr = &(*ptr)->_next) |
994 | { | |
995 | if (*ptr == NULL) | |
996 | break; | |
997 | else if (*ptr == marker) | |
998 | { | |
999 | *ptr = marker->_next; | |
1000 | return; | |
1001 | } | |
1002 | } | |
1003 | #if 0 | |
1004 | if _sbuf has a backup area that is no longer needed, should we delete | |
1005 | it now, or wait until the next underflow? | |
1006 | #endif | |
1007 | } | |
1008 | ||
1009 | #define BAD_DELTA EOF | |
1010 | ||
1011 | int | |
9d46370c | 1012 | _IO_marker_difference (struct _IO_marker *mark1, struct _IO_marker *mark2) |
96aa2d94 RM |
1013 | { |
1014 | return mark1->_pos - mark2->_pos; | |
1015 | } | |
1016 | ||
6d52618b | 1017 | /* Return difference between MARK and current position of MARK's stream. */ |
96aa2d94 | 1018 | int |
9d46370c | 1019 | _IO_marker_delta (struct _IO_marker *mark) |
96aa2d94 RM |
1020 | { |
1021 | int cur_pos; | |
1022 | if (mark->_sbuf == NULL) | |
1023 | return BAD_DELTA; | |
40a55d20 | 1024 | if (_IO_in_backup (mark->_sbuf)) |
96aa2d94 RM |
1025 | cur_pos = mark->_sbuf->_IO_read_ptr - mark->_sbuf->_IO_read_end; |
1026 | else | |
1027 | cur_pos = mark->_sbuf->_IO_read_ptr - mark->_sbuf->_IO_read_base; | |
1028 | return mark->_pos - cur_pos; | |
1029 | } | |
1030 | ||
1031 | int | |
9d46370c | 1032 | _IO_seekmark (_IO_FILE *fp, struct _IO_marker *mark, int delta) |
96aa2d94 RM |
1033 | { |
1034 | if (mark->_sbuf != fp) | |
1035 | return EOF; | |
1036 | if (mark->_pos >= 0) | |
1037 | { | |
40a55d20 UD |
1038 | if (_IO_in_backup (fp)) |
1039 | _IO_switch_to_main_get_area (fp); | |
96aa2d94 RM |
1040 | fp->_IO_read_ptr = fp->_IO_read_base + mark->_pos; |
1041 | } | |
1042 | else | |
1043 | { | |
40a55d20 | 1044 | if (!_IO_in_backup (fp)) |
05f732b3 | 1045 | _IO_switch_to_backup_area (fp); |
96aa2d94 RM |
1046 | fp->_IO_read_ptr = fp->_IO_read_end + mark->_pos; |
1047 | } | |
1048 | return 0; | |
1049 | } | |
1050 | ||
1051 | void | |
9d46370c | 1052 | _IO_unsave_markers (_IO_FILE *fp) |
96aa2d94 | 1053 | { |
40a55d20 | 1054 | struct _IO_marker *mark = fp->_markers; |
96aa2d94 RM |
1055 | if (mark) |
1056 | { | |
1057 | #ifdef TODO | |
40a55d20 | 1058 | streampos offset = seekoff (0, ios::cur, ios::in); |
96aa2d94 RM |
1059 | if (offset != EOF) |
1060 | { | |
40a55d20 | 1061 | offset += eGptr () - Gbase (); |
96aa2d94 | 1062 | for ( ; mark != NULL; mark = mark->_next) |
40a55d20 | 1063 | mark->set_streampos (mark->_pos + offset); |
96aa2d94 RM |
1064 | } |
1065 | else | |
1066 | { | |
1067 | for ( ; mark != NULL; mark = mark->_next) | |
40a55d20 | 1068 | mark->set_streampos (EOF); |
96aa2d94 RM |
1069 | } |
1070 | #endif | |
1071 | fp->_markers = 0; | |
1072 | } | |
1073 | ||
40a55d20 | 1074 | if (_IO_have_backup (fp)) |
d18ea0c5 | 1075 | _IO_free_backup_area (fp); |
96aa2d94 | 1076 | } |
d18ea0c5 | 1077 | libc_hidden_def (_IO_unsave_markers) |
96aa2d94 | 1078 | |
40a55d20 UD |
1079 | #if 0 |
1080 | /* Seems not to be needed. --drepper */ | |
96aa2d94 | 1081 | int |
9d46370c | 1082 | _IO_nobackup_pbackfail (_IO_FILE *fp, int c) |
96aa2d94 RM |
1083 | { |
1084 | if (fp->_IO_read_ptr > fp->_IO_read_base) | |
1085 | fp->_IO_read_ptr--; | |
1086 | if (c != EOF && *fp->_IO_read_ptr != c) | |
1087 | *fp->_IO_read_ptr = c; | |
40a55d20 | 1088 | return (unsigned char) c; |
96aa2d94 | 1089 | } |
40a55d20 | 1090 | #endif |
96aa2d94 RM |
1091 | |
1092 | int | |
9d46370c | 1093 | _IO_default_pbackfail (_IO_FILE *fp, int c) |
96aa2d94 | 1094 | { |
00bc5db0 | 1095 | if (fp->_IO_read_ptr > fp->_IO_read_base && !_IO_in_backup (fp) |
c94a8080 | 1096 | && (unsigned char) fp->_IO_read_ptr[-1] == c) |
00bc5db0 UD |
1097 | --fp->_IO_read_ptr; |
1098 | else | |
40a55d20 UD |
1099 | { |
1100 | /* Need to handle a filebuf in write mode (switch to read mode). FIXME!*/ | |
05f732b3 | 1101 | if (!_IO_in_backup (fp)) |
40a55d20 | 1102 | { |
05f732b3 UD |
1103 | /* We need to keep the invariant that the main get area |
1104 | logically follows the backup area. */ | |
1105 | if (fp->_IO_read_ptr > fp->_IO_read_base && _IO_have_backup (fp)) | |
1106 | { | |
1107 | if (save_for_backup (fp, fp->_IO_read_ptr)) | |
1108 | return EOF; | |
1109 | } | |
1110 | else if (!_IO_have_backup (fp)) | |
1111 | { | |
1112 | /* No backup buffer: allocate one. */ | |
1113 | /* Use nshort buffer, if unused? (probably not) FIXME */ | |
1114 | int backup_size = 128; | |
1115 | char *bbuf = (char *) malloc (backup_size); | |
1116 | if (bbuf == NULL) | |
1117 | return EOF; | |
1118 | fp->_IO_save_base = bbuf; | |
1119 | fp->_IO_save_end = fp->_IO_save_base + backup_size; | |
1120 | fp->_IO_backup_base = fp->_IO_save_end; | |
1121 | } | |
1122 | fp->_IO_read_base = fp->_IO_read_ptr; | |
40a55d20 UD |
1123 | _IO_switch_to_backup_area (fp); |
1124 | } | |
1125 | else if (fp->_IO_read_ptr <= fp->_IO_read_base) | |
1126 | { | |
1127 | /* Increase size of existing backup buffer. */ | |
1128 | _IO_size_t new_size; | |
1129 | _IO_size_t old_size = fp->_IO_read_end - fp->_IO_read_base; | |
1130 | char *new_buf; | |
1131 | new_size = 2 * old_size; | |
1132 | new_buf = (char *) malloc (new_size); | |
1133 | if (new_buf == NULL) | |
1134 | return EOF; | |
1135 | memcpy (new_buf + (new_size - old_size), fp->_IO_read_base, | |
1136 | old_size); | |
1137 | free (fp->_IO_read_base); | |
1138 | _IO_setg (fp, new_buf, new_buf + (new_size - old_size), | |
1139 | new_buf + new_size); | |
1140 | fp->_IO_backup_base = fp->_IO_read_ptr; | |
1141 | } | |
00bc5db0 UD |
1142 | |
1143 | *--fp->_IO_read_ptr = c; | |
40a55d20 | 1144 | } |
00bc5db0 | 1145 | return (unsigned char) c; |
96aa2d94 | 1146 | } |
d18ea0c5 | 1147 | libc_hidden_def (_IO_default_pbackfail) |
96aa2d94 | 1148 | |
d64b6ad0 | 1149 | _IO_off64_t |
9d46370c | 1150 | _IO_default_seek (_IO_FILE *fp, _IO_off64_t offset, int dir) |
96aa2d94 RM |
1151 | { |
1152 | return _IO_pos_BAD; | |
1153 | } | |
1154 | ||
1155 | int | |
9d46370c | 1156 | _IO_default_stat (_IO_FILE *fp, void *st) |
96aa2d94 RM |
1157 | { |
1158 | return EOF; | |
1159 | } | |
1160 | ||
1161 | _IO_ssize_t | |
9d46370c | 1162 | _IO_default_read (_IO_FILE *fp, void *data, _IO_ssize_t n) |
96aa2d94 RM |
1163 | { |
1164 | return -1; | |
1165 | } | |
1166 | ||
1167 | _IO_ssize_t | |
9d46370c | 1168 | _IO_default_write (_IO_FILE *fp, const void *data, _IO_ssize_t n) |
96aa2d94 RM |
1169 | { |
1170 | return 0; | |
1171 | } | |
1172 | ||
319d719d | 1173 | int |
9d46370c | 1174 | _IO_default_showmanyc (_IO_FILE *fp) |
dfd2257a UD |
1175 | { |
1176 | return -1; | |
1177 | } | |
1178 | ||
1179 | void | |
9d46370c | 1180 | _IO_default_imbue (_IO_FILE *fp, void *locale) |
dfd2257a UD |
1181 | { |
1182 | } | |
1183 | ||
3fc9ca4e | 1184 | _IO_ITER |
60d2f8f3 | 1185 | _IO_iter_begin (void) |
3fc9ca4e | 1186 | { |
d18ea0c5 | 1187 | return (_IO_ITER) _IO_list_all; |
3fc9ca4e | 1188 | } |
1d2b6e0c | 1189 | libc_hidden_def (_IO_iter_begin) |
3fc9ca4e UD |
1190 | |
1191 | _IO_ITER | |
60d2f8f3 | 1192 | _IO_iter_end (void) |
3fc9ca4e | 1193 | { |
2ca8b1ee | 1194 | return NULL; |
3fc9ca4e | 1195 | } |
1d2b6e0c | 1196 | libc_hidden_def (_IO_iter_end) |
3fc9ca4e UD |
1197 | |
1198 | _IO_ITER | |
9d46370c | 1199 | _IO_iter_next (_IO_ITER iter) |
3fc9ca4e | 1200 | { |
73c115ed | 1201 | return iter->_chain; |
3fc9ca4e | 1202 | } |
1d2b6e0c | 1203 | libc_hidden_def (_IO_iter_next) |
3fc9ca4e UD |
1204 | |
1205 | _IO_FILE * | |
9d46370c | 1206 | _IO_iter_file (_IO_ITER iter) |
3fc9ca4e | 1207 | { |
73c115ed | 1208 | return iter; |
3fc9ca4e | 1209 | } |
1d2b6e0c | 1210 | libc_hidden_def (_IO_iter_file) |
3fc9ca4e UD |
1211 | |
1212 | void | |
60d2f8f3 | 1213 | _IO_list_lock (void) |
3fc9ca4e | 1214 | { |
92cfdd8a | 1215 | #ifdef _IO_MTSAFE_IO |
3fc9ca4e | 1216 | _IO_lock_lock (list_all_lock); |
92cfdd8a | 1217 | #endif |
3fc9ca4e | 1218 | } |
245eab02 | 1219 | libc_hidden_def (_IO_list_lock) |
3fc9ca4e UD |
1220 | |
1221 | void | |
60d2f8f3 | 1222 | _IO_list_unlock (void) |
3fc9ca4e | 1223 | { |
92cfdd8a AJ |
1224 | #ifdef _IO_MTSAFE_IO |
1225 | _IO_lock_unlock (list_all_lock); | |
1226 | #endif | |
3fc9ca4e | 1227 | } |
245eab02 | 1228 | libc_hidden_def (_IO_list_unlock) |
3fc9ca4e UD |
1229 | |
1230 | void | |
60d2f8f3 | 1231 | _IO_list_resetlock (void) |
3fc9ca4e | 1232 | { |
92cfdd8a | 1233 | #ifdef _IO_MTSAFE_IO |
3fc9ca4e | 1234 | _IO_lock_init (list_all_lock); |
92cfdd8a | 1235 | #endif |
3fc9ca4e | 1236 | } |
245eab02 | 1237 | libc_hidden_def (_IO_list_resetlock) |
3fc9ca4e | 1238 | |
96aa2d94 RM |
1239 | |
1240 | #ifdef TODO | |
1241 | #if defined(linux) | |
1242 | #define IO_CLEANUP ; | |
1243 | #endif | |
1244 | ||
1245 | #ifdef IO_CLEANUP | |
1246 | IO_CLEANUP | |
1247 | #else | |
1248 | struct __io_defs { | |
1249 | __io_defs() { } | |
40a55d20 | 1250 | ~__io_defs() { _IO_cleanup (); } |
adfa2078 | 1251 | }; |
96aa2d94 RM |
1252 | __io_defs io_defs__; |
1253 | #endif | |
1254 | ||
1255 | #endif /* TODO */ | |
adfa2078 | 1256 | |
f65fd747 | 1257 | #ifdef text_set_element |
f5bf21a7 | 1258 | text_set_element(__libc_atexit, _IO_cleanup); |
f65fd747 | 1259 | #endif |