1 /* Copyright (C) 1993, 1995, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU IO Library.
3 Written by Per Bothner <bothner@cygnus.com>.
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2, or (at
8 your option) any later version.
10 This library is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this library; see the file COPYING. If not, write to
17 the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
20 As a special exception, if you link this library with files
21 compiled with a GNU compiler to produce an executable, this does
22 not cause the resulting executable to be covered by the GNU General
23 Public License. This exception does not however invalidate any
24 other reasons why the executable file might be covered by the GNU
25 General Public License. */
29 # define _POSIX_SOURCE
33 #include <sys/types.h>
41 # define __set_errno(Val) errno = (Val)
46 # define open(Name, Flags, Prot) __open (Name, Flags, Prot)
47 # define close(FD) __close (FD)
48 # define fstat(FD, Statbuf) __fstat (FD, Statbuf)
49 # define lseek(FD, Offset, Whence) __lseek (FD, Offset, Whence)
50 # define read(FD, Buf, NBytes) __read (FD, Buf, NBytes)
51 # define write(FD, Buf, NBytes) __write (FD, Buf, NBytes)
54 /* An fstream can be in at most one of put mode, get mode, or putback mode.
55 Putback mode is a variant of get mode.
57 In a filebuf, there is only one current position, instead of two
58 separate get and put pointers. In get mode, the current position
59 is that of gptr(); in put mode that of pptr().
61 The position in the buffer that corresponds to the position
62 in external file system is normally _IO_read_end, except in putback
63 mode, when it is _IO_save_end.
64 If the field _fb._offset is >= 0, it gives the offset in
65 the file as a whole corresponding to eGptr(). (?)
68 If a filebuf is in put mode, then all of _IO_read_ptr, _IO_read_end,
69 and _IO_read_base are equal to each other. These are usually equal
70 to _IO_buf_base, though not necessarily if we have switched from
71 get mode to put mode. (The reason is to maintain the invariant
72 that _IO_read_end corresponds to the external file position.)
73 _IO_write_base is non-NULL and usually equal to _IO_base_base.
74 We also have _IO_write_end == _IO_buf_end, but only in fully buffered mode.
75 The un-flushed character are those between _IO_write_base and _IO_write_ptr.
78 If a filebuf is in get or putback mode, eback() != egptr().
79 In get mode, the unread characters are between gptr() and egptr().
80 The OS file position corresponds to that of egptr().
83 Putback mode is used to remember "excess" characters that have
84 been sputbackc'd in a separate putback buffer.
85 In putback mode, the get buffer points to the special putback buffer.
86 The unread characters are the characters between gptr() and egptr()
87 in the putback buffer, as well as the area between save_gptr()
88 and save_egptr(), which point into the original reserve buffer.
89 (The pointers save_gptr() and save_egptr() are the values
90 of gptr() and egptr() at the time putback mode was entered.)
91 The OS position corresponds to that of save_egptr().
94 During line buffered output, _IO_write_base==base() && epptr()==base().
95 However, ptr() may be anywhere between base() and ebuf().
96 This forces a call to filebuf::overflow(int C) on every put.
97 If there is more space in the buffer, and C is not a '\n',
98 then C is inserted, and pptr() incremented.
101 If a filebuf is unbuffered(), the _shortbuf[1] is used as the buffer.
104 #define CLOSED_FILEBUF_FLAGS \
105 (_IO_IS_FILEBUF+_IO_NO_READS+_IO_NO_WRITES+_IO_TIED_PUT_GET)
112 /* POSIX.1 allows another file handle to be used to change the position
113 of our file descriptor. Hence we actually don't know the actual
114 position before we do the first fseek (and until a following fflush). */
115 fp->_offset = _IO_pos_BAD;
116 fp->_IO_file_flags |= CLOSED_FILEBUF_FLAGS;
123 _IO_file_close_it (fp)
126 int write_status, close_status;
127 if (!_IO_file_is_open (fp))
130 write_status = _IO_do_flush (fp);
132 _IO_unsave_markers(fp);
134 close_status = _IO_SYSCLOSE (fp);
137 _IO_setb (fp, NULL, NULL, 0);
138 _IO_setg (fp, NULL, NULL, NULL);
139 _IO_setp (fp, NULL, NULL);
142 fp->_flags = _IO_MAGIC|CLOSED_FILEBUF_FLAGS;
144 fp->_offset = _IO_pos_BAD;
146 return close_status ? close_status : write_status;
150 _IO_file_finish (fp, dummy)
154 if (_IO_file_is_open (fp))
157 if (!(fp->_flags & _IO_DELETE_DONT_CLOSE))
160 _IO_default_finish (fp, 0);
164 _IO_file_fopen (fp, filename, mode)
166 const char *filename;
169 int oflags = 0, omode;
170 int read_write, fdesc;
172 if (_IO_file_is_open (fp))
178 read_write = _IO_NO_WRITES;
182 oflags = O_CREAT|O_TRUNC;
183 read_write = _IO_NO_READS;
187 oflags = O_CREAT|O_APPEND;
188 read_write = _IO_NO_READS|_IO_IS_APPENDING;
191 __set_errno (EINVAL);
194 if (mode[0] == '+' || (mode[0] == 'b' && mode[1] == '+'))
197 read_write &= _IO_IS_APPENDING;
199 fdesc = open (filename, omode|oflags, oprot);
203 _IO_mask_flags (fp, read_write,_IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
204 if (read_write & _IO_IS_APPENDING)
205 if (_IO_SEEKOFF (fp, (_IO_off_t)0, _IO_seek_end, _IOS_INPUT|_IOS_OUTPUT)
206 == _IO_pos_BAD && errno != ESPIPE)
213 _IO_file_attach (fp, fd)
217 if (_IO_file_is_open (fp))
220 fp->_flags &= ~(_IO_NO_READS+_IO_NO_WRITES);
221 fp->_flags |= _IO_DELETE_DONT_CLOSE;
222 /* Get the current position of the file. */
223 /* We have to do that since that may be junk. */
224 fp->_offset = _IO_pos_BAD;
225 if (_IO_SEEKOFF (fp, (_IO_off_t)0, _IO_seek_cur, _IOS_INPUT|_IOS_OUTPUT)
226 == _IO_pos_BAD && errno != ESPIPE)
232 _IO_file_setbuf (fp, p, len)
237 if (_IO_default_setbuf (fp, p, len) == NULL)
240 fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end
242 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
247 /* Write TO_DO bytes from DATA to FP.
248 Then mark FP as having empty buffers. */
251 _IO_do_write (fp, data, to_do)
259 if (fp->_flags & _IO_IS_APPENDING)
260 /* On a system without a proper O_APPEND implementation,
261 you would need to sys_seek(0, SEEK_END) here, but is
262 is not needed nor desirable for Unix- or Posix-like systems.
263 Instead, just indicate that offset (before and after) is
265 fp->_offset = _IO_pos_BAD;
266 else if (fp->_IO_read_end != fp->_IO_write_base)
269 = _IO_SYSSEEK (fp, fp->_IO_write_base - fp->_IO_read_end, 1);
270 if (new_pos == _IO_pos_BAD)
272 fp->_offset = new_pos;
274 count = _IO_SYSWRITE (fp, data, to_do);
276 fp->_cur_column = _IO_adjust_column (fp->_cur_column - 1, data, to_do) + 1;
277 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
278 fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_buf_base;
279 fp->_IO_write_end = ((fp->_flags & (_IO_LINE_BUF+_IO_UNBUFFERED))
280 ? fp->_IO_buf_base : fp->_IO_buf_end);
281 return count != to_do ? EOF : 0;
285 _IO_file_underflow (fp)
290 /* SysV does not make this test; take it out for compatibility */
291 if (fp->_flags & _IO_EOF_SEEN)
295 if (fp->_flags & _IO_NO_READS)
300 if (fp->_IO_read_ptr < fp->_IO_read_end)
301 return *(unsigned char *) fp->_IO_read_ptr;
303 if (fp->_IO_buf_base == NULL)
306 /* Flush all line buffered files before reading. */
307 /* FIXME This can/should be moved to genops ?? */
308 if (fp->_flags & (_IO_LINE_BUF|_IO_UNBUFFERED))
309 _IO_flush_all_linebuffered ();
311 _IO_switch_to_get_mode (fp);
313 /* This is very tricky. We have to adjust those
314 pointers before we call _IO_SYSREAD () since
315 we may longjump () out while waiting for
316 input. Those pointers may be screwed up. H.J. */
317 fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_buf_base;
318 fp->_IO_read_end = fp->_IO_buf_base;
319 fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end
322 count = _IO_SYSREAD (fp, fp->_IO_buf_base,
323 fp->_IO_buf_end - fp->_IO_buf_base);
327 fp->_flags |= _IO_EOF_SEEN;
329 fp->_flags |= _IO_ERR_SEEN, count = 0;
331 fp->_IO_read_end += count;
334 if (fp->_offset != _IO_pos_BAD)
335 _IO_pos_adjust (fp->_offset, count);
336 return *(unsigned char *) fp->_IO_read_ptr;
340 _IO_file_overflow (f, ch)
344 if (f->_flags & _IO_NO_WRITES) /* SET ERROR */
346 f->_flags |= _IO_ERR_SEEN;
350 /* If currently reading or no buffer allocated. */
351 if ((f->_flags & _IO_CURRENTLY_PUTTING) == 0)
353 /* Allocate a buffer if needed. */
354 if (f->_IO_write_base == 0)
357 _IO_setg (f, f->_IO_buf_base, f->_IO_buf_base, f->_IO_buf_base);
359 /* Otherwise must be currently reading.
360 If _IO_read_ptr (and hence also _IO_read_end) is at the buffer end,
361 logically slide the buffer forwards one block (by setting the
362 read pointers to all point at the beginning of the block). This
363 makes room for subsequent output.
364 Otherwise, set the read pointers to _IO_read_end (leaving that
365 alone, so it can continue to correspond to the external position). */
366 if (f->_IO_read_ptr == f->_IO_buf_end)
367 f->_IO_read_end = f->_IO_read_ptr = f->_IO_buf_base;
368 f->_IO_write_ptr = f->_IO_read_ptr;
369 f->_IO_write_base = f->_IO_write_ptr;
370 f->_IO_write_end = f->_IO_buf_end;
371 f->_IO_read_base = f->_IO_read_ptr = f->_IO_read_end;
373 if (f->_flags & (_IO_LINE_BUF+_IO_UNBUFFERED))
374 f->_IO_write_end = f->_IO_write_ptr;
375 f->_flags |= _IO_CURRENTLY_PUTTING;
378 return _IO_do_flush (f);
379 if (f->_IO_write_ptr == f->_IO_buf_end ) /* Buffer is really full */
380 if (_IO_do_flush (f) == EOF)
382 *f->_IO_write_ptr++ = ch;
383 if ((f->_flags & _IO_UNBUFFERED)
384 || ((f->_flags & _IO_LINE_BUF) && ch == '\n'))
385 if (_IO_do_flush (f) == EOF)
387 return (unsigned char) ch;
397 _IO_cleanup_region_start ((void (*) __P ((void *))) _IO_funlockfile, fp);
399 /* char* ptr = cur_ptr(); */
400 if (fp->_IO_write_ptr > fp->_IO_write_base)
401 if (_IO_do_flush(fp)) return EOF;
402 delta = fp->_IO_read_ptr - fp->_IO_read_end;
406 if (_IO_in_backup (fp))
407 delta -= eGptr () - Gbase ();
409 _IO_off_t new_pos = _IO_SYSSEEK (fp, delta, 1);
410 if (new_pos != (_IO_off_t) EOF)
411 fp->_IO_read_end = fp->_IO_read_ptr;
413 else if (errno == ESPIPE)
414 ; /* Ignore error from unseekable devices. */
420 fp->_offset = _IO_pos_BAD;
421 /* FIXME: Cleanup - can this be shared? */
422 /* setg(base(), ptr, ptr); */
423 _IO_cleanup_region_end (1);
428 _IO_file_seekoff (fp, offset, dir, mode)
435 _IO_off_t delta, new_offset;
437 /* POSIX.1 8.2.3.7 says that after a call the fflush() the file
438 offset of the underlying file must be exact. */
439 int must_be_exact = (fp->_IO_read_base == fp->_IO_read_end
440 && fp->_IO_write_base == fp->_IO_write_ptr);
443 dir = _IO_seek_cur, offset = 0; /* Don't move any pointers. */
445 /* Flush unwritten characters.
446 (This may do an unneeded write if we seek within the buffer.
447 But to be able to switch to reading, we would need to set
448 egptr to ptr. That can't be done in the current design,
449 which assumes file_ptr() is eGptr. Anyway, since we probably
450 end up flushing when we close(), it doesn't make much difference.)
451 FIXME: simulate mem-papped files. */
453 if (fp->_IO_write_ptr > fp->_IO_write_base || _IO_in_put_mode (fp))
454 if (_IO_switch_to_get_mode (fp))
457 if (fp->_IO_buf_base == NULL)
460 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
461 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
467 /* Adjust for read-ahead (bytes is buffer). */
468 offset -= fp->_IO_read_end - fp->_IO_read_ptr;
469 if (fp->_offset == _IO_pos_BAD)
471 /* Make offset absolute, assuming current pointer is file_ptr(). */
472 offset += _IO_pos_as_off (fp->_offset);
481 if (_IO_SYSSTAT (fp, &st) == 0 && S_ISREG (st.st_mode))
483 offset += st.st_size;
490 /* At this point, dir==_IO_seek_set. */
492 /* If destination is within current buffer, optimize: */
493 if (fp->_offset != _IO_pos_BAD && fp->_IO_read_base != NULL
494 && !_IO_in_backup (fp))
496 /* Offset relative to start of main get area. */
497 _IO_pos_t rel_offset = (offset - fp->_offset
498 + (fp->_IO_read_end - fp->_IO_read_base));
502 if (_IO_in_backup (fp))
503 _IO_switch_to_main_get_area (fp);
505 if (rel_offset <= fp->_IO_read_end - fp->_IO_read_base)
507 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base + rel_offset,
509 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
513 /* If we have streammarkers, seek forward by reading ahead. */
514 if (_IO_have_markers (fp))
516 int to_skip = rel_offset
517 - (fp->_IO_read_ptr - fp->_IO_read_base);
518 if (ignore (to_skip) != to_skip)
525 if (rel_offset < 0 && rel_offset >= Bbase () - Bptr ())
527 if (!_IO_in_backup (fp))
528 _IO_switch_to_backup_area (fp);
529 gbump (fp->_IO_read_end + rel_offset - fp->_IO_read_ptr);
536 _IO_unsave_markers (fp);
539 if (fp->_flags & _IO_NO_READS)
542 /* Try to seek to a block boundary, to improve kernel page management. */
543 new_offset = offset & ~(fp->_IO_buf_end - fp->_IO_buf_base - 1);
544 delta = offset - new_offset;
545 if (delta > fp->_IO_buf_end - fp->_IO_buf_base)
550 result = _IO_SYSSEEK (fp, new_offset, 0);
557 count = _IO_SYSREAD (fp, fp->_IO_buf_base,
559 ? delta : fp->_IO_buf_end - fp->_IO_buf_base));
562 /* We weren't allowed to read, but try to seek the remainder. */
563 offset = count == EOF ? delta : delta-count;
568 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base + delta,
569 fp->_IO_buf_base + count);
570 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
571 fp->_offset = result + count;
572 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
576 _IO_unsave_markers (fp);
577 result = _IO_SYSSEEK (fp, offset, dir);
579 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
580 fp->_offset = result;
581 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
582 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
587 _IO_file_read (fp, buf, size)
592 return read (fp->_fileno, buf, size);
596 _IO_file_seek (fp, offset, dir)
601 return lseek (fp->_fileno, offset, dir);
605 _IO_file_stat (fp, st)
609 return fstat (fp->_fileno, (struct stat *) st);
616 return close (fp->_fileno);
620 _IO_file_write (f, data, n)
625 _IO_ssize_t to_do = n;
628 _IO_ssize_t count = write (f->_fileno, data, to_do);
631 f->_flags |= _IO_ERR_SEEN;
635 data = (void *) ((char *) data + count);
644 _IO_file_xsputn (f, data, n)
649 register const char *s = (char *) data;
650 _IO_size_t to_do = n;
656 /* This is an optimized implementation.
657 If the amount to be written straddles a block boundary
658 (or the filebuf is unbuffered), use sys_write directly. */
660 /* First figure out how much space is available in the buffer. */
661 count = f->_IO_write_end - f->_IO_write_ptr; /* Space available. */
662 if ((f->_flags & _IO_LINE_BUF) && (f->_flags & _IO_CURRENTLY_PUTTING))
664 count = f->_IO_buf_end - f->_IO_write_ptr;
667 register const char *p;
668 for (p = s + n; p > s; )
679 /* Then fill the buffer. */
686 memcpy (f->_IO_write_ptr, s, count);
691 register char *p = f->_IO_write_ptr;
692 register int i = (int) count;
696 f->_IO_write_ptr += count;
699 if (to_do + must_flush > 0)
701 _IO_size_t block_size, dont_write;
702 /* Next flush the (full) buffer. */
703 if (__overflow (f, EOF) == EOF)
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 dont_write = block_size >= 128 ? to_do % block_size : 0;
711 count = to_do - dont_write;
712 if (_IO_do_write (f, s, count) == EOF)
716 /* Now write out the remainder. Normally, this will fit in the
717 buffer, but it's somewhat messier for line-buffered files,
718 so we let _IO_default_xsputn handle the general case. */
720 to_do -= _IO_default_xsputn (f, s+count, dont_write);
726 /* Work in progress */
728 _IO_file_xsgetn (fp, data, n)
733 register _IO_size_t more = n;
734 register char *s = data;
737 /* Data available. */
738 _IO_ssize_t count = fp->_IO_read_end - fp->_IO_read_ptr;
745 memcpy (s, fp->_IO_read_ptr, count);
747 fp->_IO_read_ptr += count;
753 register char *p = fp->_IO_read_ptr;
754 register int i = (int) count;
757 fp->_IO_read_ptr = p;
762 if (! _IO_in put_mode (fp)
763 && ! _IO_have_markers (fp) && ! IO_have_backup (fp))
765 /* This is an optimization of _IO_file_underflow */
766 if (fp->_flags & _IO_NO_READS)
768 /* If we're reading a lot of data, don't bother allocating
769 a buffer. But if we're only reading a bit, perhaps we should ??*/
770 if (count <= 512 && fp->_IO_buf_base == NULL)
772 if (fp->_flags & (_IO_LINE_BUF|_IO_UNBUFFERED))
773 _IO_flush_all_linebuffered ();
775 _IO_switch_to_get_mode (fp); ???;
776 count = _IO_SYSREAD (fp, s, more);
780 fp->_flags |= _IO_EOF_SEEN;
782 fp->_flags |= _IO_ERR_SEEN, count = 0;
789 if (more == 0 || __underflow (fp) == EOF)
796 struct _IO_jump_t _IO_file_jumps =
799 JUMP_INIT(finish, _IO_file_finish),
800 JUMP_INIT(overflow, _IO_file_overflow),
801 JUMP_INIT(underflow, _IO_file_underflow),
802 JUMP_INIT(uflow, _IO_default_uflow),
803 JUMP_INIT(pbackfail, _IO_default_pbackfail),
804 JUMP_INIT(xsputn, _IO_file_xsputn),
805 JUMP_INIT(xsgetn, _IO_default_xsgetn),
806 JUMP_INIT(seekoff, _IO_file_seekoff),
807 JUMP_INIT(seekpos, _IO_default_seekpos),
808 JUMP_INIT(setbuf, _IO_file_setbuf),
809 JUMP_INIT(sync, _IO_file_sync),
810 JUMP_INIT(doallocate, _IO_file_doallocate),
811 JUMP_INIT(read, _IO_file_read),
812 JUMP_INIT(write, _IO_file_write),
813 JUMP_INIT(seek, _IO_file_seek),
814 JUMP_INIT(close, _IO_file_close),
815 JUMP_INIT(stat, _IO_file_stat)