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