]>
git.saurik.com Git - apple/libc.git/blob - stdio/fseek.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1990, 1993
27 * The Regents of the University of California. All rights reserved.
29 * This code is derived from software contributed to Berkeley by
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 #include <sys/types.h>
63 #include <sys/types.h>
71 #define POS_ERR (-(fpos_t)1)
74 fseek(fp
, offset
, whence
)
79 return (fseeko(fp
, offset
, whence
));
84 * Seek the given file to the given offset.
85 * `Whence' must be one of the three SEEK_* macros.
88 fseeko(fp
, offset
, whence
)
93 register fpos_t (*seekfn
) __P((void *, fpos_t, int));
94 fpos_t target
, curoff
;
99 /* make sure stdio is set up */
105 * Have to be able to seek.
107 if ((seekfn
= fp
->_seek
) == NULL
) {
108 errno
= ESPIPE
; /* historic practice */
109 /* FUNLOCKFILE(fp); */
114 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
115 * After this, whence is either SEEK_SET or SEEK_END.
121 * In order to seek relative to the current stream offset,
122 * we have to first find the current stream offset a la
123 * ftell (see ftell for details).
125 if (fp
->_flags
& __SOFF
)
126 curoff
= fp
->_offset
;
128 curoff
= (*seekfn
)(fp
->_cookie
, (fpos_t)0, SEEK_CUR
);
130 /* FUNLOCKFILE(fp); */
134 if (fp
->_flags
& __SRD
) {
138 } else if (fp
->_flags
& __SWR
&& fp
->_p
!= NULL
)
139 curoff
+= fp
->_p
- fp
->_bf
._base
;
148 curoff
= 0; /* XXX just to keep gcc quiet */
154 /* FUNLOCKFILE(fp); */
159 * Can only optimise if:
160 * reading (and not reading-and-writing);
161 * not unbuffered; and
162 * this is a `regular' Unix file (and hence seekfn==__sseek).
163 * We must check __NBF first, because it is possible to have __NBF
164 * and __SOPT both set.
166 if (fp
->_bf
._base
== NULL
)
168 if (fp
->_flags
& (__SWR
| __SRW
| __SNBF
| __SNPT
))
170 if ((fp
->_flags
& __SOPT
) == 0) {
171 if (seekfn
!= __sseek
||
172 fp
->_file
< 0 || fstat(fp
->_file
, &st
) ||
173 (st
.st_mode
& S_IFMT
) != S_IFREG
) {
174 fp
->_flags
|= __SNPT
;
177 fp
->_blksize
= st
.st_blksize
;
178 fp
->_flags
|= __SOPT
;
182 * We are reading; we can try to optimise.
183 * Figure out where we are going and where we are now.
185 if (whence
== SEEK_SET
)
188 if (fstat(fp
->_file
, &st
))
190 target
= st
.st_size
+ offset
;
194 if (fp
->_flags
& __SOFF
)
195 curoff
= fp
->_offset
;
197 curoff
= (*seekfn
)(fp
->_cookie
, (fpos_t)0, SEEK_CUR
);
198 if (curoff
== POS_ERR
)
207 * Compute the number of bytes in the input buffer (pretending
208 * that any ungetc() input has been discarded). Adjust current
209 * offset backwards by this count so that it represents the
210 * file offset for the first byte in the current input buffer.
213 curoff
+= fp
->_r
; /* kill off ungetc */
214 n
= fp
->_up
- fp
->_bf
._base
;
218 n
= fp
->_p
- fp
->_bf
._base
;
224 * If the target offset is within the current buffer,
225 * simply adjust the pointers, clear EOF, undo ungetc(),
226 * and return. (If the buffer was modified, we have to
227 * skip this; see fgetln.c.)
229 if ((fp
->_flags
& __SMOD
) == 0 &&
230 target
>= curoff
&& target
< curoff
+ n
) {
231 register int o
= target
- curoff
;
233 fp
->_p
= fp
->_bf
._base
+ o
;
237 fp
->_flags
&= ~__SEOF
;
238 /* FUNLOCKFILE(fp); */
243 * The place we want to get to is not within the current buffer,
244 * but we can still be kind to the kernel copyout mechanism.
245 * By aligning the file offset to a block boundary, we can let
246 * the kernel use the VM hardware to map pages instead of
247 * copying bytes laboriously. Using a block boundary also
248 * ensures that we only read one block, rather than two.
250 curoff
= target
& ~(fp
->_blksize
- 1);
251 if ((*seekfn
)(fp
->_cookie
, curoff
, SEEK_SET
) == POS_ERR
)
254 fp
->_p
= fp
->_bf
._base
;
257 fp
->_flags
&= ~__SEOF
;
260 if (__srefill(fp
) || fp
->_r
< n
)
265 /* FUNLOCKFILE(fp); */
269 * We get here if we cannot optimise the seek ... just
270 * do it. Allow the seek function to change fp->_bf._base.
274 (*seekfn
)(fp
->_cookie
, (fpos_t)offset
, whence
) == POS_ERR
) {
275 /* FUNLOCKFILE(fp); */
278 /* success: clear EOF indicator and discard ungetc() data */
281 fp
->_p
= fp
->_bf
._base
;
283 /* fp->_w = 0; */ /* unnecessary (I think...) */
284 fp
->_flags
&= ~__SEOF
;
285 /* FUNLOCKFILE(fp); */