Fix mistake in EMX port.
[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 #include <stdlib.h>
23
24 const char *
25 freadptr (FILE *fp, size_t *sizep)
26 {
27   size_t size;
28
29   /* Keep this code in sync with freadahead!  */
30 #if defined _IO_ferror_unlocked     /* GNU libc, BeOS */
31   if (fp->_IO_write_ptr > fp->_IO_write_base)
32     return NULL;
33   size = fp->_IO_read_end - fp->_IO_read_ptr;
34   if (size == 0)
35     return NULL;
36   *sizep = size;
37   return (const char *) fp->_IO_read_ptr;
38 #elif defined __sferror             /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
39   if ((fp->_flags & __SWR) != 0 || fp->_r < 0)
40     return NULL;
41   size = fp->_r;
42   if (size == 0)
43     return NULL;
44   *sizep = size;
45   return (const char *) fp->_p;
46 #elif defined __EMX__               /* emx+gcc */
47   if ((fp->_flags & _IOWRT) != 0)
48     return NULL;
49   /* Note: fp->_ungetc_count > 0 implies fp->_rcount <= 0,
50            fp->_ungetc_count = 0 implies fp->_rcount >= 0.  */
51   if (fp->_rcount <= 0)
52     return NULL;
53   if (!(fp->_ungetc_count == 0))
54     abort ();
55   *sizep = fp->_rcount;
56   return fp->_ptr;
57 #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw */
58 # if defined __sun && defined _LP64 /* Solaris/{SPARC,AMD64} 64-bit */
59 #  define fp_ ((struct { unsigned char *_ptr; \
60                          unsigned char *_base; \
61                          unsigned char *_end; \
62                          long _cnt; \
63                          int _file; \
64                          unsigned int _flag; \
65                        } *) fp)
66   if ((fp_->_flag & _IOWRT) != 0)
67     return NULL;
68   size = fp_->_cnt;
69   if (size == 0)
70     return NULL;
71   *sizep = size;
72   return (const char *) fp_->_ptr;
73 # else
74 #  if defined _SCO_DS               /* OpenServer */
75 #   define _flag __flag
76 #   define _ptr __ptr
77 #   define _cnt __cnt
78 #  endif
79   if ((fp->_flag & _IOWRT) != 0)
80     return NULL;
81   size = fp->_cnt;
82   if (size == 0)
83     return NULL;
84   *sizep = size;
85   return (const char *) fp->_ptr;
86 # endif
87 #elif defined __UCLIBC__            /* uClibc */
88 # ifdef __STDIO_BUFFERS
89   if (fp->__modeflags & __FLAG_WRITING)
90     return NULL;
91   size = fp->__bufread - fp->__bufpos;
92   if (size == 0)
93     return NULL;
94   *sizep = size;
95   return (const char *) fp->__bufpos;
96 # else
97   return NULL;
98 # endif
99 #elif defined __QNX__               /* QNX */
100   if ((fp->_Mode & 0x2000 /* _MWRITE */) != 0)
101     return NULL;
102   /* fp->_Buf <= fp->_Next <= fp->_Rend */
103   size = fp->_Rend - fp->_Next;
104   if (size == 0)
105     return NULL;
106   *sizep = size;
107   return (const char *) fp->_Next;
108 #else
109  #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."
110 #endif
111 }