Remove __ELF__ conditionals
[glibc.git] / libio / wfileops.c
blob8f21168fb7a675fed22476f73eb356e3e2a5f70d
1 /* Copyright (C) 1993,1995,1997-2004,2006,2009,2011 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 const char *read_stop;
345 if (__builtin_expect (fp->_flags & _IO_NO_READS, 0))
347 fp->_flags |= _IO_ERR_SEEN;
348 __set_errno (EBADF);
349 return WEOF;
351 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
352 return *fp->_wide_data->_IO_read_ptr;
354 cd = fp->_codecvt;
356 /* Maybe there is something left in the external buffer. */
357 if (fp->_IO_read_ptr >= fp->_IO_read_end
358 /* No. But maybe the read buffer is not fully set up. */
359 && _IO_file_underflow_mmap (fp) == EOF)
360 /* Nothing available. _IO_file_underflow_mmap has set the EOF or error
361 flags as appropriate. */
362 return WEOF;
364 /* There is more in the external. Convert it. */
365 read_stop = (const char *) fp->_IO_read_ptr;
367 if (fp->_wide_data->_IO_buf_base == NULL)
369 /* Maybe we already have a push back pointer. */
370 if (fp->_wide_data->_IO_save_base != NULL)
372 free (fp->_wide_data->_IO_save_base);
373 fp->_flags &= ~_IO_IN_BACKUP;
375 INTUSE(_IO_wdoallocbuf) (fp);
378 fp->_wide_data->_IO_last_state = fp->_wide_data->_IO_state;
379 fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_read_ptr =
380 fp->_wide_data->_IO_buf_base;
381 (*cd->__codecvt_do_in) (cd, &fp->_wide_data->_IO_state,
382 fp->_IO_read_ptr, fp->_IO_read_end,
383 &read_stop,
384 fp->_wide_data->_IO_read_ptr,
385 fp->_wide_data->_IO_buf_end,
386 &fp->_wide_data->_IO_read_end);
388 fp->_IO_read_ptr = (char *) read_stop;
390 /* If we managed to generate some text return the next character. */
391 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
392 return *fp->_wide_data->_IO_read_ptr;
394 /* There is some garbage at the end of the file. */
395 __set_errno (EILSEQ);
396 fp->_flags |= _IO_ERR_SEEN;
397 return WEOF;
400 static wint_t
401 _IO_wfile_underflow_maybe_mmap (_IO_FILE *fp)
403 /* This is the first read attempt. Doing the underflow will choose mmap
404 or vanilla operations and then punt to the chosen underflow routine.
405 Then we can punt to ours. */
406 if (_IO_file_underflow_maybe_mmap (fp) == EOF)
407 return WEOF;
409 return _IO_WUNDERFLOW (fp);
413 wint_t
414 _IO_wfile_overflow (f, wch)
415 _IO_FILE *f;
416 wint_t wch;
418 if (f->_flags & _IO_NO_WRITES) /* SET ERROR */
420 f->_flags |= _IO_ERR_SEEN;
421 __set_errno (EBADF);
422 return WEOF;
424 /* If currently reading or no buffer allocated. */
425 if ((f->_flags & _IO_CURRENTLY_PUTTING) == 0)
427 /* Allocate a buffer if needed. */
428 if (f->_wide_data->_IO_write_base == 0)
430 INTUSE(_IO_wdoallocbuf) (f);
431 _IO_wsetg (f, f->_wide_data->_IO_buf_base,
432 f->_wide_data->_IO_buf_base, f->_wide_data->_IO_buf_base);
434 if (f->_IO_write_base == NULL)
436 INTUSE(_IO_doallocbuf) (f);
437 _IO_setg (f, f->_IO_buf_base, f->_IO_buf_base, f->_IO_buf_base);
440 else
442 /* Otherwise must be currently reading. If _IO_read_ptr
443 (and hence also _IO_read_end) is at the buffer end,
444 logically slide the buffer forwards one block (by setting
445 the read pointers to all point at the beginning of the
446 block). This makes room for subsequent output.
447 Otherwise, set the read pointers to _IO_read_end (leaving
448 that alone, so it can continue to correspond to the
449 external position). */
450 if (f->_wide_data->_IO_read_ptr == f->_wide_data->_IO_buf_end)
452 f->_IO_read_end = f->_IO_read_ptr = f->_IO_buf_base;
453 f->_wide_data->_IO_read_end = f->_wide_data->_IO_read_ptr =
454 f->_wide_data->_IO_buf_base;
457 f->_wide_data->_IO_write_ptr = f->_wide_data->_IO_read_ptr;
458 f->_wide_data->_IO_write_base = f->_wide_data->_IO_write_ptr;
459 f->_wide_data->_IO_write_end = f->_wide_data->_IO_buf_end;
460 f->_wide_data->_IO_read_base = f->_wide_data->_IO_read_ptr =
461 f->_wide_data->_IO_read_end;
463 f->_IO_write_ptr = f->_IO_read_ptr;
464 f->_IO_write_base = f->_IO_write_ptr;
465 f->_IO_write_end = f->_IO_buf_end;
466 f->_IO_read_base = f->_IO_read_ptr = f->_IO_read_end;
468 f->_flags |= _IO_CURRENTLY_PUTTING;
469 if (f->_flags & (_IO_LINE_BUF+_IO_UNBUFFERED))
470 f->_wide_data->_IO_write_end = f->_wide_data->_IO_write_ptr;
472 if (wch == WEOF)
473 return _IO_do_flush (f);
474 if (f->_wide_data->_IO_write_ptr == f->_wide_data->_IO_buf_end)
475 /* Buffer is really full */
476 if (_IO_do_flush (f) == EOF)
477 return WEOF;
478 *f->_wide_data->_IO_write_ptr++ = wch;
479 if ((f->_flags & _IO_UNBUFFERED)
480 || ((f->_flags & _IO_LINE_BUF) && wch == L'\n'))
481 if (_IO_do_flush (f) == EOF)
482 return WEOF;
483 return wch;
485 INTDEF(_IO_wfile_overflow)
487 wint_t
488 _IO_wfile_sync (fp)
489 _IO_FILE *fp;
491 _IO_ssize_t delta;
492 wint_t retval = 0;
494 /* char* ptr = cur_ptr(); */
495 if (fp->_wide_data->_IO_write_ptr > fp->_wide_data->_IO_write_base)
496 if (_IO_do_flush (fp))
497 return WEOF;
498 delta = fp->_wide_data->_IO_read_ptr - fp->_wide_data->_IO_read_end;
499 if (delta != 0)
501 /* We have to find out how many bytes we have to go back in the
502 external buffer. */
503 struct _IO_codecvt *cv = fp->_codecvt;
504 _IO_off64_t new_pos;
506 int clen = (*cv->__codecvt_do_encoding) (cv);
508 if (clen > 0)
509 /* It is easy, a fixed number of input bytes are used for each
510 wide character. */
511 delta *= clen;
512 else
514 /* We have to find out the hard way how much to back off.
515 To do this we determine how much input we needed to
516 generate the wide characters up to the current reading
517 position. */
518 int nread;
520 fp->_wide_data->_IO_state = fp->_wide_data->_IO_last_state;
521 nread = (*cv->__codecvt_do_length) (cv, &fp->_wide_data->_IO_state,
522 fp->_IO_read_base,
523 fp->_IO_read_end, delta);
524 fp->_IO_read_ptr = fp->_IO_read_base + nread;
525 delta = -(fp->_IO_read_end - fp->_IO_read_base - nread);
528 new_pos = _IO_SYSSEEK (fp, delta, 1);
529 if (new_pos != (_IO_off64_t) EOF)
531 fp->_wide_data->_IO_read_end = fp->_wide_data->_IO_read_ptr;
532 fp->_IO_read_end = fp->_IO_read_ptr;
534 #ifdef ESPIPE
535 else if (errno == ESPIPE)
536 ; /* Ignore error from unseekable devices. */
537 #endif
538 else
539 retval = WEOF;
541 if (retval != WEOF)
542 fp->_offset = _IO_pos_BAD;
543 /* FIXME: Cleanup - can this be shared? */
544 /* setg(base(), ptr, ptr); */
545 return retval;
547 INTDEF(_IO_wfile_sync)
549 _IO_off64_t
550 _IO_wfile_seekoff (fp, offset, dir, mode)
551 _IO_FILE *fp;
552 _IO_off64_t offset;
553 int dir;
554 int mode;
556 _IO_off64_t result;
557 _IO_off64_t delta, new_offset;
558 long int count;
559 /* POSIX.1 8.2.3.7 says that after a call the fflush() the file
560 offset of the underlying file must be exact. */
561 int must_be_exact = ((fp->_wide_data->_IO_read_base
562 == fp->_wide_data->_IO_read_end)
563 && (fp->_wide_data->_IO_write_base
564 == fp->_wide_data->_IO_write_ptr));
566 if (mode == 0)
568 /* XXX For wide stream with backup store it is not very
569 reasonable to determine the offset. The pushed-back
570 character might require a state change and we need not be
571 able to compute the initial state by reverse transformation
572 since there is no guarantee of symmetry. So we don't even
573 try and return an error. */
574 if (_IO_in_backup (fp))
576 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
578 __set_errno (EINVAL);
579 return -1;
582 /* There is no more data in the backup buffer. We can
583 switch back. */
584 INTUSE(_IO_switch_to_main_wget_area) (fp);
587 dir = _IO_seek_cur, offset = 0; /* Don't move any pointers. */
590 /* Flush unwritten characters.
591 (This may do an unneeded write if we seek within the buffer.
592 But to be able to switch to reading, we would need to set
593 egptr to ptr. That can't be done in the current design,
594 which assumes file_ptr() is eGptr. Anyway, since we probably
595 end up flushing when we close(), it doesn't make much difference.)
596 FIXME: simulate mem-mapped files. */
598 if (fp->_wide_data->_IO_write_ptr > fp->_wide_data->_IO_write_base
599 || _IO_in_put_mode (fp))
600 if (INTUSE(_IO_switch_to_wget_mode) (fp))
601 return WEOF;
603 if (fp->_wide_data->_IO_buf_base == NULL)
605 /* It could be that we already have a pushback buffer. */
606 if (fp->_wide_data->_IO_read_base != NULL)
608 free (fp->_wide_data->_IO_read_base);
609 fp->_flags &= ~_IO_IN_BACKUP;
611 INTUSE(_IO_doallocbuf) (fp);
612 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
613 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
614 _IO_wsetp (fp, fp->_wide_data->_IO_buf_base,
615 fp->_wide_data->_IO_buf_base);
616 _IO_wsetg (fp, fp->_wide_data->_IO_buf_base,
617 fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base);
620 switch (dir)
622 struct _IO_codecvt *cv;
623 int clen;
625 case _IO_seek_cur:
626 /* Adjust for read-ahead (bytes is buffer). To do this we must
627 find out which position in the external buffer corresponds to
628 the current position in the internal buffer. */
629 cv = fp->_codecvt;
630 clen = (*cv->__codecvt_do_encoding) (cv);
632 if (clen > 0)
634 offset -= (fp->_wide_data->_IO_read_end
635 - fp->_wide_data->_IO_read_ptr) * clen;
636 /* Adjust by readahead in external buffer. */
637 offset -= fp->_IO_read_end - fp->_IO_read_ptr;
639 else
641 int nread;
643 delta = fp->_wide_data->_IO_read_ptr - fp->_wide_data->_IO_read_base;
644 fp->_wide_data->_IO_state = fp->_wide_data->_IO_last_state;
645 nread = (*cv->__codecvt_do_length) (cv, &fp->_wide_data->_IO_state,
646 fp->_IO_read_base,
647 fp->_IO_read_end, delta);
648 fp->_IO_read_ptr = fp->_IO_read_base + nread;
649 fp->_wide_data->_IO_read_end = fp->_wide_data->_IO_read_ptr;
650 offset -= fp->_IO_read_end - fp->_IO_read_base - nread;
653 if (fp->_offset == _IO_pos_BAD)
654 goto dumb;
655 /* Make offset absolute, assuming current pointer is file_ptr(). */
656 offset += fp->_offset;
658 dir = _IO_seek_set;
659 break;
660 case _IO_seek_set:
661 break;
662 case _IO_seek_end:
664 struct _G_stat64 st;
665 if (_IO_SYSSTAT (fp, &st) == 0 && S_ISREG (st.st_mode))
667 offset += st.st_size;
668 dir = _IO_seek_set;
670 else
671 goto dumb;
674 /* At this point, dir==_IO_seek_set. */
676 /* If we are only interested in the current position we've found it now. */
677 if (mode == 0)
678 return offset;
680 /* If destination is within current buffer, optimize: */
681 if (fp->_offset != _IO_pos_BAD && fp->_IO_read_base != NULL
682 && !_IO_in_backup (fp))
684 _IO_off64_t start_offset = (fp->_offset
685 - (fp->_IO_read_end - fp->_IO_buf_base));
686 if (offset >= start_offset && offset < fp->_offset)
688 _IO_setg (fp, fp->_IO_buf_base,
689 fp->_IO_buf_base + (offset - start_offset),
690 fp->_IO_read_end);
691 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
692 _IO_wsetg (fp, fp->_wide_data->_IO_buf_base,
693 fp->_wide_data->_IO_buf_base,
694 fp->_wide_data->_IO_buf_base);
695 _IO_wsetp (fp, fp->_wide_data->_IO_buf_base,
696 fp->_wide_data->_IO_buf_base);
697 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
698 goto resync;
702 if (fp->_flags & _IO_NO_READS)
703 goto dumb;
705 /* Try to seek to a block boundary, to improve kernel page management. */
706 new_offset = offset & ~(fp->_IO_buf_end - fp->_IO_buf_base - 1);
707 delta = offset - new_offset;
708 if (delta > fp->_IO_buf_end - fp->_IO_buf_base)
710 new_offset = offset;
711 delta = 0;
713 result = _IO_SYSSEEK (fp, new_offset, 0);
714 if (result < 0)
715 return EOF;
716 if (delta == 0)
717 count = 0;
718 else
720 count = _IO_SYSREAD (fp, fp->_IO_buf_base,
721 (must_be_exact
722 ? delta : fp->_IO_buf_end - fp->_IO_buf_base));
723 if (count < delta)
725 /* We weren't allowed to read, but try to seek the remainder. */
726 offset = count == EOF ? delta : delta-count;
727 dir = _IO_seek_cur;
728 goto dumb;
731 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base + delta,
732 fp->_IO_buf_base + count);
733 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
734 _IO_wsetg (fp, fp->_wide_data->_IO_buf_base,
735 fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base);
736 _IO_wsetp (fp, fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base);
737 fp->_offset = result + count;
738 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
739 return offset;
740 dumb:
742 INTUSE(_IO_unsave_markers) (fp);
743 result = _IO_SYSSEEK (fp, offset, dir);
744 if (result != EOF)
746 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
747 fp->_offset = result;
748 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
749 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
750 _IO_wsetg (fp, fp->_wide_data->_IO_buf_base,
751 fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base);
752 _IO_wsetp (fp, fp->_wide_data->_IO_buf_base,
753 fp->_wide_data->_IO_buf_base);
755 return result;
757 resync:
758 /* We need to do it since it is possible that the file offset in
759 the kernel may be changed behind our back. It may happen when
760 we fopen a file and then do a fork. One process may access the
761 file and the kernel file offset will be changed. */
762 if (fp->_offset >= 0)
763 _IO_SYSSEEK (fp, fp->_offset, 0);
765 return offset;
767 INTDEF(_IO_wfile_seekoff)
770 _IO_size_t
771 _IO_wfile_xsputn (f, data, n)
772 _IO_FILE *f;
773 const void *data;
774 _IO_size_t n;
776 register const wchar_t *s = (const wchar_t *) data;
777 _IO_size_t to_do = n;
778 int must_flush = 0;
779 _IO_size_t count;
781 if (n <= 0)
782 return 0;
783 /* This is an optimized implementation.
784 If the amount to be written straddles a block boundary
785 (or the filebuf is unbuffered), use sys_write directly. */
787 /* First figure out how much space is available in the buffer. */
788 count = f->_wide_data->_IO_write_end - f->_wide_data->_IO_write_ptr;
789 if ((f->_flags & _IO_LINE_BUF) && (f->_flags & _IO_CURRENTLY_PUTTING))
791 count = f->_wide_data->_IO_buf_end - f->_wide_data->_IO_write_ptr;
792 if (count >= n)
794 register const wchar_t *p;
795 for (p = s + n; p > s; )
797 if (*--p == L'\n')
799 count = p - s + 1;
800 must_flush = 1;
801 break;
806 /* Then fill the buffer. */
807 if (count > 0)
809 if (count > to_do)
810 count = to_do;
811 if (count > 20)
813 #ifdef _LIBC
814 f->_wide_data->_IO_write_ptr =
815 __wmempcpy (f->_wide_data->_IO_write_ptr, s, count);
816 #else
817 wmemcpy (f->_wide_data->_IO_write_ptr, s, count);
818 f->_wide_data->_IO_write_ptr += count;
819 #endif
820 s += count;
822 else
824 register wchar_t *p = f->_wide_data->_IO_write_ptr;
825 register int i = (int) count;
826 while (--i >= 0)
827 *p++ = *s++;
828 f->_wide_data->_IO_write_ptr = p;
830 to_do -= count;
832 if (to_do > 0)
833 to_do -= INTUSE(_IO_wdefault_xsputn) (f, s, to_do);
834 if (must_flush
835 && f->_wide_data->_IO_write_ptr != f->_wide_data->_IO_write_base)
836 INTUSE(_IO_wdo_write) (f, f->_wide_data->_IO_write_base,
837 f->_wide_data->_IO_write_ptr
838 - f->_wide_data->_IO_write_base);
840 return n - to_do;
842 INTDEF(_IO_wfile_xsputn)
845 const struct _IO_jump_t _IO_wfile_jumps =
847 JUMP_INIT_DUMMY,
848 JUMP_INIT(finish, _IO_new_file_finish),
849 JUMP_INIT(overflow, (_IO_overflow_t) INTUSE(_IO_wfile_overflow)),
850 JUMP_INIT(underflow, (_IO_underflow_t) INTUSE(_IO_wfile_underflow)),
851 JUMP_INIT(uflow, (_IO_underflow_t) INTUSE(_IO_wdefault_uflow)),
852 JUMP_INIT(pbackfail, (_IO_pbackfail_t) INTUSE(_IO_wdefault_pbackfail)),
853 JUMP_INIT(xsputn, INTUSE(_IO_wfile_xsputn)),
854 JUMP_INIT(xsgetn, INTUSE(_IO_file_xsgetn)),
855 JUMP_INIT(seekoff, INTUSE(_IO_wfile_seekoff)),
856 JUMP_INIT(seekpos, _IO_default_seekpos),
857 JUMP_INIT(setbuf, _IO_new_file_setbuf),
858 JUMP_INIT(sync, (_IO_sync_t) INTUSE(_IO_wfile_sync)),
859 JUMP_INIT(doallocate, _IO_wfile_doallocate),
860 JUMP_INIT(read, INTUSE(_IO_file_read)),
861 JUMP_INIT(write, _IO_new_file_write),
862 JUMP_INIT(seek, INTUSE(_IO_file_seek)),
863 JUMP_INIT(close, INTUSE(_IO_file_close)),
864 JUMP_INIT(stat, INTUSE(_IO_file_stat)),
865 JUMP_INIT(showmanyc, _IO_default_showmanyc),
866 JUMP_INIT(imbue, _IO_default_imbue)
868 libc_hidden_data_def (_IO_wfile_jumps)
871 const struct _IO_jump_t _IO_wfile_jumps_mmap =
873 JUMP_INIT_DUMMY,
874 JUMP_INIT(finish, _IO_new_file_finish),
875 JUMP_INIT(overflow, (_IO_overflow_t) INTUSE(_IO_wfile_overflow)),
876 JUMP_INIT(underflow, (_IO_underflow_t) _IO_wfile_underflow_mmap),
877 JUMP_INIT(uflow, (_IO_underflow_t) INTUSE(_IO_wdefault_uflow)),
878 JUMP_INIT(pbackfail, (_IO_pbackfail_t) INTUSE(_IO_wdefault_pbackfail)),
879 JUMP_INIT(xsputn, INTUSE(_IO_wfile_xsputn)),
880 JUMP_INIT(xsgetn, INTUSE(_IO_file_xsgetn)),
881 JUMP_INIT(seekoff, INTUSE(_IO_wfile_seekoff)),
882 JUMP_INIT(seekpos, _IO_default_seekpos),
883 JUMP_INIT(setbuf, _IO_file_setbuf_mmap),
884 JUMP_INIT(sync, (_IO_sync_t) INTUSE(_IO_wfile_sync)),
885 JUMP_INIT(doallocate, _IO_wfile_doallocate),
886 JUMP_INIT(read, INTUSE(_IO_file_read)),
887 JUMP_INIT(write, _IO_new_file_write),
888 JUMP_INIT(seek, INTUSE(_IO_file_seek)),
889 JUMP_INIT(close, _IO_file_close_mmap),
890 JUMP_INIT(stat, INTUSE(_IO_file_stat)),
891 JUMP_INIT(showmanyc, _IO_default_showmanyc),
892 JUMP_INIT(imbue, _IO_default_imbue)
895 const struct _IO_jump_t _IO_wfile_jumps_maybe_mmap =
897 JUMP_INIT_DUMMY,
898 JUMP_INIT(finish, _IO_new_file_finish),
899 JUMP_INIT(overflow, (_IO_overflow_t) INTUSE(_IO_wfile_overflow)),
900 JUMP_INIT(underflow, (_IO_underflow_t) _IO_wfile_underflow_maybe_mmap),
901 JUMP_INIT(uflow, (_IO_underflow_t) INTUSE(_IO_wdefault_uflow)),
902 JUMP_INIT(pbackfail, (_IO_pbackfail_t) INTUSE(_IO_wdefault_pbackfail)),
903 JUMP_INIT(xsputn, INTUSE(_IO_wfile_xsputn)),
904 JUMP_INIT(xsgetn, INTUSE(_IO_file_xsgetn)),
905 JUMP_INIT(seekoff, INTUSE(_IO_wfile_seekoff)),
906 JUMP_INIT(seekpos, _IO_default_seekpos),
907 JUMP_INIT(setbuf, _IO_file_setbuf_mmap),
908 JUMP_INIT(sync, (_IO_sync_t) INTUSE(_IO_wfile_sync)),
909 JUMP_INIT(doallocate, _IO_wfile_doallocate),
910 JUMP_INIT(read, INTUSE(_IO_file_read)),
911 JUMP_INIT(write, _IO_new_file_write),
912 JUMP_INIT(seek, INTUSE(_IO_file_seek)),
913 JUMP_INIT(close, INTUSE(_IO_file_close)),
914 JUMP_INIT(stat, INTUSE(_IO_file_stat)),
915 JUMP_INIT(showmanyc, _IO_default_showmanyc),
916 JUMP_INIT(imbue, _IO_default_imbue)