2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] = "@(#)fseek.c 8.3 (Berkeley) 1/2/94";
39 #endif /* LIBC_SCCS and not lint */
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include "namespace.h"
44 #include <sys/types.h>
51 #include "un-namespace.h"
53 #include "libc_private.h"
55 #define POS_ERR (-(fpos_t)1)
58 fseek(fp, offset, whence)
66 /* make sure stdio is set up */
71 ret = _fseeko(fp, (off_t)offset, whence, 1);
79 fseeko(fp, offset, whence)
87 /* make sure stdio is set up */
92 ret = _fseeko(fp, offset, whence, 0);
100 * Seek the given file to the given offset.
101 * `Whence' must be one of the three SEEK_* macros.
104 _fseeko(fp, offset, whence, ltest)
110 fpos_t (*seekfn)(void *, fpos_t, int);
111 fpos_t target, curoff, ret;
117 * Have to be able to seek.
119 if ((seekfn = fp->_seek) == NULL) {
120 errno = ESPIPE; /* historic practice */
125 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
126 * After this, whence is either SEEK_SET or SEEK_END.
132 * In order to seek relative to the current stream offset,
133 * we have to first find the current stream offset via
134 * ftell (see ftell for details).
136 if (_ftello(fp, &curoff))
139 /* Unspecified position because of ungetc() at 0 */
143 if (offset > 0 && curoff > OFF_MAX - offset) {
152 if (ltest && offset > LONG_MAX) {
166 curoff = 0; /* XXX just to keep gcc quiet */
176 * Can only optimise if:
177 * reading (and not reading-and-writing);
178 * not unbuffered; and
179 * this is a `regular' Unix file (and hence seekfn==__sseek).
180 * We must check __NBF first, because it is possible to have __NBF
181 * and __SOPT both set.
183 if (fp->_bf._base == NULL)
185 if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
187 if ((fp->_flags & __SOPT) == 0) {
188 if (seekfn != __sseek ||
189 fp->_file < 0 || _fstat(fp->_file, &st) ||
190 (st.st_mode & S_IFMT) != S_IFREG) {
191 fp->_flags |= __SNPT;
194 fp->_blksize = st.st_blksize;
195 fp->_flags |= __SOPT;
199 * We are reading; we can try to optimise.
200 * Figure out where we are going and where we are now.
202 if (whence == SEEK_SET)
205 if (_fstat(fp->_file, &st))
207 if (offset > 0 && st.st_size > OFF_MAX - offset) {
211 target = st.st_size + offset;
212 if ((off_t)target < 0) {
216 if (ltest && (off_t)target > LONG_MAX) {
222 if (!havepos && _ftello(fp, &curoff))
226 * (If the buffer was modified, we have to
227 * skip this; see fgetln.c.)
229 if (fp->_flags & __SMOD)
233 * Compute the number of bytes in the input buffer (pretending
234 * that any ungetc() input has been discarded). Adjust current
235 * offset backwards by this count so that it represents the
236 * file offset for the first byte in the current input buffer.
239 curoff += fp->_r; /* kill off ungetc */
240 n = fp->_extra->_up - fp->_bf._base;
244 n = fp->_p - fp->_bf._base;
250 * If the target offset is within the current buffer,
251 * simply adjust the pointers, clear EOF, undo ungetc(),
254 if (target >= curoff && target < curoff + n) {
255 size_t o = target - curoff;
257 fp->_p = fp->_bf._base + o;
261 fp->_flags &= ~__SEOF;
262 memset(&fp->_extra->mbstate, 0, sizeof(mbstate_t));
268 * The place we want to get to is not within the current buffer,
269 * but we can still be kind to the kernel copyout mechanism.
270 * By aligning the file offset to a block boundary, we can let
271 * the kernel use the VM hardware to map pages instead of
272 * copying bytes laboriously. Using a block boundary also
273 * ensures that we only read one block, rather than two.
275 curoff = target & ~(fp->_blksize - 1);
276 if (_sseek(fp, curoff, SEEK_SET) == POS_ERR)
279 fp->_p = fp->_bf._base;
284 if (__srefill(fp) || fp->_r < n)
289 fp->_flags &= ~__SEOF;
293 * We get here if we cannot optimise the seek ... just
294 * do it. Allow the seek function to change fp->_bf._base.
298 (ret = _sseek(fp, (fpos_t)offset, whence)) == POS_ERR)
300 /* success: clear EOF indicator and discard ungetc() data */
303 fp->_p = fp->_bf._base;
305 /* fp->_w = 0; */ /* unnecessary (I think...) */
306 fp->_flags &= ~__SEOF;
307 memset(&fp->_extra->mbstate, 0, sizeof(mbstate_t));
308 if (ltest && ret > LONG_MAX) {
309 fp->_flags |= __SERR;