fflush: be more paranoid about libio.h change
[gnulib.git] / lib / freadahead.c
blobed3dd0ebdaa94953e37019301e97145991d7fab1
1 /* Retrieve information about a FILE stream.
2 Copyright (C) 2007-2018 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/>. */
17 #include <config.h>
19 /* Specification. */
20 #include "freadahead.h"
22 #include <stdlib.h>
23 #include "stdio-impl.h"
25 #if defined __DragonFly__
26 /* Defined in libc, but not declared in <stdio.h>. */
27 extern size_t __sreadahead (FILE *);
28 #endif
30 size_t
31 freadahead (FILE *fp)
33 #if defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1
34 /* GNU libc, BeOS, Haiku, Linux libc5 */
35 if (fp->_IO_write_ptr > fp->_IO_write_base)
36 return 0;
37 return (fp->_IO_read_end - fp->_IO_read_ptr)
38 + (fp->_flags & _IO_IN_BACKUP ? fp->_IO_save_end - fp->_IO_save_base :
39 0);
40 #elif defined __sferror || defined __DragonFly__ || defined __ANDROID__
41 /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
42 if ((fp_->_flags & __SWR) != 0 || fp_->_r < 0)
43 return 0;
44 # if defined __DragonFly__
45 return __sreadahead (fp);
46 # else
47 return fp_->_r
48 + (HASUB (fp) ? fp_->_ur : 0);
49 # endif
50 #elif defined __EMX__ /* emx+gcc */
51 if ((fp->_flags & _IOWRT) != 0)
52 return 0;
53 /* Note: fp->_ungetc_count > 0 implies fp->_rcount <= 0,
54 fp->_ungetc_count = 0 implies fp->_rcount >= 0. */
55 /* equivalent to
56 (fp->_ungetc_count == 0 ? fp->_rcount : fp->_ungetc_count - fp->_rcount) */
57 return (fp->_rcount > 0 ? fp->_rcount : fp->_ungetc_count - fp->_rcount);
58 #elif defined __minix /* Minix */
59 if ((fp_->_flags & _IOWRITING) != 0)
60 return 0;
61 return fp_->_count;
62 #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, MSVC, NonStop Kernel, OpenVMS */
63 if ((fp_->_flag & _IOWRT) != 0)
64 return 0;
65 return fp_->_cnt;
66 #elif defined __UCLIBC__ /* uClibc */
67 # ifdef __STDIO_BUFFERS
68 if (fp->__modeflags & __FLAG_WRITING)
69 return 0;
70 return (fp->__bufread - fp->__bufpos)
71 + (fp->__modeflags & __FLAG_UNGOT ? 1 : 0);
72 # else
73 return 0;
74 # endif
75 #elif defined __QNX__ /* QNX */
76 if ((fp->_Mode & 0x2000 /* _MWRITE */) != 0)
77 return 0;
78 /* fp->_Buf <= fp->_Next <= fp->_Rend,
79 and fp->_Rend may be overridden by fp->_Rsave. */
80 return ((fp->_Rsave ? fp->_Rsave : fp->_Rend) - fp->_Next)
81 + (fp->_Mode & 0x4000 /* _MBYTE */
82 ? (fp->_Back + sizeof (fp->_Back)) - fp->_Rback
83 : 0);
84 #elif defined __MINT__ /* Atari FreeMiNT */
85 if (!fp->__mode.__read)
86 return 0;
87 return (fp->__pushed_back
88 ? fp->__get_limit - fp->__pushback_bufp + 1
89 : fp->__get_limit - fp->__bufp);
90 #elif defined EPLAN9 /* Plan9 */
91 if (fp->state == 4 /* WR */ || fp->rp >= fp->wp)
92 return 0;
93 return fp->wp - fp->rp;
94 #elif defined SLOW_BUT_NO_HACKS /* users can define this */
95 abort ();
96 return 0;
97 #else
98 #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."
99 #endif