2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #if defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid[] = "@(#)fseek.c 8.3 (Berkeley) 1/2/94";
37 #endif /* LIBC_SCCS and not lint */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include "namespace.h"
42 #include <sys/types.h>
49 #include "un-namespace.h"
51 #include "libc_private.h"
53 #define POS_ERR (-(fpos_t)1)
56 fseek(FILE *fp, long offset, int whence)
61 /* make sure stdio is set up */
65 FLOCKFILE_CANCELSAFE(fp);
66 ret = _fseeko(fp, (off_t)offset, whence, 1);
67 FUNLOCKFILE_CANCELSAFE();
74 fseeko(FILE *fp, off_t offset, int whence)
79 /* make sure stdio is set up */
83 FLOCKFILE_CANCELSAFE(fp);
84 ret = _fseeko(fp, offset, whence, 0);
85 FUNLOCKFILE_CANCELSAFE();
92 * Seek the given file to the given offset.
93 * `Whence' must be one of the three SEEK_* macros.
96 _fseeko(FILE *fp, off_t offset, int whence, int ltest)
98 fpos_t (*seekfn)(void *, fpos_t, int);
99 fpos_t target, curoff, ret;
105 * Have to be able to seek.
107 if ((seekfn = fp->_seek) == NULL) {
108 errno = ESPIPE; /* historic practice */
113 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
114 * After this, whence is either SEEK_SET or SEEK_END.
120 * In order to seek relative to the current stream offset,
121 * we have to first find the current stream offset via
122 * ftell (see ftell for details).
124 if (_ftello(fp, &curoff))
127 /* Unspecified position because of ungetc() at 0 */
131 if (offset > 0 && curoff > OFF_MAX - offset) {
140 if (ltest && offset > LONG_MAX) {
154 curoff = 0; /* XXX just to keep gcc quiet */
164 * Can only optimise if:
165 * reading (and not reading-and-writing);
166 * not unbuffered; and
167 * this is a `regular' Unix file (and hence seekfn==__sseek).
168 * We must check __NBF first, because it is possible to have __NBF
169 * and __SOPT both set.
171 if (fp->_bf._base == NULL)
173 if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
175 if ((fp->_flags & __SOPT) == 0) {
176 if (seekfn != __sseek ||
177 fp->_file < 0 || _fstat(fp->_file, &st) ||
178 (st.st_mode & S_IFMT) != S_IFREG) {
179 fp->_flags |= __SNPT;
182 fp->_blksize = st.st_blksize;
183 fp->_flags |= __SOPT;
187 * We are reading; we can try to optimise.
188 * Figure out where we are going and where we are now.
190 if (whence == SEEK_SET)
193 if (_fstat(fp->_file, &st))
195 if (offset > 0 && st.st_size > OFF_MAX - offset) {
199 target = st.st_size + offset;
200 if ((off_t)target < 0) {
204 if (ltest && (off_t)target > LONG_MAX) {
210 if (!havepos && _ftello(fp, &curoff))
214 * (If the buffer was modified, we have to
215 * skip this; see fgetln.c.)
217 if (fp->_flags & __SMOD)
221 * Compute the number of bytes in the input buffer (pretending
222 * that any ungetc() input has been discarded). Adjust current
223 * offset backwards by this count so that it represents the
224 * file offset for the first byte in the current input buffer.
227 curoff += fp->_r; /* kill off ungetc */
228 n = fp->_up - fp->_bf._base;
232 n = fp->_p - fp->_bf._base;
238 * If the target offset is within the current buffer,
239 * simply adjust the pointers, clear EOF, undo ungetc(),
242 if (target >= curoff && target < curoff + n) {
243 size_t o = target - curoff;
245 fp->_p = fp->_bf._base + o;
249 fp->_flags &= ~__SEOF;
250 memset(&fp->_mbstate, 0, sizeof(mbstate_t));
256 * The place we want to get to is not within the current buffer,
257 * but we can still be kind to the kernel copyout mechanism.
258 * By aligning the file offset to a block boundary, we can let
259 * the kernel use the VM hardware to map pages instead of
260 * copying bytes laboriously. Using a block boundary also
261 * ensures that we only read one block, rather than two.
263 curoff = target & ~(fp->_blksize - 1);
264 if (_sseek(fp, curoff, SEEK_SET) == POS_ERR)
267 fp->_p = fp->_bf._base;
272 if (__srefill(fp) || fp->_r < n)
277 fp->_flags &= ~__SEOF;
278 memset(&fp->_mbstate, 0, sizeof(mbstate_t));
282 * We get here if we cannot optimise the seek ... just
283 * do it. Allow the seek function to change fp->_bf._base.
287 (ret = _sseek(fp, (fpos_t)offset, whence)) == POS_ERR)
289 if (ltest && ret > LONG_MAX) {
290 fp->_flags |= __SERR;
294 /* success: clear EOF indicator and discard ungetc() data */
297 fp->_p = fp->_bf._base;
299 /* fp->_w = 0; */ /* unnecessary (I think...) */
300 fp->_flags &= ~__SEOF;
301 memset(&fp->_mbstate, 0, sizeof(mbstate_t));