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