1 /* Copyright (C) 1993, 1995, 1997, 1998, 1999 Free Software Foundation, Inc.
2 This file is part of the GNU IO Library.
3 Written by Ulrich Drepper <drepper@cygnus.com>.
4 Based on the single byte version by Per Bothner <bothner@cygnus.com>.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2, or (at
9 your option) any later version.
11 This library is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this library; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
21 As a special exception, if you link this library with files
22 compiled with a GNU compiler to produce an executable, this does
23 not cause the resulting executable to be covered by the GNU General
24 Public License. This exception does not however invalidate any
25 other reasons why the executable file might be covered by the GNU
26 General Public License. */
37 _IO_wfile_setbuf (fp
, p
, len
)
42 if (_IO_wdefault_setbuf (fp
, p
, len
) == NULL
)
45 fp
->_wide_data
->_IO_write_base
= fp
->_wide_data
->_IO_write_ptr
=
46 fp
->_wide_data
->_IO_write_end
= fp
->_wide_data
->_IO_buf_base
;
47 _IO_wsetg (fp
, fp
->_wide_data
->_IO_buf_base
, fp
->_wide_data
->_IO_buf_base
,
48 fp
->_wide_data
->_IO_buf_base
);
54 /* Convert TO_DO wide character from DATA to FP.
55 Then mark FP as having empty buffers. */
57 _IO_wdo_write (fp
, data
, to_do
)
62 struct _IO_codecvt
*cc
= &fp
->_wide_data
->_codecvt
;
67 enum __codecvt_result result
;
68 const wchar_t *new_data
;
70 if (fp
->_IO_write_end
== fp
->_IO_write_ptr
)
72 _IO_new_file_overflow (fp
, EOF
);
73 assert (fp
->_IO_write_end
> fp
->_IO_write_ptr
);
76 /* Now convert from the internal format into the external buffer. */
77 result
= (*cc
->__codecvt_do_out
) (cc
, &fp
->_wide_data
->_IO_state
,
78 data
, data
+ to_do
, &new_data
,
83 /* Write out what we produced so far. */
84 if (_IO_new_do_write (fp
, fp
->_IO_write_base
,
85 fp
->_IO_write_ptr
- fp
->_IO_write_base
) == EOF
)
86 /* Something went wrong. */
89 count
+= new_data
- data
;
90 to_do
-= new_data
- data
;
93 /* Next see whether we had problems during the conversion. If yes,
95 if (result
!= __codecvt_ok
)
99 _IO_wsetg (fp
, fp
->_wide_data
->_IO_buf_base
, fp
->_wide_data
->_IO_buf_base
,
100 fp
->_wide_data
->_IO_buf_base
);
101 fp
->_wide_data
->_IO_write_base
= fp
->_wide_data
->_IO_write_ptr
102 = fp
->_wide_data
->_IO_buf_base
;
103 fp
->_wide_data
->_IO_write_end
= ((fp
->_flags
& (_IO_LINE_BUF
+_IO_UNBUFFERED
))
104 ? fp
->_wide_data
->_IO_buf_base
105 : fp
->_wide_data
->_IO_buf_end
);
112 _IO_wfile_underflow (fp
)
115 struct _IO_codecvt
*cd
;
116 enum __codecvt_result status
;
119 const char *read_ptr_copy
;
121 if (fp
->_flags
& _IO_NO_READS
)
123 fp
->_flags
|= _IO_ERR_SEEN
;
127 if (fp
->_wide_data
->_IO_read_ptr
< fp
->_wide_data
->_IO_read_end
)
128 return *fp
->_wide_data
->_IO_read_ptr
;
130 cd
= &fp
->_wide_data
->_codecvt
;
132 /* Maybe there is something left in the external buffer. */
133 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
136 size_t avail_bytes
= fp
->_IO_read_end
- fp
->_IO_read_ptr
;
138 if (avail_bytes
>= (*cd
->__codecvt_do_max_length
) (cd
))
140 /* There is more in the external. */
141 const char *read_stop
= (const char *) fp
->_IO_read_ptr
;
143 fp
->_wide_data
->_IO_last_state
= fp
->_wide_data
->_IO_state
;
144 status
= (*cd
->__codecvt_do_in
) (cd
, &fp
->_wide_data
->_IO_state
,
145 fp
->_IO_read_ptr
, fp
->_IO_read_end
,
147 fp
->_wide_data
->_IO_read_end
,
148 fp
->_wide_data
->_IO_buf_end
,
149 &fp
->_wide_data
->_IO_read_end
);
151 fp
->_IO_read_ptr
= (char *) read_stop
;
153 /* If we managed to generate some text return the next character. */
154 if (fp
->_wide_data
->_IO_read_ptr
< fp
->_wide_data
->_IO_read_end
)
155 return *fp
->_wide_data
->_IO_read_ptr
;
157 if (status
== __codecvt_error
)
159 __set_errno (EILSEQ
);
160 fp
->_flags
|= _IO_ERR_SEEN
;
165 /* Move the remaining content of the read buffer to the beginning. */
166 memmove (fp
->_IO_buf_base
, fp
->_IO_read_ptr
,
167 fp
->_IO_read_end
- fp
->_IO_read_ptr
);
168 fp
->_IO_read_end
= (fp
->_IO_buf_base
169 + (fp
->_IO_read_end
- fp
->_IO_read_ptr
));
170 fp
->_IO_read_base
= fp
->_IO_read_ptr
= fp
->_IO_buf_base
;
173 fp
->_IO_read_base
= fp
->_IO_read_ptr
= fp
->_IO_read_end
=
176 fp
->_IO_write_base
= fp
->_IO_write_ptr
= fp
->_IO_write_end
=
179 if (fp
->_IO_buf_base
== NULL
)
181 /* Maybe we already have a push back pointer. */
182 if (fp
->_IO_save_base
!= NULL
)
184 free (fp
->_IO_save_base
);
185 fp
->_flags
&= ~_IO_IN_BACKUP
;
190 if (fp
->_wide_data
->_IO_buf_base
== NULL
)
192 /* Maybe we already have a push back pointer. */
193 if (fp
->_wide_data
->_IO_save_base
!= NULL
)
195 free (fp
->_wide_data
->_IO_save_base
);
196 fp
->_flags
&= ~_IO_IN_BACKUP
;
198 _IO_wdoallocbuf (fp
);
201 /* Flush all line buffered files before reading. */
202 /* FIXME This can/should be moved to genops ?? */
203 if (fp
->_flags
& (_IO_LINE_BUF
|_IO_UNBUFFERED
))
204 _IO_flush_all_linebuffered ();
206 _IO_switch_to_get_mode (fp
);
208 fp
->_IO_read_base
= fp
->_IO_read_ptr
= fp
->_IO_buf_base
;
209 fp
->_IO_read_end
= fp
->_IO_buf_base
;
210 fp
->_IO_write_base
= fp
->_IO_write_ptr
= fp
->_IO_write_end
213 fp
->_wide_data
->_IO_read_base
= fp
->_wide_data
->_IO_read_ptr
=
214 fp
->_wide_data
->_IO_buf_base
;
215 fp
->_wide_data
->_IO_read_end
= fp
->_wide_data
->_IO_buf_base
;
216 fp
->_wide_data
->_IO_write_base
= fp
->_wide_data
->_IO_write_ptr
=
217 fp
->_wide_data
->_IO_write_end
= fp
->_wide_data
->_IO_buf_base
;
221 count
= _IO_SYSREAD (fp
, fp
->_IO_read_end
,
222 fp
->_IO_buf_end
- fp
->_IO_read_end
);
225 if (count
== 0 && tries
== 0)
226 fp
->_flags
|= _IO_EOF_SEEN
;
228 fp
->_flags
|= _IO_ERR_SEEN
, count
= 0;
230 fp
->_IO_read_end
+= count
;
234 /* There are some bytes in the external buffer but they don't
235 convert to anything. */
236 __set_errno (EILSEQ
);
239 if (fp
->_offset
!= _IO_pos_BAD
)
240 _IO_pos_adjust (fp
->_offset
, count
);
242 /* Now convert the read input. */
243 fp
->_wide_data
->_IO_last_state
= fp
->_wide_data
->_IO_state
;
244 fp
->_IO_read_base
= fp
->_IO_read_ptr
;
245 status
= (*cd
->__codecvt_do_in
) (cd
, &fp
->_wide_data
->_IO_state
,
246 fp
->_IO_read_ptr
, fp
->_IO_read_end
,
248 fp
->_wide_data
->_IO_read_end
,
249 fp
->_wide_data
->_IO_buf_end
,
250 &fp
->_wide_data
->_IO_read_end
);
252 fp
->_IO_read_ptr
= (char *) read_ptr_copy
;
253 if (fp
->_wide_data
->_IO_read_end
== fp
->_wide_data
->_IO_buf_base
)
255 if (status
== __codecvt_error
|| fp
->_IO_read_end
== fp
->_IO_buf_end
)
257 __set_errno (EILSEQ
);
258 fp
->_flags
|= _IO_ERR_SEEN
;
262 /* The read bytes make no complete character. Try reading again. */
263 assert (status
== __codecvt_partial
);
268 return *fp
->_wide_data
->_IO_read_ptr
;
273 _IO_wfile_overflow (f
, wch
)
277 if (f
->_flags
& _IO_NO_WRITES
) /* SET ERROR */
279 f
->_flags
|= _IO_ERR_SEEN
;
283 /* If currently reading or no buffer allocated. */
284 if ((f
->_flags
& _IO_CURRENTLY_PUTTING
) == 0)
286 /* Allocate a buffer if needed. */
287 if (f
->_wide_data
->_IO_write_base
== 0)
290 _IO_wsetg (f
, f
->_wide_data
->_IO_buf_base
,
291 f
->_wide_data
->_IO_buf_base
, f
->_wide_data
->_IO_buf_base
);
295 /* Otherwise must be currently reading. If _IO_read_ptr
296 (and hence also _IO_read_end) is at the buffer end,
297 logically slide the buffer forwards one block (by setting
298 the read pointers to all point at the beginning of the
299 block). This makes room for subsequent output.
300 Otherwise, set the read pointers to _IO_read_end (leaving
301 that alone, so it can continue to correspond to the
302 external position). */
303 if (f
->_wide_data
->_IO_read_ptr
== f
->_wide_data
->_IO_buf_end
)
305 f
->_IO_read_end
= f
->_IO_read_ptr
= f
->_IO_buf_base
;
306 f
->_wide_data
->_IO_read_end
= f
->_wide_data
->_IO_read_ptr
=
307 f
->_wide_data
->_IO_buf_base
;
310 f
->_wide_data
->_IO_write_ptr
= f
->_wide_data
->_IO_read_ptr
;
311 f
->_wide_data
->_IO_write_base
= f
->_wide_data
->_IO_write_ptr
;
312 f
->_wide_data
->_IO_write_end
= f
->_wide_data
->_IO_buf_end
;
313 f
->_wide_data
->_IO_read_base
= f
->_wide_data
->_IO_read_ptr
=
314 f
->_wide_data
->_IO_read_end
;
316 f
->_flags
|= _IO_CURRENTLY_PUTTING
;
317 if (f
->_flags
& (_IO_LINE_BUF
+_IO_UNBUFFERED
))
318 f
->_wide_data
->_IO_write_end
= f
->_wide_data
->_IO_write_ptr
;
321 return _IO_do_flush (f
);
322 if (f
->_wide_data
->_IO_write_ptr
== f
->_wide_data
->_IO_buf_end
)
323 /* Buffer is really full */
324 if (_IO_do_flush (f
) == WEOF
)
326 *f
->_wide_data
->_IO_write_ptr
++ = wch
;
327 if ((f
->_flags
& _IO_UNBUFFERED
)
328 || ((f
->_flags
& _IO_LINE_BUF
) && wch
== L
'\n'))
329 if (_IO_do_flush (f
) == WEOF
)
341 /* char* ptr = cur_ptr(); */
342 if (fp
->_wide_data
->_IO_write_ptr
> fp
->_wide_data
->_IO_write_base
)
343 if (_IO_do_flush (fp
))
345 delta
= fp
->_wide_data
->_IO_read_ptr
- fp
->_wide_data
->_IO_read_end
;
348 /* We have to find out how many bytes we have to go back in the
350 struct _IO_codecvt
*cv
= &fp
->_wide_data
->_codecvt
;
353 int clen
= (*cv
->__codecvt_do_encoding
) (cv
);
356 /* It is easy, a fixed number of input bytes are used for each
361 /* We have to find out the hard way how much to back off.
362 To do this we determine how much input we needed to
363 generate the wide characters up to the current reading
367 fp
->_wide_data
->_IO_state
= fp
->_wide_data
->_IO_last_state
;
368 nread
= (*cv
->__codecvt_do_length
) (cv
, &fp
->_wide_data
->_IO_state
,
370 fp
->_IO_read_end
, delta
);
371 fp
->_IO_read_ptr
= fp
->_IO_read_base
+ nread
;
372 delta
= -(fp
->_IO_read_end
- fp
->_IO_read_base
- nread
);
375 new_pos
= _IO_SYSSEEK (fp
, delta
, 1);
376 if (new_pos
!= (_IO_off64_t
) EOF
)
378 fp
->_wide_data
->_IO_read_end
= fp
->_wide_data
->_IO_read_ptr
;
379 fp
->_IO_read_end
= fp
->_IO_read_ptr
;
382 else if (errno
== ESPIPE
)
383 ; /* Ignore error from unseekable devices. */
389 fp
->_offset
= _IO_pos_BAD
;
390 /* FIXME: Cleanup - can this be shared? */
391 /* setg(base(), ptr, ptr); */
396 _IO_wfile_seekoff (fp
, offset
, dir
, mode
)
403 _IO_off64_t delta
, new_offset
;
405 /* POSIX.1 8.2.3.7 says that after a call the fflush() the file
406 offset of the underlying file must be exact. */
407 int must_be_exact
= ((fp
->_wide_data
->_IO_read_base
408 == fp
->_wide_data
->_IO_read_end
)
409 && (fp
->_wide_data
->_IO_write_base
410 == fp
->_wide_data
->_IO_write_ptr
));
413 dir
= _IO_seek_cur
, offset
= 0; /* Don't move any pointers. */
415 /* Flush unwritten characters.
416 (This may do an unneeded write if we seek within the buffer.
417 But to be able to switch to reading, we would need to set
418 egptr to ptr. That can't be done in the current design,
419 which assumes file_ptr() is eGptr. Anyway, since we probably
420 end up flushing when we close(), it doesn't make much difference.)
421 FIXME: simulate mem-papped files. */
423 if (fp
->_wide_data
->_IO_write_ptr
> fp
->_wide_data
->_IO_write_base
424 || _IO_in_put_mode (fp
))
425 if (_IO_switch_to_wget_mode (fp
))
428 if (fp
->_wide_data
->_IO_buf_base
== NULL
)
430 /* It could be that we already have a pushback buffer. */
431 if (fp
->_wide_data
->_IO_read_base
!= NULL
)
433 free (fp
->_wide_data
->_IO_read_base
);
434 fp
->_flags
&= ~_IO_IN_BACKUP
;
437 _IO_setp (fp
, fp
->_IO_buf_base
, fp
->_IO_buf_base
);
438 _IO_setg (fp
, fp
->_IO_buf_base
, fp
->_IO_buf_base
, fp
->_IO_buf_base
);
439 _IO_wsetp (fp
, fp
->_wide_data
->_IO_buf_base
,
440 fp
->_wide_data
->_IO_buf_base
);
441 _IO_wsetg (fp
, fp
->_wide_data
->_IO_buf_base
,
442 fp
->_wide_data
->_IO_buf_base
, fp
->_wide_data
->_IO_buf_base
);
447 struct _IO_codecvt
*cv
;
451 /* Adjust for read-ahead (bytes is buffer). To do this we must
452 find out which position in the external buffer corresponds to
453 the current position in the internal buffer. */
454 cv
= &fp
->_wide_data
->_codecvt
;
455 clen
= (*cv
->__codecvt_do_encoding
) (cv
);
458 offset
-= (fp
->_wide_data
->_IO_read_end
459 - fp
->_wide_data
->_IO_read_ptr
) * clen
;
464 delta
= fp
->_wide_data
->_IO_read_ptr
- fp
->_wide_data
->_IO_read_end
;
465 fp
->_wide_data
->_IO_state
= fp
->_wide_data
->_IO_last_state
;
466 nread
= (*cv
->__codecvt_do_length
) (cv
, &fp
->_wide_data
->_IO_state
,
468 fp
->_IO_read_end
, delta
);
469 fp
->_IO_read_ptr
= fp
->_IO_read_base
+ nread
;
470 offset
-= fp
->_IO_read_end
- fp
->_IO_read_base
- nread
;
473 if (fp
->_offset
== _IO_pos_BAD
)
475 /* Make offset absolute, assuming current pointer is file_ptr(). */
476 offset
+= fp
->_offset
;
485 if (_IO_SYSSTAT (fp
, &st
) == 0 && S_ISREG (st
.st_mode
))
487 offset
+= st
.st_size
;
494 /* At this point, dir==_IO_seek_set. */
496 /* If we are only interested in the current position we've found it now. */
500 /* If destination is within current buffer, optimize: */
501 if (fp
->_offset
!= _IO_pos_BAD
&& fp
->_IO_read_base
!= NULL
502 && !_IO_in_backup (fp
))
504 /* Offset relative to start of main get area. */
505 _IO_off64_t rel_offset
= (offset
- fp
->_offset
506 + (fp
->_IO_read_end
- fp
->_IO_read_base
));
510 if (_IO_in_backup (fp
))
511 _IO_switch_to_main_get_area (fp
);
513 if (rel_offset
<= fp
->_IO_read_end
- fp
->_IO_read_base
)
515 fp
->_IO_read_ptr
= fp
->_IO_read_base
+ rel_offset
;
516 _IO_setp (fp
, fp
->_IO_buf_base
, fp
->_IO_buf_base
);
518 /* Now set the pointer for the internal buffer. This
519 might be an iterative process. Though the read
520 pointer is somewhere in the current external buffer
521 this does not mean we can convert this whole buffer
522 at once fitting in the internal buffer. */
529 _IO_mask_flags (fp
, 0, _IO_EOF_SEEN
);
533 /* If we have streammarkers, seek forward by reading ahead. */
534 if (_IO_have_markers (fp
))
536 int to_skip
= rel_offset
537 - (fp
->_IO_read_ptr
- fp
->_IO_read_base
);
538 if (ignore (to_skip
) != to_skip
)
540 _IO_mask_flags (fp
, 0, _IO_EOF_SEEN
);
546 if (rel_offset
< 0 && rel_offset
>= Bbase () - Bptr ())
548 if (!_IO_in_backup (fp
))
549 _IO_switch_to_backup_area (fp
);
550 gbump (fp
->_IO_read_end
+ rel_offset
- fp
->_IO_read_ptr
);
551 _IO_mask_flags (fp
, 0, _IO_EOF_SEEN
);
558 _IO_unsave_markers (fp
);
561 if (fp
->_flags
& _IO_NO_READS
)
564 /* Try to seek to a block boundary, to improve kernel page management. */
565 new_offset
= offset
& ~(fp
->_IO_buf_end
- fp
->_IO_buf_base
- 1);
566 delta
= offset
- new_offset
;
567 if (delta
> fp
->_IO_buf_end
- fp
->_IO_buf_base
)
572 result
= _IO_SYSSEEK (fp
, new_offset
, 0);
579 count
= _IO_SYSREAD (fp
, fp
->_IO_buf_base
,
581 ? delta
: fp
->_IO_buf_end
- fp
->_IO_buf_base
));
584 /* We weren't allowed to read, but try to seek the remainder. */
585 offset
= count
== EOF
? delta
: delta
-count
;
590 _IO_setg (fp
, fp
->_IO_buf_base
, fp
->_IO_buf_base
+ delta
,
591 fp
->_IO_buf_base
+ count
);
592 _IO_setp (fp
, fp
->_IO_buf_base
, fp
->_IO_buf_base
);
593 fp
->_offset
= result
+ count
;
594 _IO_mask_flags (fp
, 0, _IO_EOF_SEEN
);
598 _IO_unsave_markers (fp
);
599 result
= _IO_SYSSEEK (fp
, offset
, dir
);
602 _IO_mask_flags (fp
, 0, _IO_EOF_SEEN
);
603 fp
->_offset
= result
;
604 _IO_setg (fp
, fp
->_IO_buf_base
, fp
->_IO_buf_base
, fp
->_IO_buf_base
);
605 _IO_setp (fp
, fp
->_IO_buf_base
, fp
->_IO_buf_base
);
610 /* We need to do it since it is possible that the file offset in
611 the kernel may be changed behind our back. It may happen when
612 we fopen a file and then do a fork. One process may access the
613 the file and the kernel file offset will be changed. */
614 if (fp
->_offset
>= 0)
615 _IO_SYSSEEK (fp
, fp
->_offset
, 0);
622 _IO_wfile_xsputn (f
, data
, n
)
627 register const wchar_t *s
= (const wchar_t *) data
;
628 _IO_size_t to_do
= n
;
634 /* This is an optimized implementation.
635 If the amount to be written straddles a block boundary
636 (or the filebuf is unbuffered), use sys_write directly. */
638 /* First figure out how much space is available in the buffer. */
639 count
= f
->_wide_data
->_IO_write_end
- f
->_wide_data
->_IO_write_ptr
;
640 if ((f
->_flags
& _IO_LINE_BUF
) && (f
->_flags
& _IO_CURRENTLY_PUTTING
))
642 count
= f
->_wide_data
->_IO_buf_end
- f
->_wide_data
->_IO_write_ptr
;
645 register const wchar_t *p
;
646 for (p
= s
+ n
; p
> s
; )
657 /* Then fill the buffer. */
665 f
->_wide_data
->_IO_write_ptr
=
666 __wmempcpy (f
->_wide_data
->_IO_write_ptr
, s
, count
);
668 wmemcpy (f
->_wide_data
->_IO_write_ptr
, s
, count
);
669 f
->_wide_data
->_IO_write_ptr
+= count
;
675 register wchar_t *p
= f
->_wide_data
->_IO_write_ptr
;
676 register int i
= (int) count
;
679 f
->_wide_data
->_IO_write_ptr
= p
;
684 to_do
-= _IO_wdefault_xsputn (f
, s
, to_do
);
686 && f
->_wide_data
->_IO_write_ptr
!= f
->_wide_data
->_IO_write_base
)
687 _IO_wdo_write (f
, f
->_wide_data
->_IO_write_base
,
688 f
->_wide_data
->_IO_write_ptr
689 - f
->_wide_data
->_IO_write_base
);
695 struct _IO_jump_t _IO_wfile_jumps
=
698 JUMP_INIT(finish
, _IO_new_file_finish
),
699 JUMP_INIT(overflow
, (_IO_overflow_t
) _IO_wfile_overflow
),
700 JUMP_INIT(underflow
, (_IO_underflow_t
) _IO_wfile_underflow
),
701 JUMP_INIT(uflow
, (_IO_underflow_t
) _IO_wdefault_uflow
),
702 JUMP_INIT(pbackfail
, (_IO_pbackfail_t
) _IO_wdefault_pbackfail
),
703 JUMP_INIT(xsputn
, _IO_wfile_xsputn
),
704 JUMP_INIT(xsgetn
, _IO_file_xsgetn
),
705 JUMP_INIT(seekoff
, _IO_wfile_seekoff
),
706 JUMP_INIT(seekpos
, _IO_default_seekpos
),
707 JUMP_INIT(setbuf
, _IO_new_file_setbuf
),
708 JUMP_INIT(sync
, (_IO_sync_t
) _IO_wfile_sync
),
709 JUMP_INIT(doallocate
, _IO_wfile_doallocate
),
710 JUMP_INIT(read
, _IO_file_read
),
711 JUMP_INIT(write
, _IO_new_file_write
),
712 JUMP_INIT(seek
, _IO_file_seek
),
713 JUMP_INIT(close
, _IO_file_close
),
714 JUMP_INIT(stat
, _IO_file_stat
),
715 JUMP_INIT(showmanyc
, _IO_default_showmanyc
),
716 JUMP_INIT(imbue
, _IO_default_imbue
)