Bump to 1.14.1
[platform/upstream/augeas.git] / lib / freadptr.c
1 /* Retrieve information about a FILE stream.
2    Copyright (C) 2007-2016 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__ || defined __ANDROID__
41   /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Android */
42   if ((fp_->_flags & __SWR) != 0 || fp_->_r < 0)
43     return NULL;
44   size = fp_->_r;
45   if (size == 0)
46     return NULL;
47   *sizep = size;
48   return (const char *) fp_->_p;
49 #elif defined __EMX__               /* emx+gcc */
50   if ((fp->_flags & _IOWRT) != 0)
51     return NULL;
52   /* Note: fp->_ungetc_count > 0 implies fp->_rcount <= 0,
53            fp->_ungetc_count = 0 implies fp->_rcount >= 0.  */
54   if (fp->_rcount <= 0)
55     return NULL;
56   if (!(fp->_ungetc_count == 0))
57     abort ();
58   *sizep = fp->_rcount;
59   return fp->_ptr;
60 #elif defined __minix               /* Minix */
61   if ((fp_->_flags & _IOWRITING) != 0)
62     return NULL;
63   size = fp_->_count;
64   if (size == 0)
65     return NULL;
66   *sizep = size;
67   return (const char *) fp_->_ptr;
68 #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
69   if ((fp_->_flag & _IOWRT) != 0)
70     return NULL;
71   size = fp_->_cnt;
72   if (size == 0)
73     return NULL;
74   *sizep = size;
75   return (const char *) fp_->_ptr;
76 #elif defined __UCLIBC__            /* uClibc */
77 # ifdef __STDIO_BUFFERS
78   if (fp->__modeflags & __FLAG_WRITING)
79     return NULL;
80   if (fp->__modeflags & __FLAG_UNGOT)
81     return NULL;
82   size = fp->__bufread - fp->__bufpos;
83   if (size == 0)
84     return NULL;
85   *sizep = size;
86   return (const char *) fp->__bufpos;
87 # else
88   return NULL;
89 # endif
90 #elif defined __QNX__               /* QNX */
91   if ((fp->_Mode & 0x2000 /* _MWRITE */) != 0)
92     return NULL;
93   /* fp->_Buf <= fp->_Next <= fp->_Rend */
94   size = fp->_Rend - fp->_Next;
95   if (size == 0)
96     return NULL;
97   *sizep = size;
98   return (const char *) fp->_Next;
99 #elif defined __MINT__              /* Atari FreeMiNT */
100   if (!fp->__mode.__read)
101     return NULL;
102   size = fp->__get_limit - fp->__bufp;
103   if (size == 0)
104     return NULL;
105   *sizep = size;
106   return fp->__bufp;
107 #elif defined EPLAN9                /* Plan9 */
108   if (fp->state == 4 /* WR */)
109     return NULL;
110   if (fp->rp >= fp->wp)
111     return NULL;
112   *sizep = fp->wp - fp->rp;
113   return fp->rp;
114 #elif defined SLOW_BUT_NO_HACKS     /* users can define this */
115   /* This implementation is correct on any ANSI C platform.  It is just
116      awfully slow.  */
117   return NULL;
118 #else
119  #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."
120 #endif
121 }