~ubuntu-branches/ubuntu/gutsy/m4/gutsy

« back to all changes in this revision

Viewing changes to lib/fpurge.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-08-01 14:00:55 UTC
  • mfrom: (1.5.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070801140055-ob48jo517shsyvgk
Tags: 1.4.10-0ubuntu1
New upstream version.

Show diffs side-by-side

added added

removed removed

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