1 /* Copyright (C) 1993,1995,1997-2002, 2003, 2004, 2006, 2007, 2011-2012
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>.
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. */
28 /* Generic or default I/O operations. */
39 static _IO_lock_t list_all_lock
= _IO_lock_initializer
;
42 /* Used to signal modifications to the list of FILE decriptors. */
43 static int _IO_list_all_stamp
;
46 static _IO_FILE
*run_fp
;
49 flush_cleanup (void *not_used
)
52 _IO_funlockfile (run_fp
);
54 _IO_lock_unlock (list_all_lock
);
60 struct _IO_FILE_plus
*fp
;
62 if (fp
->file
._flags
& _IO_LINKED
)
66 _IO_cleanup_region_start_noarg (flush_cleanup
);
67 _IO_lock_lock (list_all_lock
);
68 run_fp
= (_IO_FILE
*) fp
;
69 _IO_flockfile ((_IO_FILE
*) fp
);
71 if (_IO_list_all
== NULL
)
73 else if (fp
== _IO_list_all
)
75 _IO_list_all
= (struct _IO_FILE_plus
*) _IO_list_all
->file
._chain
;
79 for (f
= &_IO_list_all
->file
._chain
; *f
; f
= &(*f
)->_chain
)
80 if (*f
== (_IO_FILE
*) fp
)
86 fp
->file
._flags
&= ~_IO_LINKED
;
88 _IO_funlockfile ((_IO_FILE
*) fp
);
90 _IO_lock_unlock (list_all_lock
);
91 _IO_cleanup_region_end (0);
95 libc_hidden_def (_IO_un_link
)
99 struct _IO_FILE_plus
*fp
;
101 if ((fp
->file
._flags
& _IO_LINKED
) == 0)
103 fp
->file
._flags
|= _IO_LINKED
;
105 _IO_cleanup_region_start_noarg (flush_cleanup
);
106 _IO_lock_lock (list_all_lock
);
107 run_fp
= (_IO_FILE
*) fp
;
108 _IO_flockfile ((_IO_FILE
*) fp
);
110 fp
->file
._chain
= (_IO_FILE
*) _IO_list_all
;
112 ++_IO_list_all_stamp
;
114 _IO_funlockfile ((_IO_FILE
*) fp
);
116 _IO_lock_unlock (list_all_lock
);
117 _IO_cleanup_region_end (0);
121 libc_hidden_def (_IO_link_in
)
123 /* Return minimum _pos markers
124 Assumes the current get area is the main get area. */
125 _IO_ssize_t
_IO_least_marker (_IO_FILE
*fp
, char *end_p
);
128 _IO_least_marker (fp
, end_p
)
132 _IO_ssize_t least_so_far
= end_p
- fp
->_IO_read_base
;
133 struct _IO_marker
*mark
;
134 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
135 if (mark
->_pos
< least_so_far
)
136 least_so_far
= mark
->_pos
;
140 /* Switch current get area from backup buffer to (start of) main get area. */
143 _IO_switch_to_main_get_area (fp
)
147 fp
->_flags
&= ~_IO_IN_BACKUP
;
148 /* Swap _IO_read_end and _IO_save_end. */
149 tmp
= fp
->_IO_read_end
;
150 fp
->_IO_read_end
= fp
->_IO_save_end
;
151 fp
->_IO_save_end
= tmp
;
152 /* Swap _IO_read_base and _IO_save_base. */
153 tmp
= fp
->_IO_read_base
;
154 fp
->_IO_read_base
= fp
->_IO_save_base
;
155 fp
->_IO_save_base
= tmp
;
156 /* Set _IO_read_ptr. */
157 fp
->_IO_read_ptr
= fp
->_IO_read_base
;
160 /* Switch current get area from main get area to (end of) backup area. */
163 _IO_switch_to_backup_area (fp
)
167 fp
->_flags
|= _IO_IN_BACKUP
;
168 /* Swap _IO_read_end and _IO_save_end. */
169 tmp
= fp
->_IO_read_end
;
170 fp
->_IO_read_end
= fp
->_IO_save_end
;
171 fp
->_IO_save_end
= tmp
;
172 /* Swap _IO_read_base and _IO_save_base. */
173 tmp
= fp
->_IO_read_base
;
174 fp
->_IO_read_base
= fp
->_IO_save_base
;
175 fp
->_IO_save_base
= tmp
;
176 /* Set _IO_read_ptr. */
177 fp
->_IO_read_ptr
= fp
->_IO_read_end
;
181 _IO_switch_to_get_mode (fp
)
184 if (fp
->_IO_write_ptr
> fp
->_IO_write_base
)
185 if (_IO_OVERFLOW (fp
, EOF
) == EOF
)
187 if (_IO_in_backup (fp
))
188 fp
->_IO_read_base
= fp
->_IO_backup_base
;
191 fp
->_IO_read_base
= fp
->_IO_buf_base
;
192 if (fp
->_IO_write_ptr
> fp
->_IO_read_end
)
193 fp
->_IO_read_end
= fp
->_IO_write_ptr
;
195 fp
->_IO_read_ptr
= fp
->_IO_write_ptr
;
197 fp
->_IO_write_base
= fp
->_IO_write_ptr
= fp
->_IO_write_end
= fp
->_IO_read_ptr
;
199 fp
->_flags
&= ~_IO_CURRENTLY_PUTTING
;
202 libc_hidden_def (_IO_switch_to_get_mode
)
205 _IO_free_backup_area (fp
)
208 if (_IO_in_backup (fp
))
209 _IO_switch_to_main_get_area (fp
); /* Just in case. */
210 free (fp
->_IO_save_base
);
211 fp
->_IO_save_base
= NULL
;
212 fp
->_IO_save_end
= NULL
;
213 fp
->_IO_backup_base
= NULL
;
215 libc_hidden_def (_IO_free_backup_area
)
219 _IO_switch_to_put_mode (fp
)
222 fp
->_IO_write_base
= fp
->_IO_read_ptr
;
223 fp
->_IO_write_ptr
= fp
->_IO_read_ptr
;
224 /* Following is wrong if line- or un-buffered? */
225 fp
->_IO_write_end
= (fp
->_flags
& _IO_IN_BACKUP
226 ? fp
->_IO_read_end
: fp
->_IO_buf_end
);
228 fp
->_IO_read_ptr
= fp
->_IO_read_end
;
229 fp
->_IO_read_base
= fp
->_IO_read_end
;
231 fp
->_flags
|= _IO_CURRENTLY_PUTTING
;
241 /* This is a single-byte stream. */
244 return _IO_OVERFLOW (f
, ch
);
246 libc_hidden_def (__overflow
)
248 static int save_for_backup (_IO_FILE
*fp
, char *end_p
)
258 save_for_backup (fp
, end_p
)
262 /* Append [_IO_read_base..end_p] to backup area. */
263 _IO_ssize_t least_mark
= _IO_least_marker (fp
, end_p
);
264 /* needed_size is how much space we need in the backup area. */
265 _IO_size_t needed_size
= (end_p
- fp
->_IO_read_base
) - least_mark
;
266 /* FIXME: Dubious arithmetic if pointers are NULL */
267 _IO_size_t current_Bsize
= fp
->_IO_save_end
- fp
->_IO_save_base
;
268 _IO_size_t avail
; /* Extra space available for future expansion. */
270 struct _IO_marker
*mark
;
271 if (needed_size
> current_Bsize
)
275 new_buffer
= (char *) malloc (avail
+ needed_size
);
276 if (new_buffer
== NULL
)
277 return EOF
; /* FIXME */
281 __mempcpy (__mempcpy (new_buffer
+ avail
,
282 fp
->_IO_save_end
+ least_mark
,
285 end_p
- fp
->_IO_read_base
);
287 memcpy (new_buffer
+ avail
,
288 fp
->_IO_save_end
+ least_mark
,
290 memcpy (new_buffer
+ avail
- least_mark
,
292 end_p
- fp
->_IO_read_base
);
296 memcpy (new_buffer
+ avail
,
297 fp
->_IO_read_base
+ least_mark
,
299 free (fp
->_IO_save_base
);
300 fp
->_IO_save_base
= new_buffer
;
301 fp
->_IO_save_end
= new_buffer
+ avail
+ needed_size
;
305 avail
= current_Bsize
- needed_size
;
308 memmove (fp
->_IO_save_base
+ avail
,
309 fp
->_IO_save_end
+ least_mark
,
311 memcpy (fp
->_IO_save_base
+ avail
- least_mark
,
313 end_p
- fp
->_IO_read_base
);
315 else if (needed_size
> 0)
316 memcpy (fp
->_IO_save_base
+ avail
,
317 fp
->_IO_read_base
+ least_mark
,
320 fp
->_IO_backup_base
= fp
->_IO_save_base
+ avail
;
321 /* Adjust all the streammarkers. */
322 delta
= end_p
- fp
->_IO_read_base
;
323 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
332 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
333 if (_IO_vtable_offset (fp
) == 0 && _IO_fwide (fp
, -1) != -1)
339 if (_IO_in_put_mode (fp
))
340 if (_IO_switch_to_get_mode (fp
) == EOF
)
342 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
343 return *(unsigned char *) fp
->_IO_read_ptr
;
344 if (_IO_in_backup (fp
))
346 _IO_switch_to_main_get_area (fp
);
347 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
348 return *(unsigned char *) fp
->_IO_read_ptr
;
350 if (_IO_have_markers (fp
))
352 if (save_for_backup (fp
, fp
->_IO_read_end
))
355 else if (_IO_have_backup (fp
))
356 _IO_free_backup_area (fp
);
357 return _IO_UNDERFLOW (fp
);
359 libc_hidden_def (__underflow
)
365 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
366 if (_IO_vtable_offset (fp
) == 0 && _IO_fwide (fp
, -1) != -1)
372 if (_IO_in_put_mode (fp
))
373 if (_IO_switch_to_get_mode (fp
) == EOF
)
375 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
376 return *(unsigned char *) fp
->_IO_read_ptr
++;
377 if (_IO_in_backup (fp
))
379 _IO_switch_to_main_get_area (fp
);
380 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
381 return *(unsigned char *) fp
->_IO_read_ptr
++;
383 if (_IO_have_markers (fp
))
385 if (save_for_backup (fp
, fp
->_IO_read_end
))
388 else if (_IO_have_backup (fp
))
389 _IO_free_backup_area (fp
);
390 return _IO_UFLOW (fp
);
392 libc_hidden_def (__uflow
)
395 _IO_setb (f
, b
, eb
, a
)
401 if (f
->_IO_buf_base
&& !(f
->_flags
& _IO_USER_BUF
))
402 FREE_BUF (f
->_IO_buf_base
, _IO_blen (f
));
406 f
->_flags
&= ~_IO_USER_BUF
;
408 f
->_flags
|= _IO_USER_BUF
;
410 libc_hidden_def (_IO_setb
)
416 if (fp
->_IO_buf_base
)
418 if (!(fp
->_flags
& _IO_UNBUFFERED
) || fp
->_mode
> 0)
419 if (_IO_DOALLOCATE (fp
) != EOF
)
421 _IO_setb (fp
, fp
->_shortbuf
, fp
->_shortbuf
+1, 0);
423 libc_hidden_def (_IO_doallocbuf
)
426 _IO_default_underflow (fp
)
433 _IO_default_uflow (fp
)
436 int ch
= _IO_UNDERFLOW (fp
);
439 return *(unsigned char *) fp
->_IO_read_ptr
++;
441 libc_hidden_def (_IO_default_uflow
)
444 _IO_default_xsputn (f
, data
, n
)
449 const char *s
= (char *) data
;
455 /* Space available. */
456 if (f
->_IO_write_ptr
< f
->_IO_write_end
)
458 _IO_size_t count
= f
->_IO_write_end
- f
->_IO_write_ptr
;
464 f
->_IO_write_ptr
= __mempcpy (f
->_IO_write_ptr
, s
, count
);
466 memcpy (f
->_IO_write_ptr
, s
, count
);
467 f
->_IO_write_ptr
+= count
;
473 char *p
= f
->_IO_write_ptr
;
475 for (i
= count
; --i
>= 0; )
477 f
->_IO_write_ptr
= p
;
481 if (more
== 0 || _IO_OVERFLOW (f
, (unsigned char) *s
++) == EOF
)
487 libc_hidden_def (_IO_default_xsputn
)
490 _IO_sgetn (fp
, data
, n
)
495 /* FIXME handle putback buffer here! */
496 return _IO_XSGETN (fp
, data
, n
);
498 libc_hidden_def (_IO_sgetn
)
501 _IO_default_xsgetn (fp
, data
, n
)
507 char *s
= (char*) data
;
510 /* Data available. */
511 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
513 _IO_size_t count
= fp
->_IO_read_end
- fp
->_IO_read_ptr
;
519 s
= __mempcpy (s
, fp
->_IO_read_ptr
, count
);
521 memcpy (s
, fp
->_IO_read_ptr
, count
);
524 fp
->_IO_read_ptr
+= count
;
528 char *p
= fp
->_IO_read_ptr
;
532 fp
->_IO_read_ptr
= p
;
536 if (more
== 0 || __underflow (fp
) == EOF
)
541 libc_hidden_def (_IO_default_xsgetn
)
544 /* Seems not to be needed. --drepper */
554 _IO_default_setbuf (fp
, p
, len
)
559 if (_IO_SYNC (fp
) == EOF
)
561 if (p
== NULL
|| len
== 0)
563 fp
->_flags
|= _IO_UNBUFFERED
;
564 _IO_setb (fp
, fp
->_shortbuf
, fp
->_shortbuf
+1, 0);
568 fp
->_flags
&= ~_IO_UNBUFFERED
;
569 _IO_setb (fp
, p
, p
+len
, 0);
571 fp
->_IO_write_base
= fp
->_IO_write_ptr
= fp
->_IO_write_end
= 0;
572 fp
->_IO_read_base
= fp
->_IO_read_ptr
= fp
->_IO_read_end
= 0;
577 _IO_default_seekpos (fp
, pos
, mode
)
582 return _IO_SEEKOFF (fp
, pos
, 0, mode
);
586 _IO_default_doallocate (fp
)
591 ALLOC_BUF (buf
, _IO_BUFSIZ
, EOF
);
592 _IO_setb (fp
, buf
, buf
+_IO_BUFSIZ
, 1);
595 libc_hidden_def (_IO_default_doallocate
)
602 _IO_no_init (fp
, flags
, -1, NULL
, NULL
);
604 libc_hidden_def (_IO_init
)
607 _IO_old_init (fp
, flags
)
611 fp
->_flags
= _IO_MAGIC
|flags
;
613 fp
->_IO_buf_base
= NULL
;
614 fp
->_IO_buf_end
= NULL
;
615 fp
->_IO_read_base
= NULL
;
616 fp
->_IO_read_ptr
= NULL
;
617 fp
->_IO_read_end
= NULL
;
618 fp
->_IO_write_base
= NULL
;
619 fp
->_IO_write_ptr
= NULL
;
620 fp
->_IO_write_end
= NULL
;
621 fp
->_chain
= NULL
; /* Not necessary. */
623 fp
->_IO_save_base
= NULL
;
624 fp
->_IO_backup_base
= NULL
;
625 fp
->_IO_save_end
= NULL
;
629 fp
->_vtable_offset
= 0;
632 if (fp
->_lock
!= NULL
)
633 _IO_lock_init (*fp
->_lock
);
638 _IO_no_init (fp
, flags
, orientation
, wd
, jmp
)
642 struct _IO_wide_data
*wd
;
643 const struct _IO_jump_t
*jmp
;
645 _IO_old_init (fp
, flags
);
646 fp
->_mode
= orientation
;
647 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
648 if (orientation
>= 0)
651 fp
->_wide_data
->_IO_buf_base
= NULL
;
652 fp
->_wide_data
->_IO_buf_end
= NULL
;
653 fp
->_wide_data
->_IO_read_base
= NULL
;
654 fp
->_wide_data
->_IO_read_ptr
= NULL
;
655 fp
->_wide_data
->_IO_read_end
= NULL
;
656 fp
->_wide_data
->_IO_write_base
= NULL
;
657 fp
->_wide_data
->_IO_write_ptr
= NULL
;
658 fp
->_wide_data
->_IO_write_end
= NULL
;
659 fp
->_wide_data
->_IO_save_base
= NULL
;
660 fp
->_wide_data
->_IO_backup_base
= NULL
;
661 fp
->_wide_data
->_IO_save_end
= NULL
;
663 fp
->_wide_data
->_wide_vtable
= jmp
;
666 fp
->_freeres_list
= NULL
;
670 _IO_default_sync (fp
)
676 /* The way the C++ classes are mapped into the C functions in the
677 current implementation, this function can get called twice! */
680 _IO_default_finish (fp
, dummy
)
684 struct _IO_marker
*mark
;
685 if (fp
->_IO_buf_base
&& !(fp
->_flags
& _IO_USER_BUF
))
687 FREE_BUF (fp
->_IO_buf_base
, _IO_blen (fp
));
688 fp
->_IO_buf_base
= fp
->_IO_buf_end
= NULL
;
691 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
694 if (fp
->_IO_save_base
)
696 free (fp
->_IO_save_base
);
697 fp
->_IO_save_base
= NULL
;
700 _IO_un_link ((struct _IO_FILE_plus
*) fp
);
703 if (fp
->_lock
!= NULL
)
704 _IO_lock_fini (*fp
->_lock
);
707 libc_hidden_def (_IO_default_finish
)
710 _IO_default_seekoff (fp
, offset
, dir
, mode
)
720 _IO_sputbackc (fp
, c
)
726 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
727 && (unsigned char)fp
->_IO_read_ptr
[-1] == (unsigned char)c
)
730 result
= (unsigned char) c
;
733 result
= _IO_PBACKFAIL (fp
, c
);
736 fp
->_flags
&= ~_IO_EOF_SEEN
;
740 libc_hidden_def (_IO_sputbackc
)
748 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
)
751 result
= (unsigned char) *fp
->_IO_read_ptr
;
754 result
= _IO_PBACKFAIL (fp
, EOF
);
757 fp
->_flags
&= ~_IO_EOF_SEEN
;
762 #if 0 /* Work in progress */
763 /* Seems not to be needed. */
766 _IO_set_column (fp
, c
)
773 fp
->_column
= c
- (fp
->_IO_write_ptr
- fp
->_IO_write_base
);
777 _IO_set_column (fp
, i
)
781 fp
->_cur_column
= i
+ 1;
789 _IO_adjust_column (start
, line
, count
)
794 const char *ptr
= line
+ count
;
797 return line
+ count
- ptr
- 1;
798 return start
+ count
;
800 libc_hidden_def (_IO_adjust_column
)
803 /* Seems not to be needed. --drepper */
809 return _IO_adjust_column (fp
->_cur_column
- 1,
811 fp
->_IO_write_ptr
- fp
->_IO_write_base
);
818 _IO_flush_all_lockp (int do_lock
)
825 __libc_cleanup_region_start (do_lock
, flush_cleanup
, 0);
827 _IO_lock_lock (list_all_lock
);
830 last_stamp
= _IO_list_all_stamp
;
831 fp
= (_IO_FILE
*) _IO_list_all
;
838 if (((fp
->_mode
<= 0 && fp
->_IO_write_ptr
> fp
->_IO_write_base
)
839 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
840 || (_IO_vtable_offset (fp
) == 0
841 && fp
->_mode
> 0 && (fp
->_wide_data
->_IO_write_ptr
842 > fp
->_wide_data
->_IO_write_base
))
845 && _IO_OVERFLOW (fp
, EOF
) == EOF
)
849 _IO_funlockfile (fp
);
852 if (last_stamp
!= _IO_list_all_stamp
)
854 /* Something was added to the list. Start all over again. */
855 fp
= (_IO_FILE
*) _IO_list_all
;
856 last_stamp
= _IO_list_all_stamp
;
864 _IO_lock_unlock (list_all_lock
);
865 __libc_cleanup_region_end (0);
875 /* We want locking. */
876 return _IO_flush_all_lockp (1);
878 libc_hidden_def (_IO_flush_all
)
881 _IO_flush_all_linebuffered ()
887 _IO_cleanup_region_start_noarg (flush_cleanup
);
888 _IO_lock_lock (list_all_lock
);
891 last_stamp
= _IO_list_all_stamp
;
892 fp
= (_IO_FILE
*) _IO_list_all
;
898 if ((fp
->_flags
& _IO_NO_WRITES
) == 0 && fp
->_flags
& _IO_LINE_BUF
)
899 _IO_OVERFLOW (fp
, EOF
);
901 _IO_funlockfile (fp
);
904 if (last_stamp
!= _IO_list_all_stamp
)
906 /* Something was added to the list. Start all over again. */
907 fp
= (_IO_FILE
*) _IO_list_all
;
908 last_stamp
= _IO_list_all_stamp
;
915 _IO_lock_unlock (list_all_lock
);
916 _IO_cleanup_region_end (0);
919 libc_hidden_def (_IO_flush_all_linebuffered
)
921 weak_alias (_IO_flush_all_linebuffered
, _flushlbf
)
925 /* The following is a bit tricky. In general, we want to unbuffer the
926 streams so that all output which follows is seen. If we are not
927 looking for memory leaks it does not make much sense to free the
928 actual buffer because this will happen anyway once the program
929 terminated. If we do want to look for memory leaks we have to free
930 the buffers. Whether something is freed is determined by the
931 function sin the libc_freeres section. Those are called as part of
932 the atexit routine, just like _IO_cleanup. The problem is we do
933 not know whether the freeres code is called first or _IO_cleanup.
934 if the former is the case, we set the DEALLOC_BUFFER variable to
935 true and _IO_unbuffer_write will take care of the rest. If
936 _IO_unbuffer_write is called first we add the streams to a list
937 which the freeres function later can walk through. */
938 static void _IO_unbuffer_write (void);
940 static bool dealloc_buffers
;
941 static _IO_FILE
*freeres_list
;
944 _IO_unbuffer_write (void)
947 for (fp
= (_IO_FILE
*) _IO_list_all
; fp
; fp
= fp
->_chain
)
949 if (! (fp
->_flags
& _IO_UNBUFFERED
)
950 && (! (fp
->_flags
& _IO_NO_WRITES
)
951 || (fp
->_flags
& _IO_IS_APPENDING
))
952 /* Iff stream is un-orientated, it wasn't used. */
957 for (cnt
= 0; cnt
< MAXTRIES
; ++cnt
)
958 if (fp
->_lock
== NULL
|| _IO_lock_trylock (*fp
->_lock
) == 0)
961 /* Give the other thread time to finish up its use of the
965 if (! dealloc_buffers
&& !(fp
->_flags
& _IO_USER_BUF
))
967 fp
->_flags
|= _IO_USER_BUF
;
969 fp
->_freeres_list
= freeres_list
;
971 fp
->_freeres_buf
= fp
->_IO_buf_base
;
972 fp
->_freeres_size
= _IO_blen (fp
);
975 _IO_SETBUF (fp
, NULL
, 0);
977 if (cnt
< MAXTRIES
&& fp
->_lock
!= NULL
)
978 _IO_lock_unlock (*fp
->_lock
);
981 /* Make sure that never again the wide char functions can be
988 libc_freeres_fn (buffer_free
)
990 dealloc_buffers
= true;
992 while (freeres_list
!= NULL
)
994 FREE_BUF (freeres_list
->_freeres_buf
, freeres_list
->_freeres_size
);
996 freeres_list
= freeres_list
->_freeres_list
;
1004 /* We do *not* want locking. Some threads might use streams but
1005 that is their problem, we flush them underneath them. */
1006 int result
= _IO_flush_all_lockp (0);
1008 /* We currently don't have a reliable mechanism for making sure that
1009 C++ static destructors are executed in the correct order.
1010 So it is possible that other static destructors might want to
1011 write to cout - and they're supposed to be able to do so.
1013 The following will make the standard streambufs be unbuffered,
1014 which forces any output from late destructors to be written out. */
1015 _IO_unbuffer_write ();
1022 _IO_init_marker (marker
, fp
)
1023 struct _IO_marker
*marker
;
1027 if (_IO_in_put_mode (fp
))
1028 _IO_switch_to_get_mode (fp
);
1029 if (_IO_in_backup (fp
))
1030 marker
->_pos
= fp
->_IO_read_ptr
- fp
->_IO_read_end
;
1032 marker
->_pos
= fp
->_IO_read_ptr
- fp
->_IO_read_base
;
1034 /* Should perhaps sort the chain? */
1035 marker
->_next
= fp
->_markers
;
1036 fp
->_markers
= marker
;
1040 _IO_remove_marker (marker
)
1041 struct _IO_marker
*marker
;
1043 /* Unlink from sb's chain. */
1044 struct _IO_marker
**ptr
= &marker
->_sbuf
->_markers
;
1045 for (; ; ptr
= &(*ptr
)->_next
)
1049 else if (*ptr
== marker
)
1051 *ptr
= marker
->_next
;
1056 if _sbuf has a backup area that is no longer needed
, should we
delete
1057 it now
, or wait until the next underflow
?
1061 #define BAD_DELTA EOF
1064 _IO_marker_difference (mark1
, mark2
)
1065 struct _IO_marker
*mark1
;
1066 struct _IO_marker
*mark2
;
1068 return mark1
->_pos
- mark2
->_pos
;
1071 /* Return difference between MARK and current position of MARK's stream. */
1073 _IO_marker_delta (mark
)
1074 struct _IO_marker
*mark
;
1077 if (mark
->_sbuf
== NULL
)
1079 if (_IO_in_backup (mark
->_sbuf
))
1080 cur_pos
= mark
->_sbuf
->_IO_read_ptr
- mark
->_sbuf
->_IO_read_end
;
1082 cur_pos
= mark
->_sbuf
->_IO_read_ptr
- mark
->_sbuf
->_IO_read_base
;
1083 return mark
->_pos
- cur_pos
;
1087 _IO_seekmark (fp
, mark
, delta
)
1089 struct _IO_marker
*mark
;
1092 if (mark
->_sbuf
!= fp
)
1094 if (mark
->_pos
>= 0)
1096 if (_IO_in_backup (fp
))
1097 _IO_switch_to_main_get_area (fp
);
1098 fp
->_IO_read_ptr
= fp
->_IO_read_base
+ mark
->_pos
;
1102 if (!_IO_in_backup (fp
))
1103 _IO_switch_to_backup_area (fp
);
1104 fp
->_IO_read_ptr
= fp
->_IO_read_end
+ mark
->_pos
;
1110 _IO_unsave_markers (fp
)
1113 struct _IO_marker
*mark
= fp
->_markers
;
1117 streampos offset
= seekoff (0, ios::cur
, ios::in
);
1120 offset
+= eGptr () - Gbase ();
1121 for ( ; mark
!= NULL
; mark
= mark
->_next
)
1122 mark
->set_streampos (mark
->_pos
+ offset
);
1126 for ( ; mark
!= NULL
; mark
= mark
->_next
)
1127 mark
->set_streampos (EOF
);
1133 if (_IO_have_backup (fp
))
1134 _IO_free_backup_area (fp
);
1136 libc_hidden_def (_IO_unsave_markers
)
1139 /* Seems not to be needed. --drepper */
1141 _IO_nobackup_pbackfail (fp
, c
)
1145 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
)
1147 if (c
!= EOF
&& *fp
->_IO_read_ptr
!= c
)
1148 *fp
->_IO_read_ptr
= c
;
1149 return (unsigned char) c
;
1154 _IO_default_pbackfail (fp
, c
)
1158 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
&& !_IO_in_backup (fp
)
1159 && (unsigned char) fp
->_IO_read_ptr
[-1] == c
)
1163 /* Need to handle a filebuf in write mode (switch to read mode). FIXME!*/
1164 if (!_IO_in_backup (fp
))
1166 /* We need to keep the invariant that the main get area
1167 logically follows the backup area. */
1168 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
&& _IO_have_backup (fp
))
1170 if (save_for_backup (fp
, fp
->_IO_read_ptr
))
1173 else if (!_IO_have_backup (fp
))
1175 /* No backup buffer: allocate one. */
1176 /* Use nshort buffer, if unused? (probably not) FIXME */
1177 int backup_size
= 128;
1178 char *bbuf
= (char *) malloc (backup_size
);
1181 fp
->_IO_save_base
= bbuf
;
1182 fp
->_IO_save_end
= fp
->_IO_save_base
+ backup_size
;
1183 fp
->_IO_backup_base
= fp
->_IO_save_end
;
1185 fp
->_IO_read_base
= fp
->_IO_read_ptr
;
1186 _IO_switch_to_backup_area (fp
);
1188 else if (fp
->_IO_read_ptr
<= fp
->_IO_read_base
)
1190 /* Increase size of existing backup buffer. */
1191 _IO_size_t new_size
;
1192 _IO_size_t old_size
= fp
->_IO_read_end
- fp
->_IO_read_base
;
1194 new_size
= 2 * old_size
;
1195 new_buf
= (char *) malloc (new_size
);
1196 if (new_buf
== NULL
)
1198 memcpy (new_buf
+ (new_size
- old_size
), fp
->_IO_read_base
,
1200 free (fp
->_IO_read_base
);
1201 _IO_setg (fp
, new_buf
, new_buf
+ (new_size
- old_size
),
1202 new_buf
+ new_size
);
1203 fp
->_IO_backup_base
= fp
->_IO_read_ptr
;
1206 *--fp
->_IO_read_ptr
= c
;
1208 return (unsigned char) c
;
1210 libc_hidden_def (_IO_default_pbackfail
)
1213 _IO_default_seek (fp
, offset
, dir
)
1222 _IO_default_stat (fp
, st
)
1230 _IO_default_read (fp
, data
, n
)
1239 _IO_default_write (fp
, data
, n
)
1248 _IO_default_showmanyc (fp
)
1255 _IO_default_imbue (fp
, locale
)
1264 return (_IO_ITER
) _IO_list_all
;
1266 libc_hidden_def (_IO_iter_begin
)
1273 libc_hidden_def (_IO_iter_end
)
1279 return iter
->_chain
;
1281 libc_hidden_def (_IO_iter_next
)
1289 libc_hidden_def (_IO_iter_file
)
1294 #ifdef _IO_MTSAFE_IO
1295 _IO_lock_lock (list_all_lock
);
1298 libc_hidden_def (_IO_list_lock
)
1303 #ifdef _IO_MTSAFE_IO
1304 _IO_lock_unlock (list_all_lock
);
1307 libc_hidden_def (_IO_list_unlock
)
1310 _IO_list_resetlock()
1312 #ifdef _IO_MTSAFE_IO
1313 _IO_lock_init (list_all_lock
);
1316 libc_hidden_def (_IO_list_resetlock
)
1321 #define IO_CLEANUP ;
1329 ~__io_defs() { _IO_cleanup (); }
1331 __io_defs io_defs__
;
1336 #ifdef text_set_element
1337 text_set_element(__libc_atexit
, _IO_cleanup
);