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. */
36 static _IO_lock_t list_all_lock
= _IO_lock_initializer
;
39 static _IO_FILE
*run_fp
;
43 flush_cleanup (void *not_used
)
46 _IO_funlockfile (run_fp
);
47 _IO_lock_unlock (list_all_lock
);
52 _IO_un_link (struct _IO_FILE_plus
*fp
)
54 if (fp
->file
._flags
& _IO_LINKED
)
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
);
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
;
68 for (f
= &_IO_list_all
->file
._chain
; *f
; f
= &(*f
)->_chain
)
69 if (*f
== (_IO_FILE
*) fp
)
74 fp
->file
._flags
&= ~_IO_LINKED
;
76 _IO_funlockfile ((_IO_FILE
*) fp
);
78 _IO_lock_unlock (list_all_lock
);
79 _IO_cleanup_region_end (0);
83 libc_hidden_def (_IO_un_link
)
86 _IO_link_in (struct _IO_FILE_plus
*fp
)
88 if ((fp
->file
._flags
& _IO_LINKED
) == 0)
90 fp
->file
._flags
|= _IO_LINKED
;
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
);
97 fp
->file
._chain
= (_IO_FILE
*) _IO_list_all
;
100 _IO_funlockfile ((_IO_FILE
*) fp
);
102 _IO_lock_unlock (list_all_lock
);
103 _IO_cleanup_region_end (0);
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
);
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
;
124 /* Switch current get area from backup buffer to (start of) main get area. */
127 _IO_switch_to_main_get_area (_IO_FILE
*fp
)
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. */
146 _IO_switch_to_backup_area (_IO_FILE
*fp
)
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
)
168 if (_IO_in_backup (fp
))
169 fp
->_IO_read_base
= fp
->_IO_backup_base
;
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
;
183 libc_hidden_def (_IO_switch_to_get_mode
)
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
)
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
;
216 __overflow (_IO_FILE
*f
, int ch
)
218 /* This is a single-byte stream. */
221 return _IO_OVERFLOW (f
, ch
);
223 libc_hidden_def (__overflow
)
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. */
236 struct _IO_marker
*mark
;
237 if (needed_size
> current_Bsize
)
241 new_buffer
= (char *) malloc (avail
+ needed_size
);
242 if (new_buffer
== NULL
)
243 return EOF
; /* FIXME */
246 __mempcpy (__mempcpy (new_buffer
+ avail
,
247 fp
->_IO_save_end
+ least_mark
,
250 end_p
- fp
->_IO_read_base
);
253 memcpy (new_buffer
+ avail
,
254 fp
->_IO_read_base
+ least_mark
,
256 free (fp
->_IO_save_base
);
257 fp
->_IO_save_base
= new_buffer
;
258 fp
->_IO_save_end
= new_buffer
+ avail
+ needed_size
;
262 avail
= current_Bsize
- needed_size
;
265 memmove (fp
->_IO_save_base
+ avail
,
266 fp
->_IO_save_end
+ least_mark
,
268 memcpy (fp
->_IO_save_base
+ avail
- least_mark
,
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
,
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
)
286 __underflow (_IO_FILE
*fp
)
288 if (_IO_vtable_offset (fp
) == 0 && _IO_fwide (fp
, -1) != -1)
293 if (_IO_in_put_mode (fp
))
294 if (_IO_switch_to_get_mode (fp
) == 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
))
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)
323 if (_IO_in_put_mode (fp
))
324 if (_IO_switch_to_get_mode (fp
) == 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
))
339 else if (_IO_have_backup (fp
))
340 _IO_free_backup_area (fp
);
341 return _IO_UFLOW (fp
);
343 libc_hidden_def (__uflow
)
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
);
353 f
->_flags
&= ~_IO_USER_BUF
;
355 f
->_flags
|= _IO_USER_BUF
;
357 libc_hidden_def (_IO_setb
)
360 _IO_doallocbuf (_IO_FILE
*fp
)
362 if (fp
->_IO_buf_base
)
364 if (!(fp
->_flags
& _IO_UNBUFFERED
) || fp
->_mode
> 0)
365 if (_IO_DOALLOCATE (fp
) != EOF
)
367 _IO_setb (fp
, fp
->_shortbuf
, fp
->_shortbuf
+1, 0);
369 libc_hidden_def (_IO_doallocbuf
)
372 _IO_default_underflow (_IO_FILE
*fp
)
378 _IO_default_uflow (_IO_FILE
*fp
)
380 int ch
= _IO_UNDERFLOW (fp
);
383 return *(unsigned char *) fp
->_IO_read_ptr
++;
385 libc_hidden_def (_IO_default_uflow
)
388 _IO_default_xsputn (_IO_FILE
*f
, const void *data
, _IO_size_t n
)
390 const char *s
= (char *) data
;
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
;
404 f
->_IO_write_ptr
= __mempcpy (f
->_IO_write_ptr
, s
, count
);
409 char *p
= f
->_IO_write_ptr
;
411 for (i
= count
; --i
>= 0; )
413 f
->_IO_write_ptr
= p
;
417 if (more
== 0 || _IO_OVERFLOW (f
, (unsigned char) *s
++) == EOF
)
423 libc_hidden_def (_IO_default_xsputn
)
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
)
434 _IO_default_xsgetn (_IO_FILE
*fp
, void *data
, _IO_size_t n
)
437 char *s
= (char*) data
;
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
;
448 s
= __mempcpy (s
, fp
->_IO_read_ptr
, count
);
449 fp
->_IO_read_ptr
+= count
;
453 char *p
= fp
->_IO_read_ptr
;
457 fp
->_IO_read_ptr
= p
;
461 if (more
== 0 || __underflow (fp
) == EOF
)
466 libc_hidden_def (_IO_default_xsgetn
)
469 /* Seems not to be needed. --drepper */
471 _IO_sync (_IO_FILE
*fp
)
478 _IO_default_setbuf (_IO_FILE
*fp
, char *p
, _IO_ssize_t len
)
480 if (_IO_SYNC (fp
) == EOF
)
482 if (p
== NULL
|| len
== 0)
484 fp
->_flags
|= _IO_UNBUFFERED
;
485 _IO_setb (fp
, fp
->_shortbuf
, fp
->_shortbuf
+1, 0);
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;
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
)
508 buf
= malloc(_IO_BUFSIZ
);
509 if (__glibc_unlikely (buf
== NULL
))
512 _IO_setb (fp
, buf
, buf
+_IO_BUFSIZ
, 1);
515 libc_hidden_def (_IO_default_doallocate
)
518 _IO_init_internal (_IO_FILE
*fp
, int flags
)
520 _IO_no_init (fp
, flags
, -1, NULL
, NULL
);
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. */
544 _IO_enable_locks (void)
548 if (stdio_needs_locking
)
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
)
557 _IO_old_init (_IO_FILE
*fp
, int flags
)
559 fp
->_flags
= _IO_MAGIC
|flags
;
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
;
579 fp
->_vtable_offset
= 0;
582 if (fp
->_lock
!= NULL
)
583 _IO_lock_init (*fp
->_lock
);
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)
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
;
611 /* Cause predictable crash when a wide function is called on a byte
613 fp
->_wide_data
= (struct _IO_wide_data
*) -1L;
614 fp
->_freeres_list
= NULL
;
618 _IO_default_sync (_IO_FILE
*fp
)
623 /* The way the C++ classes are mapped into the C functions in the
624 current implementation, this function can get called twice! */
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
)
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
);
648 if (fp
->_lock
!= NULL
)
649 _IO_lock_fini (*fp
->_lock
);
652 libc_hidden_def (_IO_default_finish
)
655 _IO_default_seekoff (_IO_FILE
*fp
, _IO_off64_t offset
, int dir
, int mode
)
661 _IO_sputbackc (_IO_FILE
*fp
, int c
)
665 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
666 && (unsigned char)fp
->_IO_read_ptr
[-1] == (unsigned char)c
)
669 result
= (unsigned char) c
;
672 result
= _IO_PBACKFAIL (fp
, c
);
675 fp
->_flags
&= ~_IO_EOF_SEEN
;
679 libc_hidden_def (_IO_sputbackc
)
682 _IO_sungetc (_IO_FILE
*fp
)
686 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
)
689 result
= (unsigned char) *fp
->_IO_read_ptr
;
692 result
= _IO_PBACKFAIL (fp
, EOF
);
695 fp
->_flags
&= ~_IO_EOF_SEEN
;
700 #if 0 /* Work in progress */
701 /* Seems not to be needed. */
704 _IO_set_column (_IO_FILE
*fp
, int c
)
709 fp
->_column
= c
- (fp
->_IO_write_ptr
- fp
->_IO_write_base
);
713 _IO_set_column (_IO_FILE
*fp
, int i
)
715 fp
->_cur_column
= i
+ 1;
723 _IO_adjust_column (unsigned start
, const char *line
, int count
)
725 const char *ptr
= line
+ count
;
728 return line
+ count
- ptr
- 1;
729 return start
+ count
;
731 libc_hidden_def (_IO_adjust_column
)
734 /* Seems not to be needed. --drepper */
736 _IO_get_column (_IO_FILE
*fp
)
739 return _IO_adjust_column (fp
->_cur_column
- 1,
741 fp
->_IO_write_ptr
- fp
->_IO_write_base
);
748 _IO_flush_all_lockp (int do_lock
)
754 _IO_cleanup_region_start_noarg (flush_cleanup
);
755 _IO_lock_lock (list_all_lock
);
758 for (fp
= (_IO_FILE
*) _IO_list_all
; fp
!= NULL
; fp
= fp
->_chain
)
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
)
773 _IO_funlockfile (fp
);
778 _IO_lock_unlock (list_all_lock
);
779 _IO_cleanup_region_end (0);
789 /* We want locking. */
790 return _IO_flush_all_lockp (1);
792 libc_hidden_def (_IO_flush_all
)
795 _IO_flush_all_linebuffered (void)
800 _IO_cleanup_region_start_noarg (flush_cleanup
);
801 _IO_lock_lock (list_all_lock
);
804 for (fp
= (_IO_FILE
*) _IO_list_all
; fp
!= NULL
; fp
= fp
->_chain
)
809 if ((fp
->_flags
& _IO_NO_WRITES
) == 0 && fp
->_flags
& _IO_LINE_BUF
)
810 _IO_OVERFLOW (fp
, EOF
);
812 _IO_funlockfile (fp
);
817 _IO_lock_unlock (list_all_lock
);
818 _IO_cleanup_region_end (0);
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
;
844 _IO_unbuffer_all (void)
849 _IO_cleanup_region_start_noarg (flush_cleanup
);
850 _IO_lock_lock (list_all_lock
);
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. */
862 for (cnt
= 0; cnt
< MAXTRIES
; ++cnt
)
863 if (fp
->_lock
== NULL
|| _IO_lock_trylock (*fp
->_lock
) == 0)
866 /* Give the other thread time to finish up its use of the
871 if (! dealloc_buffers
&& !(fp
->_flags
& _IO_USER_BUF
))
873 fp
->_flags
|= _IO_USER_BUF
;
875 fp
->_freeres_list
= freeres_list
;
877 fp
->_freeres_buf
= fp
->_IO_buf_base
;
880 _IO_SETBUF (fp
, NULL
, 0);
883 _IO_wsetb (fp
, NULL
, NULL
, 0);
886 if (cnt
< MAXTRIES
&& fp
->_lock
!= NULL
)
887 _IO_lock_unlock (*fp
->_lock
);
891 /* Make sure that never again the wide char functions can be
897 _IO_lock_unlock (list_all_lock
);
898 _IO_cleanup_region_end (0);
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
;
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. */
937 _IO_init_marker (struct _IO_marker
*marker
, _IO_FILE
*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
;
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
;
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
)
961 else if (*ptr
== marker
)
963 *ptr
= marker
->_next
;
968 if _sbuf has a backup area that is no longer needed
, should we
delete
969 it now
, or wait until the next underflow
?
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
)
986 if (mark
->_sbuf
== NULL
)
988 if (_IO_in_backup (mark
->_sbuf
))
989 cur_pos
= mark
->_sbuf
->_IO_read_ptr
- mark
->_sbuf
->_IO_read_end
;
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
)
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
;
1008 if (!_IO_in_backup (fp
))
1009 _IO_switch_to_backup_area (fp
);
1010 fp
->_IO_read_ptr
= fp
->_IO_read_end
+ mark
->_pos
;
1016 _IO_unsave_markers (_IO_FILE
*fp
)
1018 struct _IO_marker
*mark
= fp
->_markers
;
1022 streampos offset
= seekoff (0, ios::cur
, ios::in
);
1025 offset
+= eGptr () - Gbase ();
1026 for ( ; mark
!= NULL
; mark
= mark
->_next
)
1027 mark
->set_streampos (mark
->_pos
+ offset
);
1031 for ( ; mark
!= NULL
; mark
= mark
->_next
)
1032 mark
->set_streampos (EOF
);
1038 if (_IO_have_backup (fp
))
1039 _IO_free_backup_area (fp
);
1041 libc_hidden_def (_IO_unsave_markers
)
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
)
1050 if (c
!= EOF
&& *fp
->_IO_read_ptr
!= c
)
1051 *fp
->_IO_read_ptr
= c
;
1052 return (unsigned char) c
;
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
)
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
))
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
);
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
;
1095 new_size
= 2 * old_size
;
1096 new_buf
= (char *) malloc (new_size
);
1097 if (new_buf
== NULL
)
1099 memcpy (new_buf
+ (new_size
- old_size
), fp
->_IO_read_base
,
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
)
1114 _IO_default_seek (_IO_FILE
*fp
, _IO_off64_t offset
, int dir
)
1120 _IO_default_stat (_IO_FILE
*fp
, void *st
)
1126 _IO_default_read (_IO_FILE
*fp
, void *data
, _IO_ssize_t n
)
1132 _IO_default_write (_IO_FILE
*fp
, const void *data
, _IO_ssize_t n
)
1138 _IO_default_showmanyc (_IO_FILE
*fp
)
1144 _IO_default_imbue (_IO_FILE
*fp
, void *locale
)
1149 _IO_iter_begin (void)
1151 return (_IO_ITER
) _IO_list_all
;
1153 libc_hidden_def (_IO_iter_begin
)
1160 libc_hidden_def (_IO_iter_end
)
1163 _IO_iter_next (_IO_ITER iter
)
1165 return iter
->_chain
;
1167 libc_hidden_def (_IO_iter_next
)
1170 _IO_iter_file (_IO_ITER iter
)
1174 libc_hidden_def (_IO_iter_file
)
1177 _IO_list_lock (void)
1179 #ifdef _IO_MTSAFE_IO
1180 _IO_lock_lock (list_all_lock
);
1183 libc_hidden_def (_IO_list_lock
)
1186 _IO_list_unlock (void)
1188 #ifdef _IO_MTSAFE_IO
1189 _IO_lock_unlock (list_all_lock
);
1192 libc_hidden_def (_IO_list_unlock
)
1195 _IO_list_resetlock (void)
1197 #ifdef _IO_MTSAFE_IO
1198 _IO_lock_init (list_all_lock
);
1201 libc_hidden_def (_IO_list_resetlock
)
1206 #define IO_CLEANUP ;
1214 ~__io_defs() { _IO_cleanup (); }
1216 __io_defs io_defs__
;
1221 #ifdef text_set_element
1222 text_set_element(__libc_atexit
, _IO_cleanup
);