Hide internal __gettextparse function [BZ #18822]
[glibc.git] / libio / oldfileops.c
blob671b9822f094844788e4d7f3109f6e97f4be4713
1 /* Copyright (C) 1993-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Written by Per Bothner <bothner@cygnus.com>.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>.
19 As a special exception, if you link the code in this file with
20 files compiled with a GNU compiler to produce an executable,
21 that does not cause the resulting executable to be covered by
22 the GNU Lesser General Public License. This exception does not
23 however invalidate any other reasons why the executable file
24 might be covered by the GNU Lesser General Public License.
25 This exception applies to code released by its copyright holders
26 in files containing the exception. */
28 /* This is a compatibility file. If we don't build the libc with
29 versioning don't compile this file. */
30 #include <shlib-compat.h>
31 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
33 #define _IO_USE_OLD_IO_FILE
34 #include "libioP.h"
35 #include <fcntl.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <stdlib.h>
41 #include <unistd.h>
43 /* An fstream can be in at most one of put mode, get mode, or putback mode.
44 Putback mode is a variant of get mode.
46 In a filebuf, there is only one current position, instead of two
47 separate get and put pointers. In get mode, the current position
48 is that of gptr(); in put mode that of pptr().
50 The position in the buffer that corresponds to the position
51 in external file system is normally _IO_read_end, except in putback
52 mode, when it is _IO_save_end.
53 If the field _fb._offset is >= 0, it gives the offset in
54 the file as a whole corresponding to eGptr(). (?)
56 PUT MODE:
57 If a filebuf is in put mode, then all of _IO_read_ptr, _IO_read_end,
58 and _IO_read_base are equal to each other. These are usually equal
59 to _IO_buf_base, though not necessarily if we have switched from
60 get mode to put mode. (The reason is to maintain the invariant
61 that _IO_read_end corresponds to the external file position.)
62 _IO_write_base is non-NULL and usually equal to _IO_buf_base.
63 We also have _IO_write_end == _IO_buf_end, but only in fully buffered mode.
64 The un-flushed character are those between _IO_write_base and _IO_write_ptr.
66 GET MODE:
67 If a filebuf is in get or putback mode, eback() != egptr().
68 In get mode, the unread characters are between gptr() and egptr().
69 The OS file position corresponds to that of egptr().
71 PUTBACK MODE:
72 Putback mode is used to remember "excess" characters that have
73 been sputbackc'd in a separate putback buffer.
74 In putback mode, the get buffer points to the special putback buffer.
75 The unread characters are the characters between gptr() and egptr()
76 in the putback buffer, as well as the area between save_gptr()
77 and save_egptr(), which point into the original reserve buffer.
78 (The pointers save_gptr() and save_egptr() are the values
79 of gptr() and egptr() at the time putback mode was entered.)
80 The OS position corresponds to that of save_egptr().
82 LINE BUFFERED OUTPUT:
83 During line buffered output, _IO_write_base==base() && epptr()==base().
84 However, ptr() may be anywhere between base() and ebuf().
85 This forces a call to filebuf::overflow(int C) on every put.
86 If there is more space in the buffer, and C is not a '\n',
87 then C is inserted, and pptr() incremented.
89 UNBUFFERED STREAMS:
90 If a filebuf is unbuffered(), the _shortbuf[1] is used as the buffer.
93 #define CLOSED_FILEBUF_FLAGS \
94 (_IO_IS_FILEBUF+_IO_NO_READS+_IO_NO_WRITES+_IO_TIED_PUT_GET)
97 void
98 attribute_compat_text_section
99 _IO_old_file_init_internal (struct _IO_FILE_plus *fp)
101 /* POSIX.1 allows another file handle to be used to change the position
102 of our file descriptor. Hence we actually don't know the actual
103 position before we do the first fseek (and until a following fflush). */
104 fp->file._old_offset = _IO_pos_BAD;
105 fp->file._IO_file_flags |= CLOSED_FILEBUF_FLAGS;
107 _IO_link_in (fp);
108 fp->file._vtable_offset = ((int) sizeof (struct _IO_FILE)
109 - (int) sizeof (struct _IO_FILE_complete));
110 fp->file._fileno = -1;
112 if (__builtin_expect (&_IO_stdin_used != NULL, 1)
113 || (fp != (struct _IO_FILE_plus *) _IO_stdin
114 && fp != (struct _IO_FILE_plus *) _IO_stdout
115 && fp != (struct _IO_FILE_plus *) _IO_stderr))
116 /* The object is dynamically allocated and large enough. Initialize
117 the _mode element as well. */
118 ((struct _IO_FILE_complete *) fp)->_mode = -1;
121 void
122 attribute_compat_text_section
123 _IO_old_file_init (struct _IO_FILE_plus *fp)
125 IO_set_accept_foreign_vtables (&_IO_vtable_check);
126 _IO_old_file_init_internal (fp);
130 attribute_compat_text_section
131 _IO_old_file_close_it (_IO_FILE *fp)
133 int write_status, close_status;
134 if (!_IO_file_is_open (fp))
135 return EOF;
137 write_status = _IO_old_do_flush (fp);
139 _IO_unsave_markers (fp);
141 close_status = ((fp->_flags2 & _IO_FLAGS2_NOCLOSE) == 0
142 ? _IO_SYSCLOSE (fp) : 0);
144 /* Free buffer. */
145 _IO_setb (fp, NULL, NULL, 0);
146 _IO_setg (fp, NULL, NULL, NULL);
147 _IO_setp (fp, NULL, NULL);
149 _IO_un_link ((struct _IO_FILE_plus *) fp);
150 fp->_flags = _IO_MAGIC|CLOSED_FILEBUF_FLAGS;
151 fp->_fileno = -1;
152 fp->_old_offset = _IO_pos_BAD;
154 return close_status ? close_status : write_status;
157 void
158 attribute_compat_text_section
159 _IO_old_file_finish (_IO_FILE *fp, int dummy)
161 if (_IO_file_is_open (fp))
163 _IO_old_do_flush (fp);
164 if (!(fp->_flags & _IO_DELETE_DONT_CLOSE))
165 _IO_SYSCLOSE (fp);
167 _IO_default_finish (fp, 0);
170 _IO_FILE *
171 attribute_compat_text_section
172 _IO_old_file_fopen (_IO_FILE *fp, const char *filename, const char *mode)
174 int oflags = 0, omode;
175 int read_write, fdesc;
176 int oprot = 0666;
177 if (_IO_file_is_open (fp))
178 return 0;
179 switch (*mode++)
181 case 'r':
182 omode = O_RDONLY;
183 read_write = _IO_NO_WRITES;
184 break;
185 case 'w':
186 omode = O_WRONLY;
187 oflags = O_CREAT|O_TRUNC;
188 read_write = _IO_NO_READS;
189 break;
190 case 'a':
191 omode = O_WRONLY;
192 oflags = O_CREAT|O_APPEND;
193 read_write = _IO_NO_READS|_IO_IS_APPENDING;
194 break;
195 default:
196 __set_errno (EINVAL);
197 return NULL;
199 if (mode[0] == '+' || (mode[0] == 'b' && mode[1] == '+'))
201 omode = O_RDWR;
202 read_write &= _IO_IS_APPENDING;
204 fdesc = __open (filename, omode|oflags, oprot);
205 if (fdesc < 0)
206 return NULL;
207 fp->_fileno = fdesc;
208 _IO_mask_flags (fp, read_write,_IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
209 if (read_write & _IO_IS_APPENDING)
210 if (_IO_SEEKOFF (fp, (_IO_off_t)0, _IO_seek_end, _IOS_INPUT|_IOS_OUTPUT)
211 == _IO_pos_BAD && errno != ESPIPE)
212 return NULL;
213 _IO_link_in ((struct _IO_FILE_plus *) fp);
214 return fp;
217 _IO_FILE *
218 attribute_compat_text_section
219 _IO_old_file_attach (_IO_FILE *fp, int fd)
221 if (_IO_file_is_open (fp))
222 return NULL;
223 fp->_fileno = fd;
224 fp->_flags &= ~(_IO_NO_READS+_IO_NO_WRITES);
225 fp->_flags |= _IO_DELETE_DONT_CLOSE;
226 /* Get the current position of the file. */
227 /* We have to do that since that may be junk. */
228 fp->_old_offset = _IO_pos_BAD;
229 if (_IO_SEEKOFF (fp, (_IO_off_t)0, _IO_seek_cur, _IOS_INPUT|_IOS_OUTPUT)
230 == _IO_pos_BAD && errno != ESPIPE)
231 return NULL;
232 return fp;
235 _IO_FILE *
236 attribute_compat_text_section
237 _IO_old_file_setbuf (_IO_FILE *fp, char *p, _IO_ssize_t len)
239 if (_IO_default_setbuf (fp, p, len) == NULL)
240 return NULL;
242 fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end
243 = fp->_IO_buf_base;
244 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
246 return fp;
249 static int old_do_write (_IO_FILE *, const char *, _IO_size_t);
251 /* Write TO_DO bytes from DATA to FP.
252 Then mark FP as having empty buffers. */
255 attribute_compat_text_section
256 _IO_old_do_write (_IO_FILE *fp, const char *data, _IO_size_t to_do)
258 return (to_do == 0 || (_IO_size_t) old_do_write (fp, data, to_do) == to_do)
259 ? 0 : EOF;
262 static int
263 attribute_compat_text_section
264 old_do_write (_IO_FILE *fp, const char *data, _IO_size_t to_do)
266 _IO_size_t count;
267 if (fp->_flags & _IO_IS_APPENDING)
268 /* On a system without a proper O_APPEND implementation,
269 you would need to sys_seek(0, SEEK_END) here, but is
270 not needed nor desirable for Unix- or Posix-like systems.
271 Instead, just indicate that offset (before and after) is
272 unpredictable. */
273 fp->_old_offset = _IO_pos_BAD;
274 else if (fp->_IO_read_end != fp->_IO_write_base)
276 _IO_off_t new_pos
277 = _IO_SYSSEEK (fp, fp->_IO_write_base - fp->_IO_read_end, 1);
278 if (new_pos == _IO_pos_BAD)
279 return 0;
280 fp->_old_offset = new_pos;
282 count = _IO_SYSWRITE (fp, data, to_do);
283 if (fp->_cur_column && count)
284 fp->_cur_column = _IO_adjust_column (fp->_cur_column - 1, data, count) + 1;
285 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
286 fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_buf_base;
287 fp->_IO_write_end = ((fp->_flags & (_IO_LINE_BUF | _IO_UNBUFFERED))
288 ? fp->_IO_buf_base : fp->_IO_buf_end);
289 return count;
293 attribute_compat_text_section
294 _IO_old_file_underflow (_IO_FILE *fp)
296 _IO_ssize_t count;
297 #if 0
298 /* SysV does not make this test; take it out for compatibility */
299 if (fp->_flags & _IO_EOF_SEEN)
300 return (EOF);
301 #endif
303 if (fp->_flags & _IO_NO_READS)
305 fp->_flags |= _IO_ERR_SEEN;
306 __set_errno (EBADF);
307 return EOF;
309 if (fp->_IO_read_ptr < fp->_IO_read_end)
310 return *(unsigned char *) fp->_IO_read_ptr;
312 if (fp->_IO_buf_base == NULL)
314 /* Maybe we already have a push back pointer. */
315 if (fp->_IO_save_base != NULL)
317 free (fp->_IO_save_base);
318 fp->_flags &= ~_IO_IN_BACKUP;
320 _IO_doallocbuf (fp);
323 /* Flush all line buffered files before reading. */
324 /* FIXME This can/should be moved to genops ?? */
325 if (fp->_flags & (_IO_LINE_BUF|_IO_UNBUFFERED))
326 _IO_flush_all_linebuffered ();
328 _IO_switch_to_get_mode (fp);
330 /* This is very tricky. We have to adjust those
331 pointers before we call _IO_SYSREAD () since
332 we may longjump () out while waiting for
333 input. Those pointers may be screwed up. H.J. */
334 fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_buf_base;
335 fp->_IO_read_end = fp->_IO_buf_base;
336 fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end
337 = fp->_IO_buf_base;
339 count = _IO_SYSREAD (fp, fp->_IO_buf_base,
340 fp->_IO_buf_end - fp->_IO_buf_base);
341 if (count <= 0)
343 if (count == 0)
344 fp->_flags |= _IO_EOF_SEEN;
345 else
346 fp->_flags |= _IO_ERR_SEEN, count = 0;
348 fp->_IO_read_end += count;
349 if (count == 0)
350 return EOF;
351 if (fp->_old_offset != _IO_pos_BAD)
352 _IO_pos_adjust (fp->_old_offset, count);
353 return *(unsigned char *) fp->_IO_read_ptr;
357 attribute_compat_text_section
358 _IO_old_file_overflow (_IO_FILE *f, int ch)
360 if (f->_flags & _IO_NO_WRITES) /* SET ERROR */
362 f->_flags |= _IO_ERR_SEEN;
363 __set_errno (EBADF);
364 return EOF;
366 /* If currently reading or no buffer allocated. */
367 if ((f->_flags & _IO_CURRENTLY_PUTTING) == 0)
369 /* Allocate a buffer if needed. */
370 if (f->_IO_write_base == 0)
372 _IO_doallocbuf (f);
373 _IO_setg (f, f->_IO_buf_base, f->_IO_buf_base, f->_IO_buf_base);
375 /* Otherwise must be currently reading.
376 If _IO_read_ptr (and hence also _IO_read_end) is at the buffer end,
377 logically slide the buffer forwards one block (by setting the
378 read pointers to all point at the beginning of the block). This
379 makes room for subsequent output.
380 Otherwise, set the read pointers to _IO_read_end (leaving that
381 alone, so it can continue to correspond to the external position). */
382 if (f->_IO_read_ptr == f->_IO_buf_end)
383 f->_IO_read_end = f->_IO_read_ptr = f->_IO_buf_base;
384 f->_IO_write_ptr = f->_IO_read_ptr;
385 f->_IO_write_base = f->_IO_write_ptr;
386 f->_IO_write_end = f->_IO_buf_end;
387 f->_IO_read_base = f->_IO_read_ptr = f->_IO_read_end;
389 if (f->_flags & (_IO_LINE_BUF | _IO_UNBUFFERED))
390 f->_IO_write_end = f->_IO_write_ptr;
391 f->_flags |= _IO_CURRENTLY_PUTTING;
393 if (ch == EOF)
394 return _IO_old_do_flush (f);
395 if (f->_IO_write_ptr == f->_IO_buf_end ) /* Buffer is really full */
396 if (_IO_old_do_flush (f) == EOF)
397 return EOF;
398 *f->_IO_write_ptr++ = ch;
399 if ((f->_flags & _IO_UNBUFFERED)
400 || ((f->_flags & _IO_LINE_BUF) && ch == '\n'))
401 if (_IO_old_do_flush (f) == EOF)
402 return EOF;
403 return (unsigned char) ch;
407 attribute_compat_text_section
408 _IO_old_file_sync (_IO_FILE *fp)
410 _IO_ssize_t delta;
411 int retval = 0;
413 /* char* ptr = cur_ptr(); */
414 if (fp->_IO_write_ptr > fp->_IO_write_base)
415 if (_IO_old_do_flush(fp)) return EOF;
416 delta = fp->_IO_read_ptr - fp->_IO_read_end;
417 if (delta != 0)
419 #ifdef TODO
420 if (_IO_in_backup (fp))
421 delta -= eGptr () - Gbase ();
422 #endif
423 _IO_off_t new_pos = _IO_SYSSEEK (fp, delta, 1);
424 if (new_pos != (_IO_off_t) EOF)
425 fp->_IO_read_end = fp->_IO_read_ptr;
426 else if (errno == ESPIPE)
427 ; /* Ignore error from unseekable devices. */
428 else
429 retval = EOF;
431 if (retval != EOF)
432 fp->_old_offset = _IO_pos_BAD;
433 /* FIXME: Cleanup - can this be shared? */
434 /* setg(base(), ptr, ptr); */
435 return retval;
438 _IO_off64_t
439 attribute_compat_text_section
440 _IO_old_file_seekoff (_IO_FILE *fp, _IO_off64_t offset, int dir, int mode)
442 _IO_off_t result;
443 _IO_off64_t delta, new_offset;
444 long count;
445 /* POSIX.1 8.2.3.7 says that after a call the fflush() the file
446 offset of the underlying file must be exact. */
447 int must_be_exact = (fp->_IO_read_base == fp->_IO_read_end
448 && fp->_IO_write_base == fp->_IO_write_ptr);
450 if (mode == 0)
451 dir = _IO_seek_cur, offset = 0; /* Don't move any pointers. */
453 /* Flush unwritten characters.
454 (This may do an unneeded write if we seek within the buffer.
455 But to be able to switch to reading, we would need to set
456 egptr to pptr. That can't be done in the current design,
457 which assumes file_ptr() is eGptr. Anyway, since we probably
458 end up flushing when we close(), it doesn't make much difference.)
459 FIXME: simulate mem-mapped files. */
461 if (fp->_IO_write_ptr > fp->_IO_write_base || _IO_in_put_mode (fp))
462 if (_IO_switch_to_get_mode (fp))
463 return EOF;
465 if (fp->_IO_buf_base == NULL)
467 /* It could be that we already have a pushback buffer. */
468 if (fp->_IO_read_base != NULL)
470 free (fp->_IO_read_base);
471 fp->_flags &= ~_IO_IN_BACKUP;
473 _IO_doallocbuf (fp);
474 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
475 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
478 switch (dir)
480 case _IO_seek_cur:
481 /* Adjust for read-ahead (bytes is buffer). */
482 offset -= fp->_IO_read_end - fp->_IO_read_ptr;
483 if (fp->_old_offset == _IO_pos_BAD)
484 goto dumb;
485 /* Make offset absolute, assuming current pointer is file_ptr(). */
486 offset += fp->_old_offset;
488 dir = _IO_seek_set;
489 break;
490 case _IO_seek_set:
491 break;
492 case _IO_seek_end:
494 struct stat64 st;
495 if (_IO_SYSSTAT (fp, &st) == 0 && S_ISREG (st.st_mode))
497 offset += st.st_size;
498 dir = _IO_seek_set;
500 else
501 goto dumb;
504 /* At this point, dir==_IO_seek_set. */
506 /* If we are only interested in the current position we've found it now. */
507 if (mode == 0)
508 return offset;
510 /* If destination is within current buffer, optimize: */
511 if (fp->_old_offset != _IO_pos_BAD && fp->_IO_read_base != NULL
512 && !_IO_in_backup (fp))
514 /* Offset relative to start of main get area. */
515 _IO_off_t rel_offset = (offset - fp->_old_offset
516 + (fp->_IO_read_end - fp->_IO_read_base));
517 if (rel_offset >= 0)
519 #if 0
520 if (_IO_in_backup (fp))
521 _IO_switch_to_main_get_area (fp);
522 #endif
523 if (rel_offset <= fp->_IO_read_end - fp->_IO_read_base)
525 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base + rel_offset,
526 fp->_IO_read_end);
527 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
529 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
530 goto resync;
533 #ifdef TODO
534 /* If we have streammarkers, seek forward by reading ahead. */
535 if (_IO_have_markers (fp))
537 int to_skip = rel_offset
538 - (fp->_IO_read_ptr - fp->_IO_read_base);
539 if (ignore (to_skip) != to_skip)
540 goto dumb;
541 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
542 goto resync;
544 #endif
546 #ifdef TODO
547 if (rel_offset < 0 && rel_offset >= Bbase () - Bptr ())
549 if (!_IO_in_backup (fp))
550 _IO_switch_to_backup_area (fp);
551 gbump (fp->_IO_read_end + rel_offset - fp->_IO_read_ptr);
552 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
553 goto resync;
555 #endif
558 #ifdef TODO
559 _IO_unsave_markers (fp);
560 #endif
562 if (fp->_flags & _IO_NO_READS)
563 goto dumb;
565 /* Try to seek to a block boundary, to improve kernel page management. */
566 new_offset = offset & ~(fp->_IO_buf_end - fp->_IO_buf_base - 1);
567 delta = offset - new_offset;
568 if (delta > fp->_IO_buf_end - fp->_IO_buf_base)
570 new_offset = offset;
571 delta = 0;
573 result = _IO_SYSSEEK (fp, new_offset, 0);
574 if (result < 0)
575 return EOF;
576 if (delta == 0)
577 count = 0;
578 else
580 count = _IO_SYSREAD (fp, fp->_IO_buf_base,
581 (must_be_exact
582 ? delta : fp->_IO_buf_end - fp->_IO_buf_base));
583 if (count < delta)
585 /* We weren't allowed to read, but try to seek the remainder. */
586 offset = count == EOF ? delta : delta-count;
587 dir = _IO_seek_cur;
588 goto dumb;
591 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base + delta,
592 fp->_IO_buf_base + count);
593 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
594 fp->_old_offset = result + count;
595 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
596 return offset;
597 dumb:
599 _IO_unsave_markers (fp);
600 result = _IO_SYSSEEK (fp, offset, dir);
601 if (result != EOF)
603 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
604 fp->_old_offset = result;
605 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
606 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
608 return result;
610 resync:
611 /* We need to do it since it is possible that the file offset in
612 the kernel may be changed behind our back. It may happen when
613 we fopen a file and then do a fork. One process may access the
614 file and the kernel file offset will be changed. */
615 if (fp->_old_offset >= 0)
616 _IO_SYSSEEK (fp, fp->_old_offset, 0);
618 return offset;
621 _IO_ssize_t
622 attribute_compat_text_section
623 _IO_old_file_write (_IO_FILE *f, const void *data, _IO_ssize_t n)
625 _IO_ssize_t to_do = n;
626 while (to_do > 0)
628 _IO_ssize_t count = __write (f->_fileno, data, to_do);
629 if (count == EOF)
631 f->_flags |= _IO_ERR_SEEN;
632 break;
634 to_do -= count;
635 data = (void *) ((char *) data + count);
637 n -= to_do;
638 if (f->_old_offset >= 0)
639 f->_old_offset += n;
640 return n;
643 _IO_size_t
644 attribute_compat_text_section
645 _IO_old_file_xsputn (_IO_FILE *f, const void *data, _IO_size_t n)
647 const char *s = (char *) data;
648 _IO_size_t to_do = n;
649 int must_flush = 0;
650 _IO_size_t count = 0;
652 if (n <= 0)
653 return 0;
654 /* This is an optimized implementation.
655 If the amount to be written straddles a block boundary
656 (or the filebuf is unbuffered), use sys_write directly. */
658 /* First figure out how much space is available in the buffer. */
659 if ((f->_flags & _IO_LINE_BUF) && (f->_flags & _IO_CURRENTLY_PUTTING))
661 count = f->_IO_buf_end - f->_IO_write_ptr;
662 if (count >= n)
664 const char *p;
665 for (p = s + n; p > s; )
667 if (*--p == '\n')
669 count = p - s + 1;
670 must_flush = 1;
671 break;
676 else if (f->_IO_write_end > f->_IO_write_ptr)
677 count = f->_IO_write_end - f->_IO_write_ptr; /* Space available. */
679 /* Then fill the buffer. */
680 if (count > 0)
682 if (count > to_do)
683 count = to_do;
684 if (count > 20)
686 f->_IO_write_ptr = __mempcpy (f->_IO_write_ptr, s, count);
687 s += count;
689 else
691 char *p = f->_IO_write_ptr;
692 int i = (int) count;
693 while (--i >= 0)
694 *p++ = *s++;
695 f->_IO_write_ptr = p;
697 to_do -= count;
699 if (to_do + must_flush > 0)
701 _IO_size_t block_size, do_write;
702 /* Next flush the (full) buffer. */
703 if (__overflow (f, EOF) == EOF)
704 return to_do == 0 ? EOF : n - to_do;
706 /* Try to maintain alignment: write a whole number of blocks.
707 dont_write is what gets left over. */
708 block_size = f->_IO_buf_end - f->_IO_buf_base;
709 do_write = to_do - (block_size >= 128 ? to_do % block_size : 0);
711 if (do_write)
713 count = old_do_write (f, s, do_write);
714 to_do -= count;
715 if (count < do_write)
716 return n - to_do;
719 /* Now write out the remainder. Normally, this will fit in the
720 buffer, but it's somewhat messier for line-buffered files,
721 so we let _IO_default_xsputn handle the general case. */
722 if (to_do)
723 to_do -= _IO_default_xsputn (f, s+do_write, to_do);
725 return n - to_do;
729 const struct _IO_jump_t _IO_old_file_jumps libio_vtable =
731 JUMP_INIT_DUMMY,
732 JUMP_INIT(finish, _IO_old_file_finish),
733 JUMP_INIT(overflow, _IO_old_file_overflow),
734 JUMP_INIT(underflow, _IO_old_file_underflow),
735 JUMP_INIT(uflow, _IO_default_uflow),
736 JUMP_INIT(pbackfail, _IO_default_pbackfail),
737 JUMP_INIT(xsputn, _IO_old_file_xsputn),
738 JUMP_INIT(xsgetn, _IO_default_xsgetn),
739 JUMP_INIT(seekoff, _IO_old_file_seekoff),
740 JUMP_INIT(seekpos, _IO_default_seekpos),
741 JUMP_INIT(setbuf, _IO_old_file_setbuf),
742 JUMP_INIT(sync, _IO_old_file_sync),
743 JUMP_INIT(doallocate, _IO_file_doallocate),
744 JUMP_INIT(read, _IO_file_read),
745 JUMP_INIT(write, _IO_old_file_write),
746 JUMP_INIT(seek, _IO_file_seek),
747 JUMP_INIT(close, _IO_file_close),
748 JUMP_INIT(stat, _IO_file_stat)
751 compat_symbol (libc, _IO_old_do_write, _IO_do_write, GLIBC_2_0);
752 compat_symbol (libc, _IO_old_file_attach, _IO_file_attach, GLIBC_2_0);
753 compat_symbol (libc, _IO_old_file_close_it, _IO_file_close_it, GLIBC_2_0);
754 compat_symbol (libc, _IO_old_file_finish, _IO_file_finish, GLIBC_2_0);
755 compat_symbol (libc, _IO_old_file_fopen, _IO_file_fopen, GLIBC_2_0);
756 compat_symbol (libc, _IO_old_file_init, _IO_file_init, GLIBC_2_0);
757 compat_symbol (libc, _IO_old_file_setbuf, _IO_file_setbuf, GLIBC_2_0);
758 compat_symbol (libc, _IO_old_file_sync, _IO_file_sync, GLIBC_2_0);
759 compat_symbol (libc, _IO_old_file_overflow, _IO_file_overflow, GLIBC_2_0);
760 compat_symbol (libc, _IO_old_file_seekoff, _IO_file_seekoff, GLIBC_2_0);
761 compat_symbol (libc, _IO_old_file_underflow, _IO_file_underflow, GLIBC_2_0);
762 compat_symbol (libc, _IO_old_file_write, _IO_file_write, GLIBC_2_0);
763 compat_symbol (libc, _IO_old_file_xsputn, _IO_file_xsputn, GLIBC_2_0);
765 #endif