freadahead.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Retrieve information about a FILE stream.
  2. Copyright (C) 2007-2021 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #include <config.h>
  14. /* Specification. */
  15. #include "freadahead.h"
  16. #include <stdlib.h>
  17. #include "stdio-impl.h"
  18. #if defined __DragonFly__
  19. /* Defined in libc, but not declared in <stdio.h>. */
  20. extern size_t __sreadahead (FILE *);
  21. #endif
  22. /* This file is not used on systems that have the __freadahead function,
  23. namely musl libc. */
  24. size_t
  25. freadahead (FILE *fp)
  26. {
  27. #if defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1
  28. /* GNU libc, BeOS, Haiku, Linux libc5 */
  29. if (fp->_IO_write_ptr > fp->_IO_write_base)
  30. return 0;
  31. return (fp->_IO_read_end - fp->_IO_read_ptr)
  32. + (fp->_flags & _IO_IN_BACKUP ? fp->_IO_save_end - fp->_IO_save_base :
  33. 0);
  34. #elif defined __sferror || defined __DragonFly__ || defined __ANDROID__
  35. /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
  36. if ((fp_->_flags & __SWR) != 0 || fp_->_r < 0)
  37. return 0;
  38. # if defined __DragonFly__
  39. return __sreadahead (fp);
  40. # else
  41. return fp_->_r
  42. + (HASUB (fp) ? fp_->_ur : 0);
  43. # endif
  44. #elif defined __EMX__ /* emx+gcc */
  45. if ((fp->_flags & _IOWRT) != 0)
  46. return 0;
  47. /* Note: fp->_ungetc_count > 0 implies fp->_rcount <= 0,
  48. fp->_ungetc_count = 0 implies fp->_rcount >= 0. */
  49. /* equivalent to
  50. (fp->_ungetc_count == 0 ? fp->_rcount : fp->_ungetc_count - fp->_rcount) */
  51. return (fp->_rcount > 0 ? fp->_rcount : fp->_ungetc_count - fp->_rcount);
  52. #elif defined __minix /* Minix */
  53. if ((fp_->_flags & _IOWRITING) != 0)
  54. return 0;
  55. return fp_->_count;
  56. #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, UnixWare, mingw, MSVC, NonStop Kernel, OpenVMS */
  57. if ((fp_->_flag & _IOWRT) != 0)
  58. return 0;
  59. return fp_->_cnt;
  60. #elif defined __UCLIBC__ /* uClibc */
  61. # ifdef __STDIO_BUFFERS
  62. if (fp->__modeflags & __FLAG_WRITING)
  63. return 0;
  64. return (fp->__bufread - fp->__bufpos)
  65. + (fp->__modeflags & __FLAG_UNGOT ? 1 : 0);
  66. # else
  67. return 0;
  68. # endif
  69. #elif defined __QNX__ /* QNX */
  70. if ((fp->_Mode & 0x2000 /* _MWRITE */) != 0)
  71. return 0;
  72. /* fp->_Buf <= fp->_Next <= fp->_Rend,
  73. and fp->_Rend may be overridden by fp->_Rsave. */
  74. return ((fp->_Rsave ? fp->_Rsave : fp->_Rend) - fp->_Next)
  75. + (fp->_Mode & 0x4000 /* _MBYTE */
  76. ? (fp->_Back + sizeof (fp->_Back)) - fp->_Rback
  77. : 0);
  78. #elif defined __MINT__ /* Atari FreeMiNT */
  79. if (!fp->__mode.__read)
  80. return 0;
  81. return (fp->__pushed_back
  82. ? fp->__get_limit - fp->__pushback_bufp + 1
  83. : fp->__get_limit - fp->__bufp);
  84. #elif defined EPLAN9 /* Plan9 */
  85. if (fp->state == 4 /* WR */ || fp->rp >= fp->wp)
  86. return 0;
  87. return fp->wp - fp->rp;
  88. #elif defined SLOW_BUT_NO_HACKS /* users can define this */
  89. abort ();
  90. return 0;
  91. #else
  92. #error "Please port gnulib freadahead.c to your platform! Look at the definition of fflush, fread, ungetc on your system, then report this to bug-gnulib."
  93. #endif
  94. }