1 /* An fseeko() function that, together with fflush(), is POSIX compliant.
2 Copyright (C) 2007-2016 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, see <http://www.gnu.org/licenses/>. */
22 /* Get off_t, lseek, _POSIX_VERSION. */
25 #include "stdio-impl.h"
28 fseeko (FILE *fp, off_t offset, int whence)
34 #if _GL_WINDOWS_64_BIT_OFF_T
36 # if HAVE__FSEEKI64 /* msvc, mingw64 */
37 # define fseeko _fseeki64
39 # define fseeko fseeko64
44 /* mingw gives bogus answers rather than failure on non-seekable files. */
45 if (lseek (fileno (fp), 0, SEEK_CUR) == -1)
49 /* These tests are based on fpurge.c. */
50 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
51 if (fp->_IO_read_end == fp->_IO_read_ptr
52 && fp->_IO_write_ptr == fp->_IO_write_base
53 && fp->_IO_save_base == NULL)
54 #elif defined __sferror || defined __DragonFly__ || defined __ANDROID__
55 /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Android */
56 # if defined __SL64 && defined __SCLE /* Cygwin */
57 if ((fp->_flags & __SL64) == 0)
59 /* Cygwin 1.5.0 through 1.5.24 failed to open stdin in 64-bit
60 mode; but has an fseeko that requires 64-bit mode. */
61 FILE *tmp = fopen ("/dev/null", "r");
65 fp->_seek64 = tmp->_seek64;
69 if (fp_->_p == fp_->_bf._base
71 && fp_->_w == ((fp_->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully buffered and not currently reading? */
74 && fp_ub._base == NULL)
75 #elif defined __EMX__ /* emx+gcc */
76 if (fp->_ptr == fp->_buffer
79 && fp->_ungetc_count == 0)
80 #elif defined __minix /* Minix */
81 if (fp_->_ptr == fp_->_buf
82 && (fp_->_ptr == NULL || fp_->_count == 0))
83 #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
84 if (fp_->_ptr == fp_->_base
85 && (fp_->_ptr == NULL || fp_->_cnt == 0))
86 #elif defined __UCLIBC__ /* uClibc */
87 if (((fp->__modeflags & __FLAG_WRITING) == 0
88 || fp->__bufpos == fp->__bufstart)
89 && ((fp->__modeflags & (__FLAG_READONLY | __FLAG_READING)) == 0
90 || fp->__bufpos == fp->__bufread))
91 #elif defined __QNX__ /* QNX */
92 if ((fp->_Mode & 0x2000 /* _MWRITE */ ? fp->_Next == fp->_Buf : fp->_Next == fp->_Rend)
93 && fp->_Rback == fp->_Back + sizeof (fp->_Back)
94 && fp->_Rsave == NULL)
95 #elif defined __MINT__ /* Atari FreeMiNT */
96 if (fp->__bufp == fp->__buffer
97 && fp->__get_limit == fp->__bufp
98 && fp->__put_limit == fp->__bufp
99 && !fp->__pushed_back)
100 #elif defined EPLAN9 /* Plan9 */
101 if (fp->rp == fp->buf
102 && fp->wp == fp->buf)
103 #elif FUNC_FFLUSH_STDIN < 0 && 200809 <= _POSIX_VERSION
104 /* Cross-compiling to some other system advertising conformance to
105 POSIX.1-2008 or later. Assume fseeko and fflush work as advertised.
106 If this assumption is incorrect, please report the bug to
110 #error "Please port gnulib fseeko.c to your platform! Look at the code in fseeko.c, then report this to bug-gnulib."
113 /* We get here when an fflush() call immediately preceded this one (or
114 if ftell() has created buffers but no I/O has occurred on a
115 newly-opened stream). We know there are no buffers. */
116 off_t pos = lseek (fileno (fp), offset, whence);
119 #if defined __sferror || defined __DragonFly__ || defined __ANDROID__
120 /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Android */
121 fp_->_flags &= ~__SOFF;
126 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
127 fp->_flags &= ~_IO_EOF_SEEN;
129 #elif defined __sferror || defined __DragonFly__ || defined __ANDROID__
130 /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Android */
131 # if defined __CYGWIN__ || (defined __NetBSD__ && __NetBSD_Version__ >= 600000000)
132 /* fp_->_offset is typed as an integer. */
135 /* fp_->_offset is an fpos_t. */
137 /* Use a union, since on NetBSD, the compilation flags
138 determine whether fpos_t is typedef'd to off_t or a struct
139 containing a single off_t member. */
149 fp_->_flags |= __SOFF;
150 fp_->_flags &= ~__SEOF;
151 #elif defined __EMX__ /* emx+gcc */
152 fp->_flags &= ~_IOEOF;
153 #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
154 fp->_flag &= ~_IOEOF;
155 #elif defined __MINT__ /* Atari FreeMiNT */
161 return fseeko (fp, offset, whence);