1 /* Copyright (C) 1993-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>.
18 As a special exception, if you link the code in this file with
19 files compiled with a GNU compiler to produce an executable,
20 that does not cause the resulting executable to be covered by
21 the GNU Lesser General Public License. This exception does not
22 however invalidate any other reasons why the executable file
23 might be covered by the GNU Lesser General Public License.
24 This exception applies to code released by its copyright holders
25 in files containing the exception. */
34 /* Convert TO_DO wide character from DATA to FP.
35 Then mark FP as having empty buffers. */
37 _IO_wdo_write (FILE *fp
, const wchar_t *data
, size_t to_do
)
39 struct _IO_codecvt
*cc
= fp
->_codecvt
;
43 if (fp
->_IO_write_end
== fp
->_IO_write_ptr
44 && fp
->_IO_write_end
!= fp
->_IO_write_base
)
46 if (_IO_new_do_write (fp
, fp
->_IO_write_base
,
47 fp
->_IO_write_ptr
- fp
->_IO_write_base
) == EOF
)
53 enum __codecvt_result result
;
54 const wchar_t *new_data
;
55 char mb_buf
[MB_LEN_MAX
];
56 char *write_base
, *write_ptr
, *buf_end
;
58 if (fp
->_IO_write_ptr
- fp
->_IO_write_base
< sizeof (mb_buf
))
60 /* Make sure we have room for at least one multibyte
62 write_ptr
= write_base
= mb_buf
;
63 buf_end
= mb_buf
+ sizeof (mb_buf
);
67 write_ptr
= fp
->_IO_write_ptr
;
68 write_base
= fp
->_IO_write_base
;
69 buf_end
= fp
->_IO_buf_end
;
72 /* Now convert from the internal format into the external buffer. */
73 result
= __libio_codecvt_out (cc
, &fp
->_wide_data
->_IO_state
,
74 data
, data
+ to_do
, &new_data
,
79 /* Write out what we produced so far. */
80 if (_IO_new_do_write (fp
, write_base
, write_ptr
- write_base
) == EOF
)
81 /* Something went wrong. */
84 to_do
-= new_data
- data
;
86 /* Next see whether we had problems during the conversion. If yes,
88 if (result
!= __codecvt_ok
89 && (result
!= __codecvt_partial
|| new_data
- data
== 0))
97 _IO_wsetg (fp
, fp
->_wide_data
->_IO_buf_base
, fp
->_wide_data
->_IO_buf_base
,
98 fp
->_wide_data
->_IO_buf_base
);
99 fp
->_wide_data
->_IO_write_base
= fp
->_wide_data
->_IO_write_ptr
100 = fp
->_wide_data
->_IO_buf_base
;
101 fp
->_wide_data
->_IO_write_end
= ((fp
->_flags
& (_IO_LINE_BUF
| _IO_UNBUFFERED
))
102 ? fp
->_wide_data
->_IO_buf_base
103 : fp
->_wide_data
->_IO_buf_end
);
105 return to_do
== 0 ? 0 : WEOF
;
107 libc_hidden_def (_IO_wdo_write
)
111 _IO_wfile_underflow (FILE *fp
)
113 struct _IO_codecvt
*cd
;
114 enum __codecvt_result status
;
117 /* C99 requires EOF to be "sticky". */
118 if (fp
->_flags
& _IO_EOF_SEEN
)
121 if (__glibc_unlikely (fp
->_flags
& _IO_NO_READS
))
123 fp
->_flags
|= _IO_ERR_SEEN
;
127 if (fp
->_wide_data
->_IO_read_ptr
< fp
->_wide_data
->_IO_read_end
)
128 return *fp
->_wide_data
->_IO_read_ptr
;
132 /* Maybe there is something left in the external buffer. */
133 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
135 /* There is more in the external. Convert it. */
136 const char *read_stop
= (const char *) fp
->_IO_read_ptr
;
138 fp
->_wide_data
->_IO_last_state
= fp
->_wide_data
->_IO_state
;
139 fp
->_wide_data
->_IO_read_base
= fp
->_wide_data
->_IO_read_ptr
=
140 fp
->_wide_data
->_IO_buf_base
;
141 status
= __libio_codecvt_in (cd
, &fp
->_wide_data
->_IO_state
,
142 fp
->_IO_read_ptr
, fp
->_IO_read_end
,
144 fp
->_wide_data
->_IO_read_ptr
,
145 fp
->_wide_data
->_IO_buf_end
,
146 &fp
->_wide_data
->_IO_read_end
);
148 fp
->_IO_read_base
= fp
->_IO_read_ptr
;
149 fp
->_IO_read_ptr
= (char *) read_stop
;
151 /* If we managed to generate some text return the next character. */
152 if (fp
->_wide_data
->_IO_read_ptr
< fp
->_wide_data
->_IO_read_end
)
153 return *fp
->_wide_data
->_IO_read_ptr
;
155 if (status
== __codecvt_error
)
157 __set_errno (EILSEQ
);
158 fp
->_flags
|= _IO_ERR_SEEN
;
162 /* Move the remaining content of the read buffer to the beginning. */
163 memmove (fp
->_IO_buf_base
, fp
->_IO_read_ptr
,
164 fp
->_IO_read_end
- fp
->_IO_read_ptr
);
165 fp
->_IO_read_end
= (fp
->_IO_buf_base
166 + (fp
->_IO_read_end
- fp
->_IO_read_ptr
));
167 fp
->_IO_read_base
= fp
->_IO_read_ptr
= fp
->_IO_buf_base
;
170 fp
->_IO_read_base
= fp
->_IO_read_ptr
= fp
->_IO_read_end
=
173 if (fp
->_IO_buf_base
== NULL
)
175 /* Maybe we already have a push back pointer. */
176 if (fp
->_IO_save_base
!= NULL
)
178 free (fp
->_IO_save_base
);
179 fp
->_flags
&= ~_IO_IN_BACKUP
;
183 fp
->_IO_read_base
= fp
->_IO_read_ptr
= fp
->_IO_read_end
=
187 fp
->_IO_write_base
= fp
->_IO_write_ptr
= fp
->_IO_write_end
=
190 if (fp
->_wide_data
->_IO_buf_base
== NULL
)
192 /* Maybe we already have a push back pointer. */
193 if (fp
->_wide_data
->_IO_save_base
!= NULL
)
195 free (fp
->_wide_data
->_IO_save_base
);
196 fp
->_flags
&= ~_IO_IN_BACKUP
;
198 _IO_wdoallocbuf (fp
);
201 /* FIXME This can/should be moved to genops ?? */
202 if (fp
->_flags
& (_IO_LINE_BUF
| _IO_UNBUFFERED
))
204 /* We used to flush all line-buffered stream. This really isn't
205 required by any standard. My recollection is that
206 traditional Unix systems did this for stdout. stderr better
207 not be line buffered. So we do just that here
208 explicitly. --drepper */
209 _IO_acquire_lock (stdout
);
211 if ((stdout
->_flags
& (_IO_LINKED
| _IO_NO_WRITES
| _IO_LINE_BUF
))
212 == (_IO_LINKED
| _IO_LINE_BUF
))
213 _IO_OVERFLOW (stdout
, EOF
);
215 _IO_release_lock (stdout
);
218 _IO_switch_to_get_mode (fp
);
220 fp
->_wide_data
->_IO_read_base
= fp
->_wide_data
->_IO_read_ptr
=
221 fp
->_wide_data
->_IO_buf_base
;
222 fp
->_wide_data
->_IO_read_end
= fp
->_wide_data
->_IO_buf_base
;
223 fp
->_wide_data
->_IO_write_base
= fp
->_wide_data
->_IO_write_ptr
=
224 fp
->_wide_data
->_IO_write_end
= fp
->_wide_data
->_IO_buf_base
;
226 const char *read_ptr_copy
;
227 char accbuf
[MB_LEN_MAX
];
230 count
= _IO_SYSREAD (fp
, fp
->_IO_read_end
,
231 fp
->_IO_buf_end
- fp
->_IO_read_end
);
234 if (count
== 0 && naccbuf
== 0)
236 fp
->_flags
|= _IO_EOF_SEEN
;
237 fp
->_offset
= _IO_pos_BAD
;
240 fp
->_flags
|= _IO_ERR_SEEN
, count
= 0;
242 fp
->_IO_read_end
+= count
;
246 /* There are some bytes in the external buffer but they don't
247 convert to anything. */
248 __set_errno (EILSEQ
);
251 if (fp
->_offset
!= _IO_pos_BAD
)
252 _IO_pos_adjust (fp
->_offset
, count
);
254 /* Now convert the read input. */
255 fp
->_wide_data
->_IO_last_state
= fp
->_wide_data
->_IO_state
;
256 fp
->_IO_read_base
= fp
->_IO_read_ptr
;
257 const char *from
= fp
->_IO_read_ptr
;
258 const char *to
= fp
->_IO_read_end
;
259 size_t to_copy
= count
;
260 if (__glibc_unlikely (naccbuf
!= 0))
262 to_copy
= MIN (sizeof (accbuf
) - naccbuf
, count
);
263 to
= __mempcpy (&accbuf
[naccbuf
], from
, to_copy
);
267 status
= __libio_codecvt_in (cd
, &fp
->_wide_data
->_IO_state
,
268 from
, to
, &read_ptr_copy
,
269 fp
->_wide_data
->_IO_read_end
,
270 fp
->_wide_data
->_IO_buf_end
,
271 &fp
->_wide_data
->_IO_read_end
);
273 if (__glibc_unlikely (naccbuf
!= 0))
274 fp
->_IO_read_ptr
+= MAX (0, read_ptr_copy
- &accbuf
[naccbuf
- to_copy
]);
276 fp
->_IO_read_ptr
= (char *) read_ptr_copy
;
277 if (fp
->_wide_data
->_IO_read_end
== fp
->_wide_data
->_IO_buf_base
)
279 if (status
== __codecvt_error
)
282 __set_errno (EILSEQ
);
283 fp
->_flags
|= _IO_ERR_SEEN
;
287 /* The read bytes make no complete character. Try reading again. */
288 assert (status
== __codecvt_partial
);
292 if (fp
->_IO_read_base
< fp
->_IO_read_ptr
)
294 /* Partially used the buffer for some input data that
295 produces no output. */
296 size_t avail
= fp
->_IO_read_end
- fp
->_IO_read_ptr
;
297 memmove (fp
->_IO_read_base
, fp
->_IO_read_ptr
, avail
);
298 fp
->_IO_read_ptr
= fp
->_IO_read_base
;
299 fp
->_IO_read_end
-= avail
;
302 naccbuf
= fp
->_IO_read_end
- fp
->_IO_read_ptr
;
303 if (naccbuf
>= sizeof (accbuf
))
306 memcpy (accbuf
, fp
->_IO_read_ptr
, naccbuf
);
310 size_t used
= read_ptr_copy
- accbuf
;
313 memmove (accbuf
, read_ptr_copy
, naccbuf
- used
);
317 if (naccbuf
== sizeof (accbuf
))
321 fp
->_IO_read_ptr
= fp
->_IO_read_end
= fp
->_IO_read_base
;
326 return *fp
->_wide_data
->_IO_read_ptr
;
328 libc_hidden_def (_IO_wfile_underflow
)
332 _IO_wfile_underflow_mmap (FILE *fp
)
334 struct _IO_codecvt
*cd
;
335 const char *read_stop
;
337 if (__glibc_unlikely (fp
->_flags
& _IO_NO_READS
))
339 fp
->_flags
|= _IO_ERR_SEEN
;
343 if (fp
->_wide_data
->_IO_read_ptr
< fp
->_wide_data
->_IO_read_end
)
344 return *fp
->_wide_data
->_IO_read_ptr
;
348 /* Maybe there is something left in the external buffer. */
349 if (fp
->_IO_read_ptr
>= fp
->_IO_read_end
350 /* No. But maybe the read buffer is not fully set up. */
351 && _IO_file_underflow_mmap (fp
) == EOF
)
352 /* Nothing available. _IO_file_underflow_mmap has set the EOF or error
353 flags as appropriate. */
356 /* There is more in the external. Convert it. */
357 read_stop
= (const char *) fp
->_IO_read_ptr
;
359 if (fp
->_wide_data
->_IO_buf_base
== NULL
)
361 /* Maybe we already have a push back pointer. */
362 if (fp
->_wide_data
->_IO_save_base
!= NULL
)
364 free (fp
->_wide_data
->_IO_save_base
);
365 fp
->_flags
&= ~_IO_IN_BACKUP
;
367 _IO_wdoallocbuf (fp
);
370 fp
->_wide_data
->_IO_last_state
= fp
->_wide_data
->_IO_state
;
371 fp
->_wide_data
->_IO_read_base
= fp
->_wide_data
->_IO_read_ptr
=
372 fp
->_wide_data
->_IO_buf_base
;
373 __libio_codecvt_in (cd
, &fp
->_wide_data
->_IO_state
,
374 fp
->_IO_read_ptr
, fp
->_IO_read_end
,
376 fp
->_wide_data
->_IO_read_ptr
,
377 fp
->_wide_data
->_IO_buf_end
,
378 &fp
->_wide_data
->_IO_read_end
);
380 fp
->_IO_read_ptr
= (char *) read_stop
;
382 /* If we managed to generate some text return the next character. */
383 if (fp
->_wide_data
->_IO_read_ptr
< fp
->_wide_data
->_IO_read_end
)
384 return *fp
->_wide_data
->_IO_read_ptr
;
386 /* There is some garbage at the end of the file. */
387 __set_errno (EILSEQ
);
388 fp
->_flags
|= _IO_ERR_SEEN
;
393 _IO_wfile_underflow_maybe_mmap (FILE *fp
)
395 /* This is the first read attempt. Doing the underflow will choose mmap
396 or vanilla operations and then punt to the chosen underflow routine.
397 Then we can punt to ours. */
398 if (_IO_file_underflow_maybe_mmap (fp
) == EOF
)
401 return _IO_WUNDERFLOW (fp
);
406 _IO_wfile_overflow (FILE *f
, wint_t wch
)
408 if (f
->_flags
& _IO_NO_WRITES
) /* SET ERROR */
410 f
->_flags
|= _IO_ERR_SEEN
;
414 /* If currently reading or no buffer allocated. */
415 if ((f
->_flags
& _IO_CURRENTLY_PUTTING
) == 0
416 || f
->_wide_data
->_IO_write_base
== NULL
)
418 /* Allocate a buffer if needed. */
419 if (f
->_wide_data
->_IO_write_base
== 0)
422 _IO_free_wbackup_area (f
);
423 _IO_wsetg (f
, f
->_wide_data
->_IO_buf_base
,
424 f
->_wide_data
->_IO_buf_base
, f
->_wide_data
->_IO_buf_base
);
426 if (f
->_IO_write_base
== NULL
)
429 _IO_setg (f
, f
->_IO_buf_base
, f
->_IO_buf_base
, f
->_IO_buf_base
);
434 /* Otherwise must be currently reading. If _IO_read_ptr
435 (and hence also _IO_read_end) is at the buffer end,
436 logically slide the buffer forwards one block (by setting
437 the read pointers to all point at the beginning of the
438 block). This makes room for subsequent output.
439 Otherwise, set the read pointers to _IO_read_end (leaving
440 that alone, so it can continue to correspond to the
441 external position). */
442 if (f
->_wide_data
->_IO_read_ptr
== f
->_wide_data
->_IO_buf_end
)
444 f
->_IO_read_end
= f
->_IO_read_ptr
= f
->_IO_buf_base
;
445 f
->_wide_data
->_IO_read_end
= f
->_wide_data
->_IO_read_ptr
=
446 f
->_wide_data
->_IO_buf_base
;
449 f
->_wide_data
->_IO_write_ptr
= f
->_wide_data
->_IO_read_ptr
;
450 f
->_wide_data
->_IO_write_base
= f
->_wide_data
->_IO_write_ptr
;
451 f
->_wide_data
->_IO_write_end
= f
->_wide_data
->_IO_buf_end
;
452 f
->_wide_data
->_IO_read_base
= f
->_wide_data
->_IO_read_ptr
=
453 f
->_wide_data
->_IO_read_end
;
455 f
->_IO_write_ptr
= f
->_IO_read_ptr
;
456 f
->_IO_write_base
= f
->_IO_write_ptr
;
457 f
->_IO_write_end
= f
->_IO_buf_end
;
458 f
->_IO_read_base
= f
->_IO_read_ptr
= f
->_IO_read_end
;
460 f
->_flags
|= _IO_CURRENTLY_PUTTING
;
461 if (f
->_flags
& (_IO_LINE_BUF
| _IO_UNBUFFERED
))
462 f
->_wide_data
->_IO_write_end
= f
->_wide_data
->_IO_write_ptr
;
465 return _IO_do_flush (f
);
466 if (f
->_wide_data
->_IO_write_ptr
== f
->_wide_data
->_IO_buf_end
)
467 /* Buffer is really full */
468 if (_IO_do_flush (f
) == EOF
)
470 *f
->_wide_data
->_IO_write_ptr
++ = wch
;
471 if ((f
->_flags
& _IO_UNBUFFERED
)
472 || ((f
->_flags
& _IO_LINE_BUF
) && wch
== L
'\n'))
473 if (_IO_do_flush (f
) == EOF
)
477 libc_hidden_def (_IO_wfile_overflow
)
480 _IO_wfile_sync (FILE *fp
)
485 /* char* ptr = cur_ptr(); */
486 if (fp
->_wide_data
->_IO_write_ptr
> fp
->_wide_data
->_IO_write_base
)
487 if (_IO_do_flush (fp
))
489 delta
= fp
->_wide_data
->_IO_read_ptr
- fp
->_wide_data
->_IO_read_end
;
492 /* We have to find out how many bytes we have to go back in the
494 struct _IO_codecvt
*cv
= fp
->_codecvt
;
497 int clen
= __libio_codecvt_encoding (cv
);
500 /* It is easy, a fixed number of input bytes are used for each
505 /* We have to find out the hard way how much to back off.
506 To do this we determine how much input we needed to
507 generate the wide characters up to the current reading
510 size_t wnread
= (fp
->_wide_data
->_IO_read_ptr
511 - fp
->_wide_data
->_IO_read_base
);
512 fp
->_wide_data
->_IO_state
= fp
->_wide_data
->_IO_last_state
;
513 nread
= __libio_codecvt_length (cv
, &fp
->_wide_data
->_IO_state
,
515 fp
->_IO_read_end
, wnread
);
516 fp
->_IO_read_ptr
= fp
->_IO_read_base
+ nread
;
517 delta
= -(fp
->_IO_read_end
- fp
->_IO_read_base
- nread
);
520 new_pos
= _IO_SYSSEEK (fp
, delta
, 1);
521 if (new_pos
!= (off64_t
) EOF
)
523 fp
->_wide_data
->_IO_read_end
= fp
->_wide_data
->_IO_read_ptr
;
524 fp
->_IO_read_end
= fp
->_IO_read_ptr
;
526 else if (errno
== ESPIPE
)
527 ; /* Ignore error from unseekable devices. */
532 fp
->_offset
= _IO_pos_BAD
;
533 /* FIXME: Cleanup - can this be shared? */
534 /* setg(base(), ptr, ptr); */
537 libc_hidden_def (_IO_wfile_sync
)
539 /* Adjust the internal buffer pointers to reflect the state in the external
540 buffer. The content between fp->_IO_read_base and fp->_IO_read_ptr is
541 assumed to be converted and available in the range
542 fp->_wide_data->_IO_read_base and fp->_wide_data->_IO_read_end.
544 Returns 0 on success and -1 on error with the _IO_ERR_SEEN flag set. */
546 adjust_wide_data (FILE *fp
, bool do_convert
)
548 struct _IO_codecvt
*cv
= fp
->_codecvt
;
550 int clen
= __libio_codecvt_encoding (cv
);
552 /* Take the easy way out for constant length encodings if we don't need to
554 if (!do_convert
&& clen
> 0)
556 fp
->_wide_data
->_IO_read_end
+= ((fp
->_IO_read_ptr
- fp
->_IO_read_base
)
561 enum __codecvt_result status
;
562 const char *read_stop
= (const char *) fp
->_IO_read_base
;
566 fp
->_wide_data
->_IO_last_state
= fp
->_wide_data
->_IO_state
;
567 status
= __libio_codecvt_in (cv
, &fp
->_wide_data
->_IO_state
,
568 fp
->_IO_read_base
, fp
->_IO_read_ptr
,
570 fp
->_wide_data
->_IO_read_base
,
571 fp
->_wide_data
->_IO_buf_end
,
572 &fp
->_wide_data
->_IO_read_end
);
574 /* Should we return EILSEQ? */
575 if (__glibc_unlikely (status
== __codecvt_error
))
577 fp
->_flags
|= _IO_ERR_SEEN
;
581 while (__builtin_expect (status
== __codecvt_partial
, 0));
584 /* Now seek to _IO_read_end to behave as if we have read it all in. */
585 fp
->_wide_data
->_IO_read_ptr
= fp
->_wide_data
->_IO_read_end
;
590 /* ftell{,o} implementation for wide mode. Don't modify any state of the file
591 pointer while we try to get the current state of the stream except in one
592 case, which is when we have unflushed writes in append mode. */
594 do_ftell_wide (FILE *fp
)
596 off64_t result
, offset
= 0;
598 /* No point looking for offsets in the buffer if it hasn't even been
600 if (fp
->_wide_data
->_IO_buf_base
!= NULL
)
602 const wchar_t *wide_read_base
;
603 const wchar_t *wide_read_ptr
;
604 const wchar_t *wide_read_end
;
605 bool unflushed_writes
= (fp
->_wide_data
->_IO_write_ptr
606 > fp
->_wide_data
->_IO_write_base
);
608 bool append_mode
= (fp
->_flags
& _IO_IS_APPENDING
) == _IO_IS_APPENDING
;
610 /* When we have unflushed writes in append mode, seek to the end of the
611 file and record that offset. This is the only time we change the file
612 stream state and it is safe since the file handle is active. */
613 if (unflushed_writes
&& append_mode
)
615 result
= _IO_SYSSEEK (fp
, 0, _IO_seek_end
);
616 if (result
== _IO_pos_BAD
)
619 fp
->_offset
= result
;
622 /* XXX For wide stream with backup store it is not very
623 reasonable to determine the offset. The pushed-back
624 character might require a state change and we need not be
625 able to compute the initial state by reverse transformation
626 since there is no guarantee of symmetry. So we don't even
627 try and return an error. */
628 if (_IO_in_backup (fp
))
630 if (fp
->_wide_data
->_IO_read_ptr
< fp
->_wide_data
->_IO_read_end
)
632 __set_errno (EINVAL
);
636 /* Nothing in the backup store, so note the backed up pointers
637 without changing the state. */
638 wide_read_base
= fp
->_wide_data
->_IO_save_base
;
639 wide_read_ptr
= wide_read_base
;
640 wide_read_end
= fp
->_wide_data
->_IO_save_end
;
644 wide_read_base
= fp
->_wide_data
->_IO_read_base
;
645 wide_read_ptr
= fp
->_wide_data
->_IO_read_ptr
;
646 wide_read_end
= fp
->_wide_data
->_IO_read_end
;
649 struct _IO_codecvt
*cv
= fp
->_codecvt
;
650 int clen
= __libio_codecvt_encoding (cv
);
652 if (!unflushed_writes
)
656 offset
-= (wide_read_end
- wide_read_ptr
) * clen
;
657 offset
-= fp
->_IO_read_end
- fp
->_IO_read_ptr
;
663 size_t delta
= wide_read_ptr
- wide_read_base
;
664 __mbstate_t state
= fp
->_wide_data
->_IO_last_state
;
665 nread
= __libio_codecvt_length (cv
, &state
,
667 fp
->_IO_read_end
, delta
);
668 offset
-= fp
->_IO_read_end
- fp
->_IO_read_base
- nread
;
674 offset
+= (fp
->_wide_data
->_IO_write_ptr
675 - fp
->_wide_data
->_IO_write_base
) * clen
;
678 size_t delta
= (fp
->_wide_data
->_IO_write_ptr
679 - fp
->_wide_data
->_IO_write_base
);
681 /* Allocate enough space for the conversion. */
682 size_t outsize
= delta
* sizeof (wchar_t);
683 char *out
= malloc (outsize
);
685 const wchar_t *in
= fp
->_wide_data
->_IO_write_base
;
687 enum __codecvt_result status
;
689 __mbstate_t state
= fp
->_wide_data
->_IO_last_state
;
690 status
= __libio_codecvt_out (cv
, &state
, in
, in
+ delta
, &in
,
691 out
, out
+ outsize
, &outstop
);
693 /* We don't check for __codecvt_partial because it can be
694 returned on one of two conditions: either the output
695 buffer is full or the input sequence is incomplete. We
696 take care to allocate enough buffer and our input
697 sequences must be complete since they are accepted as
698 wchar_t; if not, then that is an error. */
699 if (__glibc_unlikely (status
!= __codecvt_ok
))
705 offset
+= outstop
- out
;
709 /* We don't trust _IO_read_end to represent the current file offset
710 when writing in append mode because the value would have to be
711 shifted to the end of the file during a flush. Use the write base
712 instead, along with the new offset we got above when we did a seek
713 to the end of the file. */
715 offset
+= fp
->_IO_write_ptr
- fp
->_IO_write_base
;
716 /* For all other modes, _IO_read_end represents the file offset. */
718 offset
+= fp
->_IO_write_ptr
- fp
->_IO_read_end
;
722 if (fp
->_offset
!= _IO_pos_BAD
)
723 result
= fp
->_offset
;
725 result
= _IO_SYSSEEK (fp
, 0, _IO_seek_cur
);
734 __set_errno (EINVAL
);
742 _IO_wfile_seekoff (FILE *fp
, off64_t offset
, int dir
, int mode
)
745 off64_t delta
, new_offset
;
748 /* Short-circuit into a separate function. We don't want to mix any
749 functionality and we don't want to touch anything inside the FILE
752 return do_ftell_wide (fp
);
754 /* POSIX.1 8.2.3.7 says that after a call the fflush() the file
755 offset of the underlying file must be exact. */
756 int must_be_exact
= ((fp
->_wide_data
->_IO_read_base
757 == fp
->_wide_data
->_IO_read_end
)
758 && (fp
->_wide_data
->_IO_write_base
759 == fp
->_wide_data
->_IO_write_ptr
));
761 bool was_writing
= ((fp
->_wide_data
->_IO_write_ptr
762 > fp
->_wide_data
->_IO_write_base
)
763 || _IO_in_put_mode (fp
));
765 /* Flush unwritten characters.
766 (This may do an unneeded write if we seek within the buffer.
767 But to be able to switch to reading, we would need to set
768 egptr to pptr. That can't be done in the current design,
769 which assumes file_ptr() is eGptr. Anyway, since we probably
770 end up flushing when we close(), it doesn't make much difference.)
771 FIXME: simulate mem-mapped files. */
772 if (was_writing
&& _IO_switch_to_wget_mode (fp
))
775 if (fp
->_wide_data
->_IO_buf_base
== NULL
)
777 /* It could be that we already have a pushback buffer. */
778 if (fp
->_wide_data
->_IO_read_base
!= NULL
)
780 free (fp
->_wide_data
->_IO_read_base
);
781 fp
->_flags
&= ~_IO_IN_BACKUP
;
784 _IO_setp (fp
, fp
->_IO_buf_base
, fp
->_IO_buf_base
);
785 _IO_setg (fp
, fp
->_IO_buf_base
, fp
->_IO_buf_base
, fp
->_IO_buf_base
);
786 _IO_wsetp (fp
, fp
->_wide_data
->_IO_buf_base
,
787 fp
->_wide_data
->_IO_buf_base
);
788 _IO_wsetg (fp
, fp
->_wide_data
->_IO_buf_base
,
789 fp
->_wide_data
->_IO_buf_base
, fp
->_wide_data
->_IO_buf_base
);
794 struct _IO_codecvt
*cv
;
798 /* Adjust for read-ahead (bytes is buffer). To do this we must
799 find out which position in the external buffer corresponds to
800 the current position in the internal buffer. */
802 clen
= __libio_codecvt_encoding (cv
);
804 if (mode
!= 0 || !was_writing
)
808 offset
-= (fp
->_wide_data
->_IO_read_end
809 - fp
->_wide_data
->_IO_read_ptr
) * clen
;
810 /* Adjust by readahead in external buffer. */
811 offset
-= fp
->_IO_read_end
- fp
->_IO_read_ptr
;
817 delta
= (fp
->_wide_data
->_IO_read_ptr
818 - fp
->_wide_data
->_IO_read_base
);
819 fp
->_wide_data
->_IO_state
= fp
->_wide_data
->_IO_last_state
;
820 nread
= __libio_codecvt_length (cv
,
821 &fp
->_wide_data
->_IO_state
,
823 fp
->_IO_read_end
, delta
);
824 fp
->_IO_read_ptr
= fp
->_IO_read_base
+ nread
;
825 fp
->_wide_data
->_IO_read_end
= fp
->_wide_data
->_IO_read_ptr
;
826 offset
-= fp
->_IO_read_end
- fp
->_IO_read_base
- nread
;
830 if (fp
->_offset
== _IO_pos_BAD
)
833 /* Make offset absolute, assuming current pointer is file_ptr(). */
834 offset
+= fp
->_offset
;
842 struct __stat64_t64 st
;
843 if (_IO_SYSSTAT (fp
, &st
) == 0 && S_ISREG (st
.st_mode
))
845 offset
+= st
.st_size
;
853 _IO_free_wbackup_area (fp
);
855 /* At this point, dir==_IO_seek_set. */
857 /* If destination is within current buffer, optimize: */
858 if (fp
->_offset
!= _IO_pos_BAD
&& fp
->_IO_read_base
!= NULL
859 && !_IO_in_backup (fp
))
861 off64_t start_offset
= (fp
->_offset
862 - (fp
->_IO_read_end
- fp
->_IO_buf_base
));
863 if (offset
>= start_offset
&& offset
< fp
->_offset
)
865 _IO_setg (fp
, fp
->_IO_buf_base
,
866 fp
->_IO_buf_base
+ (offset
- start_offset
),
868 _IO_setp (fp
, fp
->_IO_buf_base
, fp
->_IO_buf_base
);
869 _IO_wsetg (fp
, fp
->_wide_data
->_IO_buf_base
,
870 fp
->_wide_data
->_IO_buf_base
,
871 fp
->_wide_data
->_IO_buf_base
);
872 _IO_wsetp (fp
, fp
->_wide_data
->_IO_buf_base
,
873 fp
->_wide_data
->_IO_buf_base
);
875 if (adjust_wide_data (fp
, false))
878 _IO_mask_flags (fp
, 0, _IO_EOF_SEEN
);
883 if (fp
->_flags
& _IO_NO_READS
)
886 /* Try to seek to a block boundary, to improve kernel page management. */
887 new_offset
= offset
& ~(fp
->_IO_buf_end
- fp
->_IO_buf_base
- 1);
888 delta
= offset
- new_offset
;
889 if (delta
> fp
->_IO_buf_end
- fp
->_IO_buf_base
)
894 result
= _IO_SYSSEEK (fp
, new_offset
, 0);
901 count
= _IO_SYSREAD (fp
, fp
->_IO_buf_base
,
903 ? delta
: fp
->_IO_buf_end
- fp
->_IO_buf_base
));
906 /* We weren't allowed to read, but try to seek the remainder. */
907 offset
= count
== EOF
? delta
: delta
-count
;
912 _IO_setg (fp
, fp
->_IO_buf_base
, fp
->_IO_buf_base
+ delta
,
913 fp
->_IO_buf_base
+ count
);
914 _IO_setp (fp
, fp
->_IO_buf_base
, fp
->_IO_buf_base
);
915 _IO_wsetg (fp
, fp
->_wide_data
->_IO_buf_base
,
916 fp
->_wide_data
->_IO_buf_base
, fp
->_wide_data
->_IO_buf_base
);
917 _IO_wsetp (fp
, fp
->_wide_data
->_IO_buf_base
, fp
->_wide_data
->_IO_buf_base
);
919 if (adjust_wide_data (fp
, true))
922 fp
->_offset
= result
+ count
;
923 _IO_mask_flags (fp
, 0, _IO_EOF_SEEN
);
927 _IO_unsave_markers (fp
);
928 result
= _IO_SYSSEEK (fp
, offset
, dir
);
931 _IO_mask_flags (fp
, 0, _IO_EOF_SEEN
);
932 fp
->_offset
= result
;
933 _IO_setg (fp
, fp
->_IO_buf_base
, fp
->_IO_buf_base
, fp
->_IO_buf_base
);
934 _IO_setp (fp
, fp
->_IO_buf_base
, fp
->_IO_buf_base
);
935 _IO_wsetg (fp
, fp
->_wide_data
->_IO_buf_base
,
936 fp
->_wide_data
->_IO_buf_base
, fp
->_wide_data
->_IO_buf_base
);
937 _IO_wsetp (fp
, fp
->_wide_data
->_IO_buf_base
,
938 fp
->_wide_data
->_IO_buf_base
);
943 /* We need to do it since it is possible that the file offset in
944 the kernel may be changed behind our back. It may happen when
945 we fopen a file and then do a fork. One process may access the
946 file and the kernel file offset will be changed. */
947 if (fp
->_offset
>= 0)
948 _IO_SYSSEEK (fp
, fp
->_offset
, 0);
952 libc_hidden_def (_IO_wfile_seekoff
)
956 _IO_wfile_xsputn (FILE *f
, const void *data
, size_t n
)
958 const wchar_t *s
= (const wchar_t *) data
;
965 /* This is an optimized implementation.
966 If the amount to be written straddles a block boundary
967 (or the filebuf is unbuffered), use sys_write directly. */
969 /* First figure out how much space is available in the buffer. */
970 count
= f
->_wide_data
->_IO_write_end
- f
->_wide_data
->_IO_write_ptr
;
971 if ((f
->_flags
& _IO_LINE_BUF
) && (f
->_flags
& _IO_CURRENTLY_PUTTING
))
973 count
= f
->_wide_data
->_IO_buf_end
- f
->_wide_data
->_IO_write_ptr
;
977 for (p
= s
+ n
; p
> s
; )
988 /* Then fill the buffer. */
995 f
->_wide_data
->_IO_write_ptr
=
996 __wmempcpy (f
->_wide_data
->_IO_write_ptr
, s
, count
);
1001 wchar_t *p
= f
->_wide_data
->_IO_write_ptr
;
1002 int i
= (int) count
;
1005 f
->_wide_data
->_IO_write_ptr
= p
;
1010 to_do
-= _IO_wdefault_xsputn (f
, s
, to_do
);
1012 && f
->_wide_data
->_IO_write_ptr
!= f
->_wide_data
->_IO_write_base
)
1013 _IO_wdo_write (f
, f
->_wide_data
->_IO_write_base
,
1014 f
->_wide_data
->_IO_write_ptr
1015 - f
->_wide_data
->_IO_write_base
);
1019 libc_hidden_def (_IO_wfile_xsputn
)
1022 const struct _IO_jump_t _IO_wfile_jumps libio_vtable
=
1025 JUMP_INIT(finish
, _IO_new_file_finish
),
1026 JUMP_INIT(overflow
, (_IO_overflow_t
) _IO_wfile_overflow
),
1027 JUMP_INIT(underflow
, (_IO_underflow_t
) _IO_wfile_underflow
),
1028 JUMP_INIT(uflow
, (_IO_underflow_t
) _IO_wdefault_uflow
),
1029 JUMP_INIT(pbackfail
, (_IO_pbackfail_t
) _IO_wdefault_pbackfail
),
1030 JUMP_INIT(xsputn
, _IO_wfile_xsputn
),
1031 JUMP_INIT(xsgetn
, _IO_file_xsgetn
),
1032 JUMP_INIT(seekoff
, _IO_wfile_seekoff
),
1033 JUMP_INIT(seekpos
, _IO_default_seekpos
),
1034 JUMP_INIT(setbuf
, _IO_new_file_setbuf
),
1035 JUMP_INIT(sync
, (_IO_sync_t
) _IO_wfile_sync
),
1036 JUMP_INIT(doallocate
, _IO_wfile_doallocate
),
1037 JUMP_INIT(read
, _IO_file_read
),
1038 JUMP_INIT(write
, _IO_new_file_write
),
1039 JUMP_INIT(seek
, _IO_file_seek
),
1040 JUMP_INIT(close
, _IO_file_close
),
1041 JUMP_INIT(stat
, _IO_file_stat
),
1042 JUMP_INIT(showmanyc
, _IO_default_showmanyc
),
1043 JUMP_INIT(imbue
, _IO_default_imbue
)
1045 libc_hidden_data_def (_IO_wfile_jumps
)
1048 const struct _IO_jump_t _IO_wfile_jumps_mmap libio_vtable
=
1051 JUMP_INIT(finish
, _IO_new_file_finish
),
1052 JUMP_INIT(overflow
, (_IO_overflow_t
) _IO_wfile_overflow
),
1053 JUMP_INIT(underflow
, (_IO_underflow_t
) _IO_wfile_underflow_mmap
),
1054 JUMP_INIT(uflow
, (_IO_underflow_t
) _IO_wdefault_uflow
),
1055 JUMP_INIT(pbackfail
, (_IO_pbackfail_t
) _IO_wdefault_pbackfail
),
1056 JUMP_INIT(xsputn
, _IO_wfile_xsputn
),
1057 JUMP_INIT(xsgetn
, _IO_file_xsgetn
),
1058 JUMP_INIT(seekoff
, _IO_wfile_seekoff
),
1059 JUMP_INIT(seekpos
, _IO_default_seekpos
),
1060 JUMP_INIT(setbuf
, _IO_file_setbuf_mmap
),
1061 JUMP_INIT(sync
, (_IO_sync_t
) _IO_wfile_sync
),
1062 JUMP_INIT(doallocate
, _IO_wfile_doallocate
),
1063 JUMP_INIT(read
, _IO_file_read
),
1064 JUMP_INIT(write
, _IO_new_file_write
),
1065 JUMP_INIT(seek
, _IO_file_seek
),
1066 JUMP_INIT(close
, _IO_file_close_mmap
),
1067 JUMP_INIT(stat
, _IO_file_stat
),
1068 JUMP_INIT(showmanyc
, _IO_default_showmanyc
),
1069 JUMP_INIT(imbue
, _IO_default_imbue
)
1072 const struct _IO_jump_t _IO_wfile_jumps_maybe_mmap libio_vtable
=
1075 JUMP_INIT(finish
, _IO_new_file_finish
),
1076 JUMP_INIT(overflow
, (_IO_overflow_t
) _IO_wfile_overflow
),
1077 JUMP_INIT(underflow
, (_IO_underflow_t
) _IO_wfile_underflow_maybe_mmap
),
1078 JUMP_INIT(uflow
, (_IO_underflow_t
) _IO_wdefault_uflow
),
1079 JUMP_INIT(pbackfail
, (_IO_pbackfail_t
) _IO_wdefault_pbackfail
),
1080 JUMP_INIT(xsputn
, _IO_wfile_xsputn
),
1081 JUMP_INIT(xsgetn
, _IO_file_xsgetn
),
1082 JUMP_INIT(seekoff
, _IO_wfile_seekoff
),
1083 JUMP_INIT(seekpos
, _IO_default_seekpos
),
1084 JUMP_INIT(setbuf
, _IO_file_setbuf_mmap
),
1085 JUMP_INIT(sync
, (_IO_sync_t
) _IO_wfile_sync
),
1086 JUMP_INIT(doallocate
, _IO_wfile_doallocate
),
1087 JUMP_INIT(read
, _IO_file_read
),
1088 JUMP_INIT(write
, _IO_new_file_write
),
1089 JUMP_INIT(seek
, _IO_file_seek
),
1090 JUMP_INIT(close
, _IO_file_close
),
1091 JUMP_INIT(stat
, _IO_file_stat
),
1092 JUMP_INIT(showmanyc
, _IO_default_showmanyc
),
1093 JUMP_INIT(imbue
, _IO_default_imbue
)