Allow SO* symbols.
[glibc.git] / libio / genops.c
blobc86adee4c01582021a4687713add7c9f9d5853fc
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) || fp->_mode > 0)
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);
777 #ifdef _LIBC
778 weak_alias (_IO_flush_all_linebuffered, _flushlbf)
779 #endif
781 static void _IO_unbuffer_write __P ((void));
783 static void
784 _IO_unbuffer_write ()
786 struct _IO_FILE *fp;
787 for (fp = (_IO_FILE *) _IO_list_all; fp; fp = fp->_chain)
788 if (! (fp->_flags & _IO_UNBUFFERED)
789 && (! (fp->_flags & _IO_NO_WRITES)
790 || (fp->_flags & _IO_IS_APPENDING))
791 /* Iff stream is un-orientated, it wasn't used. */
792 && fp->_mode != 0)
793 _IO_SETBUF (fp, NULL, 0);
797 _IO_cleanup ()
799 int result = _IO_flush_all ();
801 /* We currently don't have a reliable mechanism for making sure that
802 C++ static destructors are executed in the correct order.
803 So it is possible that other static destructors might want to
804 write to cout - and they're supposed to be able to do so.
806 The following will make the standard streambufs be unbuffered,
807 which forces any output from late destructors to be written out. */
808 _IO_unbuffer_write ();
810 return result;
814 void
815 _IO_init_marker (marker, fp)
816 struct _IO_marker *marker;
817 _IO_FILE *fp;
819 marker->_sbuf = fp;
820 if (_IO_in_put_mode (fp))
821 _IO_switch_to_get_mode (fp);
822 if (_IO_in_backup (fp))
823 marker->_pos = fp->_IO_read_ptr - fp->_IO_read_end;
824 else
825 marker->_pos = fp->_IO_read_ptr - fp->_IO_read_base;
827 /* Should perhaps sort the chain? */
828 marker->_next = fp->_markers;
829 fp->_markers = marker;
832 void
833 _IO_remove_marker (marker)
834 struct _IO_marker *marker;
836 /* Unlink from sb's chain. */
837 struct _IO_marker **ptr = &marker->_sbuf->_markers;
838 for (; ; ptr = &(*ptr)->_next)
840 if (*ptr == NULL)
841 break;
842 else if (*ptr == marker)
844 *ptr = marker->_next;
845 return;
848 #if 0
849 if _sbuf has a backup area that is no longer needed, should we delete
850 it now, or wait until the next underflow?
851 #endif
854 #define BAD_DELTA EOF
857 _IO_marker_difference (mark1, mark2)
858 struct _IO_marker *mark1;
859 struct _IO_marker *mark2;
861 return mark1->_pos - mark2->_pos;
864 /* Return difference between MARK and current position of MARK's stream. */
866 _IO_marker_delta (mark)
867 struct _IO_marker *mark;
869 int cur_pos;
870 if (mark->_sbuf == NULL)
871 return BAD_DELTA;
872 if (_IO_in_backup (mark->_sbuf))
873 cur_pos = mark->_sbuf->_IO_read_ptr - mark->_sbuf->_IO_read_end;
874 else
875 cur_pos = mark->_sbuf->_IO_read_ptr - mark->_sbuf->_IO_read_base;
876 return mark->_pos - cur_pos;
880 _IO_seekmark (fp, mark, delta)
881 _IO_FILE *fp;
882 struct _IO_marker *mark;
883 int delta;
885 if (mark->_sbuf != fp)
886 return EOF;
887 if (mark->_pos >= 0)
889 if (_IO_in_backup (fp))
890 _IO_switch_to_main_get_area (fp);
891 fp->_IO_read_ptr = fp->_IO_read_base + mark->_pos;
893 else
895 if (!_IO_in_backup (fp))
896 _IO_switch_to_backup_area (fp);
897 fp->_IO_read_ptr = fp->_IO_read_end + mark->_pos;
899 return 0;
902 void
903 _IO_unsave_markers (fp)
904 _IO_FILE *fp;
906 struct _IO_marker *mark = fp->_markers;
907 if (mark)
909 #ifdef TODO
910 streampos offset = seekoff (0, ios::cur, ios::in);
911 if (offset != EOF)
913 offset += eGptr () - Gbase ();
914 for ( ; mark != NULL; mark = mark->_next)
915 mark->set_streampos (mark->_pos + offset);
917 else
919 for ( ; mark != NULL; mark = mark->_next)
920 mark->set_streampos (EOF);
922 #endif
923 fp->_markers = 0;
926 if (_IO_have_backup (fp))
927 _IO_free_backup_area (fp);
930 #if 0
931 /* Seems not to be needed. --drepper */
933 _IO_nobackup_pbackfail (fp, c)
934 _IO_FILE *fp;
935 int c;
937 if (fp->_IO_read_ptr > fp->_IO_read_base)
938 fp->_IO_read_ptr--;
939 if (c != EOF && *fp->_IO_read_ptr != c)
940 *fp->_IO_read_ptr = c;
941 return (unsigned char) c;
943 #endif
946 _IO_default_pbackfail (fp, c)
947 _IO_FILE *fp;
948 int c;
950 if (fp->_IO_read_ptr > fp->_IO_read_base && !_IO_in_backup (fp)
951 && (unsigned char) fp->_IO_read_ptr[-1] == c)
952 --fp->_IO_read_ptr;
953 else
955 /* Need to handle a filebuf in write mode (switch to read mode). FIXME!*/
956 if (!_IO_in_backup (fp))
958 /* We need to keep the invariant that the main get area
959 logically follows the backup area. */
960 if (fp->_IO_read_ptr > fp->_IO_read_base && _IO_have_backup (fp))
962 if (save_for_backup (fp, fp->_IO_read_ptr))
963 return EOF;
965 else if (!_IO_have_backup (fp))
967 /* No backup buffer: allocate one. */
968 /* Use nshort buffer, if unused? (probably not) FIXME */
969 int backup_size = 128;
970 char *bbuf = (char *) malloc (backup_size);
971 if (bbuf == NULL)
972 return EOF;
973 fp->_IO_save_base = bbuf;
974 fp->_IO_save_end = fp->_IO_save_base + backup_size;
975 fp->_IO_backup_base = fp->_IO_save_end;
977 fp->_IO_read_base = fp->_IO_read_ptr;
978 _IO_switch_to_backup_area (fp);
980 else if (fp->_IO_read_ptr <= fp->_IO_read_base)
982 /* Increase size of existing backup buffer. */
983 _IO_size_t new_size;
984 _IO_size_t old_size = fp->_IO_read_end - fp->_IO_read_base;
985 char *new_buf;
986 new_size = 2 * old_size;
987 new_buf = (char *) malloc (new_size);
988 if (new_buf == NULL)
989 return EOF;
990 memcpy (new_buf + (new_size - old_size), fp->_IO_read_base,
991 old_size);
992 free (fp->_IO_read_base);
993 _IO_setg (fp, new_buf, new_buf + (new_size - old_size),
994 new_buf + new_size);
995 fp->_IO_backup_base = fp->_IO_read_ptr;
998 *--fp->_IO_read_ptr = c;
1000 return (unsigned char) c;
1003 _IO_off64_t
1004 _IO_default_seek (fp, offset, dir)
1005 _IO_FILE *fp;
1006 _IO_off64_t offset;
1007 int dir;
1009 return _IO_pos_BAD;
1013 _IO_default_stat (fp, st)
1014 _IO_FILE *fp;
1015 void* st;
1017 return EOF;
1020 _IO_ssize_t
1021 _IO_default_read (fp, data, n)
1022 _IO_FILE* fp;
1023 void *data;
1024 _IO_ssize_t n;
1026 return -1;
1029 _IO_ssize_t
1030 _IO_default_write (fp, data, n)
1031 _IO_FILE *fp;
1032 const void *data;
1033 _IO_ssize_t n;
1035 return 0;
1039 _IO_default_showmanyc (fp)
1040 _IO_FILE *fp;
1042 return -1;
1045 void
1046 _IO_default_imbue (fp, locale)
1047 _IO_FILE *fp;
1048 void *locale;
1052 _IO_ITER
1053 _IO_iter_begin()
1055 return (_IO_ITER) _IO_list_all;
1058 _IO_ITER
1059 _IO_iter_end()
1061 return NULL;
1064 _IO_ITER
1065 _IO_iter_next(iter)
1066 _IO_ITER iter;
1068 return iter->_chain;
1071 _IO_FILE *
1072 _IO_iter_file(iter)
1073 _IO_ITER iter;
1075 return iter;
1078 void
1079 _IO_list_lock()
1081 #ifdef _IO_MTSAFE_IO
1082 _IO_lock_lock (list_all_lock);
1083 #endif
1086 void
1087 _IO_list_unlock()
1089 #ifdef _IO_MTSAFE_IO
1090 _IO_lock_unlock (list_all_lock);
1091 #endif
1094 void
1095 _IO_list_resetlock()
1097 #ifdef _IO_MTSAFE_IO
1098 _IO_lock_init (list_all_lock);
1099 #endif
1103 #ifdef TODO
1104 #if defined(linux)
1105 #define IO_CLEANUP ;
1106 #endif
1108 #ifdef IO_CLEANUP
1109 IO_CLEANUP
1110 #else
1111 struct __io_defs {
1112 __io_defs() { }
1113 ~__io_defs() { _IO_cleanup (); }
1115 __io_defs io_defs__;
1116 #endif
1118 #endif /* TODO */
1120 #ifdef weak_alias
1121 weak_alias (_IO_cleanup, _cleanup)
1122 #endif
1124 #ifdef text_set_element
1125 text_set_element(__libc_atexit, _cleanup);
1126 #endif