Add support for QNX (untested).
[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 2, or (at your option)
7    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 along
15    with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 #include <config.h>
19
20 /* Specification.  */
21 #include "freadahead.h"
22
23 size_t
24 freadahead (FILE *fp)
25 {
26 #if defined _IO_ferror_unlocked     /* GNU libc, BeOS */
27   if (fp->_IO_write_ptr > fp->_IO_write_base)
28     return 0;
29   return fp->_IO_read_end - fp->_IO_read_ptr;
30 #elif defined __sferror             /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
31   if ((fp->_flags & __SWR) != 0 || fp->_r < 0)
32     return 0;
33   return fp->_r;
34 #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, mingw */
35 # if defined __sun && defined _LP64 /* Solaris/{SPARC,AMD64} 64-bit */
36 #  define fp_ ((struct { unsigned char *_ptr; \
37                          unsigned char *_base; \
38                          unsigned char *_end; \
39                          long _cnt; \
40                          int _file; \
41                          unsigned int _flag; \
42                        } *) fp)
43   if ((fp_->_flag & _IOWRT) != 0)
44     return 0;
45   return fp_->_cnt;
46 # else
47   if ((fp->_flag & _IOWRT) != 0)
48     return 0;
49   return fp->_cnt;
50 # endif
51 #elif defined __UCLIBC__            /* uClibc */
52 # ifdef __STDIO_BUFFERS
53   if (fp->__modeflags & __FLAG_WRITING)
54     return 0;
55   return fp->__bufread - fp->__bufpos;
56 # else
57   return 0;
58 # endif
59 #elif defined __QNX__               /* QNX */
60   if ((fp->_Mode & _MWRITE) != 0)
61     return 0;
62   /* fp->_Buf <= fp->_Next <= fp->_Rend */
63   return fp->_Rend - fp->_Next;
64 #else
65  #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."
66 #endif
67 }