fseeko.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* An fseeko() function that, together with fflush(), is POSIX compliant.
  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, or (at your option)
  6. 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 along
  12. with this program; if not, see <http://www.gnu.org/licenses/>. */
  13. #include <config.h>
  14. /* Specification. */
  15. #include <stdio.h>
  16. /* Get off_t, lseek, _POSIX_VERSION. */
  17. #include <unistd.h>
  18. #include "stdio-impl.h"
  19. int
  20. fseeko (FILE *fp, off_t offset, int whence)
  21. #undef fseeko
  22. #if !HAVE_FSEEKO
  23. # undef fseek
  24. # define fseeko fseek
  25. #endif
  26. #if _GL_WINDOWS_64_BIT_OFF_T
  27. # undef fseeko
  28. # if HAVE__FSEEKI64 /* msvc, mingw64 */
  29. # define fseeko _fseeki64
  30. # else /* mingw */
  31. # define fseeko fseeko64
  32. # endif
  33. #endif
  34. {
  35. #if LSEEK_PIPE_BROKEN
  36. /* mingw gives bogus answers rather than failure on non-seekable files. */
  37. if (lseek (fileno (fp), 0, SEEK_CUR) == -1)
  38. return EOF;
  39. #endif
  40. /* These tests are based on fpurge.c. */
  41. #if defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
  42. if (fp->_IO_read_end == fp->_IO_read_ptr
  43. && fp->_IO_write_ptr == fp->_IO_write_base
  44. && fp->_IO_save_base == NULL)
  45. #elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
  46. # if defined __SL64 && defined __SCLE /* Cygwin */
  47. if ((fp->_flags & __SL64) == 0)
  48. {
  49. /* Cygwin 1.5.0 through 1.5.24 failed to open stdin in 64-bit
  50. mode; but has an fseeko that requires 64-bit mode. */
  51. FILE *tmp = fopen ("/dev/null", "r");
  52. if (!tmp)
  53. return -1;
  54. fp->_flags |= __SL64;
  55. fp->_seek64 = tmp->_seek64;
  56. fclose (tmp);
  57. }
  58. # endif
  59. if (fp_->_p == fp_->_bf._base
  60. && fp_->_r == 0
  61. && fp_->_w == ((fp_->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully buffered and not currently reading? */
  62. ? fp_->_bf._size
  63. : 0)
  64. && fp_ub._base == NULL)
  65. #elif defined __EMX__ /* emx+gcc */
  66. if (fp->_ptr == fp->_buffer
  67. && fp->_rcount == 0
  68. && fp->_wcount == 0
  69. && fp->_ungetc_count == 0)
  70. #elif defined __minix /* Minix */
  71. if (fp_->_ptr == fp_->_buf
  72. && (fp_->_ptr == NULL || fp_->_count == 0))
  73. #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
  74. if (fp_->_ptr == fp_->_base
  75. && (fp_->_ptr == NULL || fp_->_cnt == 0))
  76. #elif WIN_SDK10
  77. if (((TWinSdk10File*)fp)->_ptr == ((TWinSdk10File*)fp)->_base
  78. && (((TWinSdk10File*)fp)->_ptr == NULL || ((TWinSdk10File*)fp)->_cnt == 0))
  79. #elif defined __UCLIBC__ /* uClibc */
  80. if (((fp->__modeflags & __FLAG_WRITING) == 0
  81. || fp->__bufpos == fp->__bufstart)
  82. && ((fp->__modeflags & (__FLAG_READONLY | __FLAG_READING)) == 0
  83. || fp->__bufpos == fp->__bufread))
  84. #elif defined __QNX__ /* QNX */
  85. if ((fp->_Mode & 0x2000 /* _MWRITE */ ? fp->_Next == fp->_Buf : fp->_Next == fp->_Rend)
  86. && fp->_Rback == fp->_Back + sizeof (fp->_Back)
  87. && fp->_Rsave == NULL)
  88. #elif defined __MINT__ /* Atari FreeMiNT */
  89. if (fp->__bufp == fp->__buffer
  90. && fp->__get_limit == fp->__bufp
  91. && fp->__put_limit == fp->__bufp
  92. && !fp->__pushed_back)
  93. #elif defined EPLAN9 /* Plan9 */
  94. if (fp->rp == fp->buf
  95. && fp->wp == fp->buf)
  96. #elif FUNC_FFLUSH_STDIN < 0 && 200809 <= _POSIX_VERSION
  97. /* Cross-compiling to some other system advertising conformance to
  98. POSIX.1-2008 or later. Assume fseeko and fflush work as advertised.
  99. If this assumption is incorrect, please report the bug to
  100. bug-gnulib. */
  101. if (0)
  102. #else
  103. #error "Please port gnulib fseeko.c to your platform! Look at the code in fseeko.c, then report this to bug-gnulib."
  104. #endif
  105. {
  106. /* We get here when an fflush() call immediately preceded this one (or
  107. if ftell() has created buffers but no I/O has occurred on a
  108. newly-opened stream). We know there are no buffers. */
  109. off_t pos = lseek (fileno (fp), offset, whence);
  110. if (pos == -1)
  111. {
  112. #if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
  113. fp_->_flags &= ~__SOFF;
  114. #endif
  115. return -1;
  116. }
  117. #if defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
  118. fp->_flags &= ~_IO_EOF_SEEN;
  119. fp->_offset = pos;
  120. #elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
  121. # if defined __CYGWIN__
  122. /* fp_->_offset is typed as an integer. */
  123. fp_->_offset = pos;
  124. # else
  125. /* fp_->_offset is an fpos_t. */
  126. {
  127. /* Use a union, since on NetBSD, the compilation flags
  128. determine whether fpos_t is typedef'd to off_t or a struct
  129. containing a single off_t member. */
  130. union
  131. {
  132. fpos_t f;
  133. off_t o;
  134. } u;
  135. u.o = pos;
  136. fp_->_offset = u.f;
  137. }
  138. # endif
  139. fp_->_flags |= __SOFF;
  140. fp_->_flags &= ~__SEOF;
  141. #elif defined __EMX__ /* emx+gcc */
  142. fp->_flags &= ~_IOEOF;
  143. #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
  144. fp->_flag &= ~_IOEOF;
  145. #elif defined __MINT__ /* Atari FreeMiNT */
  146. fp->__offset = pos;
  147. fp->__eof = 0;
  148. #endif
  149. return 0;
  150. }
  151. return fseeko (fp, offset, whence);
  152. }