1 /* Retrieve information about a FILE stream.
2 Copyright (C) 2007-2020 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 <https://www.gnu.org/licenses/>. */
24 #include "stdio-impl.h"
26 /* This file is not used on systems that have the __freadptr function,
30 freadptr (FILE *fp
, size_t *sizep
)
34 /* Keep this code in sync with freadahead! */
35 #if defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1
36 /* GNU libc, BeOS, Haiku, Linux libc5 */
37 if (fp
->_IO_write_ptr
> fp
->_IO_write_base
)
39 size
= fp
->_IO_read_end
- fp
->_IO_read_ptr
;
43 return (const char *) fp
->_IO_read_ptr
;
44 #elif defined __sferror || defined __DragonFly__ || defined __ANDROID__
45 /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
46 if ((fp_
->_flags
& __SWR
) != 0 || fp_
->_r
< 0)
52 return (const char *) fp_
->_p
;
53 #elif defined __EMX__ /* emx+gcc */
54 if ((fp
->_flags
& _IOWRT
) != 0)
56 /* Note: fp->_ungetc_count > 0 implies fp->_rcount <= 0,
57 fp->_ungetc_count = 0 implies fp->_rcount >= 0. */
60 if (!(fp
->_ungetc_count
== 0))
64 #elif defined __minix /* Minix */
65 if ((fp_
->_flags
& _IOWRITING
) != 0)
71 return (const char *) fp_
->_ptr
;
72 #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, MSVC, NonStop Kernel, OpenVMS */
73 if ((fp_
->_flag
& _IOWRT
) != 0)
79 return (const char *) fp_
->_ptr
;
80 #elif defined __UCLIBC__ /* uClibc */
81 # ifdef __STDIO_BUFFERS
82 if (fp
->__modeflags
& __FLAG_WRITING
)
84 if (fp
->__modeflags
& __FLAG_UNGOT
)
86 size
= fp
->__bufread
- fp
->__bufpos
;
90 return (const char *) fp
->__bufpos
;
94 #elif defined __QNX__ /* QNX */
95 if ((fp
->_Mode
& 0x2000 /* _MWRITE */) != 0)
97 /* fp->_Buf <= fp->_Next <= fp->_Rend */
98 size
= fp
->_Rend
- fp
->_Next
;
102 return (const char *) fp
->_Next
;
103 #elif defined __MINT__ /* Atari FreeMiNT */
104 if (!fp
->__mode
.__read
)
106 size
= fp
->__get_limit
- fp
->__bufp
;
111 #elif defined EPLAN9 /* Plan9 */
112 if (fp
->state
== 4 /* WR */)
114 if (fp
->rp
>= fp
->wp
)
116 *sizep
= fp
->wp
- fp
->rp
;
118 #elif defined SLOW_BUT_NO_HACKS /* users can define this */
119 /* This implementation is correct on any ANSI C platform. It is just
123 #error "Please port gnulib freadptr.c to your platform! Look at the definition of fflush, fread, getc, getc_unlocked on your system, then report this to bug-gnulib."