]> git.ipfire.org Git - thirdparty/glibc.git/blob - libio/wfileops.c
Update to LGPL v2.1.
[thirdparty/glibc.git] / libio / wfileops.c
1 /* Copyright (C) 1993, 95, 97, 98, 99, 2000 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>.
5
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.
10
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.
15
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.
20
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. */
29
30 #include <assert.h>
31 #include <libioP.h>
32 #include <wchar.h>
33 #include <gconv.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37
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
53
54
55 _IO_FILE *
56 _IO_wfile_setbuf (fp, p, len)
57 _IO_FILE *fp;
58 wchar_t *p;
59 _IO_ssize_t len;
60 {
61 if (_IO_wdefault_setbuf (fp, p, len) == NULL)
62 return NULL;
63
64 fp->_wide_data->_IO_write_base = fp->_wide_data->_IO_write_ptr =
65 fp->_wide_data->_IO_write_end = fp->_wide_data->_IO_buf_base;
66 _IO_wsetg (fp, fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base,
67 fp->_wide_data->_IO_buf_base);
68
69 return fp;
70 }
71
72
73 /* Convert TO_DO wide character from DATA to FP.
74 Then mark FP as having empty buffers. */
75 int
76 _IO_wdo_write (fp, data, to_do)
77 _IO_FILE *fp;
78 const wchar_t *data;
79 _IO_size_t to_do;
80 {
81 struct _IO_codecvt *cc = fp->_codecvt;
82
83 if (to_do > 0)
84 {
85 if (fp->_IO_write_end == fp->_IO_write_ptr
86 && fp->_IO_write_end != fp->_IO_write_base)
87 {
88 if (_IO_new_do_write (fp, fp->_IO_write_base,
89 fp->_IO_write_ptr - fp->_IO_write_base) == EOF)
90 return EOF;
91 }
92
93 do
94 {
95 enum __codecvt_result result;
96 const wchar_t *new_data;
97
98 /* Now convert from the internal format into the external buffer. */
99 result = (*cc->__codecvt_do_out) (cc, &fp->_wide_data->_IO_state,
100 data, data + to_do, &new_data,
101 fp->_IO_write_ptr,
102 fp->_IO_buf_end,
103 &fp->_IO_write_ptr);
104
105 /* Write out what we produced so far. */
106 if (_IO_new_do_write (fp, fp->_IO_write_base,
107 fp->_IO_write_ptr - fp->_IO_write_base) == EOF)
108 /* Something went wrong. */
109 return EOF;
110
111 to_do -= new_data - data;
112
113 /* Next see whether we had problems during the conversion. If yes,
114 we cannot go on. */
115 if (result != __codecvt_ok
116 && (result != __codecvt_partial || new_data - data == 0))
117 break;
118
119 data = new_data;
120 }
121 while (to_do > 0);
122 }
123
124 _IO_wsetg (fp, fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base,
125 fp->_wide_data->_IO_buf_base);
126 fp->_wide_data->_IO_write_base = fp->_wide_data->_IO_write_ptr
127 = fp->_wide_data->_IO_buf_base;
128 fp->_wide_data->_IO_write_end = ((fp->_flags & (_IO_LINE_BUF+_IO_UNBUFFERED))
129 ? fp->_wide_data->_IO_buf_base
130 : fp->_wide_data->_IO_buf_end);
131
132 return to_do == 0 ? 0 : WEOF;
133 }
134
135
136 wint_t
137 _IO_wfile_underflow (fp)
138 _IO_FILE *fp;
139 {
140 struct _IO_codecvt *cd;
141 enum __codecvt_result status;
142 _IO_ssize_t count;
143 int tries;
144 const char *read_ptr_copy;
145
146 if (fp->_flags & _IO_NO_READS)
147 {
148 fp->_flags |= _IO_ERR_SEEN;
149 __set_errno (EBADF);
150 return WEOF;
151 }
152 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
153 return *fp->_wide_data->_IO_read_ptr;
154
155 cd = fp->_codecvt;
156
157 /* Maybe there is something left in the external buffer. */
158 if (fp->_IO_read_ptr < fp->_IO_read_end)
159 {
160 /* Convert it. */
161 size_t avail_bytes = fp->_IO_read_end - fp->_IO_read_ptr;
162
163 if (avail_bytes >= (*cd->__codecvt_do_max_length) (cd))
164 {
165 /* There is more in the external. */
166 const char *read_stop = (const char *) fp->_IO_read_ptr;
167
168 fp->_wide_data->_IO_last_state = fp->_wide_data->_IO_state;
169 status = (*cd->__codecvt_do_in) (cd, &fp->_wide_data->_IO_state,
170 fp->_IO_read_ptr, fp->_IO_read_end,
171 &read_stop,
172 fp->_wide_data->_IO_read_end,
173 fp->_wide_data->_IO_buf_end,
174 &fp->_wide_data->_IO_read_end);
175
176 fp->_IO_read_ptr = (char *) read_stop;
177
178 /* If we managed to generate some text return the next character. */
179 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
180 return *fp->_wide_data->_IO_read_ptr;
181
182 if (status == __codecvt_error)
183 {
184 __set_errno (EILSEQ);
185 fp->_flags |= _IO_ERR_SEEN;
186 return WEOF;
187 }
188 }
189
190 /* Move the remaining content of the read buffer to the beginning. */
191 memmove (fp->_IO_buf_base, fp->_IO_read_ptr,
192 fp->_IO_read_end - fp->_IO_read_ptr);
193 fp->_IO_read_end = (fp->_IO_buf_base
194 + (fp->_IO_read_end - fp->_IO_read_ptr));
195 fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_buf_base;
196 }
197 else
198 fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_read_end =
199 fp->_IO_buf_base;
200
201 if (fp->_IO_buf_base == NULL)
202 {
203 /* Maybe we already have a push back pointer. */
204 if (fp->_IO_save_base != NULL)
205 {
206 free (fp->_IO_save_base);
207 fp->_flags &= ~_IO_IN_BACKUP;
208 }
209 _IO_doallocbuf (fp);
210
211 fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_read_end =
212 fp->_IO_buf_base;
213 }
214
215 fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end =
216 fp->_IO_buf_base;
217
218 if (fp->_wide_data->_IO_buf_base == NULL)
219 {
220 /* Maybe we already have a push back pointer. */
221 if (fp->_wide_data->_IO_save_base != NULL)
222 {
223 free (fp->_wide_data->_IO_save_base);
224 fp->_flags &= ~_IO_IN_BACKUP;
225 }
226 _IO_wdoallocbuf (fp);
227 }
228
229 /* Flush all line buffered files before reading. */
230 /* FIXME This can/should be moved to genops ?? */
231 if (fp->_flags & (_IO_LINE_BUF|_IO_UNBUFFERED))
232 _IO_flush_all_linebuffered ();
233
234 _IO_switch_to_get_mode (fp);
235
236 fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_read_ptr =
237 fp->_wide_data->_IO_buf_base;
238 fp->_wide_data->_IO_read_end = fp->_wide_data->_IO_buf_base;
239 fp->_wide_data->_IO_write_base = fp->_wide_data->_IO_write_ptr =
240 fp->_wide_data->_IO_write_end = fp->_wide_data->_IO_buf_base;
241
242 tries = 0;
243 again:
244 count = _IO_SYSREAD (fp, fp->_IO_read_end,
245 fp->_IO_buf_end - fp->_IO_read_end);
246 if (count <= 0)
247 {
248 if (count == 0 && tries == 0)
249 fp->_flags |= _IO_EOF_SEEN;
250 else
251 fp->_flags |= _IO_ERR_SEEN, count = 0;
252 }
253 fp->_IO_read_end += count;
254 if (count == 0)
255 {
256 if (tries != 0)
257 /* There are some bytes in the external buffer but they don't
258 convert to anything. */
259 __set_errno (EILSEQ);
260 return WEOF;
261 }
262 if (fp->_offset != _IO_pos_BAD)
263 _IO_pos_adjust (fp->_offset, count);
264
265 /* Now convert the read input. */
266 fp->_wide_data->_IO_last_state = fp->_wide_data->_IO_state;
267 fp->_IO_read_base = fp->_IO_read_ptr;
268 status = (*cd->__codecvt_do_in) (cd, &fp->_wide_data->_IO_state,
269 fp->_IO_read_ptr, fp->_IO_read_end,
270 &read_ptr_copy,
271 fp->_wide_data->_IO_read_end,
272 fp->_wide_data->_IO_buf_end,
273 &fp->_wide_data->_IO_read_end);
274
275 fp->_IO_read_ptr = (char *) read_ptr_copy;
276 if (fp->_wide_data->_IO_read_end == fp->_wide_data->_IO_buf_base)
277 {
278 if (status == __codecvt_error || fp->_IO_read_end == fp->_IO_buf_end)
279 {
280 __set_errno (EILSEQ);
281 fp->_flags |= _IO_ERR_SEEN;
282 return WEOF;
283 }
284
285 /* The read bytes make no complete character. Try reading again. */
286 assert (status == __codecvt_partial);
287 ++tries;
288 goto again;
289 }
290
291 return *fp->_wide_data->_IO_read_ptr;
292 }
293
294
295 wint_t
296 _IO_wfile_overflow (f, wch)
297 _IO_FILE *f;
298 wint_t wch;
299 {
300 if (f->_flags & _IO_NO_WRITES) /* SET ERROR */
301 {
302 f->_flags |= _IO_ERR_SEEN;
303 __set_errno (EBADF);
304 return WEOF;
305 }
306 /* If currently reading or no buffer allocated. */
307 if ((f->_flags & _IO_CURRENTLY_PUTTING) == 0)
308 {
309 /* Allocate a buffer if needed. */
310 if (f->_wide_data->_IO_write_base == 0)
311 {
312 _IO_wdoallocbuf (f);
313 _IO_wsetg (f, f->_wide_data->_IO_buf_base,
314 f->_wide_data->_IO_buf_base, f->_wide_data->_IO_buf_base);
315
316 if (f->_IO_write_base == NULL)
317 {
318 _IO_doallocbuf (f);
319 _IO_setg (f, f->_IO_buf_base, f->_IO_buf_base, f->_IO_buf_base);
320 }
321 }
322 else
323 {
324 /* Otherwise must be currently reading. If _IO_read_ptr
325 (and hence also _IO_read_end) is at the buffer end,
326 logically slide the buffer forwards one block (by setting
327 the read pointers to all point at the beginning of the
328 block). This makes room for subsequent output.
329 Otherwise, set the read pointers to _IO_read_end (leaving
330 that alone, so it can continue to correspond to the
331 external position). */
332 if (f->_wide_data->_IO_read_ptr == f->_wide_data->_IO_buf_end)
333 {
334 f->_IO_read_end = f->_IO_read_ptr = f->_IO_buf_base;
335 f->_wide_data->_IO_read_end = f->_wide_data->_IO_read_ptr =
336 f->_wide_data->_IO_buf_base;
337 }
338 }
339 f->_wide_data->_IO_write_ptr = f->_wide_data->_IO_read_ptr;
340 f->_wide_data->_IO_write_base = f->_wide_data->_IO_write_ptr;
341 f->_wide_data->_IO_write_end = f->_wide_data->_IO_buf_end;
342 f->_wide_data->_IO_read_base = f->_wide_data->_IO_read_ptr =
343 f->_wide_data->_IO_read_end;
344
345 f->_IO_write_ptr = f->_IO_read_ptr;
346 f->_IO_write_base = f->_IO_write_ptr;
347 f->_IO_write_end = f->_IO_buf_end;
348 f->_IO_read_base = f->_IO_read_ptr = f->_IO_read_end;
349
350 f->_flags |= _IO_CURRENTLY_PUTTING;
351 if (f->_flags & (_IO_LINE_BUF+_IO_UNBUFFERED))
352 f->_wide_data->_IO_write_end = f->_wide_data->_IO_write_ptr;
353 }
354 if (wch == WEOF)
355 return _IO_do_flush (f);
356 if (f->_wide_data->_IO_write_ptr == f->_wide_data->_IO_buf_end)
357 /* Buffer is really full */
358 if (_IO_do_flush (f) == WEOF)
359 return WEOF;
360 *f->_wide_data->_IO_write_ptr++ = wch;
361 if ((f->_flags & _IO_UNBUFFERED)
362 || ((f->_flags & _IO_LINE_BUF) && wch == L'\n'))
363 if (_IO_do_flush (f) == WEOF)
364 return WEOF;
365 return wch;
366 }
367
368 wint_t
369 _IO_wfile_sync (fp)
370 _IO_FILE *fp;
371 {
372 _IO_ssize_t delta;
373 wint_t retval = 0;
374
375 /* char* ptr = cur_ptr(); */
376 if (fp->_wide_data->_IO_write_ptr > fp->_wide_data->_IO_write_base)
377 if (_IO_do_flush (fp))
378 return WEOF;
379 delta = fp->_wide_data->_IO_read_ptr - fp->_wide_data->_IO_read_end;
380 if (delta != 0)
381 {
382 /* We have to find out how many bytes we have to go back in the
383 external buffer. */
384 struct _IO_codecvt *cv = fp->_codecvt;
385 _IO_off64_t new_pos;
386
387 int clen = (*cv->__codecvt_do_encoding) (cv);
388
389 if (clen > 0)
390 /* It is easy, a fixed number of input bytes are used for each
391 wide character. */
392 delta *= clen;
393 else
394 {
395 /* We have to find out the hard way how much to back off.
396 To do this we determine how much input we needed to
397 generate the wide characters up to the current reading
398 position. */
399 int nread;
400
401 fp->_wide_data->_IO_state = fp->_wide_data->_IO_last_state;
402 nread = (*cv->__codecvt_do_length) (cv, &fp->_wide_data->_IO_state,
403 fp->_IO_read_base,
404 fp->_IO_read_end, delta);
405 fp->_IO_read_ptr = fp->_IO_read_base + nread;
406 delta = -(fp->_IO_read_end - fp->_IO_read_base - nread);
407 }
408
409 new_pos = _IO_SYSSEEK (fp, delta, 1);
410 if (new_pos != (_IO_off64_t) EOF)
411 {
412 fp->_wide_data->_IO_read_end = fp->_wide_data->_IO_read_ptr;
413 fp->_IO_read_end = fp->_IO_read_ptr;
414 }
415 #ifdef ESPIPE
416 else if (errno == ESPIPE)
417 ; /* Ignore error from unseekable devices. */
418 #endif
419 else
420 retval = WEOF;
421 }
422 if (retval != WEOF)
423 fp->_offset = _IO_pos_BAD;
424 /* FIXME: Cleanup - can this be shared? */
425 /* setg(base(), ptr, ptr); */
426 return retval;
427 }
428
429 _IO_off64_t
430 _IO_wfile_seekoff (fp, offset, dir, mode)
431 _IO_FILE *fp;
432 _IO_off64_t offset;
433 int dir;
434 int mode;
435 {
436 _IO_off64_t result;
437 _IO_off64_t delta, new_offset;
438 long int count;
439 /* POSIX.1 8.2.3.7 says that after a call the fflush() the file
440 offset of the underlying file must be exact. */
441 int must_be_exact = ((fp->_wide_data->_IO_read_base
442 == fp->_wide_data->_IO_read_end)
443 && (fp->_wide_data->_IO_write_base
444 == fp->_wide_data->_IO_write_ptr));
445
446 if (mode == 0)
447 dir = _IO_seek_cur, offset = 0; /* Don't move any pointers. */
448
449 /* Flush unwritten characters.
450 (This may do an unneeded write if we seek within the buffer.
451 But to be able to switch to reading, we would need to set
452 egptr to ptr. That can't be done in the current design,
453 which assumes file_ptr() is eGptr. Anyway, since we probably
454 end up flushing when we close(), it doesn't make much difference.)
455 FIXME: simulate mem-papped files. */
456
457 if (fp->_wide_data->_IO_write_ptr > fp->_wide_data->_IO_write_base
458 || _IO_in_put_mode (fp))
459 if (_IO_switch_to_wget_mode (fp))
460 return WEOF;
461
462 if (fp->_wide_data->_IO_buf_base == NULL)
463 {
464 /* It could be that we already have a pushback buffer. */
465 if (fp->_wide_data->_IO_read_base != NULL)
466 {
467 free (fp->_wide_data->_IO_read_base);
468 fp->_flags &= ~_IO_IN_BACKUP;
469 }
470 _IO_doallocbuf (fp);
471 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
472 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
473 _IO_wsetp (fp, fp->_wide_data->_IO_buf_base,
474 fp->_wide_data->_IO_buf_base);
475 _IO_wsetg (fp, fp->_wide_data->_IO_buf_base,
476 fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base);
477 }
478
479 switch (dir)
480 {
481 struct _IO_codecvt *cv;
482 int clen;
483
484 case _IO_seek_cur:
485 /* Adjust for read-ahead (bytes is buffer). To do this we must
486 find out which position in the external buffer corresponds to
487 the current position in the internal buffer. */
488 cv = fp->_codecvt;
489 clen = (*cv->__codecvt_do_encoding) (cv);
490
491 if (clen > 0)
492 offset -= (fp->_wide_data->_IO_read_end
493 - fp->_wide_data->_IO_read_ptr) * clen;
494 else
495 {
496 int nread;
497
498 delta = fp->_wide_data->_IO_read_ptr - fp->_wide_data->_IO_read_end;
499 fp->_wide_data->_IO_state = fp->_wide_data->_IO_last_state;
500 nread = (*cv->__codecvt_do_length) (cv, &fp->_wide_data->_IO_state,
501 fp->_IO_read_base,
502 fp->_IO_read_end, delta);
503 fp->_IO_read_ptr = fp->_IO_read_base + nread;
504 offset -= fp->_IO_read_end - fp->_IO_read_base - nread;
505 }
506
507 if (fp->_offset == _IO_pos_BAD)
508 goto dumb;
509 /* Make offset absolute, assuming current pointer is file_ptr(). */
510 offset += fp->_offset;
511
512 dir = _IO_seek_set;
513 break;
514 case _IO_seek_set:
515 break;
516 case _IO_seek_end:
517 {
518 struct _G_stat64 st;
519 if (_IO_SYSSTAT (fp, &st) == 0 && S_ISREG (st.st_mode))
520 {
521 offset += st.st_size;
522 dir = _IO_seek_set;
523 }
524 else
525 goto dumb;
526 }
527 }
528 /* At this point, dir==_IO_seek_set. */
529
530 /* If we are only interested in the current position we've found it now. */
531 if (mode == 0)
532 return offset;
533
534 /* If destination is within current buffer, optimize: */
535 if (fp->_offset != _IO_pos_BAD && fp->_IO_read_base != NULL
536 && !_IO_in_backup (fp))
537 {
538 /* Offset relative to start of main get area. */
539 _IO_off64_t rel_offset = (offset - fp->_offset
540 + (fp->_IO_read_end - fp->_IO_read_base));
541 if (rel_offset >= 0)
542 {
543 #if 0
544 if (_IO_in_backup (fp))
545 _IO_switch_to_main_get_area (fp);
546 #endif
547 if (rel_offset <= fp->_IO_read_end - fp->_IO_read_base)
548 {
549 fp->_IO_read_ptr = fp->_IO_read_base + rel_offset;
550 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
551
552 /* Now set the pointer for the internal buffer. This
553 might be an iterative process. Though the read
554 pointer is somewhere in the current external buffer
555 this does not mean we can convert this whole buffer
556 at once fitting in the internal buffer. */
557 do
558 {
559
560 }
561 while (0);
562
563 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
564 goto resync;
565 }
566 #ifdef TODO
567 /* If we have streammarkers, seek forward by reading ahead. */
568 if (_IO_have_markers (fp))
569 {
570 int to_skip = rel_offset
571 - (fp->_IO_read_ptr - fp->_IO_read_base);
572 if (ignore (to_skip) != to_skip)
573 goto dumb;
574 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
575 goto resync;
576 }
577 #endif
578 }
579 #ifdef TODO
580 if (rel_offset < 0 && rel_offset >= Bbase () - Bptr ())
581 {
582 if (!_IO_in_backup (fp))
583 _IO_switch_to_backup_area (fp);
584 gbump (fp->_IO_read_end + rel_offset - fp->_IO_read_ptr);
585 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
586 goto resync;
587 }
588 #endif
589 }
590
591 #ifdef TODO
592 _IO_unsave_markers (fp);
593 #endif
594
595 if (fp->_flags & _IO_NO_READS)
596 goto dumb;
597
598 /* Try to seek to a block boundary, to improve kernel page management. */
599 new_offset = offset & ~(fp->_IO_buf_end - fp->_IO_buf_base - 1);
600 delta = offset - new_offset;
601 if (delta > fp->_IO_buf_end - fp->_IO_buf_base)
602 {
603 new_offset = offset;
604 delta = 0;
605 }
606 result = _IO_SYSSEEK (fp, new_offset, 0);
607 if (result < 0)
608 return EOF;
609 if (delta == 0)
610 count = 0;
611 else
612 {
613 count = _IO_SYSREAD (fp, fp->_IO_buf_base,
614 (must_be_exact
615 ? delta : fp->_IO_buf_end - fp->_IO_buf_base));
616 if (count < delta)
617 {
618 /* We weren't allowed to read, but try to seek the remainder. */
619 offset = count == EOF ? delta : delta-count;
620 dir = _IO_seek_cur;
621 goto dumb;
622 }
623 }
624 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base + delta,
625 fp->_IO_buf_base + count);
626 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
627 fp->_offset = result + count;
628 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
629 return offset;
630 dumb:
631
632 _IO_unsave_markers (fp);
633 result = _IO_SYSSEEK (fp, offset, dir);
634 if (result != EOF)
635 {
636 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
637 fp->_offset = result;
638 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
639 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
640 }
641 return result;
642
643 resync:
644 /* We need to do it since it is possible that the file offset in
645 the kernel may be changed behind our back. It may happen when
646 we fopen a file and then do a fork. One process may access the
647 the file and the kernel file offset will be changed. */
648 if (fp->_offset >= 0)
649 _IO_SYSSEEK (fp, fp->_offset, 0);
650
651 return offset;
652 }
653
654
655 _IO_size_t
656 _IO_wfile_xsputn (f, data, n)
657 _IO_FILE *f;
658 const void *data;
659 _IO_size_t n;
660 {
661 register const wchar_t *s = (const wchar_t *) data;
662 _IO_size_t to_do = n;
663 int must_flush = 0;
664 _IO_size_t count;
665
666 if (n <= 0)
667 return 0;
668 /* This is an optimized implementation.
669 If the amount to be written straddles a block boundary
670 (or the filebuf is unbuffered), use sys_write directly. */
671
672 /* First figure out how much space is available in the buffer. */
673 count = f->_wide_data->_IO_write_end - f->_wide_data->_IO_write_ptr;
674 if ((f->_flags & _IO_LINE_BUF) && (f->_flags & _IO_CURRENTLY_PUTTING))
675 {
676 count = f->_wide_data->_IO_buf_end - f->_wide_data->_IO_write_ptr;
677 if (count >= n)
678 {
679 register const wchar_t *p;
680 for (p = s + n; p > s; )
681 {
682 if (*--p == L'\n')
683 {
684 count = p - s + 1;
685 must_flush = 1;
686 break;
687 }
688 }
689 }
690 }
691 /* Then fill the buffer. */
692 if (count > 0)
693 {
694 if (count > to_do)
695 count = to_do;
696 if (count > 20)
697 {
698 #ifdef _LIBC
699 f->_wide_data->_IO_write_ptr =
700 __wmempcpy (f->_wide_data->_IO_write_ptr, s, count);
701 #else
702 wmemcpy (f->_wide_data->_IO_write_ptr, s, count);
703 f->_wide_data->_IO_write_ptr += count;
704 #endif
705 s += count;
706 }
707 else
708 {
709 register wchar_t *p = f->_wide_data->_IO_write_ptr;
710 register int i = (int) count;
711 while (--i >= 0)
712 *p++ = *s++;
713 f->_wide_data->_IO_write_ptr = p;
714 }
715 to_do -= count;
716 }
717 if (to_do > 0)
718 to_do -= _IO_wdefault_xsputn (f, s, to_do);
719 if (must_flush
720 && f->_wide_data->_IO_write_ptr != f->_wide_data->_IO_write_base)
721 _IO_wdo_write (f, f->_wide_data->_IO_write_base,
722 f->_wide_data->_IO_write_ptr
723 - f->_wide_data->_IO_write_base);
724
725 return n - to_do;
726 }
727
728
729 struct _IO_jump_t _IO_wfile_jumps =
730 {
731 JUMP_INIT_DUMMY,
732 JUMP_INIT(finish, _IO_new_file_finish),
733 JUMP_INIT(overflow, (_IO_overflow_t) _IO_wfile_overflow),
734 JUMP_INIT(underflow, (_IO_underflow_t) _IO_wfile_underflow),
735 JUMP_INIT(uflow, (_IO_underflow_t) _IO_wdefault_uflow),
736 JUMP_INIT(pbackfail, (_IO_pbackfail_t) _IO_wdefault_pbackfail),
737 JUMP_INIT(xsputn, _IO_wfile_xsputn),
738 JUMP_INIT(xsgetn, _IO_file_xsgetn),
739 JUMP_INIT(seekoff, _IO_wfile_seekoff),
740 JUMP_INIT(seekpos, _IO_default_seekpos),
741 JUMP_INIT(setbuf, _IO_new_file_setbuf),
742 JUMP_INIT(sync, (_IO_sync_t) _IO_wfile_sync),
743 JUMP_INIT(doallocate, _IO_wfile_doallocate),
744 JUMP_INIT(read, _IO_file_read),
745 JUMP_INIT(write, _IO_new_file_write),
746 JUMP_INIT(seek, _IO_file_seek),
747 JUMP_INIT(close, _IO_file_close),
748 JUMP_INIT(stat, _IO_file_stat),
749 JUMP_INIT(showmanyc, _IO_default_showmanyc),
750 JUMP_INIT(imbue, _IO_default_imbue)
751 };