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