1 /* Copyright (C) 1993,1995,1997-2002, 2003, 2004, 2006
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
)
67 struct _IO_FILE_plus
**f
;
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 for (f
= &INTUSE(_IO_list_all
); *f
;
75 f
= (struct _IO_FILE_plus
**) &(*f
)->file
._chain
)
79 *f
= (struct _IO_FILE_plus
*) fp
->file
._chain
;
84 fp
->file
._flags
&= ~_IO_LINKED
;
86 _IO_funlockfile ((_IO_FILE
*) fp
);
88 _IO_lock_unlock (list_all_lock
);
89 _IO_cleanup_region_end (0);
97 struct _IO_FILE_plus
*fp
;
99 if ((fp
->file
._flags
& _IO_LINKED
) == 0)
101 fp
->file
._flags
|= _IO_LINKED
;
103 _IO_cleanup_region_start_noarg (flush_cleanup
);
104 _IO_lock_lock (list_all_lock
);
105 run_fp
= (_IO_FILE
*) fp
;
106 _IO_flockfile ((_IO_FILE
*) fp
);
108 fp
->file
._chain
= (_IO_FILE
*) INTUSE(_IO_list_all
);
109 INTUSE(_IO_list_all
) = fp
;
110 ++_IO_list_all_stamp
;
112 _IO_funlockfile ((_IO_FILE
*) fp
);
114 _IO_lock_unlock (list_all_lock
);
115 _IO_cleanup_region_end (0);
121 /* Return minimum _pos markers
122 Assumes the current get area is the main get area. */
123 _IO_ssize_t
_IO_least_marker (_IO_FILE
*fp
, char *end_p
);
126 _IO_least_marker (fp
, end_p
)
130 _IO_ssize_t least_so_far
= end_p
- fp
->_IO_read_base
;
131 struct _IO_marker
*mark
;
132 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
133 if (mark
->_pos
< least_so_far
)
134 least_so_far
= mark
->_pos
;
138 /* Switch current get area from backup buffer to (start of) main get area. */
141 _IO_switch_to_main_get_area (fp
)
145 fp
->_flags
&= ~_IO_IN_BACKUP
;
146 /* Swap _IO_read_end and _IO_save_end. */
147 tmp
= fp
->_IO_read_end
;
148 fp
->_IO_read_end
= fp
->_IO_save_end
;
149 fp
->_IO_save_end
= tmp
;
150 /* Swap _IO_read_base and _IO_save_base. */
151 tmp
= fp
->_IO_read_base
;
152 fp
->_IO_read_base
= fp
->_IO_save_base
;
153 fp
->_IO_save_base
= tmp
;
154 /* Set _IO_read_ptr. */
155 fp
->_IO_read_ptr
= fp
->_IO_read_base
;
158 /* Switch current get area from main get area to (end of) backup area. */
161 _IO_switch_to_backup_area (fp
)
165 fp
->_flags
|= _IO_IN_BACKUP
;
166 /* Swap _IO_read_end and _IO_save_end. */
167 tmp
= fp
->_IO_read_end
;
168 fp
->_IO_read_end
= fp
->_IO_save_end
;
169 fp
->_IO_save_end
= tmp
;
170 /* Swap _IO_read_base and _IO_save_base. */
171 tmp
= fp
->_IO_read_base
;
172 fp
->_IO_read_base
= fp
->_IO_save_base
;
173 fp
->_IO_save_base
= tmp
;
174 /* Set _IO_read_ptr. */
175 fp
->_IO_read_ptr
= fp
->_IO_read_end
;
179 _IO_switch_to_get_mode (fp
)
182 if (fp
->_IO_write_ptr
> fp
->_IO_write_base
)
183 if (_IO_OVERFLOW (fp
, EOF
) == EOF
)
185 if (_IO_in_backup (fp
))
186 fp
->_IO_read_base
= fp
->_IO_backup_base
;
189 fp
->_IO_read_base
= fp
->_IO_buf_base
;
190 if (fp
->_IO_write_ptr
> fp
->_IO_read_end
)
191 fp
->_IO_read_end
= fp
->_IO_write_ptr
;
193 fp
->_IO_read_ptr
= fp
->_IO_write_ptr
;
195 fp
->_IO_write_base
= fp
->_IO_write_ptr
= fp
->_IO_write_end
= fp
->_IO_read_ptr
;
197 fp
->_flags
&= ~_IO_CURRENTLY_PUTTING
;
200 INTDEF(_IO_switch_to_get_mode
)
203 _IO_free_backup_area (fp
)
206 if (_IO_in_backup (fp
))
207 _IO_switch_to_main_get_area (fp
); /* Just in case. */
208 free (fp
->_IO_save_base
);
209 fp
->_IO_save_base
= NULL
;
210 fp
->_IO_save_end
= NULL
;
211 fp
->_IO_backup_base
= NULL
;
213 INTDEF(_IO_free_backup_area
)
217 _IO_switch_to_put_mode (fp
)
220 fp
->_IO_write_base
= fp
->_IO_read_ptr
;
221 fp
->_IO_write_ptr
= fp
->_IO_read_ptr
;
222 /* Following is wrong if line- or un-buffered? */
223 fp
->_IO_write_end
= (fp
->_flags
& _IO_IN_BACKUP
224 ? fp
->_IO_read_end
: fp
->_IO_buf_end
);
226 fp
->_IO_read_ptr
= fp
->_IO_read_end
;
227 fp
->_IO_read_base
= fp
->_IO_read_end
;
229 fp
->_flags
|= _IO_CURRENTLY_PUTTING
;
239 /* This is a single-byte stream. */
242 return _IO_OVERFLOW (f
, ch
);
244 libc_hidden_def (__overflow
)
246 static int save_for_backup (_IO_FILE
*fp
, char *end_p
)
256 save_for_backup (fp
, end_p
)
260 /* Append [_IO_read_base..end_p] to backup area. */
261 _IO_ssize_t least_mark
= _IO_least_marker (fp
, end_p
);
262 /* needed_size is how much space we need in the backup area. */
263 _IO_size_t needed_size
= (end_p
- fp
->_IO_read_base
) - least_mark
;
264 /* FIXME: Dubious arithmetic if pointers are NULL */
265 _IO_size_t current_Bsize
= fp
->_IO_save_end
- fp
->_IO_save_base
;
266 _IO_size_t avail
; /* Extra space available for future expansion. */
268 struct _IO_marker
*mark
;
269 if (needed_size
> current_Bsize
)
273 new_buffer
= (char *) malloc (avail
+ needed_size
);
274 if (new_buffer
== NULL
)
275 return EOF
; /* FIXME */
279 __mempcpy (__mempcpy (new_buffer
+ avail
,
280 fp
->_IO_save_end
+ least_mark
,
283 end_p
- fp
->_IO_read_base
);
285 memcpy (new_buffer
+ avail
,
286 fp
->_IO_save_end
+ least_mark
,
288 memcpy (new_buffer
+ avail
- least_mark
,
290 end_p
- fp
->_IO_read_base
);
294 memcpy (new_buffer
+ avail
,
295 fp
->_IO_read_base
+ least_mark
,
297 if (fp
->_IO_save_base
)
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 (INTUSE(_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 INTUSE(_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 (INTUSE(_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 INTUSE(_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_BUF (f
->_IO_buf_base
, _IO_blen (f
));
405 f
->_flags
&= ~_IO_USER_BUF
;
407 f
->_flags
|= _IO_USER_BUF
;
415 if (fp
->_IO_buf_base
)
417 if (!(fp
->_flags
& _IO_UNBUFFERED
) || fp
->_mode
> 0)
418 if (_IO_DOALLOCATE (fp
) != EOF
)
420 INTUSE(_IO_setb
) (fp
, fp
->_shortbuf
, fp
->_shortbuf
+1, 0);
422 INTDEF(_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 INTDEF(_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 INTDEF(_IO_default_xsputn
)
489 _IO_sgetn (fp
, data
, n
)
494 /* FIXME handle putback buffer here! */
495 return _IO_XSGETN (fp
, data
, n
);
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 INTDEF(_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 INTUSE(_IO_setb
) (fp
, fp
->_shortbuf
, fp
->_shortbuf
+1, 0);
567 fp
->_flags
&= ~_IO_UNBUFFERED
;
568 INTUSE(_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 ALLOC_BUF (buf
, _IO_BUFSIZ
, EOF
);
591 INTUSE(_IO_setb
) (fp
, buf
, buf
+_IO_BUFSIZ
, 1);
594 INTDEF(_IO_default_doallocate
)
601 _IO_no_init (fp
, flags
, -1, NULL
, NULL
);
606 _IO_old_init (fp
, flags
)
610 fp
->_flags
= _IO_MAGIC
|flags
;
612 fp
->_IO_buf_base
= NULL
;
613 fp
->_IO_buf_end
= NULL
;
614 fp
->_IO_read_base
= NULL
;
615 fp
->_IO_read_ptr
= NULL
;
616 fp
->_IO_read_end
= NULL
;
617 fp
->_IO_write_base
= NULL
;
618 fp
->_IO_write_ptr
= NULL
;
619 fp
->_IO_write_end
= NULL
;
620 fp
->_chain
= NULL
; /* Not necessary. */
622 fp
->_IO_save_base
= NULL
;
623 fp
->_IO_backup_base
= NULL
;
624 fp
->_IO_save_end
= NULL
;
628 fp
->_vtable_offset
= 0;
631 if (fp
->_lock
!= NULL
)
632 _IO_lock_init (*fp
->_lock
);
637 _IO_no_init (fp
, flags
, orientation
, wd
, jmp
)
641 struct _IO_wide_data
*wd
;
642 const struct _IO_jump_t
*jmp
;
644 _IO_old_init (fp
, flags
);
645 fp
->_mode
= orientation
;
646 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
647 if (orientation
>= 0)
650 fp
->_wide_data
->_IO_buf_base
= NULL
;
651 fp
->_wide_data
->_IO_buf_end
= NULL
;
652 fp
->_wide_data
->_IO_read_base
= NULL
;
653 fp
->_wide_data
->_IO_read_ptr
= NULL
;
654 fp
->_wide_data
->_IO_read_end
= NULL
;
655 fp
->_wide_data
->_IO_write_base
= NULL
;
656 fp
->_wide_data
->_IO_write_ptr
= NULL
;
657 fp
->_wide_data
->_IO_write_end
= NULL
;
658 fp
->_wide_data
->_IO_save_base
= NULL
;
659 fp
->_wide_data
->_IO_backup_base
= NULL
;
660 fp
->_wide_data
->_IO_save_end
= NULL
;
662 fp
->_wide_data
->_wide_vtable
= jmp
;
665 fp
->_freeres_list
= NULL
;
669 _IO_default_sync (fp
)
675 /* The way the C++ classes are mapped into the C functions in the
676 current implementation, this function can get called twice! */
679 _IO_default_finish (fp
, dummy
)
683 struct _IO_marker
*mark
;
684 if (fp
->_IO_buf_base
&& !(fp
->_flags
& _IO_USER_BUF
))
686 FREE_BUF (fp
->_IO_buf_base
, _IO_blen (fp
));
687 fp
->_IO_buf_base
= fp
->_IO_buf_end
= NULL
;
690 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
693 if (fp
->_IO_save_base
)
695 free (fp
->_IO_save_base
);
696 fp
->_IO_save_base
= NULL
;
700 if (fp
->_lock
!= NULL
)
701 _IO_lock_fini (*fp
->_lock
);
704 INTUSE(_IO_un_link
) ((struct _IO_FILE_plus
*) fp
);
706 INTDEF(_IO_default_finish
)
709 _IO_default_seekoff (fp
, offset
, dir
, mode
)
719 _IO_sputbackc (fp
, c
)
725 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
726 && (unsigned char)fp
->_IO_read_ptr
[-1] == (unsigned char)c
)
729 result
= (unsigned char) c
;
732 result
= _IO_PBACKFAIL (fp
, c
);
735 fp
->_flags
&= ~_IO_EOF_SEEN
;
739 INTDEF(_IO_sputbackc
)
747 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
)
750 result
= (unsigned char) *fp
->_IO_read_ptr
;
753 result
= _IO_PBACKFAIL (fp
, EOF
);
756 fp
->_flags
&= ~_IO_EOF_SEEN
;
761 #if 0 /* Work in progress */
762 /* Seems not to be needed. */
765 _IO_set_column (fp
, c
)
772 fp
->_column
= c
- (fp
->_IO_write_ptr
- fp
->_IO_write_base
);
776 _IO_set_column (fp
, i
)
780 fp
->_cur_column
= i
+ 1;
788 _IO_adjust_column (start
, line
, count
)
793 const char *ptr
= line
+ count
;
796 return line
+ count
- ptr
- 1;
797 return start
+ count
;
799 INTDEF(_IO_adjust_column
)
802 /* Seems not to be needed. --drepper */
808 return _IO_adjust_column (fp
->_cur_column
- 1,
810 fp
->_IO_write_ptr
- fp
->_IO_write_base
);
817 _IO_flush_all_lockp (int do_lock
)
824 _IO_cleanup_region_start_noarg (flush_cleanup
);
826 _IO_lock_lock (list_all_lock
);
829 last_stamp
= _IO_list_all_stamp
;
830 fp
= (_IO_FILE
*) INTUSE(_IO_list_all
);
837 if (((fp
->_mode
<= 0 && fp
->_IO_write_ptr
> fp
->_IO_write_base
)
838 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
839 || (_IO_vtable_offset (fp
) == 0
840 && fp
->_mode
> 0 && (fp
->_wide_data
->_IO_write_ptr
841 > fp
->_wide_data
->_IO_write_base
))
844 && _IO_OVERFLOW (fp
, EOF
) == EOF
)
848 _IO_funlockfile (fp
);
851 if (last_stamp
!= _IO_list_all_stamp
)
853 /* Something was added to the list. Start all over again. */
854 fp
= (_IO_FILE
*) INTUSE(_IO_list_all
);
855 last_stamp
= _IO_list_all_stamp
;
863 _IO_lock_unlock (list_all_lock
);
864 _IO_cleanup_region_end (0);
874 /* We want locking. */
875 return _IO_flush_all_lockp (1);
877 INTDEF(_IO_flush_all
)
880 _IO_flush_all_linebuffered ()
886 _IO_cleanup_region_start_noarg (flush_cleanup
);
887 _IO_lock_lock (list_all_lock
);
890 last_stamp
= _IO_list_all_stamp
;
891 fp
= (_IO_FILE
*) INTUSE(_IO_list_all
);
897 if ((fp
->_flags
& _IO_NO_WRITES
) == 0 && fp
->_flags
& _IO_LINE_BUF
)
898 _IO_OVERFLOW (fp
, EOF
);
900 _IO_funlockfile (fp
);
903 if (last_stamp
!= _IO_list_all_stamp
)
905 /* Something was added to the list. Start all over again. */
906 fp
= (_IO_FILE
*) INTUSE(_IO_list_all
);
907 last_stamp
= _IO_list_all_stamp
;
914 _IO_lock_unlock (list_all_lock
);
915 _IO_cleanup_region_end (0);
918 INTDEF(_IO_flush_all_linebuffered
)
920 weak_alias (_IO_flush_all_linebuffered
, _flushlbf
)
924 /* The following is a bit tricky. In general, we want to unbuffer the
925 streams so that all output which follows is seen. If we are not
926 looking for memory leaks it does not make much sense to free the
927 actual buffer because this will happen anyway once the program
928 terminated. If we do want to look for memory leaks we have to free
929 the buffers. Whether something is freed is determined by the
930 function sin the libc_freeres section. Those are called as part of
931 the atexit routine, just like _IO_cleanup. The problem is we do
932 not know whether the freeres code is called first or _IO_cleanup.
933 if the former is the case, we set the DEALLOC_BUFFER variable to
934 true and _IO_unbuffer_write will take care of the rest. If
935 _IO_unbuffer_write is called first we add the streams to a list
936 which the freeres function later can walk through. */
937 static void _IO_unbuffer_write (void);
939 static bool dealloc_buffers
;
940 static _IO_FILE
*freeres_list
;
943 _IO_unbuffer_write (void)
946 for (fp
= (_IO_FILE
*) INTUSE(_IO_list_all
); fp
; fp
= fp
->_chain
)
948 if (! (fp
->_flags
& _IO_UNBUFFERED
)
949 && (! (fp
->_flags
& _IO_NO_WRITES
)
950 || (fp
->_flags
& _IO_IS_APPENDING
))
951 /* Iff stream is un-orientated, it wasn't used. */
956 for (cnt
= 0; cnt
< MAXTRIES
; ++cnt
)
957 if (_IO_lock_trylock (*fp
->_lock
) == 0)
960 /* Give the other thread time to finish up its use of the
964 if (! dealloc_buffers
&& !(fp
->_flags
& _IO_USER_BUF
))
966 fp
->_flags
|= _IO_USER_BUF
;
968 fp
->_freeres_list
= freeres_list
;
970 fp
->_freeres_buf
= fp
->_IO_buf_base
;
971 fp
->_freeres_size
= _IO_blen (fp
);
974 _IO_SETBUF (fp
, NULL
, 0);
977 _IO_lock_unlock (*fp
->_lock
);
980 /* Make sure that never again the wide char functions can be
987 libc_freeres_fn (buffer_free
)
989 dealloc_buffers
= true;
991 while (freeres_list
!= NULL
)
993 FREE_BUF (freeres_list
->_freeres_buf
, freeres_list
->_freeres_size
);
995 freeres_list
= freeres_list
->_freeres_list
;
1003 /* We do *not* want locking. Some threads might use streams but
1004 that is their problem, we flush them underneath them. */
1005 int result
= _IO_flush_all_lockp (0);
1007 /* We currently don't have a reliable mechanism for making sure that
1008 C++ static destructors are executed in the correct order.
1009 So it is possible that other static destructors might want to
1010 write to cout - and they're supposed to be able to do so.
1012 The following will make the standard streambufs be unbuffered,
1013 which forces any output from late destructors to be written out. */
1014 _IO_unbuffer_write ();
1021 _IO_init_marker (marker
, fp
)
1022 struct _IO_marker
*marker
;
1026 if (_IO_in_put_mode (fp
))
1027 INTUSE(_IO_switch_to_get_mode
) (fp
);
1028 if (_IO_in_backup (fp
))
1029 marker
->_pos
= fp
->_IO_read_ptr
- fp
->_IO_read_end
;
1031 marker
->_pos
= fp
->_IO_read_ptr
- fp
->_IO_read_base
;
1033 /* Should perhaps sort the chain? */
1034 marker
->_next
= fp
->_markers
;
1035 fp
->_markers
= marker
;
1039 _IO_remove_marker (marker
)
1040 struct _IO_marker
*marker
;
1042 /* Unlink from sb's chain. */
1043 struct _IO_marker
**ptr
= &marker
->_sbuf
->_markers
;
1044 for (; ; ptr
= &(*ptr
)->_next
)
1048 else if (*ptr
== marker
)
1050 *ptr
= marker
->_next
;
1055 if _sbuf has a backup area that is no longer needed
, should we
delete
1056 it now
, or wait until the next underflow
?
1060 #define BAD_DELTA EOF
1063 _IO_marker_difference (mark1
, mark2
)
1064 struct _IO_marker
*mark1
;
1065 struct _IO_marker
*mark2
;
1067 return mark1
->_pos
- mark2
->_pos
;
1070 /* Return difference between MARK and current position of MARK's stream. */
1072 _IO_marker_delta (mark
)
1073 struct _IO_marker
*mark
;
1076 if (mark
->_sbuf
== NULL
)
1078 if (_IO_in_backup (mark
->_sbuf
))
1079 cur_pos
= mark
->_sbuf
->_IO_read_ptr
- mark
->_sbuf
->_IO_read_end
;
1081 cur_pos
= mark
->_sbuf
->_IO_read_ptr
- mark
->_sbuf
->_IO_read_base
;
1082 return mark
->_pos
- cur_pos
;
1086 _IO_seekmark (fp
, mark
, delta
)
1088 struct _IO_marker
*mark
;
1091 if (mark
->_sbuf
!= fp
)
1093 if (mark
->_pos
>= 0)
1095 if (_IO_in_backup (fp
))
1096 _IO_switch_to_main_get_area (fp
);
1097 fp
->_IO_read_ptr
= fp
->_IO_read_base
+ mark
->_pos
;
1101 if (!_IO_in_backup (fp
))
1102 _IO_switch_to_backup_area (fp
);
1103 fp
->_IO_read_ptr
= fp
->_IO_read_end
+ mark
->_pos
;
1109 _IO_unsave_markers (fp
)
1112 struct _IO_marker
*mark
= fp
->_markers
;
1116 streampos offset
= seekoff (0, ios::cur
, ios::in
);
1119 offset
+= eGptr () - Gbase ();
1120 for ( ; mark
!= NULL
; mark
= mark
->_next
)
1121 mark
->set_streampos (mark
->_pos
+ offset
);
1125 for ( ; mark
!= NULL
; mark
= mark
->_next
)
1126 mark
->set_streampos (EOF
);
1132 if (_IO_have_backup (fp
))
1133 INTUSE(_IO_free_backup_area
) (fp
);
1135 INTDEF(_IO_unsave_markers
)
1138 /* Seems not to be needed. --drepper */
1140 _IO_nobackup_pbackfail (fp
, c
)
1144 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
)
1146 if (c
!= EOF
&& *fp
->_IO_read_ptr
!= c
)
1147 *fp
->_IO_read_ptr
= c
;
1148 return (unsigned char) c
;
1153 _IO_default_pbackfail (fp
, c
)
1157 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
&& !_IO_in_backup (fp
)
1158 && (unsigned char) fp
->_IO_read_ptr
[-1] == c
)
1162 /* Need to handle a filebuf in write mode (switch to read mode). FIXME!*/
1163 if (!_IO_in_backup (fp
))
1165 /* We need to keep the invariant that the main get area
1166 logically follows the backup area. */
1167 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
&& _IO_have_backup (fp
))
1169 if (save_for_backup (fp
, fp
->_IO_read_ptr
))
1172 else if (!_IO_have_backup (fp
))
1174 /* No backup buffer: allocate one. */
1175 /* Use nshort buffer, if unused? (probably not) FIXME */
1176 int backup_size
= 128;
1177 char *bbuf
= (char *) malloc (backup_size
);
1180 fp
->_IO_save_base
= bbuf
;
1181 fp
->_IO_save_end
= fp
->_IO_save_base
+ backup_size
;
1182 fp
->_IO_backup_base
= fp
->_IO_save_end
;
1184 fp
->_IO_read_base
= fp
->_IO_read_ptr
;
1185 _IO_switch_to_backup_area (fp
);
1187 else if (fp
->_IO_read_ptr
<= fp
->_IO_read_base
)
1189 /* Increase size of existing backup buffer. */
1190 _IO_size_t new_size
;
1191 _IO_size_t old_size
= fp
->_IO_read_end
- fp
->_IO_read_base
;
1193 new_size
= 2 * old_size
;
1194 new_buf
= (char *) malloc (new_size
);
1195 if (new_buf
== NULL
)
1197 memcpy (new_buf
+ (new_size
- old_size
), fp
->_IO_read_base
,
1199 free (fp
->_IO_read_base
);
1200 _IO_setg (fp
, new_buf
, new_buf
+ (new_size
- old_size
),
1201 new_buf
+ new_size
);
1202 fp
->_IO_backup_base
= fp
->_IO_read_ptr
;
1205 *--fp
->_IO_read_ptr
= c
;
1207 return (unsigned char) c
;
1209 INTDEF(_IO_default_pbackfail
)
1212 _IO_default_seek (fp
, offset
, dir
)
1221 _IO_default_stat (fp
, st
)
1229 _IO_default_read (fp
, data
, n
)
1238 _IO_default_write (fp
, data
, n
)
1247 _IO_default_showmanyc (fp
)
1254 _IO_default_imbue (fp
, locale
)
1263 return (_IO_ITER
) INTUSE(_IO_list_all
);
1265 libc_hidden_def (_IO_iter_begin
)
1272 libc_hidden_def (_IO_iter_end
)
1278 return iter
->_chain
;
1280 libc_hidden_def (_IO_iter_next
)
1288 libc_hidden_def (_IO_iter_file
)
1293 #ifdef _IO_MTSAFE_IO
1294 _IO_lock_lock (list_all_lock
);
1297 libc_hidden_def (_IO_list_lock
)
1302 #ifdef _IO_MTSAFE_IO
1303 _IO_lock_unlock (list_all_lock
);
1306 libc_hidden_def (_IO_list_unlock
)
1309 _IO_list_resetlock()
1311 #ifdef _IO_MTSAFE_IO
1312 _IO_lock_init (list_all_lock
);
1315 libc_hidden_def (_IO_list_resetlock
)
1320 #define IO_CLEANUP ;
1328 ~__io_defs() { _IO_cleanup (); }
1330 __io_defs io_defs__
;
1335 #ifdef text_set_element
1336 text_set_element(__libc_atexit
, _IO_cleanup
);