Don't premature stop parsing mode string of fopen.
[glibc.git] / libio / wfileops.c
blob57ed786a8f2eb9405d9e248adb04aa9ac33653e7
1 /* Copyright (C) 1993,1995,1997-2004,2006,2009 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 WEOF;
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;
127 if (__builtin_expect (fp->_flags & _IO_NO_READS, 0))
129 fp->_flags |= _IO_ERR_SEEN;
130 __set_errno (EBADF);
131 return WEOF;
133 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
134 return *fp->_wide_data->_IO_read_ptr;
136 cd = fp->_codecvt;
138 /* Maybe there is something left in the external buffer. */
139 if (fp->_IO_read_ptr < fp->_IO_read_end)
141 /* There is more in the external. Convert it. */
142 const char *read_stop = (const char *) fp->_IO_read_ptr;
144 fp->_wide_data->_IO_last_state = fp->_wide_data->_IO_state;
145 fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_read_ptr =
146 fp->_wide_data->_IO_buf_base;
147 status = (*cd->__codecvt_do_in) (cd, &fp->_wide_data->_IO_state,
148 fp->_IO_read_ptr, fp->_IO_read_end,
149 &read_stop,
150 fp->_wide_data->_IO_read_ptr,
151 fp->_wide_data->_IO_buf_end,
152 &fp->_wide_data->_IO_read_end);
154 fp->_IO_read_base = fp->_IO_read_ptr;
155 fp->_IO_read_ptr = (char *) read_stop;
157 /* If we managed to generate some text return the next character. */
158 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
159 return *fp->_wide_data->_IO_read_ptr;
161 if (status == __codecvt_error)
163 __set_errno (EILSEQ);
164 fp->_flags |= _IO_ERR_SEEN;
165 return WEOF;
168 /* Move the remaining content of the read buffer to the beginning. */
169 memmove (fp->_IO_buf_base, fp->_IO_read_ptr,
170 fp->_IO_read_end - fp->_IO_read_ptr);
171 fp->_IO_read_end = (fp->_IO_buf_base
172 + (fp->_IO_read_end - fp->_IO_read_ptr));
173 fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_buf_base;
175 else
176 fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_read_end =
177 fp->_IO_buf_base;
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;
187 INTUSE(_IO_doallocbuf) (fp);
189 fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_read_end =
190 fp->_IO_buf_base;
193 fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end =
194 fp->_IO_buf_base;
196 if (fp->_wide_data->_IO_buf_base == NULL)
198 /* Maybe we already have a push back pointer. */
199 if (fp->_wide_data->_IO_save_base != NULL)
201 free (fp->_wide_data->_IO_save_base);
202 fp->_flags &= ~_IO_IN_BACKUP;
204 INTUSE(_IO_wdoallocbuf) (fp);
207 /* Flush all line buffered files before reading. */
208 /* FIXME This can/should be moved to genops ?? */
209 if (fp->_flags & (_IO_LINE_BUF|_IO_UNBUFFERED))
211 #if 0
212 INTUSE(_IO_flush_all_linebuffered) ();
213 #else
214 /* We used to flush all line-buffered stream. This really isn't
215 required by any standard. My recollection is that
216 traditional Unix systems did this for stdout. stderr better
217 not be line buffered. So we do just that here
218 explicitly. --drepper */
219 _IO_acquire_lock (_IO_stdout);
221 if ((_IO_stdout->_flags & (_IO_LINKED | _IO_NO_WRITES | _IO_LINE_BUF))
222 == (_IO_LINKED | _IO_LINE_BUF))
223 _IO_OVERFLOW (_IO_stdout, EOF);
225 _IO_release_lock (_IO_stdout);
226 #endif
229 INTUSE(_IO_switch_to_get_mode) (fp);
231 fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_read_ptr =
232 fp->_wide_data->_IO_buf_base;
233 fp->_wide_data->_IO_read_end = fp->_wide_data->_IO_buf_base;
234 fp->_wide_data->_IO_write_base = fp->_wide_data->_IO_write_ptr =
235 fp->_wide_data->_IO_write_end = fp->_wide_data->_IO_buf_base;
237 const char *read_ptr_copy;
238 char accbuf[MB_LEN_MAX];
239 size_t naccbuf = 0;
240 again:
241 count = _IO_SYSREAD (fp, fp->_IO_read_end,
242 fp->_IO_buf_end - fp->_IO_read_end);
243 if (count <= 0)
245 if (count == 0 && naccbuf == 0)
246 fp->_flags |= _IO_EOF_SEEN;
247 else
248 fp->_flags |= _IO_ERR_SEEN, count = 0;
250 fp->_IO_read_end += count;
251 if (count == 0)
253 if (naccbuf != 0)
254 /* There are some bytes in the external buffer but they don't
255 convert to anything. */
256 __set_errno (EILSEQ);
257 return WEOF;
259 if (fp->_offset != _IO_pos_BAD)
260 _IO_pos_adjust (fp->_offset, count);
262 /* Now convert the read input. */
263 fp->_wide_data->_IO_last_state = fp->_wide_data->_IO_state;
264 fp->_IO_read_base = fp->_IO_read_ptr;
265 const char *from = fp->_IO_read_ptr;
266 const char *to = fp->_IO_read_end;
267 size_t to_copy = count;
268 if (__builtin_expect (naccbuf != 0, 0))
270 to_copy = MIN (sizeof (accbuf) - naccbuf, count);
271 to = __mempcpy (&accbuf[naccbuf], from, to_copy);
272 naccbuf += to_copy;
273 from = accbuf;
275 status = (*cd->__codecvt_do_in) (cd, &fp->_wide_data->_IO_state,
276 from, to, &read_ptr_copy,
277 fp->_wide_data->_IO_read_end,
278 fp->_wide_data->_IO_buf_end,
279 &fp->_wide_data->_IO_read_end);
281 if (__builtin_expect (naccbuf != 0, 0))
282 fp->_IO_read_ptr += MAX (0, read_ptr_copy - &accbuf[naccbuf - to_copy]);
283 else
284 fp->_IO_read_ptr = (char *) read_ptr_copy;
285 if (fp->_wide_data->_IO_read_end == fp->_wide_data->_IO_buf_base)
287 if (status == __codecvt_error)
289 out_eilseq:
290 __set_errno (EILSEQ);
291 fp->_flags |= _IO_ERR_SEEN;
292 return WEOF;
295 /* The read bytes make no complete character. Try reading again. */
296 assert (status == __codecvt_partial);
298 if (naccbuf == 0)
300 if (fp->_IO_read_base < fp->_IO_read_ptr)
302 /* Partially used the buffer for some input data that
303 produces no output. */
304 size_t avail = fp->_IO_read_end - fp->_IO_read_ptr;
305 memmove (fp->_IO_read_base, fp->_IO_read_ptr, avail);
306 fp->_IO_read_ptr = fp->_IO_read_base;
307 fp->_IO_read_end -= avail;
308 goto again;
310 naccbuf = fp->_IO_read_end - fp->_IO_read_ptr;
311 if (naccbuf >= sizeof (accbuf))
312 goto out_eilseq;
314 memcpy (accbuf, fp->_IO_read_ptr, naccbuf);
316 else
318 size_t used = read_ptr_copy - accbuf;
319 if (used > 0)
321 memmove (accbuf, read_ptr_copy, naccbuf - used);
322 naccbuf -= used;
325 if (naccbuf == sizeof (accbuf))
326 goto out_eilseq;
329 fp->_IO_read_ptr = fp->_IO_read_end = fp->_IO_read_base;
331 goto again;
334 return *fp->_wide_data->_IO_read_ptr;
336 INTDEF(_IO_wfile_underflow)
339 static wint_t
340 _IO_wfile_underflow_mmap (_IO_FILE *fp)
342 struct _IO_codecvt *cd;
343 enum __codecvt_result status;
344 const char *read_stop;
346 if (__builtin_expect (fp->_flags & _IO_NO_READS, 0))
348 fp->_flags |= _IO_ERR_SEEN;
349 __set_errno (EBADF);
350 return WEOF;
352 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
353 return *fp->_wide_data->_IO_read_ptr;
355 cd = fp->_codecvt;
357 /* Maybe there is something left in the external buffer. */
358 if (fp->_IO_read_ptr >= fp->_IO_read_end
359 /* No. But maybe the read buffer is not fully set up. */
360 && _IO_file_underflow_mmap (fp) == EOF)
361 /* Nothing available. _IO_file_underflow_mmap has set the EOF or error
362 flags as appropriate. */
363 return WEOF;
365 /* There is more in the external. Convert it. */
366 read_stop = (const char *) fp->_IO_read_ptr;
368 if (fp->_wide_data->_IO_buf_base == NULL)
370 /* Maybe we already have a push back pointer. */
371 if (fp->_wide_data->_IO_save_base != NULL)
373 free (fp->_wide_data->_IO_save_base);
374 fp->_flags &= ~_IO_IN_BACKUP;
376 INTUSE(_IO_wdoallocbuf) (fp);
379 fp->_wide_data->_IO_last_state = fp->_wide_data->_IO_state;
380 fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_read_ptr =
381 fp->_wide_data->_IO_buf_base;
382 status = (*cd->__codecvt_do_in) (cd, &fp->_wide_data->_IO_state,
383 fp->_IO_read_ptr, fp->_IO_read_end,
384 &read_stop,
385 fp->_wide_data->_IO_read_ptr,
386 fp->_wide_data->_IO_buf_end,
387 &fp->_wide_data->_IO_read_end);
389 fp->_IO_read_ptr = (char *) read_stop;
391 /* If we managed to generate some text return the next character. */
392 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
393 return *fp->_wide_data->_IO_read_ptr;
395 /* There is some garbage at the end of the file. */
396 __set_errno (EILSEQ);
397 fp->_flags |= _IO_ERR_SEEN;
398 return WEOF;
401 static wint_t
402 _IO_wfile_underflow_maybe_mmap (_IO_FILE *fp)
404 /* This is the first read attempt. Doing the underflow will choose mmap
405 or vanilla operations and then punt to the chosen underflow routine.
406 Then we can punt to ours. */
407 if (_IO_file_underflow_maybe_mmap (fp) == EOF)
408 return WEOF;
410 return _IO_WUNDERFLOW (fp);
414 wint_t
415 _IO_wfile_overflow (f, wch)
416 _IO_FILE *f;
417 wint_t wch;
419 if (f->_flags & _IO_NO_WRITES) /* SET ERROR */
421 f->_flags |= _IO_ERR_SEEN;
422 __set_errno (EBADF);
423 return WEOF;
425 /* If currently reading or no buffer allocated. */
426 if ((f->_flags & _IO_CURRENTLY_PUTTING) == 0)
428 /* Allocate a buffer if needed. */
429 if (f->_wide_data->_IO_write_base == 0)
431 INTUSE(_IO_wdoallocbuf) (f);
432 _IO_wsetg (f, f->_wide_data->_IO_buf_base,
433 f->_wide_data->_IO_buf_base, f->_wide_data->_IO_buf_base);
435 if (f->_IO_write_base == NULL)
437 INTUSE(_IO_doallocbuf) (f);
438 _IO_setg (f, f->_IO_buf_base, f->_IO_buf_base, f->_IO_buf_base);
441 else
443 /* Otherwise must be currently reading. If _IO_read_ptr
444 (and hence also _IO_read_end) is at the buffer end,
445 logically slide the buffer forwards one block (by setting
446 the read pointers to all point at the beginning of the
447 block). This makes room for subsequent output.
448 Otherwise, set the read pointers to _IO_read_end (leaving
449 that alone, so it can continue to correspond to the
450 external position). */
451 if (f->_wide_data->_IO_read_ptr == f->_wide_data->_IO_buf_end)
453 f->_IO_read_end = f->_IO_read_ptr = f->_IO_buf_base;
454 f->_wide_data->_IO_read_end = f->_wide_data->_IO_read_ptr =
455 f->_wide_data->_IO_buf_base;
458 f->_wide_data->_IO_write_ptr = f->_wide_data->_IO_read_ptr;
459 f->_wide_data->_IO_write_base = f->_wide_data->_IO_write_ptr;
460 f->_wide_data->_IO_write_end = f->_wide_data->_IO_buf_end;
461 f->_wide_data->_IO_read_base = f->_wide_data->_IO_read_ptr =
462 f->_wide_data->_IO_read_end;
464 f->_IO_write_ptr = f->_IO_read_ptr;
465 f->_IO_write_base = f->_IO_write_ptr;
466 f->_IO_write_end = f->_IO_buf_end;
467 f->_IO_read_base = f->_IO_read_ptr = f->_IO_read_end;
469 f->_flags |= _IO_CURRENTLY_PUTTING;
470 if (f->_flags & (_IO_LINE_BUF+_IO_UNBUFFERED))
471 f->_wide_data->_IO_write_end = f->_wide_data->_IO_write_ptr;
473 if (wch == WEOF)
474 return _IO_do_flush (f);
475 if (f->_wide_data->_IO_write_ptr == f->_wide_data->_IO_buf_end)
476 /* Buffer is really full */
477 if (_IO_do_flush (f) == EOF)
478 return WEOF;
479 *f->_wide_data->_IO_write_ptr++ = wch;
480 if ((f->_flags & _IO_UNBUFFERED)
481 || ((f->_flags & _IO_LINE_BUF) && wch == L'\n'))
482 if (_IO_do_flush (f) == EOF)
483 return WEOF;
484 return wch;
486 INTDEF(_IO_wfile_overflow)
488 wint_t
489 _IO_wfile_sync (fp)
490 _IO_FILE *fp;
492 _IO_ssize_t delta;
493 wint_t retval = 0;
495 /* char* ptr = cur_ptr(); */
496 if (fp->_wide_data->_IO_write_ptr > fp->_wide_data->_IO_write_base)
497 if (_IO_do_flush (fp))
498 return WEOF;
499 delta = fp->_wide_data->_IO_read_ptr - fp->_wide_data->_IO_read_end;
500 if (delta != 0)
502 /* We have to find out how many bytes we have to go back in the
503 external buffer. */
504 struct _IO_codecvt *cv = fp->_codecvt;
505 _IO_off64_t new_pos;
507 int clen = (*cv->__codecvt_do_encoding) (cv);
509 if (clen > 0)
510 /* It is easy, a fixed number of input bytes are used for each
511 wide character. */
512 delta *= clen;
513 else
515 /* We have to find out the hard way how much to back off.
516 To do this we determine how much input we needed to
517 generate the wide characters up to the current reading
518 position. */
519 int nread;
521 fp->_wide_data->_IO_state = fp->_wide_data->_IO_last_state;
522 nread = (*cv->__codecvt_do_length) (cv, &fp->_wide_data->_IO_state,
523 fp->_IO_read_base,
524 fp->_IO_read_end, delta);
525 fp->_IO_read_ptr = fp->_IO_read_base + nread;
526 delta = -(fp->_IO_read_end - fp->_IO_read_base - nread);
529 new_pos = _IO_SYSSEEK (fp, delta, 1);
530 if (new_pos != (_IO_off64_t) EOF)
532 fp->_wide_data->_IO_read_end = fp->_wide_data->_IO_read_ptr;
533 fp->_IO_read_end = fp->_IO_read_ptr;
535 #ifdef ESPIPE
536 else if (errno == ESPIPE)
537 ; /* Ignore error from unseekable devices. */
538 #endif
539 else
540 retval = WEOF;
542 if (retval != WEOF)
543 fp->_offset = _IO_pos_BAD;
544 /* FIXME: Cleanup - can this be shared? */
545 /* setg(base(), ptr, ptr); */
546 return retval;
548 INTDEF(_IO_wfile_sync)
550 _IO_off64_t
551 _IO_wfile_seekoff (fp, offset, dir, mode)
552 _IO_FILE *fp;
553 _IO_off64_t offset;
554 int dir;
555 int mode;
557 _IO_off64_t result;
558 _IO_off64_t delta, new_offset;
559 long int count;
560 /* POSIX.1 8.2.3.7 says that after a call the fflush() the file
561 offset of the underlying file must be exact. */
562 int must_be_exact = ((fp->_wide_data->_IO_read_base
563 == fp->_wide_data->_IO_read_end)
564 && (fp->_wide_data->_IO_write_base
565 == fp->_wide_data->_IO_write_ptr));
567 if (mode == 0)
569 /* XXX For wide stream with backup store it is not very
570 reasonable to determine the offset. The pushed-back
571 character might require a state change and we need not be
572 able to compute the initial state by reverse transformation
573 since there is no guarantee of symmetry. So we don't even
574 try and return an error. */
575 if (_IO_in_backup (fp))
577 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
579 __set_errno (EINVAL);
580 return -1;
583 /* There is no more data in the backup buffer. We can
584 switch back. */
585 INTUSE(_IO_switch_to_main_wget_area) (fp);
588 dir = _IO_seek_cur, offset = 0; /* Don't move any pointers. */
591 /* Flush unwritten characters.
592 (This may do an unneeded write if we seek within the buffer.
593 But to be able to switch to reading, we would need to set
594 egptr to ptr. That can't be done in the current design,
595 which assumes file_ptr() is eGptr. Anyway, since we probably
596 end up flushing when we close(), it doesn't make much difference.)
597 FIXME: simulate mem-mapped files. */
599 if (fp->_wide_data->_IO_write_ptr > fp->_wide_data->_IO_write_base
600 || _IO_in_put_mode (fp))
601 if (INTUSE(_IO_switch_to_wget_mode) (fp))
602 return WEOF;
604 if (fp->_wide_data->_IO_buf_base == NULL)
606 /* It could be that we already have a pushback buffer. */
607 if (fp->_wide_data->_IO_read_base != NULL)
609 free (fp->_wide_data->_IO_read_base);
610 fp->_flags &= ~_IO_IN_BACKUP;
612 INTUSE(_IO_doallocbuf) (fp);
613 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
614 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
615 _IO_wsetp (fp, fp->_wide_data->_IO_buf_base,
616 fp->_wide_data->_IO_buf_base);
617 _IO_wsetg (fp, fp->_wide_data->_IO_buf_base,
618 fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base);
621 switch (dir)
623 struct _IO_codecvt *cv;
624 int clen;
626 case _IO_seek_cur:
627 /* Adjust for read-ahead (bytes is buffer). To do this we must
628 find out which position in the external buffer corresponds to
629 the current position in the internal buffer. */
630 cv = fp->_codecvt;
631 clen = (*cv->__codecvt_do_encoding) (cv);
633 if (clen > 0)
634 offset -= (fp->_wide_data->_IO_read_end
635 - fp->_wide_data->_IO_read_ptr) * clen;
636 else
638 int nread;
640 delta = fp->_wide_data->_IO_read_ptr - fp->_wide_data->_IO_read_base;
641 fp->_wide_data->_IO_state = fp->_wide_data->_IO_last_state;
642 nread = (*cv->__codecvt_do_length) (cv, &fp->_wide_data->_IO_state,
643 fp->_IO_read_base,
644 fp->_IO_read_end, delta);
645 fp->_IO_read_ptr = fp->_IO_read_base + nread;
646 fp->_wide_data->_IO_read_end = fp->_wide_data->_IO_read_ptr;
647 offset -= fp->_IO_read_end - fp->_IO_read_base - nread;
650 if (fp->_offset == _IO_pos_BAD)
651 goto dumb;
652 /* Make offset absolute, assuming current pointer is file_ptr(). */
653 offset += fp->_offset;
655 dir = _IO_seek_set;
656 break;
657 case _IO_seek_set:
658 break;
659 case _IO_seek_end:
661 struct _G_stat64 st;
662 if (_IO_SYSSTAT (fp, &st) == 0 && S_ISREG (st.st_mode))
664 offset += st.st_size;
665 dir = _IO_seek_set;
667 else
668 goto dumb;
671 /* At this point, dir==_IO_seek_set. */
673 /* If we are only interested in the current position we've found it now. */
674 if (mode == 0)
675 return offset;
677 /* If destination is within current buffer, optimize: */
678 if (fp->_offset != _IO_pos_BAD && fp->_IO_read_base != NULL
679 && !_IO_in_backup (fp))
681 /* Offset relative to start of main get area. */
682 _IO_off64_t rel_offset = (offset - fp->_offset
683 + (fp->_IO_read_end - fp->_IO_read_base));
684 if (rel_offset >= 0)
686 #if 0
687 if (_IO_in_backup (fp))
688 _IO_switch_to_main_get_area (fp);
689 #endif
690 if (rel_offset <= fp->_IO_read_end - fp->_IO_read_base)
692 enum __codecvt_result status;
693 struct _IO_codecvt *cd = fp->_codecvt;
694 const char *read_ptr_copy;
696 fp->_IO_read_ptr = fp->_IO_read_base + rel_offset;
697 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
699 /* Now set the pointer for the internal buffer. This
700 might be an iterative process. Though the read
701 pointer is somewhere in the current external buffer
702 this does not mean we can convert this whole buffer
703 at once fitting in the internal buffer. */
704 fp->_wide_data->_IO_state = fp->_wide_data->_IO_last_state;
705 read_ptr_copy = fp->_IO_read_base;
706 fp->_wide_data->_IO_read_ptr = fp->_wide_data->_IO_read_base;
709 wchar_t buffer[1024];
710 wchar_t *ignore;
711 status = (*cd->__codecvt_do_in) (cd,
712 &fp->_wide_data->_IO_state,
713 read_ptr_copy,
714 fp->_IO_read_ptr,
715 &read_ptr_copy,
716 buffer,
717 buffer
718 + (sizeof (buffer)
719 / sizeof (buffer[0])),
720 &ignore);
721 if (status != __codecvt_ok && status != __codecvt_partial)
723 fp->_flags |= _IO_ERR_SEEN;
724 goto dumb;
727 while (read_ptr_copy != fp->_IO_read_ptr);
729 fp->_wide_data->_IO_read_ptr = fp->_wide_data->_IO_read_base;
731 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
732 goto resync;
734 #ifdef TODO
735 /* If we have streammarkers, seek forward by reading ahead. */
736 if (_IO_have_markers (fp))
738 int to_skip = rel_offset
739 - (fp->_IO_read_ptr - fp->_IO_read_base);
740 if (ignore (to_skip) != to_skip)
741 goto dumb;
742 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
743 goto resync;
745 #endif
747 #ifdef TODO
748 if (rel_offset < 0 && rel_offset >= Bbase () - Bptr ())
750 if (!_IO_in_backup (fp))
751 _IO_switch_to_backup_area (fp);
752 gbump (fp->_IO_read_end + rel_offset - fp->_IO_read_ptr);
753 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
754 goto resync;
756 #endif
759 #ifdef TODO
760 INTUSE(_IO_unsave_markers) (fp);
761 #endif
763 if (fp->_flags & _IO_NO_READS)
764 goto dumb;
766 /* Try to seek to a block boundary, to improve kernel page management. */
767 new_offset = offset & ~(fp->_IO_buf_end - fp->_IO_buf_base - 1);
768 delta = offset - new_offset;
769 if (delta > fp->_IO_buf_end - fp->_IO_buf_base)
771 new_offset = offset;
772 delta = 0;
774 result = _IO_SYSSEEK (fp, new_offset, 0);
775 if (result < 0)
776 return EOF;
777 if (delta == 0)
778 count = 0;
779 else
781 count = _IO_SYSREAD (fp, fp->_IO_buf_base,
782 (must_be_exact
783 ? delta : fp->_IO_buf_end - fp->_IO_buf_base));
784 if (count < delta)
786 /* We weren't allowed to read, but try to seek the remainder. */
787 offset = count == EOF ? delta : delta-count;
788 dir = _IO_seek_cur;
789 goto dumb;
792 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base + delta,
793 fp->_IO_buf_base + count);
794 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
795 fp->_offset = result + count;
796 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
797 return offset;
798 dumb:
800 INTUSE(_IO_unsave_markers) (fp);
801 result = _IO_SYSSEEK (fp, offset, dir);
802 if (result != EOF)
804 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
805 fp->_offset = result;
806 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
807 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
808 _IO_wsetg (fp, fp->_wide_data->_IO_buf_base,
809 fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base);
810 _IO_wsetp (fp, fp->_wide_data->_IO_buf_base,
811 fp->_wide_data->_IO_buf_base);
813 return result;
815 resync:
816 /* We need to do it since it is possible that the file offset in
817 the kernel may be changed behind our back. It may happen when
818 we fopen a file and then do a fork. One process may access the
819 the file and the kernel file offset will be changed. */
820 if (fp->_offset >= 0)
821 _IO_SYSSEEK (fp, fp->_offset, 0);
823 return offset;
825 INTDEF(_IO_wfile_seekoff)
828 _IO_size_t
829 _IO_wfile_xsputn (f, data, n)
830 _IO_FILE *f;
831 const void *data;
832 _IO_size_t n;
834 register const wchar_t *s = (const wchar_t *) data;
835 _IO_size_t to_do = n;
836 int must_flush = 0;
837 _IO_size_t count;
839 if (n <= 0)
840 return 0;
841 /* This is an optimized implementation.
842 If the amount to be written straddles a block boundary
843 (or the filebuf is unbuffered), use sys_write directly. */
845 /* First figure out how much space is available in the buffer. */
846 count = f->_wide_data->_IO_write_end - f->_wide_data->_IO_write_ptr;
847 if ((f->_flags & _IO_LINE_BUF) && (f->_flags & _IO_CURRENTLY_PUTTING))
849 count = f->_wide_data->_IO_buf_end - f->_wide_data->_IO_write_ptr;
850 if (count >= n)
852 register const wchar_t *p;
853 for (p = s + n; p > s; )
855 if (*--p == L'\n')
857 count = p - s + 1;
858 must_flush = 1;
859 break;
864 /* Then fill the buffer. */
865 if (count > 0)
867 if (count > to_do)
868 count = to_do;
869 if (count > 20)
871 #ifdef _LIBC
872 f->_wide_data->_IO_write_ptr =
873 __wmempcpy (f->_wide_data->_IO_write_ptr, s, count);
874 #else
875 wmemcpy (f->_wide_data->_IO_write_ptr, s, count);
876 f->_wide_data->_IO_write_ptr += count;
877 #endif
878 s += count;
880 else
882 register wchar_t *p = f->_wide_data->_IO_write_ptr;
883 register int i = (int) count;
884 while (--i >= 0)
885 *p++ = *s++;
886 f->_wide_data->_IO_write_ptr = p;
888 to_do -= count;
890 if (to_do > 0)
891 to_do -= INTUSE(_IO_wdefault_xsputn) (f, s, to_do);
892 if (must_flush
893 && f->_wide_data->_IO_write_ptr != f->_wide_data->_IO_write_base)
894 INTUSE(_IO_wdo_write) (f, f->_wide_data->_IO_write_base,
895 f->_wide_data->_IO_write_ptr
896 - f->_wide_data->_IO_write_base);
898 return n - to_do;
900 INTDEF(_IO_wfile_xsputn)
903 const struct _IO_jump_t _IO_wfile_jumps =
905 JUMP_INIT_DUMMY,
906 JUMP_INIT(finish, _IO_new_file_finish),
907 JUMP_INIT(overflow, (_IO_overflow_t) INTUSE(_IO_wfile_overflow)),
908 JUMP_INIT(underflow, (_IO_underflow_t) INTUSE(_IO_wfile_underflow)),
909 JUMP_INIT(uflow, (_IO_underflow_t) INTUSE(_IO_wdefault_uflow)),
910 JUMP_INIT(pbackfail, (_IO_pbackfail_t) INTUSE(_IO_wdefault_pbackfail)),
911 JUMP_INIT(xsputn, INTUSE(_IO_wfile_xsputn)),
912 JUMP_INIT(xsgetn, INTUSE(_IO_file_xsgetn)),
913 JUMP_INIT(seekoff, INTUSE(_IO_wfile_seekoff)),
914 JUMP_INIT(seekpos, _IO_default_seekpos),
915 JUMP_INIT(setbuf, _IO_new_file_setbuf),
916 JUMP_INIT(sync, (_IO_sync_t) INTUSE(_IO_wfile_sync)),
917 JUMP_INIT(doallocate, _IO_wfile_doallocate),
918 JUMP_INIT(read, INTUSE(_IO_file_read)),
919 JUMP_INIT(write, _IO_new_file_write),
920 JUMP_INIT(seek, INTUSE(_IO_file_seek)),
921 JUMP_INIT(close, INTUSE(_IO_file_close)),
922 JUMP_INIT(stat, INTUSE(_IO_file_stat)),
923 JUMP_INIT(showmanyc, _IO_default_showmanyc),
924 JUMP_INIT(imbue, _IO_default_imbue)
926 libc_hidden_data_def (_IO_wfile_jumps)
929 const struct _IO_jump_t _IO_wfile_jumps_mmap =
931 JUMP_INIT_DUMMY,
932 JUMP_INIT(finish, _IO_new_file_finish),
933 JUMP_INIT(overflow, (_IO_overflow_t) INTUSE(_IO_wfile_overflow)),
934 JUMP_INIT(underflow, (_IO_underflow_t) _IO_wfile_underflow_mmap),
935 JUMP_INIT(uflow, (_IO_underflow_t) INTUSE(_IO_wdefault_uflow)),
936 JUMP_INIT(pbackfail, (_IO_pbackfail_t) INTUSE(_IO_wdefault_pbackfail)),
937 JUMP_INIT(xsputn, INTUSE(_IO_wfile_xsputn)),
938 JUMP_INIT(xsgetn, INTUSE(_IO_file_xsgetn)),
939 JUMP_INIT(seekoff, INTUSE(_IO_wfile_seekoff)),
940 JUMP_INIT(seekpos, _IO_default_seekpos),
941 JUMP_INIT(setbuf, _IO_file_setbuf_mmap),
942 JUMP_INIT(sync, (_IO_sync_t) INTUSE(_IO_wfile_sync)),
943 JUMP_INIT(doallocate, _IO_wfile_doallocate),
944 JUMP_INIT(read, INTUSE(_IO_file_read)),
945 JUMP_INIT(write, _IO_new_file_write),
946 JUMP_INIT(seek, INTUSE(_IO_file_seek)),
947 JUMP_INIT(close, _IO_file_close_mmap),
948 JUMP_INIT(stat, INTUSE(_IO_file_stat)),
949 JUMP_INIT(showmanyc, _IO_default_showmanyc),
950 JUMP_INIT(imbue, _IO_default_imbue)
953 const struct _IO_jump_t _IO_wfile_jumps_maybe_mmap =
955 JUMP_INIT_DUMMY,
956 JUMP_INIT(finish, _IO_new_file_finish),
957 JUMP_INIT(overflow, (_IO_overflow_t) INTUSE(_IO_wfile_overflow)),
958 JUMP_INIT(underflow, (_IO_underflow_t) _IO_wfile_underflow_maybe_mmap),
959 JUMP_INIT(uflow, (_IO_underflow_t) INTUSE(_IO_wdefault_uflow)),
960 JUMP_INIT(pbackfail, (_IO_pbackfail_t) INTUSE(_IO_wdefault_pbackfail)),
961 JUMP_INIT(xsputn, INTUSE(_IO_wfile_xsputn)),
962 JUMP_INIT(xsgetn, INTUSE(_IO_file_xsgetn)),
963 JUMP_INIT(seekoff, INTUSE(_IO_wfile_seekoff)),
964 JUMP_INIT(seekpos, _IO_default_seekpos),
965 JUMP_INIT(setbuf, _IO_file_setbuf_mmap),
966 JUMP_INIT(sync, (_IO_sync_t) INTUSE(_IO_wfile_sync)),
967 JUMP_INIT(doallocate, _IO_wfile_doallocate),
968 JUMP_INIT(read, INTUSE(_IO_file_read)),
969 JUMP_INIT(write, _IO_new_file_write),
970 JUMP_INIT(seek, INTUSE(_IO_file_seek)),
971 JUMP_INIT(close, INTUSE(_IO_file_close)),
972 JUMP_INIT(stat, INTUSE(_IO_file_stat)),
973 JUMP_INIT(showmanyc, _IO_default_showmanyc),
974 JUMP_INIT(imbue, _IO_default_imbue)