1 /* Copyright (C) 1993-2015 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. */
38 static _IO_lock_t list_all_lock
= _IO_lock_initializer
;
41 /* Used to signal modifications to the list of FILE decriptors. */
42 static int _IO_list_all_stamp
;
45 static _IO_FILE
*run_fp
;
49 flush_cleanup (void *not_used
)
52 _IO_funlockfile (run_fp
);
53 _IO_lock_unlock (list_all_lock
);
59 struct _IO_FILE_plus
*fp
;
61 if (fp
->file
._flags
& _IO_LINKED
)
65 _IO_cleanup_region_start_noarg (flush_cleanup
);
66 _IO_lock_lock (list_all_lock
);
67 run_fp
= (_IO_FILE
*) fp
;
68 _IO_flockfile ((_IO_FILE
*) fp
);
70 if (_IO_list_all
== NULL
)
72 else if (fp
== _IO_list_all
)
74 _IO_list_all
= (struct _IO_FILE_plus
*) _IO_list_all
->file
._chain
;
78 for (f
= &_IO_list_all
->file
._chain
; *f
; f
= &(*f
)->_chain
)
79 if (*f
== (_IO_FILE
*) fp
)
85 fp
->file
._flags
&= ~_IO_LINKED
;
87 _IO_funlockfile ((_IO_FILE
*) fp
);
89 _IO_lock_unlock (list_all_lock
);
90 _IO_cleanup_region_end (0);
94 libc_hidden_def (_IO_un_link
)
98 struct _IO_FILE_plus
*fp
;
100 if ((fp
->file
._flags
& _IO_LINKED
) == 0)
102 fp
->file
._flags
|= _IO_LINKED
;
104 _IO_cleanup_region_start_noarg (flush_cleanup
);
105 _IO_lock_lock (list_all_lock
);
106 run_fp
= (_IO_FILE
*) fp
;
107 _IO_flockfile ((_IO_FILE
*) fp
);
109 fp
->file
._chain
= (_IO_FILE
*) _IO_list_all
;
111 ++_IO_list_all_stamp
;
113 _IO_funlockfile ((_IO_FILE
*) fp
);
115 _IO_lock_unlock (list_all_lock
);
116 _IO_cleanup_region_end (0);
120 libc_hidden_def (_IO_link_in
)
122 /* Return minimum _pos markers
123 Assumes the current get area is the main get area. */
124 _IO_ssize_t
_IO_least_marker (_IO_FILE
*fp
, char *end_p
);
127 _IO_least_marker (fp
, end_p
)
131 _IO_ssize_t least_so_far
= end_p
- fp
->_IO_read_base
;
132 struct _IO_marker
*mark
;
133 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
134 if (mark
->_pos
< least_so_far
)
135 least_so_far
= mark
->_pos
;
139 /* Switch current get area from backup buffer to (start of) main get area. */
142 _IO_switch_to_main_get_area (fp
)
146 fp
->_flags
&= ~_IO_IN_BACKUP
;
147 /* Swap _IO_read_end and _IO_save_end. */
148 tmp
= fp
->_IO_read_end
;
149 fp
->_IO_read_end
= fp
->_IO_save_end
;
150 fp
->_IO_save_end
= tmp
;
151 /* Swap _IO_read_base and _IO_save_base. */
152 tmp
= fp
->_IO_read_base
;
153 fp
->_IO_read_base
= fp
->_IO_save_base
;
154 fp
->_IO_save_base
= tmp
;
155 /* Set _IO_read_ptr. */
156 fp
->_IO_read_ptr
= fp
->_IO_read_base
;
159 /* Switch current get area from main get area to (end of) backup area. */
162 _IO_switch_to_backup_area (fp
)
166 fp
->_flags
|= _IO_IN_BACKUP
;
167 /* Swap _IO_read_end and _IO_save_end. */
168 tmp
= fp
->_IO_read_end
;
169 fp
->_IO_read_end
= fp
->_IO_save_end
;
170 fp
->_IO_save_end
= tmp
;
171 /* Swap _IO_read_base and _IO_save_base. */
172 tmp
= fp
->_IO_read_base
;
173 fp
->_IO_read_base
= fp
->_IO_save_base
;
174 fp
->_IO_save_base
= tmp
;
175 /* Set _IO_read_ptr. */
176 fp
->_IO_read_ptr
= fp
->_IO_read_end
;
180 _IO_switch_to_get_mode (fp
)
183 if (fp
->_IO_write_ptr
> fp
->_IO_write_base
)
184 if (_IO_OVERFLOW (fp
, EOF
) == EOF
)
186 if (_IO_in_backup (fp
))
187 fp
->_IO_read_base
= fp
->_IO_backup_base
;
190 fp
->_IO_read_base
= fp
->_IO_buf_base
;
191 if (fp
->_IO_write_ptr
> fp
->_IO_read_end
)
192 fp
->_IO_read_end
= fp
->_IO_write_ptr
;
194 fp
->_IO_read_ptr
= fp
->_IO_write_ptr
;
196 fp
->_IO_write_base
= fp
->_IO_write_ptr
= fp
->_IO_write_end
= fp
->_IO_read_ptr
;
198 fp
->_flags
&= ~_IO_CURRENTLY_PUTTING
;
201 libc_hidden_def (_IO_switch_to_get_mode
)
204 _IO_free_backup_area (fp
)
207 if (_IO_in_backup (fp
))
208 _IO_switch_to_main_get_area (fp
); /* Just in case. */
209 free (fp
->_IO_save_base
);
210 fp
->_IO_save_base
= NULL
;
211 fp
->_IO_save_end
= NULL
;
212 fp
->_IO_backup_base
= NULL
;
214 libc_hidden_def (_IO_free_backup_area
)
218 _IO_switch_to_put_mode (fp
)
221 fp
->_IO_write_base
= fp
->_IO_read_ptr
;
222 fp
->_IO_write_ptr
= fp
->_IO_read_ptr
;
223 /* Following is wrong if line- or un-buffered? */
224 fp
->_IO_write_end
= (fp
->_flags
& _IO_IN_BACKUP
225 ? fp
->_IO_read_end
: fp
->_IO_buf_end
);
227 fp
->_IO_read_ptr
= fp
->_IO_read_end
;
228 fp
->_IO_read_base
= fp
->_IO_read_end
;
230 fp
->_flags
|= _IO_CURRENTLY_PUTTING
;
240 /* This is a single-byte stream. */
243 return _IO_OVERFLOW (f
, ch
);
245 libc_hidden_def (__overflow
)
247 static int save_for_backup (_IO_FILE
*fp
, char *end_p
)
257 save_for_backup (fp
, end_p
)
261 /* Append [_IO_read_base..end_p] to backup area. */
262 _IO_ssize_t least_mark
= _IO_least_marker (fp
, end_p
);
263 /* needed_size is how much space we need in the backup area. */
264 _IO_size_t needed_size
= (end_p
- fp
->_IO_read_base
) - least_mark
;
265 /* FIXME: Dubious arithmetic if pointers are NULL */
266 _IO_size_t current_Bsize
= fp
->_IO_save_end
- fp
->_IO_save_base
;
267 _IO_size_t avail
; /* Extra space available for future expansion. */
269 struct _IO_marker
*mark
;
270 if (needed_size
> current_Bsize
)
274 new_buffer
= (char *) malloc (avail
+ needed_size
);
275 if (new_buffer
== NULL
)
276 return EOF
; /* FIXME */
280 __mempcpy (__mempcpy (new_buffer
+ avail
,
281 fp
->_IO_save_end
+ least_mark
,
284 end_p
- fp
->_IO_read_base
);
286 memcpy (new_buffer
+ avail
,
287 fp
->_IO_save_end
+ least_mark
,
289 memcpy (new_buffer
+ avail
- least_mark
,
291 end_p
- fp
->_IO_read_base
);
295 memcpy (new_buffer
+ avail
,
296 fp
->_IO_read_base
+ least_mark
,
298 free (fp
->_IO_save_base
);
299 fp
->_IO_save_base
= new_buffer
;
300 fp
->_IO_save_end
= new_buffer
+ avail
+ needed_size
;
304 avail
= current_Bsize
- needed_size
;
307 memmove (fp
->_IO_save_base
+ avail
,
308 fp
->_IO_save_end
+ least_mark
,
310 memcpy (fp
->_IO_save_base
+ avail
- least_mark
,
312 end_p
- fp
->_IO_read_base
);
314 else if (needed_size
> 0)
315 memcpy (fp
->_IO_save_base
+ avail
,
316 fp
->_IO_read_base
+ least_mark
,
319 fp
->_IO_backup_base
= fp
->_IO_save_base
+ avail
;
320 /* Adjust all the streammarkers. */
321 delta
= end_p
- fp
->_IO_read_base
;
322 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
331 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
332 if (_IO_vtable_offset (fp
) == 0 && _IO_fwide (fp
, -1) != -1)
338 if (_IO_in_put_mode (fp
))
339 if (_IO_switch_to_get_mode (fp
) == EOF
)
341 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
342 return *(unsigned char *) fp
->_IO_read_ptr
;
343 if (_IO_in_backup (fp
))
345 _IO_switch_to_main_get_area (fp
);
346 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
347 return *(unsigned char *) fp
->_IO_read_ptr
;
349 if (_IO_have_markers (fp
))
351 if (save_for_backup (fp
, fp
->_IO_read_end
))
354 else if (_IO_have_backup (fp
))
355 _IO_free_backup_area (fp
);
356 return _IO_UNDERFLOW (fp
);
358 libc_hidden_def (__underflow
)
364 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
365 if (_IO_vtable_offset (fp
) == 0 && _IO_fwide (fp
, -1) != -1)
371 if (_IO_in_put_mode (fp
))
372 if (_IO_switch_to_get_mode (fp
) == EOF
)
374 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
375 return *(unsigned char *) fp
->_IO_read_ptr
++;
376 if (_IO_in_backup (fp
))
378 _IO_switch_to_main_get_area (fp
);
379 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
380 return *(unsigned char *) fp
->_IO_read_ptr
++;
382 if (_IO_have_markers (fp
))
384 if (save_for_backup (fp
, fp
->_IO_read_end
))
387 else if (_IO_have_backup (fp
))
388 _IO_free_backup_area (fp
);
389 return _IO_UFLOW (fp
);
391 libc_hidden_def (__uflow
)
394 _IO_setb (f
, b
, eb
, a
)
400 if (f
->_IO_buf_base
&& !(f
->_flags
& _IO_USER_BUF
))
401 free (f
->_IO_buf_base
);
405 f
->_flags
&= ~_IO_USER_BUF
;
407 f
->_flags
|= _IO_USER_BUF
;
409 libc_hidden_def (_IO_setb
)
415 if (fp
->_IO_buf_base
)
417 if (!(fp
->_flags
& _IO_UNBUFFERED
) || fp
->_mode
> 0)
418 if (_IO_DOALLOCATE (fp
) != EOF
)
420 _IO_setb (fp
, fp
->_shortbuf
, fp
->_shortbuf
+1, 0);
422 libc_hidden_def (_IO_doallocbuf
)
425 _IO_default_underflow (fp
)
432 _IO_default_uflow (fp
)
435 int ch
= _IO_UNDERFLOW (fp
);
438 return *(unsigned char *) fp
->_IO_read_ptr
++;
440 libc_hidden_def (_IO_default_uflow
)
443 _IO_default_xsputn (f
, data
, n
)
448 const char *s
= (char *) data
;
454 /* Space available. */
455 if (f
->_IO_write_ptr
< f
->_IO_write_end
)
457 _IO_size_t count
= f
->_IO_write_end
- f
->_IO_write_ptr
;
463 f
->_IO_write_ptr
= __mempcpy (f
->_IO_write_ptr
, s
, count
);
465 memcpy (f
->_IO_write_ptr
, s
, count
);
466 f
->_IO_write_ptr
+= count
;
472 char *p
= f
->_IO_write_ptr
;
474 for (i
= count
; --i
>= 0; )
476 f
->_IO_write_ptr
= p
;
480 if (more
== 0 || _IO_OVERFLOW (f
, (unsigned char) *s
++) == EOF
)
486 libc_hidden_def (_IO_default_xsputn
)
489 _IO_sgetn (fp
, data
, n
)
494 /* FIXME handle putback buffer here! */
495 return _IO_XSGETN (fp
, data
, n
);
497 libc_hidden_def (_IO_sgetn
)
500 _IO_default_xsgetn (fp
, data
, n
)
506 char *s
= (char*) data
;
509 /* Data available. */
510 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
512 _IO_size_t count
= fp
->_IO_read_end
- fp
->_IO_read_ptr
;
518 s
= __mempcpy (s
, fp
->_IO_read_ptr
, count
);
520 memcpy (s
, fp
->_IO_read_ptr
, count
);
523 fp
->_IO_read_ptr
+= count
;
527 char *p
= fp
->_IO_read_ptr
;
531 fp
->_IO_read_ptr
= p
;
535 if (more
== 0 || __underflow (fp
) == EOF
)
540 libc_hidden_def (_IO_default_xsgetn
)
543 /* Seems not to be needed. --drepper */
553 _IO_default_setbuf (fp
, p
, len
)
558 if (_IO_SYNC (fp
) == EOF
)
560 if (p
== NULL
|| len
== 0)
562 fp
->_flags
|= _IO_UNBUFFERED
;
563 _IO_setb (fp
, fp
->_shortbuf
, fp
->_shortbuf
+1, 0);
567 fp
->_flags
&= ~_IO_UNBUFFERED
;
568 _IO_setb (fp
, p
, p
+len
, 0);
570 fp
->_IO_write_base
= fp
->_IO_write_ptr
= fp
->_IO_write_end
= 0;
571 fp
->_IO_read_base
= fp
->_IO_read_ptr
= fp
->_IO_read_end
= 0;
576 _IO_default_seekpos (fp
, pos
, mode
)
581 return _IO_SEEKOFF (fp
, pos
, 0, mode
);
585 _IO_default_doallocate (fp
)
590 buf
= malloc(_IO_BUFSIZ
);
591 if (__glibc_unlikely (buf
== NULL
))
594 _IO_setb (fp
, buf
, buf
+_IO_BUFSIZ
, 1);
597 libc_hidden_def (_IO_default_doallocate
)
604 _IO_no_init (fp
, flags
, -1, NULL
, NULL
);
606 libc_hidden_def (_IO_init
)
609 _IO_old_init (fp
, flags
)
613 fp
->_flags
= _IO_MAGIC
|flags
;
615 fp
->_IO_buf_base
= NULL
;
616 fp
->_IO_buf_end
= NULL
;
617 fp
->_IO_read_base
= NULL
;
618 fp
->_IO_read_ptr
= NULL
;
619 fp
->_IO_read_end
= NULL
;
620 fp
->_IO_write_base
= NULL
;
621 fp
->_IO_write_ptr
= NULL
;
622 fp
->_IO_write_end
= NULL
;
623 fp
->_chain
= NULL
; /* Not necessary. */
625 fp
->_IO_save_base
= NULL
;
626 fp
->_IO_backup_base
= NULL
;
627 fp
->_IO_save_end
= NULL
;
631 fp
->_vtable_offset
= 0;
634 if (fp
->_lock
!= NULL
)
635 _IO_lock_init (*fp
->_lock
);
640 _IO_no_init (fp
, flags
, orientation
, wd
, jmp
)
644 struct _IO_wide_data
*wd
;
645 const struct _IO_jump_t
*jmp
;
647 _IO_old_init (fp
, flags
);
648 fp
->_mode
= orientation
;
649 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
650 if (orientation
>= 0)
653 fp
->_wide_data
->_IO_buf_base
= NULL
;
654 fp
->_wide_data
->_IO_buf_end
= NULL
;
655 fp
->_wide_data
->_IO_read_base
= NULL
;
656 fp
->_wide_data
->_IO_read_ptr
= NULL
;
657 fp
->_wide_data
->_IO_read_end
= NULL
;
658 fp
->_wide_data
->_IO_write_base
= NULL
;
659 fp
->_wide_data
->_IO_write_ptr
= NULL
;
660 fp
->_wide_data
->_IO_write_end
= NULL
;
661 fp
->_wide_data
->_IO_save_base
= NULL
;
662 fp
->_wide_data
->_IO_backup_base
= NULL
;
663 fp
->_wide_data
->_IO_save_end
= NULL
;
665 fp
->_wide_data
->_wide_vtable
= jmp
;
668 /* Cause predictable crash when a wide function is called on a byte
670 fp
->_wide_data
= (struct _IO_wide_data
*) -1L;
672 fp
->_freeres_list
= NULL
;
676 _IO_default_sync (fp
)
682 /* The way the C++ classes are mapped into the C functions in the
683 current implementation, this function can get called twice! */
686 _IO_default_finish (fp
, dummy
)
690 struct _IO_marker
*mark
;
691 if (fp
->_IO_buf_base
&& !(fp
->_flags
& _IO_USER_BUF
))
693 free (fp
->_IO_buf_base
);
694 fp
->_IO_buf_base
= fp
->_IO_buf_end
= NULL
;
697 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
700 if (fp
->_IO_save_base
)
702 free (fp
->_IO_save_base
);
703 fp
->_IO_save_base
= NULL
;
706 _IO_un_link ((struct _IO_FILE_plus
*) fp
);
709 if (fp
->_lock
!= NULL
)
710 _IO_lock_fini (*fp
->_lock
);
713 libc_hidden_def (_IO_default_finish
)
716 _IO_default_seekoff (fp
, offset
, dir
, mode
)
726 _IO_sputbackc (fp
, c
)
732 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
733 && (unsigned char)fp
->_IO_read_ptr
[-1] == (unsigned char)c
)
736 result
= (unsigned char) c
;
739 result
= _IO_PBACKFAIL (fp
, c
);
742 fp
->_flags
&= ~_IO_EOF_SEEN
;
746 libc_hidden_def (_IO_sputbackc
)
754 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
)
757 result
= (unsigned char) *fp
->_IO_read_ptr
;
760 result
= _IO_PBACKFAIL (fp
, EOF
);
763 fp
->_flags
&= ~_IO_EOF_SEEN
;
768 #if 0 /* Work in progress */
769 /* Seems not to be needed. */
772 _IO_set_column (fp
, c
)
779 fp
->_column
= c
- (fp
->_IO_write_ptr
- fp
->_IO_write_base
);
783 _IO_set_column (fp
, i
)
787 fp
->_cur_column
= i
+ 1;
795 _IO_adjust_column (start
, line
, count
)
800 const char *ptr
= line
+ count
;
803 return line
+ count
- ptr
- 1;
804 return start
+ count
;
806 libc_hidden_def (_IO_adjust_column
)
809 /* Seems not to be needed. --drepper */
815 return _IO_adjust_column (fp
->_cur_column
- 1,
817 fp
->_IO_write_ptr
- fp
->_IO_write_base
);
824 _IO_flush_all_lockp (int do_lock
)
831 __libc_cleanup_region_start (do_lock
, flush_cleanup
, NULL
);
833 _IO_lock_lock (list_all_lock
);
836 last_stamp
= _IO_list_all_stamp
;
837 fp
= (_IO_FILE
*) _IO_list_all
;
844 if (((fp
->_mode
<= 0 && fp
->_IO_write_ptr
> fp
->_IO_write_base
)
845 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
846 || (_IO_vtable_offset (fp
) == 0
847 && fp
->_mode
> 0 && (fp
->_wide_data
->_IO_write_ptr
848 > fp
->_wide_data
->_IO_write_base
))
851 && _IO_OVERFLOW (fp
, EOF
) == EOF
)
855 _IO_funlockfile (fp
);
858 if (last_stamp
!= _IO_list_all_stamp
)
860 /* Something was added to the list. Start all over again. */
861 fp
= (_IO_FILE
*) _IO_list_all
;
862 last_stamp
= _IO_list_all_stamp
;
870 _IO_lock_unlock (list_all_lock
);
871 __libc_cleanup_region_end (0);
881 /* We want locking. */
882 return _IO_flush_all_lockp (1);
884 libc_hidden_def (_IO_flush_all
)
887 _IO_flush_all_linebuffered (void)
893 _IO_cleanup_region_start_noarg (flush_cleanup
);
894 _IO_lock_lock (list_all_lock
);
897 last_stamp
= _IO_list_all_stamp
;
898 fp
= (_IO_FILE
*) _IO_list_all
;
904 if ((fp
->_flags
& _IO_NO_WRITES
) == 0 && fp
->_flags
& _IO_LINE_BUF
)
905 _IO_OVERFLOW (fp
, EOF
);
907 _IO_funlockfile (fp
);
910 if (last_stamp
!= _IO_list_all_stamp
)
912 /* Something was added to the list. Start all over again. */
913 fp
= (_IO_FILE
*) _IO_list_all
;
914 last_stamp
= _IO_list_all_stamp
;
921 _IO_lock_unlock (list_all_lock
);
922 _IO_cleanup_region_end (0);
925 libc_hidden_def (_IO_flush_all_linebuffered
)
927 weak_alias (_IO_flush_all_linebuffered
, _flushlbf
)
931 /* The following is a bit tricky. In general, we want to unbuffer the
932 streams so that all output which follows is seen. If we are not
933 looking for memory leaks it does not make much sense to free the
934 actual buffer because this will happen anyway once the program
935 terminated. If we do want to look for memory leaks we have to free
936 the buffers. Whether something is freed is determined by the
937 function sin the libc_freeres section. Those are called as part of
938 the atexit routine, just like _IO_cleanup. The problem is we do
939 not know whether the freeres code is called first or _IO_cleanup.
940 if the former is the case, we set the DEALLOC_BUFFER variable to
941 true and _IO_unbuffer_all will take care of the rest. If
942 _IO_unbuffer_all is called first we add the streams to a list
943 which the freeres function later can walk through. */
944 static void _IO_unbuffer_all (void);
946 static bool dealloc_buffers
;
947 static _IO_FILE
*freeres_list
;
950 _IO_unbuffer_all (void)
953 for (fp
= (_IO_FILE
*) _IO_list_all
; fp
; fp
= fp
->_chain
)
955 if (! (fp
->_flags
& _IO_UNBUFFERED
)
956 /* Iff stream is un-orientated, it wasn't used. */
962 for (cnt
= 0; cnt
< MAXTRIES
; ++cnt
)
963 if (fp
->_lock
== NULL
|| _IO_lock_trylock (*fp
->_lock
) == 0)
966 /* Give the other thread time to finish up its use of the
971 if (! dealloc_buffers
&& !(fp
->_flags
& _IO_USER_BUF
))
973 fp
->_flags
|= _IO_USER_BUF
;
975 fp
->_freeres_list
= freeres_list
;
977 fp
->_freeres_buf
= fp
->_IO_buf_base
;
980 _IO_SETBUF (fp
, NULL
, 0);
983 _IO_wsetb (fp
, NULL
, NULL
, 0);
986 if (cnt
< MAXTRIES
&& fp
->_lock
!= NULL
)
987 _IO_lock_unlock (*fp
->_lock
);
991 /* Make sure that never again the wide char functions can be
998 libc_freeres_fn (buffer_free
)
1000 dealloc_buffers
= true;
1002 while (freeres_list
!= NULL
)
1004 free (freeres_list
->_freeres_buf
);
1006 freeres_list
= freeres_list
->_freeres_list
;
1014 /* We do *not* want locking. Some threads might use streams but
1015 that is their problem, we flush them underneath them. */
1016 int result
= _IO_flush_all_lockp (0);
1018 /* We currently don't have a reliable mechanism for making sure that
1019 C++ static destructors are executed in the correct order.
1020 So it is possible that other static destructors might want to
1021 write to cout - and they're supposed to be able to do so.
1023 The following will make the standard streambufs be unbuffered,
1024 which forces any output from late destructors to be written out. */
1025 _IO_unbuffer_all ();
1032 _IO_init_marker (marker
, fp
)
1033 struct _IO_marker
*marker
;
1037 if (_IO_in_put_mode (fp
))
1038 _IO_switch_to_get_mode (fp
);
1039 if (_IO_in_backup (fp
))
1040 marker
->_pos
= fp
->_IO_read_ptr
- fp
->_IO_read_end
;
1042 marker
->_pos
= fp
->_IO_read_ptr
- fp
->_IO_read_base
;
1044 /* Should perhaps sort the chain? */
1045 marker
->_next
= fp
->_markers
;
1046 fp
->_markers
= marker
;
1050 _IO_remove_marker (marker
)
1051 struct _IO_marker
*marker
;
1053 /* Unlink from sb's chain. */
1054 struct _IO_marker
**ptr
= &marker
->_sbuf
->_markers
;
1055 for (; ; ptr
= &(*ptr
)->_next
)
1059 else if (*ptr
== marker
)
1061 *ptr
= marker
->_next
;
1066 if _sbuf has a backup area that is no longer needed
, should we
delete
1067 it now
, or wait until the next underflow
?
1071 #define BAD_DELTA EOF
1074 _IO_marker_difference (mark1
, mark2
)
1075 struct _IO_marker
*mark1
;
1076 struct _IO_marker
*mark2
;
1078 return mark1
->_pos
- mark2
->_pos
;
1081 /* Return difference between MARK and current position of MARK's stream. */
1083 _IO_marker_delta (mark
)
1084 struct _IO_marker
*mark
;
1087 if (mark
->_sbuf
== NULL
)
1089 if (_IO_in_backup (mark
->_sbuf
))
1090 cur_pos
= mark
->_sbuf
->_IO_read_ptr
- mark
->_sbuf
->_IO_read_end
;
1092 cur_pos
= mark
->_sbuf
->_IO_read_ptr
- mark
->_sbuf
->_IO_read_base
;
1093 return mark
->_pos
- cur_pos
;
1097 _IO_seekmark (fp
, mark
, delta
)
1099 struct _IO_marker
*mark
;
1102 if (mark
->_sbuf
!= fp
)
1104 if (mark
->_pos
>= 0)
1106 if (_IO_in_backup (fp
))
1107 _IO_switch_to_main_get_area (fp
);
1108 fp
->_IO_read_ptr
= fp
->_IO_read_base
+ mark
->_pos
;
1112 if (!_IO_in_backup (fp
))
1113 _IO_switch_to_backup_area (fp
);
1114 fp
->_IO_read_ptr
= fp
->_IO_read_end
+ mark
->_pos
;
1120 _IO_unsave_markers (fp
)
1123 struct _IO_marker
*mark
= fp
->_markers
;
1127 streampos offset
= seekoff (0, ios::cur
, ios::in
);
1130 offset
+= eGptr () - Gbase ();
1131 for ( ; mark
!= NULL
; mark
= mark
->_next
)
1132 mark
->set_streampos (mark
->_pos
+ offset
);
1136 for ( ; mark
!= NULL
; mark
= mark
->_next
)
1137 mark
->set_streampos (EOF
);
1143 if (_IO_have_backup (fp
))
1144 _IO_free_backup_area (fp
);
1146 libc_hidden_def (_IO_unsave_markers
)
1149 /* Seems not to be needed. --drepper */
1151 _IO_nobackup_pbackfail (fp
, c
)
1155 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
)
1157 if (c
!= EOF
&& *fp
->_IO_read_ptr
!= c
)
1158 *fp
->_IO_read_ptr
= c
;
1159 return (unsigned char) c
;
1164 _IO_default_pbackfail (fp
, c
)
1168 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
&& !_IO_in_backup (fp
)
1169 && (unsigned char) fp
->_IO_read_ptr
[-1] == c
)
1173 /* Need to handle a filebuf in write mode (switch to read mode). FIXME!*/
1174 if (!_IO_in_backup (fp
))
1176 /* We need to keep the invariant that the main get area
1177 logically follows the backup area. */
1178 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
&& _IO_have_backup (fp
))
1180 if (save_for_backup (fp
, fp
->_IO_read_ptr
))
1183 else if (!_IO_have_backup (fp
))
1185 /* No backup buffer: allocate one. */
1186 /* Use nshort buffer, if unused? (probably not) FIXME */
1187 int backup_size
= 128;
1188 char *bbuf
= (char *) malloc (backup_size
);
1191 fp
->_IO_save_base
= bbuf
;
1192 fp
->_IO_save_end
= fp
->_IO_save_base
+ backup_size
;
1193 fp
->_IO_backup_base
= fp
->_IO_save_end
;
1195 fp
->_IO_read_base
= fp
->_IO_read_ptr
;
1196 _IO_switch_to_backup_area (fp
);
1198 else if (fp
->_IO_read_ptr
<= fp
->_IO_read_base
)
1200 /* Increase size of existing backup buffer. */
1201 _IO_size_t new_size
;
1202 _IO_size_t old_size
= fp
->_IO_read_end
- fp
->_IO_read_base
;
1204 new_size
= 2 * old_size
;
1205 new_buf
= (char *) malloc (new_size
);
1206 if (new_buf
== NULL
)
1208 memcpy (new_buf
+ (new_size
- old_size
), fp
->_IO_read_base
,
1210 free (fp
->_IO_read_base
);
1211 _IO_setg (fp
, new_buf
, new_buf
+ (new_size
- old_size
),
1212 new_buf
+ new_size
);
1213 fp
->_IO_backup_base
= fp
->_IO_read_ptr
;
1216 *--fp
->_IO_read_ptr
= c
;
1218 return (unsigned char) c
;
1220 libc_hidden_def (_IO_default_pbackfail
)
1223 _IO_default_seek (fp
, offset
, dir
)
1232 _IO_default_stat (fp
, st
)
1240 _IO_default_read (fp
, data
, n
)
1249 _IO_default_write (fp
, data
, n
)
1258 _IO_default_showmanyc (fp
)
1265 _IO_default_imbue (fp
, locale
)
1272 _IO_iter_begin (void)
1274 return (_IO_ITER
) _IO_list_all
;
1276 libc_hidden_def (_IO_iter_begin
)
1283 libc_hidden_def (_IO_iter_end
)
1289 return iter
->_chain
;
1291 libc_hidden_def (_IO_iter_next
)
1299 libc_hidden_def (_IO_iter_file
)
1302 _IO_list_lock (void)
1304 #ifdef _IO_MTSAFE_IO
1305 _IO_lock_lock (list_all_lock
);
1308 libc_hidden_def (_IO_list_lock
)
1311 _IO_list_unlock (void)
1313 #ifdef _IO_MTSAFE_IO
1314 _IO_lock_unlock (list_all_lock
);
1317 libc_hidden_def (_IO_list_unlock
)
1320 _IO_list_resetlock (void)
1322 #ifdef _IO_MTSAFE_IO
1323 _IO_lock_init (list_all_lock
);
1326 libc_hidden_def (_IO_list_resetlock
)
1331 #define IO_CLEANUP ;
1339 ~__io_defs() { _IO_cleanup (); }
1341 __io_defs io_defs__
;
1346 #ifdef text_set_element
1347 text_set_element(__libc_atexit
, _IO_cleanup
);