1 /* Copyright (C) 1993-2017 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, see
16 <http://www.gnu.org/licenses/>.
18 As a special exception, if you link the code in this file with
19 files compiled with a GNU compiler to produce an executable,
20 that does not cause the resulting executable to be covered by
21 the GNU Lesser General Public License. This exception does not
22 however invalidate any other reasons why the executable file
23 might be covered by the GNU Lesser General Public License.
24 This exception applies to code released by its copyright holders
25 in files containing the exception. */
27 /* Generic or default I/O operations. */
38 static _IO_lock_t list_all_lock
= _IO_lock_initializer
;
41 /* Used to signal modifications to the list of FILE decriptors. */
42 static int _IO_list_all_stamp
;
45 static _IO_FILE
*run_fp
;
49 flush_cleanup (void *not_used
)
52 _IO_funlockfile (run_fp
);
53 _IO_lock_unlock (list_all_lock
);
58 _IO_un_link (struct _IO_FILE_plus
*fp
)
60 if (fp
->file
._flags
& _IO_LINKED
)
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 if (_IO_list_all
== NULL
)
71 else if (fp
== _IO_list_all
)
73 _IO_list_all
= (struct _IO_FILE_plus
*) _IO_list_all
->file
._chain
;
77 for (f
= &_IO_list_all
->file
._chain
; *f
; f
= &(*f
)->_chain
)
78 if (*f
== (_IO_FILE
*) fp
)
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);
93 libc_hidden_def (_IO_un_link
)
96 _IO_link_in (struct _IO_FILE_plus
*fp
)
98 if ((fp
->file
._flags
& _IO_LINKED
) == 0)
100 fp
->file
._flags
|= _IO_LINKED
;
102 _IO_cleanup_region_start_noarg (flush_cleanup
);
103 _IO_lock_lock (list_all_lock
);
104 run_fp
= (_IO_FILE
*) fp
;
105 _IO_flockfile ((_IO_FILE
*) fp
);
107 fp
->file
._chain
= (_IO_FILE
*) _IO_list_all
;
109 ++_IO_list_all_stamp
;
111 _IO_funlockfile ((_IO_FILE
*) fp
);
113 _IO_lock_unlock (list_all_lock
);
114 _IO_cleanup_region_end (0);
118 libc_hidden_def (_IO_link_in
)
120 /* Return minimum _pos markers
121 Assumes the current get area is the main get area. */
122 _IO_ssize_t
_IO_least_marker (_IO_FILE
*fp
, char *end_p
);
125 _IO_least_marker (_IO_FILE
*fp
, char *end_p
)
127 _IO_ssize_t least_so_far
= end_p
- fp
->_IO_read_base
;
128 struct _IO_marker
*mark
;
129 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
130 if (mark
->_pos
< least_so_far
)
131 least_so_far
= mark
->_pos
;
135 /* Switch current get area from backup buffer to (start of) main get area. */
138 _IO_switch_to_main_get_area (_IO_FILE
*fp
)
141 fp
->_flags
&= ~_IO_IN_BACKUP
;
142 /* Swap _IO_read_end and _IO_save_end. */
143 tmp
= fp
->_IO_read_end
;
144 fp
->_IO_read_end
= fp
->_IO_save_end
;
145 fp
->_IO_save_end
= tmp
;
146 /* Swap _IO_read_base and _IO_save_base. */
147 tmp
= fp
->_IO_read_base
;
148 fp
->_IO_read_base
= fp
->_IO_save_base
;
149 fp
->_IO_save_base
= tmp
;
150 /* Set _IO_read_ptr. */
151 fp
->_IO_read_ptr
= fp
->_IO_read_base
;
154 /* Switch current get area from main get area to (end of) backup area. */
157 _IO_switch_to_backup_area (_IO_FILE
*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 (_IO_FILE
*fp
)
176 if (fp
->_IO_write_ptr
> fp
->_IO_write_base
)
177 if (_IO_OVERFLOW (fp
, EOF
) == EOF
)
179 if (_IO_in_backup (fp
))
180 fp
->_IO_read_base
= fp
->_IO_backup_base
;
183 fp
->_IO_read_base
= fp
->_IO_buf_base
;
184 if (fp
->_IO_write_ptr
> fp
->_IO_read_end
)
185 fp
->_IO_read_end
= fp
->_IO_write_ptr
;
187 fp
->_IO_read_ptr
= fp
->_IO_write_ptr
;
189 fp
->_IO_write_base
= fp
->_IO_write_ptr
= fp
->_IO_write_end
= fp
->_IO_read_ptr
;
191 fp
->_flags
&= ~_IO_CURRENTLY_PUTTING
;
194 libc_hidden_def (_IO_switch_to_get_mode
)
197 _IO_free_backup_area (_IO_FILE
*fp
)
199 if (_IO_in_backup (fp
))
200 _IO_switch_to_main_get_area (fp
); /* Just in case. */
201 free (fp
->_IO_save_base
);
202 fp
->_IO_save_base
= NULL
;
203 fp
->_IO_save_end
= NULL
;
204 fp
->_IO_backup_base
= NULL
;
206 libc_hidden_def (_IO_free_backup_area
)
210 _IO_switch_to_put_mode (_IO_FILE
*fp
)
212 fp
->_IO_write_base
= fp
->_IO_read_ptr
;
213 fp
->_IO_write_ptr
= fp
->_IO_read_ptr
;
214 /* Following is wrong if line- or un-buffered? */
215 fp
->_IO_write_end
= (fp
->_flags
& _IO_IN_BACKUP
216 ? fp
->_IO_read_end
: fp
->_IO_buf_end
);
218 fp
->_IO_read_ptr
= fp
->_IO_read_end
;
219 fp
->_IO_read_base
= fp
->_IO_read_end
;
221 fp
->_flags
|= _IO_CURRENTLY_PUTTING
;
227 __overflow (_IO_FILE
*f
, int ch
)
229 /* This is a single-byte stream. */
232 return _IO_OVERFLOW (f
, ch
);
234 libc_hidden_def (__overflow
)
236 static int save_for_backup (_IO_FILE
*fp
, char *end_p
)
246 save_for_backup (_IO_FILE
*fp
, char *end_p
)
248 /* Append [_IO_read_base..end_p] to backup area. */
249 _IO_ssize_t least_mark
= _IO_least_marker (fp
, end_p
);
250 /* needed_size is how much space we need in the backup area. */
251 _IO_size_t needed_size
= (end_p
- fp
->_IO_read_base
) - least_mark
;
252 /* FIXME: Dubious arithmetic if pointers are NULL */
253 _IO_size_t current_Bsize
= fp
->_IO_save_end
- fp
->_IO_save_base
;
254 _IO_size_t avail
; /* Extra space available for future expansion. */
256 struct _IO_marker
*mark
;
257 if (needed_size
> current_Bsize
)
261 new_buffer
= (char *) malloc (avail
+ needed_size
);
262 if (new_buffer
== NULL
)
263 return EOF
; /* FIXME */
267 __mempcpy (__mempcpy (new_buffer
+ avail
,
268 fp
->_IO_save_end
+ least_mark
,
271 end_p
- fp
->_IO_read_base
);
273 memcpy (new_buffer
+ avail
,
274 fp
->_IO_save_end
+ least_mark
,
276 memcpy (new_buffer
+ avail
- least_mark
,
278 end_p
- fp
->_IO_read_base
);
282 memcpy (new_buffer
+ avail
,
283 fp
->_IO_read_base
+ least_mark
,
285 free (fp
->_IO_save_base
);
286 fp
->_IO_save_base
= new_buffer
;
287 fp
->_IO_save_end
= new_buffer
+ avail
+ needed_size
;
291 avail
= current_Bsize
- needed_size
;
294 memmove (fp
->_IO_save_base
+ avail
,
295 fp
->_IO_save_end
+ least_mark
,
297 memcpy (fp
->_IO_save_base
+ avail
- least_mark
,
299 end_p
- fp
->_IO_read_base
);
301 else if (needed_size
> 0)
302 memcpy (fp
->_IO_save_base
+ avail
,
303 fp
->_IO_read_base
+ least_mark
,
306 fp
->_IO_backup_base
= fp
->_IO_save_base
+ avail
;
307 /* Adjust all the streammarkers. */
308 delta
= end_p
- fp
->_IO_read_base
;
309 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
315 __underflow (_IO_FILE
*fp
)
317 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
318 if (_IO_vtable_offset (fp
) == 0 && _IO_fwide (fp
, -1) != -1)
324 if (_IO_in_put_mode (fp
))
325 if (_IO_switch_to_get_mode (fp
) == EOF
)
327 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
328 return *(unsigned char *) fp
->_IO_read_ptr
;
329 if (_IO_in_backup (fp
))
331 _IO_switch_to_main_get_area (fp
);
332 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
333 return *(unsigned char *) fp
->_IO_read_ptr
;
335 if (_IO_have_markers (fp
))
337 if (save_for_backup (fp
, fp
->_IO_read_end
))
340 else if (_IO_have_backup (fp
))
341 _IO_free_backup_area (fp
);
342 return _IO_UNDERFLOW (fp
);
344 libc_hidden_def (__underflow
)
347 __uflow (_IO_FILE
*fp
)
349 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
350 if (_IO_vtable_offset (fp
) == 0 && _IO_fwide (fp
, -1) != -1)
356 if (_IO_in_put_mode (fp
))
357 if (_IO_switch_to_get_mode (fp
) == EOF
)
359 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
360 return *(unsigned char *) fp
->_IO_read_ptr
++;
361 if (_IO_in_backup (fp
))
363 _IO_switch_to_main_get_area (fp
);
364 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
365 return *(unsigned char *) fp
->_IO_read_ptr
++;
367 if (_IO_have_markers (fp
))
369 if (save_for_backup (fp
, fp
->_IO_read_end
))
372 else if (_IO_have_backup (fp
))
373 _IO_free_backup_area (fp
);
374 return _IO_UFLOW (fp
);
376 libc_hidden_def (__uflow
)
379 _IO_setb (_IO_FILE
*f
, char *b
, char *eb
, int a
)
381 if (f
->_IO_buf_base
&& !(f
->_flags
& _IO_USER_BUF
))
382 free (f
->_IO_buf_base
);
386 f
->_flags
&= ~_IO_USER_BUF
;
388 f
->_flags
|= _IO_USER_BUF
;
390 libc_hidden_def (_IO_setb
)
393 _IO_doallocbuf (_IO_FILE
*fp
)
395 if (fp
->_IO_buf_base
)
397 if (!(fp
->_flags
& _IO_UNBUFFERED
) || fp
->_mode
> 0)
398 if (_IO_DOALLOCATE (fp
) != EOF
)
400 _IO_setb (fp
, fp
->_shortbuf
, fp
->_shortbuf
+1, 0);
402 libc_hidden_def (_IO_doallocbuf
)
405 _IO_default_underflow (_IO_FILE
*fp
)
411 _IO_default_uflow (_IO_FILE
*fp
)
413 int ch
= _IO_UNDERFLOW (fp
);
416 return *(unsigned char *) fp
->_IO_read_ptr
++;
418 libc_hidden_def (_IO_default_uflow
)
421 _IO_default_xsputn (_IO_FILE
*f
, const void *data
, _IO_size_t n
)
423 const char *s
= (char *) data
;
429 /* Space available. */
430 if (f
->_IO_write_ptr
< f
->_IO_write_end
)
432 _IO_size_t count
= f
->_IO_write_end
- f
->_IO_write_ptr
;
438 f
->_IO_write_ptr
= __mempcpy (f
->_IO_write_ptr
, s
, count
);
440 memcpy (f
->_IO_write_ptr
, s
, count
);
441 f
->_IO_write_ptr
+= count
;
447 char *p
= f
->_IO_write_ptr
;
449 for (i
= count
; --i
>= 0; )
451 f
->_IO_write_ptr
= p
;
455 if (more
== 0 || _IO_OVERFLOW (f
, (unsigned char) *s
++) == EOF
)
461 libc_hidden_def (_IO_default_xsputn
)
464 _IO_sgetn (_IO_FILE
*fp
, void *data
, _IO_size_t n
)
466 /* FIXME handle putback buffer here! */
467 return _IO_XSGETN (fp
, data
, n
);
469 libc_hidden_def (_IO_sgetn
)
472 _IO_default_xsgetn (_IO_FILE
*fp
, void *data
, _IO_size_t n
)
475 char *s
= (char*) data
;
478 /* Data available. */
479 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
481 _IO_size_t count
= fp
->_IO_read_end
- fp
->_IO_read_ptr
;
487 s
= __mempcpy (s
, fp
->_IO_read_ptr
, count
);
489 memcpy (s
, fp
->_IO_read_ptr
, count
);
492 fp
->_IO_read_ptr
+= count
;
496 char *p
= fp
->_IO_read_ptr
;
500 fp
->_IO_read_ptr
= p
;
504 if (more
== 0 || __underflow (fp
) == EOF
)
509 libc_hidden_def (_IO_default_xsgetn
)
512 /* Seems not to be needed. --drepper */
514 _IO_sync (_IO_FILE
*fp
)
521 _IO_default_setbuf (_IO_FILE
*fp
, char *p
, _IO_ssize_t len
)
523 if (_IO_SYNC (fp
) == EOF
)
525 if (p
== NULL
|| len
== 0)
527 fp
->_flags
|= _IO_UNBUFFERED
;
528 _IO_setb (fp
, fp
->_shortbuf
, fp
->_shortbuf
+1, 0);
532 fp
->_flags
&= ~_IO_UNBUFFERED
;
533 _IO_setb (fp
, p
, p
+len
, 0);
535 fp
->_IO_write_base
= fp
->_IO_write_ptr
= fp
->_IO_write_end
= 0;
536 fp
->_IO_read_base
= fp
->_IO_read_ptr
= fp
->_IO_read_end
= 0;
541 _IO_default_seekpos (_IO_FILE
*fp
, _IO_off64_t pos
, int mode
)
543 return _IO_SEEKOFF (fp
, pos
, 0, mode
);
547 _IO_default_doallocate (_IO_FILE
*fp
)
551 buf
= malloc(_IO_BUFSIZ
);
552 if (__glibc_unlikely (buf
== NULL
))
555 _IO_setb (fp
, buf
, buf
+_IO_BUFSIZ
, 1);
558 libc_hidden_def (_IO_default_doallocate
)
561 _IO_init_internal (_IO_FILE
*fp
, int flags
)
563 _IO_no_init (fp
, flags
, -1, NULL
, NULL
);
567 _IO_init (_IO_FILE
*fp
, int flags
)
569 IO_set_accept_foreign_vtables (&_IO_vtable_check
);
570 _IO_init_internal (fp
, flags
);
573 static int stdio_needs_locking
;
575 /* In a single-threaded process most stdio locks can be omitted. After
576 _IO_enable_locks is called, locks are not optimized away any more.
577 It must be first called while the process is still single-threaded.
579 This lock optimization can be disabled on a per-file basis by setting
580 _IO_FLAGS2_NEED_LOCK, because a file can have user-defined callbacks
581 or can be locked with flockfile and then a thread may be created
582 between a lock and unlock, so omitting the lock is not valid.
584 Here we have to make sure that the flag is set on all existing files
585 and files created later. */
587 _IO_enable_locks (void)
591 if (stdio_needs_locking
)
593 stdio_needs_locking
= 1;
594 for (i
= _IO_iter_begin (); i
!= _IO_iter_end (); i
= _IO_iter_next (i
))
595 _IO_iter_file (i
)->_flags2
|= _IO_FLAGS2_NEED_LOCK
;
597 libc_hidden_def (_IO_enable_locks
)
600 _IO_old_init (_IO_FILE
*fp
, int flags
)
602 fp
->_flags
= _IO_MAGIC
|flags
;
604 if (stdio_needs_locking
)
605 fp
->_flags2
|= _IO_FLAGS2_NEED_LOCK
;
606 fp
->_IO_buf_base
= NULL
;
607 fp
->_IO_buf_end
= NULL
;
608 fp
->_IO_read_base
= NULL
;
609 fp
->_IO_read_ptr
= NULL
;
610 fp
->_IO_read_end
= NULL
;
611 fp
->_IO_write_base
= NULL
;
612 fp
->_IO_write_ptr
= NULL
;
613 fp
->_IO_write_end
= NULL
;
614 fp
->_chain
= NULL
; /* Not necessary. */
616 fp
->_IO_save_base
= NULL
;
617 fp
->_IO_backup_base
= NULL
;
618 fp
->_IO_save_end
= NULL
;
622 fp
->_vtable_offset
= 0;
625 if (fp
->_lock
!= NULL
)
626 _IO_lock_init (*fp
->_lock
);
631 _IO_no_init (_IO_FILE
*fp
, int flags
, int orientation
,
632 struct _IO_wide_data
*wd
, const struct _IO_jump_t
*jmp
)
634 _IO_old_init (fp
, flags
);
635 fp
->_mode
= orientation
;
636 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
637 if (orientation
>= 0)
640 fp
->_wide_data
->_IO_buf_base
= NULL
;
641 fp
->_wide_data
->_IO_buf_end
= NULL
;
642 fp
->_wide_data
->_IO_read_base
= NULL
;
643 fp
->_wide_data
->_IO_read_ptr
= NULL
;
644 fp
->_wide_data
->_IO_read_end
= NULL
;
645 fp
->_wide_data
->_IO_write_base
= NULL
;
646 fp
->_wide_data
->_IO_write_ptr
= NULL
;
647 fp
->_wide_data
->_IO_write_end
= NULL
;
648 fp
->_wide_data
->_IO_save_base
= NULL
;
649 fp
->_wide_data
->_IO_backup_base
= NULL
;
650 fp
->_wide_data
->_IO_save_end
= NULL
;
652 fp
->_wide_data
->_wide_vtable
= jmp
;
655 /* Cause predictable crash when a wide function is called on a byte
657 fp
->_wide_data
= (struct _IO_wide_data
*) -1L;
659 fp
->_freeres_list
= NULL
;
663 _IO_default_sync (_IO_FILE
*fp
)
668 /* The way the C++ classes are mapped into the C functions in the
669 current implementation, this function can get called twice! */
672 _IO_default_finish (_IO_FILE
*fp
, int dummy
)
674 struct _IO_marker
*mark
;
675 if (fp
->_IO_buf_base
&& !(fp
->_flags
& _IO_USER_BUF
))
677 free (fp
->_IO_buf_base
);
678 fp
->_IO_buf_base
= fp
->_IO_buf_end
= NULL
;
681 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
684 if (fp
->_IO_save_base
)
686 free (fp
->_IO_save_base
);
687 fp
->_IO_save_base
= NULL
;
690 _IO_un_link ((struct _IO_FILE_plus
*) fp
);
693 if (fp
->_lock
!= NULL
)
694 _IO_lock_fini (*fp
->_lock
);
697 libc_hidden_def (_IO_default_finish
)
700 _IO_default_seekoff (_IO_FILE
*fp
, _IO_off64_t offset
, int dir
, int mode
)
706 _IO_sputbackc (_IO_FILE
*fp
, int c
)
710 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
711 && (unsigned char)fp
->_IO_read_ptr
[-1] == (unsigned char)c
)
714 result
= (unsigned char) c
;
717 result
= _IO_PBACKFAIL (fp
, c
);
720 fp
->_flags
&= ~_IO_EOF_SEEN
;
724 libc_hidden_def (_IO_sputbackc
)
727 _IO_sungetc (_IO_FILE
*fp
)
731 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
)
734 result
= (unsigned char) *fp
->_IO_read_ptr
;
737 result
= _IO_PBACKFAIL (fp
, EOF
);
740 fp
->_flags
&= ~_IO_EOF_SEEN
;
745 #if 0 /* Work in progress */
746 /* Seems not to be needed. */
749 _IO_set_column (_IO_FILE
*fp
, int c
)
754 fp
->_column
= c
- (fp
->_IO_write_ptr
- fp
->_IO_write_base
);
758 _IO_set_column (_IO_FILE
*fp
, int i
)
760 fp
->_cur_column
= i
+ 1;
768 _IO_adjust_column (unsigned start
, const char *line
, int count
)
770 const char *ptr
= line
+ count
;
773 return line
+ count
- ptr
- 1;
774 return start
+ count
;
776 libc_hidden_def (_IO_adjust_column
)
779 /* Seems not to be needed. --drepper */
781 _IO_get_column (_IO_FILE
*fp
)
784 return _IO_adjust_column (fp
->_cur_column
- 1,
786 fp
->_IO_write_ptr
- fp
->_IO_write_base
);
793 _IO_flush_all_lockp (int do_lock
)
800 __libc_cleanup_region_start (do_lock
, flush_cleanup
, NULL
);
802 _IO_lock_lock (list_all_lock
);
805 last_stamp
= _IO_list_all_stamp
;
806 fp
= (_IO_FILE
*) _IO_list_all
;
813 if (((fp
->_mode
<= 0 && fp
->_IO_write_ptr
> fp
->_IO_write_base
)
814 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
815 || (_IO_vtable_offset (fp
) == 0
816 && fp
->_mode
> 0 && (fp
->_wide_data
->_IO_write_ptr
817 > fp
->_wide_data
->_IO_write_base
))
820 && _IO_OVERFLOW (fp
, EOF
) == EOF
)
824 _IO_funlockfile (fp
);
827 if (last_stamp
!= _IO_list_all_stamp
)
829 /* Something was added to the list. Start all over again. */
830 fp
= (_IO_FILE
*) _IO_list_all
;
831 last_stamp
= _IO_list_all_stamp
;
839 _IO_lock_unlock (list_all_lock
);
840 __libc_cleanup_region_end (0);
850 /* We want locking. */
851 return _IO_flush_all_lockp (1);
853 libc_hidden_def (_IO_flush_all
)
856 _IO_flush_all_linebuffered (void)
862 _IO_cleanup_region_start_noarg (flush_cleanup
);
863 _IO_lock_lock (list_all_lock
);
866 last_stamp
= _IO_list_all_stamp
;
867 fp
= (_IO_FILE
*) _IO_list_all
;
873 if ((fp
->_flags
& _IO_NO_WRITES
) == 0 && fp
->_flags
& _IO_LINE_BUF
)
874 _IO_OVERFLOW (fp
, EOF
);
876 _IO_funlockfile (fp
);
879 if (last_stamp
!= _IO_list_all_stamp
)
881 /* Something was added to the list. Start all over again. */
882 fp
= (_IO_FILE
*) _IO_list_all
;
883 last_stamp
= _IO_list_all_stamp
;
890 _IO_lock_unlock (list_all_lock
);
891 _IO_cleanup_region_end (0);
894 libc_hidden_def (_IO_flush_all_linebuffered
)
896 weak_alias (_IO_flush_all_linebuffered
, _flushlbf
)
900 /* The following is a bit tricky. In general, we want to unbuffer the
901 streams so that all output which follows is seen. If we are not
902 looking for memory leaks it does not make much sense to free the
903 actual buffer because this will happen anyway once the program
904 terminated. If we do want to look for memory leaks we have to free
905 the buffers. Whether something is freed is determined by the
906 function sin the libc_freeres section. Those are called as part of
907 the atexit routine, just like _IO_cleanup. The problem is we do
908 not know whether the freeres code is called first or _IO_cleanup.
909 if the former is the case, we set the DEALLOC_BUFFER variable to
910 true and _IO_unbuffer_all will take care of the rest. If
911 _IO_unbuffer_all is called first we add the streams to a list
912 which the freeres function later can walk through. */
913 static void _IO_unbuffer_all (void);
915 static bool dealloc_buffers
;
916 static _IO_FILE
*freeres_list
;
919 _IO_unbuffer_all (void)
922 for (fp
= (_IO_FILE
*) _IO_list_all
; fp
; fp
= fp
->_chain
)
924 if (! (fp
->_flags
& _IO_UNBUFFERED
)
925 /* Iff stream is un-orientated, it wasn't used. */
931 for (cnt
= 0; cnt
< MAXTRIES
; ++cnt
)
932 if (fp
->_lock
== NULL
|| _IO_lock_trylock (*fp
->_lock
) == 0)
935 /* Give the other thread time to finish up its use of the
940 if (! dealloc_buffers
&& !(fp
->_flags
& _IO_USER_BUF
))
942 fp
->_flags
|= _IO_USER_BUF
;
944 fp
->_freeres_list
= freeres_list
;
946 fp
->_freeres_buf
= fp
->_IO_buf_base
;
949 _IO_SETBUF (fp
, NULL
, 0);
952 _IO_wsetb (fp
, NULL
, NULL
, 0);
955 if (cnt
< MAXTRIES
&& fp
->_lock
!= NULL
)
956 _IO_lock_unlock (*fp
->_lock
);
960 /* Make sure that never again the wide char functions can be
967 libc_freeres_fn (buffer_free
)
969 dealloc_buffers
= true;
971 while (freeres_list
!= NULL
)
973 free (freeres_list
->_freeres_buf
);
975 freeres_list
= freeres_list
->_freeres_list
;
983 /* We do *not* want locking. Some threads might use streams but
984 that is their problem, we flush them underneath them. */
985 int result
= _IO_flush_all_lockp (0);
987 /* We currently don't have a reliable mechanism for making sure that
988 C++ static destructors are executed in the correct order.
989 So it is possible that other static destructors might want to
990 write to cout - and they're supposed to be able to do so.
992 The following will make the standard streambufs be unbuffered,
993 which forces any output from late destructors to be written out. */
1001 _IO_init_marker (struct _IO_marker
*marker
, _IO_FILE
*fp
)
1004 if (_IO_in_put_mode (fp
))
1005 _IO_switch_to_get_mode (fp
);
1006 if (_IO_in_backup (fp
))
1007 marker
->_pos
= fp
->_IO_read_ptr
- fp
->_IO_read_end
;
1009 marker
->_pos
= fp
->_IO_read_ptr
- fp
->_IO_read_base
;
1011 /* Should perhaps sort the chain? */
1012 marker
->_next
= fp
->_markers
;
1013 fp
->_markers
= marker
;
1017 _IO_remove_marker (struct _IO_marker
*marker
)
1019 /* Unlink from sb's chain. */
1020 struct _IO_marker
**ptr
= &marker
->_sbuf
->_markers
;
1021 for (; ; ptr
= &(*ptr
)->_next
)
1025 else if (*ptr
== marker
)
1027 *ptr
= marker
->_next
;
1032 if _sbuf has a backup area that is no longer needed
, should we
delete
1033 it now
, or wait until the next underflow
?
1037 #define BAD_DELTA EOF
1040 _IO_marker_difference (struct _IO_marker
*mark1
, struct _IO_marker
*mark2
)
1042 return mark1
->_pos
- mark2
->_pos
;
1045 /* Return difference between MARK and current position of MARK's stream. */
1047 _IO_marker_delta (struct _IO_marker
*mark
)
1050 if (mark
->_sbuf
== NULL
)
1052 if (_IO_in_backup (mark
->_sbuf
))
1053 cur_pos
= mark
->_sbuf
->_IO_read_ptr
- mark
->_sbuf
->_IO_read_end
;
1055 cur_pos
= mark
->_sbuf
->_IO_read_ptr
- mark
->_sbuf
->_IO_read_base
;
1056 return mark
->_pos
- cur_pos
;
1060 _IO_seekmark (_IO_FILE
*fp
, struct _IO_marker
*mark
, int delta
)
1062 if (mark
->_sbuf
!= fp
)
1064 if (mark
->_pos
>= 0)
1066 if (_IO_in_backup (fp
))
1067 _IO_switch_to_main_get_area (fp
);
1068 fp
->_IO_read_ptr
= fp
->_IO_read_base
+ mark
->_pos
;
1072 if (!_IO_in_backup (fp
))
1073 _IO_switch_to_backup_area (fp
);
1074 fp
->_IO_read_ptr
= fp
->_IO_read_end
+ mark
->_pos
;
1080 _IO_unsave_markers (_IO_FILE
*fp
)
1082 struct _IO_marker
*mark
= fp
->_markers
;
1086 streampos offset
= seekoff (0, ios::cur
, ios::in
);
1089 offset
+= eGptr () - Gbase ();
1090 for ( ; mark
!= NULL
; mark
= mark
->_next
)
1091 mark
->set_streampos (mark
->_pos
+ offset
);
1095 for ( ; mark
!= NULL
; mark
= mark
->_next
)
1096 mark
->set_streampos (EOF
);
1102 if (_IO_have_backup (fp
))
1103 _IO_free_backup_area (fp
);
1105 libc_hidden_def (_IO_unsave_markers
)
1108 /* Seems not to be needed. --drepper */
1110 _IO_nobackup_pbackfail (_IO_FILE
*fp
, int c
)
1112 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
)
1114 if (c
!= EOF
&& *fp
->_IO_read_ptr
!= c
)
1115 *fp
->_IO_read_ptr
= c
;
1116 return (unsigned char) c
;
1121 _IO_default_pbackfail (_IO_FILE
*fp
, int c
)
1123 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
&& !_IO_in_backup (fp
)
1124 && (unsigned char) fp
->_IO_read_ptr
[-1] == c
)
1128 /* Need to handle a filebuf in write mode (switch to read mode). FIXME!*/
1129 if (!_IO_in_backup (fp
))
1131 /* We need to keep the invariant that the main get area
1132 logically follows the backup area. */
1133 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
&& _IO_have_backup (fp
))
1135 if (save_for_backup (fp
, fp
->_IO_read_ptr
))
1138 else if (!_IO_have_backup (fp
))
1140 /* No backup buffer: allocate one. */
1141 /* Use nshort buffer, if unused? (probably not) FIXME */
1142 int backup_size
= 128;
1143 char *bbuf
= (char *) malloc (backup_size
);
1146 fp
->_IO_save_base
= bbuf
;
1147 fp
->_IO_save_end
= fp
->_IO_save_base
+ backup_size
;
1148 fp
->_IO_backup_base
= fp
->_IO_save_end
;
1150 fp
->_IO_read_base
= fp
->_IO_read_ptr
;
1151 _IO_switch_to_backup_area (fp
);
1153 else if (fp
->_IO_read_ptr
<= fp
->_IO_read_base
)
1155 /* Increase size of existing backup buffer. */
1156 _IO_size_t new_size
;
1157 _IO_size_t old_size
= fp
->_IO_read_end
- fp
->_IO_read_base
;
1159 new_size
= 2 * old_size
;
1160 new_buf
= (char *) malloc (new_size
);
1161 if (new_buf
== NULL
)
1163 memcpy (new_buf
+ (new_size
- old_size
), fp
->_IO_read_base
,
1165 free (fp
->_IO_read_base
);
1166 _IO_setg (fp
, new_buf
, new_buf
+ (new_size
- old_size
),
1167 new_buf
+ new_size
);
1168 fp
->_IO_backup_base
= fp
->_IO_read_ptr
;
1171 *--fp
->_IO_read_ptr
= c
;
1173 return (unsigned char) c
;
1175 libc_hidden_def (_IO_default_pbackfail
)
1178 _IO_default_seek (_IO_FILE
*fp
, _IO_off64_t offset
, int dir
)
1184 _IO_default_stat (_IO_FILE
*fp
, void *st
)
1190 _IO_default_read (_IO_FILE
*fp
, void *data
, _IO_ssize_t n
)
1196 _IO_default_write (_IO_FILE
*fp
, const void *data
, _IO_ssize_t n
)
1202 _IO_default_showmanyc (_IO_FILE
*fp
)
1208 _IO_default_imbue (_IO_FILE
*fp
, void *locale
)
1213 _IO_iter_begin (void)
1215 return (_IO_ITER
) _IO_list_all
;
1217 libc_hidden_def (_IO_iter_begin
)
1224 libc_hidden_def (_IO_iter_end
)
1227 _IO_iter_next (_IO_ITER iter
)
1229 return iter
->_chain
;
1231 libc_hidden_def (_IO_iter_next
)
1234 _IO_iter_file (_IO_ITER iter
)
1238 libc_hidden_def (_IO_iter_file
)
1241 _IO_list_lock (void)
1243 #ifdef _IO_MTSAFE_IO
1244 _IO_lock_lock (list_all_lock
);
1247 libc_hidden_def (_IO_list_lock
)
1250 _IO_list_unlock (void)
1252 #ifdef _IO_MTSAFE_IO
1253 _IO_lock_unlock (list_all_lock
);
1256 libc_hidden_def (_IO_list_unlock
)
1259 _IO_list_resetlock (void)
1261 #ifdef _IO_MTSAFE_IO
1262 _IO_lock_init (list_all_lock
);
1265 libc_hidden_def (_IO_list_resetlock
)
1270 #define IO_CLEANUP ;
1278 ~__io_defs() { _IO_cleanup (); }
1280 __io_defs io_defs__
;
1285 #ifdef text_set_element
1286 text_set_element(__libc_atexit
, _IO_cleanup
);