1 /* fpending.c -- return the number of pending output bytes on a stream
2    Copyright (C) 2000, 2004, 2006-2007, 2009-2017 Free Software Foundation,
3    Inc.
4 
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17 
18 /* Written by Jim Meyering. */
19 
20 #include <config.h>
21 
22 /* Specification.  */
23 #include "fpending.h"
24 
25 #include "stdio-impl.h"
26 
27 /* Return the number of pending (aka buffered, unflushed)
28    bytes on the stream, FP, that is open for writing.  */
29 size_t
__fpending(FILE * fp)30 __fpending (FILE *fp)
31 {
32   /* Most systems provide FILE as a struct and the necessary bitmask in
33      <stdio.h>, because they need it for implementing getc() and putc() as
34      fast macros.  */
35 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
36   return fp->_IO_write_ptr - fp->_IO_write_base;
37 #elif defined __sferror || defined __DragonFly__ || defined __ANDROID__
38   /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Android */
39   return fp->_p - fp->_bf._base;
40 #elif defined __EMX__                /* emx+gcc */
41   return fp->_ptr - fp->_buffer;
42 #elif defined __minix                /* Minix */
43   return fp_->_ptr - fp_->_buf;
44 #elif defined _IOERR                 /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, MSVC, NonStop Kernel */
45   return (fp_->_ptr ? fp_->_ptr - fp_->_base : 0);
46 #elif defined __UCLIBC__             /* uClibc */
47   return (fp->__modeflags & __FLAG_WRITING ? fp->__bufpos - fp->__bufstart : 0);
48 #elif defined __QNX__                /* QNX */
49   return (fp->_Mode & 0x2000 /*_MWRITE*/ ? fp->_Next - fp->_Buf : 0);
50 #elif defined __MINT__               /* Atari FreeMiNT */
51   return fp->__bufp - fp->__buffer;
52 #elif defined EPLAN9                 /* Plan9 */
53   return fp->wp - fp->buf;
54 #elif defined __VMS                  /* VMS */
55   return (*fp)->_ptr - (*fp)->_base;
56 #else
57 # error "Please port gnulib fpending.c to your platform!"
58   return 1;
59 #endif
60 }
61