5 kx /* An fseeko() function that, together with fflush(), is POSIX compliant.
5 kx Copyright (C) 2007-2015 Free Software Foundation, Inc.
5 kx
5 kx This program is free software; you can redistribute it and/or modify
5 kx it under the terms of the GNU General Public License as published by
5 kx the Free Software Foundation; either version 3, or (at your option)
5 kx any later version.
5 kx
5 kx This program is distributed in the hope that it will be useful,
5 kx but WITHOUT ANY WARRANTY; without even the implied warranty of
5 kx MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5 kx GNU General Public License for more details.
5 kx
5 kx You should have received a copy of the GNU General Public License along
5 kx with this program; if not, see <http://www.gnu.org/licenses/>. */
5 kx
5 kx #include <config.h>
5 kx
5 kx /* Specification. */
5 kx #include <stdio.h>
5 kx
5 kx /* Get off_t, lseek, _POSIX_VERSION. */
5 kx #include <unistd.h>
5 kx
5 kx #include "stdio-impl.h"
5 kx
5 kx int
5 kx fseeko (FILE *fp, off_t offset, int whence)
5 kx #undef fseeko
5 kx #if !HAVE_FSEEKO
5 kx # undef fseek
5 kx # define fseeko fseek
5 kx #endif
5 kx #if _GL_WINDOWS_64_BIT_OFF_T
5 kx # undef fseeko
5 kx # if HAVE__FSEEKI64 /* msvc, mingw64 */
5 kx # define fseeko _fseeki64
5 kx # else /* mingw */
5 kx # define fseeko fseeko64
5 kx # endif
5 kx #endif
5 kx {
5 kx #if LSEEK_PIPE_BROKEN
5 kx /* mingw gives bogus answers rather than failure on non-seekable files. */
5 kx if (lseek (fileno (fp), 0, SEEK_CUR) == -1)
5 kx return EOF;
5 kx #endif
5 kx
5 kx /* These tests are based on fpurge.c. */
5 kx #if defined _IO_EOF_SEEN || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
5 kx if (fp->_IO_read_end == fp->_IO_read_ptr
5 kx && fp->_IO_write_ptr == fp->_IO_write_base
5 kx && fp->_IO_save_base == NULL)
5 kx #elif defined __sferror || defined __DragonFly__ || defined __ANDROID__
5 kx /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Android */
5 kx # if defined __SL64 && defined __SCLE /* Cygwin */
5 kx if ((fp->_flags & __SL64) == 0)
5 kx {
5 kx /* Cygwin 1.5.0 through 1.5.24 failed to open stdin in 64-bit
5 kx mode; but has an fseeko that requires 64-bit mode. */
5 kx FILE *tmp = fopen ("/dev/null", "r");
5 kx if (!tmp)
5 kx return -1;
5 kx fp->_flags |= __SL64;
5 kx fp->_seek64 = tmp->_seek64;
5 kx fclose (tmp);
5 kx }
5 kx # endif
5 kx if (fp_->_p == fp_->_bf._base
5 kx && fp_->_r == 0
5 kx && fp_->_w == ((fp_->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully buffered and not currently reading? */
5 kx ? fp_->_bf._size
5 kx : 0)
5 kx && fp_ub._base == NULL)
5 kx #elif defined __EMX__ /* emx+gcc */
5 kx if (fp->_ptr == fp->_buffer
5 kx && fp->_rcount == 0
5 kx && fp->_wcount == 0
5 kx && fp->_ungetc_count == 0)
5 kx #elif defined __minix /* Minix */
5 kx if (fp_->_ptr == fp_->_buf
5 kx && (fp_->_ptr == NULL || fp_->_count == 0))
5 kx #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
5 kx if (fp_->_ptr == fp_->_base
5 kx && (fp_->_ptr == NULL || fp_->_cnt == 0))
5 kx #elif defined __UCLIBC__ /* uClibc */
5 kx if (((fp->__modeflags & __FLAG_WRITING) == 0
5 kx || fp->__bufpos == fp->__bufstart)
5 kx && ((fp->__modeflags & (__FLAG_READONLY | __FLAG_READING)) == 0
5 kx || fp->__bufpos == fp->__bufread))
5 kx #elif defined __QNX__ /* QNX */
5 kx if ((fp->_Mode & 0x2000 /* _MWRITE */ ? fp->_Next == fp->_Buf : fp->_Next == fp->_Rend)
5 kx && fp->_Rback == fp->_Back + sizeof (fp->_Back)
5 kx && fp->_Rsave == NULL)
5 kx #elif defined __MINT__ /* Atari FreeMiNT */
5 kx if (fp->__bufp == fp->__buffer
5 kx && fp->__get_limit == fp->__bufp
5 kx && fp->__put_limit == fp->__bufp
5 kx && !fp->__pushed_back)
5 kx #elif defined EPLAN9 /* Plan9 */
5 kx if (fp->rp == fp->buf
5 kx && fp->wp == fp->buf)
5 kx #elif FUNC_FFLUSH_STDIN < 0 && 200809 <= _POSIX_VERSION
5 kx /* Cross-compiling to some other system advertising conformance to
5 kx POSIX.1-2008 or later. Assume fseeko and fflush work as advertised.
5 kx If this assumption is incorrect, please report the bug to
5 kx bug-gnulib. */
5 kx if (0)
5 kx #else
5 kx #error "Please port gnulib fseeko.c to your platform! Look at the code in fseeko.c, then report this to bug-gnulib."
5 kx #endif
5 kx {
5 kx /* We get here when an fflush() call immediately preceded this one (or
5 kx if ftell() has created buffers but no I/O has occurred on a
5 kx newly-opened stream). We know there are no buffers. */
5 kx off_t pos = lseek (fileno (fp), offset, whence);
5 kx if (pos == -1)
5 kx {
5 kx #if defined __sferror || defined __DragonFly__ || defined __ANDROID__
5 kx /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Android */
5 kx fp_->_flags &= ~__SOFF;
5 kx #endif
5 kx return -1;
5 kx }
5 kx
5 kx #if defined _IO_EOF_SEEN || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
5 kx fp->_flags &= ~_IO_EOF_SEEN;
5 kx fp->_offset = pos;
5 kx #elif defined __sferror || defined __DragonFly__ || defined __ANDROID__
5 kx /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Android */
5 kx # if defined __CYGWIN__ || (defined __NetBSD__ && __NetBSD_Version__ >= 600000000)
5 kx /* fp_->_offset is typed as an integer. */
5 kx fp_->_offset = pos;
5 kx # else
5 kx /* fp_->_offset is an fpos_t. */
5 kx {
5 kx /* Use a union, since on NetBSD, the compilation flags
5 kx determine whether fpos_t is typedef'd to off_t or a struct
5 kx containing a single off_t member. */
5 kx union
5 kx {
5 kx fpos_t f;
5 kx off_t o;
5 kx } u;
5 kx u.o = pos;
5 kx fp_->_offset = u.f;
5 kx }
5 kx # endif
5 kx fp_->_flags |= __SOFF;
5 kx fp_->_flags &= ~__SEOF;
5 kx #elif defined __EMX__ /* emx+gcc */
5 kx fp->_flags &= ~_IOEOF;
5 kx #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
5 kx fp->_flag &= ~_IOEOF;
5 kx #elif defined __MINT__ /* Atari FreeMiNT */
5 kx fp->__offset = pos;
5 kx fp->__eof = 0;
5 kx #endif
5 kx return 0;
5 kx }
5 kx return fseeko (fp, offset, whence);
5 kx }