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
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. */
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
55 /* Convert TO_DO wide character from DATA to FP.
56 Then mark FP as having empty buffers. */
58 _IO_wdo_write (fp
, data
, to_do
)
63 struct _IO_codecvt
*cc
= fp
->_codecvt
;
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
)
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
,
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. */
93 to_do
-= new_data
- data
;
95 /* Next see whether we had problems during the conversion. If yes,
97 if (result
!= __codecvt_ok
98 && (result
!= __codecvt_partial
|| new_data
- data
== 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
)
120 _IO_wfile_underflow (fp
)
123 struct _IO_codecvt
*cd
;
124 enum __codecvt_result status
;
127 if (__builtin_expect (fp
->_flags
& _IO_NO_READS
, 0))
129 fp
->_flags
|= _IO_ERR_SEEN
;
133 if (fp
->_wide_data
->_IO_read_ptr
< fp
->_wide_data
->_IO_read_end
)
134 return *fp
->_wide_data
->_IO_read_ptr
;
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
,
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
;
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
;
176 fp
->_IO_read_base
= fp
->_IO_read_ptr
= fp
->_IO_read_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
;
187 INTUSE(_IO_doallocbuf
) (fp
);
189 fp
->_IO_read_base
= fp
->_IO_read_ptr
= fp
->_IO_read_end
=
193 fp
->_IO_write_base
= fp
->_IO_write_ptr
= fp
->_IO_write_end
=
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
))
212 INTUSE(_IO_flush_all_linebuffered
) ();
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
);
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
];
241 count
= _IO_SYSREAD (fp
, fp
->_IO_read_end
,
242 fp
->_IO_buf_end
- fp
->_IO_read_end
);
245 if (count
== 0 && naccbuf
== 0)
246 fp
->_flags
|= _IO_EOF_SEEN
;
248 fp
->_flags
|= _IO_ERR_SEEN
, count
= 0;
250 fp
->_IO_read_end
+= count
;
254 /* There are some bytes in the external buffer but they don't
255 convert to anything. */
256 __set_errno (EILSEQ
);
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
);
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
]);
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
)
290 __set_errno (EILSEQ
);
291 fp
->_flags
|= _IO_ERR_SEEN
;
295 /* The read bytes make no complete character. Try reading again. */
296 assert (status
== __codecvt_partial
);
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
;
310 naccbuf
= fp
->_IO_read_end
- fp
->_IO_read_ptr
;
311 if (naccbuf
>= sizeof (accbuf
))
314 memcpy (accbuf
, fp
->_IO_read_ptr
, naccbuf
);
318 size_t used
= read_ptr_copy
- accbuf
;
321 memmove (accbuf
, read_ptr_copy
, naccbuf
- used
);
325 if (naccbuf
== sizeof (accbuf
))
329 fp
->_IO_read_ptr
= fp
->_IO_read_end
= fp
->_IO_read_base
;
334 return *fp
->_wide_data
->_IO_read_ptr
;
336 INTDEF(_IO_wfile_underflow
)
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
;
352 if (fp
->_wide_data
->_IO_read_ptr
< fp
->_wide_data
->_IO_read_end
)
353 return *fp
->_wide_data
->_IO_read_ptr
;
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. */
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
,
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
;
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
)
410 return _IO_WUNDERFLOW (fp
);
415 _IO_wfile_overflow (f
, wch
)
419 if (f
->_flags
& _IO_NO_WRITES
) /* SET ERROR */
421 f
->_flags
|= _IO_ERR_SEEN
;
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
);
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
;
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
)
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
)
486 INTDEF(_IO_wfile_overflow
)
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
))
499 delta
= fp
->_wide_data
->_IO_read_ptr
- fp
->_wide_data
->_IO_read_end
;
502 /* We have to find out how many bytes we have to go back in the
504 struct _IO_codecvt
*cv
= fp
->_codecvt
;
507 int clen
= (*cv
->__codecvt_do_encoding
) (cv
);
510 /* It is easy, a fixed number of input bytes are used for each
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
521 fp
->_wide_data
->_IO_state
= fp
->_wide_data
->_IO_last_state
;
522 nread
= (*cv
->__codecvt_do_length
) (cv
, &fp
->_wide_data
->_IO_state
,
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
;
536 else if (errno
== ESPIPE
)
537 ; /* Ignore error from unseekable devices. */
543 fp
->_offset
= _IO_pos_BAD
;
544 /* FIXME: Cleanup - can this be shared? */
545 /* setg(base(), ptr, ptr); */
548 INTDEF(_IO_wfile_sync
)
551 _IO_wfile_seekoff (fp
, offset
, dir
, mode
)
558 _IO_off64_t delta
, new_offset
;
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
));
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
);
583 /* There is no more data in the backup buffer. We can
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
))
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
);
623 struct _IO_codecvt
*cv
;
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. */
631 clen
= (*cv
->__codecvt_do_encoding
) (cv
);
634 offset
-= (fp
->_wide_data
->_IO_read_end
635 - fp
->_wide_data
->_IO_read_ptr
) * clen
;
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
,
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
)
652 /* Make offset absolute, assuming current pointer is file_ptr(). */
653 offset
+= fp
->_offset
;
662 if (_IO_SYSSTAT (fp
, &st
) == 0 && S_ISREG (st
.st_mode
))
664 offset
+= st
.st_size
;
671 /* At this point, dir==_IO_seek_set. */
673 /* If we are only interested in the current position we've found it now. */
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
));
687 if (_IO_in_backup (fp
))
688 _IO_switch_to_main_get_area (fp
);
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];
711 status
= (*cd
->__codecvt_do_in
) (cd
,
712 &fp
->_wide_data
->_IO_state
,
719 / sizeof (buffer
[0])),
721 if (status
!= __codecvt_ok
&& status
!= __codecvt_partial
)
723 fp
->_flags
|= _IO_ERR_SEEN
;
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
);
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
)
742 _IO_mask_flags (fp
, 0, _IO_EOF_SEEN
);
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
);
760 INTUSE(_IO_unsave_markers
) (fp
);
763 if (fp
->_flags
& _IO_NO_READS
)
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
)
774 result
= _IO_SYSSEEK (fp
, new_offset
, 0);
781 count
= _IO_SYSREAD (fp
, fp
->_IO_buf_base
,
783 ? delta
: fp
->_IO_buf_end
- fp
->_IO_buf_base
));
786 /* We weren't allowed to read, but try to seek the remainder. */
787 offset
= count
== EOF
? delta
: delta
-count
;
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
);
800 INTUSE(_IO_unsave_markers
) (fp
);
801 result
= _IO_SYSSEEK (fp
, offset
, dir
);
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
);
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);
825 INTDEF(_IO_wfile_seekoff
)
829 _IO_wfile_xsputn (f
, data
, n
)
834 register const wchar_t *s
= (const wchar_t *) data
;
835 _IO_size_t to_do
= n
;
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
;
852 register const wchar_t *p
;
853 for (p
= s
+ n
; p
> s
; )
864 /* Then fill the buffer. */
872 f
->_wide_data
->_IO_write_ptr
=
873 __wmempcpy (f
->_wide_data
->_IO_write_ptr
, s
, count
);
875 wmemcpy (f
->_wide_data
->_IO_write_ptr
, s
, count
);
876 f
->_wide_data
->_IO_write_ptr
+= count
;
882 register wchar_t *p
= f
->_wide_data
->_IO_write_ptr
;
883 register int i
= (int) count
;
886 f
->_wide_data
->_IO_write_ptr
= p
;
891 to_do
-= INTUSE(_IO_wdefault_xsputn
) (f
, s
, to_do
);
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
);
900 INTDEF(_IO_wfile_xsputn
)
903 const struct _IO_jump_t _IO_wfile_jumps
=
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
=
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
=
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
)