Use long double not double for _Float64 with old GCC if values the same.
[glibc.git] / libio / genops.c
blob8ba56dd710fb3cf25c5f41027966cdd8daad7d8f
1 /* Copyright (C) 1993-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>.
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. */
27 /* Generic or default I/O operations. */
29 #include "libioP.h"
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdbool.h>
33 #include <sched.h>
35 #ifdef _IO_MTSAFE_IO
36 static _IO_lock_t list_all_lock = _IO_lock_initializer;
37 #endif
39 static _IO_FILE *run_fp;
41 #ifdef _IO_MTSAFE_IO
42 static void
43 flush_cleanup (void *not_used)
45 if (run_fp != NULL)
46 _IO_funlockfile (run_fp);
47 _IO_lock_unlock (list_all_lock);
49 #endif
51 void
52 _IO_un_link (struct _IO_FILE_plus *fp)
54 if (fp->file._flags & _IO_LINKED)
56 struct _IO_FILE **f;
57 #ifdef _IO_MTSAFE_IO
58 _IO_cleanup_region_start_noarg (flush_cleanup);
59 _IO_lock_lock (list_all_lock);
60 run_fp = (_IO_FILE *) fp;
61 _IO_flockfile ((_IO_FILE *) fp);
62 #endif
63 if (_IO_list_all == NULL)
65 else if (fp == _IO_list_all)
66 _IO_list_all = (struct _IO_FILE_plus *) _IO_list_all->file._chain;
67 else
68 for (f = &_IO_list_all->file._chain; *f; f = &(*f)->_chain)
69 if (*f == (_IO_FILE *) fp)
71 *f = fp->file._chain;
72 break;
74 fp->file._flags &= ~_IO_LINKED;
75 #ifdef _IO_MTSAFE_IO
76 _IO_funlockfile ((_IO_FILE *) fp);
77 run_fp = NULL;
78 _IO_lock_unlock (list_all_lock);
79 _IO_cleanup_region_end (0);
80 #endif
83 libc_hidden_def (_IO_un_link)
85 void
86 _IO_link_in (struct _IO_FILE_plus *fp)
88 if ((fp->file._flags & _IO_LINKED) == 0)
90 fp->file._flags |= _IO_LINKED;
91 #ifdef _IO_MTSAFE_IO
92 _IO_cleanup_region_start_noarg (flush_cleanup);
93 _IO_lock_lock (list_all_lock);
94 run_fp = (_IO_FILE *) fp;
95 _IO_flockfile ((_IO_FILE *) fp);
96 #endif
97 fp->file._chain = (_IO_FILE *) _IO_list_all;
98 _IO_list_all = fp;
99 #ifdef _IO_MTSAFE_IO
100 _IO_funlockfile ((_IO_FILE *) fp);
101 run_fp = NULL;
102 _IO_lock_unlock (list_all_lock);
103 _IO_cleanup_region_end (0);
104 #endif
107 libc_hidden_def (_IO_link_in)
109 /* Return minimum _pos markers
110 Assumes the current get area is the main get area. */
111 _IO_ssize_t _IO_least_marker (_IO_FILE *fp, char *end_p);
113 _IO_ssize_t
114 _IO_least_marker (_IO_FILE *fp, char *end_p)
116 _IO_ssize_t least_so_far = end_p - fp->_IO_read_base;
117 struct _IO_marker *mark;
118 for (mark = fp->_markers; mark != NULL; mark = mark->_next)
119 if (mark->_pos < least_so_far)
120 least_so_far = mark->_pos;
121 return least_so_far;
124 /* Switch current get area from backup buffer to (start of) main get area. */
126 void
127 _IO_switch_to_main_get_area (_IO_FILE *fp)
129 char *tmp;
130 fp->_flags &= ~_IO_IN_BACKUP;
131 /* Swap _IO_read_end and _IO_save_end. */
132 tmp = fp->_IO_read_end;
133 fp->_IO_read_end = fp->_IO_save_end;
134 fp->_IO_save_end= tmp;
135 /* Swap _IO_read_base and _IO_save_base. */
136 tmp = fp->_IO_read_base;
137 fp->_IO_read_base = fp->_IO_save_base;
138 fp->_IO_save_base = tmp;
139 /* Set _IO_read_ptr. */
140 fp->_IO_read_ptr = fp->_IO_read_base;
143 /* Switch current get area from main get area to (end of) backup area. */
145 void
146 _IO_switch_to_backup_area (_IO_FILE *fp)
148 char *tmp;
149 fp->_flags |= _IO_IN_BACKUP;
150 /* Swap _IO_read_end and _IO_save_end. */
151 tmp = fp->_IO_read_end;
152 fp->_IO_read_end = fp->_IO_save_end;
153 fp->_IO_save_end = tmp;
154 /* Swap _IO_read_base and _IO_save_base. */
155 tmp = fp->_IO_read_base;
156 fp->_IO_read_base = fp->_IO_save_base;
157 fp->_IO_save_base = tmp;
158 /* Set _IO_read_ptr. */
159 fp->_IO_read_ptr = fp->_IO_read_end;
163 _IO_switch_to_get_mode (_IO_FILE *fp)
165 if (fp->_IO_write_ptr > fp->_IO_write_base)
166 if (_IO_OVERFLOW (fp, EOF) == EOF)
167 return EOF;
168 if (_IO_in_backup (fp))
169 fp->_IO_read_base = fp->_IO_backup_base;
170 else
172 fp->_IO_read_base = fp->_IO_buf_base;
173 if (fp->_IO_write_ptr > fp->_IO_read_end)
174 fp->_IO_read_end = fp->_IO_write_ptr;
176 fp->_IO_read_ptr = fp->_IO_write_ptr;
178 fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end = fp->_IO_read_ptr;
180 fp->_flags &= ~_IO_CURRENTLY_PUTTING;
181 return 0;
183 libc_hidden_def (_IO_switch_to_get_mode)
185 void
186 _IO_free_backup_area (_IO_FILE *fp)
188 if (_IO_in_backup (fp))
189 _IO_switch_to_main_get_area (fp); /* Just in case. */
190 free (fp->_IO_save_base);
191 fp->_IO_save_base = NULL;
192 fp->_IO_save_end = NULL;
193 fp->_IO_backup_base = NULL;
195 libc_hidden_def (_IO_free_backup_area)
197 #if 0
199 _IO_switch_to_put_mode (_IO_FILE *fp)
201 fp->_IO_write_base = fp->_IO_read_ptr;
202 fp->_IO_write_ptr = fp->_IO_read_ptr;
203 /* Following is wrong if line- or un-buffered? */
204 fp->_IO_write_end = (fp->_flags & _IO_IN_BACKUP
205 ? fp->_IO_read_end : fp->_IO_buf_end);
207 fp->_IO_read_ptr = fp->_IO_read_end;
208 fp->_IO_read_base = fp->_IO_read_end;
210 fp->_flags |= _IO_CURRENTLY_PUTTING;
211 return 0;
213 #endif
216 __overflow (_IO_FILE *f, int ch)
218 /* This is a single-byte stream. */
219 if (f->_mode == 0)
220 _IO_fwide (f, -1);
221 return _IO_OVERFLOW (f, ch);
223 libc_hidden_def (__overflow)
225 static int
226 save_for_backup (_IO_FILE *fp, char *end_p)
228 /* Append [_IO_read_base..end_p] to backup area. */
229 _IO_ssize_t least_mark = _IO_least_marker (fp, end_p);
230 /* needed_size is how much space we need in the backup area. */
231 _IO_size_t needed_size = (end_p - fp->_IO_read_base) - least_mark;
232 /* FIXME: Dubious arithmetic if pointers are NULL */
233 _IO_size_t current_Bsize = fp->_IO_save_end - fp->_IO_save_base;
234 _IO_size_t avail; /* Extra space available for future expansion. */
235 _IO_ssize_t delta;
236 struct _IO_marker *mark;
237 if (needed_size > current_Bsize)
239 char *new_buffer;
240 avail = 100;
241 new_buffer = (char *) malloc (avail + needed_size);
242 if (new_buffer == NULL)
243 return EOF; /* FIXME */
244 if (least_mark < 0)
246 __mempcpy (__mempcpy (new_buffer + avail,
247 fp->_IO_save_end + least_mark,
248 -least_mark),
249 fp->_IO_read_base,
250 end_p - fp->_IO_read_base);
252 else
253 memcpy (new_buffer + avail,
254 fp->_IO_read_base + least_mark,
255 needed_size);
256 free (fp->_IO_save_base);
257 fp->_IO_save_base = new_buffer;
258 fp->_IO_save_end = new_buffer + avail + needed_size;
260 else
262 avail = current_Bsize - needed_size;
263 if (least_mark < 0)
265 memmove (fp->_IO_save_base + avail,
266 fp->_IO_save_end + least_mark,
267 -least_mark);
268 memcpy (fp->_IO_save_base + avail - least_mark,
269 fp->_IO_read_base,
270 end_p - fp->_IO_read_base);
272 else if (needed_size > 0)
273 memcpy (fp->_IO_save_base + avail,
274 fp->_IO_read_base + least_mark,
275 needed_size);
277 fp->_IO_backup_base = fp->_IO_save_base + avail;
278 /* Adjust all the streammarkers. */
279 delta = end_p - fp->_IO_read_base;
280 for (mark = fp->_markers; mark != NULL; mark = mark->_next)
281 mark->_pos -= delta;
282 return 0;
286 __underflow (_IO_FILE *fp)
288 if (_IO_vtable_offset (fp) == 0 && _IO_fwide (fp, -1) != -1)
289 return EOF;
291 if (fp->_mode == 0)
292 _IO_fwide (fp, -1);
293 if (_IO_in_put_mode (fp))
294 if (_IO_switch_to_get_mode (fp) == EOF)
295 return EOF;
296 if (fp->_IO_read_ptr < fp->_IO_read_end)
297 return *(unsigned char *) fp->_IO_read_ptr;
298 if (_IO_in_backup (fp))
300 _IO_switch_to_main_get_area (fp);
301 if (fp->_IO_read_ptr < fp->_IO_read_end)
302 return *(unsigned char *) fp->_IO_read_ptr;
304 if (_IO_have_markers (fp))
306 if (save_for_backup (fp, fp->_IO_read_end))
307 return EOF;
309 else if (_IO_have_backup (fp))
310 _IO_free_backup_area (fp);
311 return _IO_UNDERFLOW (fp);
313 libc_hidden_def (__underflow)
316 __uflow (_IO_FILE *fp)
318 if (_IO_vtable_offset (fp) == 0 && _IO_fwide (fp, -1) != -1)
319 return EOF;
321 if (fp->_mode == 0)
322 _IO_fwide (fp, -1);
323 if (_IO_in_put_mode (fp))
324 if (_IO_switch_to_get_mode (fp) == EOF)
325 return EOF;
326 if (fp->_IO_read_ptr < fp->_IO_read_end)
327 return *(unsigned char *) fp->_IO_read_ptr++;
328 if (_IO_in_backup (fp))
330 _IO_switch_to_main_get_area (fp);
331 if (fp->_IO_read_ptr < fp->_IO_read_end)
332 return *(unsigned char *) fp->_IO_read_ptr++;
334 if (_IO_have_markers (fp))
336 if (save_for_backup (fp, fp->_IO_read_end))
337 return EOF;
339 else if (_IO_have_backup (fp))
340 _IO_free_backup_area (fp);
341 return _IO_UFLOW (fp);
343 libc_hidden_def (__uflow)
345 void
346 _IO_setb (_IO_FILE *f, char *b, char *eb, int a)
348 if (f->_IO_buf_base && !(f->_flags & _IO_USER_BUF))
349 free (f->_IO_buf_base);
350 f->_IO_buf_base = b;
351 f->_IO_buf_end = eb;
352 if (a)
353 f->_flags &= ~_IO_USER_BUF;
354 else
355 f->_flags |= _IO_USER_BUF;
357 libc_hidden_def (_IO_setb)
359 void
360 _IO_doallocbuf (_IO_FILE *fp)
362 if (fp->_IO_buf_base)
363 return;
364 if (!(fp->_flags & _IO_UNBUFFERED) || fp->_mode > 0)
365 if (_IO_DOALLOCATE (fp) != EOF)
366 return;
367 _IO_setb (fp, fp->_shortbuf, fp->_shortbuf+1, 0);
369 libc_hidden_def (_IO_doallocbuf)
372 _IO_default_underflow (_IO_FILE *fp)
374 return EOF;
378 _IO_default_uflow (_IO_FILE *fp)
380 int ch = _IO_UNDERFLOW (fp);
381 if (ch == EOF)
382 return EOF;
383 return *(unsigned char *) fp->_IO_read_ptr++;
385 libc_hidden_def (_IO_default_uflow)
387 _IO_size_t
388 _IO_default_xsputn (_IO_FILE *f, const void *data, _IO_size_t n)
390 const char *s = (char *) data;
391 _IO_size_t more = n;
392 if (more <= 0)
393 return 0;
394 for (;;)
396 /* Space available. */
397 if (f->_IO_write_ptr < f->_IO_write_end)
399 _IO_size_t count = f->_IO_write_end - f->_IO_write_ptr;
400 if (count > more)
401 count = more;
402 if (count > 20)
404 f->_IO_write_ptr = __mempcpy (f->_IO_write_ptr, s, count);
405 s += count;
407 else if (count)
409 char *p = f->_IO_write_ptr;
410 _IO_ssize_t i;
411 for (i = count; --i >= 0; )
412 *p++ = *s++;
413 f->_IO_write_ptr = p;
415 more -= count;
417 if (more == 0 || _IO_OVERFLOW (f, (unsigned char) *s++) == EOF)
418 break;
419 more--;
421 return n - more;
423 libc_hidden_def (_IO_default_xsputn)
425 _IO_size_t
426 _IO_sgetn (_IO_FILE *fp, void *data, _IO_size_t n)
428 /* FIXME handle putback buffer here! */
429 return _IO_XSGETN (fp, data, n);
431 libc_hidden_def (_IO_sgetn)
433 _IO_size_t
434 _IO_default_xsgetn (_IO_FILE *fp, void *data, _IO_size_t n)
436 _IO_size_t more = n;
437 char *s = (char*) data;
438 for (;;)
440 /* Data available. */
441 if (fp->_IO_read_ptr < fp->_IO_read_end)
443 _IO_size_t count = fp->_IO_read_end - fp->_IO_read_ptr;
444 if (count > more)
445 count = more;
446 if (count > 20)
448 s = __mempcpy (s, fp->_IO_read_ptr, count);
449 fp->_IO_read_ptr += count;
451 else if (count)
453 char *p = fp->_IO_read_ptr;
454 int i = (int) count;
455 while (--i >= 0)
456 *s++ = *p++;
457 fp->_IO_read_ptr = p;
459 more -= count;
461 if (more == 0 || __underflow (fp) == EOF)
462 break;
464 return n - more;
466 libc_hidden_def (_IO_default_xsgetn)
468 #if 0
469 /* Seems not to be needed. --drepper */
471 _IO_sync (_IO_FILE *fp)
473 return 0;
475 #endif
477 _IO_FILE *
478 _IO_default_setbuf (_IO_FILE *fp, char *p, _IO_ssize_t len)
480 if (_IO_SYNC (fp) == EOF)
481 return NULL;
482 if (p == NULL || len == 0)
484 fp->_flags |= _IO_UNBUFFERED;
485 _IO_setb (fp, fp->_shortbuf, fp->_shortbuf+1, 0);
487 else
489 fp->_flags &= ~_IO_UNBUFFERED;
490 _IO_setb (fp, p, p+len, 0);
492 fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end = 0;
493 fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_read_end = 0;
494 return fp;
497 _IO_off64_t
498 _IO_default_seekpos (_IO_FILE *fp, _IO_off64_t pos, int mode)
500 return _IO_SEEKOFF (fp, pos, 0, mode);
504 _IO_default_doallocate (_IO_FILE *fp)
506 char *buf;
508 buf = malloc(_IO_BUFSIZ);
509 if (__glibc_unlikely (buf == NULL))
510 return EOF;
512 _IO_setb (fp, buf, buf+_IO_BUFSIZ, 1);
513 return 1;
515 libc_hidden_def (_IO_default_doallocate)
517 void
518 _IO_init_internal (_IO_FILE *fp, int flags)
520 _IO_no_init (fp, flags, -1, NULL, NULL);
523 void
524 _IO_init (_IO_FILE *fp, int flags)
526 IO_set_accept_foreign_vtables (&_IO_vtable_check);
527 _IO_init_internal (fp, flags);
530 static int stdio_needs_locking;
532 /* In a single-threaded process most stdio locks can be omitted. After
533 _IO_enable_locks is called, locks are not optimized away any more.
534 It must be first called while the process is still single-threaded.
536 This lock optimization can be disabled on a per-file basis by setting
537 _IO_FLAGS2_NEED_LOCK, because a file can have user-defined callbacks
538 or can be locked with flockfile and then a thread may be created
539 between a lock and unlock, so omitting the lock is not valid.
541 Here we have to make sure that the flag is set on all existing files
542 and files created later. */
543 void
544 _IO_enable_locks (void)
546 _IO_ITER i;
548 if (stdio_needs_locking)
549 return;
550 stdio_needs_locking = 1;
551 for (i = _IO_iter_begin (); i != _IO_iter_end (); i = _IO_iter_next (i))
552 _IO_iter_file (i)->_flags2 |= _IO_FLAGS2_NEED_LOCK;
554 libc_hidden_def (_IO_enable_locks)
556 void
557 _IO_old_init (_IO_FILE *fp, int flags)
559 fp->_flags = _IO_MAGIC|flags;
560 fp->_flags2 = 0;
561 if (stdio_needs_locking)
562 fp->_flags2 |= _IO_FLAGS2_NEED_LOCK;
563 fp->_IO_buf_base = NULL;
564 fp->_IO_buf_end = NULL;
565 fp->_IO_read_base = NULL;
566 fp->_IO_read_ptr = NULL;
567 fp->_IO_read_end = NULL;
568 fp->_IO_write_base = NULL;
569 fp->_IO_write_ptr = NULL;
570 fp->_IO_write_end = NULL;
571 fp->_chain = NULL; /* Not necessary. */
573 fp->_IO_save_base = NULL;
574 fp->_IO_backup_base = NULL;
575 fp->_IO_save_end = NULL;
576 fp->_markers = NULL;
577 fp->_cur_column = 0;
578 #if _IO_JUMPS_OFFSET
579 fp->_vtable_offset = 0;
580 #endif
581 #ifdef _IO_MTSAFE_IO
582 if (fp->_lock != NULL)
583 _IO_lock_init (*fp->_lock);
584 #endif
587 void
588 _IO_no_init (_IO_FILE *fp, int flags, int orientation,
589 struct _IO_wide_data *wd, const struct _IO_jump_t *jmp)
591 _IO_old_init (fp, flags);
592 fp->_mode = orientation;
593 if (orientation >= 0)
595 fp->_wide_data = wd;
596 fp->_wide_data->_IO_buf_base = NULL;
597 fp->_wide_data->_IO_buf_end = NULL;
598 fp->_wide_data->_IO_read_base = NULL;
599 fp->_wide_data->_IO_read_ptr = NULL;
600 fp->_wide_data->_IO_read_end = NULL;
601 fp->_wide_data->_IO_write_base = NULL;
602 fp->_wide_data->_IO_write_ptr = NULL;
603 fp->_wide_data->_IO_write_end = NULL;
604 fp->_wide_data->_IO_save_base = NULL;
605 fp->_wide_data->_IO_backup_base = NULL;
606 fp->_wide_data->_IO_save_end = NULL;
608 fp->_wide_data->_wide_vtable = jmp;
610 else
611 /* Cause predictable crash when a wide function is called on a byte
612 stream. */
613 fp->_wide_data = (struct _IO_wide_data *) -1L;
614 fp->_freeres_list = NULL;
618 _IO_default_sync (_IO_FILE *fp)
620 return 0;
623 /* The way the C++ classes are mapped into the C functions in the
624 current implementation, this function can get called twice! */
626 void
627 _IO_default_finish (_IO_FILE *fp, int dummy)
629 struct _IO_marker *mark;
630 if (fp->_IO_buf_base && !(fp->_flags & _IO_USER_BUF))
632 free (fp->_IO_buf_base);
633 fp->_IO_buf_base = fp->_IO_buf_end = NULL;
636 for (mark = fp->_markers; mark != NULL; mark = mark->_next)
637 mark->_sbuf = NULL;
639 if (fp->_IO_save_base)
641 free (fp->_IO_save_base);
642 fp->_IO_save_base = NULL;
645 _IO_un_link ((struct _IO_FILE_plus *) fp);
647 #ifdef _IO_MTSAFE_IO
648 if (fp->_lock != NULL)
649 _IO_lock_fini (*fp->_lock);
650 #endif
652 libc_hidden_def (_IO_default_finish)
654 _IO_off64_t
655 _IO_default_seekoff (_IO_FILE *fp, _IO_off64_t offset, int dir, int mode)
657 return _IO_pos_BAD;
661 _IO_sputbackc (_IO_FILE *fp, int c)
663 int result;
665 if (fp->_IO_read_ptr > fp->_IO_read_base
666 && (unsigned char)fp->_IO_read_ptr[-1] == (unsigned char)c)
668 fp->_IO_read_ptr--;
669 result = (unsigned char) c;
671 else
672 result = _IO_PBACKFAIL (fp, c);
674 if (result != EOF)
675 fp->_flags &= ~_IO_EOF_SEEN;
677 return result;
679 libc_hidden_def (_IO_sputbackc)
682 _IO_sungetc (_IO_FILE *fp)
684 int result;
686 if (fp->_IO_read_ptr > fp->_IO_read_base)
688 fp->_IO_read_ptr--;
689 result = (unsigned char) *fp->_IO_read_ptr;
691 else
692 result = _IO_PBACKFAIL (fp, EOF);
694 if (result != EOF)
695 fp->_flags &= ~_IO_EOF_SEEN;
697 return result;
700 #if 0 /* Work in progress */
701 /* Seems not to be needed. */
702 #if 0
703 void
704 _IO_set_column (_IO_FILE *fp, int c)
706 if (c == -1)
707 fp->_column = -1;
708 else
709 fp->_column = c - (fp->_IO_write_ptr - fp->_IO_write_base);
711 #else
713 _IO_set_column (_IO_FILE *fp, int i)
715 fp->_cur_column = i + 1;
716 return 0;
718 #endif
719 #endif
722 unsigned
723 _IO_adjust_column (unsigned start, const char *line, int count)
725 const char *ptr = line + count;
726 while (ptr > line)
727 if (*--ptr == '\n')
728 return line + count - ptr - 1;
729 return start + count;
731 libc_hidden_def (_IO_adjust_column)
733 #if 0
734 /* Seems not to be needed. --drepper */
736 _IO_get_column (_IO_FILE *fp)
738 if (fp->_cur_column)
739 return _IO_adjust_column (fp->_cur_column - 1,
740 fp->_IO_write_base,
741 fp->_IO_write_ptr - fp->_IO_write_base);
742 return -1;
744 #endif
748 _IO_flush_all_lockp (int do_lock)
750 int result = 0;
751 struct _IO_FILE *fp;
753 #ifdef _IO_MTSAFE_IO
754 _IO_cleanup_region_start_noarg (flush_cleanup);
755 _IO_lock_lock (list_all_lock);
756 #endif
758 for (fp = (_IO_FILE *) _IO_list_all; fp != NULL; fp = fp->_chain)
760 run_fp = fp;
761 if (do_lock)
762 _IO_flockfile (fp);
764 if (((fp->_mode <= 0 && fp->_IO_write_ptr > fp->_IO_write_base)
765 || (_IO_vtable_offset (fp) == 0
766 && fp->_mode > 0 && (fp->_wide_data->_IO_write_ptr
767 > fp->_wide_data->_IO_write_base))
769 && _IO_OVERFLOW (fp, EOF) == EOF)
770 result = EOF;
772 if (do_lock)
773 _IO_funlockfile (fp);
774 run_fp = NULL;
777 #ifdef _IO_MTSAFE_IO
778 _IO_lock_unlock (list_all_lock);
779 _IO_cleanup_region_end (0);
780 #endif
782 return result;
787 _IO_flush_all (void)
789 /* We want locking. */
790 return _IO_flush_all_lockp (1);
792 libc_hidden_def (_IO_flush_all)
794 void
795 _IO_flush_all_linebuffered (void)
797 struct _IO_FILE *fp;
799 #ifdef _IO_MTSAFE_IO
800 _IO_cleanup_region_start_noarg (flush_cleanup);
801 _IO_lock_lock (list_all_lock);
802 #endif
804 for (fp = (_IO_FILE *) _IO_list_all; fp != NULL; fp = fp->_chain)
806 run_fp = fp;
807 _IO_flockfile (fp);
809 if ((fp->_flags & _IO_NO_WRITES) == 0 && fp->_flags & _IO_LINE_BUF)
810 _IO_OVERFLOW (fp, EOF);
812 _IO_funlockfile (fp);
813 run_fp = NULL;
816 #ifdef _IO_MTSAFE_IO
817 _IO_lock_unlock (list_all_lock);
818 _IO_cleanup_region_end (0);
819 #endif
821 libc_hidden_def (_IO_flush_all_linebuffered)
822 weak_alias (_IO_flush_all_linebuffered, _flushlbf)
825 /* The following is a bit tricky. In general, we want to unbuffer the
826 streams so that all output which follows is seen. If we are not
827 looking for memory leaks it does not make much sense to free the
828 actual buffer because this will happen anyway once the program
829 terminated. If we do want to look for memory leaks we have to free
830 the buffers. Whether something is freed is determined by the
831 function sin the libc_freeres section. Those are called as part of
832 the atexit routine, just like _IO_cleanup. The problem is we do
833 not know whether the freeres code is called first or _IO_cleanup.
834 if the former is the case, we set the DEALLOC_BUFFER variable to
835 true and _IO_unbuffer_all will take care of the rest. If
836 _IO_unbuffer_all is called first we add the streams to a list
837 which the freeres function later can walk through. */
838 static void _IO_unbuffer_all (void);
840 static bool dealloc_buffers;
841 static _IO_FILE *freeres_list;
843 static void
844 _IO_unbuffer_all (void)
846 struct _IO_FILE *fp;
848 #ifdef _IO_MTSAFE_IO
849 _IO_cleanup_region_start_noarg (flush_cleanup);
850 _IO_lock_lock (list_all_lock);
851 #endif
853 for (fp = (_IO_FILE *) _IO_list_all; fp; fp = fp->_chain)
855 if (! (fp->_flags & _IO_UNBUFFERED)
856 /* Iff stream is un-orientated, it wasn't used. */
857 && fp->_mode != 0)
859 #ifdef _IO_MTSAFE_IO
860 int cnt;
861 #define MAXTRIES 2
862 for (cnt = 0; cnt < MAXTRIES; ++cnt)
863 if (fp->_lock == NULL || _IO_lock_trylock (*fp->_lock) == 0)
864 break;
865 else
866 /* Give the other thread time to finish up its use of the
867 stream. */
868 __sched_yield ();
869 #endif
871 if (! dealloc_buffers && !(fp->_flags & _IO_USER_BUF))
873 fp->_flags |= _IO_USER_BUF;
875 fp->_freeres_list = freeres_list;
876 freeres_list = fp;
877 fp->_freeres_buf = fp->_IO_buf_base;
880 _IO_SETBUF (fp, NULL, 0);
882 if (fp->_mode > 0)
883 _IO_wsetb (fp, NULL, NULL, 0);
885 #ifdef _IO_MTSAFE_IO
886 if (cnt < MAXTRIES && fp->_lock != NULL)
887 _IO_lock_unlock (*fp->_lock);
888 #endif
891 /* Make sure that never again the wide char functions can be
892 used. */
893 fp->_mode = -1;
896 #ifdef _IO_MTSAFE_IO
897 _IO_lock_unlock (list_all_lock);
898 _IO_cleanup_region_end (0);
899 #endif
903 libc_freeres_fn (buffer_free)
905 dealloc_buffers = true;
907 while (freeres_list != NULL)
909 free (freeres_list->_freeres_buf);
911 freeres_list = freeres_list->_freeres_list;
917 _IO_cleanup (void)
919 /* We do *not* want locking. Some threads might use streams but
920 that is their problem, we flush them underneath them. */
921 int result = _IO_flush_all_lockp (0);
923 /* We currently don't have a reliable mechanism for making sure that
924 C++ static destructors are executed in the correct order.
925 So it is possible that other static destructors might want to
926 write to cout - and they're supposed to be able to do so.
928 The following will make the standard streambufs be unbuffered,
929 which forces any output from late destructors to be written out. */
930 _IO_unbuffer_all ();
932 return result;
936 void
937 _IO_init_marker (struct _IO_marker *marker, _IO_FILE *fp)
939 marker->_sbuf = fp;
940 if (_IO_in_put_mode (fp))
941 _IO_switch_to_get_mode (fp);
942 if (_IO_in_backup (fp))
943 marker->_pos = fp->_IO_read_ptr - fp->_IO_read_end;
944 else
945 marker->_pos = fp->_IO_read_ptr - fp->_IO_read_base;
947 /* Should perhaps sort the chain? */
948 marker->_next = fp->_markers;
949 fp->_markers = marker;
952 void
953 _IO_remove_marker (struct _IO_marker *marker)
955 /* Unlink from sb's chain. */
956 struct _IO_marker **ptr = &marker->_sbuf->_markers;
957 for (; ; ptr = &(*ptr)->_next)
959 if (*ptr == NULL)
960 break;
961 else if (*ptr == marker)
963 *ptr = marker->_next;
964 return;
967 #if 0
968 if _sbuf has a backup area that is no longer needed, should we delete
969 it now, or wait until the next underflow?
970 #endif
973 #define BAD_DELTA EOF
976 _IO_marker_difference (struct _IO_marker *mark1, struct _IO_marker *mark2)
978 return mark1->_pos - mark2->_pos;
981 /* Return difference between MARK and current position of MARK's stream. */
983 _IO_marker_delta (struct _IO_marker *mark)
985 int cur_pos;
986 if (mark->_sbuf == NULL)
987 return BAD_DELTA;
988 if (_IO_in_backup (mark->_sbuf))
989 cur_pos = mark->_sbuf->_IO_read_ptr - mark->_sbuf->_IO_read_end;
990 else
991 cur_pos = mark->_sbuf->_IO_read_ptr - mark->_sbuf->_IO_read_base;
992 return mark->_pos - cur_pos;
996 _IO_seekmark (_IO_FILE *fp, struct _IO_marker *mark, int delta)
998 if (mark->_sbuf != fp)
999 return EOF;
1000 if (mark->_pos >= 0)
1002 if (_IO_in_backup (fp))
1003 _IO_switch_to_main_get_area (fp);
1004 fp->_IO_read_ptr = fp->_IO_read_base + mark->_pos;
1006 else
1008 if (!_IO_in_backup (fp))
1009 _IO_switch_to_backup_area (fp);
1010 fp->_IO_read_ptr = fp->_IO_read_end + mark->_pos;
1012 return 0;
1015 void
1016 _IO_unsave_markers (_IO_FILE *fp)
1018 struct _IO_marker *mark = fp->_markers;
1019 if (mark)
1021 #ifdef TODO
1022 streampos offset = seekoff (0, ios::cur, ios::in);
1023 if (offset != EOF)
1025 offset += eGptr () - Gbase ();
1026 for ( ; mark != NULL; mark = mark->_next)
1027 mark->set_streampos (mark->_pos + offset);
1029 else
1031 for ( ; mark != NULL; mark = mark->_next)
1032 mark->set_streampos (EOF);
1034 #endif
1035 fp->_markers = 0;
1038 if (_IO_have_backup (fp))
1039 _IO_free_backup_area (fp);
1041 libc_hidden_def (_IO_unsave_markers)
1043 #if 0
1044 /* Seems not to be needed. --drepper */
1046 _IO_nobackup_pbackfail (_IO_FILE *fp, int c)
1048 if (fp->_IO_read_ptr > fp->_IO_read_base)
1049 fp->_IO_read_ptr--;
1050 if (c != EOF && *fp->_IO_read_ptr != c)
1051 *fp->_IO_read_ptr = c;
1052 return (unsigned char) c;
1054 #endif
1057 _IO_default_pbackfail (_IO_FILE *fp, int c)
1059 if (fp->_IO_read_ptr > fp->_IO_read_base && !_IO_in_backup (fp)
1060 && (unsigned char) fp->_IO_read_ptr[-1] == c)
1061 --fp->_IO_read_ptr;
1062 else
1064 /* Need to handle a filebuf in write mode (switch to read mode). FIXME!*/
1065 if (!_IO_in_backup (fp))
1067 /* We need to keep the invariant that the main get area
1068 logically follows the backup area. */
1069 if (fp->_IO_read_ptr > fp->_IO_read_base && _IO_have_backup (fp))
1071 if (save_for_backup (fp, fp->_IO_read_ptr))
1072 return EOF;
1074 else if (!_IO_have_backup (fp))
1076 /* No backup buffer: allocate one. */
1077 /* Use nshort buffer, if unused? (probably not) FIXME */
1078 int backup_size = 128;
1079 char *bbuf = (char *) malloc (backup_size);
1080 if (bbuf == NULL)
1081 return EOF;
1082 fp->_IO_save_base = bbuf;
1083 fp->_IO_save_end = fp->_IO_save_base + backup_size;
1084 fp->_IO_backup_base = fp->_IO_save_end;
1086 fp->_IO_read_base = fp->_IO_read_ptr;
1087 _IO_switch_to_backup_area (fp);
1089 else if (fp->_IO_read_ptr <= fp->_IO_read_base)
1091 /* Increase size of existing backup buffer. */
1092 _IO_size_t new_size;
1093 _IO_size_t old_size = fp->_IO_read_end - fp->_IO_read_base;
1094 char *new_buf;
1095 new_size = 2 * old_size;
1096 new_buf = (char *) malloc (new_size);
1097 if (new_buf == NULL)
1098 return EOF;
1099 memcpy (new_buf + (new_size - old_size), fp->_IO_read_base,
1100 old_size);
1101 free (fp->_IO_read_base);
1102 _IO_setg (fp, new_buf, new_buf + (new_size - old_size),
1103 new_buf + new_size);
1104 fp->_IO_backup_base = fp->_IO_read_ptr;
1107 *--fp->_IO_read_ptr = c;
1109 return (unsigned char) c;
1111 libc_hidden_def (_IO_default_pbackfail)
1113 _IO_off64_t
1114 _IO_default_seek (_IO_FILE *fp, _IO_off64_t offset, int dir)
1116 return _IO_pos_BAD;
1120 _IO_default_stat (_IO_FILE *fp, void *st)
1122 return EOF;
1125 _IO_ssize_t
1126 _IO_default_read (_IO_FILE *fp, void *data, _IO_ssize_t n)
1128 return -1;
1131 _IO_ssize_t
1132 _IO_default_write (_IO_FILE *fp, const void *data, _IO_ssize_t n)
1134 return 0;
1138 _IO_default_showmanyc (_IO_FILE *fp)
1140 return -1;
1143 void
1144 _IO_default_imbue (_IO_FILE *fp, void *locale)
1148 _IO_ITER
1149 _IO_iter_begin (void)
1151 return (_IO_ITER) _IO_list_all;
1153 libc_hidden_def (_IO_iter_begin)
1155 _IO_ITER
1156 _IO_iter_end (void)
1158 return NULL;
1160 libc_hidden_def (_IO_iter_end)
1162 _IO_ITER
1163 _IO_iter_next (_IO_ITER iter)
1165 return iter->_chain;
1167 libc_hidden_def (_IO_iter_next)
1169 _IO_FILE *
1170 _IO_iter_file (_IO_ITER iter)
1172 return iter;
1174 libc_hidden_def (_IO_iter_file)
1176 void
1177 _IO_list_lock (void)
1179 #ifdef _IO_MTSAFE_IO
1180 _IO_lock_lock (list_all_lock);
1181 #endif
1183 libc_hidden_def (_IO_list_lock)
1185 void
1186 _IO_list_unlock (void)
1188 #ifdef _IO_MTSAFE_IO
1189 _IO_lock_unlock (list_all_lock);
1190 #endif
1192 libc_hidden_def (_IO_list_unlock)
1194 void
1195 _IO_list_resetlock (void)
1197 #ifdef _IO_MTSAFE_IO
1198 _IO_lock_init (list_all_lock);
1199 #endif
1201 libc_hidden_def (_IO_list_resetlock)
1204 #ifdef TODO
1205 #if defined(linux)
1206 #define IO_CLEANUP ;
1207 #endif
1209 #ifdef IO_CLEANUP
1210 IO_CLEANUP
1211 #else
1212 struct __io_defs {
1213 __io_defs() { }
1214 ~__io_defs() { _IO_cleanup (); }
1216 __io_defs io_defs__;
1217 #endif
1219 #endif /* TODO */
1221 #ifdef text_set_element
1222 text_set_element(__libc_atexit, _IO_cleanup);
1223 #endif