1 /* Copyright (C) 1993,1995,1997-2002, 2003, 2004, 2006, 2007, 2011
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 As a special exception, if you link the code in this file with
21 files compiled with a GNU compiler to produce an executable,
22 that does not cause the resulting executable to be covered by
23 the GNU Lesser General Public License. This exception does not
24 however invalidate any other reasons why the executable file
25 might be covered by the GNU Lesser General Public License.
26 This exception applies to code released by its copyright holders
27 in files containing the exception. */
29 /* Generic or default I/O operations. */
42 static _IO_lock_t list_all_lock
= _IO_lock_initializer
;
45 /* Used to signal modifications to the list of FILE decriptors. */
46 static int _IO_list_all_stamp
;
49 static _IO_FILE
*run_fp
;
52 flush_cleanup (void *not_used
)
55 _IO_funlockfile (run_fp
);
57 _IO_lock_unlock (list_all_lock
);
63 struct _IO_FILE_plus
*fp
;
65 if (fp
->file
._flags
& _IO_LINKED
)
69 _IO_cleanup_region_start_noarg (flush_cleanup
);
70 _IO_lock_lock (list_all_lock
);
71 run_fp
= (_IO_FILE
*) fp
;
72 _IO_flockfile ((_IO_FILE
*) fp
);
74 if (INTUSE(_IO_list_all
) == NULL
)
76 else if (fp
== INTUSE(_IO_list_all
))
79 = (struct _IO_FILE_plus
*) INTUSE(_IO_list_all
)->file
._chain
;
83 for (f
= &INTUSE(_IO_list_all
)->file
._chain
; *f
; f
= &(*f
)->_chain
)
84 if (*f
== (_IO_FILE
*) fp
)
90 fp
->file
._flags
&= ~_IO_LINKED
;
92 _IO_funlockfile ((_IO_FILE
*) fp
);
94 _IO_lock_unlock (list_all_lock
);
95 _IO_cleanup_region_end (0);
103 struct _IO_FILE_plus
*fp
;
105 if ((fp
->file
._flags
& _IO_LINKED
) == 0)
107 fp
->file
._flags
|= _IO_LINKED
;
109 _IO_cleanup_region_start_noarg (flush_cleanup
);
110 _IO_lock_lock (list_all_lock
);
111 run_fp
= (_IO_FILE
*) fp
;
112 _IO_flockfile ((_IO_FILE
*) fp
);
114 fp
->file
._chain
= (_IO_FILE
*) INTUSE(_IO_list_all
);
115 INTUSE(_IO_list_all
) = fp
;
116 ++_IO_list_all_stamp
;
118 _IO_funlockfile ((_IO_FILE
*) fp
);
120 _IO_lock_unlock (list_all_lock
);
121 _IO_cleanup_region_end (0);
127 /* Return minimum _pos markers
128 Assumes the current get area is the main get area. */
129 _IO_ssize_t
_IO_least_marker (_IO_FILE
*fp
, char *end_p
);
132 _IO_least_marker (fp
, end_p
)
136 _IO_ssize_t least_so_far
= end_p
- fp
->_IO_read_base
;
137 struct _IO_marker
*mark
;
138 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
139 if (mark
->_pos
< least_so_far
)
140 least_so_far
= mark
->_pos
;
144 /* Switch current get area from backup buffer to (start of) main get area. */
147 _IO_switch_to_main_get_area (fp
)
151 fp
->_flags
&= ~_IO_IN_BACKUP
;
152 /* Swap _IO_read_end and _IO_save_end. */
153 tmp
= fp
->_IO_read_end
;
154 fp
->_IO_read_end
= fp
->_IO_save_end
;
155 fp
->_IO_save_end
= tmp
;
156 /* Swap _IO_read_base and _IO_save_base. */
157 tmp
= fp
->_IO_read_base
;
158 fp
->_IO_read_base
= fp
->_IO_save_base
;
159 fp
->_IO_save_base
= tmp
;
160 /* Set _IO_read_ptr. */
161 fp
->_IO_read_ptr
= fp
->_IO_read_base
;
164 /* Switch current get area from main get area to (end of) backup area. */
167 _IO_switch_to_backup_area (fp
)
171 fp
->_flags
|= _IO_IN_BACKUP
;
172 /* Swap _IO_read_end and _IO_save_end. */
173 tmp
= fp
->_IO_read_end
;
174 fp
->_IO_read_end
= fp
->_IO_save_end
;
175 fp
->_IO_save_end
= tmp
;
176 /* Swap _IO_read_base and _IO_save_base. */
177 tmp
= fp
->_IO_read_base
;
178 fp
->_IO_read_base
= fp
->_IO_save_base
;
179 fp
->_IO_save_base
= tmp
;
180 /* Set _IO_read_ptr. */
181 fp
->_IO_read_ptr
= fp
->_IO_read_end
;
185 _IO_switch_to_get_mode (fp
)
188 if (fp
->_IO_write_ptr
> fp
->_IO_write_base
)
189 if (_IO_OVERFLOW (fp
, EOF
) == EOF
)
191 if (_IO_in_backup (fp
))
192 fp
->_IO_read_base
= fp
->_IO_backup_base
;
195 fp
->_IO_read_base
= fp
->_IO_buf_base
;
196 if (fp
->_IO_write_ptr
> fp
->_IO_read_end
)
197 fp
->_IO_read_end
= fp
->_IO_write_ptr
;
199 fp
->_IO_read_ptr
= fp
->_IO_write_ptr
;
201 fp
->_IO_write_base
= fp
->_IO_write_ptr
= fp
->_IO_write_end
= fp
->_IO_read_ptr
;
203 fp
->_flags
&= ~_IO_CURRENTLY_PUTTING
;
206 INTDEF(_IO_switch_to_get_mode
)
209 _IO_free_backup_area (fp
)
212 if (_IO_in_backup (fp
))
213 _IO_switch_to_main_get_area (fp
); /* Just in case. */
214 free (fp
->_IO_save_base
);
215 fp
->_IO_save_base
= NULL
;
216 fp
->_IO_save_end
= NULL
;
217 fp
->_IO_backup_base
= NULL
;
219 INTDEF(_IO_free_backup_area
)
223 _IO_switch_to_put_mode (fp
)
226 fp
->_IO_write_base
= fp
->_IO_read_ptr
;
227 fp
->_IO_write_ptr
= fp
->_IO_read_ptr
;
228 /* Following is wrong if line- or un-buffered? */
229 fp
->_IO_write_end
= (fp
->_flags
& _IO_IN_BACKUP
230 ? fp
->_IO_read_end
: fp
->_IO_buf_end
);
232 fp
->_IO_read_ptr
= fp
->_IO_read_end
;
233 fp
->_IO_read_base
= fp
->_IO_read_end
;
235 fp
->_flags
|= _IO_CURRENTLY_PUTTING
;
245 /* This is a single-byte stream. */
248 return _IO_OVERFLOW (f
, ch
);
250 libc_hidden_def (__overflow
)
252 static int save_for_backup (_IO_FILE
*fp
, char *end_p
)
262 save_for_backup (fp
, end_p
)
266 /* Append [_IO_read_base..end_p] to backup area. */
267 _IO_ssize_t least_mark
= _IO_least_marker (fp
, end_p
);
268 /* needed_size is how much space we need in the backup area. */
269 _IO_size_t needed_size
= (end_p
- fp
->_IO_read_base
) - least_mark
;
270 /* FIXME: Dubious arithmetic if pointers are NULL */
271 _IO_size_t current_Bsize
= fp
->_IO_save_end
- fp
->_IO_save_base
;
272 _IO_size_t avail
; /* Extra space available for future expansion. */
274 struct _IO_marker
*mark
;
275 if (needed_size
> current_Bsize
)
279 new_buffer
= (char *) malloc (avail
+ needed_size
);
280 if (new_buffer
== NULL
)
281 return EOF
; /* FIXME */
285 __mempcpy (__mempcpy (new_buffer
+ avail
,
286 fp
->_IO_save_end
+ least_mark
,
289 end_p
- fp
->_IO_read_base
);
291 memcpy (new_buffer
+ avail
,
292 fp
->_IO_save_end
+ least_mark
,
294 memcpy (new_buffer
+ avail
- least_mark
,
296 end_p
- fp
->_IO_read_base
);
300 memcpy (new_buffer
+ avail
,
301 fp
->_IO_read_base
+ least_mark
,
303 free (fp
->_IO_save_base
);
304 fp
->_IO_save_base
= new_buffer
;
305 fp
->_IO_save_end
= new_buffer
+ avail
+ needed_size
;
309 avail
= current_Bsize
- needed_size
;
312 memmove (fp
->_IO_save_base
+ avail
,
313 fp
->_IO_save_end
+ least_mark
,
315 memcpy (fp
->_IO_save_base
+ avail
- least_mark
,
317 end_p
- fp
->_IO_read_base
);
319 else if (needed_size
> 0)
320 memcpy (fp
->_IO_save_base
+ avail
,
321 fp
->_IO_read_base
+ least_mark
,
324 fp
->_IO_backup_base
= fp
->_IO_save_base
+ avail
;
325 /* Adjust all the streammarkers. */
326 delta
= end_p
- fp
->_IO_read_base
;
327 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
336 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
337 if (_IO_vtable_offset (fp
) == 0 && _IO_fwide (fp
, -1) != -1)
343 if (_IO_in_put_mode (fp
))
344 if (INTUSE(_IO_switch_to_get_mode
) (fp
) == EOF
)
346 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
347 return *(unsigned char *) fp
->_IO_read_ptr
;
348 if (_IO_in_backup (fp
))
350 _IO_switch_to_main_get_area (fp
);
351 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
352 return *(unsigned char *) fp
->_IO_read_ptr
;
354 if (_IO_have_markers (fp
))
356 if (save_for_backup (fp
, fp
->_IO_read_end
))
359 else if (_IO_have_backup (fp
))
360 INTUSE(_IO_free_backup_area
) (fp
);
361 return _IO_UNDERFLOW (fp
);
363 libc_hidden_def (__underflow
)
369 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
370 if (_IO_vtable_offset (fp
) == 0 && _IO_fwide (fp
, -1) != -1)
376 if (_IO_in_put_mode (fp
))
377 if (INTUSE(_IO_switch_to_get_mode
) (fp
) == EOF
)
379 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
380 return *(unsigned char *) fp
->_IO_read_ptr
++;
381 if (_IO_in_backup (fp
))
383 _IO_switch_to_main_get_area (fp
);
384 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
385 return *(unsigned char *) fp
->_IO_read_ptr
++;
387 if (_IO_have_markers (fp
))
389 if (save_for_backup (fp
, fp
->_IO_read_end
))
392 else if (_IO_have_backup (fp
))
393 INTUSE(_IO_free_backup_area
) (fp
);
394 return _IO_UFLOW (fp
);
396 libc_hidden_def (__uflow
)
399 _IO_setb (f
, b
, eb
, a
)
405 if (f
->_IO_buf_base
&& !(f
->_flags
& _IO_USER_BUF
))
406 FREE_BUF (f
->_IO_buf_base
, _IO_blen (f
));
410 f
->_flags
&= ~_IO_USER_BUF
;
412 f
->_flags
|= _IO_USER_BUF
;
420 if (fp
->_IO_buf_base
)
422 if (!(fp
->_flags
& _IO_UNBUFFERED
) || fp
->_mode
> 0)
423 if (_IO_DOALLOCATE (fp
) != EOF
)
425 INTUSE(_IO_setb
) (fp
, fp
->_shortbuf
, fp
->_shortbuf
+1, 0);
427 INTDEF(_IO_doallocbuf
)
430 _IO_default_underflow (fp
)
437 _IO_default_uflow (fp
)
440 int ch
= _IO_UNDERFLOW (fp
);
443 return *(unsigned char *) fp
->_IO_read_ptr
++;
445 INTDEF(_IO_default_uflow
)
448 _IO_default_xsputn (f
, data
, n
)
453 const char *s
= (char *) data
;
459 /* Space available. */
460 if (f
->_IO_write_ptr
< f
->_IO_write_end
)
462 _IO_size_t count
= f
->_IO_write_end
- f
->_IO_write_ptr
;
468 f
->_IO_write_ptr
= __mempcpy (f
->_IO_write_ptr
, s
, count
);
470 memcpy (f
->_IO_write_ptr
, s
, count
);
471 f
->_IO_write_ptr
+= count
;
477 char *p
= f
->_IO_write_ptr
;
479 for (i
= count
; --i
>= 0; )
481 f
->_IO_write_ptr
= p
;
485 if (more
== 0 || _IO_OVERFLOW (f
, (unsigned char) *s
++) == EOF
)
491 INTDEF(_IO_default_xsputn
)
494 _IO_sgetn (fp
, data
, n
)
499 /* FIXME handle putback buffer here! */
500 return _IO_XSGETN (fp
, data
, n
);
505 _IO_default_xsgetn (fp
, data
, n
)
511 char *s
= (char*) data
;
514 /* Data available. */
515 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
517 _IO_size_t count
= fp
->_IO_read_end
- fp
->_IO_read_ptr
;
523 s
= __mempcpy (s
, fp
->_IO_read_ptr
, count
);
525 memcpy (s
, fp
->_IO_read_ptr
, count
);
528 fp
->_IO_read_ptr
+= count
;
532 char *p
= fp
->_IO_read_ptr
;
536 fp
->_IO_read_ptr
= p
;
540 if (more
== 0 || __underflow (fp
) == EOF
)
545 INTDEF(_IO_default_xsgetn
)
548 /* Seems not to be needed. --drepper */
558 _IO_default_setbuf (fp
, p
, len
)
563 if (_IO_SYNC (fp
) == EOF
)
565 if (p
== NULL
|| len
== 0)
567 fp
->_flags
|= _IO_UNBUFFERED
;
568 INTUSE(_IO_setb
) (fp
, fp
->_shortbuf
, fp
->_shortbuf
+1, 0);
572 fp
->_flags
&= ~_IO_UNBUFFERED
;
573 INTUSE(_IO_setb
) (fp
, p
, p
+len
, 0);
575 fp
->_IO_write_base
= fp
->_IO_write_ptr
= fp
->_IO_write_end
= 0;
576 fp
->_IO_read_base
= fp
->_IO_read_ptr
= fp
->_IO_read_end
= 0;
581 _IO_default_seekpos (fp
, pos
, mode
)
586 return _IO_SEEKOFF (fp
, pos
, 0, mode
);
590 _IO_default_doallocate (fp
)
595 ALLOC_BUF (buf
, _IO_BUFSIZ
, EOF
);
596 INTUSE(_IO_setb
) (fp
, buf
, buf
+_IO_BUFSIZ
, 1);
599 INTDEF(_IO_default_doallocate
)
606 _IO_no_init (fp
, flags
, -1, NULL
, NULL
);
611 _IO_old_init (fp
, flags
)
615 fp
->_flags
= _IO_MAGIC
|flags
;
617 fp
->_IO_buf_base
= NULL
;
618 fp
->_IO_buf_end
= NULL
;
619 fp
->_IO_read_base
= NULL
;
620 fp
->_IO_read_ptr
= NULL
;
621 fp
->_IO_read_end
= NULL
;
622 fp
->_IO_write_base
= NULL
;
623 fp
->_IO_write_ptr
= NULL
;
624 fp
->_IO_write_end
= NULL
;
625 fp
->_chain
= NULL
; /* Not necessary. */
627 fp
->_IO_save_base
= NULL
;
628 fp
->_IO_backup_base
= NULL
;
629 fp
->_IO_save_end
= NULL
;
633 fp
->_vtable_offset
= 0;
636 if (fp
->_lock
!= NULL
)
637 _IO_lock_init (*fp
->_lock
);
642 _IO_no_init (fp
, flags
, orientation
, wd
, jmp
)
646 struct _IO_wide_data
*wd
;
647 const struct _IO_jump_t
*jmp
;
649 _IO_old_init (fp
, flags
);
650 fp
->_mode
= orientation
;
651 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
652 if (orientation
>= 0)
655 fp
->_wide_data
->_IO_buf_base
= NULL
;
656 fp
->_wide_data
->_IO_buf_end
= NULL
;
657 fp
->_wide_data
->_IO_read_base
= NULL
;
658 fp
->_wide_data
->_IO_read_ptr
= NULL
;
659 fp
->_wide_data
->_IO_read_end
= NULL
;
660 fp
->_wide_data
->_IO_write_base
= NULL
;
661 fp
->_wide_data
->_IO_write_ptr
= NULL
;
662 fp
->_wide_data
->_IO_write_end
= NULL
;
663 fp
->_wide_data
->_IO_save_base
= NULL
;
664 fp
->_wide_data
->_IO_backup_base
= NULL
;
665 fp
->_wide_data
->_IO_save_end
= NULL
;
667 fp
->_wide_data
->_wide_vtable
= jmp
;
670 fp
->_freeres_list
= NULL
;
674 _IO_default_sync (fp
)
680 /* The way the C++ classes are mapped into the C functions in the
681 current implementation, this function can get called twice! */
684 _IO_default_finish (fp
, dummy
)
688 struct _IO_marker
*mark
;
689 if (fp
->_IO_buf_base
&& !(fp
->_flags
& _IO_USER_BUF
))
691 FREE_BUF (fp
->_IO_buf_base
, _IO_blen (fp
));
692 fp
->_IO_buf_base
= fp
->_IO_buf_end
= NULL
;
695 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
698 if (fp
->_IO_save_base
)
700 free (fp
->_IO_save_base
);
701 fp
->_IO_save_base
= NULL
;
704 INTUSE(_IO_un_link
) ((struct _IO_FILE_plus
*) fp
);
707 if (fp
->_lock
!= NULL
)
708 _IO_lock_fini (*fp
->_lock
);
711 INTDEF(_IO_default_finish
)
714 _IO_default_seekoff (fp
, offset
, dir
, mode
)
724 _IO_sputbackc (fp
, c
)
730 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
731 && (unsigned char)fp
->_IO_read_ptr
[-1] == (unsigned char)c
)
734 result
= (unsigned char) c
;
737 result
= _IO_PBACKFAIL (fp
, c
);
740 fp
->_flags
&= ~_IO_EOF_SEEN
;
744 INTDEF(_IO_sputbackc
)
752 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
)
755 result
= (unsigned char) *fp
->_IO_read_ptr
;
758 result
= _IO_PBACKFAIL (fp
, EOF
);
761 fp
->_flags
&= ~_IO_EOF_SEEN
;
766 #if 0 /* Work in progress */
767 /* Seems not to be needed. */
770 _IO_set_column (fp
, c
)
777 fp
->_column
= c
- (fp
->_IO_write_ptr
- fp
->_IO_write_base
);
781 _IO_set_column (fp
, i
)
785 fp
->_cur_column
= i
+ 1;
793 _IO_adjust_column (start
, line
, count
)
798 const char *ptr
= line
+ count
;
801 return line
+ count
- ptr
- 1;
802 return start
+ count
;
804 INTDEF(_IO_adjust_column
)
807 /* Seems not to be needed. --drepper */
813 return _IO_adjust_column (fp
->_cur_column
- 1,
815 fp
->_IO_write_ptr
- fp
->_IO_write_base
);
822 _IO_flush_all_lockp (int do_lock
)
829 __libc_cleanup_region_start (do_lock
, flush_cleanup
, 0);
831 _IO_lock_lock (list_all_lock
);
834 last_stamp
= _IO_list_all_stamp
;
835 fp
= (_IO_FILE
*) INTUSE(_IO_list_all
);
842 if (((fp
->_mode
<= 0 && fp
->_IO_write_ptr
> fp
->_IO_write_base
)
843 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
844 || (_IO_vtable_offset (fp
) == 0
845 && fp
->_mode
> 0 && (fp
->_wide_data
->_IO_write_ptr
846 > fp
->_wide_data
->_IO_write_base
))
849 && _IO_OVERFLOW (fp
, EOF
) == EOF
)
853 _IO_funlockfile (fp
);
856 if (last_stamp
!= _IO_list_all_stamp
)
858 /* Something was added to the list. Start all over again. */
859 fp
= (_IO_FILE
*) INTUSE(_IO_list_all
);
860 last_stamp
= _IO_list_all_stamp
;
868 _IO_lock_unlock (list_all_lock
);
869 __libc_cleanup_region_end (0);
879 /* We want locking. */
880 return _IO_flush_all_lockp (1);
882 INTDEF(_IO_flush_all
)
885 _IO_flush_all_linebuffered ()
891 _IO_cleanup_region_start_noarg (flush_cleanup
);
892 _IO_lock_lock (list_all_lock
);
895 last_stamp
= _IO_list_all_stamp
;
896 fp
= (_IO_FILE
*) INTUSE(_IO_list_all
);
902 if ((fp
->_flags
& _IO_NO_WRITES
) == 0 && fp
->_flags
& _IO_LINE_BUF
)
903 _IO_OVERFLOW (fp
, EOF
);
905 _IO_funlockfile (fp
);
908 if (last_stamp
!= _IO_list_all_stamp
)
910 /* Something was added to the list. Start all over again. */
911 fp
= (_IO_FILE
*) INTUSE(_IO_list_all
);
912 last_stamp
= _IO_list_all_stamp
;
919 _IO_lock_unlock (list_all_lock
);
920 _IO_cleanup_region_end (0);
923 INTDEF(_IO_flush_all_linebuffered
)
925 weak_alias (_IO_flush_all_linebuffered
, _flushlbf
)
929 /* The following is a bit tricky. In general, we want to unbuffer the
930 streams so that all output which follows is seen. If we are not
931 looking for memory leaks it does not make much sense to free the
932 actual buffer because this will happen anyway once the program
933 terminated. If we do want to look for memory leaks we have to free
934 the buffers. Whether something is freed is determined by the
935 function sin the libc_freeres section. Those are called as part of
936 the atexit routine, just like _IO_cleanup. The problem is we do
937 not know whether the freeres code is called first or _IO_cleanup.
938 if the former is the case, we set the DEALLOC_BUFFER variable to
939 true and _IO_unbuffer_write will take care of the rest. If
940 _IO_unbuffer_write is called first we add the streams to a list
941 which the freeres function later can walk through. */
942 static void _IO_unbuffer_write (void);
944 static bool dealloc_buffers
;
945 static _IO_FILE
*freeres_list
;
948 _IO_unbuffer_write (void)
951 for (fp
= (_IO_FILE
*) INTUSE(_IO_list_all
); fp
; fp
= fp
->_chain
)
953 if (! (fp
->_flags
& _IO_UNBUFFERED
)
954 && (! (fp
->_flags
& _IO_NO_WRITES
)
955 || (fp
->_flags
& _IO_IS_APPENDING
))
956 /* Iff stream is un-orientated, it wasn't used. */
961 for (cnt
= 0; cnt
< MAXTRIES
; ++cnt
)
962 if (fp
->_lock
== NULL
|| _IO_lock_trylock (*fp
->_lock
) == 0)
965 /* Give the other thread time to finish up its use of the
969 if (! dealloc_buffers
&& !(fp
->_flags
& _IO_USER_BUF
))
971 fp
->_flags
|= _IO_USER_BUF
;
973 fp
->_freeres_list
= freeres_list
;
975 fp
->_freeres_buf
= fp
->_IO_buf_base
;
976 fp
->_freeres_size
= _IO_blen (fp
);
979 _IO_SETBUF (fp
, NULL
, 0);
981 if (cnt
< MAXTRIES
&& fp
->_lock
!= NULL
)
982 _IO_lock_unlock (*fp
->_lock
);
985 /* Make sure that never again the wide char functions can be
992 libc_freeres_fn (buffer_free
)
994 dealloc_buffers
= true;
996 while (freeres_list
!= NULL
)
998 FREE_BUF (freeres_list
->_freeres_buf
, freeres_list
->_freeres_size
);
1000 freeres_list
= freeres_list
->_freeres_list
;
1008 /* We do *not* want locking. Some threads might use streams but
1009 that is their problem, we flush them underneath them. */
1010 int result
= _IO_flush_all_lockp (0);
1012 /* We currently don't have a reliable mechanism for making sure that
1013 C++ static destructors are executed in the correct order.
1014 So it is possible that other static destructors might want to
1015 write to cout - and they're supposed to be able to do so.
1017 The following will make the standard streambufs be unbuffered,
1018 which forces any output from late destructors to be written out. */
1019 _IO_unbuffer_write ();
1026 _IO_init_marker (marker
, fp
)
1027 struct _IO_marker
*marker
;
1031 if (_IO_in_put_mode (fp
))
1032 INTUSE(_IO_switch_to_get_mode
) (fp
);
1033 if (_IO_in_backup (fp
))
1034 marker
->_pos
= fp
->_IO_read_ptr
- fp
->_IO_read_end
;
1036 marker
->_pos
= fp
->_IO_read_ptr
- fp
->_IO_read_base
;
1038 /* Should perhaps sort the chain? */
1039 marker
->_next
= fp
->_markers
;
1040 fp
->_markers
= marker
;
1044 _IO_remove_marker (marker
)
1045 struct _IO_marker
*marker
;
1047 /* Unlink from sb's chain. */
1048 struct _IO_marker
**ptr
= &marker
->_sbuf
->_markers
;
1049 for (; ; ptr
= &(*ptr
)->_next
)
1053 else if (*ptr
== marker
)
1055 *ptr
= marker
->_next
;
1060 if _sbuf has a backup area that is no longer needed
, should we
delete
1061 it now
, or wait until the next underflow
?
1065 #define BAD_DELTA EOF
1068 _IO_marker_difference (mark1
, mark2
)
1069 struct _IO_marker
*mark1
;
1070 struct _IO_marker
*mark2
;
1072 return mark1
->_pos
- mark2
->_pos
;
1075 /* Return difference between MARK and current position of MARK's stream. */
1077 _IO_marker_delta (mark
)
1078 struct _IO_marker
*mark
;
1081 if (mark
->_sbuf
== NULL
)
1083 if (_IO_in_backup (mark
->_sbuf
))
1084 cur_pos
= mark
->_sbuf
->_IO_read_ptr
- mark
->_sbuf
->_IO_read_end
;
1086 cur_pos
= mark
->_sbuf
->_IO_read_ptr
- mark
->_sbuf
->_IO_read_base
;
1087 return mark
->_pos
- cur_pos
;
1091 _IO_seekmark (fp
, mark
, delta
)
1093 struct _IO_marker
*mark
;
1096 if (mark
->_sbuf
!= fp
)
1098 if (mark
->_pos
>= 0)
1100 if (_IO_in_backup (fp
))
1101 _IO_switch_to_main_get_area (fp
);
1102 fp
->_IO_read_ptr
= fp
->_IO_read_base
+ mark
->_pos
;
1106 if (!_IO_in_backup (fp
))
1107 _IO_switch_to_backup_area (fp
);
1108 fp
->_IO_read_ptr
= fp
->_IO_read_end
+ mark
->_pos
;
1114 _IO_unsave_markers (fp
)
1117 struct _IO_marker
*mark
= fp
->_markers
;
1121 streampos offset
= seekoff (0, ios::cur
, ios::in
);
1124 offset
+= eGptr () - Gbase ();
1125 for ( ; mark
!= NULL
; mark
= mark
->_next
)
1126 mark
->set_streampos (mark
->_pos
+ offset
);
1130 for ( ; mark
!= NULL
; mark
= mark
->_next
)
1131 mark
->set_streampos (EOF
);
1137 if (_IO_have_backup (fp
))
1138 INTUSE(_IO_free_backup_area
) (fp
);
1140 INTDEF(_IO_unsave_markers
)
1143 /* Seems not to be needed. --drepper */
1145 _IO_nobackup_pbackfail (fp
, c
)
1149 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
)
1151 if (c
!= EOF
&& *fp
->_IO_read_ptr
!= c
)
1152 *fp
->_IO_read_ptr
= c
;
1153 return (unsigned char) c
;
1158 _IO_default_pbackfail (fp
, c
)
1162 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
&& !_IO_in_backup (fp
)
1163 && (unsigned char) fp
->_IO_read_ptr
[-1] == c
)
1167 /* Need to handle a filebuf in write mode (switch to read mode). FIXME!*/
1168 if (!_IO_in_backup (fp
))
1170 /* We need to keep the invariant that the main get area
1171 logically follows the backup area. */
1172 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
&& _IO_have_backup (fp
))
1174 if (save_for_backup (fp
, fp
->_IO_read_ptr
))
1177 else if (!_IO_have_backup (fp
))
1179 /* No backup buffer: allocate one. */
1180 /* Use nshort buffer, if unused? (probably not) FIXME */
1181 int backup_size
= 128;
1182 char *bbuf
= (char *) malloc (backup_size
);
1185 fp
->_IO_save_base
= bbuf
;
1186 fp
->_IO_save_end
= fp
->_IO_save_base
+ backup_size
;
1187 fp
->_IO_backup_base
= fp
->_IO_save_end
;
1189 fp
->_IO_read_base
= fp
->_IO_read_ptr
;
1190 _IO_switch_to_backup_area (fp
);
1192 else if (fp
->_IO_read_ptr
<= fp
->_IO_read_base
)
1194 /* Increase size of existing backup buffer. */
1195 _IO_size_t new_size
;
1196 _IO_size_t old_size
= fp
->_IO_read_end
- fp
->_IO_read_base
;
1198 new_size
= 2 * old_size
;
1199 new_buf
= (char *) malloc (new_size
);
1200 if (new_buf
== NULL
)
1202 memcpy (new_buf
+ (new_size
- old_size
), fp
->_IO_read_base
,
1204 free (fp
->_IO_read_base
);
1205 _IO_setg (fp
, new_buf
, new_buf
+ (new_size
- old_size
),
1206 new_buf
+ new_size
);
1207 fp
->_IO_backup_base
= fp
->_IO_read_ptr
;
1210 *--fp
->_IO_read_ptr
= c
;
1212 return (unsigned char) c
;
1214 INTDEF(_IO_default_pbackfail
)
1217 _IO_default_seek (fp
, offset
, dir
)
1226 _IO_default_stat (fp
, st
)
1234 _IO_default_read (fp
, data
, n
)
1243 _IO_default_write (fp
, data
, n
)
1252 _IO_default_showmanyc (fp
)
1259 _IO_default_imbue (fp
, locale
)
1268 return (_IO_ITER
) INTUSE(_IO_list_all
);
1270 libc_hidden_def (_IO_iter_begin
)
1277 libc_hidden_def (_IO_iter_end
)
1283 return iter
->_chain
;
1285 libc_hidden_def (_IO_iter_next
)
1293 libc_hidden_def (_IO_iter_file
)
1298 #ifdef _IO_MTSAFE_IO
1299 _IO_lock_lock (list_all_lock
);
1302 libc_hidden_def (_IO_list_lock
)
1307 #ifdef _IO_MTSAFE_IO
1308 _IO_lock_unlock (list_all_lock
);
1311 libc_hidden_def (_IO_list_unlock
)
1314 _IO_list_resetlock()
1316 #ifdef _IO_MTSAFE_IO
1317 _IO_lock_init (list_all_lock
);
1320 libc_hidden_def (_IO_list_resetlock
)
1325 #define IO_CLEANUP ;
1333 ~__io_defs() { _IO_cleanup (); }
1335 __io_defs io_defs__
;
1340 #ifdef text_set_element
1341 text_set_element(__libc_atexit
, _IO_cleanup
);