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