2 Copyright (C) 1993, 1995 Free Software Foundation
4 This file is part of the GNU IO Library. This library is free
5 software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option)
10 This 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this library; see the file COPYING. If not, write to the Free
17 Software Foundation, 59 Temple Place - Suite 330, Boston, 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 not cause
21 the resulting executable to be covered by the GNU General Public License.
22 This exception does not however invalidate any other reasons why
23 the executable file might be covered by the GNU General Public License. */
25 /* Generic or default I/O operations. */
34 DEFUN(_IO_un_link
, (fp
),
37 if (fp
->_flags
& _IO_LINKED
) {
39 for (f
= &_IO_list_all
; *f
!= NULL
; f
= &(*f
)->_chain
) {
45 fp
->_flags
&= ~_IO_LINKED
;
50 DEFUN(_IO_link_in
, (fp
),
53 if ((fp
->_flags
& _IO_LINKED
) == 0) {
54 fp
->_flags
|= _IO_LINKED
;
55 fp
->_chain
= _IO_list_all
;
60 /* Return minimum _pos markers
61 Assumes the current get area is the main get area. */
64 DEFUN(_IO_least_marker
, (fp
),
65 register _IO_FILE
*fp
)
67 _IO_ssize_t least_so_far
= fp
->_IO_read_end
- fp
->_IO_read_base
;
68 register struct _IO_marker
*mark
;
69 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
70 if (mark
->_pos
< least_so_far
)
71 least_so_far
= mark
->_pos
;
75 /* Switch current get area from backup buffer to (start of) main get area. */
78 DEFUN(_IO_switch_to_main_get_area
, (fp
),
82 fp
->_flags
&= ~_IO_IN_BACKUP
;
83 /* Swap _IO_read_end and _IO_save_end. */
84 tmp
= fp
->_IO_read_end
; fp
->_IO_read_end
= fp
->_IO_save_end
; fp
->_IO_save_end
= tmp
;
85 /* Swap _IO_read_base and _IO_save_base. */
86 tmp
= fp
->_IO_read_base
; fp
->_IO_read_base
= fp
->_IO_save_base
; fp
->_IO_save_base
= tmp
;
87 fp
->_IO_read_ptr
= fp
->_IO_read_base
;
90 /* Switch current get area from main get area to (end of) backup area. */
93 DEFUN(_IO_switch_to_backup_area
, (fp
),
94 register _IO_FILE
*fp
)
97 fp
->_flags
|= _IO_IN_BACKUP
;
98 /* Swap _IO_read_end and _IO_save_end. */
99 tmp
= fp
->_IO_read_end
; fp
->_IO_read_end
= fp
->_IO_save_end
; fp
->_IO_save_end
= tmp
;
100 /* Swap _gbase and _IO_save_base. */
101 tmp
= fp
->_IO_read_base
; fp
->_IO_read_base
= fp
->_IO_save_base
; fp
->_IO_save_base
= tmp
;
102 fp
->_IO_read_ptr
= fp
->_IO_read_end
;
106 DEFUN(_IO_switch_to_get_mode
, (fp
),
107 register _IO_FILE
*fp
)
109 if (fp
->_IO_write_ptr
> fp
->_IO_write_base
)
110 if (_IO_OVERFLOW (fp
, EOF
) == EOF
)
112 if (_IO_in_backup(fp
))
113 fp
->_IO_read_base
= fp
->_IO_backup_base
;
116 fp
->_IO_read_base
= fp
->_IO_buf_base
;
117 if (fp
->_IO_write_ptr
> fp
->_IO_read_end
)
118 fp
->_IO_read_end
= fp
->_IO_write_ptr
;
120 fp
->_IO_read_ptr
= fp
->_IO_write_ptr
;
122 fp
->_IO_write_base
= fp
->_IO_write_ptr
= fp
->_IO_write_end
= fp
->_IO_read_ptr
;
124 fp
->_flags
&= ~_IO_CURRENTLY_PUTTING
;
129 DEFUN(_IO_free_backup_area
, (fp
),
130 register _IO_FILE
*fp
)
132 if (_IO_in_backup (fp
))
133 _IO_switch_to_main_get_area(fp
); /* Just in case. */
134 free (fp
->_IO_save_base
);
135 fp
->_IO_save_base
= NULL
;
136 fp
->_IO_save_end
= NULL
;
137 fp
->_IO_backup_base
= NULL
;
142 DEFUN(_IO_switch_to_put_mode
, (fp
),
143 register _IO_FILE
*fp
)
145 fp
->_IO_write_base
= fp
->_IO_read_ptr
;
146 fp
->_IO_write_ptr
= fp
->_IO_read_ptr
;
147 /* Following is wrong if line- or un-buffered? */
148 fp
->_IO_write_end
= fp
->_flags
& _IO_IN_BACKUP
? fp
->_IO_read_end
: fp
->_IO_buf_end
;
150 fp
->_IO_read_ptr
= fp
->_IO_read_end
;
151 fp
->_IO_read_base
= fp
->_IO_read_end
;
153 fp
->_flags
|= _IO_CURRENTLY_PUTTING
;
159 DEFUN(__overflow
, (f
, ch
),
160 _IO_FILE
*f AND
int ch
)
162 return _IO_OVERFLOW (f
, ch
);
166 DEFUN(save_for_backup
, (fp
),
169 /* Append [_IO_read_base.._IO_read_end] to backup area. */
170 int least_mark
= _IO_least_marker(fp
);
171 /* needed_size is how much space we need in the backup area. */
172 int needed_size
= (fp
->_IO_read_end
- fp
->_IO_read_base
) - least_mark
;
173 int current_Bsize
= fp
->_IO_save_end
- fp
->_IO_save_base
;
174 int avail
; /* Extra space available for future expansion. */
176 struct _IO_marker
*mark
;
177 if (needed_size
> current_Bsize
)
181 new_buffer
= (char*)malloc(avail
+needed_size
);
182 if (new_buffer
== NULL
)
183 return EOF
; /* FIXME */
186 memcpy(new_buffer
+ avail
,
187 fp
->_IO_save_end
+ least_mark
,
189 memcpy(new_buffer
+avail
- least_mark
,
191 fp
->_IO_read_end
- fp
->_IO_read_base
);
194 memcpy(new_buffer
+ avail
,
195 fp
->_IO_read_base
+ least_mark
,
197 if (fp
->_IO_save_base
)
198 free (fp
->_IO_save_base
);
199 fp
->_IO_save_base
= new_buffer
;
200 fp
->_IO_save_end
= new_buffer
+ avail
+ needed_size
;
204 avail
= current_Bsize
- needed_size
;
207 memmove(fp
->_IO_save_base
+ avail
,
208 fp
->_IO_save_end
+ least_mark
,
210 memcpy(fp
->_IO_save_base
+ avail
- least_mark
,
212 fp
->_IO_read_end
- fp
->_IO_read_base
);
214 else if (needed_size
> 0)
215 memcpy(fp
->_IO_save_base
+ avail
,
216 fp
->_IO_read_base
+ least_mark
,
219 /* FIXME: Dubious arithmetic if pointers are NULL */
220 fp
->_IO_backup_base
= fp
->_IO_save_base
+ avail
;
221 /* Adjust all the streammarkers. */
222 delta
= fp
->_IO_read_end
- fp
->_IO_read_base
;
223 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
229 DEFUN(__underflow
, (fp
),
232 if (_IO_in_put_mode(fp
))
233 if (_IO_switch_to_get_mode(fp
) == EOF
) return EOF
;
234 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
235 return *(unsigned char*)fp
->_IO_read_ptr
;
236 if (_IO_in_backup(fp
))
238 _IO_switch_to_main_get_area(fp
);
239 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
240 return *fp
->_IO_read_ptr
;
242 if (_IO_have_markers(fp
))
244 if (save_for_backup (fp
))
247 else if (_IO_have_backup(fp
))
248 _IO_free_backup_area(fp
);
249 return _IO_UNDERFLOW (fp
);
256 if (_IO_in_put_mode(fp
))
257 if (_IO_switch_to_get_mode(fp
) == EOF
) return EOF
;
258 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
259 return *(unsigned char*)fp
->_IO_read_ptr
++;
260 if (_IO_in_backup(fp
))
262 _IO_switch_to_main_get_area(fp
);
263 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
264 return *fp
->_IO_read_ptr
++;
266 if (_IO_have_markers(fp
))
268 if (save_for_backup (fp
))
271 else if (_IO_have_backup(fp
))
272 _IO_free_backup_area(fp
);
273 return _IO_UFLOW (fp
);
277 DEFUN(_IO_setb
, (f
, b
, eb
, a
),
278 _IO_FILE
*f AND
char *b AND
char *eb AND
int a
)
280 if (f
->_IO_buf_base
&& !(f
->_flags
& _IO_USER_BUF
))
281 FREE_BUF(f
->_IO_buf_base
);
285 f
->_flags
&= ~_IO_USER_BUF
;
287 f
->_flags
|= _IO_USER_BUF
;
291 DEFUN(_IO_doallocbuf
, (fp
),
292 register _IO_FILE
*fp
)
294 if (fp
->_IO_buf_base
)
296 if (!(fp
->_flags
& _IO_UNBUFFERED
))
297 if (_IO_DOALLOCATE (fp
) != EOF
)
299 _IO_setb(fp
, fp
->_shortbuf
, fp
->_shortbuf
+1, 0);
303 DEFUN(_IO_default_underflow
, (fp
),
310 DEFUN(_IO_default_uflow
, (fp
),
313 int ch
= _IO_UNDERFLOW (fp
);
316 return *(unsigned char*)fp
->_IO_read_ptr
++;
320 DEFUN(_IO_default_xsputn
, (f
, data
, n
),
321 register _IO_FILE
*f AND
const void *data AND _IO_size_t n
)
323 register const char *s
= (char*) data
;
324 register _IO_size_t more
= n
;
329 _IO_ssize_t count
= f
->_IO_write_end
- f
->_IO_write_ptr
; /* Space available. */
332 if ((_IO_size_t
) count
> more
)
336 memcpy(f
->_IO_write_ptr
, s
, count
);
338 f
->_IO_write_ptr
+= count
;
344 register char *p
= f
->_IO_write_ptr
;
345 register _IO_ssize_t i
;
346 for (i
= count
; --i
>= 0; ) *p
++ = *s
++;
347 f
->_IO_write_ptr
= p
;
351 if (more
== 0 || __overflow(f
, (unsigned char)*s
++) == EOF
)
359 DEFUN(_IO_sgetn
, (fp
, data
, n
),
360 _IO_FILE
*fp AND
void *data AND _IO_size_t n
)
362 /* FIXME handle putback buffer here! */
363 return _IO_XSGETN (fp
, data
, n
);
367 DEFUN(_IO_default_xsgetn
, (fp
, data
, n
),
368 _IO_FILE
*fp AND
void *data AND _IO_size_t n
)
370 register _IO_size_t more
= n
;
371 register char *s
= (char*) data
;
374 _IO_ssize_t count
= fp
->_IO_read_end
- fp
->_IO_read_ptr
; /* Data available. */
377 if ((_IO_size_t
) count
> more
)
381 memcpy(s
, fp
->_IO_read_ptr
, count
);
383 fp
->_IO_read_ptr
+= count
;
389 register char *p
= fp
->_IO_read_ptr
;
390 register int i
= (int)count
;
391 while (--i
>= 0) *s
++ = *p
++;
392 fp
->_IO_read_ptr
= p
;
396 if (more
== 0 || __underflow(fp
) == EOF
)
403 DEFUN(_IO_sync
, (fp
),
404 register _IO_FILE
*fp
)
410 DEFUN(_IO_default_setbuf
, (fp
, p
, len
),
411 register _IO_FILE
*fp AND
char* p AND _IO_ssize_t len
)
413 if (_IO_SYNC (fp
) == EOF
)
415 if (p
== NULL
|| len
== 0)
417 fp
->_flags
|= _IO_UNBUFFERED
;
418 _IO_setb(fp
, fp
->_shortbuf
, fp
->_shortbuf
+1, 0);
422 fp
->_flags
&= ~_IO_UNBUFFERED
;
423 _IO_setb(fp
, p
, p
+len
, 0);
425 fp
->_IO_write_base
= fp
->_IO_write_ptr
= fp
->_IO_write_end
= 0;
426 fp
->_IO_read_base
= fp
->_IO_read_ptr
= fp
->_IO_read_end
= 0;
431 DEFUN(_IO_default_seekpos
, (fp
, pos
, mode
),
432 _IO_FILE
*fp AND _IO_pos_t pos AND
int mode
)
434 return _IO_SEEKOFF (fp
, _IO_pos_as_off(pos
), 0, mode
);
438 DEFUN(_IO_default_doallocate
, (fp
),
441 char *buf
= ALLOC_BUF(_IO_BUFSIZ
);
444 _IO_setb(fp
, buf
, buf
+_IO_BUFSIZ
, 1);
449 DEFUN(_IO_init
, (fp
, flags
),
450 register _IO_FILE
*fp AND
int flags
)
452 fp
->_flags
= _IO_MAGIC
|flags
;
453 fp
->_IO_buf_base
= NULL
;
454 fp
->_IO_buf_end
= NULL
;
455 fp
->_IO_read_base
= NULL
;
456 fp
->_IO_read_ptr
= NULL
;
457 fp
->_IO_read_end
= NULL
;
458 fp
->_IO_write_base
= NULL
;
459 fp
->_IO_write_ptr
= NULL
;
460 fp
->_IO_write_end
= NULL
;
461 fp
->_chain
= NULL
; /* Not necessary. */
463 fp
->_IO_save_base
= NULL
;
464 fp
->_IO_backup_base
= NULL
;
465 fp
->_IO_save_end
= NULL
;
469 __libc_lock_init (*fp
->_lock
);
474 DEFUN(_IO_default_sync
, (fp
),
480 /* The way the C++ classes are mapped into the C functions in the
481 current implementation, this function can get called twice! */
484 DEFUN(_IO_default_finish
, (fp
),
487 struct _IO_marker
*mark
;
488 if (fp
->_IO_buf_base
&& !(fp
->_flags
& _IO_USER_BUF
))
490 FREE_BUF(fp
->_IO_buf_base
);
491 fp
->_IO_buf_base
= fp
->_IO_buf_end
= NULL
;
494 for (mark
= fp
->_markers
; mark
!= NULL
; mark
= mark
->_next
)
497 if (fp
->_IO_save_base
)
499 free (fp
->_IO_save_base
);
500 fp
->_IO_save_base
= NULL
;
504 __libc_lock_fini (*fp
->_lock
);
511 DEFUN(_IO_default_seekoff
, (fp
, offset
, dir
, mode
),
512 register _IO_FILE
*fp AND _IO_off_t offset AND
int dir AND
int mode
)
518 DEFUN(_IO_sputbackc
, (fp
, c
),
519 register _IO_FILE
*fp AND
int c
)
523 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
524 && (unsigned char)fp
->_IO_read_ptr
[-1] == (unsigned char)c
)
527 result
= (unsigned char)c
;
530 result
= _IO_PBACKFAIL (fp
, c
);
533 fp
->_flags
&= ~_IO_EOF_SEEN
;
539 DEFUN(_IO_sungetc
, (fp
),
540 register _IO_FILE
*fp
)
544 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
)
547 result
= (unsigned char)*fp
->_IO_read_ptr
;
550 result
= _IO_PBACKFAIL (fp
, EOF
);
553 fp
->_flags
&= ~_IO_EOF_SEEN
;
558 #if 0 /* Work in progress */
560 DEFUN(_IO_set_column
, (fp
, c
),
561 register _IO_FILE
*fp AND
int c
)
566 fp
->_column
= c
- (fp
->_IO_write_ptr
- fp
->_IO_write_base
);
570 DEFUN(_IO_set_column
, (fp
, i
),
571 register _IO_FILE
*fp AND
int i
)
573 fp
->_cur_column
= i
+1;
580 DEFUN(_IO_adjust_column
, (start
, line
, count
),
581 unsigned start AND
const char *line AND
int count
)
583 register const char *ptr
= line
+ count
;
586 return line
+ count
- ptr
- 1;
587 return start
+ count
;
591 DEFUN(_IO_get_column
, (fp
),
592 register _IO_FILE
*fp
)
595 return _IO_adjust_column(fp
->_cur_column
- 1,
597 fp
->_IO_write_ptr
- fp
->_IO_write_base
);
602 DEFUN_VOID(_IO_flush_all
)
606 for (fp
= _IO_list_all
; fp
!= NULL
; fp
= fp
->_chain
)
607 if (fp
->_IO_write_ptr
> fp
->_IO_write_base
608 && _IO_OVERFLOW (fp
, EOF
) == EOF
)
614 DEFUN_VOID(_IO_flush_all_linebuffered
)
617 for (fp
= _IO_list_all
; fp
!= NULL
; fp
= fp
->_chain
)
618 if (fp
->_flags
& _IO_LINE_BUF
)
619 _IO_OVERFLOW (fp
, EOF
);
623 DEFUN_VOID(_IO_unbuffer_all
)
626 for (fp
= _IO_list_all
; fp
!= NULL
; fp
= fp
->_chain
)
627 if (! (fp
->_flags
& _IO_UNBUFFERED
))
628 _IO_SETBUF (fp
, NULL
, 0);
632 DEFUN_VOID(_IO_cleanup
)
636 /* We currently don't have a reliable mechanism for making sure that
637 C++ static destructors are executed in the correct order.
638 So it is possible that other static destructord might want to
639 write to cout - and they're supposed to be able to do so.
641 The following will make the standard streambufs be unbuffered,
642 which forces any output from late destructors to be written out. */
647 DEFUN(_IO_init_marker
, (marker
, fp
),
648 struct _IO_marker
*marker AND _IO_FILE
*fp
)
651 if (_IO_in_put_mode(fp
))
652 _IO_switch_to_get_mode(fp
);
653 if (_IO_in_backup(fp
))
654 marker
->_pos
= fp
->_IO_read_ptr
- fp
->_IO_read_end
;
656 marker
->_pos
= fp
->_IO_read_ptr
- fp
->_IO_read_base
;
658 /* Should perhaps sort the chain? */
659 marker
->_next
= fp
->_markers
;
660 fp
->_markers
= marker
;
664 DEFUN(_IO_remove_marker
, (marker
),
665 register struct _IO_marker
*marker
)
667 /* Unlink from sb's chain. */
668 register struct _IO_marker
**ptr
= &marker
->_sbuf
->_markers
;
669 for (; ; ptr
= &(*ptr
)->_next
)
673 else if (*ptr
== marker
)
675 *ptr
= marker
->_next
;
680 if _sbuf has a backup area that is no longer needed
, should we
delete
681 it now
, or wait until the next underflow
?
685 #define BAD_DELTA EOF
688 DEFUN(_IO_marker_difference
, (mark1
, mark2
),
689 struct _IO_marker
*mark1 AND
struct _IO_marker
*mark2
)
691 return mark1
->_pos
- mark2
->_pos
;
694 /* Return difference between MARK and current posistion of MARK's stream. */
696 DEFUN(_IO_marker_delta
, (mark
),
697 struct _IO_marker
*mark
)
700 if (mark
->_sbuf
== NULL
)
702 if (_IO_in_backup(mark
->_sbuf
))
703 cur_pos
= mark
->_sbuf
->_IO_read_ptr
- mark
->_sbuf
->_IO_read_end
;
705 cur_pos
= mark
->_sbuf
->_IO_read_ptr
- mark
->_sbuf
->_IO_read_base
;
706 return mark
->_pos
- cur_pos
;
710 DEFUN(_IO_seekmark
, (fp
, mark
, delta
),
711 _IO_FILE
*fp AND
struct _IO_marker
*mark AND
int delta
)
713 if (mark
->_sbuf
!= fp
)
717 if (_IO_in_backup(fp
))
718 _IO_switch_to_main_get_area(fp
);
719 fp
->_IO_read_ptr
= fp
->_IO_read_base
+ mark
->_pos
;
723 if (!_IO_in_backup(fp
))
724 _IO_switch_to_backup_area(fp
);
725 fp
->_IO_read_ptr
= fp
->_IO_read_end
+ mark
->_pos
;
731 DEFUN(_IO_unsave_markers
, (fp
),
732 register _IO_FILE
*fp
)
734 register struct _IO_marker
*mark
= fp
->_markers
;
738 streampos offset
= seekoff(0, ios::cur
, ios::in
);
741 offset
+= eGptr() - Gbase();
742 for ( ; mark
!= NULL
; mark
= mark
->_next
)
743 mark
->set_streampos(mark
->_pos
+ offset
);
747 for ( ; mark
!= NULL
; mark
= mark
->_next
)
748 mark
->set_streampos(EOF
);
754 if (_IO_have_backup(fp
))
755 _IO_free_backup_area(fp
);
759 DEFUN(_IO_nobackup_pbackfail
, (fp
, c
),
760 register _IO_FILE
*fp AND
int c
)
762 if (fp
->_IO_read_ptr
> fp
->_IO_read_base
)
764 if (c
!= EOF
&& *fp
->_IO_read_ptr
!= c
)
765 *fp
->_IO_read_ptr
= c
;
766 return (unsigned char)c
;
770 DEFUN(_IO_default_pbackfail
, (fp
, c
),
771 register _IO_FILE
*fp AND
int c
)
773 if (fp
->_IO_read_ptr
<= fp
->_IO_read_base
)
775 /* Need to handle a filebuf in write mode (switch to read mode). FIXME!*/
776 if (_IO_have_backup(fp
) && !_IO_in_backup(fp
))
777 _IO_switch_to_backup_area(fp
);
779 if (!_IO_have_backup(fp
))
781 /* No backup buffer: allocate one. */
782 /* Use nshort buffer, if unused? (probably not) FIXME */
783 int backup_size
= 128;
784 char *bbuf
= (char*)malloc(backup_size
);
787 fp
->_IO_save_base
= bbuf
;
788 fp
->_IO_save_end
= fp
->_IO_save_base
+ backup_size
;
789 fp
->_IO_backup_base
= fp
->_IO_save_end
;
790 _IO_switch_to_backup_area(fp
);
792 else if (fp
->_IO_read_ptr
<= fp
->_IO_read_base
)
794 /* Increase size of existing backup buffer. */
796 _IO_size_t old_size
= fp
->_IO_read_end
- fp
->_IO_read_base
;
798 new_size
= 2 * old_size
;
799 new_buf
= (char*)malloc(new_size
);
802 memcpy(new_buf
+(new_size
-old_size
), fp
->_IO_read_base
, old_size
);
803 free (fp
->_IO_read_base
);
805 new_buf
, new_buf
+(new_size
-old_size
), new_buf
+new_size
);
806 fp
->_IO_backup_base
= fp
->_IO_read_ptr
;
810 if (c
!= EOF
&& *fp
->_IO_read_ptr
!= c
)
811 *fp
->_IO_read_ptr
= c
;
812 return (unsigned char)*fp
->_IO_read_ptr
;
816 DEFUN(_IO_default_seek
, (fp
, offset
, dir
),
817 _IO_FILE
*fp AND _IO_off_t offset AND
int dir
)
823 DEFUN(_IO_default_stat
, (fp
, st
),
824 _IO_FILE
*fp AND
void* st
)
830 DEFUN(_IO_default_read
, (fp
, data
, n
),
831 register _IO_FILE
* fp AND
void* data AND _IO_ssize_t n
)
837 DEFUN(_IO_default_write
, (fp
, data
, n
),
838 register _IO_FILE
* fp AND
const void* data AND _IO_ssize_t n
)
854 ~__io_defs() { _IO_cleanup(); }
862 weak_alias (_IO_cleanup
, _cleanup
)