1 /* Copyright (C) 1993,1995,1997-2002, 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
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. */
37 static _IO_lock_t list_all_lock
= _IO_lock_initializer
;
40 /* Used to signal modifications to the list of FILE decriptors. */
41 static int _IO_list_all_stamp
;
44 static _IO_FILE
*run_fp
;
47 flush_cleanup (void *not_used
)
50 _IO_funlockfile (run_fp
);
52 _IO_lock_unlock (list_all_lock
);
58 struct _IO_FILE_plus
*fp
;
60 if (fp
->file
._flags
& _IO_LINKED
)
62 struct _IO_FILE_plus
**f
;
64 _IO_cleanup_region_start_noarg (flush_cleanup
);
65 _IO_lock_lock (list_all_lock
);
66 run_fp
= (_IO_FILE
*) fp
;
67 _IO_flockfile ((_IO_FILE
*) fp
);
69 for (f
= &INTUSE(_IO_list_all
); *f
;
70 f
= (struct _IO_FILE_plus
**) &(*f
)->file
._chain
)
74 *f
= (struct _IO_FILE_plus
*) fp
->file
._chain
;
79 fp
->file
._flags
&= ~_IO_LINKED
;
81 _IO_funlockfile ((_IO_FILE
*) fp
);
83 _IO_lock_unlock (list_all_lock
);
84 _IO_cleanup_region_end (0);
92 struct _IO_FILE_plus
*fp
;
94 if ((fp
->file
._flags
& _IO_LINKED
) == 0)
96 fp
->file
._flags
|= _IO_LINKED
;
98 _IO_cleanup_region_start_noarg (flush_cleanup
);
99 _IO_lock_lock (list_all_lock
);
100 run_fp
= (_IO_FILE
*) fp
;
101 _IO_flockfile ((_IO_FILE
*) fp
);
103 fp
->file
._chain
= (_IO_FILE
*) INTUSE(_IO_list_all
);
104 INTUSE(_IO_list_all
) = fp
;
105 ++_IO_list_all_stamp
;
107 _IO_funlockfile ((_IO_FILE
*) fp
);
109 _IO_lock_unlock (list_all_lock
);
110 _IO_cleanup_region_end (0);
116 /* Return minimum _pos markers
117 Assumes the current get area is the main get area. */
118 _IO_ssize_t _IO_least_marker
__P ((_IO_FILE
*fp
, char *end_p
));
121 _IO_least_marker (fp
, end_p
)
125 _IO_ssize_t least_so_far
= end_p
- fp
->_IO_read_base
;
126 struct _IO_marker
*mark
;
127 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
128 if (mark
->_pos
< least_so_far
)
129 least_so_far
= mark
->_pos
;
133 /* Switch current get area from backup buffer to (start of) main get area. */
136 _IO_switch_to_main_get_area (fp
)
140 fp
->_flags
&= ~_IO_IN_BACKUP
;
141 /* Swap _IO_read_end and _IO_save_end. */
142 tmp
= fp
->_IO_read_end
;
143 fp
->_IO_read_end
= fp
->_IO_save_end
;
144 fp
->_IO_save_end
= tmp
;
145 /* Swap _IO_read_base and _IO_save_base. */
146 tmp
= fp
->_IO_read_base
;
147 fp
->_IO_read_base
= fp
->_IO_save_base
;
148 fp
->_IO_save_base
= tmp
;
149 /* Set _IO_read_ptr. */
150 fp
->_IO_read_ptr
= fp
->_IO_read_base
;
153 /* Switch current get area from main get area to (end of) backup area. */
156 _IO_switch_to_backup_area (fp
)
160 fp
->_flags
|= _IO_IN_BACKUP
;
161 /* Swap _IO_read_end and _IO_save_end. */
162 tmp
= fp
->_IO_read_end
;
163 fp
->_IO_read_end
= fp
->_IO_save_end
;
164 fp
->_IO_save_end
= tmp
;
165 /* Swap _IO_read_base and _IO_save_base. */
166 tmp
= fp
->_IO_read_base
;
167 fp
->_IO_read_base
= fp
->_IO_save_base
;
168 fp
->_IO_save_base
= tmp
;
169 /* Set _IO_read_ptr. */
170 fp
->_IO_read_ptr
= fp
->_IO_read_end
;
174 _IO_switch_to_get_mode (fp
)
177 if (fp
->_IO_write_ptr
> fp
->_IO_write_base
)
178 if (_IO_OVERFLOW (fp
, EOF
) == EOF
)
180 if (_IO_in_backup (fp
))
181 fp
->_IO_read_base
= fp
->_IO_backup_base
;
184 fp
->_IO_read_base
= fp
->_IO_buf_base
;
185 if (fp
->_IO_write_ptr
> fp
->_IO_read_end
)
186 fp
->_IO_read_end
= fp
->_IO_write_ptr
;
188 fp
->_IO_read_ptr
= fp
->_IO_write_ptr
;
190 fp
->_IO_write_base
= fp
->_IO_write_ptr
= fp
->_IO_write_end
= fp
->_IO_read_ptr
;
192 fp
->_flags
&= ~_IO_CURRENTLY_PUTTING
;
195 INTDEF(_IO_switch_to_get_mode
)
198 _IO_free_backup_area (fp
)
201 if (_IO_in_backup (fp
))
202 _IO_switch_to_main_get_area (fp
); /* Just in case. */
203 free (fp
->_IO_save_base
);
204 fp
->_IO_save_base
= NULL
;
205 fp
->_IO_save_end
= NULL
;
206 fp
->_IO_backup_base
= NULL
;
208 INTDEF(_IO_free_backup_area
)
212 _IO_switch_to_put_mode (fp
)
215 fp
->_IO_write_base
= fp
->_IO_read_ptr
;
216 fp
->_IO_write_ptr
= fp
->_IO_read_ptr
;
217 /* Following is wrong if line- or un-buffered? */
218 fp
->_IO_write_end
= (fp
->_flags
& _IO_IN_BACKUP
219 ? fp
->_IO_read_end
: fp
->_IO_buf_end
);
221 fp
->_IO_read_ptr
= fp
->_IO_read_end
;
222 fp
->_IO_read_base
= fp
->_IO_read_end
;
224 fp
->_flags
|= _IO_CURRENTLY_PUTTING
;
234 /* This is a single-byte stream. */
237 return _IO_OVERFLOW (f
, ch
);
239 libc_hidden_def (__overflow
)
241 static int save_for_backup
__P ((_IO_FILE
*fp
, char *end_p
))
251 save_for_backup (fp
, end_p
)
255 /* Append [_IO_read_base..end_p] to backup area. */
256 _IO_ssize_t least_mark
= _IO_least_marker (fp
, end_p
);
257 /* needed_size is how much space we need in the backup area. */
258 _IO_size_t needed_size
= (end_p
- fp
->_IO_read_base
) - least_mark
;
259 /* FIXME: Dubious arithmetic if pointers are NULL */
260 _IO_size_t current_Bsize
= fp
->_IO_save_end
- fp
->_IO_save_base
;
261 _IO_size_t avail
; /* Extra space available for future expansion. */
263 struct _IO_marker
*mark
;
264 if (needed_size
> current_Bsize
)
268 new_buffer
= (char *) malloc (avail
+ needed_size
);
269 if (new_buffer
== NULL
)
270 return EOF
; /* FIXME */
274 __mempcpy (__mempcpy (new_buffer
+ avail
,
275 fp
->_IO_save_end
+ least_mark
,
278 end_p
- fp
->_IO_read_base
);
280 memcpy (new_buffer
+ avail
,
281 fp
->_IO_save_end
+ least_mark
,
283 memcpy (new_buffer
+ avail
- least_mark
,
285 end_p
- fp
->_IO_read_base
);
289 memcpy (new_buffer
+ avail
,
290 fp
->_IO_read_base
+ least_mark
,
292 if (fp
->_IO_save_base
)
293 free (fp
->_IO_save_base
);
294 fp
->_IO_save_base
= new_buffer
;
295 fp
->_IO_save_end
= new_buffer
+ avail
+ needed_size
;
299 avail
= current_Bsize
- needed_size
;
302 memmove (fp
->_IO_save_base
+ avail
,
303 fp
->_IO_save_end
+ least_mark
,
305 memcpy (fp
->_IO_save_base
+ avail
- least_mark
,
307 end_p
- fp
->_IO_read_base
);
309 else if (needed_size
> 0)
310 memcpy (fp
->_IO_save_base
+ avail
,
311 fp
->_IO_read_base
+ least_mark
,
314 fp
->_IO_backup_base
= fp
->_IO_save_base
+ avail
;
315 /* Adjust all the streammarkers. */
316 delta
= end_p
- fp
->_IO_read_base
;
317 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
326 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
327 if (_IO_vtable_offset (fp
) == 0 && _IO_fwide (fp
, -1) != -1)
333 if (_IO_in_put_mode (fp
))
334 if (INTUSE(_IO_switch_to_get_mode
) (fp
) == EOF
)
336 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
337 return *(unsigned char *) fp
->_IO_read_ptr
;
338 if (_IO_in_backup (fp
))
340 _IO_switch_to_main_get_area (fp
);
341 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
342 return *(unsigned char *) fp
->_IO_read_ptr
;
344 if (_IO_have_markers (fp
))
346 if (save_for_backup (fp
, fp
->_IO_read_end
))
349 else if (_IO_have_backup (fp
))
350 INTUSE(_IO_free_backup_area
) (fp
);
351 return _IO_UNDERFLOW (fp
);
353 libc_hidden_def (__underflow
)
359 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
360 if (_IO_vtable_offset (fp
) == 0 && _IO_fwide (fp
, -1) != -1)
366 if (_IO_in_put_mode (fp
))
367 if (INTUSE(_IO_switch_to_get_mode
) (fp
) == EOF
)
369 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
370 return *(unsigned char *) fp
->_IO_read_ptr
++;
371 if (_IO_in_backup (fp
))
373 _IO_switch_to_main_get_area (fp
);
374 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
375 return *(unsigned char *) fp
->_IO_read_ptr
++;
377 if (_IO_have_markers (fp
))
379 if (save_for_backup (fp
, fp
->_IO_read_end
))
382 else if (_IO_have_backup (fp
))
383 INTUSE(_IO_free_backup_area
) (fp
);
384 return _IO_UFLOW (fp
);
386 libc_hidden_def (__uflow
)
389 _IO_setb (f
, b
, eb
, a
)
395 if (f
->_IO_buf_base
&& !(f
->_flags
& _IO_USER_BUF
))
396 FREE_BUF (f
->_IO_buf_base
, _IO_blen (f
));
400 f
->_flags
&= ~_IO_USER_BUF
;
402 f
->_flags
|= _IO_USER_BUF
;
410 if (fp
->_IO_buf_base
)
412 if (!(fp
->_flags
& _IO_UNBUFFERED
) || fp
->_mode
> 0)
413 if (_IO_DOALLOCATE (fp
) != EOF
)
415 INTUSE(_IO_setb
) (fp
, fp
->_shortbuf
, fp
->_shortbuf
+1, 0);
417 INTDEF(_IO_doallocbuf
)
420 _IO_default_underflow (fp
)
427 _IO_default_uflow (fp
)
430 int ch
= _IO_UNDERFLOW (fp
);
433 return *(unsigned char *) fp
->_IO_read_ptr
++;
435 INTDEF(_IO_default_uflow
)
438 _IO_default_xsputn (f
, data
, n
)
443 const char *s
= (char *) data
;
449 /* Space available. */
450 if (f
->_IO_write_ptr
< f
->_IO_write_end
)
452 _IO_size_t count
= f
->_IO_write_end
- f
->_IO_write_ptr
;
458 f
->_IO_write_ptr
= __mempcpy (f
->_IO_write_ptr
, s
, count
);
460 memcpy (f
->_IO_write_ptr
, s
, count
);
461 f
->_IO_write_ptr
+= count
;
467 char *p
= f
->_IO_write_ptr
;
469 for (i
= count
; --i
>= 0; )
471 f
->_IO_write_ptr
= p
;
475 if (more
== 0 || _IO_OVERFLOW (f
, (unsigned char) *s
++) == EOF
)
481 INTDEF(_IO_default_xsputn
)
484 _IO_sgetn (fp
, data
, n
)
489 /* FIXME handle putback buffer here! */
490 return _IO_XSGETN (fp
, data
, n
);
495 _IO_default_xsgetn (fp
, data
, n
)
501 char *s
= (char*) data
;
504 /* Data available. */
505 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
507 _IO_size_t count
= fp
->_IO_read_end
- fp
->_IO_read_ptr
;
513 s
= __mempcpy (s
, fp
->_IO_read_ptr
, count
);
515 memcpy (s
, fp
->_IO_read_ptr
, count
);
518 fp
->_IO_read_ptr
+= count
;
522 char *p
= fp
->_IO_read_ptr
;
526 fp
->_IO_read_ptr
= p
;
530 if (more
== 0 || __underflow (fp
) == EOF
)
535 INTDEF(_IO_default_xsgetn
)
538 /* Seems not to be needed. --drepper */
548 _IO_default_setbuf (fp
, p
, len
)
553 if (_IO_SYNC (fp
) == EOF
)
555 if (p
== NULL
|| len
== 0)
557 fp
->_flags
|= _IO_UNBUFFERED
;
558 INTUSE(_IO_setb
) (fp
, fp
->_shortbuf
, fp
->_shortbuf
+1, 0);
562 fp
->_flags
&= ~_IO_UNBUFFERED
;
563 INTUSE(_IO_setb
) (fp
, p
, p
+len
, 0);
565 fp
->_IO_write_base
= fp
->_IO_write_ptr
= fp
->_IO_write_end
= 0;
566 fp
->_IO_read_base
= fp
->_IO_read_ptr
= fp
->_IO_read_end
= 0;
571 _IO_default_seekpos (fp
, pos
, mode
)
576 return _IO_SEEKOFF (fp
, pos
, 0, mode
);
580 _IO_default_doallocate (fp
)
585 ALLOC_BUF (buf
, _IO_BUFSIZ
, EOF
);
586 INTUSE(_IO_setb
) (fp
, buf
, buf
+_IO_BUFSIZ
, 1);
589 INTDEF(_IO_default_doallocate
)
596 _IO_no_init (fp
, flags
, -1, NULL
, NULL
);
601 _IO_old_init (fp
, flags
)
605 fp
->_flags
= _IO_MAGIC
|flags
;
607 fp
->_IO_buf_base
= NULL
;
608 fp
->_IO_buf_end
= NULL
;
609 fp
->_IO_read_base
= NULL
;
610 fp
->_IO_read_ptr
= NULL
;
611 fp
->_IO_read_end
= NULL
;
612 fp
->_IO_write_base
= NULL
;
613 fp
->_IO_write_ptr
= NULL
;
614 fp
->_IO_write_end
= NULL
;
615 fp
->_chain
= NULL
; /* Not necessary. */
617 fp
->_IO_save_base
= NULL
;
618 fp
->_IO_backup_base
= NULL
;
619 fp
->_IO_save_end
= NULL
;
623 fp
->_vtable_offset
= 0;
626 if (fp
->_lock
!= NULL
)
627 _IO_lock_init (*fp
->_lock
);
632 _IO_no_init (fp
, flags
, orientation
, wd
, jmp
)
636 struct _IO_wide_data
*wd
;
637 const struct _IO_jump_t
*jmp
;
639 _IO_old_init (fp
, flags
);
640 fp
->_mode
= orientation
;
641 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
642 if (orientation
>= 0)
645 fp
->_wide_data
->_IO_buf_base
= NULL
;
646 fp
->_wide_data
->_IO_buf_end
= NULL
;
647 fp
->_wide_data
->_IO_read_base
= NULL
;
648 fp
->_wide_data
->_IO_read_ptr
= NULL
;
649 fp
->_wide_data
->_IO_read_end
= NULL
;
650 fp
->_wide_data
->_IO_write_base
= NULL
;
651 fp
->_wide_data
->_IO_write_ptr
= NULL
;
652 fp
->_wide_data
->_IO_write_end
= NULL
;
653 fp
->_wide_data
->_IO_save_base
= NULL
;
654 fp
->_wide_data
->_IO_backup_base
= NULL
;
655 fp
->_wide_data
->_IO_save_end
= NULL
;
657 fp
->_wide_data
->_wide_vtable
= jmp
;
663 _IO_default_sync (fp
)
669 /* The way the C++ classes are mapped into the C functions in the
670 current implementation, this function can get called twice! */
673 _IO_default_finish (fp
, dummy
)
677 struct _IO_marker
*mark
;
678 if (fp
->_IO_buf_base
&& !(fp
->_flags
& _IO_USER_BUF
))
680 FREE_BUF (fp
->_IO_buf_base
, _IO_blen (fp
));
681 fp
->_IO_buf_base
= fp
->_IO_buf_end
= NULL
;
684 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
687 if (fp
->_IO_save_base
)
689 free (fp
->_IO_save_base
);
690 fp
->_IO_save_base
= NULL
;
694 if (fp
->_lock
!= NULL
)
695 _IO_lock_fini (*fp
->_lock
);
698 INTUSE(_IO_un_link
) ((struct _IO_FILE_plus
*) fp
);
700 INTDEF(_IO_default_finish
)
703 _IO_default_seekoff (fp
, offset
, dir
, mode
)
713 _IO_sputbackc (fp
, c
)
719 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
720 && (unsigned char)fp
->_IO_read_ptr
[-1] == (unsigned char)c
)
723 result
= (unsigned char) c
;
726 result
= _IO_PBACKFAIL (fp
, c
);
729 fp
->_flags
&= ~_IO_EOF_SEEN
;
733 INTDEF(_IO_sputbackc
)
741 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
)
744 result
= (unsigned char) *fp
->_IO_read_ptr
;
747 result
= _IO_PBACKFAIL (fp
, EOF
);
750 fp
->_flags
&= ~_IO_EOF_SEEN
;
755 #if 0 /* Work in progress */
756 /* Seems not to be needed. */
759 _IO_set_column (fp
, c
)
766 fp
->_column
= c
- (fp
->_IO_write_ptr
- fp
->_IO_write_base
);
770 _IO_set_column (fp
, i
)
774 fp
->_cur_column
= i
+ 1;
782 _IO_adjust_column (start
, line
, count
)
787 const char *ptr
= line
+ count
;
790 return line
+ count
- ptr
- 1;
791 return start
+ count
;
793 INTDEF(_IO_adjust_column
)
796 /* Seems not to be needed. --drepper */
802 return _IO_adjust_column (fp
->_cur_column
- 1,
804 fp
->_IO_write_ptr
- fp
->_IO_write_base
);
811 _IO_flush_all_lockp (int do_lock
)
818 _IO_cleanup_region_start_noarg (flush_cleanup
);
820 _IO_lock_lock (list_all_lock
);
823 last_stamp
= _IO_list_all_stamp
;
824 fp
= (_IO_FILE
*) INTUSE(_IO_list_all
);
831 if (((fp
->_mode
<= 0 && fp
->_IO_write_ptr
> fp
->_IO_write_base
)
832 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
833 || (_IO_vtable_offset (fp
) == 0
834 && fp
->_mode
> 0 && (fp
->_wide_data
->_IO_write_ptr
835 > fp
->_wide_data
->_IO_write_base
))
838 && _IO_OVERFLOW (fp
, EOF
) == EOF
)
842 _IO_funlockfile (fp
);
845 if (last_stamp
!= _IO_list_all_stamp
)
847 /* Something was added to the list. Start all over again. */
848 fp
= (_IO_FILE
*) INTUSE(_IO_list_all
);
849 last_stamp
= _IO_list_all_stamp
;
857 _IO_lock_unlock (list_all_lock
);
858 _IO_cleanup_region_end (0);
868 /* We want locking. */
869 return _IO_flush_all_lockp (1);
871 INTDEF(_IO_flush_all
)
874 _IO_flush_all_linebuffered ()
880 _IO_cleanup_region_start_noarg (flush_cleanup
);
881 _IO_lock_lock (list_all_lock
);
884 last_stamp
= _IO_list_all_stamp
;
885 fp
= (_IO_FILE
*) INTUSE(_IO_list_all
);
891 if ((fp
->_flags
& _IO_NO_WRITES
) == 0 && fp
->_flags
& _IO_LINE_BUF
)
892 _IO_OVERFLOW (fp
, EOF
);
894 _IO_funlockfile (fp
);
897 if (last_stamp
!= _IO_list_all_stamp
)
899 /* Something was added to the list. Start all over again. */
900 fp
= (_IO_FILE
*) INTUSE(_IO_list_all
);
901 last_stamp
= _IO_list_all_stamp
;
908 _IO_lock_unlock (list_all_lock
);
909 _IO_cleanup_region_end (0);
912 INTDEF(_IO_flush_all_linebuffered
)
914 weak_alias (_IO_flush_all_linebuffered
, _flushlbf
)
917 static void _IO_unbuffer_write
__P ((void));
920 _IO_unbuffer_write ()
923 for (fp
= (_IO_FILE
*) INTUSE(_IO_list_all
); fp
; fp
= fp
->_chain
)
925 if (! (fp
->_flags
& _IO_UNBUFFERED
)
926 && (! (fp
->_flags
& _IO_NO_WRITES
)
927 || (fp
->_flags
& _IO_IS_APPENDING
))
928 /* Iff stream is un-orientated, it wasn't used. */
930 _IO_SETBUF (fp
, NULL
, 0);
932 /* Make sure that never again the wide char functions can be
941 /* We do *not* want locking. Some threads might use streams but
942 that is there problem, we flush them underneath them. */
943 int result
= _IO_flush_all_lockp (0);
945 /* We currently don't have a reliable mechanism for making sure that
946 C++ static destructors are executed in the correct order.
947 So it is possible that other static destructors might want to
948 write to cout - and they're supposed to be able to do so.
950 The following will make the standard streambufs be unbuffered,
951 which forces any output from late destructors to be written out. */
952 _IO_unbuffer_write ();
959 _IO_init_marker (marker
, fp
)
960 struct _IO_marker
*marker
;
964 if (_IO_in_put_mode (fp
))
965 INTUSE(_IO_switch_to_get_mode
) (fp
);
966 if (_IO_in_backup (fp
))
967 marker
->_pos
= fp
->_IO_read_ptr
- fp
->_IO_read_end
;
969 marker
->_pos
= fp
->_IO_read_ptr
- fp
->_IO_read_base
;
971 /* Should perhaps sort the chain? */
972 marker
->_next
= fp
->_markers
;
973 fp
->_markers
= marker
;
977 _IO_remove_marker (marker
)
978 struct _IO_marker
*marker
;
980 /* Unlink from sb's chain. */
981 struct _IO_marker
**ptr
= &marker
->_sbuf
->_markers
;
982 for (; ; ptr
= &(*ptr
)->_next
)
986 else if (*ptr
== marker
)
988 *ptr
= marker
->_next
;
993 if _sbuf has a backup area that is no longer needed
, should we
delete
994 it now
, or wait until the next underflow
?
998 #define BAD_DELTA EOF
1001 _IO_marker_difference (mark1
, mark2
)
1002 struct _IO_marker
*mark1
;
1003 struct _IO_marker
*mark2
;
1005 return mark1
->_pos
- mark2
->_pos
;
1008 /* Return difference between MARK and current position of MARK's stream. */
1010 _IO_marker_delta (mark
)
1011 struct _IO_marker
*mark
;
1014 if (mark
->_sbuf
== NULL
)
1016 if (_IO_in_backup (mark
->_sbuf
))
1017 cur_pos
= mark
->_sbuf
->_IO_read_ptr
- mark
->_sbuf
->_IO_read_end
;
1019 cur_pos
= mark
->_sbuf
->_IO_read_ptr
- mark
->_sbuf
->_IO_read_base
;
1020 return mark
->_pos
- cur_pos
;
1024 _IO_seekmark (fp
, mark
, delta
)
1026 struct _IO_marker
*mark
;
1029 if (mark
->_sbuf
!= fp
)
1031 if (mark
->_pos
>= 0)
1033 if (_IO_in_backup (fp
))
1034 _IO_switch_to_main_get_area (fp
);
1035 fp
->_IO_read_ptr
= fp
->_IO_read_base
+ mark
->_pos
;
1039 if (!_IO_in_backup (fp
))
1040 _IO_switch_to_backup_area (fp
);
1041 fp
->_IO_read_ptr
= fp
->_IO_read_end
+ mark
->_pos
;
1047 _IO_unsave_markers (fp
)
1050 struct _IO_marker
*mark
= fp
->_markers
;
1054 streampos offset
= seekoff (0, ios::cur
, ios::in
);
1057 offset
+= eGptr () - Gbase ();
1058 for ( ; mark
!= NULL
; mark
= mark
->_next
)
1059 mark
->set_streampos (mark
->_pos
+ offset
);
1063 for ( ; mark
!= NULL
; mark
= mark
->_next
)
1064 mark
->set_streampos (EOF
);
1070 if (_IO_have_backup (fp
))
1071 INTUSE(_IO_free_backup_area
) (fp
);
1073 INTDEF(_IO_unsave_markers
)
1076 /* Seems not to be needed. --drepper */
1078 _IO_nobackup_pbackfail (fp
, c
)
1082 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
)
1084 if (c
!= EOF
&& *fp
->_IO_read_ptr
!= c
)
1085 *fp
->_IO_read_ptr
= c
;
1086 return (unsigned char) c
;
1091 _IO_default_pbackfail (fp
, c
)
1095 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
&& !_IO_in_backup (fp
)
1096 && (unsigned char) fp
->_IO_read_ptr
[-1] == c
)
1100 /* Need to handle a filebuf in write mode (switch to read mode). FIXME!*/
1101 if (!_IO_in_backup (fp
))
1103 /* We need to keep the invariant that the main get area
1104 logically follows the backup area. */
1105 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
&& _IO_have_backup (fp
))
1107 if (save_for_backup (fp
, fp
->_IO_read_ptr
))
1110 else if (!_IO_have_backup (fp
))
1112 /* No backup buffer: allocate one. */
1113 /* Use nshort buffer, if unused? (probably not) FIXME */
1114 int backup_size
= 128;
1115 char *bbuf
= (char *) malloc (backup_size
);
1118 fp
->_IO_save_base
= bbuf
;
1119 fp
->_IO_save_end
= fp
->_IO_save_base
+ backup_size
;
1120 fp
->_IO_backup_base
= fp
->_IO_save_end
;
1122 fp
->_IO_read_base
= fp
->_IO_read_ptr
;
1123 _IO_switch_to_backup_area (fp
);
1125 else if (fp
->_IO_read_ptr
<= fp
->_IO_read_base
)
1127 /* Increase size of existing backup buffer. */
1128 _IO_size_t new_size
;
1129 _IO_size_t old_size
= fp
->_IO_read_end
- fp
->_IO_read_base
;
1131 new_size
= 2 * old_size
;
1132 new_buf
= (char *) malloc (new_size
);
1133 if (new_buf
== NULL
)
1135 memcpy (new_buf
+ (new_size
- old_size
), fp
->_IO_read_base
,
1137 free (fp
->_IO_read_base
);
1138 _IO_setg (fp
, new_buf
, new_buf
+ (new_size
- old_size
),
1139 new_buf
+ new_size
);
1140 fp
->_IO_backup_base
= fp
->_IO_read_ptr
;
1143 *--fp
->_IO_read_ptr
= c
;
1145 return (unsigned char) c
;
1147 INTDEF(_IO_default_pbackfail
)
1150 _IO_default_seek (fp
, offset
, dir
)
1159 _IO_default_stat (fp
, st
)
1167 _IO_default_read (fp
, data
, n
)
1176 _IO_default_write (fp
, data
, n
)
1185 _IO_default_showmanyc (fp
)
1192 _IO_default_imbue (fp
, locale
)
1201 return (_IO_ITER
) INTUSE(_IO_list_all
);
1203 libc_hidden_def (_IO_iter_begin
)
1210 libc_hidden_def (_IO_iter_end
)
1216 return iter
->_chain
;
1218 libc_hidden_def (_IO_iter_next
)
1226 libc_hidden_def (_IO_iter_file
)
1231 #ifdef _IO_MTSAFE_IO
1232 _IO_lock_lock (list_all_lock
);
1235 libc_hidden_def (_IO_list_lock
)
1240 #ifdef _IO_MTSAFE_IO
1241 _IO_lock_unlock (list_all_lock
);
1244 libc_hidden_def (_IO_list_unlock
)
1247 _IO_list_resetlock()
1249 #ifdef _IO_MTSAFE_IO
1250 _IO_lock_init (list_all_lock
);
1253 libc_hidden_def (_IO_list_resetlock
)
1258 #define IO_CLEANUP ;
1266 ~__io_defs() { _IO_cleanup (); }
1268 __io_defs io_defs__
;
1273 #ifdef text_set_element
1274 text_set_element(__libc_atexit
, _IO_cleanup
);