maint: update copyright
[gnulib.git] / lib / freadptr.c
1 /* Retrieve information about a FILE stream.
2    Copyright (C) 2007-2014 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 #include <stdlib.h>
23
24 #include "stdio-impl.h"
25
26 const char *
27 freadptr (FILE *fp, size_t *sizep)
28 {
29   size_t size;
30
31   /* Keep this code in sync with freadahead!  */
32 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
33   if (fp->_IO_write_ptr > fp->_IO_write_base)
34     return NULL;
35   size = fp->_IO_read_end - fp->_IO_read_ptr;
36   if (size == 0)
37     return NULL;
38   *sizep = size;
39   return (const char *) fp->_IO_read_ptr;
40 #elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
41   if ((fp_->_flags & __SWR) != 0 || fp_->_r < 0)
42     return NULL;
43   size = fp_->_r;
44   if (size == 0)
45     return NULL;
46   *sizep = size;
47   return (const char *) fp_->_p;
48 #elif defined __EMX__               /* emx+gcc */
49   if ((fp->_flags & _IOWRT) != 0)
50     return NULL;
51   /* Note: fp->_ungetc_count > 0 implies fp->_rcount <= 0,
52            fp->_ungetc_count = 0 implies fp->_rcount >= 0.  */
53   if (fp->_rcount <= 0)
54     return NULL;
55   if (!(fp->_ungetc_count == 0))
56     abort ();
57   *sizep = fp->_rcount;
58   return fp->_ptr;
59 #elif defined __minix               /* Minix */
60   if ((fp_->_flags & _IOWRITING) != 0)
61     return NULL;
62   size = fp_->_count;
63   if (size == 0)
64     return NULL;
65   *sizep = size;
66   return (const char *) fp_->_ptr;
67 #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
68   if ((fp_->_flag & _IOWRT) != 0)
69     return NULL;
70   size = fp_->_cnt;
71   if (size == 0)
72     return NULL;
73   *sizep = size;
74   return (const char *) fp_->_ptr;
75 #elif defined __UCLIBC__            /* uClibc */
76 # ifdef __STDIO_BUFFERS
77   if (fp->__modeflags & __FLAG_WRITING)
78     return NULL;
79   size = fp->__bufread - fp->__bufpos;
80   if (size == 0)
81     return NULL;
82   *sizep = size;
83   return (const char *) fp->__bufpos;
84 # else
85   return NULL;
86 # endif
87 #elif defined __QNX__               /* QNX */
88   if ((fp->_Mode & 0x2000 /* _MWRITE */) != 0)
89     return NULL;
90   /* fp->_Buf <= fp->_Next <= fp->_Rend */
91   size = fp->_Rend - fp->_Next;
92   if (size == 0)
93     return NULL;
94   *sizep = size;
95   return (const char *) fp->_Next;
96 #elif defined __MINT__              /* Atari FreeMiNT */
97   if (!fp->__mode.__read)
98     return NULL;
99   size = fp->__get_limit - fp->__bufp;
100   if (size == 0)
101     return NULL;
102   *sizep = size;
103   return fp->__bufp;
104 #elif defined EPLAN9                /* Plan9 */
105   if (fp->state == 4 /* WR */)
106     return NULL;
107   if (fp->rp >= fp->wp)
108     return NULL;
109   *sizep = fp->wp - fp->rp;
110   return fp->rp;
111 #elif defined SLOW_BUT_NO_HACKS     /* users can define this */
112   /* This implementation is correct on any ANSI C platform.  It is just
113      awfully slow.  */
114   return NULL;
115 #else
116  #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."
117 #endif
118 }