freadahead.c 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Retrieve information about a FILE stream.
  2. Copyright (C) 2007-2013 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 <http://www.gnu.org/licenses/>. */
  13. #include <config.h>
  14. /* Specification. */
  15. #include "freadahead.h"
  16. #include <stdlib.h>
  17. #include "stdio-impl.h"
  18. size_t
  19. freadahead (FILE *fp)
  20. {
  21. #if defined _IO_EOF_SEEN || defined _IO_EOF_SEEN || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
  22. if (fp->_IO_write_ptr > fp->_IO_write_base)
  23. return 0;
  24. return (fp->_IO_read_end - fp->_IO_read_ptr)
  25. + (fp->_flags & _IO_IN_BACKUP ? fp->_IO_save_end - fp->_IO_save_base :
  26. 0);
  27. #elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
  28. if ((fp_->_flags & __SWR) != 0 || fp_->_r < 0)
  29. return 0;
  30. # if defined __DragonFly__
  31. return __sreadahead (fp);
  32. # else
  33. return fp_->_r
  34. + (HASUB (fp) ? fp_->_ur : 0);
  35. # endif
  36. #elif defined __EMX__ /* emx+gcc */
  37. if ((fp->_flags & _IOWRT) != 0)
  38. return 0;
  39. /* Note: fp->_ungetc_count > 0 implies fp->_rcount <= 0,
  40. fp->_ungetc_count = 0 implies fp->_rcount >= 0. */
  41. /* equivalent to
  42. (fp->_ungetc_count == 0 ? fp->_rcount : fp->_ungetc_count - fp->_rcount) */
  43. return (fp->_rcount > 0 ? fp->_rcount : fp->_ungetc_count - fp->_rcount);
  44. #elif defined __minix /* Minix */
  45. if ((fp_->_flags & _IOWRITING) != 0)
  46. return 0;
  47. return fp_->_count;
  48. #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
  49. if ((fp_->_flag & _IOWRT) != 0)
  50. return 0;
  51. return fp_->_cnt;
  52. #elif WIN_SDK10
  53. if ((((TWinSdk10File*)fp)->_flags & WIN_SDK10_IOWRITE) != 0)
  54. return 0;
  55. return ((TWinSdk10File*)fp)->_cnt;
  56. #elif defined __UCLIBC__ /* uClibc */
  57. # ifdef __STDIO_BUFFERS
  58. if (fp->__modeflags & __FLAG_WRITING)
  59. return 0;
  60. return (fp->__bufread - fp->__bufpos)
  61. + (fp->__modeflags & __FLAG_UNGOT ? 1 : 0);
  62. # else
  63. return 0;
  64. # endif
  65. #elif defined __QNX__ /* QNX */
  66. if ((fp->_Mode & 0x2000 /* _MWRITE */) != 0)
  67. return 0;
  68. /* fp->_Buf <= fp->_Next <= fp->_Rend,
  69. and fp->_Rend may be overridden by fp->_Rsave. */
  70. return ((fp->_Rsave ? fp->_Rsave : fp->_Rend) - fp->_Next)
  71. + (fp->_Mode & 0x4000 /* _MBYTE */
  72. ? (fp->_Back + sizeof (fp->_Back)) - fp->_Rback
  73. : 0);
  74. #elif defined __MINT__ /* Atari FreeMiNT */
  75. if (!fp->__mode.__read)
  76. return 0;
  77. return (fp->__pushed_back
  78. ? fp->__get_limit - fp->__pushback_bufp + 1
  79. : fp->__get_limit - fp->__bufp);
  80. #elif defined EPLAN9 /* Plan9 */
  81. if (fp->state == 4 /* WR */ || fp->rp >= fp->wp)
  82. return 0;
  83. return fp->wp - fp->rp;
  84. #elif defined SLOW_BUT_NO_HACKS /* users can define this */
  85. abort ();
  86. return 0;
  87. #else
  88. #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."
  89. #endif
  90. }