.
[glibc/history.git] / libio / genops.c
blob481fbc52b0c0f7f0d4cabaa6676c48f92b84a093
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
18 02111-1307 USA.
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. */
31 #include "libioP.h"
32 #ifdef __STDC__
33 #include <stdlib.h>
34 #endif
35 #include <string.h>
36 #include <stdbool.h>
37 #ifdef _LIBC
38 #include <sched.h>
39 #endif
41 #ifdef _IO_MTSAFE_IO
42 static _IO_lock_t list_all_lock = _IO_lock_initializer;
43 #endif
45 /* Used to signal modifications to the list of FILE decriptors. */
46 static int _IO_list_all_stamp;
49 static _IO_FILE *run_fp;
51 static void
52 flush_cleanup (void *not_used)
54 if (run_fp != NULL)
55 _IO_funlockfile (run_fp);
56 #ifdef _IO_MTSAFE_IO
57 _IO_lock_unlock (list_all_lock);
58 #endif
61 void
62 _IO_un_link (fp)
63 struct _IO_FILE_plus *fp;
65 if (fp->file._flags & _IO_LINKED)
67 struct _IO_FILE **f;
68 #ifdef _IO_MTSAFE_IO
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);
73 #endif
74 if (INTUSE(_IO_list_all) == NULL)
76 else if (fp == INTUSE(_IO_list_all))
78 INTUSE(_IO_list_all)
79 = (struct _IO_FILE_plus *) INTUSE(_IO_list_all)->file._chain;
80 ++_IO_list_all_stamp;
82 else
83 for (f = &INTUSE(_IO_list_all)->file._chain; *f; f = &(*f)->_chain)
84 if (*f == (_IO_FILE *) fp)
86 *f = fp->file._chain;
87 ++_IO_list_all_stamp;
88 break;
90 fp->file._flags &= ~_IO_LINKED;
91 #ifdef _IO_MTSAFE_IO
92 _IO_funlockfile ((_IO_FILE *) fp);
93 run_fp = NULL;
94 _IO_lock_unlock (list_all_lock);
95 _IO_cleanup_region_end (0);
96 #endif
99 INTDEF(_IO_un_link)
101 void
102 _IO_link_in (fp)
103 struct _IO_FILE_plus *fp;
105 if ((fp->file._flags & _IO_LINKED) == 0)
107 fp->file._flags |= _IO_LINKED;
108 #ifdef _IO_MTSAFE_IO
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);
113 #endif
114 fp->file._chain = (_IO_FILE *) INTUSE(_IO_list_all);
115 INTUSE(_IO_list_all) = fp;
116 ++_IO_list_all_stamp;
117 #ifdef _IO_MTSAFE_IO
118 _IO_funlockfile ((_IO_FILE *) fp);
119 run_fp = NULL;
120 _IO_lock_unlock (list_all_lock);
121 _IO_cleanup_region_end (0);
122 #endif
125 INTDEF(_IO_link_in)
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);
131 _IO_ssize_t
132 _IO_least_marker (fp, end_p)
133 _IO_FILE *fp;
134 char *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;
141 return least_so_far;
144 /* Switch current get area from backup buffer to (start of) main get area. */
146 void
147 _IO_switch_to_main_get_area (fp)
148 _IO_FILE *fp;
150 char *tmp;
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. */
166 void
167 _IO_switch_to_backup_area (fp)
168 _IO_FILE *fp;
170 char *tmp;
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)
186 _IO_FILE *fp;
188 if (fp->_IO_write_ptr > fp->_IO_write_base)
189 if (_IO_OVERFLOW (fp, EOF) == EOF)
190 return EOF;
191 if (_IO_in_backup (fp))
192 fp->_IO_read_base = fp->_IO_backup_base;
193 else
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;
204 return 0;
206 INTDEF(_IO_switch_to_get_mode)
208 void
209 _IO_free_backup_area (fp)
210 _IO_FILE *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)
221 #if 0
223 _IO_switch_to_put_mode (fp)
224 _IO_FILE *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;
236 return 0;
238 #endif
241 __overflow (f, ch)
242 _IO_FILE *f;
243 int ch;
245 /* This is a single-byte stream. */
246 if (f->_mode == 0)
247 _IO_fwide (f, -1);
248 return _IO_OVERFLOW (f, ch);
250 libc_hidden_def (__overflow)
252 static int save_for_backup (_IO_FILE *fp, char *end_p)
253 #ifdef _LIBC
254 internal_function
255 #endif
258 static int
259 #ifdef _LIBC
260 internal_function
261 #endif
262 save_for_backup (fp, end_p)
263 _IO_FILE *fp;
264 char *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. */
273 _IO_ssize_t delta;
274 struct _IO_marker *mark;
275 if (needed_size > current_Bsize)
277 char *new_buffer;
278 avail = 100;
279 new_buffer = (char *) malloc (avail + needed_size);
280 if (new_buffer == NULL)
281 return EOF; /* FIXME */
282 if (least_mark < 0)
284 #ifdef _LIBC
285 __mempcpy (__mempcpy (new_buffer + avail,
286 fp->_IO_save_end + least_mark,
287 -least_mark),
288 fp->_IO_read_base,
289 end_p - fp->_IO_read_base);
290 #else
291 memcpy (new_buffer + avail,
292 fp->_IO_save_end + least_mark,
293 -least_mark);
294 memcpy (new_buffer + avail - least_mark,
295 fp->_IO_read_base,
296 end_p - fp->_IO_read_base);
297 #endif
299 else
300 memcpy (new_buffer + avail,
301 fp->_IO_read_base + least_mark,
302 needed_size);
303 free (fp->_IO_save_base);
304 fp->_IO_save_base = new_buffer;
305 fp->_IO_save_end = new_buffer + avail + needed_size;
307 else
309 avail = current_Bsize - needed_size;
310 if (least_mark < 0)
312 memmove (fp->_IO_save_base + avail,
313 fp->_IO_save_end + least_mark,
314 -least_mark);
315 memcpy (fp->_IO_save_base + avail - least_mark,
316 fp->_IO_read_base,
317 end_p - fp->_IO_read_base);
319 else if (needed_size > 0)
320 memcpy (fp->_IO_save_base + avail,
321 fp->_IO_read_base + least_mark,
322 needed_size);
324 fp->_IO_backup_base = fp->_IO_save_base + avail;
325 /* Adjust all the streammarkers. */
326 delta = end_p - fp->_IO_read_base;
327 for (mark = fp->_markers; mark != NULL; mark = mark->_next)
328 mark->_pos -= delta;
329 return 0;
333 __underflow (fp)
334 _IO_FILE *fp;
336 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
337 if (_IO_vtable_offset (fp) == 0 && _IO_fwide (fp, -1) != -1)
338 return EOF;
339 #endif
341 if (fp->_mode == 0)
342 _IO_fwide (fp, -1);
343 if (_IO_in_put_mode (fp))
344 if (INTUSE(_IO_switch_to_get_mode) (fp) == EOF)
345 return EOF;
346 if (fp->_IO_read_ptr < fp->_IO_read_end)
347 return *(unsigned char *) fp->_IO_read_ptr;
348 if (_IO_in_backup (fp))
350 _IO_switch_to_main_get_area (fp);
351 if (fp->_IO_read_ptr < fp->_IO_read_end)
352 return *(unsigned char *) fp->_IO_read_ptr;
354 if (_IO_have_markers (fp))
356 if (save_for_backup (fp, fp->_IO_read_end))
357 return EOF;
359 else if (_IO_have_backup (fp))
360 INTUSE(_IO_free_backup_area) (fp);
361 return _IO_UNDERFLOW (fp);
363 libc_hidden_def (__underflow)
366 __uflow (fp)
367 _IO_FILE *fp;
369 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
370 if (_IO_vtable_offset (fp) == 0 && _IO_fwide (fp, -1) != -1)
371 return EOF;
372 #endif
374 if (fp->_mode == 0)
375 _IO_fwide (fp, -1);
376 if (_IO_in_put_mode (fp))
377 if (INTUSE(_IO_switch_to_get_mode) (fp) == EOF)
378 return EOF;
379 if (fp->_IO_read_ptr < fp->_IO_read_end)
380 return *(unsigned char *) fp->_IO_read_ptr++;
381 if (_IO_in_backup (fp))
383 _IO_switch_to_main_get_area (fp);
384 if (fp->_IO_read_ptr < fp->_IO_read_end)
385 return *(unsigned char *) fp->_IO_read_ptr++;
387 if (_IO_have_markers (fp))
389 if (save_for_backup (fp, fp->_IO_read_end))
390 return EOF;
392 else if (_IO_have_backup (fp))
393 INTUSE(_IO_free_backup_area) (fp);
394 return _IO_UFLOW (fp);
396 libc_hidden_def (__uflow)
398 void
399 _IO_setb (f, b, eb, a)
400 _IO_FILE *f;
401 char *b;
402 char *eb;
403 int a;
405 if (f->_IO_buf_base && !(f->_flags & _IO_USER_BUF))
406 FREE_BUF (f->_IO_buf_base, _IO_blen (f));
407 f->_IO_buf_base = b;
408 f->_IO_buf_end = eb;
409 if (a)
410 f->_flags &= ~_IO_USER_BUF;
411 else
412 f->_flags |= _IO_USER_BUF;
414 INTDEF(_IO_setb)
416 void
417 _IO_doallocbuf (fp)
418 _IO_FILE *fp;
420 if (fp->_IO_buf_base)
421 return;
422 if (!(fp->_flags & _IO_UNBUFFERED) || fp->_mode > 0)
423 if (_IO_DOALLOCATE (fp) != EOF)
424 return;
425 INTUSE(_IO_setb) (fp, fp->_shortbuf, fp->_shortbuf+1, 0);
427 INTDEF(_IO_doallocbuf)
430 _IO_default_underflow (fp)
431 _IO_FILE *fp;
433 return EOF;
437 _IO_default_uflow (fp)
438 _IO_FILE *fp;
440 int ch = _IO_UNDERFLOW (fp);
441 if (ch == EOF)
442 return EOF;
443 return *(unsigned char *) fp->_IO_read_ptr++;
445 INTDEF(_IO_default_uflow)
447 _IO_size_t
448 _IO_default_xsputn (f, data, n)
449 _IO_FILE *f;
450 const void *data;
451 _IO_size_t n;
453 const char *s = (char *) data;
454 _IO_size_t more = n;
455 if (more <= 0)
456 return 0;
457 for (;;)
459 /* Space available. */
460 if (f->_IO_write_ptr < f->_IO_write_end)
462 _IO_size_t count = f->_IO_write_end - f->_IO_write_ptr;
463 if (count > more)
464 count = more;
465 if (count > 20)
467 #ifdef _LIBC
468 f->_IO_write_ptr = __mempcpy (f->_IO_write_ptr, s, count);
469 #else
470 memcpy (f->_IO_write_ptr, s, count);
471 f->_IO_write_ptr += count;
472 #endif
473 s += count;
475 else if (count)
477 char *p = f->_IO_write_ptr;
478 _IO_ssize_t i;
479 for (i = count; --i >= 0; )
480 *p++ = *s++;
481 f->_IO_write_ptr = p;
483 more -= count;
485 if (more == 0 || _IO_OVERFLOW (f, (unsigned char) *s++) == EOF)
486 break;
487 more--;
489 return n - more;
491 INTDEF(_IO_default_xsputn)
493 _IO_size_t
494 _IO_sgetn (fp, data, n)
495 _IO_FILE *fp;
496 void *data;
497 _IO_size_t n;
499 /* FIXME handle putback buffer here! */
500 return _IO_XSGETN (fp, data, n);
502 INTDEF(_IO_sgetn)
504 _IO_size_t
505 _IO_default_xsgetn (fp, data, n)
506 _IO_FILE *fp;
507 void *data;
508 _IO_size_t n;
510 _IO_size_t more = n;
511 char *s = (char*) data;
512 for (;;)
514 /* Data available. */
515 if (fp->_IO_read_ptr < fp->_IO_read_end)
517 _IO_size_t count = fp->_IO_read_end - fp->_IO_read_ptr;
518 if (count > more)
519 count = more;
520 if (count > 20)
522 #ifdef _LIBC
523 s = __mempcpy (s, fp->_IO_read_ptr, count);
524 #else
525 memcpy (s, fp->_IO_read_ptr, count);
526 s += count;
527 #endif
528 fp->_IO_read_ptr += count;
530 else if (count)
532 char *p = fp->_IO_read_ptr;
533 int i = (int) count;
534 while (--i >= 0)
535 *s++ = *p++;
536 fp->_IO_read_ptr = p;
538 more -= count;
540 if (more == 0 || __underflow (fp) == EOF)
541 break;
543 return n - more;
545 INTDEF(_IO_default_xsgetn)
547 #if 0
548 /* Seems not to be needed. --drepper */
550 _IO_sync (fp)
551 _IO_FILE *fp;
553 return 0;
555 #endif
557 _IO_FILE *
558 _IO_default_setbuf (fp, p, len)
559 _IO_FILE *fp;
560 char *p;
561 _IO_ssize_t len;
563 if (_IO_SYNC (fp) == EOF)
564 return NULL;
565 if (p == NULL || len == 0)
567 fp->_flags |= _IO_UNBUFFERED;
568 INTUSE(_IO_setb) (fp, fp->_shortbuf, fp->_shortbuf+1, 0);
570 else
572 fp->_flags &= ~_IO_UNBUFFERED;
573 INTUSE(_IO_setb) (fp, p, p+len, 0);
575 fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end = 0;
576 fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_read_end = 0;
577 return fp;
580 _IO_off64_t
581 _IO_default_seekpos (fp, pos, mode)
582 _IO_FILE *fp;
583 _IO_off64_t pos;
584 int mode;
586 return _IO_SEEKOFF (fp, pos, 0, mode);
590 _IO_default_doallocate (fp)
591 _IO_FILE *fp;
593 char *buf;
595 ALLOC_BUF (buf, _IO_BUFSIZ, EOF);
596 INTUSE(_IO_setb) (fp, buf, buf+_IO_BUFSIZ, 1);
597 return 1;
599 INTDEF(_IO_default_doallocate)
601 void
602 _IO_init (fp, flags)
603 _IO_FILE *fp;
604 int flags;
606 _IO_no_init (fp, flags, -1, NULL, NULL);
608 INTDEF(_IO_init)
610 void
611 _IO_old_init (fp, flags)
612 _IO_FILE *fp;
613 int flags;
615 fp->_flags = _IO_MAGIC|flags;
616 fp->_flags2 = 0;
617 fp->_IO_buf_base = NULL;
618 fp->_IO_buf_end = NULL;
619 fp->_IO_read_base = NULL;
620 fp->_IO_read_ptr = NULL;
621 fp->_IO_read_end = NULL;
622 fp->_IO_write_base = NULL;
623 fp->_IO_write_ptr = NULL;
624 fp->_IO_write_end = NULL;
625 fp->_chain = NULL; /* Not necessary. */
627 fp->_IO_save_base = NULL;
628 fp->_IO_backup_base = NULL;
629 fp->_IO_save_end = NULL;
630 fp->_markers = NULL;
631 fp->_cur_column = 0;
632 #if _IO_JUMPS_OFFSET
633 fp->_vtable_offset = 0;
634 #endif
635 #ifdef _IO_MTSAFE_IO
636 if (fp->_lock != NULL)
637 _IO_lock_init (*fp->_lock);
638 #endif
641 void
642 _IO_no_init (fp, flags, orientation, wd, jmp)
643 _IO_FILE *fp;
644 int flags;
645 int orientation;
646 struct _IO_wide_data *wd;
647 const struct _IO_jump_t *jmp;
649 _IO_old_init (fp, flags);
650 fp->_mode = orientation;
651 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
652 if (orientation >= 0)
654 fp->_wide_data = wd;
655 fp->_wide_data->_IO_buf_base = NULL;
656 fp->_wide_data->_IO_buf_end = NULL;
657 fp->_wide_data->_IO_read_base = NULL;
658 fp->_wide_data->_IO_read_ptr = NULL;
659 fp->_wide_data->_IO_read_end = NULL;
660 fp->_wide_data->_IO_write_base = NULL;
661 fp->_wide_data->_IO_write_ptr = NULL;
662 fp->_wide_data->_IO_write_end = NULL;
663 fp->_wide_data->_IO_save_base = NULL;
664 fp->_wide_data->_IO_backup_base = NULL;
665 fp->_wide_data->_IO_save_end = NULL;
667 fp->_wide_data->_wide_vtable = jmp;
669 #endif
670 fp->_freeres_list = NULL;
674 _IO_default_sync (fp)
675 _IO_FILE *fp;
677 return 0;
680 /* The way the C++ classes are mapped into the C functions in the
681 current implementation, this function can get called twice! */
683 void
684 _IO_default_finish (fp, dummy)
685 _IO_FILE *fp;
686 int dummy;
688 struct _IO_marker *mark;
689 if (fp->_IO_buf_base && !(fp->_flags & _IO_USER_BUF))
691 FREE_BUF (fp->_IO_buf_base, _IO_blen (fp));
692 fp->_IO_buf_base = fp->_IO_buf_end = NULL;
695 for (mark = fp->_markers; mark != NULL; mark = mark->_next)
696 mark->_sbuf = NULL;
698 if (fp->_IO_save_base)
700 free (fp->_IO_save_base);
701 fp->_IO_save_base = NULL;
704 INTUSE(_IO_un_link) ((struct _IO_FILE_plus *) fp);
706 #ifdef _IO_MTSAFE_IO
707 if (fp->_lock != NULL)
708 _IO_lock_fini (*fp->_lock);
709 #endif
711 INTDEF(_IO_default_finish)
713 _IO_off64_t
714 _IO_default_seekoff (fp, offset, dir, mode)
715 _IO_FILE *fp;
716 _IO_off64_t offset;
717 int dir;
718 int mode;
720 return _IO_pos_BAD;
724 _IO_sputbackc (fp, c)
725 _IO_FILE *fp;
726 int c;
728 int result;
730 if (fp->_IO_read_ptr > fp->_IO_read_base
731 && (unsigned char)fp->_IO_read_ptr[-1] == (unsigned char)c)
733 fp->_IO_read_ptr--;
734 result = (unsigned char) c;
736 else
737 result = _IO_PBACKFAIL (fp, c);
739 if (result != EOF)
740 fp->_flags &= ~_IO_EOF_SEEN;
742 return result;
744 INTDEF(_IO_sputbackc)
747 _IO_sungetc (fp)
748 _IO_FILE *fp;
750 int result;
752 if (fp->_IO_read_ptr > fp->_IO_read_base)
754 fp->_IO_read_ptr--;
755 result = (unsigned char) *fp->_IO_read_ptr;
757 else
758 result = _IO_PBACKFAIL (fp, EOF);
760 if (result != EOF)
761 fp->_flags &= ~_IO_EOF_SEEN;
763 return result;
766 #if 0 /* Work in progress */
767 /* Seems not to be needed. */
768 #if 0
769 void
770 _IO_set_column (fp, c)
771 _IO_FILE *fp;
772 int c;
774 if (c == -1)
775 fp->_column = -1;
776 else
777 fp->_column = c - (fp->_IO_write_ptr - fp->_IO_write_base);
779 #else
781 _IO_set_column (fp, i)
782 _IO_FILE *fp;
783 int i;
785 fp->_cur_column = i + 1;
786 return 0;
788 #endif
789 #endif
792 unsigned
793 _IO_adjust_column (start, line, count)
794 unsigned start;
795 const char *line;
796 int count;
798 const char *ptr = line + count;
799 while (ptr > line)
800 if (*--ptr == '\n')
801 return line + count - ptr - 1;
802 return start + count;
804 INTDEF(_IO_adjust_column)
806 #if 0
807 /* Seems not to be needed. --drepper */
809 _IO_get_column (fp)
810 _IO_FILE *fp;
812 if (fp->_cur_column)
813 return _IO_adjust_column (fp->_cur_column - 1,
814 fp->_IO_write_base,
815 fp->_IO_write_ptr - fp->_IO_write_base);
816 return -1;
818 #endif
822 _IO_flush_all_lockp (int do_lock)
824 int result = 0;
825 struct _IO_FILE *fp;
826 int last_stamp;
828 #ifdef _IO_MTSAFE_IO
829 _IO_cleanup_region_start_noarg (flush_cleanup);
830 if (do_lock)
831 _IO_lock_lock (list_all_lock);
832 #endif
834 last_stamp = _IO_list_all_stamp;
835 fp = (_IO_FILE *) INTUSE(_IO_list_all);
836 while (fp != NULL)
838 run_fp = fp;
839 if (do_lock)
840 _IO_flockfile (fp);
842 if (((fp->_mode <= 0 && fp->_IO_write_ptr > fp->_IO_write_base)
843 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
844 || (_IO_vtable_offset (fp) == 0
845 && fp->_mode > 0 && (fp->_wide_data->_IO_write_ptr
846 > fp->_wide_data->_IO_write_base))
847 #endif
849 && _IO_OVERFLOW (fp, EOF) == EOF)
850 result = EOF;
852 if (do_lock)
853 _IO_funlockfile (fp);
854 run_fp = NULL;
856 if (last_stamp != _IO_list_all_stamp)
858 /* Something was added to the list. Start all over again. */
859 fp = (_IO_FILE *) INTUSE(_IO_list_all);
860 last_stamp = _IO_list_all_stamp;
862 else
863 fp = fp->_chain;
866 #ifdef _IO_MTSAFE_IO
867 if (do_lock)
868 _IO_lock_unlock (list_all_lock);
869 _IO_cleanup_region_end (0);
870 #endif
872 return result;
877 _IO_flush_all ()
879 /* We want locking. */
880 return _IO_flush_all_lockp (1);
882 INTDEF(_IO_flush_all)
884 void
885 _IO_flush_all_linebuffered ()
887 struct _IO_FILE *fp;
888 int last_stamp;
890 #ifdef _IO_MTSAFE_IO
891 _IO_cleanup_region_start_noarg (flush_cleanup);
892 _IO_lock_lock (list_all_lock);
893 #endif
895 last_stamp = _IO_list_all_stamp;
896 fp = (_IO_FILE *) INTUSE(_IO_list_all);
897 while (fp != NULL)
899 run_fp = fp;
900 _IO_flockfile (fp);
902 if ((fp->_flags & _IO_NO_WRITES) == 0 && fp->_flags & _IO_LINE_BUF)
903 _IO_OVERFLOW (fp, EOF);
905 _IO_funlockfile (fp);
906 run_fp = NULL;
908 if (last_stamp != _IO_list_all_stamp)
910 /* Something was added to the list. Start all over again. */
911 fp = (_IO_FILE *) INTUSE(_IO_list_all);
912 last_stamp = _IO_list_all_stamp;
914 else
915 fp = fp->_chain;
918 #ifdef _IO_MTSAFE_IO
919 _IO_lock_unlock (list_all_lock);
920 _IO_cleanup_region_end (0);
921 #endif
923 INTDEF(_IO_flush_all_linebuffered)
924 #ifdef _LIBC
925 weak_alias (_IO_flush_all_linebuffered, _flushlbf)
926 #endif
929 /* The following is a bit tricky. In general, we want to unbuffer the
930 streams so that all output which follows is seen. If we are not
931 looking for memory leaks it does not make much sense to free the
932 actual buffer because this will happen anyway once the program
933 terminated. If we do want to look for memory leaks we have to free
934 the buffers. Whether something is freed is determined by the
935 function sin the libc_freeres section. Those are called as part of
936 the atexit routine, just like _IO_cleanup. The problem is we do
937 not know whether the freeres code is called first or _IO_cleanup.
938 if the former is the case, we set the DEALLOC_BUFFER variable to
939 true and _IO_unbuffer_write will take care of the rest. If
940 _IO_unbuffer_write is called first we add the streams to a list
941 which the freeres function later can walk through. */
942 static void _IO_unbuffer_write (void);
944 static bool dealloc_buffers;
945 static _IO_FILE *freeres_list;
947 static void
948 _IO_unbuffer_write (void)
950 struct _IO_FILE *fp;
951 for (fp = (_IO_FILE *) INTUSE(_IO_list_all); fp; fp = fp->_chain)
953 if (! (fp->_flags & _IO_UNBUFFERED)
954 && (! (fp->_flags & _IO_NO_WRITES)
955 || (fp->_flags & _IO_IS_APPENDING))
956 /* Iff stream is un-orientated, it wasn't used. */
957 && fp->_mode != 0)
959 int cnt;
960 #define MAXTRIES 2
961 for (cnt = 0; cnt < MAXTRIES; ++cnt)
962 if (_IO_lock_trylock (*fp->_lock) == 0)
963 break;
964 else
965 /* Give the other thread time to finish up its use of the
966 stream. */
967 __sched_yield ();
969 if (! dealloc_buffers && !(fp->_flags & _IO_USER_BUF))
971 fp->_flags |= _IO_USER_BUF;
973 fp->_freeres_list = freeres_list;
974 freeres_list = fp;
975 fp->_freeres_buf = fp->_IO_buf_base;
976 fp->_freeres_size = _IO_blen (fp);
979 _IO_SETBUF (fp, NULL, 0);
981 if (cnt < MAXTRIES)
982 _IO_lock_unlock (*fp->_lock);
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 INTUSE(_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 INTUSE(_IO_free_backup_area) (fp);
1140 INTDEF(_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 INTDEF(_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) INTUSE(_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