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