1 /* fpending.c -- return the number of pending output bytes on a stream
2    Copyright (C) 2000, 2004, 2006-2007, 2009-2020 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 <https://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 /* This file is not used on systems that already have the __fpending function,
28    namely glibc >= 2.2, Solaris >= 7, Android API >= 23.  */
29 
30 /* Return the number of pending (aka buffered, unflushed)
31    bytes on the stream, FP, that is open for writing.  */
32 size_t
__fpending(FILE * fp)33 __fpending (FILE *fp)
34 {
35   /* Most systems provide FILE as a struct and the necessary bitmask in
36      <stdio.h>, because they need it for implementing getc() and putc() as
37      fast macros.  */
38 #if defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1
39   /* GNU libc, BeOS, Haiku, Linux libc5 */
40   return fp->_IO_write_ptr - fp->_IO_write_base;
41 #elif defined __sferror || defined __DragonFly__ || defined __ANDROID__
42   /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
43   return fp->_p - fp->_bf._base;
44 #elif defined __EMX__                /* emx+gcc */
45   return fp->_ptr - fp->_buffer;
46 #elif defined __minix                /* Minix */
47   return fp_->_ptr - fp_->_buf;
48 #elif defined _IOERR                 /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, MSVC, NonStop Kernel, OpenVMS */
49   return (fp_->_ptr ? fp_->_ptr - fp_->_base : 0);
50 #elif defined __UCLIBC__             /* uClibc */
51   return (fp->__modeflags & __FLAG_WRITING ? fp->__bufpos - fp->__bufstart : 0);
52 #elif defined __QNX__                /* QNX */
53   return (fp->_Mode & 0x2000 /*_MWRITE*/ ? fp->_Next - fp->_Buf : 0);
54 #elif defined __MINT__               /* Atari FreeMiNT */
55   return fp->__bufp - fp->__buffer;
56 #elif defined EPLAN9                 /* Plan9 */
57   return fp->wp - fp->buf;
58 #else
59 # error "Please port gnulib fpending.c to your platform!"
60   return 1;
61 #endif
62 }
63