1 /* Retrieve information about a FILE stream.
2 Copyright (C) 2007-2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "freadahead.h"
23 #include "stdio-impl.h"
28 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
29 if (fp->_IO_write_ptr > fp->_IO_write_base)
31 return (fp->_IO_read_end - fp->_IO_read_ptr)
32 + (fp->_flags & _IO_IN_BACKUP ? fp->_IO_save_end - fp->_IO_save_base :
34 #elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
35 if ((fp_->_flags & __SWR) != 0 || fp_->_r < 0)
37 # if defined __DragonFly__
38 return __sreadahead (fp);
41 + (HASUB (fp) ? fp_->_ur : 0);
43 #elif defined __EMX__ /* emx+gcc */
44 if ((fp->_flags & _IOWRT) != 0)
46 /* Note: fp->_ungetc_count > 0 implies fp->_rcount <= 0,
47 fp->_ungetc_count = 0 implies fp->_rcount >= 0. */
49 (fp->_ungetc_count == 0 ? fp->_rcount : fp->_ungetc_count - fp->_rcount) */
50 return (fp->_rcount > 0 ? fp->_rcount : fp->_ungetc_count - fp->_rcount);
51 #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
52 if ((fp_->_flag & _IOWRT) != 0)
55 #elif defined __UCLIBC__ /* uClibc */
56 # ifdef __STDIO_BUFFERS
57 if (fp->__modeflags & __FLAG_WRITING)
59 return (fp->__bufread - fp->__bufpos)
60 + (fp->__modeflags & __FLAG_UNGOT ? 1 : 0);
64 #elif defined __QNX__ /* QNX */
65 if ((fp->_Mode & 0x2000 /* _MWRITE */) != 0)
67 /* fp->_Buf <= fp->_Next <= fp->_Rend,
68 and fp->_Rend may be overridden by fp->_Rsave. */
69 return ((fp->_Rsave ? fp->_Rsave : fp->_Rend) - fp->_Next)
70 + (fp->_Mode & 0x4000 /* _MBYTE */
71 ? (fp->_Back + sizeof (fp->_Back)) - fp->_Rback
73 #elif defined __MINT__ /* Atari FreeMiNT */
74 if (!fp->__mode.__read)
76 return (fp->__pushed_back
77 ? fp->__get_limit - fp->__pushback_bufp + 1
78 : fp->__get_limit - fp->__bufp);
79 #elif defined SLOW_BUT_NO_HACKS /* users can define this */
83 #error "Please port gnulib freadahead.c to your platform! Look at the definition of fflush, fread, ungetc on your system, then report this to bug-gnulib."