use _GL_ATTRIBUTE_CONST and _GL_ATTRIBUTE_PURE
[gnulib.git] / lib / freadahead.c
1 /* Retrieve information about a FILE stream.
2    Copyright (C) 2007-2011 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 #include <config.h>
18
19 /* Specification.  */
20 #include "freadahead.h"
21
22 #include <stdlib.h>
23 #include "stdio-impl.h"
24
25 /* The attribute __pure__ was added in gcc 2.96.  */
26 #undef _GL_ATTRIBUTE_PURE
27 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
28 # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
29 #else
30 # define _GL_ATTRIBUTE_PURE /* empty */
31 #endif
32
33 size_t _GL_ATTRIBUTE_PURE
34 freadahead (FILE *fp)
35 {
36 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
37   if (fp->_IO_write_ptr > fp->_IO_write_base)
38     return 0;
39   return (fp->_IO_read_end - fp->_IO_read_ptr)
40          + (fp->_flags & _IO_IN_BACKUP ? fp->_IO_save_end - fp->_IO_save_base :
41             0);
42 #elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
43   if ((fp_->_flags & __SWR) != 0 || fp_->_r < 0)
44     return 0;
45 # if defined __DragonFly__
46   return __sreadahead (fp);
47 # else
48   return fp_->_r
49          + (HASUB (fp) ? fp_->_ur : 0);
50 # endif
51 #elif defined __EMX__               /* emx+gcc */
52   if ((fp->_flags & _IOWRT) != 0)
53     return 0;
54   /* Note: fp->_ungetc_count > 0 implies fp->_rcount <= 0,
55            fp->_ungetc_count = 0 implies fp->_rcount >= 0.  */
56   /* equivalent to
57      (fp->_ungetc_count == 0 ? fp->_rcount : fp->_ungetc_count - fp->_rcount) */
58   return (fp->_rcount > 0 ? fp->_rcount : fp->_ungetc_count - fp->_rcount);
59 #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
60   if ((fp_->_flag & _IOWRT) != 0)
61     return 0;
62   return fp_->_cnt;
63 #elif defined __UCLIBC__            /* uClibc */
64 # ifdef __STDIO_BUFFERS
65   if (fp->__modeflags & __FLAG_WRITING)
66     return 0;
67   return (fp->__bufread - fp->__bufpos)
68          + (fp->__modeflags & __FLAG_UNGOT ? 1 : 0);
69 # else
70   return 0;
71 # endif
72 #elif defined __QNX__               /* QNX */
73   if ((fp->_Mode & 0x2000 /* _MWRITE */) != 0)
74     return 0;
75   /* fp->_Buf <= fp->_Next <= fp->_Rend,
76      and fp->_Rend may be overridden by fp->_Rsave. */
77   return ((fp->_Rsave ? fp->_Rsave : fp->_Rend) - fp->_Next)
78          + (fp->_Mode & 0x4000 /* _MBYTE */
79             ? (fp->_Back + sizeof (fp->_Back)) - fp->_Rback
80             : 0);
81 #elif defined __MINT__              /* Atari FreeMiNT */
82   if (!fp->__mode.__read)
83     return 0;
84   return (fp->__pushed_back
85           ? fp->__get_limit - fp->__pushback_bufp + 1
86           : fp->__get_limit - fp->__bufp);
87 #elif defined SLOW_BUT_NO_HACKS     /* users can define this */
88   abort ();
89   return 0;
90 #else
91  #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."
92 #endif
93 }