1 /* Copyright (C) 1993,1995,1997-2002, 2003, 2004, 2006, 2007
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 if (fp
->_IO_save_base
)
304 free (fp
->_IO_save_base
);
305 fp
->_IO_save_base
= new_buffer
;
306 fp
->_IO_save_end
= new_buffer
+ avail
+ needed_size
;
310 avail
= current_Bsize
- needed_size
;
313 memmove (fp
->_IO_save_base
+ avail
,
314 fp
->_IO_save_end
+ least_mark
,
316 memcpy (fp
->_IO_save_base
+ avail
- least_mark
,
318 end_p
- fp
->_IO_read_base
);
320 else if (needed_size
> 0)
321 memcpy (fp
->_IO_save_base
+ avail
,
322 fp
->_IO_read_base
+ least_mark
,
325 fp
->_IO_backup_base
= fp
->_IO_save_base
+ avail
;
326 /* Adjust all the streammarkers. */
327 delta
= end_p
- fp
->_IO_read_base
;
328 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
337 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
338 if (_IO_vtable_offset (fp
) == 0 && _IO_fwide (fp
, -1) != -1)
344 if (_IO_in_put_mode (fp
))
345 if (INTUSE(_IO_switch_to_get_mode
) (fp
) == EOF
)
347 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
348 return *(unsigned char *) fp
->_IO_read_ptr
;
349 if (_IO_in_backup (fp
))
351 _IO_switch_to_main_get_area (fp
);
352 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
353 return *(unsigned char *) fp
->_IO_read_ptr
;
355 if (_IO_have_markers (fp
))
357 if (save_for_backup (fp
, fp
->_IO_read_end
))
360 else if (_IO_have_backup (fp
))
361 INTUSE(_IO_free_backup_area
) (fp
);
362 return _IO_UNDERFLOW (fp
);
364 libc_hidden_def (__underflow
)
370 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
371 if (_IO_vtable_offset (fp
) == 0 && _IO_fwide (fp
, -1) != -1)
377 if (_IO_in_put_mode (fp
))
378 if (INTUSE(_IO_switch_to_get_mode
) (fp
) == EOF
)
380 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
381 return *(unsigned char *) fp
->_IO_read_ptr
++;
382 if (_IO_in_backup (fp
))
384 _IO_switch_to_main_get_area (fp
);
385 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
386 return *(unsigned char *) fp
->_IO_read_ptr
++;
388 if (_IO_have_markers (fp
))
390 if (save_for_backup (fp
, fp
->_IO_read_end
))
393 else if (_IO_have_backup (fp
))
394 INTUSE(_IO_free_backup_area
) (fp
);
395 return _IO_UFLOW (fp
);
397 libc_hidden_def (__uflow
)
400 _IO_setb (f
, b
, eb
, a
)
406 if (f
->_IO_buf_base
&& !(f
->_flags
& _IO_USER_BUF
))
407 FREE_BUF (f
->_IO_buf_base
, _IO_blen (f
));
411 f
->_flags
&= ~_IO_USER_BUF
;
413 f
->_flags
|= _IO_USER_BUF
;
421 if (fp
->_IO_buf_base
)
423 if (!(fp
->_flags
& _IO_UNBUFFERED
) || fp
->_mode
> 0)
424 if (_IO_DOALLOCATE (fp
) != EOF
)
426 INTUSE(_IO_setb
) (fp
, fp
->_shortbuf
, fp
->_shortbuf
+1, 0);
428 INTDEF(_IO_doallocbuf
)
431 _IO_default_underflow (fp
)
438 _IO_default_uflow (fp
)
441 int ch
= _IO_UNDERFLOW (fp
);
444 return *(unsigned char *) fp
->_IO_read_ptr
++;
446 INTDEF(_IO_default_uflow
)
449 _IO_default_xsputn (f
, data
, n
)
454 const char *s
= (char *) data
;
460 /* Space available. */
461 if (f
->_IO_write_ptr
< f
->_IO_write_end
)
463 _IO_size_t count
= f
->_IO_write_end
- f
->_IO_write_ptr
;
469 f
->_IO_write_ptr
= __mempcpy (f
->_IO_write_ptr
, s
, count
);
471 memcpy (f
->_IO_write_ptr
, s
, count
);
472 f
->_IO_write_ptr
+= count
;
478 char *p
= f
->_IO_write_ptr
;
480 for (i
= count
; --i
>= 0; )
482 f
->_IO_write_ptr
= p
;
486 if (more
== 0 || _IO_OVERFLOW (f
, (unsigned char) *s
++) == EOF
)
492 INTDEF(_IO_default_xsputn
)
495 _IO_sgetn (fp
, data
, n
)
500 /* FIXME handle putback buffer here! */
501 return _IO_XSGETN (fp
, data
, n
);
506 _IO_default_xsgetn (fp
, data
, n
)
512 char *s
= (char*) data
;
515 /* Data available. */
516 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
518 _IO_size_t count
= fp
->_IO_read_end
- fp
->_IO_read_ptr
;
524 s
= __mempcpy (s
, fp
->_IO_read_ptr
, count
);
526 memcpy (s
, fp
->_IO_read_ptr
, count
);
529 fp
->_IO_read_ptr
+= count
;
533 char *p
= fp
->_IO_read_ptr
;
537 fp
->_IO_read_ptr
= p
;
541 if (more
== 0 || __underflow (fp
) == EOF
)
546 INTDEF(_IO_default_xsgetn
)
549 /* Seems not to be needed. --drepper */
559 _IO_default_setbuf (fp
, p
, len
)
564 if (_IO_SYNC (fp
) == EOF
)
566 if (p
== NULL
|| len
== 0)
568 fp
->_flags
|= _IO_UNBUFFERED
;
569 INTUSE(_IO_setb
) (fp
, fp
->_shortbuf
, fp
->_shortbuf
+1, 0);
573 fp
->_flags
&= ~_IO_UNBUFFERED
;
574 INTUSE(_IO_setb
) (fp
, p
, p
+len
, 0);
576 fp
->_IO_write_base
= fp
->_IO_write_ptr
= fp
->_IO_write_end
= 0;
577 fp
->_IO_read_base
= fp
->_IO_read_ptr
= fp
->_IO_read_end
= 0;
582 _IO_default_seekpos (fp
, pos
, mode
)
587 return _IO_SEEKOFF (fp
, pos
, 0, mode
);
591 _IO_default_doallocate (fp
)
596 ALLOC_BUF (buf
, _IO_BUFSIZ
, EOF
);
597 INTUSE(_IO_setb
) (fp
, buf
, buf
+_IO_BUFSIZ
, 1);
600 INTDEF(_IO_default_doallocate
)
607 _IO_no_init (fp
, flags
, -1, NULL
, NULL
);
612 _IO_old_init (fp
, flags
)
616 fp
->_flags
= _IO_MAGIC
|flags
;
618 fp
->_IO_buf_base
= NULL
;
619 fp
->_IO_buf_end
= NULL
;
620 fp
->_IO_read_base
= NULL
;
621 fp
->_IO_read_ptr
= NULL
;
622 fp
->_IO_read_end
= NULL
;
623 fp
->_IO_write_base
= NULL
;
624 fp
->_IO_write_ptr
= NULL
;
625 fp
->_IO_write_end
= NULL
;
626 fp
->_chain
= NULL
; /* Not necessary. */
628 fp
->_IO_save_base
= NULL
;
629 fp
->_IO_backup_base
= NULL
;
630 fp
->_IO_save_end
= NULL
;
634 fp
->_vtable_offset
= 0;
637 if (fp
->_lock
!= NULL
)
638 _IO_lock_init (*fp
->_lock
);
643 _IO_no_init (fp
, flags
, orientation
, wd
, jmp
)
647 struct _IO_wide_data
*wd
;
648 const struct _IO_jump_t
*jmp
;
650 _IO_old_init (fp
, flags
);
651 fp
->_mode
= orientation
;
652 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
653 if (orientation
>= 0)
656 fp
->_wide_data
->_IO_buf_base
= NULL
;
657 fp
->_wide_data
->_IO_buf_end
= NULL
;
658 fp
->_wide_data
->_IO_read_base
= NULL
;
659 fp
->_wide_data
->_IO_read_ptr
= NULL
;
660 fp
->_wide_data
->_IO_read_end
= NULL
;
661 fp
->_wide_data
->_IO_write_base
= NULL
;
662 fp
->_wide_data
->_IO_write_ptr
= NULL
;
663 fp
->_wide_data
->_IO_write_end
= NULL
;
664 fp
->_wide_data
->_IO_save_base
= NULL
;
665 fp
->_wide_data
->_IO_backup_base
= NULL
;
666 fp
->_wide_data
->_IO_save_end
= NULL
;
668 fp
->_wide_data
->_wide_vtable
= jmp
;
671 fp
->_freeres_list
= NULL
;
675 _IO_default_sync (fp
)
681 /* The way the C++ classes are mapped into the C functions in the
682 current implementation, this function can get called twice! */
685 _IO_default_finish (fp
, dummy
)
689 struct _IO_marker
*mark
;
690 if (fp
->_IO_buf_base
&& !(fp
->_flags
& _IO_USER_BUF
))
692 FREE_BUF (fp
->_IO_buf_base
, _IO_blen (fp
));
693 fp
->_IO_buf_base
= fp
->_IO_buf_end
= NULL
;
696 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
699 if (fp
->_IO_save_base
)
701 free (fp
->_IO_save_base
);
702 fp
->_IO_save_base
= NULL
;
705 INTUSE(_IO_un_link
) ((struct _IO_FILE_plus
*) fp
);
708 if (fp
->_lock
!= NULL
)
709 _IO_lock_fini (*fp
->_lock
);
712 INTDEF(_IO_default_finish
)
715 _IO_default_seekoff (fp
, offset
, dir
, mode
)
725 _IO_sputbackc (fp
, c
)
731 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
732 && (unsigned char)fp
->_IO_read_ptr
[-1] == (unsigned char)c
)
735 result
= (unsigned char) c
;
738 result
= _IO_PBACKFAIL (fp
, c
);
741 fp
->_flags
&= ~_IO_EOF_SEEN
;
745 INTDEF(_IO_sputbackc
)
753 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
)
756 result
= (unsigned char) *fp
->_IO_read_ptr
;
759 result
= _IO_PBACKFAIL (fp
, EOF
);
762 fp
->_flags
&= ~_IO_EOF_SEEN
;
767 #if 0 /* Work in progress */
768 /* Seems not to be needed. */
771 _IO_set_column (fp
, c
)
778 fp
->_column
= c
- (fp
->_IO_write_ptr
- fp
->_IO_write_base
);
782 _IO_set_column (fp
, i
)
786 fp
->_cur_column
= i
+ 1;
794 _IO_adjust_column (start
, line
, count
)
799 const char *ptr
= line
+ count
;
802 return line
+ count
- ptr
- 1;
803 return start
+ count
;
805 INTDEF(_IO_adjust_column
)
808 /* Seems not to be needed. --drepper */
814 return _IO_adjust_column (fp
->_cur_column
- 1,
816 fp
->_IO_write_ptr
- fp
->_IO_write_base
);
823 _IO_flush_all_lockp (int do_lock
)
830 _IO_cleanup_region_start_noarg (flush_cleanup
);
832 _IO_lock_lock (list_all_lock
);
835 last_stamp
= _IO_list_all_stamp
;
836 fp
= (_IO_FILE
*) INTUSE(_IO_list_all
);
843 if (((fp
->_mode
<= 0 && fp
->_IO_write_ptr
> fp
->_IO_write_base
)
844 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
845 || (_IO_vtable_offset (fp
) == 0
846 && fp
->_mode
> 0 && (fp
->_wide_data
->_IO_write_ptr
847 > fp
->_wide_data
->_IO_write_base
))
850 && _IO_OVERFLOW (fp
, EOF
) == EOF
)
854 _IO_funlockfile (fp
);
857 if (last_stamp
!= _IO_list_all_stamp
)
859 /* Something was added to the list. Start all over again. */
860 fp
= (_IO_FILE
*) INTUSE(_IO_list_all
);
861 last_stamp
= _IO_list_all_stamp
;
869 _IO_lock_unlock (list_all_lock
);
870 _IO_cleanup_region_end (0);
880 /* We want locking. */
881 return _IO_flush_all_lockp (1);
883 INTDEF(_IO_flush_all
)
886 _IO_flush_all_linebuffered ()
892 _IO_cleanup_region_start_noarg (flush_cleanup
);
893 _IO_lock_lock (list_all_lock
);
896 last_stamp
= _IO_list_all_stamp
;
897 fp
= (_IO_FILE
*) INTUSE(_IO_list_all
);
903 if ((fp
->_flags
& _IO_NO_WRITES
) == 0 && fp
->_flags
& _IO_LINE_BUF
)
904 _IO_OVERFLOW (fp
, EOF
);
906 _IO_funlockfile (fp
);
909 if (last_stamp
!= _IO_list_all_stamp
)
911 /* Something was added to the list. Start all over again. */
912 fp
= (_IO_FILE
*) INTUSE(_IO_list_all
);
913 last_stamp
= _IO_list_all_stamp
;
920 _IO_lock_unlock (list_all_lock
);
921 _IO_cleanup_region_end (0);
924 INTDEF(_IO_flush_all_linebuffered
)
926 weak_alias (_IO_flush_all_linebuffered
, _flushlbf
)
930 /* The following is a bit tricky. In general, we want to unbuffer the
931 streams so that all output which follows is seen. If we are not
932 looking for memory leaks it does not make much sense to free the
933 actual buffer because this will happen anyway once the program
934 terminated. If we do want to look for memory leaks we have to free
935 the buffers. Whether something is freed is determined by the
936 function sin the libc_freeres section. Those are called as part of
937 the atexit routine, just like _IO_cleanup. The problem is we do
938 not know whether the freeres code is called first or _IO_cleanup.
939 if the former is the case, we set the DEALLOC_BUFFER variable to
940 true and _IO_unbuffer_write will take care of the rest. If
941 _IO_unbuffer_write is called first we add the streams to a list
942 which the freeres function later can walk through. */
943 static void _IO_unbuffer_write (void);
945 static bool dealloc_buffers
;
946 static _IO_FILE
*freeres_list
;
949 _IO_unbuffer_write (void)
952 for (fp
= (_IO_FILE
*) INTUSE(_IO_list_all
); fp
; fp
= fp
->_chain
)
954 if (! (fp
->_flags
& _IO_UNBUFFERED
)
955 && (! (fp
->_flags
& _IO_NO_WRITES
)
956 || (fp
->_flags
& _IO_IS_APPENDING
))
957 /* Iff stream is un-orientated, it wasn't used. */
962 for (cnt
= 0; cnt
< MAXTRIES
; ++cnt
)
963 if (_IO_lock_trylock (*fp
->_lock
) == 0)
966 /* Give the other thread time to finish up its use of the
970 if (! dealloc_buffers
&& !(fp
->_flags
& _IO_USER_BUF
))
972 fp
->_flags
|= _IO_USER_BUF
;
974 fp
->_freeres_list
= freeres_list
;
976 fp
->_freeres_buf
= fp
->_IO_buf_base
;
977 fp
->_freeres_size
= _IO_blen (fp
);
980 _IO_SETBUF (fp
, NULL
, 0);
983 _IO_lock_unlock (*fp
->_lock
);
986 /* Make sure that never again the wide char functions can be
993 libc_freeres_fn (buffer_free
)
995 dealloc_buffers
= true;
997 while (freeres_list
!= NULL
)
999 FREE_BUF (freeres_list
->_freeres_buf
, freeres_list
->_freeres_size
);
1001 freeres_list
= freeres_list
->_freeres_list
;
1009 /* We do *not* want locking. Some threads might use streams but
1010 that is their problem, we flush them underneath them. */
1011 int result
= _IO_flush_all_lockp (0);
1013 /* We currently don't have a reliable mechanism for making sure that
1014 C++ static destructors are executed in the correct order.
1015 So it is possible that other static destructors might want to
1016 write to cout - and they're supposed to be able to do so.
1018 The following will make the standard streambufs be unbuffered,
1019 which forces any output from late destructors to be written out. */
1020 _IO_unbuffer_write ();
1027 _IO_init_marker (marker
, fp
)
1028 struct _IO_marker
*marker
;
1032 if (_IO_in_put_mode (fp
))
1033 INTUSE(_IO_switch_to_get_mode
) (fp
);
1034 if (_IO_in_backup (fp
))
1035 marker
->_pos
= fp
->_IO_read_ptr
- fp
->_IO_read_end
;
1037 marker
->_pos
= fp
->_IO_read_ptr
- fp
->_IO_read_base
;
1039 /* Should perhaps sort the chain? */
1040 marker
->_next
= fp
->_markers
;
1041 fp
->_markers
= marker
;
1045 _IO_remove_marker (marker
)
1046 struct _IO_marker
*marker
;
1048 /* Unlink from sb's chain. */
1049 struct _IO_marker
**ptr
= &marker
->_sbuf
->_markers
;
1050 for (; ; ptr
= &(*ptr
)->_next
)
1054 else if (*ptr
== marker
)
1056 *ptr
= marker
->_next
;
1061 if _sbuf has a backup area that is no longer needed
, should we
delete
1062 it now
, or wait until the next underflow
?
1066 #define BAD_DELTA EOF
1069 _IO_marker_difference (mark1
, mark2
)
1070 struct _IO_marker
*mark1
;
1071 struct _IO_marker
*mark2
;
1073 return mark1
->_pos
- mark2
->_pos
;
1076 /* Return difference between MARK and current position of MARK's stream. */
1078 _IO_marker_delta (mark
)
1079 struct _IO_marker
*mark
;
1082 if (mark
->_sbuf
== NULL
)
1084 if (_IO_in_backup (mark
->_sbuf
))
1085 cur_pos
= mark
->_sbuf
->_IO_read_ptr
- mark
->_sbuf
->_IO_read_end
;
1087 cur_pos
= mark
->_sbuf
->_IO_read_ptr
- mark
->_sbuf
->_IO_read_base
;
1088 return mark
->_pos
- cur_pos
;
1092 _IO_seekmark (fp
, mark
, delta
)
1094 struct _IO_marker
*mark
;
1097 if (mark
->_sbuf
!= fp
)
1099 if (mark
->_pos
>= 0)
1101 if (_IO_in_backup (fp
))
1102 _IO_switch_to_main_get_area (fp
);
1103 fp
->_IO_read_ptr
= fp
->_IO_read_base
+ mark
->_pos
;
1107 if (!_IO_in_backup (fp
))
1108 _IO_switch_to_backup_area (fp
);
1109 fp
->_IO_read_ptr
= fp
->_IO_read_end
+ mark
->_pos
;
1115 _IO_unsave_markers (fp
)
1118 struct _IO_marker
*mark
= fp
->_markers
;
1122 streampos offset
= seekoff (0, ios::cur
, ios::in
);
1125 offset
+= eGptr () - Gbase ();
1126 for ( ; mark
!= NULL
; mark
= mark
->_next
)
1127 mark
->set_streampos (mark
->_pos
+ offset
);
1131 for ( ; mark
!= NULL
; mark
= mark
->_next
)
1132 mark
->set_streampos (EOF
);
1138 if (_IO_have_backup (fp
))
1139 INTUSE(_IO_free_backup_area
) (fp
);
1141 INTDEF(_IO_unsave_markers
)
1144 /* Seems not to be needed. --drepper */
1146 _IO_nobackup_pbackfail (fp
, c
)
1150 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
)
1152 if (c
!= EOF
&& *fp
->_IO_read_ptr
!= c
)
1153 *fp
->_IO_read_ptr
= c
;
1154 return (unsigned char) c
;
1159 _IO_default_pbackfail (fp
, c
)
1163 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
&& !_IO_in_backup (fp
)
1164 && (unsigned char) fp
->_IO_read_ptr
[-1] == c
)
1168 /* Need to handle a filebuf in write mode (switch to read mode). FIXME!*/
1169 if (!_IO_in_backup (fp
))
1171 /* We need to keep the invariant that the main get area
1172 logically follows the backup area. */
1173 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
&& _IO_have_backup (fp
))
1175 if (save_for_backup (fp
, fp
->_IO_read_ptr
))
1178 else if (!_IO_have_backup (fp
))
1180 /* No backup buffer: allocate one. */
1181 /* Use nshort buffer, if unused? (probably not) FIXME */
1182 int backup_size
= 128;
1183 char *bbuf
= (char *) malloc (backup_size
);
1186 fp
->_IO_save_base
= bbuf
;
1187 fp
->_IO_save_end
= fp
->_IO_save_base
+ backup_size
;
1188 fp
->_IO_backup_base
= fp
->_IO_save_end
;
1190 fp
->_IO_read_base
= fp
->_IO_read_ptr
;
1191 _IO_switch_to_backup_area (fp
);
1193 else if (fp
->_IO_read_ptr
<= fp
->_IO_read_base
)
1195 /* Increase size of existing backup buffer. */
1196 _IO_size_t new_size
;
1197 _IO_size_t old_size
= fp
->_IO_read_end
- fp
->_IO_read_base
;
1199 new_size
= 2 * old_size
;
1200 new_buf
= (char *) malloc (new_size
);
1201 if (new_buf
== NULL
)
1203 memcpy (new_buf
+ (new_size
- old_size
), fp
->_IO_read_base
,
1205 free (fp
->_IO_read_base
);
1206 _IO_setg (fp
, new_buf
, new_buf
+ (new_size
- old_size
),
1207 new_buf
+ new_size
);
1208 fp
->_IO_backup_base
= fp
->_IO_read_ptr
;
1211 *--fp
->_IO_read_ptr
= c
;
1213 return (unsigned char) c
;
1215 INTDEF(_IO_default_pbackfail
)
1218 _IO_default_seek (fp
, offset
, dir
)
1227 _IO_default_stat (fp
, st
)
1235 _IO_default_read (fp
, data
, n
)
1244 _IO_default_write (fp
, data
, n
)
1253 _IO_default_showmanyc (fp
)
1260 _IO_default_imbue (fp
, locale
)
1269 return (_IO_ITER
) INTUSE(_IO_list_all
);
1271 libc_hidden_def (_IO_iter_begin
)
1278 libc_hidden_def (_IO_iter_end
)
1284 return iter
->_chain
;
1286 libc_hidden_def (_IO_iter_next
)
1294 libc_hidden_def (_IO_iter_file
)
1299 #ifdef _IO_MTSAFE_IO
1300 _IO_lock_lock (list_all_lock
);
1303 libc_hidden_def (_IO_list_lock
)
1308 #ifdef _IO_MTSAFE_IO
1309 _IO_lock_unlock (list_all_lock
);
1312 libc_hidden_def (_IO_list_unlock
)
1315 _IO_list_resetlock()
1317 #ifdef _IO_MTSAFE_IO
1318 _IO_lock_init (list_all_lock
);
1321 libc_hidden_def (_IO_list_resetlock
)
1326 #define IO_CLEANUP ;
1334 ~__io_defs() { _IO_cleanup (); }
1336 __io_defs io_defs__
;
1341 #ifdef text_set_element
1342 text_set_element(__libc_atexit
, _IO_cleanup
);