Change copyright notice from GPLv2+ to GPLv3+.
[gnulib.git] / lib / freadahead.c
1 /* Retrieve information about a FILE stream.
2    Copyright (C) 2007 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 size_t
23 freadahead (FILE *fp)
24 {
25 #if defined _IO_ferror_unlocked     /* GNU libc, BeOS */
26   if (fp->_IO_write_ptr > fp->_IO_write_base)
27     return 0;
28   return fp->_IO_read_end - fp->_IO_read_ptr;
29 #elif defined __sferror             /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
30   if ((fp->_flags & __SWR) != 0 || fp->_r < 0)
31     return 0;
32   return fp->_r;
33 #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, mingw */
34 # if defined __sun && defined _LP64 /* Solaris/{SPARC,AMD64} 64-bit */
35 #  define fp_ ((struct { unsigned char *_ptr; \
36                          unsigned char *_base; \
37                          unsigned char *_end; \
38                          long _cnt; \
39                          int _file; \
40                          unsigned int _flag; \
41                        } *) fp)
42   if ((fp_->_flag & _IOWRT) != 0)
43     return 0;
44   return fp_->_cnt;
45 # else
46   if ((fp->_flag & _IOWRT) != 0)
47     return 0;
48   return fp->_cnt;
49 # endif
50 #elif defined __UCLIBC__            /* uClibc */
51 # ifdef __STDIO_BUFFERS
52   if (fp->__modeflags & __FLAG_WRITING)
53     return 0;
54   return fp->__bufread - fp->__bufpos;
55 # else
56   return 0;
57 # endif
58 #elif defined __QNX__               /* QNX */
59   if ((fp->_Mode & _MWRITE) != 0)
60     return 0;
61   /* fp->_Buf <= fp->_Next <= fp->_Rend */
62   return fp->_Rend - fp->_Next;
63 #else
64  #error "Please port gnulib freadahead.c to your platform! Look at the definition of fflush, fread on your system, then report this to bug-gnulib."
65 #endif
66 }