Extend freadptr to return also the buffer size.
[gnulib.git] / lib / freadptr.c
1 /* Retrieve information about a FILE stream.
2    Copyright (C) 2007-2008 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 "freadptr.h"
21
22 const char *
23 freadptr (FILE *fp, size_t *sizep)
24 {
25   size_t size;
26
27   /* Keep this code in sync with freadahead!  */
28 #if defined _IO_ferror_unlocked     /* GNU libc, BeOS */
29   if (fp->_IO_write_ptr > fp->_IO_write_base)
30     return NULL;
31   size = fp->_IO_read_end - fp->_IO_read_ptr;
32   if (size == 0)
33     return NULL;
34   *sizep = size;
35   return (const char *) fp->_IO_read_ptr;
36 #elif defined __sferror             /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
37   if ((fp->_flags & __SWR) != 0 || fp->_r < 0)
38     return NULL;
39   size = fp->_r;
40   if (size == 0)
41     return NULL;
42   *sizep = size;
43   return (const char *) fp->_p;
44 #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, mingw */
45 # if defined __sun && defined _LP64 /* Solaris/{SPARC,AMD64} 64-bit */
46 #  define fp_ ((struct { unsigned char *_ptr; \
47                          unsigned char *_base; \
48                          unsigned char *_end; \
49                          long _cnt; \
50                          int _file; \
51                          unsigned int _flag; \
52                        } *) fp)
53   if ((fp_->_flag & _IOWRT) != 0)
54     return NULL;
55   size = fp_->_cnt;
56   if (size == 0)
57     return NULL;
58   *sizep = size;
59   return (const char *) fp_->_ptr;
60 # else
61   if ((fp->_flag & _IOWRT) != 0)
62     return NULL;
63   size = fp->_cnt;
64   if (size == 0)
65     return NULL;
66   *sizep = size;
67   return (const char *) fp->_ptr;
68 # endif
69 #elif defined __UCLIBC__            /* uClibc */
70 # ifdef __STDIO_BUFFERS
71   if (fp->__modeflags & __FLAG_WRITING)
72     return NULL;
73   size = fp->__bufread - fp->__bufpos;
74   if (size == 0)
75     return NULL;
76   *sizep = size;
77   return (const char *) fp->__bufpos;
78 # else
79   return NULL;
80 # endif
81 #elif defined __QNX__               /* QNX */
82   if ((fp->_Mode & 0x2000 /* _MWRITE */) != 0)
83     return NULL;
84   /* fp->_Buf <= fp->_Next <= fp->_Rend */
85   size = fp->_Rend - fp->_Next;
86   if (size == 0)
87     return NULL;
88   *sizep = size;
89   return (const char *) fp->_Next;
90 #else
91  #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."
92 #endif
93 }