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 (INTUSE(_IO_list_all
) == NULL
)
73 else if (fp
== INTUSE(_IO_list_all
))
76 = (struct _IO_FILE_plus
*) INTUSE(_IO_list_all
)->file
._chain
;
80 for (f
= &INTUSE(_IO_list_all
)->file
._chain
; *f
; f
= &(*f
)->_chain
)
81 if (*f
== (_IO_FILE
*) fp
)
87 fp
->file
._flags
&= ~_IO_LINKED
;
89 _IO_funlockfile ((_IO_FILE
*) fp
);
91 _IO_lock_unlock (list_all_lock
);
92 _IO_cleanup_region_end (0);
100 struct _IO_FILE_plus
*fp
;
102 if ((fp
->file
._flags
& _IO_LINKED
) == 0)
104 fp
->file
._flags
|= _IO_LINKED
;
106 _IO_cleanup_region_start_noarg (flush_cleanup
);
107 _IO_lock_lock (list_all_lock
);
108 run_fp
= (_IO_FILE
*) fp
;
109 _IO_flockfile ((_IO_FILE
*) fp
);
111 fp
->file
._chain
= (_IO_FILE
*) INTUSE(_IO_list_all
);
112 INTUSE(_IO_list_all
) = fp
;
113 ++_IO_list_all_stamp
;
115 _IO_funlockfile ((_IO_FILE
*) fp
);
117 _IO_lock_unlock (list_all_lock
);
118 _IO_cleanup_region_end (0);
124 /* Return minimum _pos markers
125 Assumes the current get area is the main get area. */
126 _IO_ssize_t
_IO_least_marker (_IO_FILE
*fp
, char *end_p
);
129 _IO_least_marker (fp
, end_p
)
133 _IO_ssize_t least_so_far
= end_p
- fp
->_IO_read_base
;
134 struct _IO_marker
*mark
;
135 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
136 if (mark
->_pos
< least_so_far
)
137 least_so_far
= mark
->_pos
;
141 /* Switch current get area from backup buffer to (start of) main get area. */
144 _IO_switch_to_main_get_area (fp
)
148 fp
->_flags
&= ~_IO_IN_BACKUP
;
149 /* Swap _IO_read_end and _IO_save_end. */
150 tmp
= fp
->_IO_read_end
;
151 fp
->_IO_read_end
= fp
->_IO_save_end
;
152 fp
->_IO_save_end
= tmp
;
153 /* Swap _IO_read_base and _IO_save_base. */
154 tmp
= fp
->_IO_read_base
;
155 fp
->_IO_read_base
= fp
->_IO_save_base
;
156 fp
->_IO_save_base
= tmp
;
157 /* Set _IO_read_ptr. */
158 fp
->_IO_read_ptr
= fp
->_IO_read_base
;
161 /* Switch current get area from main get area to (end of) backup area. */
164 _IO_switch_to_backup_area (fp
)
168 fp
->_flags
|= _IO_IN_BACKUP
;
169 /* Swap _IO_read_end and _IO_save_end. */
170 tmp
= fp
->_IO_read_end
;
171 fp
->_IO_read_end
= fp
->_IO_save_end
;
172 fp
->_IO_save_end
= tmp
;
173 /* Swap _IO_read_base and _IO_save_base. */
174 tmp
= fp
->_IO_read_base
;
175 fp
->_IO_read_base
= fp
->_IO_save_base
;
176 fp
->_IO_save_base
= tmp
;
177 /* Set _IO_read_ptr. */
178 fp
->_IO_read_ptr
= fp
->_IO_read_end
;
182 _IO_switch_to_get_mode (fp
)
185 if (fp
->_IO_write_ptr
> fp
->_IO_write_base
)
186 if (_IO_OVERFLOW (fp
, EOF
) == EOF
)
188 if (_IO_in_backup (fp
))
189 fp
->_IO_read_base
= fp
->_IO_backup_base
;
192 fp
->_IO_read_base
= fp
->_IO_buf_base
;
193 if (fp
->_IO_write_ptr
> fp
->_IO_read_end
)
194 fp
->_IO_read_end
= fp
->_IO_write_ptr
;
196 fp
->_IO_read_ptr
= fp
->_IO_write_ptr
;
198 fp
->_IO_write_base
= fp
->_IO_write_ptr
= fp
->_IO_write_end
= fp
->_IO_read_ptr
;
200 fp
->_flags
&= ~_IO_CURRENTLY_PUTTING
;
203 INTDEF(_IO_switch_to_get_mode
)
206 _IO_free_backup_area (fp
)
209 if (_IO_in_backup (fp
))
210 _IO_switch_to_main_get_area (fp
); /* Just in case. */
211 free (fp
->_IO_save_base
);
212 fp
->_IO_save_base
= NULL
;
213 fp
->_IO_save_end
= NULL
;
214 fp
->_IO_backup_base
= NULL
;
216 INTDEF(_IO_free_backup_area
)
220 _IO_switch_to_put_mode (fp
)
223 fp
->_IO_write_base
= fp
->_IO_read_ptr
;
224 fp
->_IO_write_ptr
= fp
->_IO_read_ptr
;
225 /* Following is wrong if line- or un-buffered? */
226 fp
->_IO_write_end
= (fp
->_flags
& _IO_IN_BACKUP
227 ? fp
->_IO_read_end
: fp
->_IO_buf_end
);
229 fp
->_IO_read_ptr
= fp
->_IO_read_end
;
230 fp
->_IO_read_base
= fp
->_IO_read_end
;
232 fp
->_flags
|= _IO_CURRENTLY_PUTTING
;
242 /* This is a single-byte stream. */
245 return _IO_OVERFLOW (f
, ch
);
247 libc_hidden_def (__overflow
)
249 static int save_for_backup (_IO_FILE
*fp
, char *end_p
)
259 save_for_backup (fp
, end_p
)
263 /* Append [_IO_read_base..end_p] to backup area. */
264 _IO_ssize_t least_mark
= _IO_least_marker (fp
, end_p
);
265 /* needed_size is how much space we need in the backup area. */
266 _IO_size_t needed_size
= (end_p
- fp
->_IO_read_base
) - least_mark
;
267 /* FIXME: Dubious arithmetic if pointers are NULL */
268 _IO_size_t current_Bsize
= fp
->_IO_save_end
- fp
->_IO_save_base
;
269 _IO_size_t avail
; /* Extra space available for future expansion. */
271 struct _IO_marker
*mark
;
272 if (needed_size
> current_Bsize
)
276 new_buffer
= (char *) malloc (avail
+ needed_size
);
277 if (new_buffer
== NULL
)
278 return EOF
; /* FIXME */
282 __mempcpy (__mempcpy (new_buffer
+ avail
,
283 fp
->_IO_save_end
+ least_mark
,
286 end_p
- fp
->_IO_read_base
);
288 memcpy (new_buffer
+ avail
,
289 fp
->_IO_save_end
+ least_mark
,
291 memcpy (new_buffer
+ avail
- least_mark
,
293 end_p
- fp
->_IO_read_base
);
297 memcpy (new_buffer
+ avail
,
298 fp
->_IO_read_base
+ least_mark
,
300 free (fp
->_IO_save_base
);
301 fp
->_IO_save_base
= new_buffer
;
302 fp
->_IO_save_end
= new_buffer
+ avail
+ needed_size
;
306 avail
= current_Bsize
- needed_size
;
309 memmove (fp
->_IO_save_base
+ avail
,
310 fp
->_IO_save_end
+ least_mark
,
312 memcpy (fp
->_IO_save_base
+ avail
- least_mark
,
314 end_p
- fp
->_IO_read_base
);
316 else if (needed_size
> 0)
317 memcpy (fp
->_IO_save_base
+ avail
,
318 fp
->_IO_read_base
+ least_mark
,
321 fp
->_IO_backup_base
= fp
->_IO_save_base
+ avail
;
322 /* Adjust all the streammarkers. */
323 delta
= end_p
- fp
->_IO_read_base
;
324 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
333 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
334 if (_IO_vtable_offset (fp
) == 0 && _IO_fwide (fp
, -1) != -1)
340 if (_IO_in_put_mode (fp
))
341 if (INTUSE(_IO_switch_to_get_mode
) (fp
) == EOF
)
343 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
344 return *(unsigned char *) fp
->_IO_read_ptr
;
345 if (_IO_in_backup (fp
))
347 _IO_switch_to_main_get_area (fp
);
348 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
349 return *(unsigned char *) fp
->_IO_read_ptr
;
351 if (_IO_have_markers (fp
))
353 if (save_for_backup (fp
, fp
->_IO_read_end
))
356 else if (_IO_have_backup (fp
))
357 INTUSE(_IO_free_backup_area
) (fp
);
358 return _IO_UNDERFLOW (fp
);
360 libc_hidden_def (__underflow
)
366 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
367 if (_IO_vtable_offset (fp
) == 0 && _IO_fwide (fp
, -1) != -1)
373 if (_IO_in_put_mode (fp
))
374 if (INTUSE(_IO_switch_to_get_mode
) (fp
) == EOF
)
376 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
377 return *(unsigned char *) fp
->_IO_read_ptr
++;
378 if (_IO_in_backup (fp
))
380 _IO_switch_to_main_get_area (fp
);
381 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
382 return *(unsigned char *) fp
->_IO_read_ptr
++;
384 if (_IO_have_markers (fp
))
386 if (save_for_backup (fp
, fp
->_IO_read_end
))
389 else if (_IO_have_backup (fp
))
390 INTUSE(_IO_free_backup_area
) (fp
);
391 return _IO_UFLOW (fp
);
393 libc_hidden_def (__uflow
)
396 _IO_setb (f
, b
, eb
, a
)
402 if (f
->_IO_buf_base
&& !(f
->_flags
& _IO_USER_BUF
))
403 FREE_BUF (f
->_IO_buf_base
, _IO_blen (f
));
407 f
->_flags
&= ~_IO_USER_BUF
;
409 f
->_flags
|= _IO_USER_BUF
;
417 if (fp
->_IO_buf_base
)
419 if (!(fp
->_flags
& _IO_UNBUFFERED
) || fp
->_mode
> 0)
420 if (_IO_DOALLOCATE (fp
) != EOF
)
422 INTUSE(_IO_setb
) (fp
, fp
->_shortbuf
, fp
->_shortbuf
+1, 0);
424 INTDEF(_IO_doallocbuf
)
427 _IO_default_underflow (fp
)
434 _IO_default_uflow (fp
)
437 int ch
= _IO_UNDERFLOW (fp
);
440 return *(unsigned char *) fp
->_IO_read_ptr
++;
442 INTDEF(_IO_default_uflow
)
445 _IO_default_xsputn (f
, data
, n
)
450 const char *s
= (char *) data
;
456 /* Space available. */
457 if (f
->_IO_write_ptr
< f
->_IO_write_end
)
459 _IO_size_t count
= f
->_IO_write_end
- f
->_IO_write_ptr
;
465 f
->_IO_write_ptr
= __mempcpy (f
->_IO_write_ptr
, s
, count
);
467 memcpy (f
->_IO_write_ptr
, s
, count
);
468 f
->_IO_write_ptr
+= count
;
474 char *p
= f
->_IO_write_ptr
;
476 for (i
= count
; --i
>= 0; )
478 f
->_IO_write_ptr
= p
;
482 if (more
== 0 || _IO_OVERFLOW (f
, (unsigned char) *s
++) == EOF
)
488 INTDEF(_IO_default_xsputn
)
491 _IO_sgetn (fp
, data
, n
)
496 /* FIXME handle putback buffer here! */
497 return _IO_XSGETN (fp
, data
, n
);
502 _IO_default_xsgetn (fp
, data
, n
)
508 char *s
= (char*) data
;
511 /* Data available. */
512 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
514 _IO_size_t count
= fp
->_IO_read_end
- fp
->_IO_read_ptr
;
520 s
= __mempcpy (s
, fp
->_IO_read_ptr
, count
);
522 memcpy (s
, fp
->_IO_read_ptr
, count
);
525 fp
->_IO_read_ptr
+= count
;
529 char *p
= fp
->_IO_read_ptr
;
533 fp
->_IO_read_ptr
= p
;
537 if (more
== 0 || __underflow (fp
) == EOF
)
542 INTDEF(_IO_default_xsgetn
)
545 /* Seems not to be needed. --drepper */
555 _IO_default_setbuf (fp
, p
, len
)
560 if (_IO_SYNC (fp
) == EOF
)
562 if (p
== NULL
|| len
== 0)
564 fp
->_flags
|= _IO_UNBUFFERED
;
565 INTUSE(_IO_setb
) (fp
, fp
->_shortbuf
, fp
->_shortbuf
+1, 0);
569 fp
->_flags
&= ~_IO_UNBUFFERED
;
570 INTUSE(_IO_setb
) (fp
, p
, p
+len
, 0);
572 fp
->_IO_write_base
= fp
->_IO_write_ptr
= fp
->_IO_write_end
= 0;
573 fp
->_IO_read_base
= fp
->_IO_read_ptr
= fp
->_IO_read_end
= 0;
578 _IO_default_seekpos (fp
, pos
, mode
)
583 return _IO_SEEKOFF (fp
, pos
, 0, mode
);
587 _IO_default_doallocate (fp
)
592 ALLOC_BUF (buf
, _IO_BUFSIZ
, EOF
);
593 INTUSE(_IO_setb
) (fp
, buf
, buf
+_IO_BUFSIZ
, 1);
596 INTDEF(_IO_default_doallocate
)
603 _IO_no_init (fp
, flags
, -1, NULL
, NULL
);
608 _IO_old_init (fp
, flags
)
612 fp
->_flags
= _IO_MAGIC
|flags
;
614 fp
->_IO_buf_base
= NULL
;
615 fp
->_IO_buf_end
= NULL
;
616 fp
->_IO_read_base
= NULL
;
617 fp
->_IO_read_ptr
= NULL
;
618 fp
->_IO_read_end
= NULL
;
619 fp
->_IO_write_base
= NULL
;
620 fp
->_IO_write_ptr
= NULL
;
621 fp
->_IO_write_end
= NULL
;
622 fp
->_chain
= NULL
; /* Not necessary. */
624 fp
->_IO_save_base
= NULL
;
625 fp
->_IO_backup_base
= NULL
;
626 fp
->_IO_save_end
= NULL
;
630 fp
->_vtable_offset
= 0;
633 if (fp
->_lock
!= NULL
)
634 _IO_lock_init (*fp
->_lock
);
639 _IO_no_init (fp
, flags
, orientation
, wd
, jmp
)
643 struct _IO_wide_data
*wd
;
644 const struct _IO_jump_t
*jmp
;
646 _IO_old_init (fp
, flags
);
647 fp
->_mode
= orientation
;
648 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
649 if (orientation
>= 0)
652 fp
->_wide_data
->_IO_buf_base
= NULL
;
653 fp
->_wide_data
->_IO_buf_end
= NULL
;
654 fp
->_wide_data
->_IO_read_base
= NULL
;
655 fp
->_wide_data
->_IO_read_ptr
= NULL
;
656 fp
->_wide_data
->_IO_read_end
= NULL
;
657 fp
->_wide_data
->_IO_write_base
= NULL
;
658 fp
->_wide_data
->_IO_write_ptr
= NULL
;
659 fp
->_wide_data
->_IO_write_end
= NULL
;
660 fp
->_wide_data
->_IO_save_base
= NULL
;
661 fp
->_wide_data
->_IO_backup_base
= NULL
;
662 fp
->_wide_data
->_IO_save_end
= NULL
;
664 fp
->_wide_data
->_wide_vtable
= jmp
;
667 fp
->_freeres_list
= NULL
;
671 _IO_default_sync (fp
)
677 /* The way the C++ classes are mapped into the C functions in the
678 current implementation, this function can get called twice! */
681 _IO_default_finish (fp
, dummy
)
685 struct _IO_marker
*mark
;
686 if (fp
->_IO_buf_base
&& !(fp
->_flags
& _IO_USER_BUF
))
688 FREE_BUF (fp
->_IO_buf_base
, _IO_blen (fp
));
689 fp
->_IO_buf_base
= fp
->_IO_buf_end
= NULL
;
692 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
695 if (fp
->_IO_save_base
)
697 free (fp
->_IO_save_base
);
698 fp
->_IO_save_base
= NULL
;
701 INTUSE(_IO_un_link
) ((struct _IO_FILE_plus
*) fp
);
704 if (fp
->_lock
!= NULL
)
705 _IO_lock_fini (*fp
->_lock
);
708 INTDEF(_IO_default_finish
)
711 _IO_default_seekoff (fp
, offset
, dir
, mode
)
721 _IO_sputbackc (fp
, c
)
727 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
728 && (unsigned char)fp
->_IO_read_ptr
[-1] == (unsigned char)c
)
731 result
= (unsigned char) c
;
734 result
= _IO_PBACKFAIL (fp
, c
);
737 fp
->_flags
&= ~_IO_EOF_SEEN
;
741 INTDEF(_IO_sputbackc
)
749 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
)
752 result
= (unsigned char) *fp
->_IO_read_ptr
;
755 result
= _IO_PBACKFAIL (fp
, EOF
);
758 fp
->_flags
&= ~_IO_EOF_SEEN
;
763 #if 0 /* Work in progress */
764 /* Seems not to be needed. */
767 _IO_set_column (fp
, c
)
774 fp
->_column
= c
- (fp
->_IO_write_ptr
- fp
->_IO_write_base
);
778 _IO_set_column (fp
, i
)
782 fp
->_cur_column
= i
+ 1;
790 _IO_adjust_column (start
, line
, count
)
795 const char *ptr
= line
+ count
;
798 return line
+ count
- ptr
- 1;
799 return start
+ count
;
801 INTDEF(_IO_adjust_column
)
804 /* Seems not to be needed. --drepper */
810 return _IO_adjust_column (fp
->_cur_column
- 1,
812 fp
->_IO_write_ptr
- fp
->_IO_write_base
);
819 _IO_flush_all_lockp (int do_lock
)
826 __libc_cleanup_region_start (do_lock
, flush_cleanup
, 0);
828 _IO_lock_lock (list_all_lock
);
831 last_stamp
= _IO_list_all_stamp
;
832 fp
= (_IO_FILE
*) INTUSE(_IO_list_all
);
839 if (((fp
->_mode
<= 0 && fp
->_IO_write_ptr
> fp
->_IO_write_base
)
840 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
841 || (_IO_vtable_offset (fp
) == 0
842 && fp
->_mode
> 0 && (fp
->_wide_data
->_IO_write_ptr
843 > fp
->_wide_data
->_IO_write_base
))
846 && _IO_OVERFLOW (fp
, EOF
) == EOF
)
850 _IO_funlockfile (fp
);
853 if (last_stamp
!= _IO_list_all_stamp
)
855 /* Something was added to the list. Start all over again. */
856 fp
= (_IO_FILE
*) INTUSE(_IO_list_all
);
857 last_stamp
= _IO_list_all_stamp
;
865 _IO_lock_unlock (list_all_lock
);
866 __libc_cleanup_region_end (0);
876 /* We want locking. */
877 return _IO_flush_all_lockp (1);
879 INTDEF(_IO_flush_all
)
882 _IO_flush_all_linebuffered ()
888 _IO_cleanup_region_start_noarg (flush_cleanup
);
889 _IO_lock_lock (list_all_lock
);
892 last_stamp
= _IO_list_all_stamp
;
893 fp
= (_IO_FILE
*) INTUSE(_IO_list_all
);
899 if ((fp
->_flags
& _IO_NO_WRITES
) == 0 && fp
->_flags
& _IO_LINE_BUF
)
900 _IO_OVERFLOW (fp
, EOF
);
902 _IO_funlockfile (fp
);
905 if (last_stamp
!= _IO_list_all_stamp
)
907 /* Something was added to the list. Start all over again. */
908 fp
= (_IO_FILE
*) INTUSE(_IO_list_all
);
909 last_stamp
= _IO_list_all_stamp
;
916 _IO_lock_unlock (list_all_lock
);
917 _IO_cleanup_region_end (0);
920 INTDEF(_IO_flush_all_linebuffered
)
922 weak_alias (_IO_flush_all_linebuffered
, _flushlbf
)
926 /* The following is a bit tricky. In general, we want to unbuffer the
927 streams so that all output which follows is seen. If we are not
928 looking for memory leaks it does not make much sense to free the
929 actual buffer because this will happen anyway once the program
930 terminated. If we do want to look for memory leaks we have to free
931 the buffers. Whether something is freed is determined by the
932 function sin the libc_freeres section. Those are called as part of
933 the atexit routine, just like _IO_cleanup. The problem is we do
934 not know whether the freeres code is called first or _IO_cleanup.
935 if the former is the case, we set the DEALLOC_BUFFER variable to
936 true and _IO_unbuffer_write will take care of the rest. If
937 _IO_unbuffer_write is called first we add the streams to a list
938 which the freeres function later can walk through. */
939 static void _IO_unbuffer_write (void);
941 static bool dealloc_buffers
;
942 static _IO_FILE
*freeres_list
;
945 _IO_unbuffer_write (void)
948 for (fp
= (_IO_FILE
*) INTUSE(_IO_list_all
); fp
; fp
= fp
->_chain
)
950 if (! (fp
->_flags
& _IO_UNBUFFERED
)
951 && (! (fp
->_flags
& _IO_NO_WRITES
)
952 || (fp
->_flags
& _IO_IS_APPENDING
))
953 /* Iff stream is un-orientated, it wasn't used. */
958 for (cnt
= 0; cnt
< MAXTRIES
; ++cnt
)
959 if (fp
->_lock
== NULL
|| _IO_lock_trylock (*fp
->_lock
) == 0)
962 /* Give the other thread time to finish up its use of the
966 if (! dealloc_buffers
&& !(fp
->_flags
& _IO_USER_BUF
))
968 fp
->_flags
|= _IO_USER_BUF
;
970 fp
->_freeres_list
= freeres_list
;
972 fp
->_freeres_buf
= fp
->_IO_buf_base
;
973 fp
->_freeres_size
= _IO_blen (fp
);
976 _IO_SETBUF (fp
, NULL
, 0);
978 if (cnt
< MAXTRIES
&& fp
->_lock
!= NULL
)
979 _IO_lock_unlock (*fp
->_lock
);
982 /* Make sure that never again the wide char functions can be
989 libc_freeres_fn (buffer_free
)
991 dealloc_buffers
= true;
993 while (freeres_list
!= NULL
)
995 FREE_BUF (freeres_list
->_freeres_buf
, freeres_list
->_freeres_size
);
997 freeres_list
= freeres_list
->_freeres_list
;
1005 /* We do *not* want locking. Some threads might use streams but
1006 that is their problem, we flush them underneath them. */
1007 int result
= _IO_flush_all_lockp (0);
1009 /* We currently don't have a reliable mechanism for making sure that
1010 C++ static destructors are executed in the correct order.
1011 So it is possible that other static destructors might want to
1012 write to cout - and they're supposed to be able to do so.
1014 The following will make the standard streambufs be unbuffered,
1015 which forces any output from late destructors to be written out. */
1016 _IO_unbuffer_write ();
1023 _IO_init_marker (marker
, fp
)
1024 struct _IO_marker
*marker
;
1028 if (_IO_in_put_mode (fp
))
1029 INTUSE(_IO_switch_to_get_mode
) (fp
);
1030 if (_IO_in_backup (fp
))
1031 marker
->_pos
= fp
->_IO_read_ptr
- fp
->_IO_read_end
;
1033 marker
->_pos
= fp
->_IO_read_ptr
- fp
->_IO_read_base
;
1035 /* Should perhaps sort the chain? */
1036 marker
->_next
= fp
->_markers
;
1037 fp
->_markers
= marker
;
1041 _IO_remove_marker (marker
)
1042 struct _IO_marker
*marker
;
1044 /* Unlink from sb's chain. */
1045 struct _IO_marker
**ptr
= &marker
->_sbuf
->_markers
;
1046 for (; ; ptr
= &(*ptr
)->_next
)
1050 else if (*ptr
== marker
)
1052 *ptr
= marker
->_next
;
1057 if _sbuf has a backup area that is no longer needed
, should we
delete
1058 it now
, or wait until the next underflow
?
1062 #define BAD_DELTA EOF
1065 _IO_marker_difference (mark1
, mark2
)
1066 struct _IO_marker
*mark1
;
1067 struct _IO_marker
*mark2
;
1069 return mark1
->_pos
- mark2
->_pos
;
1072 /* Return difference between MARK and current position of MARK's stream. */
1074 _IO_marker_delta (mark
)
1075 struct _IO_marker
*mark
;
1078 if (mark
->_sbuf
== NULL
)
1080 if (_IO_in_backup (mark
->_sbuf
))
1081 cur_pos
= mark
->_sbuf
->_IO_read_ptr
- mark
->_sbuf
->_IO_read_end
;
1083 cur_pos
= mark
->_sbuf
->_IO_read_ptr
- mark
->_sbuf
->_IO_read_base
;
1084 return mark
->_pos
- cur_pos
;
1088 _IO_seekmark (fp
, mark
, delta
)
1090 struct _IO_marker
*mark
;
1093 if (mark
->_sbuf
!= fp
)
1095 if (mark
->_pos
>= 0)
1097 if (_IO_in_backup (fp
))
1098 _IO_switch_to_main_get_area (fp
);
1099 fp
->_IO_read_ptr
= fp
->_IO_read_base
+ mark
->_pos
;
1103 if (!_IO_in_backup (fp
))
1104 _IO_switch_to_backup_area (fp
);
1105 fp
->_IO_read_ptr
= fp
->_IO_read_end
+ mark
->_pos
;
1111 _IO_unsave_markers (fp
)
1114 struct _IO_marker
*mark
= fp
->_markers
;
1118 streampos offset
= seekoff (0, ios::cur
, ios::in
);
1121 offset
+= eGptr () - Gbase ();
1122 for ( ; mark
!= NULL
; mark
= mark
->_next
)
1123 mark
->set_streampos (mark
->_pos
+ offset
);
1127 for ( ; mark
!= NULL
; mark
= mark
->_next
)
1128 mark
->set_streampos (EOF
);
1134 if (_IO_have_backup (fp
))
1135 INTUSE(_IO_free_backup_area
) (fp
);
1137 INTDEF(_IO_unsave_markers
)
1140 /* Seems not to be needed. --drepper */
1142 _IO_nobackup_pbackfail (fp
, c
)
1146 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
)
1148 if (c
!= EOF
&& *fp
->_IO_read_ptr
!= c
)
1149 *fp
->_IO_read_ptr
= c
;
1150 return (unsigned char) c
;
1155 _IO_default_pbackfail (fp
, c
)
1159 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
&& !_IO_in_backup (fp
)
1160 && (unsigned char) fp
->_IO_read_ptr
[-1] == c
)
1164 /* Need to handle a filebuf in write mode (switch to read mode). FIXME!*/
1165 if (!_IO_in_backup (fp
))
1167 /* We need to keep the invariant that the main get area
1168 logically follows the backup area. */
1169 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
&& _IO_have_backup (fp
))
1171 if (save_for_backup (fp
, fp
->_IO_read_ptr
))
1174 else if (!_IO_have_backup (fp
))
1176 /* No backup buffer: allocate one. */
1177 /* Use nshort buffer, if unused? (probably not) FIXME */
1178 int backup_size
= 128;
1179 char *bbuf
= (char *) malloc (backup_size
);
1182 fp
->_IO_save_base
= bbuf
;
1183 fp
->_IO_save_end
= fp
->_IO_save_base
+ backup_size
;
1184 fp
->_IO_backup_base
= fp
->_IO_save_end
;
1186 fp
->_IO_read_base
= fp
->_IO_read_ptr
;
1187 _IO_switch_to_backup_area (fp
);
1189 else if (fp
->_IO_read_ptr
<= fp
->_IO_read_base
)
1191 /* Increase size of existing backup buffer. */
1192 _IO_size_t new_size
;
1193 _IO_size_t old_size
= fp
->_IO_read_end
- fp
->_IO_read_base
;
1195 new_size
= 2 * old_size
;
1196 new_buf
= (char *) malloc (new_size
);
1197 if (new_buf
== NULL
)
1199 memcpy (new_buf
+ (new_size
- old_size
), fp
->_IO_read_base
,
1201 free (fp
->_IO_read_base
);
1202 _IO_setg (fp
, new_buf
, new_buf
+ (new_size
- old_size
),
1203 new_buf
+ new_size
);
1204 fp
->_IO_backup_base
= fp
->_IO_read_ptr
;
1207 *--fp
->_IO_read_ptr
= c
;
1209 return (unsigned char) c
;
1211 INTDEF(_IO_default_pbackfail
)
1214 _IO_default_seek (fp
, offset
, dir
)
1223 _IO_default_stat (fp
, st
)
1231 _IO_default_read (fp
, data
, n
)
1240 _IO_default_write (fp
, data
, n
)
1249 _IO_default_showmanyc (fp
)
1256 _IO_default_imbue (fp
, locale
)
1265 return (_IO_ITER
) INTUSE(_IO_list_all
);
1267 libc_hidden_def (_IO_iter_begin
)
1274 libc_hidden_def (_IO_iter_end
)
1280 return iter
->_chain
;
1282 libc_hidden_def (_IO_iter_next
)
1290 libc_hidden_def (_IO_iter_file
)
1295 #ifdef _IO_MTSAFE_IO
1296 _IO_lock_lock (list_all_lock
);
1299 libc_hidden_def (_IO_list_lock
)
1304 #ifdef _IO_MTSAFE_IO
1305 _IO_lock_unlock (list_all_lock
);
1308 libc_hidden_def (_IO_list_unlock
)
1311 _IO_list_resetlock()
1313 #ifdef _IO_MTSAFE_IO
1314 _IO_lock_init (list_all_lock
);
1317 libc_hidden_def (_IO_list_resetlock
)
1322 #define IO_CLEANUP ;
1330 ~__io_defs() { _IO_cleanup (); }
1332 __io_defs io_defs__
;
1337 #ifdef text_set_element
1338 text_set_element(__libc_atexit
, _IO_cleanup
);