fpending.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. 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. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. /* Written by Jim Meyering. */
  15. #include <config.h>
  16. /* Specification. */
  17. #include "fpending.h"
  18. #include "stdio-impl.h"
  19. /* This file is not used on systems that already have the __fpending function,
  20. namely glibc >= 2.2, Solaris >= 7, UnixWare >= 7.1.4.MP4, Cygwin >= 1.7.34,
  21. Android API >= 23. */
  22. /* Return the number of pending (aka buffered, unflushed)
  23. bytes on the stream, FP, that is open for writing. */
  24. size_t
  25. __fpending (FILE *fp)
  26. {
  27. /* Most systems provide FILE as a struct and the necessary bitmask in
  28. <stdio.h>, because they need it for implementing getc() and putc() as
  29. fast macros. */
  30. #if defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1
  31. /* GNU libc, BeOS, Haiku, Linux libc5 */
  32. return fp->_IO_write_ptr - fp->_IO_write_base;
  33. #elif defined __sferror || defined __DragonFly__ || defined __ANDROID__
  34. /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin < 1.7.34, Minix 3, Android */
  35. return fp->_p - fp->_bf._base;
  36. #elif defined __EMX__ /* emx+gcc */
  37. return fp->_ptr - fp->_buffer;
  38. #elif defined __minix /* Minix */
  39. return fp_->_ptr - fp_->_buf;
  40. #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, UnixWare, mingw, MSVC, NonStop Kernel, OpenVMS */
  41. return (fp_->_ptr ? fp_->_ptr - fp_->_base : 0);
  42. #elif defined __UCLIBC__ /* uClibc */
  43. return (fp->__modeflags & __FLAG_WRITING ? fp->__bufpos - fp->__bufstart : 0);
  44. #elif defined __QNX__ /* QNX */
  45. return (fp->_Mode & 0x2000 /*_MWRITE*/ ? fp->_Next - fp->_Buf : 0);
  46. #elif defined __MINT__ /* Atari FreeMiNT */
  47. return fp->__bufp - fp->__buffer;
  48. #elif defined EPLAN9 /* Plan9 */
  49. return fp->wp - fp->buf;
  50. #else
  51. # error "Please port gnulib fpending.c to your platform!"
  52. return 1;
  53. #endif
  54. }