.
[glibc/history.git] / libio / wfileops.c
blob5292f4854f782f1cab673ee9f7e4e647102c3274
1 /* Copyright (C) 1993,95,97,98,99,2000,2001,2002, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Written by Ulrich Drepper <drepper@cygnus.com>.
4 Based on the single byte version by Per Bothner <bothner@cygnus.com>.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 As a special exception, if you link the code in this file with
22 files compiled with a GNU compiler to produce an executable,
23 that does not cause the resulting executable to be covered by
24 the GNU Lesser General Public License. This exception does not
25 however invalidate any other reasons why the executable file
26 might be covered by the GNU Lesser General Public License.
27 This exception applies to code released by its copyright holders
28 in files containing the exception. */
30 #include <assert.h>
31 #include <libioP.h>
32 #include <wchar.h>
33 #include <gconv.h>
34 #include <stdlib.h>
35 #include <string.h>
38 #ifndef _LIBC
39 # define _IO_new_do_write _IO_do_write
40 # define _IO_new_file_attach _IO_file_attach
41 # define _IO_new_file_close_it _IO_file_close_it
42 # define _IO_new_file_finish _IO_file_finish
43 # define _IO_new_file_fopen _IO_file_fopen
44 # define _IO_new_file_init _IO_file_init
45 # define _IO_new_file_setbuf _IO_file_setbuf
46 # define _IO_new_file_sync _IO_file_sync
47 # define _IO_new_file_overflow _IO_file_overflow
48 # define _IO_new_file_seekoff _IO_file_seekoff
49 # define _IO_new_file_underflow _IO_file_underflow
50 # define _IO_new_file_write _IO_file_write
51 # define _IO_new_file_xsputn _IO_file_xsputn
52 #endif
55 /* Convert TO_DO wide character from DATA to FP.
56 Then mark FP as having empty buffers. */
57 int
58 _IO_wdo_write (fp, data, to_do)
59 _IO_FILE *fp;
60 const wchar_t *data;
61 _IO_size_t to_do;
63 struct _IO_codecvt *cc = fp->_codecvt;
65 if (to_do > 0)
67 if (fp->_IO_write_end == fp->_IO_write_ptr
68 && fp->_IO_write_end != fp->_IO_write_base)
70 if (_IO_new_do_write (fp, fp->_IO_write_base,
71 fp->_IO_write_ptr - fp->_IO_write_base) == EOF)
72 return EOF;
77 enum __codecvt_result result;
78 const wchar_t *new_data;
80 /* Now convert from the internal format into the external buffer. */
81 result = (*cc->__codecvt_do_out) (cc, &fp->_wide_data->_IO_state,
82 data, data + to_do, &new_data,
83 fp->_IO_write_ptr,
84 fp->_IO_buf_end,
85 &fp->_IO_write_ptr);
87 /* Write out what we produced so far. */
88 if (_IO_new_do_write (fp, fp->_IO_write_base,
89 fp->_IO_write_ptr - fp->_IO_write_base) == EOF)
90 /* Something went wrong. */
91 return WEOF;
93 to_do -= new_data - data;
95 /* Next see whether we had problems during the conversion. If yes,
96 we cannot go on. */
97 if (result != __codecvt_ok
98 && (result != __codecvt_partial || new_data - data == 0))
99 break;
101 data = new_data;
103 while (to_do > 0);
106 _IO_wsetg (fp, fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base,
107 fp->_wide_data->_IO_buf_base);
108 fp->_wide_data->_IO_write_base = fp->_wide_data->_IO_write_ptr
109 = fp->_wide_data->_IO_buf_base;
110 fp->_wide_data->_IO_write_end = ((fp->_flags & (_IO_LINE_BUF+_IO_UNBUFFERED))
111 ? fp->_wide_data->_IO_buf_base
112 : fp->_wide_data->_IO_buf_end);
114 return to_do == 0 ? 0 : WEOF;
116 INTDEF(_IO_wdo_write)
119 wint_t
120 _IO_wfile_underflow (fp)
121 _IO_FILE *fp;
123 struct _IO_codecvt *cd;
124 enum __codecvt_result status;
125 _IO_ssize_t count;
126 int tries;
127 const char *read_ptr_copy;
129 if (__builtin_expect (fp->_flags & _IO_NO_READS, 0))
131 fp->_flags |= _IO_ERR_SEEN;
132 __set_errno (EBADF);
133 return WEOF;
135 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
136 return *fp->_wide_data->_IO_read_ptr;
138 cd = fp->_codecvt;
140 /* Maybe there is something left in the external buffer. */
141 if (fp->_IO_read_ptr < fp->_IO_read_end)
143 /* There is more in the external. Convert it. */
144 const char *read_stop = (const char *) fp->_IO_read_ptr;
146 fp->_wide_data->_IO_last_state = fp->_wide_data->_IO_state;
147 fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_read_ptr =
148 fp->_wide_data->_IO_buf_base;
149 status = (*cd->__codecvt_do_in) (cd, &fp->_wide_data->_IO_state,
150 fp->_IO_read_ptr, fp->_IO_read_end,
151 &read_stop,
152 fp->_wide_data->_IO_read_ptr,
153 fp->_wide_data->_IO_buf_end,
154 &fp->_wide_data->_IO_read_end);
156 fp->_IO_read_ptr = (char *) read_stop;
158 /* If we managed to generate some text return the next character. */
159 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
160 return *fp->_wide_data->_IO_read_ptr;
162 if (status == __codecvt_error)
164 __set_errno (EILSEQ);
165 fp->_flags |= _IO_ERR_SEEN;
166 return WEOF;
169 /* Move the remaining content of the read buffer to the beginning. */
170 memmove (fp->_IO_buf_base, fp->_IO_read_ptr,
171 fp->_IO_read_end - fp->_IO_read_ptr);
172 fp->_IO_read_end = (fp->_IO_buf_base
173 + (fp->_IO_read_end - fp->_IO_read_ptr));
174 fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_buf_base;
176 else
177 fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_read_end =
178 fp->_IO_buf_base;
180 if (fp->_IO_buf_base == NULL)
182 /* Maybe we already have a push back pointer. */
183 if (fp->_IO_save_base != NULL)
185 free (fp->_IO_save_base);
186 fp->_flags &= ~_IO_IN_BACKUP;
188 INTUSE(_IO_doallocbuf) (fp);
190 fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_read_end =
191 fp->_IO_buf_base;
194 fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end =
195 fp->_IO_buf_base;
197 if (fp->_wide_data->_IO_buf_base == NULL)
199 /* Maybe we already have a push back pointer. */
200 if (fp->_wide_data->_IO_save_base != NULL)
202 free (fp->_wide_data->_IO_save_base);
203 fp->_flags &= ~_IO_IN_BACKUP;
205 INTUSE(_IO_wdoallocbuf) (fp);
208 /* Flush all line buffered files before reading. */
209 /* FIXME This can/should be moved to genops ?? */
210 if (fp->_flags & (_IO_LINE_BUF|_IO_UNBUFFERED))
212 #if 0
213 INTUSE(_IO_flush_all_linebuffered) ();
214 #else
215 /* We used to flush all line-buffered stream. This really isn't
216 required by any standard. My recollection is that
217 traditional Unix systems did this for stdout. stderr better
218 not be line buffered. So we do just that here
219 explicitly. --drepper */
220 _IO_acquire_lock (_IO_stdout);
222 if ((_IO_stdout->_flags & (_IO_LINKED | _IO_NO_WRITES | _IO_LINE_BUF))
223 == (_IO_LINKED | _IO_LINE_BUF))
224 _IO_OVERFLOW (_IO_stdout, EOF);
226 _IO_release_lock (_IO_stdout);
227 #endif
230 INTUSE(_IO_switch_to_get_mode) (fp);
232 fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_read_ptr =
233 fp->_wide_data->_IO_buf_base;
234 fp->_wide_data->_IO_read_end = fp->_wide_data->_IO_buf_base;
235 fp->_wide_data->_IO_write_base = fp->_wide_data->_IO_write_ptr =
236 fp->_wide_data->_IO_write_end = fp->_wide_data->_IO_buf_base;
238 tries = 0;
239 again:
240 count = _IO_SYSREAD (fp, fp->_IO_read_end,
241 fp->_IO_buf_end - fp->_IO_read_end);
242 if (count <= 0)
244 if (count == 0 && tries == 0)
245 fp->_flags |= _IO_EOF_SEEN;
246 else
247 fp->_flags |= _IO_ERR_SEEN, count = 0;
249 fp->_IO_read_end += count;
250 if (count == 0)
252 if (tries != 0)
253 /* There are some bytes in the external buffer but they don't
254 convert to anything. */
255 __set_errno (EILSEQ);
256 return WEOF;
258 if (fp->_offset != _IO_pos_BAD)
259 _IO_pos_adjust (fp->_offset, count);
261 /* Now convert the read input. */
262 fp->_wide_data->_IO_last_state = fp->_wide_data->_IO_state;
263 fp->_IO_read_base = fp->_IO_read_ptr;
264 status = (*cd->__codecvt_do_in) (cd, &fp->_wide_data->_IO_state,
265 fp->_IO_read_ptr, fp->_IO_read_end,
266 &read_ptr_copy,
267 fp->_wide_data->_IO_read_end,
268 fp->_wide_data->_IO_buf_end,
269 &fp->_wide_data->_IO_read_end);
271 fp->_IO_read_ptr = (char *) read_ptr_copy;
272 if (fp->_wide_data->_IO_read_end == fp->_wide_data->_IO_buf_base)
274 if (status == __codecvt_error || fp->_IO_read_end == fp->_IO_buf_end)
276 __set_errno (EILSEQ);
277 fp->_flags |= _IO_ERR_SEEN;
278 return WEOF;
281 /* The read bytes make no complete character. Try reading again. */
282 assert (status == __codecvt_partial);
283 ++tries;
284 goto again;
287 return *fp->_wide_data->_IO_read_ptr;
289 INTDEF(_IO_wfile_underflow)
292 static wint_t
293 _IO_wfile_underflow_mmap (_IO_FILE *fp)
295 struct _IO_codecvt *cd;
296 enum __codecvt_result status;
297 const char *read_stop;
299 if (__builtin_expect (fp->_flags & _IO_NO_READS, 0))
301 fp->_flags |= _IO_ERR_SEEN;
302 __set_errno (EBADF);
303 return WEOF;
305 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
306 return *fp->_wide_data->_IO_read_ptr;
308 cd = fp->_codecvt;
310 /* Maybe there is something left in the external buffer. */
311 if (fp->_IO_read_ptr >= fp->_IO_read_end
312 /* No. But maybe the read buffer is not fully set up. */
313 && _IO_file_underflow_mmap (fp) == EOF)
314 /* Nothing available. _IO_file_underflow_mmap has set the EOF or error
315 flags as appropriate. */
316 return WEOF;
318 /* There is more in the external. Convert it. */
319 read_stop = (const char *) fp->_IO_read_ptr;
321 if (fp->_wide_data->_IO_buf_base == NULL)
323 /* Maybe we already have a push back pointer. */
324 if (fp->_wide_data->_IO_save_base != NULL)
326 free (fp->_wide_data->_IO_save_base);
327 fp->_flags &= ~_IO_IN_BACKUP;
329 INTUSE(_IO_wdoallocbuf) (fp);
332 fp->_wide_data->_IO_last_state = fp->_wide_data->_IO_state;
333 fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_read_ptr =
334 fp->_wide_data->_IO_buf_base;
335 status = (*cd->__codecvt_do_in) (cd, &fp->_wide_data->_IO_state,
336 fp->_IO_read_ptr, fp->_IO_read_end,
337 &read_stop,
338 fp->_wide_data->_IO_read_ptr,
339 fp->_wide_data->_IO_buf_end,
340 &fp->_wide_data->_IO_read_end);
342 fp->_IO_read_ptr = (char *) read_stop;
344 /* If we managed to generate some text return the next character. */
345 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
346 return *fp->_wide_data->_IO_read_ptr;
348 /* There is some garbage at the end of the file. */
349 __set_errno (EILSEQ);
350 fp->_flags |= _IO_ERR_SEEN;
351 return WEOF;
354 static wint_t
355 _IO_wfile_underflow_maybe_mmap (_IO_FILE *fp)
357 /* This is the first read attempt. Doing the underflow will choose mmap
358 or vanilla operations and then punt to the chosen underflow routine.
359 Then we can punt to ours. */
360 if (_IO_file_underflow_maybe_mmap (fp) == EOF)
361 return WEOF;
363 return _IO_WUNDERFLOW (fp);
367 wint_t
368 _IO_wfile_overflow (f, wch)
369 _IO_FILE *f;
370 wint_t wch;
372 if (f->_flags & _IO_NO_WRITES) /* SET ERROR */
374 f->_flags |= _IO_ERR_SEEN;
375 __set_errno (EBADF);
376 return WEOF;
378 /* If currently reading or no buffer allocated. */
379 if ((f->_flags & _IO_CURRENTLY_PUTTING) == 0)
381 /* Allocate a buffer if needed. */
382 if (f->_wide_data->_IO_write_base == 0)
384 INTUSE(_IO_wdoallocbuf) (f);
385 _IO_wsetg (f, f->_wide_data->_IO_buf_base,
386 f->_wide_data->_IO_buf_base, f->_wide_data->_IO_buf_base);
388 if (f->_IO_write_base == NULL)
390 INTUSE(_IO_doallocbuf) (f);
391 _IO_setg (f, f->_IO_buf_base, f->_IO_buf_base, f->_IO_buf_base);
394 else
396 /* Otherwise must be currently reading. If _IO_read_ptr
397 (and hence also _IO_read_end) is at the buffer end,
398 logically slide the buffer forwards one block (by setting
399 the read pointers to all point at the beginning of the
400 block). This makes room for subsequent output.
401 Otherwise, set the read pointers to _IO_read_end (leaving
402 that alone, so it can continue to correspond to the
403 external position). */
404 if (f->_wide_data->_IO_read_ptr == f->_wide_data->_IO_buf_end)
406 f->_IO_read_end = f->_IO_read_ptr = f->_IO_buf_base;
407 f->_wide_data->_IO_read_end = f->_wide_data->_IO_read_ptr =
408 f->_wide_data->_IO_buf_base;
411 f->_wide_data->_IO_write_ptr = f->_wide_data->_IO_read_ptr;
412 f->_wide_data->_IO_write_base = f->_wide_data->_IO_write_ptr;
413 f->_wide_data->_IO_write_end = f->_wide_data->_IO_buf_end;
414 f->_wide_data->_IO_read_base = f->_wide_data->_IO_read_ptr =
415 f->_wide_data->_IO_read_end;
417 f->_IO_write_ptr = f->_IO_read_ptr;
418 f->_IO_write_base = f->_IO_write_ptr;
419 f->_IO_write_end = f->_IO_buf_end;
420 f->_IO_read_base = f->_IO_read_ptr = f->_IO_read_end;
422 f->_flags |= _IO_CURRENTLY_PUTTING;
423 if (f->_flags & (_IO_LINE_BUF+_IO_UNBUFFERED))
424 f->_wide_data->_IO_write_end = f->_wide_data->_IO_write_ptr;
426 if (wch == WEOF)
427 return _IO_do_flush (f);
428 if (f->_wide_data->_IO_write_ptr == f->_wide_data->_IO_buf_end)
429 /* Buffer is really full */
430 if (_IO_do_flush (f) == EOF)
431 return WEOF;
432 *f->_wide_data->_IO_write_ptr++ = wch;
433 if ((f->_flags & _IO_UNBUFFERED)
434 || ((f->_flags & _IO_LINE_BUF) && wch == L'\n'))
435 if (_IO_do_flush (f) == EOF)
436 return WEOF;
437 return wch;
439 INTDEF(_IO_wfile_overflow)
441 wint_t
442 _IO_wfile_sync (fp)
443 _IO_FILE *fp;
445 _IO_ssize_t delta;
446 wint_t retval = 0;
448 /* char* ptr = cur_ptr(); */
449 if (fp->_wide_data->_IO_write_ptr > fp->_wide_data->_IO_write_base)
450 if (_IO_do_flush (fp))
451 return WEOF;
452 delta = fp->_wide_data->_IO_read_ptr - fp->_wide_data->_IO_read_end;
453 if (delta != 0)
455 /* We have to find out how many bytes we have to go back in the
456 external buffer. */
457 struct _IO_codecvt *cv = fp->_codecvt;
458 _IO_off64_t new_pos;
460 int clen = (*cv->__codecvt_do_encoding) (cv);
462 if (clen > 0)
463 /* It is easy, a fixed number of input bytes are used for each
464 wide character. */
465 delta *= clen;
466 else
468 /* We have to find out the hard way how much to back off.
469 To do this we determine how much input we needed to
470 generate the wide characters up to the current reading
471 position. */
472 int nread;
474 fp->_wide_data->_IO_state = fp->_wide_data->_IO_last_state;
475 nread = (*cv->__codecvt_do_length) (cv, &fp->_wide_data->_IO_state,
476 fp->_IO_read_base,
477 fp->_IO_read_end, delta);
478 fp->_IO_read_ptr = fp->_IO_read_base + nread;
479 delta = -(fp->_IO_read_end - fp->_IO_read_base - nread);
482 new_pos = _IO_SYSSEEK (fp, delta, 1);
483 if (new_pos != (_IO_off64_t) EOF)
485 fp->_wide_data->_IO_read_end = fp->_wide_data->_IO_read_ptr;
486 fp->_IO_read_end = fp->_IO_read_ptr;
488 #ifdef ESPIPE
489 else if (errno == ESPIPE)
490 ; /* Ignore error from unseekable devices. */
491 #endif
492 else
493 retval = WEOF;
495 if (retval != WEOF)
496 fp->_offset = _IO_pos_BAD;
497 /* FIXME: Cleanup - can this be shared? */
498 /* setg(base(), ptr, ptr); */
499 return retval;
501 INTDEF(_IO_wfile_sync)
503 _IO_off64_t
504 _IO_wfile_seekoff (fp, offset, dir, mode)
505 _IO_FILE *fp;
506 _IO_off64_t offset;
507 int dir;
508 int mode;
510 _IO_off64_t result;
511 _IO_off64_t delta, new_offset;
512 long int count;
513 /* POSIX.1 8.2.3.7 says that after a call the fflush() the file
514 offset of the underlying file must be exact. */
515 int must_be_exact = ((fp->_wide_data->_IO_read_base
516 == fp->_wide_data->_IO_read_end)
517 && (fp->_wide_data->_IO_write_base
518 == fp->_wide_data->_IO_write_ptr));
520 if (mode == 0)
522 /* XXX For wide stream with backup store it is not very
523 reasonable to determine the offset. The pushed-back
524 character might require a state change and we need not be
525 able to compute the initial state by reverse transformation
526 since there is no guarantee of symmetry. So we don't even
527 try and return an error. */
528 if (_IO_in_backup (fp))
530 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
532 __set_errno (EINVAL);
533 return -1;
536 /* There is no more data in the backup buffer. We can
537 switch back. */
538 INTUSE(_IO_switch_to_main_wget_area) (fp);
541 dir = _IO_seek_cur, offset = 0; /* Don't move any pointers. */
544 /* Flush unwritten characters.
545 (This may do an unneeded write if we seek within the buffer.
546 But to be able to switch to reading, we would need to set
547 egptr to ptr. That can't be done in the current design,
548 which assumes file_ptr() is eGptr. Anyway, since we probably
549 end up flushing when we close(), it doesn't make much difference.)
550 FIXME: simulate mem-mapped files. */
552 if (fp->_wide_data->_IO_write_ptr > fp->_wide_data->_IO_write_base
553 || _IO_in_put_mode (fp))
554 if (INTUSE(_IO_switch_to_wget_mode) (fp))
555 return WEOF;
557 if (fp->_wide_data->_IO_buf_base == NULL)
559 /* It could be that we already have a pushback buffer. */
560 if (fp->_wide_data->_IO_read_base != NULL)
562 free (fp->_wide_data->_IO_read_base);
563 fp->_flags &= ~_IO_IN_BACKUP;
565 INTUSE(_IO_doallocbuf) (fp);
566 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
567 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
568 _IO_wsetp (fp, fp->_wide_data->_IO_buf_base,
569 fp->_wide_data->_IO_buf_base);
570 _IO_wsetg (fp, fp->_wide_data->_IO_buf_base,
571 fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base);
574 switch (dir)
576 struct _IO_codecvt *cv;
577 int clen;
579 case _IO_seek_cur:
580 /* Adjust for read-ahead (bytes is buffer). To do this we must
581 find out which position in the external buffer corresponds to
582 the current position in the internal buffer. */
583 cv = fp->_codecvt;
584 clen = (*cv->__codecvt_do_encoding) (cv);
586 if (clen > 0)
587 offset -= (fp->_wide_data->_IO_read_end
588 - fp->_wide_data->_IO_read_ptr) * clen;
589 else
591 int nread;
593 delta = fp->_wide_data->_IO_read_ptr - fp->_wide_data->_IO_read_base;
594 fp->_wide_data->_IO_state = fp->_wide_data->_IO_last_state;
595 nread = (*cv->__codecvt_do_length) (cv, &fp->_wide_data->_IO_state,
596 fp->_IO_read_base,
597 fp->_IO_read_end, delta);
598 fp->_IO_read_ptr = fp->_IO_read_base + nread;
599 fp->_wide_data->_IO_read_end = fp->_wide_data->_IO_read_ptr;
600 offset -= fp->_IO_read_end - fp->_IO_read_base - nread;
603 if (fp->_offset == _IO_pos_BAD)
604 goto dumb;
605 /* Make offset absolute, assuming current pointer is file_ptr(). */
606 offset += fp->_offset;
608 dir = _IO_seek_set;
609 break;
610 case _IO_seek_set:
611 break;
612 case _IO_seek_end:
614 struct _G_stat64 st;
615 if (_IO_SYSSTAT (fp, &st) == 0 && S_ISREG (st.st_mode))
617 offset += st.st_size;
618 dir = _IO_seek_set;
620 else
621 goto dumb;
624 /* At this point, dir==_IO_seek_set. */
626 /* If we are only interested in the current position we've found it now. */
627 if (mode == 0)
628 return offset;
630 /* If destination is within current buffer, optimize: */
631 if (fp->_offset != _IO_pos_BAD && fp->_IO_read_base != NULL
632 && !_IO_in_backup (fp))
634 /* Offset relative to start of main get area. */
635 _IO_off64_t rel_offset = (offset - fp->_offset
636 + (fp->_IO_read_end - fp->_IO_read_base));
637 if (rel_offset >= 0)
639 #if 0
640 if (_IO_in_backup (fp))
641 _IO_switch_to_main_get_area (fp);
642 #endif
643 if (rel_offset <= fp->_IO_read_end - fp->_IO_read_base)
645 enum __codecvt_result status;
646 struct _IO_codecvt *cd = fp->_codecvt;
647 const char *read_ptr_copy;
649 fp->_IO_read_ptr = fp->_IO_read_base + rel_offset;
650 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
652 /* Now set the pointer for the internal buffer. This
653 might be an iterative process. Though the read
654 pointer is somewhere in the current external buffer
655 this does not mean we can convert this whole buffer
656 at once fitting in the internal buffer. */
657 fp->_wide_data->_IO_state = fp->_wide_data->_IO_last_state;
658 read_ptr_copy = fp->_IO_read_base;
659 fp->_wide_data->_IO_read_ptr = fp->_wide_data->_IO_read_base;
662 wchar_t buffer[1024];
663 wchar_t *ignore;
664 status = (*cd->__codecvt_do_in) (cd,
665 &fp->_wide_data->_IO_state,
666 read_ptr_copy,
667 fp->_IO_read_ptr,
668 &read_ptr_copy,
669 buffer,
670 buffer
671 + (sizeof (buffer)
672 / sizeof (buffer[0])),
673 &ignore);
674 if (status != __codecvt_ok && status != __codecvt_partial)
676 fp->_flags |= _IO_ERR_SEEN;
677 goto dumb;
680 while (read_ptr_copy != fp->_IO_read_ptr);
682 fp->_wide_data->_IO_read_ptr = fp->_wide_data->_IO_read_base;
684 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
685 goto resync;
687 #ifdef TODO
688 /* If we have streammarkers, seek forward by reading ahead. */
689 if (_IO_have_markers (fp))
691 int to_skip = rel_offset
692 - (fp->_IO_read_ptr - fp->_IO_read_base);
693 if (ignore (to_skip) != to_skip)
694 goto dumb;
695 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
696 goto resync;
698 #endif
700 #ifdef TODO
701 if (rel_offset < 0 && rel_offset >= Bbase () - Bptr ())
703 if (!_IO_in_backup (fp))
704 _IO_switch_to_backup_area (fp);
705 gbump (fp->_IO_read_end + rel_offset - fp->_IO_read_ptr);
706 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
707 goto resync;
709 #endif
712 #ifdef TODO
713 INTUSE(_IO_unsave_markers) (fp);
714 #endif
716 if (fp->_flags & _IO_NO_READS)
717 goto dumb;
719 /* Try to seek to a block boundary, to improve kernel page management. */
720 new_offset = offset & ~(fp->_IO_buf_end - fp->_IO_buf_base - 1);
721 delta = offset - new_offset;
722 if (delta > fp->_IO_buf_end - fp->_IO_buf_base)
724 new_offset = offset;
725 delta = 0;
727 result = _IO_SYSSEEK (fp, new_offset, 0);
728 if (result < 0)
729 return EOF;
730 if (delta == 0)
731 count = 0;
732 else
734 count = _IO_SYSREAD (fp, fp->_IO_buf_base,
735 (must_be_exact
736 ? delta : fp->_IO_buf_end - fp->_IO_buf_base));
737 if (count < delta)
739 /* We weren't allowed to read, but try to seek the remainder. */
740 offset = count == EOF ? delta : delta-count;
741 dir = _IO_seek_cur;
742 goto dumb;
745 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base + delta,
746 fp->_IO_buf_base + count);
747 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
748 fp->_offset = result + count;
749 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
750 return offset;
751 dumb:
753 INTUSE(_IO_unsave_markers) (fp);
754 result = _IO_SYSSEEK (fp, offset, dir);
755 if (result != EOF)
757 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
758 fp->_offset = result;
759 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
760 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
761 _IO_wsetg (fp, fp->_wide_data->_IO_buf_base,
762 fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base);
763 _IO_wsetp (fp, fp->_wide_data->_IO_buf_base,
764 fp->_wide_data->_IO_buf_base);
766 return result;
768 resync:
769 /* We need to do it since it is possible that the file offset in
770 the kernel may be changed behind our back. It may happen when
771 we fopen a file and then do a fork. One process may access the
772 the file and the kernel file offset will be changed. */
773 if (fp->_offset >= 0)
774 _IO_SYSSEEK (fp, fp->_offset, 0);
776 return offset;
778 INTDEF(_IO_wfile_seekoff)
781 _IO_size_t
782 _IO_wfile_xsputn (f, data, n)
783 _IO_FILE *f;
784 const void *data;
785 _IO_size_t n;
787 register const wchar_t *s = (const wchar_t *) data;
788 _IO_size_t to_do = n;
789 int must_flush = 0;
790 _IO_size_t count;
792 if (n <= 0)
793 return 0;
794 /* This is an optimized implementation.
795 If the amount to be written straddles a block boundary
796 (or the filebuf is unbuffered), use sys_write directly. */
798 /* First figure out how much space is available in the buffer. */
799 count = f->_wide_data->_IO_write_end - f->_wide_data->_IO_write_ptr;
800 if ((f->_flags & _IO_LINE_BUF) && (f->_flags & _IO_CURRENTLY_PUTTING))
802 count = f->_wide_data->_IO_buf_end - f->_wide_data->_IO_write_ptr;
803 if (count >= n)
805 register const wchar_t *p;
806 for (p = s + n; p > s; )
808 if (*--p == L'\n')
810 count = p - s + 1;
811 must_flush = 1;
812 break;
817 /* Then fill the buffer. */
818 if (count > 0)
820 if (count > to_do)
821 count = to_do;
822 if (count > 20)
824 #ifdef _LIBC
825 f->_wide_data->_IO_write_ptr =
826 __wmempcpy (f->_wide_data->_IO_write_ptr, s, count);
827 #else
828 wmemcpy (f->_wide_data->_IO_write_ptr, s, count);
829 f->_wide_data->_IO_write_ptr += count;
830 #endif
831 s += count;
833 else
835 register wchar_t *p = f->_wide_data->_IO_write_ptr;
836 register int i = (int) count;
837 while (--i >= 0)
838 *p++ = *s++;
839 f->_wide_data->_IO_write_ptr = p;
841 to_do -= count;
843 if (to_do > 0)
844 to_do -= INTUSE(_IO_wdefault_xsputn) (f, s, to_do);
845 if (must_flush
846 && f->_wide_data->_IO_write_ptr != f->_wide_data->_IO_write_base)
847 INTUSE(_IO_wdo_write) (f, f->_wide_data->_IO_write_base,
848 f->_wide_data->_IO_write_ptr
849 - f->_wide_data->_IO_write_base);
851 return n - to_do;
853 INTDEF(_IO_wfile_xsputn)
856 struct _IO_jump_t _IO_wfile_jumps =
858 JUMP_INIT_DUMMY,
859 JUMP_INIT(finish, _IO_new_file_finish),
860 JUMP_INIT(overflow, (_IO_overflow_t) INTUSE(_IO_wfile_overflow)),
861 JUMP_INIT(underflow, (_IO_underflow_t) INTUSE(_IO_wfile_underflow)),
862 JUMP_INIT(uflow, (_IO_underflow_t) INTUSE(_IO_wdefault_uflow)),
863 JUMP_INIT(pbackfail, (_IO_pbackfail_t) INTUSE(_IO_wdefault_pbackfail)),
864 JUMP_INIT(xsputn, INTUSE(_IO_wfile_xsputn)),
865 JUMP_INIT(xsgetn, INTUSE(_IO_file_xsgetn)),
866 JUMP_INIT(seekoff, INTUSE(_IO_wfile_seekoff)),
867 JUMP_INIT(seekpos, _IO_default_seekpos),
868 JUMP_INIT(setbuf, _IO_new_file_setbuf),
869 JUMP_INIT(sync, (_IO_sync_t) INTUSE(_IO_wfile_sync)),
870 JUMP_INIT(doallocate, _IO_wfile_doallocate),
871 JUMP_INIT(read, INTUSE(_IO_file_read)),
872 JUMP_INIT(write, _IO_new_file_write),
873 JUMP_INIT(seek, INTUSE(_IO_file_seek)),
874 JUMP_INIT(close, INTUSE(_IO_file_close)),
875 JUMP_INIT(stat, INTUSE(_IO_file_stat)),
876 JUMP_INIT(showmanyc, _IO_default_showmanyc),
877 JUMP_INIT(imbue, _IO_default_imbue)
879 libc_hidden_data_def (_IO_wfile_jumps)
882 struct _IO_jump_t _IO_wfile_jumps_mmap =
884 JUMP_INIT_DUMMY,
885 JUMP_INIT(finish, _IO_new_file_finish),
886 JUMP_INIT(overflow, (_IO_overflow_t) INTUSE(_IO_wfile_overflow)),
887 JUMP_INIT(underflow, (_IO_underflow_t) _IO_wfile_underflow_mmap),
888 JUMP_INIT(uflow, (_IO_underflow_t) INTUSE(_IO_wdefault_uflow)),
889 JUMP_INIT(pbackfail, (_IO_pbackfail_t) INTUSE(_IO_wdefault_pbackfail)),
890 JUMP_INIT(xsputn, INTUSE(_IO_wfile_xsputn)),
891 JUMP_INIT(xsgetn, INTUSE(_IO_file_xsgetn)),
892 JUMP_INIT(seekoff, INTUSE(_IO_wfile_seekoff)),
893 JUMP_INIT(seekpos, _IO_default_seekpos),
894 JUMP_INIT(setbuf, _IO_file_setbuf_mmap),
895 JUMP_INIT(sync, (_IO_sync_t) INTUSE(_IO_wfile_sync)),
896 JUMP_INIT(doallocate, _IO_wfile_doallocate),
897 JUMP_INIT(read, INTUSE(_IO_file_read)),
898 JUMP_INIT(write, _IO_new_file_write),
899 JUMP_INIT(seek, INTUSE(_IO_file_seek)),
900 JUMP_INIT(close, _IO_file_close_mmap),
901 JUMP_INIT(stat, INTUSE(_IO_file_stat)),
902 JUMP_INIT(showmanyc, _IO_default_showmanyc),
903 JUMP_INIT(imbue, _IO_default_imbue)
906 struct _IO_jump_t _IO_wfile_jumps_maybe_mmap =
908 JUMP_INIT_DUMMY,
909 JUMP_INIT(finish, _IO_new_file_finish),
910 JUMP_INIT(overflow, (_IO_overflow_t) INTUSE(_IO_wfile_overflow)),
911 JUMP_INIT(underflow, (_IO_underflow_t) _IO_wfile_underflow_maybe_mmap),
912 JUMP_INIT(uflow, (_IO_underflow_t) INTUSE(_IO_wdefault_uflow)),
913 JUMP_INIT(pbackfail, (_IO_pbackfail_t) INTUSE(_IO_wdefault_pbackfail)),
914 JUMP_INIT(xsputn, INTUSE(_IO_wfile_xsputn)),
915 JUMP_INIT(xsgetn, INTUSE(_IO_file_xsgetn)),
916 JUMP_INIT(seekoff, INTUSE(_IO_wfile_seekoff)),
917 JUMP_INIT(seekpos, _IO_default_seekpos),
918 JUMP_INIT(setbuf, _IO_file_setbuf_mmap),
919 JUMP_INIT(sync, (_IO_sync_t) INTUSE(_IO_wfile_sync)),
920 JUMP_INIT(doallocate, _IO_wfile_doallocate),
921 JUMP_INIT(read, INTUSE(_IO_file_read)),
922 JUMP_INIT(write, _IO_new_file_write),
923 JUMP_INIT(seek, INTUSE(_IO_file_seek)),
924 JUMP_INIT(close, INTUSE(_IO_file_close)),
925 JUMP_INIT(stat, INTUSE(_IO_file_stat)),
926 JUMP_INIT(showmanyc, _IO_default_showmanyc),
927 JUMP_INIT(imbue, _IO_default_imbue)