Not all VALUEs are identical
[official-gcc.git] / libstdc++-v3 / libio / genops.c
blobb1ae5afc506fd350fc344b64ccbca13d8f2bb139
1 /* Copyright (C) 1993, 1995, 1997-1999, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU IO Library.
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2, or (at
7 your option) any later version.
9 This library is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this library; see the file COPYING. If not, write to
16 the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
17 MA 02111-1307, USA.
19 As a special exception, if you link this library with files
20 compiled with a GNU compiler to produce an executable, this does
21 not cause the resulting executable to be covered by the GNU General
22 Public License. This exception does not however invalidate any
23 other reasons why the executable file might be covered by the GNU
24 General Public License. */
26 /* Generic or default I/O operations. */
28 #include "libioP.h"
29 #ifdef __STDC__
30 #include <stdlib.h>
31 #endif
32 #include <string.h>
34 #ifdef _IO_MTSAFE_IO
35 static _IO_lock_t list_all_lock = _IO_lock_initializer;
36 #endif
38 void
39 _IO_un_link (fp)
40 struct _IO_FILE_plus *fp;
42 if (fp->file._flags & _IO_LINKED)
44 struct _IO_FILE_plus **f;
45 #ifdef _IO_MTSAFE_IO
46 _IO_lock_lock (list_all_lock);
47 #endif
48 for (f = &_IO_list_all; *f; f = (struct _IO_FILE_plus **) &(*f)->file._chain)
50 if (*f == fp)
52 *f = (struct _IO_FILE_plus *) fp->file._chain;
53 break;
56 #ifdef _IO_MTSAFE_IO
57 _IO_lock_unlock (list_all_lock);
58 #endif
59 fp->file._flags &= ~_IO_LINKED;
63 void
64 _IO_link_in (fp)
65 struct _IO_FILE_plus *fp;
67 if ((fp->file._flags & _IO_LINKED) == 0)
69 fp->file._flags |= _IO_LINKED;
70 #ifdef _IO_MTSAFE_IO
71 _IO_lock_lock (list_all_lock);
72 #endif
73 fp->file._chain = (_IO_FILE *) _IO_list_all;
74 _IO_list_all = fp;
75 #ifdef _IO_MTSAFE_IO
76 _IO_lock_unlock (list_all_lock);
77 #endif
81 /* Return minimum _pos markers
82 Assumes the current get area is the main get area. */
83 _IO_ssize_t _IO_least_marker __P ((_IO_FILE *fp, char *end_p));
85 _IO_ssize_t
86 _IO_least_marker (fp, end_p)
87 _IO_FILE *fp;
88 char *end_p;
90 _IO_ssize_t least_so_far = end_p - fp->_IO_read_base;
91 struct _IO_marker *mark;
92 for (mark = fp->_markers; mark != NULL; mark = mark->_next)
93 if (mark->_pos < least_so_far)
94 least_so_far = mark->_pos;
95 return least_so_far;
98 /* Switch current get area from backup buffer to (start of) main get area. */
100 void
101 _IO_switch_to_main_get_area (fp)
102 _IO_FILE *fp;
104 char *tmp;
105 fp->_flags &= ~_IO_IN_BACKUP;
106 /* Swap _IO_read_end and _IO_save_end. */
107 tmp = fp->_IO_read_end;
108 fp->_IO_read_end = fp->_IO_save_end;
109 fp->_IO_save_end= tmp;
110 /* Swap _IO_read_base and _IO_save_base. */
111 tmp = fp->_IO_read_base;
112 fp->_IO_read_base = fp->_IO_save_base;
113 fp->_IO_save_base = tmp;
114 /* Set _IO_read_ptr. */
115 fp->_IO_read_ptr = fp->_IO_read_base;
118 /* Switch current get area from main get area to (end of) backup area. */
120 void
121 _IO_switch_to_backup_area (fp)
122 _IO_FILE *fp;
124 char *tmp;
125 fp->_flags |= _IO_IN_BACKUP;
126 /* Swap _IO_read_end and _IO_save_end. */
127 tmp = fp->_IO_read_end;
128 fp->_IO_read_end = fp->_IO_save_end;
129 fp->_IO_save_end = tmp;
130 /* Swap _IO_read_base and _IO_save_base. */
131 tmp = fp->_IO_read_base;
132 fp->_IO_read_base = fp->_IO_save_base;
133 fp->_IO_save_base = tmp;
134 /* Set _IO_read_ptr. */
135 fp->_IO_read_ptr = fp->_IO_read_end;
139 _IO_switch_to_get_mode (fp)
140 _IO_FILE *fp;
142 if (fp->_IO_write_ptr > fp->_IO_write_base)
143 if (_IO_OVERFLOW (fp, EOF) == EOF)
144 return EOF;
145 if (_IO_in_backup (fp))
146 fp->_IO_read_base = fp->_IO_backup_base;
147 else
149 fp->_IO_read_base = fp->_IO_buf_base;
150 if (fp->_IO_write_ptr > fp->_IO_read_end)
151 fp->_IO_read_end = fp->_IO_write_ptr;
153 fp->_IO_read_ptr = fp->_IO_write_ptr;
155 fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end = fp->_IO_read_ptr;
157 fp->_flags &= ~_IO_CURRENTLY_PUTTING;
158 return 0;
161 void
162 _IO_free_backup_area (fp)
163 _IO_FILE *fp;
165 if (_IO_in_backup (fp))
166 _IO_switch_to_main_get_area (fp); /* Just in case. */
167 free (fp->_IO_save_base);
168 fp->_IO_save_base = NULL;
169 fp->_IO_save_end = NULL;
170 fp->_IO_backup_base = NULL;
173 #if 0
175 _IO_switch_to_put_mode (fp)
176 _IO_FILE *fp;
178 fp->_IO_write_base = fp->_IO_read_ptr;
179 fp->_IO_write_ptr = fp->_IO_read_ptr;
180 /* Following is wrong if line- or un-buffered? */
181 fp->_IO_write_end = (fp->_flags & _IO_IN_BACKUP
182 ? fp->_IO_read_end : fp->_IO_buf_end);
184 fp->_IO_read_ptr = fp->_IO_read_end;
185 fp->_IO_read_base = fp->_IO_read_end;
187 fp->_flags |= _IO_CURRENTLY_PUTTING;
188 return 0;
190 #endif
193 __overflow (f, ch)
194 _IO_FILE *f;
195 int ch;
197 /* This is a single-byte stream. */
198 if (f->_mode == 0)
199 _IO_fwide (f, -1);
200 return _IO_OVERFLOW (f, ch);
203 static int save_for_backup __P ((_IO_FILE *fp, char *end_p))
204 #ifdef _LIBC
205 internal_function
206 #endif
209 static int
210 #ifdef _LIBC
211 internal_function
212 #endif
213 save_for_backup (fp, end_p)
214 _IO_FILE *fp;
215 char *end_p;
217 /* Append [_IO_read_base..end_p] to backup area. */
218 _IO_ssize_t least_mark = _IO_least_marker (fp, end_p);
219 /* needed_size is how much space we need in the backup area. */
220 _IO_size_t needed_size = (end_p - fp->_IO_read_base) - least_mark;
221 /* FIXME: Dubious arithmetic if pointers are NULL */
222 _IO_size_t current_Bsize = fp->_IO_save_end - fp->_IO_save_base;
223 _IO_size_t avail; /* Extra space available for future expansion. */
224 _IO_ssize_t delta;
225 struct _IO_marker *mark;
226 if (needed_size > current_Bsize)
228 char *new_buffer;
229 avail = 100;
230 new_buffer = (char *) malloc (avail + needed_size);
231 if (new_buffer == NULL)
232 return EOF; /* FIXME */
233 if (least_mark < 0)
235 #ifdef _LIBC
236 __mempcpy (__mempcpy (new_buffer + avail,
237 fp->_IO_save_end + least_mark,
238 -least_mark),
239 fp->_IO_read_base,
240 end_p - fp->_IO_read_base);
241 #else
242 memcpy (new_buffer + avail,
243 fp->_IO_save_end + least_mark,
244 -least_mark);
245 memcpy (new_buffer + avail - least_mark,
246 fp->_IO_read_base,
247 end_p - fp->_IO_read_base);
248 #endif
250 else
251 memcpy (new_buffer + avail,
252 fp->_IO_read_base + least_mark,
253 needed_size);
254 if (fp->_IO_save_base)
255 free (fp->_IO_save_base);
256 fp->_IO_save_base = new_buffer;
257 fp->_IO_save_end = new_buffer + avail + needed_size;
259 else
261 avail = current_Bsize - needed_size;
262 if (least_mark < 0)
264 memmove (fp->_IO_save_base + avail,
265 fp->_IO_save_end + least_mark,
266 -least_mark);
267 memcpy (fp->_IO_save_base + avail - least_mark,
268 fp->_IO_read_base,
269 end_p - fp->_IO_read_base);
271 else if (needed_size > 0)
272 memcpy (fp->_IO_save_base + avail,
273 fp->_IO_read_base + least_mark,
274 needed_size);
276 fp->_IO_backup_base = fp->_IO_save_base + avail;
277 /* Adjust all the streammarkers. */
278 delta = end_p - fp->_IO_read_base;
279 for (mark = fp->_markers; mark != NULL; mark = mark->_next)
280 mark->_pos -= delta;
281 return 0;
285 __underflow (fp)
286 _IO_FILE *fp;
288 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
289 if (fp->_vtable_offset == 0 && _IO_fwide (fp, -1) != -1)
290 return EOF;
291 #endif
293 if (fp->_mode == 0)
294 _IO_fwide (fp, -1);
295 if (_IO_in_put_mode (fp))
296 if (_IO_switch_to_get_mode (fp) == EOF)
297 return EOF;
298 if (fp->_IO_read_ptr < fp->_IO_read_end)
299 return *(unsigned char *) fp->_IO_read_ptr;
300 if (_IO_in_backup (fp))
302 _IO_switch_to_main_get_area (fp);
303 if (fp->_IO_read_ptr < fp->_IO_read_end)
304 return *(unsigned char *) fp->_IO_read_ptr;
306 if (_IO_have_markers (fp))
308 if (save_for_backup (fp, fp->_IO_read_end))
309 return EOF;
311 else if (_IO_have_backup (fp))
312 _IO_free_backup_area (fp);
313 return _IO_UNDERFLOW (fp);
317 __uflow (fp)
318 _IO_FILE *fp;
320 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
321 if (fp->_vtable_offset == 0 && _IO_fwide (fp, -1) != -1)
322 return EOF;
323 #endif
325 if (fp->_mode == 0)
326 _IO_fwide (fp, -11);
327 if (_IO_in_put_mode (fp))
328 if (_IO_switch_to_get_mode (fp) == EOF)
329 return EOF;
330 if (fp->_IO_read_ptr < fp->_IO_read_end)
331 return *(unsigned char *) fp->_IO_read_ptr++;
332 if (_IO_in_backup (fp))
334 _IO_switch_to_main_get_area (fp);
335 if (fp->_IO_read_ptr < fp->_IO_read_end)
336 return *(unsigned char *) fp->_IO_read_ptr++;
338 if (_IO_have_markers (fp))
340 if (save_for_backup (fp, fp->_IO_read_end))
341 return EOF;
343 else if (_IO_have_backup (fp))
344 _IO_free_backup_area (fp);
345 return _IO_UFLOW (fp);
348 void
349 _IO_setb (f, b, eb, a)
350 _IO_FILE *f;
351 char *b;
352 char *eb;
353 int a;
355 if (f->_IO_buf_base && !(f->_flags & _IO_USER_BUF))
356 FREE_BUF (f->_IO_buf_base, _IO_blen (f));
357 f->_IO_buf_base = b;
358 f->_IO_buf_end = eb;
359 if (a)
360 f->_flags &= ~_IO_USER_BUF;
361 else
362 f->_flags |= _IO_USER_BUF;
365 void
366 _IO_doallocbuf (fp)
367 _IO_FILE *fp;
369 if (fp->_IO_buf_base)
370 return;
371 if (!(fp->_flags & _IO_UNBUFFERED))
372 if (_IO_DOALLOCATE (fp) != EOF)
373 return;
374 _IO_setb (fp, fp->_shortbuf, fp->_shortbuf+1, 0);
378 _IO_default_underflow (fp)
379 _IO_FILE *fp;
381 return EOF;
385 _IO_default_uflow (fp)
386 _IO_FILE *fp;
388 int ch = _IO_UNDERFLOW (fp);
389 if (ch == EOF)
390 return EOF;
391 return *(unsigned char *) fp->_IO_read_ptr++;
394 _IO_size_t
395 _IO_default_xsputn (f, data, n)
396 _IO_FILE *f;
397 const void *data;
398 _IO_size_t n;
400 const char *s = (char *) data;
401 _IO_size_t more = n;
402 if (more <= 0)
403 return 0;
404 for (;;)
406 /* Space available. */
407 _IO_ssize_t count = f->_IO_write_end - f->_IO_write_ptr;
408 if (count > 0)
410 if ((_IO_size_t) count > more)
411 count = more;
412 if (count > 20)
414 #ifdef _LIBC
415 f->_IO_write_ptr = __mempcpy (f->_IO_write_ptr, s, count);
416 #else
417 memcpy (f->_IO_write_ptr, s, count);
418 f->_IO_write_ptr += count;
419 #endif
420 s += count;
422 else if (count <= 0)
423 count = 0;
424 else
426 char *p = f->_IO_write_ptr;
427 _IO_ssize_t i;
428 for (i = count; --i >= 0; )
429 *p++ = *s++;
430 f->_IO_write_ptr = p;
432 more -= count;
434 if (more == 0 || _IO_OVERFLOW (f, (unsigned char) *s++) == EOF)
435 break;
436 more--;
438 return n - more;
441 _IO_size_t
442 _IO_sgetn (fp, data, n)
443 _IO_FILE *fp;
444 void *data;
445 _IO_size_t n;
447 /* FIXME handle putback buffer here! */
448 return _IO_XSGETN (fp, data, n);
451 _IO_size_t
452 _IO_default_xsgetn (fp, data, n)
453 _IO_FILE *fp;
454 void *data;
455 _IO_size_t n;
457 _IO_size_t more = n;
458 char *s = (char*) data;
459 for (;;)
461 /* Data available. */
462 _IO_ssize_t count = fp->_IO_read_end - fp->_IO_read_ptr;
463 if (count > 0)
465 if ((_IO_size_t) count > more)
466 count = more;
467 if (count > 20)
469 #ifdef _LIBC
470 s = __mempcpy (s, fp->_IO_read_ptr, count);
471 #else
472 memcpy (s, fp->_IO_read_ptr, count);
473 s += count;
474 #endif
475 fp->_IO_read_ptr += count;
477 else if (count <= 0)
478 count = 0;
479 else
481 char *p = fp->_IO_read_ptr;
482 int i = (int) count;
483 while (--i >= 0)
484 *s++ = *p++;
485 fp->_IO_read_ptr = p;
487 more -= count;
489 if (more == 0 || __underflow (fp) == EOF)
490 break;
492 return n - more;
495 #if 0
496 /* Seems not to be needed. --drepper */
498 _IO_sync (fp)
499 _IO_FILE *fp;
501 return 0;
503 #endif
505 _IO_FILE *
506 _IO_default_setbuf (fp, p, len)
507 _IO_FILE *fp;
508 char *p;
509 _IO_ssize_t len;
511 if (_IO_SYNC (fp) == EOF)
512 return NULL;
513 if (p == NULL || len == 0)
515 fp->_flags |= _IO_UNBUFFERED;
516 _IO_setb (fp, fp->_shortbuf, fp->_shortbuf+1, 0);
518 else
520 fp->_flags &= ~_IO_UNBUFFERED;
521 _IO_setb (fp, p, p+len, 0);
523 fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end = 0;
524 fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_read_end = 0;
525 return fp;
528 _IO_off64_t
529 _IO_default_seekpos (fp, pos, mode)
530 _IO_FILE *fp;
531 _IO_off64_t pos;
532 int mode;
534 return _IO_SEEKOFF (fp, pos, 0, mode);
538 _IO_default_doallocate (fp)
539 _IO_FILE *fp;
541 char *buf;
543 ALLOC_BUF (buf, _IO_BUFSIZ, EOF);
544 _IO_setb (fp, buf, buf+_IO_BUFSIZ, 1);
545 return 1;
548 void
549 _IO_init (fp, flags)
550 _IO_FILE *fp;
551 int flags;
553 _IO_no_init (fp, flags, -1, NULL, NULL);
556 void
557 _IO_no_init (fp, flags, orientation, wd, jmp)
558 _IO_FILE *fp;
559 int flags;
560 int orientation;
561 struct _IO_wide_data *wd;
562 struct _IO_jump_t *jmp;
564 fp->_flags = _IO_MAGIC|flags;
565 fp->_IO_buf_base = NULL;
566 fp->_IO_buf_end = NULL;
567 fp->_IO_read_base = NULL;
568 fp->_IO_read_ptr = NULL;
569 fp->_IO_read_end = NULL;
570 fp->_IO_write_base = NULL;
571 fp->_IO_write_ptr = NULL;
572 fp->_IO_write_end = NULL;
573 fp->_chain = NULL; /* Not necessary. */
575 fp->_IO_save_base = NULL;
576 fp->_IO_backup_base = NULL;
577 fp->_IO_save_end = NULL;
578 fp->_markers = NULL;
579 fp->_cur_column = 0;
580 #if _IO_JUMPS_OFFSET
581 fp->_vtable_offset = 0;
582 #endif
583 #ifdef _IO_MTSAFE_IO
584 _IO_lock_init (*fp->_lock);
585 #endif
586 fp->_mode = orientation;
587 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
588 if (orientation >= 0)
590 fp->_wide_data = wd;
591 fp->_wide_data->_IO_buf_base = NULL;
592 fp->_wide_data->_IO_buf_end = NULL;
593 fp->_wide_data->_IO_read_base = NULL;
594 fp->_wide_data->_IO_read_ptr = NULL;
595 fp->_wide_data->_IO_read_end = NULL;
596 fp->_wide_data->_IO_write_base = NULL;
597 fp->_wide_data->_IO_write_ptr = NULL;
598 fp->_wide_data->_IO_write_end = NULL;
599 fp->_wide_data->_IO_save_base = NULL;
600 fp->_wide_data->_IO_backup_base = NULL;
601 fp->_wide_data->_IO_save_end = NULL;
603 fp->_wide_data->_wide_vtable = jmp;
605 #endif
609 _IO_default_sync (fp)
610 _IO_FILE *fp;
612 return 0;
615 /* The way the C++ classes are mapped into the C functions in the
616 current implementation, this function can get called twice! */
618 void
619 _IO_default_finish (fp, dummy)
620 _IO_FILE *fp;
621 int dummy;
623 struct _IO_marker *mark;
624 if (fp->_IO_buf_base && !(fp->_flags & _IO_USER_BUF))
626 FREE_BUF (fp->_IO_buf_base, _IO_blen (fp));
627 fp->_IO_buf_base = fp->_IO_buf_end = NULL;
630 for (mark = fp->_markers; mark != NULL; mark = mark->_next)
631 mark->_sbuf = NULL;
633 if (fp->_IO_save_base)
635 free (fp->_IO_save_base);
636 fp->_IO_save_base = NULL;
639 #ifdef _IO_MTSAFE_IO
640 _IO_lock_fini (*fp->_lock);
641 #endif
643 _IO_un_link ((struct _IO_FILE_plus *) fp);
646 _IO_off64_t
647 _IO_default_seekoff (fp, offset, dir, mode)
648 _IO_FILE *fp;
649 _IO_off64_t offset;
650 int dir;
651 int mode;
653 return _IO_pos_BAD;
657 _IO_sputbackc (fp, c)
658 _IO_FILE *fp;
659 int c;
661 int result;
663 if (fp->_IO_read_ptr > fp->_IO_read_base
664 && (unsigned char)fp->_IO_read_ptr[-1] == (unsigned char)c)
666 fp->_IO_read_ptr--;
667 result = (unsigned char) c;
669 else
670 result = _IO_PBACKFAIL (fp, c);
672 if (result != EOF)
673 fp->_flags &= ~_IO_EOF_SEEN;
675 return result;
679 _IO_sungetc (fp)
680 _IO_FILE *fp;
682 int result;
684 if (fp->_IO_read_ptr > fp->_IO_read_base)
686 fp->_IO_read_ptr--;
687 result = (unsigned char) *fp->_IO_read_ptr;
689 else
690 result = _IO_PBACKFAIL (fp, EOF);
692 if (result != EOF)
693 fp->_flags &= ~_IO_EOF_SEEN;
695 return result;
698 #if 0 /* Work in progress */
699 /* Seems not to be needed. */
700 #if 0
701 void
702 _IO_set_column (fp, c)
703 _IO_FILE *fp;
704 int c;
706 if (c == -1)
707 fp->_column = -1;
708 else
709 fp->_column = c - (fp->_IO_write_ptr - fp->_IO_write_base);
711 #else
713 _IO_set_column (fp, i)
714 _IO_FILE *fp;
715 int i;
717 fp->_cur_column = i + 1;
718 return 0;
720 #endif
721 #endif
724 unsigned
725 _IO_adjust_column (start, line, count)
726 unsigned start;
727 const char *line;
728 int count;
730 const char *ptr = line + count;
731 while (ptr > line)
732 if (*--ptr == '\n')
733 return line + count - ptr - 1;
734 return start + count;
737 #if 0
738 /* Seems not to be needed. --drepper */
740 _IO_get_column (fp)
741 _IO_FILE *fp;
743 if (fp->_cur_column)
744 return _IO_adjust_column (fp->_cur_column - 1,
745 fp->_IO_write_base,
746 fp->_IO_write_ptr - fp->_IO_write_base);
747 return -1;
749 #endif
752 _IO_flush_all ()
754 int result = 0;
755 struct _IO_FILE *fp;
756 for (fp = (_IO_FILE *) _IO_list_all; fp; fp = fp->_chain)
757 if (((fp->_mode < 0 && fp->_IO_write_ptr > fp->_IO_write_base)
758 #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
759 || (fp->_vtable_offset == 0
760 && fp->_mode > 0 && (fp->_wide_data->_IO_write_ptr
761 > fp->_wide_data->_IO_write_base))
762 #endif
764 && _IO_OVERFLOW (fp, EOF) == EOF)
765 result = EOF;
766 return result;
769 void
770 _IO_flush_all_linebuffered ()
772 struct _IO_FILE *fp;
773 for (fp = (_IO_FILE *) _IO_list_all; fp; fp = fp->_chain)
774 if ((fp->_flags & _IO_NO_WRITES) == 0 && fp->_flags & _IO_LINE_BUF)
775 _IO_OVERFLOW (fp, EOF);
778 static void _IO_unbuffer_write __P ((void));
780 static void
781 _IO_unbuffer_write ()
783 struct _IO_FILE *fp;
784 for (fp = (_IO_FILE *) _IO_list_all; fp; fp = fp->_chain)
785 if (! (fp->_flags & _IO_UNBUFFERED)
786 && (! (fp->_flags & _IO_NO_WRITES)
787 || (fp->_flags & _IO_IS_APPENDING))
788 /* Iff stream is un-orientated, it wasn't used. */
789 && fp->_mode != 0)
790 _IO_SETBUF (fp, NULL, 0);
794 _IO_cleanup ()
796 int result = _IO_flush_all ();
798 /* We currently don't have a reliable mechanism for making sure that
799 C++ static destructors are executed in the correct order.
800 So it is possible that other static destructors might want to
801 write to cout - and they're supposed to be able to do so.
803 The following will make the standard streambufs be unbuffered,
804 which forces any output from late destructors to be written out. */
805 _IO_unbuffer_write ();
807 return result;
811 void
812 _IO_init_marker (marker, fp)
813 struct _IO_marker *marker;
814 _IO_FILE *fp;
816 marker->_sbuf = fp;
817 if (_IO_in_put_mode (fp))
818 _IO_switch_to_get_mode (fp);
819 if (_IO_in_backup (fp))
820 marker->_pos = fp->_IO_read_ptr - fp->_IO_read_end;
821 else
822 marker->_pos = fp->_IO_read_ptr - fp->_IO_read_base;
824 /* Should perhaps sort the chain? */
825 marker->_next = fp->_markers;
826 fp->_markers = marker;
829 void
830 _IO_remove_marker (marker)
831 struct _IO_marker *marker;
833 /* Unlink from sb's chain. */
834 struct _IO_marker **ptr = &marker->_sbuf->_markers;
835 for (; ; ptr = &(*ptr)->_next)
837 if (*ptr == NULL)
838 break;
839 else if (*ptr == marker)
841 *ptr = marker->_next;
842 return;
845 #if 0
846 if _sbuf has a backup area that is no longer needed, should we delete
847 it now, or wait until the next underflow?
848 #endif
851 #define BAD_DELTA EOF
854 _IO_marker_difference (mark1, mark2)
855 struct _IO_marker *mark1;
856 struct _IO_marker *mark2;
858 return mark1->_pos - mark2->_pos;
861 /* Return difference between MARK and current position of MARK's stream. */
863 _IO_marker_delta (mark)
864 struct _IO_marker *mark;
866 int cur_pos;
867 if (mark->_sbuf == NULL)
868 return BAD_DELTA;
869 if (_IO_in_backup (mark->_sbuf))
870 cur_pos = mark->_sbuf->_IO_read_ptr - mark->_sbuf->_IO_read_end;
871 else
872 cur_pos = mark->_sbuf->_IO_read_ptr - mark->_sbuf->_IO_read_base;
873 return mark->_pos - cur_pos;
877 _IO_seekmark (fp, mark, delta)
878 _IO_FILE *fp;
879 struct _IO_marker *mark;
880 int delta;
882 if (mark->_sbuf != fp)
883 return EOF;
884 if (mark->_pos >= 0)
886 if (_IO_in_backup (fp))
887 _IO_switch_to_main_get_area (fp);
888 fp->_IO_read_ptr = fp->_IO_read_base + mark->_pos;
890 else
892 if (!_IO_in_backup (fp))
893 _IO_switch_to_backup_area (fp);
894 fp->_IO_read_ptr = fp->_IO_read_end + mark->_pos;
896 return 0;
899 void
900 _IO_unsave_markers (fp)
901 _IO_FILE *fp;
903 struct _IO_marker *mark = fp->_markers;
904 if (mark)
906 #ifdef TODO
907 streampos offset = seekoff (0, ios::cur, ios::in);
908 if (offset != EOF)
910 offset += eGptr () - Gbase ();
911 for ( ; mark != NULL; mark = mark->_next)
912 mark->set_streampos (mark->_pos + offset);
914 else
916 for ( ; mark != NULL; mark = mark->_next)
917 mark->set_streampos (EOF);
919 #endif
920 fp->_markers = 0;
923 if (_IO_have_backup (fp))
924 _IO_free_backup_area (fp);
927 #if 0
928 /* Seems not to be needed. --drepper */
930 _IO_nobackup_pbackfail (fp, c)
931 _IO_FILE *fp;
932 int c;
934 if (fp->_IO_read_ptr > fp->_IO_read_base)
935 fp->_IO_read_ptr--;
936 if (c != EOF && *fp->_IO_read_ptr != c)
937 *fp->_IO_read_ptr = c;
938 return (unsigned char) c;
940 #endif
943 _IO_default_pbackfail (fp, c)
944 _IO_FILE *fp;
945 int c;
947 if (fp->_IO_read_ptr > fp->_IO_read_base && !_IO_in_backup (fp)
948 && (unsigned char) fp->_IO_read_ptr[-1] == c)
949 --fp->_IO_read_ptr;
950 else
952 /* Need to handle a filebuf in write mode (switch to read mode). FIXME!*/
953 if (!_IO_in_backup (fp))
955 /* We need to keep the invariant that the main get area
956 logically follows the backup area. */
957 if (fp->_IO_read_ptr > fp->_IO_read_base && _IO_have_backup (fp))
959 if (save_for_backup (fp, fp->_IO_read_ptr))
960 return EOF;
962 else if (!_IO_have_backup (fp))
964 /* No backup buffer: allocate one. */
965 /* Use nshort buffer, if unused? (probably not) FIXME */
966 int backup_size = 128;
967 char *bbuf = (char *) malloc (backup_size);
968 if (bbuf == NULL)
969 return EOF;
970 fp->_IO_save_base = bbuf;
971 fp->_IO_save_end = fp->_IO_save_base + backup_size;
972 fp->_IO_backup_base = fp->_IO_save_end;
974 fp->_IO_read_base = fp->_IO_read_ptr;
975 _IO_switch_to_backup_area (fp);
977 else if (fp->_IO_read_ptr <= fp->_IO_read_base)
979 /* Increase size of existing backup buffer. */
980 _IO_size_t new_size;
981 _IO_size_t old_size = fp->_IO_read_end - fp->_IO_read_base;
982 char *new_buf;
983 new_size = 2 * old_size;
984 new_buf = (char *) malloc (new_size);
985 if (new_buf == NULL)
986 return EOF;
987 memcpy (new_buf + (new_size - old_size), fp->_IO_read_base,
988 old_size);
989 free (fp->_IO_read_base);
990 _IO_setg (fp, new_buf, new_buf + (new_size - old_size),
991 new_buf + new_size);
992 fp->_IO_backup_base = fp->_IO_read_ptr;
995 *--fp->_IO_read_ptr = c;
997 return (unsigned char) c;
1000 _IO_off64_t
1001 _IO_default_seek (fp, offset, dir)
1002 _IO_FILE *fp;
1003 _IO_off64_t offset;
1004 int dir;
1006 return _IO_pos_BAD;
1010 _IO_default_stat (fp, st)
1011 _IO_FILE *fp;
1012 void* st;
1014 return EOF;
1017 _IO_ssize_t
1018 _IO_default_read (fp, data, n)
1019 _IO_FILE* fp;
1020 void *data;
1021 _IO_ssize_t n;
1023 return -1;
1026 _IO_ssize_t
1027 _IO_default_write (fp, data, n)
1028 _IO_FILE *fp;
1029 const void *data;
1030 _IO_ssize_t n;
1032 return 0;
1036 _IO_default_showmanyc (fp)
1037 _IO_FILE *fp;
1039 return -1;
1042 void
1043 _IO_default_imbue (fp, locale)
1044 _IO_FILE *fp;
1045 void *locale;
1049 _IO_ITER
1050 _IO_iter_begin()
1052 return (_IO_ITER) _IO_list_all;
1055 _IO_ITER
1056 _IO_iter_end()
1058 return NULL;
1061 _IO_ITER
1062 _IO_iter_next(iter)
1063 _IO_ITER iter;
1065 return iter->_chain;
1068 _IO_FILE *
1069 _IO_iter_file(iter)
1070 _IO_ITER iter;
1072 return iter;
1075 void
1076 _IO_list_lock()
1078 #ifdef _IO_MTSAFE_IO
1079 _IO_lock_lock (list_all_lock);
1080 #endif
1083 void
1084 _IO_list_unlock()
1086 #ifdef _IO_MTSAFE_IO
1087 _IO_lock_unlock (list_all_lock);
1088 #endif
1091 void
1092 _IO_list_resetlock()
1094 #ifdef _IO_MTSAFE_IO
1095 _IO_lock_init (list_all_lock);
1096 #endif
1100 #ifdef TODO
1101 #if defined(linux)
1102 #define IO_CLEANUP ;
1103 #endif
1105 #ifdef IO_CLEANUP
1106 IO_CLEANUP
1107 #else
1108 struct __io_defs {
1109 __io_defs() { }
1110 ~__io_defs() { _IO_cleanup (); }
1112 __io_defs io_defs__;
1113 #endif
1115 #endif /* TODO */
1117 #ifdef weak_alias
1118 weak_alias (_IO_cleanup, _cleanup)
1119 #endif
1121 #ifdef text_set_element
1122 text_set_element(__libc_atexit, _cleanup);
1123 #endif