| 1 | /* An fseeko() function that, together with fflush(), is POSIX compliant. |
| 2 | Copyright (C) 2007-2011 Free Software Foundation, Inc. |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU Lesser General Public License as published by |
| 6 | the Free Software Foundation; either version 2, or (at your option) |
| 7 | any later version. |
| 8 | |
| 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 Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public License along |
| 15 | with this program; if not, write to the Free Software Foundation, |
| 16 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
| 17 | |
| 18 | #include <config.h> |
| 19 | |
| 20 | /* Specification. */ |
| 21 | #include <stdio.h> |
| 22 | |
| 23 | /* Get off_t and lseek. */ |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include "stdio-impl.h" |
| 27 | |
| 28 | int |
| 29 | fseeko (FILE *fp, off_t offset, int whence) |
| 30 | #undef fseeko |
| 31 | #if !HAVE_FSEEKO |
| 32 | # undef fseek |
| 33 | # define fseeko fseek |
| 34 | #endif |
| 35 | { |
| 36 | #if LSEEK_PIPE_BROKEN |
| 37 | /* mingw gives bogus answers rather than failure on non-seekable files. */ |
| 38 | if (lseek (fileno (fp), 0, SEEK_CUR) == -1) |
| 39 | return EOF; |
| 40 | #endif |
| 41 | |
| 42 | /* These tests are based on fpurge.c. */ |
| 43 | #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */ |
| 44 | if (fp->_IO_read_end == fp->_IO_read_ptr |
| 45 | && fp->_IO_write_ptr == fp->_IO_write_base |
| 46 | && fp->_IO_save_base == NULL) |
| 47 | #elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */ |
| 48 | # if defined __SL64 && defined __SCLE /* Cygwin */ |
| 49 | if ((fp->_flags & __SL64) == 0) |
| 50 | { |
| 51 | /* Cygwin 1.5.0 through 1.5.24 failed to open stdin in 64-bit |
| 52 | mode; but has an fseeko that requires 64-bit mode. */ |
| 53 | FILE *tmp = fopen ("/dev/null", "r"); |
| 54 | if (!tmp) |
| 55 | return -1; |
| 56 | fp->_flags |= __SL64; |
| 57 | fp->_seek64 = tmp->_seek64; |
| 58 | fclose (tmp); |
| 59 | } |
| 60 | # endif |
| 61 | if (fp_->_p == fp_->_bf._base |
| 62 | && fp_->_r == 0 |
| 63 | && fp_->_w == ((fp_->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully buffered and not currently reading? */ |
| 64 | ? fp_->_bf._size |
| 65 | : 0) |
| 66 | && fp_ub._base == NULL) |
| 67 | #elif defined __EMX__ /* emx+gcc */ |
| 68 | if (fp->_ptr == fp->_buffer |
| 69 | && fp->_rcount == 0 |
| 70 | && fp->_wcount == 0 |
| 71 | && fp->_ungetc_count == 0) |
| 72 | #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */ |
| 73 | if (fp_->_ptr == fp_->_base |
| 74 | && (fp_->_ptr == NULL || fp_->_cnt == 0)) |
| 75 | #elif defined __UCLIBC__ /* uClibc */ |
| 76 | if (((fp->__modeflags & __FLAG_WRITING) == 0 |
| 77 | || fp->__bufpos == fp->__bufstart) |
| 78 | && ((fp->__modeflags & (__FLAG_READONLY | __FLAG_READING)) == 0 |
| 79 | || fp->__bufpos == fp->__bufread)) |
| 80 | #elif defined __QNX__ /* QNX */ |
| 81 | if ((fp->_Mode & 0x2000 /* _MWRITE */ ? fp->_Next == fp->_Buf : fp->_Next == fp->_Rend) |
| 82 | && fp->_Rback == fp->_Back + sizeof (fp->_Back) |
| 83 | && fp->_Rsave == NULL) |
| 84 | #elif defined __MINT__ /* Atari FreeMiNT */ |
| 85 | if (fp->__bufp == fp->__buffer |
| 86 | && fp->__get_limit == fp->__bufp |
| 87 | && fp->__put_limit == fp->__bufp |
| 88 | && !fp->__pushed_back) |
| 89 | #else |
| 90 | #error "Please port gnulib fseeko.c to your platform! Look at the code in fpurge.c, then report this to bug-gnulib." |
| 91 | #endif |
| 92 | { |
| 93 | /* We get here when an fflush() call immediately preceded this one. We |
| 94 | know there are no buffers. |
| 95 | POSIX requires us to modify the file descriptor's position. |
| 96 | But we cannot position beyond end of file here. */ |
| 97 | off_t pos = |
| 98 | lseek (fileno (fp), |
| 99 | whence == SEEK_END && offset > 0 ? 0 : offset, |
| 100 | whence); |
| 101 | if (pos == -1) |
| 102 | { |
| 103 | #if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */ |
| 104 | fp_->_flags &= ~__SOFF; |
| 105 | #endif |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */ |
| 110 | fp->_flags &= ~_IO_EOF_SEEN; |
| 111 | #elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */ |
| 112 | # if defined __CYGWIN__ |
| 113 | /* fp_->_offset is typed as an integer. */ |
| 114 | fp_->_offset = pos; |
| 115 | # else |
| 116 | /* fp_->_offset is an fpos_t. */ |
| 117 | { |
| 118 | /* Use a union, since on NetBSD, the compilation flags |
| 119 | determine whether fpos_t is typedef'd to off_t or a struct |
| 120 | containing a single off_t member. */ |
| 121 | union |
| 122 | { |
| 123 | fpos_t f; |
| 124 | off_t o; |
| 125 | } u; |
| 126 | u.o = pos; |
| 127 | fp_->_offset = u.f; |
| 128 | } |
| 129 | # endif |
| 130 | fp_->_flags |= __SOFF; |
| 131 | fp_->_flags &= ~__SEOF; |
| 132 | #elif defined __EMX__ /* emx+gcc */ |
| 133 | fp->_flags &= ~_IOEOF; |
| 134 | #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */ |
| 135 | fp->_flag &= ~_IOEOF; |
| 136 | #elif defined __MINT__ /* Atari FreeMiNT */ |
| 137 | fp->__offset = pos; |
| 138 | fp->__eof = 0; |
| 139 | #endif |
| 140 | /* If we were not requested to position beyond end of file, we're |
| 141 | done. */ |
| 142 | if (!(whence == SEEK_END && offset > 0)) |
| 143 | return 0; |
| 144 | } |
| 145 | return fseeko (fp, offset, whence); |
| 146 | } |