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
44 #include <errno_private.h>
48 #define POS_ERR (-(fpos_t)1)
51 * Seek the given file to the given offset.
52 * `Whence' must be one of the three SEEK_* macros.
55 fseeko(fp
, offset
, whence
)
60 register fpos_t (*seekfn
) (void *, fpos_t, int);
61 fpos_t target
, curoff
;
66 /* make sure stdio is set up */
71 * Have to be able to seek.
73 if ((seekfn
= fp
->_seek
) == NULL
) {
74 __set_errno(ESPIPE
); /* historic practice */
79 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
80 * After this, whence is either SEEK_SET or SEEK_END.
86 * In order to seek relative to the current stream offset,
87 * we have to first find the current stream offset a la
88 * ftell (see ftell for details).
90 __sflush(fp
); /* may adjust seek offset on append stream */
91 if (fp
->_flags
& __SOFF
)
94 curoff
= (*seekfn
)(fp
->_cookie
, (fpos_t)0, SEEK_CUR
);
95 if (curoff
== (fpos_t)-1)
98 if (fp
->_flags
& __SRD
) {
102 } else if (fp
->_flags
& __SWR
&& fp
->_p
!= NULL
)
103 curoff
+= fp
->_p
- fp
->_bf
._base
;
112 curoff
= 0; /* XXX just to keep gcc quiet */
122 * Can only optimise if:
123 * reading (and not reading-and-writing);
124 * not unbuffered; and
125 * this is a `regular' Unix file (and hence seekfn==__sseek).
126 * We must check __NBF first, because it is possible to have __NBF
127 * and __SOPT both set.
129 if (fp
->_bf
._base
== NULL
)
131 if (fp
->_flags
& (__SWR
| __SRW
| __SNBF
| __SNPT
))
133 if ((fp
->_flags
& __SOPT
) == 0) {
134 if (seekfn
!= __sseek
||
135 fp
->_file
< 0 || fstat(fp
->_file
, &st
) ||
136 S_ISREG(st
.st_mode
)) {
137 fp
->_flags
|= __SNPT
;
140 fp
->_blksize
= st
.st_blksize
;
141 fp
->_flags
|= __SOPT
;
145 * We are reading; we can try to optimise.
146 * Figure out where we are going and where we are now.
148 if (whence
== SEEK_SET
)
151 if (fstat(fp
->_file
, &st
))
153 target
= st
.st_size
+ offset
;
157 if (fp
->_flags
& __SOFF
)
158 curoff
= fp
->_offset
;
160 curoff
= (*seekfn
)(fp
->_cookie
, (fpos_t)0, SEEK_CUR
);
161 if (curoff
== POS_ERR
)
170 * Compute the number of bytes in the input buffer (pretending
171 * that any ungetc() input has been discarded). Adjust current
172 * offset backwards by this count so that it represents the
173 * file offset for the first byte in the current input buffer.
176 curoff
+= fp
->_r
; /* kill off ungetc */
177 n
= fp
->_up
- fp
->_bf
._base
;
181 n
= fp
->_p
- fp
->_bf
._base
;
187 * If the target offset is within the current buffer,
188 * simply adjust the pointers, clear EOF, undo ungetc(),
189 * and return. (If the buffer was modified, we have to
190 * skip this; see fgetln.c.)
192 if ((fp
->_flags
& __SMOD
) == 0 &&
193 target
>= curoff
&& target
< curoff
+ n
) {
194 register int o
= target
- curoff
;
196 fp
->_p
= fp
->_bf
._base
+ o
;
200 fp
->_flags
&= ~__SEOF
;
205 * The place we want to get to is not within the current buffer,
206 * but we can still be kind to the kernel copyout mechanism.
207 * By aligning the file offset to a block boundary, we can let
208 * the kernel use the VM hardware to map pages instead of
209 * copying bytes laboriously. Using a block boundary also
210 * ensures that we only read one block, rather than two.
212 curoff
= target
& ~(fp
->_blksize
- 1);
213 if ((*seekfn
)(fp
->_cookie
, curoff
, SEEK_SET
) == POS_ERR
)
216 fp
->_p
= fp
->_bf
._base
;
219 fp
->_flags
&= ~__SEOF
;
222 if (__srefill(fp
) || fp
->_r
< n
)
230 * We get here if we cannot optimise the seek ... just
231 * do it. Allow the seek function to change fp->_bf._base.
235 (*seekfn
)(fp
->_cookie
, (fpos_t)offset
, whence
) == POS_ERR
) {
238 /* success: clear EOF indicator and discard ungetc() data */
241 fp
->_p
= fp
->_bf
._base
;
243 /* fp->_w = 0; */ /* unnecessary (I think...) */
244 fp
->_flags
&= ~__SEOF
;
249 * fseek()'s offset is a long and sizeof(off_t) != sizeof(long) on all arches
251 #if defined(__alpha__) && defined(__indr_reference)
252 __indr_reference(fseeko
, fseek
);
255 fseek(fp
, offset
, whence
)
262 return(fseeko(fp
, off
, whence
));