Mark BZ#10114 as fixed.
[glibc.git] / libio / genops.c
blob61e59f95a1e103c16218ae821da3162b17f83f9f
1 /* Copyright (C) 1993,1995,1997-2002, 2003, 2004, 2006, 2007, 2011-2012
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>.
19 As a special exception, if you link the code in this file with
20 files compiled with a GNU compiler to produce an executable,
21 that does not cause the resulting executable to be covered by
22 the GNU Lesser General Public License. This exception does not
23 however invalidate any other reasons why the executable file
24 might be covered by the GNU Lesser General Public License.
25 This exception applies to code released by its copyright holders
26 in files containing the exception. */
28 /* Generic or default I/O operations. */
30 #include "libioP.h"
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdbool.h>
34 #ifdef _LIBC
35 #include <sched.h>
36 #endif
38 #ifdef _IO_MTSAFE_IO
39 static _IO_lock_t list_all_lock = _IO_lock_initializer;
40 #endif
42 /* Used to signal modifications to the list of FILE decriptors. */
43 static int _IO_list_all_stamp;
46 static _IO_FILE *run_fp;
48 #ifdef _IO_MTSAFE_IO
49 static void
50 flush_cleanup (void *not_used)
52 if (run_fp != NULL)
53 _IO_funlockfile (run_fp);
54 _IO_lock_unlock (list_all_lock);
56 #endif
58 void
59 _IO_un_link (fp)
60 struct _IO_FILE_plus *fp;
62 if (fp->file._flags & _IO_LINKED)
64 struct _IO_FILE **f;
65 #ifdef _IO_MTSAFE_IO
66 _IO_cleanup_region_start_noarg (flush_cleanup);
67 _IO_lock_lock (list_all_lock);
68 run_fp = (_IO_FILE *) fp;
69 _IO_flockfile ((_IO_FILE *) fp);
70 #endif
71 if (_IO_list_all == NULL)
73 else if (fp == _IO_list_all)
75 _IO_list_all = (struct _IO_FILE_plus *) _IO_list_all->file._chain;
76 ++_IO_list_all_stamp;
78 else
79 for (f = &_IO_list_all->file._chain; *f; f = &(*f)->_chain)
80 if (*f == (_IO_FILE *) fp)
82 *f = fp->file._chain;
83 ++_IO_list_all_stamp;
84 break;
86 fp->file._flags &= ~_IO_LINKED;
87 #ifdef _IO_MTSAFE_IO
88 _IO_funlockfile ((_IO_FILE *) fp);
89 run_fp = NULL;
90 _IO_lock_unlock (list_all_lock);
91 _IO_cleanup_region_end (0);
92 #endif
95 libc_hidden_def (_IO_un_link)
97 void
98 _IO_link_in (fp)
99 struct _IO_FILE_plus *fp;
101 if ((fp->file._flags & _IO_LINKED) == 0)
103 fp->file._flags |= _IO_LINKED;
104 #ifdef _IO_MTSAFE_IO
105 _IO_cleanup_region_start_noarg (flush_cleanup);
106 _IO_lock_lock (list_all_lock);
107 run_fp = (_IO_FILE *) fp;
108 _IO_flockfile ((_IO_FILE *) fp);
109 #endif
110 fp->file._chain = (_IO_FILE *) _IO_list_all;
111 _IO_list_all = fp;
112 ++_IO_list_all_stamp;
113 #ifdef _IO_MTSAFE_IO
114 _IO_funlockfile ((_IO_FILE *) fp);
115 run_fp = NULL;
116 _IO_lock_unlock (list_all_lock);
117 _IO_cleanup_region_end (0);
118 #endif
121 libc_hidden_def (_IO_link_in)
123 /* Return minimum _pos markers
124 Assumes the current get area is the main get area. */
125 _IO_ssize_t _IO_least_marker (_IO_FILE *fp, char *end_p);
127 _IO_ssize_t
128 _IO_least_marker (fp, end_p)
129 _IO_FILE *fp;
130 char *end_p;
132 _IO_ssize_t least_so_far = end_p - fp->_IO_read_base;
133 struct _IO_marker *mark;
134 for (mark = fp->_markers; mark != NULL; mark = mark->_next)
135 if (mark->_pos < least_so_far)
136 least_so_far = mark->_pos;
137 return least_so_far;
140 /* Switch current get area from backup buffer to (start of) main get area. */
142 void
143 _IO_switch_to_main_get_area (fp)
144 _IO_FILE *fp;
146 char *tmp;
147 fp->_flags &= ~_IO_IN_BACKUP;
148 /* Swap _IO_read_end and _IO_save_end. */
149 tmp = fp->_IO_read_end;
150 fp->_IO_read_end = fp->_IO_save_end;
151 fp->_IO_save_end= tmp;
152 /* Swap _IO_read_base and _IO_save_base. */
153 tmp = fp->_IO_read_base;
154 fp->_IO_read_base = fp->_IO_save_base;
155 fp->_IO_save_base = tmp;
156 /* Set _IO_read_ptr. */
157 fp->_IO_read_ptr = fp->_IO_read_base;
160 /* Switch current get area from main get area to (end of) backup area. */
162 void
163 _IO_switch_to_backup_area (fp)
164 _IO_FILE *fp;
166 char *tmp;
167 fp->_flags |= _IO_IN_BACKUP;
168 /* Swap _IO_read_end and _IO_save_end. */
169 tmp = fp->_IO_read_end;
170 fp->_IO_read_end = fp->_IO_save_end;
171 fp->_IO_save_end = tmp;
172 /* Swap _IO_read_base and _IO_save_base. */
173 tmp = fp->_IO_read_base;
174 fp->_IO_read_base = fp->_IO_save_base;
175 fp->_IO_save_base = tmp;
176 /* Set _IO_read_ptr. */
177 fp->_IO_read_ptr = fp->_IO_read_end;
181 _IO_switch_to_get_mode (fp)
182 _IO_FILE *fp;
184 if (fp->_IO_write_ptr > fp->_IO_write_base)
185 if (_IO_OVERFLOW (fp, EOF) == EOF)
186 return EOF;
187 if (_IO_in_backup (fp))
188 fp->_IO_read_base = fp->_IO_backup_base;
189 else
191 fp->_IO_read_base = fp->_IO_buf_base;
192 if (fp->_IO_write_ptr > fp->_IO_read_end)
193 fp->_IO_read_end = fp->_IO_write_ptr;
195 fp->_IO_read_ptr = fp->_IO_write_ptr;
197 fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end = fp->_IO_read_ptr;
199 fp->_flags &= ~_IO_CURRENTLY_PUTTING;
200 return 0;
202 libc_hidden_def (_IO_switch_to_get_mode)
204 void
205 _IO_free_backup_area (fp)
206 _IO_FILE *fp;
208 if (_IO_in_backup (fp))
209 _IO_switch_to_main_get_area (fp); /* Just in case. */
210 free (fp->_IO_save_base);
211 fp->_IO_save_base = NULL;
212 fp->_IO_save_end = NULL;
213 fp->_IO_backup_base = NULL;
215 libc_hidden_def (_IO_free_backup_area)
217 #if 0
219 _IO_switch_to_put_mode (fp)
220 _IO_FILE *fp;
222 fp->_IO_write_base = fp->_IO_read_ptr;
223 fp->_IO_write_ptr = fp->_IO_read_ptr;
224 /* Following is wrong if line- or un-buffered? */
225 fp->_IO_write_end = (fp->_flags & _IO_IN_BACKUP
226 ? fp->_IO_read_end : fp->_IO_buf_end);
228 fp->_IO_read_ptr = fp->_IO_read_end;
229 fp->_IO_read_base = fp->_IO_read_end;
231 fp->_flags |= _IO_CURRENTLY_PUTTING;
232 return 0;
234 #endif
237 __overflow (f, ch)
238 _IO_FILE *f;
239 int ch;
241 /* This is a single-byte stream. */
242 if (f->_mode == 0)
243 _IO_fwide (f, -1);
244 return _IO_OVERFLOW (f, ch);
246 libc_hidden_def (__overflow)
248 static int save_for_backup (_IO_FILE *fp, char *end_p)
249 #ifdef _LIBC
250 internal_function
251 #endif
254 static int
255 #ifdef _LIBC
256 internal_function
257 #endif
258 save_for_backup (fp, end_p)
259 _IO_FILE *fp;
260 char *end_p;
262 /* Append [_IO_read_base..end_p] to backup area. */
263 _IO_ssize_t least_mark = _IO_least_marker (fp, end_p);
264 /* needed_size is how much space we need in the backup area. */
265 _IO_size_t needed_size = (end_p - fp->_IO_read_base) - least_mark;
266 /* FIXME: Dubious arithmetic if pointers are NULL */
267 _IO_size_t current_Bsize = fp->_IO_save_end - fp->_IO_save_base;
268 _IO_size_t avail; /* Extra space available for future expansion. */
269 _IO_ssize_t delta;
270 struct _IO_marker *mark;
271 if (needed_size > current_Bsize)
273 char *new_buffer;
274 avail = 100;
275 new_buffer = (char *) malloc (avail + needed_size);
276 if (new_buffer == NULL)
277 return EOF; /* FIXME */
278 if (least_mark < 0)
280 #ifdef _LIBC
281 __mempcpy (__mempcpy (new_buffer + avail,
282 fp->_IO_save_end + least_mark,
283 -least_mark),
284 fp->_IO_read_base,
285 end_p - fp->_IO_read_base);
286 #else
287 memcpy (new_buffer + avail,
288 fp->_IO_save_end + least_mark,
289 -least_mark);
290 memcpy (new_buffer + avail - least_mark,
291 fp->_IO_read_base,
292 end_p - fp->_IO_read_base);
293 #endif
295 else
296 memcpy (new_buffer + avail,
297 fp->_IO_read_base + least_mark,
298 needed_size);
299 free (fp->_IO_save_base);
300 fp->_IO_save_base = new_buffer;
301 fp->_IO_save_end = new_buffer + avail + needed_size;
303 else
305 avail = current_Bsize - needed_size;
306 if (least_mark < 0)
308 memmove (fp->_IO_save_base + avail,
309 fp->_IO_save_end + least_mark,
310 -least_mark);
311 memcpy (fp->_IO_save_base + avail - least_mark,
312 fp->_IO_read_base,
313 end_p - fp->_IO_read_base);
315 else if (needed_size > 0)
316 memcpy (fp->_IO_save_base + avail,
317 fp->_IO_read_base + least_mark,
318 needed_size);
320 fp->_IO_backup_base = fp->_IO_save_base + avail;
321 /* Adjust all the streammarkers. */
322 delta = end_p - fp->_IO_read_base;
323 for (mark = fp->_markers; mark != NULL; mark = mark->_next)
324 mark->_pos -= delta;
325 return 0;
329 __underflow (fp)
330 _IO_FILE *fp;
332 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
333 if (_IO_vtable_offset (fp) == 0 && _IO_fwide (fp, -1) != -1)
334 return EOF;
335 #endif
337 if (fp->_mode == 0)
338 _IO_fwide (fp, -1);
339 if (_IO_in_put_mode (fp))
340 if (_IO_switch_to_get_mode (fp) == EOF)
341 return EOF;
342 if (fp->_IO_read_ptr < fp->_IO_read_end)
343 return *(unsigned char *) fp->_IO_read_ptr;
344 if (_IO_in_backup (fp))
346 _IO_switch_to_main_get_area (fp);
347 if (fp->_IO_read_ptr < fp->_IO_read_end)
348 return *(unsigned char *) fp->_IO_read_ptr;
350 if (_IO_have_markers (fp))
352 if (save_for_backup (fp, fp->_IO_read_end))
353 return EOF;
355 else if (_IO_have_backup (fp))
356 _IO_free_backup_area (fp);
357 return _IO_UNDERFLOW (fp);
359 libc_hidden_def (__underflow)
362 __uflow (fp)
363 _IO_FILE *fp;
365 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
366 if (_IO_vtable_offset (fp) == 0 && _IO_fwide (fp, -1) != -1)
367 return EOF;
368 #endif
370 if (fp->_mode == 0)
371 _IO_fwide (fp, -1);
372 if (_IO_in_put_mode (fp))
373 if (_IO_switch_to_get_mode (fp) == EOF)
374 return EOF;
375 if (fp->_IO_read_ptr < fp->_IO_read_end)
376 return *(unsigned char *) fp->_IO_read_ptr++;
377 if (_IO_in_backup (fp))
379 _IO_switch_to_main_get_area (fp);
380 if (fp->_IO_read_ptr < fp->_IO_read_end)
381 return *(unsigned char *) fp->_IO_read_ptr++;
383 if (_IO_have_markers (fp))
385 if (save_for_backup (fp, fp->_IO_read_end))
386 return EOF;
388 else if (_IO_have_backup (fp))
389 _IO_free_backup_area (fp);
390 return _IO_UFLOW (fp);
392 libc_hidden_def (__uflow)
394 void
395 _IO_setb (f, b, eb, a)
396 _IO_FILE *f;
397 char *b;
398 char *eb;
399 int a;
401 if (f->_IO_buf_base && !(f->_flags & _IO_USER_BUF))
402 FREE_BUF (f->_IO_buf_base, _IO_blen (f));
403 f->_IO_buf_base = b;
404 f->_IO_buf_end = eb;
405 if (a)
406 f->_flags &= ~_IO_USER_BUF;
407 else
408 f->_flags |= _IO_USER_BUF;
410 libc_hidden_def (_IO_setb)
412 void
413 _IO_doallocbuf (fp)
414 _IO_FILE *fp;
416 if (fp->_IO_buf_base)
417 return;
418 if (!(fp->_flags & _IO_UNBUFFERED) || fp->_mode > 0)
419 if (_IO_DOALLOCATE (fp) != EOF)
420 return;
421 _IO_setb (fp, fp->_shortbuf, fp->_shortbuf+1, 0);
423 libc_hidden_def (_IO_doallocbuf)
426 _IO_default_underflow (fp)
427 _IO_FILE *fp;
429 return EOF;
433 _IO_default_uflow (fp)
434 _IO_FILE *fp;
436 int ch = _IO_UNDERFLOW (fp);
437 if (ch == EOF)
438 return EOF;
439 return *(unsigned char *) fp->_IO_read_ptr++;
441 libc_hidden_def (_IO_default_uflow)
443 _IO_size_t
444 _IO_default_xsputn (f, data, n)
445 _IO_FILE *f;
446 const void *data;
447 _IO_size_t n;
449 const char *s = (char *) data;
450 _IO_size_t more = n;
451 if (more <= 0)
452 return 0;
453 for (;;)
455 /* Space available. */
456 if (f->_IO_write_ptr < f->_IO_write_end)
458 _IO_size_t count = f->_IO_write_end - f->_IO_write_ptr;
459 if (count > more)
460 count = more;
461 if (count > 20)
463 #ifdef _LIBC
464 f->_IO_write_ptr = __mempcpy (f->_IO_write_ptr, s, count);
465 #else
466 memcpy (f->_IO_write_ptr, s, count);
467 f->_IO_write_ptr += count;
468 #endif
469 s += count;
471 else if (count)
473 char *p = f->_IO_write_ptr;
474 _IO_ssize_t i;
475 for (i = count; --i >= 0; )
476 *p++ = *s++;
477 f->_IO_write_ptr = p;
479 more -= count;
481 if (more == 0 || _IO_OVERFLOW (f, (unsigned char) *s++) == EOF)
482 break;
483 more--;
485 return n - more;
487 libc_hidden_def (_IO_default_xsputn)
489 _IO_size_t
490 _IO_sgetn (fp, data, n)
491 _IO_FILE *fp;
492 void *data;
493 _IO_size_t n;
495 /* FIXME handle putback buffer here! */
496 return _IO_XSGETN (fp, data, n);
498 libc_hidden_def (_IO_sgetn)
500 _IO_size_t
501 _IO_default_xsgetn (fp, data, n)
502 _IO_FILE *fp;
503 void *data;
504 _IO_size_t n;
506 _IO_size_t more = n;
507 char *s = (char*) data;
508 for (;;)
510 /* Data available. */
511 if (fp->_IO_read_ptr < fp->_IO_read_end)
513 _IO_size_t count = fp->_IO_read_end - fp->_IO_read_ptr;
514 if (count > more)
515 count = more;
516 if (count > 20)
518 #ifdef _LIBC
519 s = __mempcpy (s, fp->_IO_read_ptr, count);
520 #else
521 memcpy (s, fp->_IO_read_ptr, count);
522 s += count;
523 #endif
524 fp->_IO_read_ptr += count;
526 else if (count)
528 char *p = fp->_IO_read_ptr;
529 int i = (int) count;
530 while (--i >= 0)
531 *s++ = *p++;
532 fp->_IO_read_ptr = p;
534 more -= count;
536 if (more == 0 || __underflow (fp) == EOF)
537 break;
539 return n - more;
541 libc_hidden_def (_IO_default_xsgetn)
543 #if 0
544 /* Seems not to be needed. --drepper */
546 _IO_sync (fp)
547 _IO_FILE *fp;
549 return 0;
551 #endif
553 _IO_FILE *
554 _IO_default_setbuf (fp, p, len)
555 _IO_FILE *fp;
556 char *p;
557 _IO_ssize_t len;
559 if (_IO_SYNC (fp) == EOF)
560 return NULL;
561 if (p == NULL || len == 0)
563 fp->_flags |= _IO_UNBUFFERED;
564 _IO_setb (fp, fp->_shortbuf, fp->_shortbuf+1, 0);
566 else
568 fp->_flags &= ~_IO_UNBUFFERED;
569 _IO_setb (fp, p, p+len, 0);
571 fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end = 0;
572 fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_read_end = 0;
573 return fp;
576 _IO_off64_t
577 _IO_default_seekpos (fp, pos, mode)
578 _IO_FILE *fp;
579 _IO_off64_t pos;
580 int mode;
582 return _IO_SEEKOFF (fp, pos, 0, mode);
586 _IO_default_doallocate (fp)
587 _IO_FILE *fp;
589 char *buf;
591 ALLOC_BUF (buf, _IO_BUFSIZ, EOF);
592 _IO_setb (fp, buf, buf+_IO_BUFSIZ, 1);
593 return 1;
595 libc_hidden_def (_IO_default_doallocate)
597 void
598 _IO_init (fp, flags)
599 _IO_FILE *fp;
600 int flags;
602 _IO_no_init (fp, flags, -1, NULL, NULL);
604 libc_hidden_def (_IO_init)
606 void
607 _IO_old_init (fp, flags)
608 _IO_FILE *fp;
609 int flags;
611 fp->_flags = _IO_MAGIC|flags;
612 fp->_flags2 = 0;
613 fp->_IO_buf_base = NULL;
614 fp->_IO_buf_end = NULL;
615 fp->_IO_read_base = NULL;
616 fp->_IO_read_ptr = NULL;
617 fp->_IO_read_end = NULL;
618 fp->_IO_write_base = NULL;
619 fp->_IO_write_ptr = NULL;
620 fp->_IO_write_end = NULL;
621 fp->_chain = NULL; /* Not necessary. */
623 fp->_IO_save_base = NULL;
624 fp->_IO_backup_base = NULL;
625 fp->_IO_save_end = NULL;
626 fp->_markers = NULL;
627 fp->_cur_column = 0;
628 #if _IO_JUMPS_OFFSET
629 fp->_vtable_offset = 0;
630 #endif
631 #ifdef _IO_MTSAFE_IO
632 if (fp->_lock != NULL)
633 _IO_lock_init (*fp->_lock);
634 #endif
637 void
638 _IO_no_init (fp, flags, orientation, wd, jmp)
639 _IO_FILE *fp;
640 int flags;
641 int orientation;
642 struct _IO_wide_data *wd;
643 const struct _IO_jump_t *jmp;
645 _IO_old_init (fp, flags);
646 fp->_mode = orientation;
647 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
648 if (orientation >= 0)
650 fp->_wide_data = wd;
651 fp->_wide_data->_IO_buf_base = NULL;
652 fp->_wide_data->_IO_buf_end = NULL;
653 fp->_wide_data->_IO_read_base = NULL;
654 fp->_wide_data->_IO_read_ptr = NULL;
655 fp->_wide_data->_IO_read_end = NULL;
656 fp->_wide_data->_IO_write_base = NULL;
657 fp->_wide_data->_IO_write_ptr = NULL;
658 fp->_wide_data->_IO_write_end = NULL;
659 fp->_wide_data->_IO_save_base = NULL;
660 fp->_wide_data->_IO_backup_base = NULL;
661 fp->_wide_data->_IO_save_end = NULL;
663 fp->_wide_data->_wide_vtable = jmp;
665 #endif
666 fp->_freeres_list = NULL;
670 _IO_default_sync (fp)
671 _IO_FILE *fp;
673 return 0;
676 /* The way the C++ classes are mapped into the C functions in the
677 current implementation, this function can get called twice! */
679 void
680 _IO_default_finish (fp, dummy)
681 _IO_FILE *fp;
682 int dummy;
684 struct _IO_marker *mark;
685 if (fp->_IO_buf_base && !(fp->_flags & _IO_USER_BUF))
687 FREE_BUF (fp->_IO_buf_base, _IO_blen (fp));
688 fp->_IO_buf_base = fp->_IO_buf_end = NULL;
691 for (mark = fp->_markers; mark != NULL; mark = mark->_next)
692 mark->_sbuf = NULL;
694 if (fp->_IO_save_base)
696 free (fp->_IO_save_base);
697 fp->_IO_save_base = NULL;
700 _IO_un_link ((struct _IO_FILE_plus *) fp);
702 #ifdef _IO_MTSAFE_IO
703 if (fp->_lock != NULL)
704 _IO_lock_fini (*fp->_lock);
705 #endif
707 libc_hidden_def (_IO_default_finish)
709 _IO_off64_t
710 _IO_default_seekoff (fp, offset, dir, mode)
711 _IO_FILE *fp;
712 _IO_off64_t offset;
713 int dir;
714 int mode;
716 return _IO_pos_BAD;
720 _IO_sputbackc (fp, c)
721 _IO_FILE *fp;
722 int c;
724 int result;
726 if (fp->_IO_read_ptr > fp->_IO_read_base
727 && (unsigned char)fp->_IO_read_ptr[-1] == (unsigned char)c)
729 fp->_IO_read_ptr--;
730 result = (unsigned char) c;
732 else
733 result = _IO_PBACKFAIL (fp, c);
735 if (result != EOF)
736 fp->_flags &= ~_IO_EOF_SEEN;
738 return result;
740 libc_hidden_def (_IO_sputbackc)
743 _IO_sungetc (fp)
744 _IO_FILE *fp;
746 int result;
748 if (fp->_IO_read_ptr > fp->_IO_read_base)
750 fp->_IO_read_ptr--;
751 result = (unsigned char) *fp->_IO_read_ptr;
753 else
754 result = _IO_PBACKFAIL (fp, EOF);
756 if (result != EOF)
757 fp->_flags &= ~_IO_EOF_SEEN;
759 return result;
762 #if 0 /* Work in progress */
763 /* Seems not to be needed. */
764 #if 0
765 void
766 _IO_set_column (fp, c)
767 _IO_FILE *fp;
768 int c;
770 if (c == -1)
771 fp->_column = -1;
772 else
773 fp->_column = c - (fp->_IO_write_ptr - fp->_IO_write_base);
775 #else
777 _IO_set_column (fp, i)
778 _IO_FILE *fp;
779 int i;
781 fp->_cur_column = i + 1;
782 return 0;
784 #endif
785 #endif
788 unsigned
789 _IO_adjust_column (start, line, count)
790 unsigned start;
791 const char *line;
792 int count;
794 const char *ptr = line + count;
795 while (ptr > line)
796 if (*--ptr == '\n')
797 return line + count - ptr - 1;
798 return start + count;
800 libc_hidden_def (_IO_adjust_column)
802 #if 0
803 /* Seems not to be needed. --drepper */
805 _IO_get_column (fp)
806 _IO_FILE *fp;
808 if (fp->_cur_column)
809 return _IO_adjust_column (fp->_cur_column - 1,
810 fp->_IO_write_base,
811 fp->_IO_write_ptr - fp->_IO_write_base);
812 return -1;
814 #endif
818 _IO_flush_all_lockp (int do_lock)
820 int result = 0;
821 struct _IO_FILE *fp;
822 int last_stamp;
824 #ifdef _IO_MTSAFE_IO
825 __libc_cleanup_region_start (do_lock, flush_cleanup, NULL);
826 if (do_lock)
827 _IO_lock_lock (list_all_lock);
828 #endif
830 last_stamp = _IO_list_all_stamp;
831 fp = (_IO_FILE *) _IO_list_all;
832 while (fp != NULL)
834 run_fp = fp;
835 if (do_lock)
836 _IO_flockfile (fp);
838 if (((fp->_mode <= 0 && fp->_IO_write_ptr > fp->_IO_write_base)
839 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
840 || (_IO_vtable_offset (fp) == 0
841 && fp->_mode > 0 && (fp->_wide_data->_IO_write_ptr
842 > fp->_wide_data->_IO_write_base))
843 #endif
845 && _IO_OVERFLOW (fp, EOF) == EOF)
846 result = EOF;
848 if (do_lock)
849 _IO_funlockfile (fp);
850 run_fp = NULL;
852 if (last_stamp != _IO_list_all_stamp)
854 /* Something was added to the list. Start all over again. */
855 fp = (_IO_FILE *) _IO_list_all;
856 last_stamp = _IO_list_all_stamp;
858 else
859 fp = fp->_chain;
862 #ifdef _IO_MTSAFE_IO
863 if (do_lock)
864 _IO_lock_unlock (list_all_lock);
865 __libc_cleanup_region_end (0);
866 #endif
868 return result;
873 _IO_flush_all ()
875 /* We want locking. */
876 return _IO_flush_all_lockp (1);
878 libc_hidden_def (_IO_flush_all)
880 void
881 _IO_flush_all_linebuffered ()
883 struct _IO_FILE *fp;
884 int last_stamp;
886 #ifdef _IO_MTSAFE_IO
887 _IO_cleanup_region_start_noarg (flush_cleanup);
888 _IO_lock_lock (list_all_lock);
889 #endif
891 last_stamp = _IO_list_all_stamp;
892 fp = (_IO_FILE *) _IO_list_all;
893 while (fp != NULL)
895 run_fp = fp;
896 _IO_flockfile (fp);
898 if ((fp->_flags & _IO_NO_WRITES) == 0 && fp->_flags & _IO_LINE_BUF)
899 _IO_OVERFLOW (fp, EOF);
901 _IO_funlockfile (fp);
902 run_fp = NULL;
904 if (last_stamp != _IO_list_all_stamp)
906 /* Something was added to the list. Start all over again. */
907 fp = (_IO_FILE *) _IO_list_all;
908 last_stamp = _IO_list_all_stamp;
910 else
911 fp = fp->_chain;
914 #ifdef _IO_MTSAFE_IO
915 _IO_lock_unlock (list_all_lock);
916 _IO_cleanup_region_end (0);
917 #endif
919 libc_hidden_def (_IO_flush_all_linebuffered)
920 #ifdef _LIBC
921 weak_alias (_IO_flush_all_linebuffered, _flushlbf)
922 #endif
925 /* The following is a bit tricky. In general, we want to unbuffer the
926 streams so that all output which follows is seen. If we are not
927 looking for memory leaks it does not make much sense to free the
928 actual buffer because this will happen anyway once the program
929 terminated. If we do want to look for memory leaks we have to free
930 the buffers. Whether something is freed is determined by the
931 function sin the libc_freeres section. Those are called as part of
932 the atexit routine, just like _IO_cleanup. The problem is we do
933 not know whether the freeres code is called first or _IO_cleanup.
934 if the former is the case, we set the DEALLOC_BUFFER variable to
935 true and _IO_unbuffer_write will take care of the rest. If
936 _IO_unbuffer_write is called first we add the streams to a list
937 which the freeres function later can walk through. */
938 static void _IO_unbuffer_write (void);
940 static bool dealloc_buffers;
941 static _IO_FILE *freeres_list;
943 static void
944 _IO_unbuffer_write (void)
946 struct _IO_FILE *fp;
947 for (fp = (_IO_FILE *) _IO_list_all; fp; fp = fp->_chain)
949 if (! (fp->_flags & _IO_UNBUFFERED)
950 && (! (fp->_flags & _IO_NO_WRITES)
951 || (fp->_flags & _IO_IS_APPENDING))
952 /* Iff stream is un-orientated, it wasn't used. */
953 && fp->_mode != 0)
955 #ifdef _IO_MTSAFE_IO
956 int cnt;
957 #define MAXTRIES 2
958 for (cnt = 0; cnt < MAXTRIES; ++cnt)
959 if (fp->_lock == NULL || _IO_lock_trylock (*fp->_lock) == 0)
960 break;
961 else
962 /* Give the other thread time to finish up its use of the
963 stream. */
964 __sched_yield ();
965 #endif
967 if (! dealloc_buffers && !(fp->_flags & _IO_USER_BUF))
969 fp->_flags |= _IO_USER_BUF;
971 fp->_freeres_list = freeres_list;
972 freeres_list = fp;
973 fp->_freeres_buf = fp->_IO_buf_base;
974 fp->_freeres_size = _IO_blen (fp);
977 _IO_SETBUF (fp, NULL, 0);
979 #ifdef _IO_MTSAFE_IO
980 if (cnt < MAXTRIES && fp->_lock != NULL)
981 _IO_lock_unlock (*fp->_lock);
982 #endif
985 /* Make sure that never again the wide char functions can be
986 used. */
987 fp->_mode = -1;
992 libc_freeres_fn (buffer_free)
994 dealloc_buffers = true;
996 while (freeres_list != NULL)
998 FREE_BUF (freeres_list->_freeres_buf, freeres_list->_freeres_size);
1000 freeres_list = freeres_list->_freeres_list;
1006 _IO_cleanup ()
1008 /* We do *not* want locking. Some threads might use streams but
1009 that is their problem, we flush them underneath them. */
1010 int result = _IO_flush_all_lockp (0);
1012 /* We currently don't have a reliable mechanism for making sure that
1013 C++ static destructors are executed in the correct order.
1014 So it is possible that other static destructors might want to
1015 write to cout - and they're supposed to be able to do so.
1017 The following will make the standard streambufs be unbuffered,
1018 which forces any output from late destructors to be written out. */
1019 _IO_unbuffer_write ();
1021 return result;
1025 void
1026 _IO_init_marker (marker, fp)
1027 struct _IO_marker *marker;
1028 _IO_FILE *fp;
1030 marker->_sbuf = fp;
1031 if (_IO_in_put_mode (fp))
1032 _IO_switch_to_get_mode (fp);
1033 if (_IO_in_backup (fp))
1034 marker->_pos = fp->_IO_read_ptr - fp->_IO_read_end;
1035 else
1036 marker->_pos = fp->_IO_read_ptr - fp->_IO_read_base;
1038 /* Should perhaps sort the chain? */
1039 marker->_next = fp->_markers;
1040 fp->_markers = marker;
1043 void
1044 _IO_remove_marker (marker)
1045 struct _IO_marker *marker;
1047 /* Unlink from sb's chain. */
1048 struct _IO_marker **ptr = &marker->_sbuf->_markers;
1049 for (; ; ptr = &(*ptr)->_next)
1051 if (*ptr == NULL)
1052 break;
1053 else if (*ptr == marker)
1055 *ptr = marker->_next;
1056 return;
1059 #if 0
1060 if _sbuf has a backup area that is no longer needed, should we delete
1061 it now, or wait until the next underflow?
1062 #endif
1065 #define BAD_DELTA EOF
1068 _IO_marker_difference (mark1, mark2)
1069 struct _IO_marker *mark1;
1070 struct _IO_marker *mark2;
1072 return mark1->_pos - mark2->_pos;
1075 /* Return difference between MARK and current position of MARK's stream. */
1077 _IO_marker_delta (mark)
1078 struct _IO_marker *mark;
1080 int cur_pos;
1081 if (mark->_sbuf == NULL)
1082 return BAD_DELTA;
1083 if (_IO_in_backup (mark->_sbuf))
1084 cur_pos = mark->_sbuf->_IO_read_ptr - mark->_sbuf->_IO_read_end;
1085 else
1086 cur_pos = mark->_sbuf->_IO_read_ptr - mark->_sbuf->_IO_read_base;
1087 return mark->_pos - cur_pos;
1091 _IO_seekmark (fp, mark, delta)
1092 _IO_FILE *fp;
1093 struct _IO_marker *mark;
1094 int delta;
1096 if (mark->_sbuf != fp)
1097 return EOF;
1098 if (mark->_pos >= 0)
1100 if (_IO_in_backup (fp))
1101 _IO_switch_to_main_get_area (fp);
1102 fp->_IO_read_ptr = fp->_IO_read_base + mark->_pos;
1104 else
1106 if (!_IO_in_backup (fp))
1107 _IO_switch_to_backup_area (fp);
1108 fp->_IO_read_ptr = fp->_IO_read_end + mark->_pos;
1110 return 0;
1113 void
1114 _IO_unsave_markers (fp)
1115 _IO_FILE *fp;
1117 struct _IO_marker *mark = fp->_markers;
1118 if (mark)
1120 #ifdef TODO
1121 streampos offset = seekoff (0, ios::cur, ios::in);
1122 if (offset != EOF)
1124 offset += eGptr () - Gbase ();
1125 for ( ; mark != NULL; mark = mark->_next)
1126 mark->set_streampos (mark->_pos + offset);
1128 else
1130 for ( ; mark != NULL; mark = mark->_next)
1131 mark->set_streampos (EOF);
1133 #endif
1134 fp->_markers = 0;
1137 if (_IO_have_backup (fp))
1138 _IO_free_backup_area (fp);
1140 libc_hidden_def (_IO_unsave_markers)
1142 #if 0
1143 /* Seems not to be needed. --drepper */
1145 _IO_nobackup_pbackfail (fp, c)
1146 _IO_FILE *fp;
1147 int c;
1149 if (fp->_IO_read_ptr > fp->_IO_read_base)
1150 fp->_IO_read_ptr--;
1151 if (c != EOF && *fp->_IO_read_ptr != c)
1152 *fp->_IO_read_ptr = c;
1153 return (unsigned char) c;
1155 #endif
1158 _IO_default_pbackfail (fp, c)
1159 _IO_FILE *fp;
1160 int c;
1162 if (fp->_IO_read_ptr > fp->_IO_read_base && !_IO_in_backup (fp)
1163 && (unsigned char) fp->_IO_read_ptr[-1] == c)
1164 --fp->_IO_read_ptr;
1165 else
1167 /* Need to handle a filebuf in write mode (switch to read mode). FIXME!*/
1168 if (!_IO_in_backup (fp))
1170 /* We need to keep the invariant that the main get area
1171 logically follows the backup area. */
1172 if (fp->_IO_read_ptr > fp->_IO_read_base && _IO_have_backup (fp))
1174 if (save_for_backup (fp, fp->_IO_read_ptr))
1175 return EOF;
1177 else if (!_IO_have_backup (fp))
1179 /* No backup buffer: allocate one. */
1180 /* Use nshort buffer, if unused? (probably not) FIXME */
1181 int backup_size = 128;
1182 char *bbuf = (char *) malloc (backup_size);
1183 if (bbuf == NULL)
1184 return EOF;
1185 fp->_IO_save_base = bbuf;
1186 fp->_IO_save_end = fp->_IO_save_base + backup_size;
1187 fp->_IO_backup_base = fp->_IO_save_end;
1189 fp->_IO_read_base = fp->_IO_read_ptr;
1190 _IO_switch_to_backup_area (fp);
1192 else if (fp->_IO_read_ptr <= fp->_IO_read_base)
1194 /* Increase size of existing backup buffer. */
1195 _IO_size_t new_size;
1196 _IO_size_t old_size = fp->_IO_read_end - fp->_IO_read_base;
1197 char *new_buf;
1198 new_size = 2 * old_size;
1199 new_buf = (char *) malloc (new_size);
1200 if (new_buf == NULL)
1201 return EOF;
1202 memcpy (new_buf + (new_size - old_size), fp->_IO_read_base,
1203 old_size);
1204 free (fp->_IO_read_base);
1205 _IO_setg (fp, new_buf, new_buf + (new_size - old_size),
1206 new_buf + new_size);
1207 fp->_IO_backup_base = fp->_IO_read_ptr;
1210 *--fp->_IO_read_ptr = c;
1212 return (unsigned char) c;
1214 libc_hidden_def (_IO_default_pbackfail)
1216 _IO_off64_t
1217 _IO_default_seek (fp, offset, dir)
1218 _IO_FILE *fp;
1219 _IO_off64_t offset;
1220 int dir;
1222 return _IO_pos_BAD;
1226 _IO_default_stat (fp, st)
1227 _IO_FILE *fp;
1228 void* st;
1230 return EOF;
1233 _IO_ssize_t
1234 _IO_default_read (fp, data, n)
1235 _IO_FILE* fp;
1236 void *data;
1237 _IO_ssize_t n;
1239 return -1;
1242 _IO_ssize_t
1243 _IO_default_write (fp, data, n)
1244 _IO_FILE *fp;
1245 const void *data;
1246 _IO_ssize_t n;
1248 return 0;
1252 _IO_default_showmanyc (fp)
1253 _IO_FILE *fp;
1255 return -1;
1258 void
1259 _IO_default_imbue (fp, locale)
1260 _IO_FILE *fp;
1261 void *locale;
1265 _IO_ITER
1266 _IO_iter_begin()
1268 return (_IO_ITER) _IO_list_all;
1270 libc_hidden_def (_IO_iter_begin)
1272 _IO_ITER
1273 _IO_iter_end()
1275 return NULL;
1277 libc_hidden_def (_IO_iter_end)
1279 _IO_ITER
1280 _IO_iter_next(iter)
1281 _IO_ITER iter;
1283 return iter->_chain;
1285 libc_hidden_def (_IO_iter_next)
1287 _IO_FILE *
1288 _IO_iter_file(iter)
1289 _IO_ITER iter;
1291 return iter;
1293 libc_hidden_def (_IO_iter_file)
1295 void
1296 _IO_list_lock()
1298 #ifdef _IO_MTSAFE_IO
1299 _IO_lock_lock (list_all_lock);
1300 #endif
1302 libc_hidden_def (_IO_list_lock)
1304 void
1305 _IO_list_unlock()
1307 #ifdef _IO_MTSAFE_IO
1308 _IO_lock_unlock (list_all_lock);
1309 #endif
1311 libc_hidden_def (_IO_list_unlock)
1313 void
1314 _IO_list_resetlock()
1316 #ifdef _IO_MTSAFE_IO
1317 _IO_lock_init (list_all_lock);
1318 #endif
1320 libc_hidden_def (_IO_list_resetlock)
1323 #ifdef TODO
1324 #if defined(linux)
1325 #define IO_CLEANUP ;
1326 #endif
1328 #ifdef IO_CLEANUP
1329 IO_CLEANUP
1330 #else
1331 struct __io_defs {
1332 __io_defs() { }
1333 ~__io_defs() { _IO_cleanup (); }
1335 __io_defs io_defs__;
1336 #endif
1338 #endif /* TODO */
1340 #ifdef text_set_element
1341 text_set_element(__libc_atexit, _IO_cleanup);
1342 #endif