freading.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 "freading.h"
  16. #include "stdio-impl.h"
  17. /* Don't use glibc's __freading function in glibc < 2.7, see
  18. <http://sourceware.org/bugzilla/show_bug.cgi?id=4359> */
  19. #if !(HAVE___FREADING && (!defined __GLIBC__ || defined __UCLIBC__ || __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)))
  20. bool
  21. freading (FILE *fp)
  22. {
  23. /* Most systems provide FILE as a struct and the necessary bitmask in
  24. <stdio.h>, because they need it for implementing getc() and putc() as
  25. fast macros. */
  26. # if defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
  27. return ((fp->_flags & _IO_NO_WRITES) != 0
  28. || ((fp->_flags & (_IO_NO_READS | _IO_CURRENTLY_PUTTING)) == 0
  29. && fp->_IO_read_base != NULL));
  30. # elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
  31. return (fp_->_flags & __SRD) != 0;
  32. # elif defined __EMX__ /* emx+gcc */
  33. return (fp->_flags & _IOREAD) != 0;
  34. # elif defined __minix /* Minix */
  35. return (fp->_flags & _IOREADING) != 0;
  36. # elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
  37. # if defined __sun /* Solaris */
  38. return (fp->_flag & _IOREAD) != 0 && (fp->_flag & _IOWRT) == 0;
  39. # else
  40. return (fp->_flag & _IOREAD) != 0;
  41. # endif
  42. # elif WIN_SDK10
  43. return (((TWinSdk10File*)fp)->_flags & WIN_SDK10_IOREAD) != 0;
  44. # elif defined __UCLIBC__ /* uClibc */
  45. return (fp->__modeflags & (__FLAG_READONLY | __FLAG_READING)) != 0;
  46. # elif defined __QNX__ /* QNX */
  47. return ((fp->_Mode & 0x2 /* _MOPENW */) == 0
  48. || (fp->_Mode & 0x1000 /* _MREAD */) != 0);
  49. # elif defined __MINT__ /* Atari FreeMiNT */
  50. if (!fp->__mode.__write)
  51. return 1;
  52. if (!fp->__mode.__read)
  53. return 0;
  54. # ifdef _IO_CURRENTLY_GETTING /* Flag added on 2009-02-28 */
  55. return (fp->__flags & _IO_CURRENTLY_GETTING) != 0;
  56. # else
  57. return (fp->__buffer < fp->__get_limit /*|| fp->__bufp == fp->__put_limit ??*/);
  58. # endif
  59. # elif defined EPLAN9 /* Plan9 */
  60. if (fp->state == 0 /* CLOSED */ || fp->state == 4 /* WR */)
  61. return 0;
  62. return (fp->state == 3 /* RD */ && (fp->bufl == 0 || fp->rp < fp->wp));
  63. # else
  64. # error "Please port gnulib freading.c to your platform!"
  65. # endif
  66. }
  67. #endif