Debian Codegraph @

lib/fseeko.c

raw
1 /* An fseeko() function that, together with fflush(), is POSIX compliant.
2 Copyright (C) 2007-2026 Free Software Foundation, Inc.
3
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
8
9 This file 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
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 /* Specification. */
20 #include <stdio.h>
21
22 /* Get off_t, lseek, _POSIX_VERSION. */
23 #include <unistd.h>
24
25 #include "stdio-impl.h"
26
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 && HAVE_DECL__FSEEKI64 /* msvc, mingw since msvcrt8.0, mingw64 */
37 # define fseeko _fseeki64
38 # else /* mingw before msvcrt8.0 */
39 # define fseeko fseeko64
40 # endif
41 #endif
42 {
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
48
49 /* These tests are based on fpurge.c. */
50 #if defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1
51 # if !defined __HAIKU__
52 # define fp_ fp
53 # endif
54 /* GNU libc, BeOS, Haiku, Linux libc5 */
55 if (fp_->_IO_read_end == fp_->_IO_read_ptr
56 && fp_->_IO_write_ptr == fp_->_IO_write_base
57 && fp_->_IO_save_base == NULL)
58 #elif defined __sferror || defined __DragonFly__ || defined __ANDROID__
59 /* FreeBSD, NetBSD, OpenBSD <= 7.7, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
60 # if defined __SL64 && defined __SCLE /* Cygwin */
61 if ((fp->_flags & __SL64) == 0)
62 {
63 /* Cygwin 1.5.0 through 1.5.24 failed to open stdin in 64-bit
64 mode; but has an fseeko that requires 64-bit mode. */
65 FILE *tmp = fopen ("/dev/null", "r");
66 if (!tmp)
67 return -1;
68 fp->_flags |= __SL64;
69 fp->_seek64 = tmp->_seek64;
70 fclose (tmp);
71 }
72 # endif
73 if (fp_->_p == fp_->_bf._base
74 && fp_->_r == 0
75 && fp_->_w == ((fp_->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully buffered and not currently reading? */
76 ? fp_->_bf._size
77 : 0)
78 && fp_ub._base == NULL)
79 #elif defined __EMX__ /* emx+gcc */
80 if (fp->_ptr == fp->_buffer
81 && fp->_rcount == 0
82 && fp->_wcount == 0
83 && fp->_ungetc_count == 0)
84 #elif defined __minix /* Minix */
85 if (fp_->_ptr == fp_->_buf
86 && (fp_->_ptr == NULL || fp_->_count == 0))
87 #elif defined _IOERR /* AIX, HP-UX, Solaris, OpenServer, UnixWare, mingw, MSVC, NonStop Kernel, OpenVMS */
88 if (fp_->_ptr == fp_->_base
89 && (fp_->_ptr == NULL || fp_->_cnt == 0))
90 #elif defined __UCLIBC__ /* uClibc */
91 if (((fp->__modeflags & __FLAG_WRITING) == 0
92 || fp->__bufpos == fp->__bufstart)
93 && ((fp->__modeflags & (__FLAG_READONLY | __FLAG_READING)) == 0
94 || fp->__bufpos == fp->__bufread))
95 #elif defined __QNX__ /* QNX */
96 if ((fp->_Mode & 0x2000 /* _MWRITE */ ? fp->_Next == fp->_Buf : fp->_Next == fp->_Rend)
97 && fp->_Rback == fp->_Back + sizeof (fp->_Back)
98 && fp->_Rsave == NULL)
99 #elif defined __MINT__ /* Atari FreeMiNT */
100 if (fp->__bufp == fp->__buffer
101 && fp->__get_limit == fp->__bufp
102 && fp->__put_limit == fp->__bufp
103 && !fp->__pushed_back)
104 #elif defined EPLAN9 /* Plan9 */
105 if (fp->rp == fp->buf
106 && fp->wp == fp->buf)
107 #elif defined __OpenBSD__ && !defined __sferror /* OpenBSD >= 7.8 */
108 /* fseeko and fflush work as advertised. */
109 if (0)
110 #elif FUNC_FFLUSH_STDIN < 0 && 200809 <= _POSIX_VERSION
111 /* Cross-compiling to some other system advertising conformance to
112 POSIX.1-2008 or later. Assume fseeko and fflush work as advertised.
113 If this assumption is incorrect, please report the bug to
114 bug-gnulib. */
115 if (0)
116 #else
117 #error "Please port gnulib fseeko.c to your platform! Look at the code in fseeko.c, then report this to bug-gnulib."
118 #endif
119 {
120 /* We get here when an fflush() call immediately preceded this one (or
121 if ftell() has created buffers but no I/O has occurred on a
122 newly-opened stream). We know there are no buffers. */
123 off_t pos = lseek (fileno (fp), offset, whence);
124 if (pos == -1)
125 {
126 #if defined __sferror || defined __DragonFly__ || defined __ANDROID__
127 /* FreeBSD, NetBSD, OpenBSD <= 7.7, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
128 fp_->_flags &= ~__SOFF;
129 #endif
130 return -1;
131 }
132
133 #if defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1
134 /* GNU libc, BeOS, Haiku, Linux libc5 */
135 fp_->_flags &= ~_IO_EOF_SEEN;
136 fp_->_offset = pos;
137 #elif defined __sferror || defined __DragonFly__ || defined __ANDROID__
138 /* FreeBSD, NetBSD, OpenBSD <= 7.7, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
139 # if defined __CYGWIN__ || (defined __NetBSD__ && __NetBSD_Version__ >= 600000000) || defined __minix
140 /* fp_->_offset is typed as an integer. */
141 fp_->_offset = pos;
142 # else
143 /* fp_->_offset is an fpos_t. */
144 {
145 /* Use a union, since on NetBSD, the compilation flags
146 determine whether fpos_t is typedef'd to off_t or a struct
147 containing a single off_t member. */
148 union
149 {
150 fpos_t f;
151 off_t o;
152 } u;
153 u.o = pos;
154 fp_->_offset = u.f;
155 }
156 # endif
157 fp_->_flags |= __SOFF;
158 fp_->_flags &= ~__SEOF;
159 #elif defined __EMX__ /* emx+gcc */
160 fp->_flags &= ~_IOEOF;
161 #elif defined _IOERR /* AIX, HP-UX, Solaris, OpenServer, UnixWare, mingw, MSVC, NonStop Kernel, OpenVMS */
162 fp_->_flag &= ~_IOEOF;
163 #elif defined __MINT__ /* Atari FreeMiNT */
164 fp->__offset = pos;
165 fp->__eof = 0;
166 #endif
167 return 0;
168 }
169 return fseeko (fp, offset, whence);
170 }