]>
git.saurik.com Git - apple/libc.git/blob - stdio.subproj/fseek.c
802e3ccaad2d6d0960bedf5724fd78e06477e90b
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1990, 1993
24 * The Regents of the University of California. All rights reserved.
26 * This code is derived from software contributed to Berkeley by
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/types.h>
60 #include <sys/types.h>
68 #define POS_ERR (-(fpos_t)1)
71 fseek(fp
, offset
, whence
)
76 return (fseeko(fp
, offset
, whence
));
81 * Seek the given file to the given offset.
82 * `Whence' must be one of the three SEEK_* macros.
85 fseeko(fp
, offset
, whence
)
90 register fpos_t (*seekfn
) __P((void *, fpos_t, int));
91 fpos_t target
, curoff
;
96 /* make sure stdio is set up */
102 * Have to be able to seek.
104 if ((seekfn
= fp
->_seek
) == NULL
) {
105 errno
= ESPIPE
; /* historic practice */
106 /* FUNLOCKFILE(fp); */
111 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
112 * After this, whence is either SEEK_SET or SEEK_END.
118 * In order to seek relative to the current stream offset,
119 * we have to first find the current stream offset a la
120 * ftell (see ftell for details).
122 if (fp
->_flags
& __SOFF
)
123 curoff
= fp
->_offset
;
125 curoff
= (*seekfn
)(fp
->_cookie
, (fpos_t)0, SEEK_CUR
);
127 /* FUNLOCKFILE(fp); */
131 if (fp
->_flags
& __SRD
) {
135 } else if (fp
->_flags
& __SWR
&& fp
->_p
!= NULL
)
136 curoff
+= fp
->_p
- fp
->_bf
._base
;
145 curoff
= 0; /* XXX just to keep gcc quiet */
151 /* FUNLOCKFILE(fp); */
156 * Can only optimise if:
157 * reading (and not reading-and-writing);
158 * not unbuffered; and
159 * this is a `regular' Unix file (and hence seekfn==__sseek).
160 * We must check __NBF first, because it is possible to have __NBF
161 * and __SOPT both set.
163 if (fp
->_bf
._base
== NULL
)
165 if (fp
->_flags
& (__SWR
| __SRW
| __SNBF
| __SNPT
))
167 if ((fp
->_flags
& __SOPT
) == 0) {
168 if (seekfn
!= __sseek
||
169 fp
->_file
< 0 || fstat(fp
->_file
, &st
) ||
170 (st
.st_mode
& S_IFMT
) != S_IFREG
) {
171 fp
->_flags
|= __SNPT
;
174 fp
->_blksize
= st
.st_blksize
;
175 fp
->_flags
|= __SOPT
;
179 * We are reading; we can try to optimise.
180 * Figure out where we are going and where we are now.
182 if (whence
== SEEK_SET
)
185 if (fstat(fp
->_file
, &st
))
187 target
= st
.st_size
+ offset
;
191 if (fp
->_flags
& __SOFF
)
192 curoff
= fp
->_offset
;
194 curoff
= (*seekfn
)(fp
->_cookie
, (fpos_t)0, SEEK_CUR
);
195 if (curoff
== POS_ERR
)
204 * Compute the number of bytes in the input buffer (pretending
205 * that any ungetc() input has been discarded). Adjust current
206 * offset backwards by this count so that it represents the
207 * file offset for the first byte in the current input buffer.
210 curoff
+= fp
->_r
; /* kill off ungetc */
211 n
= fp
->_up
- fp
->_bf
._base
;
215 n
= fp
->_p
- fp
->_bf
._base
;
221 * If the target offset is within the current buffer,
222 * simply adjust the pointers, clear EOF, undo ungetc(),
223 * and return. (If the buffer was modified, we have to
224 * skip this; see fgetln.c.)
226 if ((fp
->_flags
& __SMOD
) == 0 &&
227 target
>= curoff
&& target
< curoff
+ n
) {
228 register int o
= target
- curoff
;
230 fp
->_p
= fp
->_bf
._base
+ o
;
234 fp
->_flags
&= ~__SEOF
;
235 /* FUNLOCKFILE(fp); */
240 * The place we want to get to is not within the current buffer,
241 * but we can still be kind to the kernel copyout mechanism.
242 * By aligning the file offset to a block boundary, we can let
243 * the kernel use the VM hardware to map pages instead of
244 * copying bytes laboriously. Using a block boundary also
245 * ensures that we only read one block, rather than two.
247 curoff
= target
& ~(fp
->_blksize
- 1);
248 if ((*seekfn
)(fp
->_cookie
, curoff
, SEEK_SET
) == POS_ERR
)
251 fp
->_p
= fp
->_bf
._base
;
254 fp
->_flags
&= ~__SEOF
;
257 if (__srefill(fp
) || fp
->_r
< n
)
262 /* FUNLOCKFILE(fp); */
266 * We get here if we cannot optimise the seek ... just
267 * do it. Allow the seek function to change fp->_bf._base.
271 (*seekfn
)(fp
->_cookie
, (fpos_t)offset
, whence
) == POS_ERR
) {
272 /* FUNLOCKFILE(fp); */
275 /* success: clear EOF indicator and discard ungetc() data */
278 fp
->_p
= fp
->_bf
._base
;
280 /* fp->_w = 0; */ /* unnecessary (I think...) */
281 fp
->_flags
&= ~__SEOF
;
282 /* FUNLOCKFILE(fp); */