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