]> git.saurik.com Git - apple/libc.git/blob - stdio/fseek.c
Libc-262.3.2.tar.gz
[apple/libc.git] / stdio / fseek.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright (c) 1990, 1993
27 * The Regents of the University of California. All rights reserved.
28 *
29 * This code is derived from software contributed to Berkeley by
30 * Chris Torek.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
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.
47 *
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
58 * SUCH DAMAGE.
59 */
60
61
62 #include <sys/types.h>
63 #include <sys/types.h>
64 #include <sys/stat.h>
65 #include <fcntl.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <errno.h>
69 #include "local.h"
70
71 #define POS_ERR (-(fpos_t)1)
72
73 int
74 fseek(fp, offset, whence)
75 register FILE *fp;
76 long offset;
77 int whence;
78 {
79 return (fseeko(fp, offset, whence));
80 }
81
82
83 /*
84 * Seek the given file to the given offset.
85 * `Whence' must be one of the three SEEK_* macros.
86 */
87 int
88 fseeko(fp, offset, whence)
89 register FILE *fp;
90 off_t offset;
91 int whence;
92 {
93 register fpos_t (*seekfn) __P((void *, fpos_t, int));
94 fpos_t target, curoff;
95 size_t n;
96 struct stat st;
97 int havepos;
98
99 /* make sure stdio is set up */
100 if (!__sdidinit)
101 __sinit();
102
103 /* FLOCKFILE(fp); */
104 /*
105 * Have to be able to seek.
106 */
107 if ((seekfn = fp->_seek) == NULL) {
108 errno = ESPIPE; /* historic practice */
109 /* FUNLOCKFILE(fp); */
110 return (EOF);
111 }
112
113 /*
114 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
115 * After this, whence is either SEEK_SET or SEEK_END.
116 */
117 switch (whence) {
118
119 case SEEK_CUR:
120 /*
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).
124 */
125 if (fp->_flags & __SOFF)
126 curoff = fp->_offset;
127 else {
128 curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
129 if (curoff == -1) {
130 /* FUNLOCKFILE(fp); */
131 return (EOF);
132 }
133 }
134 if (fp->_flags & __SRD) {
135 curoff -= fp->_r;
136 if (HASUB(fp))
137 curoff -= fp->_ur;
138 } else if (fp->_flags & __SWR && fp->_p != NULL)
139 curoff += fp->_p - fp->_bf._base;
140
141 offset += curoff;
142 whence = SEEK_SET;
143 havepos = 1;
144 break;
145
146 case SEEK_SET:
147 case SEEK_END:
148 curoff = 0; /* XXX just to keep gcc quiet */
149 havepos = 0;
150 break;
151
152 default:
153 errno = EINVAL;
154 /* FUNLOCKFILE(fp); */
155 return (EOF);
156 }
157
158 /*
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.
165 */
166 if (fp->_bf._base == NULL)
167 __smakebuf(fp);
168 if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
169 goto dumb;
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;
175 goto dumb;
176 }
177 fp->_blksize = st.st_blksize;
178 fp->_flags |= __SOPT;
179 }
180
181 /*
182 * We are reading; we can try to optimise.
183 * Figure out where we are going and where we are now.
184 */
185 if (whence == SEEK_SET)
186 target = offset;
187 else {
188 if (fstat(fp->_file, &st))
189 goto dumb;
190 target = st.st_size + offset;
191 }
192
193 if (!havepos) {
194 if (fp->_flags & __SOFF)
195 curoff = fp->_offset;
196 else {
197 curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
198 if (curoff == POS_ERR)
199 goto dumb;
200 }
201 curoff -= fp->_r;
202 if (HASUB(fp))
203 curoff -= fp->_ur;
204 }
205
206 /*
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.
211 */
212 if (HASUB(fp)) {
213 curoff += fp->_r; /* kill off ungetc */
214 n = fp->_up - fp->_bf._base;
215 curoff -= n;
216 n += fp->_ur;
217 } else {
218 n = fp->_p - fp->_bf._base;
219 curoff -= n;
220 n += fp->_r;
221 }
222
223 /*
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.)
228 */
229 if ((fp->_flags & __SMOD) == 0 &&
230 target >= curoff && target < curoff + n) {
231 register int o = target - curoff;
232
233 fp->_p = fp->_bf._base + o;
234 fp->_r = n - o;
235 if (HASUB(fp))
236 FREEUB(fp);
237 fp->_flags &= ~__SEOF;
238 /* FUNLOCKFILE(fp); */
239 return (0);
240 }
241
242 /*
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.
249 */
250 curoff = target & ~(fp->_blksize - 1);
251 if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR)
252 goto dumb;
253 fp->_r = 0;
254 fp->_p = fp->_bf._base;
255 if (HASUB(fp))
256 FREEUB(fp);
257 fp->_flags &= ~__SEOF;
258 n = target - curoff;
259 if (n) {
260 if (__srefill(fp) || fp->_r < n)
261 goto dumb;
262 fp->_p += n;
263 fp->_r -= n;
264 }
265 /* FUNLOCKFILE(fp); */
266 return (0);
267
268 /*
269 * We get here if we cannot optimise the seek ... just
270 * do it. Allow the seek function to change fp->_bf._base.
271 */
272 dumb:
273 if (__sflush(fp) ||
274 (*seekfn)(fp->_cookie, (fpos_t)offset, whence) == POS_ERR) {
275 /* FUNLOCKFILE(fp); */
276 return (EOF);
277 }
278 /* success: clear EOF indicator and discard ungetc() data */
279 if (HASUB(fp))
280 FREEUB(fp);
281 fp->_p = fp->_bf._base;
282 fp->_r = 0;
283 /* fp->_w = 0; */ /* unnecessary (I think...) */
284 fp->_flags &= ~__SEOF;
285 /* FUNLOCKFILE(fp); */
286 return (0);
287 }