some more text for TPMs
[gnutls.git] / gl / fseeko.c
blob7364f7ffef1ed0885ae15ad18734049c7608601b
1 /* An fseeko() function that, together with fflush(), is POSIX compliant.
2 Copyright (C) 2007-2012 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, see <http://www.gnu.org/licenses/>. */
17 #include <config.h>
19 /* Specification. */
20 #include <stdio.h>
22 /* Get off_t and lseek. */
23 #include <unistd.h>
25 #include "stdio-impl.h"
27 int
28 fseeko (FILE *fp, off_t offset, int whence)
29 #undef fseeko
30 #if !HAVE_FSEEKO
31 # undef fseek
32 # define fseeko fseek
33 #endif
34 #if _GL_WINDOWS_64_BIT_OFF_T
35 # undef fseeko
36 # if HAVE__FSEEKI64 /* msvc, mingw64 */
37 # define fseeko _fseeki64
38 # else /* mingw */
39 # define fseeko fseeko64
40 # endif
41 #endif
43 #if LSEEK_PIPE_BROKEN
44 /* mingw gives bogus answers rather than failure on non-seekable files. */
45 if (lseek (fileno (fp), 0, SEEK_CUR) == -1)
46 return EOF;
47 #endif
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__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
55 # if defined __SL64 && defined __SCLE /* Cygwin */
56 if ((fp->_flags & __SL64) == 0)
58 /* Cygwin 1.5.0 through 1.5.24 failed to open stdin in 64-bit
59 mode; but has an fseeko that requires 64-bit mode. */
60 FILE *tmp = fopen ("/dev/null", "r");
61 if (!tmp)
62 return -1;
63 fp->_flags |= __SL64;
64 fp->_seek64 = tmp->_seek64;
65 fclose (tmp);
67 # endif
68 if (fp_->_p == fp_->_bf._base
69 && fp_->_r == 0
70 && fp_->_w == ((fp_->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully buffered and not currently reading? */
71 ? fp_->_bf._size
72 : 0)
73 && fp_ub._base == NULL)
74 #elif defined __EMX__ /* emx+gcc */
75 if (fp->_ptr == fp->_buffer
76 && fp->_rcount == 0
77 && fp->_wcount == 0
78 && fp->_ungetc_count == 0)
79 #elif defined __minix /* Minix */
80 if (fp_->_ptr == fp_->_buf
81 && (fp_->_ptr == NULL || fp_->_count == 0))
82 #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
83 if (fp_->_ptr == fp_->_base
84 && (fp_->_ptr == NULL || fp_->_cnt == 0))
85 #elif defined __UCLIBC__ /* uClibc */
86 if (((fp->__modeflags & __FLAG_WRITING) == 0
87 || fp->__bufpos == fp->__bufstart)
88 && ((fp->__modeflags & (__FLAG_READONLY | __FLAG_READING)) == 0
89 || fp->__bufpos == fp->__bufread))
90 #elif defined __QNX__ /* QNX */
91 if ((fp->_Mode & 0x2000 /* _MWRITE */ ? fp->_Next == fp->_Buf : fp->_Next == fp->_Rend)
92 && fp->_Rback == fp->_Back + sizeof (fp->_Back)
93 && fp->_Rsave == NULL)
94 #elif defined __MINT__ /* Atari FreeMiNT */
95 if (fp->__bufp == fp->__buffer
96 && fp->__get_limit == fp->__bufp
97 && fp->__put_limit == fp->__bufp
98 && !fp->__pushed_back)
99 #elif defined EPLAN9 /* Plan9 */
100 if (fp->rp == fp->buf
101 && fp->wp == fp->buf)
102 #else
103 #error "Please port gnulib fseeko.c to your platform! Look at the code in fpurge.c, then report this to bug-gnulib."
104 #endif
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)
112 #if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
113 fp_->_flags &= ~__SOFF;
114 #endif
115 return -1;
118 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
119 fp->_flags &= ~_IO_EOF_SEEN;
120 fp->_offset = pos;
121 #elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
122 # if defined __CYGWIN__
123 /* fp_->_offset is typed as an integer. */
124 fp_->_offset = pos;
125 # else
126 /* fp_->_offset is an fpos_t. */
128 /* Use a union, since on NetBSD, the compilation flags
129 determine whether fpos_t is typedef'd to off_t or a struct
130 containing a single off_t member. */
131 union
133 fpos_t f;
134 off_t o;
135 } u;
136 u.o = pos;
137 fp_->_offset = u.f;
139 # endif
140 fp_->_flags |= __SOFF;
141 fp_->_flags &= ~__SEOF;
142 #elif defined __EMX__ /* emx+gcc */
143 fp->_flags &= ~_IOEOF;
144 #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
145 fp->_flag &= ~_IOEOF;
146 #elif defined __MINT__ /* Atari FreeMiNT */
147 fp->__offset = pos;
148 fp->__eof = 0;
149 #endif
150 return 0;
152 return fseeko (fp, offset, whence);