Fix port to QNX.
[gnulib.git] / lib / fpurge.c
1 /* Flushing buffers of 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 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 "fpurge.h"
21
22 #if HAVE___FPURGE                   /* glibc >= 2.2, Solaris >= 7 */
23 # include <stdio_ext.h>
24 #endif
25 #include <stdlib.h>
26
27 int
28 fpurge (FILE *fp)
29 {
30 #if HAVE___FPURGE                   /* glibc >= 2.2, Solaris >= 7 */
31
32   __fpurge (fp);
33   /* The __fpurge function does not have a return value.  */
34   return 0;
35
36 #elif HAVE_FPURGE                   /* FreeBSD, NetBSD, OpenBSD, MacOS X */
37
38   /* Call the system's fpurge function.  */
39 # undef fpurge
40 # if !HAVE_DECL_FPURGE
41   extern int fpurge (FILE *);
42 # endif
43   int result = fpurge (fp);
44 # if defined __sferror              /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
45   if (result == 0)
46     /* Correct the invariants that fpurge broke.
47        <stdio.h> on BSD systems says:
48          "The following always hold: if _flags & __SRD, _w is 0."
49        If this invariant is not fulfilled and the stream is read-write but
50        currently writing, subsequent putc or fputc calls will write directly
51        into the buffer, although they shouldn't be allowed to.  */
52     if ((fp->_flags & __SRD) != 0)
53       fp->_w = 0;
54 # endif
55   return result;
56
57 #else
58
59   /* Most systems provide FILE as a struct and the necessary bitmask in
60      <stdio.h>, because they need it for implementing getc() and putc() as
61      fast macros.  */
62 # if defined _IO_ferror_unlocked    /* GNU libc, BeOS */
63   fp->_IO_read_end = fp->_IO_read_ptr;
64   fp->_IO_write_ptr = fp->_IO_write_base;
65   /* Avoid memory leak when there is an active ungetc buffer.  */
66   if (fp->_IO_save_base != NULL)
67     {
68       free (fp->_IO_save_base);
69       fp->_IO_save_base = NULL;
70     }
71   return 0;
72 # elif defined __sferror            /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
73   fp->_p = fp->_bf._base;
74   fp->_r = 0;
75   fp->_w = ((fp->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully buffered and not currently reading? */
76             ? fp->_bf._size
77             : 0);
78   /* Avoid memory leak when there is an active ungetc buffer.  */
79 #  if defined __NetBSD__ || defined __OpenBSD__ /* NetBSD, OpenBSD */
80    /* See <http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup>
81       and <http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup> */
82 #   define fp_ub ((struct { struct __sbuf _ub; } *) fp->_ext._base)->_ub
83 #  else                                         /* FreeBSD, MacOS X, Cygwin */
84 #   define fp_ub fp->_ub
85 #  endif
86   if (fp_ub._base != NULL)
87     {
88       if (fp_ub._base != fp->_ubuf)
89         free (fp_ub._base);
90       fp_ub._base = NULL;
91     }
92   return 0;
93 # elif defined _IOERR               /* AIX, HP-UX, IRIX, OSF/1, Solaris, mingw */
94   fp->_ptr = fp->_base;
95   if (fp->_ptr != NULL)
96     fp->_cnt = 0;
97   return 0;
98 # elif defined __UCLIBC__           /* uClibc */
99 #  ifdef __STDIO_BUFFERS
100   if (fp->__modeflags & __FLAG_WRITING)
101     fp->__bufpos = fp->__bufstart;
102   else if (fp->__modeflags & (__FLAG_READONLY | __FLAG_READING))
103     fp->__bufpos = fp->__bufread;
104 #  endif
105   return 0;
106 # elif defined __QNX__              /* QNX */
107   fp->_Rback = fp->_Back + sizeof (fp->_Back);
108   fp->_Rsave = NULL;
109   if (fp->_Mode & 0x2000 /* _MWRITE */)
110     /* fp->_Buf <= fp->_Next <= fp->_Wend */
111     fp->_Next = fp->_Buf;
112   else
113     /* fp->_Buf <= fp->_Next <= fp->_Rend */
114     fp->_Rend = fp->_Next;
115   return 0;
116 # else
117  #error "Please port gnulib fpurge.c to your platform! Look at the definitions of fflush, setvbuf and ungetc on your system, then report this to bug-gnulib."
118 # endif
119
120 #endif
121 }