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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)fseek.c 8.3 (Berkeley) 1/2/94
33 * $FreeBSD: src/lib/libc/stdio/fseek.c,v 1.44 2008/04/17 22:17:54 jhb Exp $
34 * $DragonFly: src/lib/libc/stdio/fseek.c,v 1.10 2005/11/20 11:07:30 swildner Exp $
37 #include "namespace.h"
38 #include <sys/types.h>
45 #include "un-namespace.h"
47 #include "libc_private.h"
48 #include "priv_stdio.h"
50 #define POS_ERR (-(fpos_t)1)
53 fseek(FILE *fp
, long offset
, int whence
)
58 /* make sure stdio is set up */
63 ret
= _fseeko(fp
, (off_t
)offset
, whence
, 1);
71 fseeko(FILE *fp
, off_t offset
, int whence
)
76 /* make sure stdio is set up */
81 ret
= _fseeko(fp
, offset
, whence
, 0);
89 * Seek the given file to the given offset.
90 * `Whence' must be one of the three SEEK_* macros.
93 _fseeko(FILE *fp
, off_t offset
, int whence
, int ltest
)
95 fpos_t (*seekfn
)(void *, fpos_t, int);
96 fpos_t target
, curoff
, ret
;
102 * Have to be able to seek.
104 if ((seekfn
= fp
->_seek
) == NULL
) {
105 errno
= ESPIPE
; /* historic practice */
110 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
111 * After this, whence is either SEEK_SET or SEEK_END.
117 * In order to seek relative to the current stream offset,
118 * we have to first find the current stream offset via
119 * ftell (see ftell for details).
121 if (_ftello(fp
, &curoff
))
124 /* Unspecified position because of ungetc() at 0 */
128 if (offset
> 0 && curoff
> OFF_MAX
- offset
) {
137 if (ltest
&& offset
> LONG_MAX
) {
151 curoff
= 0; /* XXX just to keep gcc quiet */
161 * Can only optimise if:
162 * reading (and not reading-and-writing);
163 * not unbuffered; and
164 * this is a `regular' Unix file (and hence seekfn==__sseek).
165 * We must check __NBF first, because it is possible to have __NBF
166 * and __SOPT both set.
168 if (fp
->_bf
._base
== NULL
)
170 if (fp
->pub
._flags
& (__SWR
| __SRW
| __SNBF
| __SNPT
))
172 if ((fp
->pub
._flags
& __SOPT
) == 0) {
173 if (seekfn
!= __sseek
||
174 fp
->pub
._fileno
< 0 || _fstat(fp
->pub
._fileno
, &st
) ||
175 (st
.st_mode
& S_IFMT
) != S_IFREG
) {
176 fp
->pub
._flags
|= __SNPT
;
179 fp
->_blksize
= st
.st_blksize
;
180 fp
->pub
._flags
|= __SOPT
;
184 * We are reading; we can try to optimise.
185 * Figure out where we are going and where we are now.
187 if (whence
== SEEK_SET
)
190 if (_fstat(fp
->pub
._fileno
, &st
))
192 if (offset
> 0 && st
.st_size
> OFF_MAX
- offset
) {
196 target
= st
.st_size
+ offset
;
197 if ((off_t
)target
< 0) {
201 if (ltest
&& (off_t
)target
> LONG_MAX
) {
207 if (!havepos
&& _ftello(fp
, &curoff
))
211 * (If the buffer was modified, we have to
212 * skip this; see fgetln.c.)
214 if (fp
->pub
._flags
& __SMOD
)
218 * Compute the number of bytes in the input buffer (pretending
219 * that any ungetc() input has been discarded). Adjust current
220 * offset backwards by this count so that it represents the
221 * file offset for the first byte in the current input buffer.
224 curoff
+= fp
->pub
._r
; /* kill off ungetc */
225 n
= fp
->_up
- fp
->_bf
._base
;
229 n
= fp
->pub
._p
- fp
->_bf
._base
;
235 * If the target offset is within the current buffer,
236 * simply adjust the pointers, clear EOF, undo ungetc(),
239 if (target
>= curoff
&& target
< curoff
+ n
) {
240 size_t o
= target
- curoff
;
242 fp
->pub
._p
= fp
->_bf
._base
+ o
;
246 fp
->pub
._flags
&= ~__SEOF
;
247 memset(WCIO_GET(fp
), 0, sizeof(struct wchar_io_data
));
253 * The place we want to get to is not within the current buffer,
254 * but we can still be kind to the kernel copyout mechanism.
255 * By aligning the file offset to a block boundary, we can let
256 * the kernel use the VM hardware to map pages instead of
257 * copying bytes laboriously. Using a block boundary also
258 * ensures that we only read one block, rather than two.
260 curoff
= target
& ~(fp
->_blksize
- 1);
261 if (_sseek(fp
, curoff
, SEEK_SET
) == POS_ERR
)
264 fp
->pub
._p
= fp
->_bf
._base
;
269 if (__srefill(fp
) || fp
->pub
._r
< n
)
274 fp
->pub
._flags
&= ~__SEOF
;
275 memset(WCIO_GET(fp
), 0, sizeof(struct wchar_io_data
));
279 * We get here if we cannot optimise the seek ... just
280 * do it. Allow the seek function to change fp->_bf._base.
284 (ret
= _sseek(fp
, (fpos_t)offset
, whence
)) == POS_ERR
)
286 if (ltest
&& ret
> LONG_MAX
) {
287 fp
->pub
._flags
|= __SERR
;
291 /* success: clear EOF indicator and discard ungetc() data */
294 fp
->pub
._p
= fp
->_bf
._base
;
296 /* fp->pub._w = 0; */ /* unnecessary (I think...) */
297 fp
->pub
._flags
&= ~__SEOF
;
298 memset(WCIO_GET(fp
), 0, sizeof(struct wchar_io_data
));