fseek.c revision 1.2 1 /*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
23 *
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
34 * SUCH DAMAGE.
35 *
36 * PATCHES MAGIC LEVEL PATCH THAT GOT US HERE
37 * -------------------- ----- ----------------------
38 * CURRENT PATCH LEVEL: 1 00109
39 * -------------------- ----- ----------------------
40 *
41 * 02 Mar 93 Branko Lankester Update internal buffer pointer
42 *
43 */
44
45 #if defined(LIBC_SCCS) && !defined(lint)
46 static char sccsid[] = "@(#)fseek.c 5.7 (Berkeley) 2/24/91";
47 #endif /* LIBC_SCCS and not lint */
48
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <fcntl.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <errno.h>
55 #include "local.h"
56
57 #define POS_ERR (-(fpos_t)1)
58
59 /*
60 * Seek the given file to the given offset.
61 * `Whence' must be one of the three SEEK_* macros.
62 */
63 fseek(fp, offset, whence)
64 register FILE *fp;
65 long offset;
66 int whence;
67 {
68 #if __STDC__
69 register fpos_t (*seekfn)(void *, fpos_t, int);
70 #else
71 register fpos_t (*seekfn)();
72 #endif
73 fpos_t target, curoff;
74 size_t n;
75 struct stat st;
76 int havepos;
77
78 /* make sure stdio is set up */
79 if (!__sdidinit)
80 __sinit();
81
82 /*
83 * Have to be able to seek.
84 */
85 if ((seekfn = fp->_seek) == NULL) {
86 errno = ESPIPE; /* historic practice */
87 return (EOF);
88 }
89
90 /*
91 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
92 * After this, whence is either SEEK_SET or SEEK_END.
93 */
94 switch (whence) {
95
96 case SEEK_CUR:
97 /*
98 * In order to seek relative to the current stream offset,
99 * we have to first find the current stream offset a la
100 * ftell (see ftell for details).
101 */
102 if (fp->_flags & __SOFF)
103 curoff = fp->_offset;
104 else {
105 curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
106 if (curoff == -1L)
107 return (EOF);
108 }
109 if (fp->_flags & __SRD) {
110 curoff -= fp->_r;
111 if (HASUB(fp))
112 curoff -= fp->_ur;
113 } else if (fp->_flags & __SWR && fp->_p != NULL)
114 curoff += fp->_p - fp->_bf._base;
115
116 offset += curoff;
117 whence = SEEK_SET;
118 havepos = 1;
119 break;
120
121 case SEEK_SET:
122 case SEEK_END:
123 curoff = 0; /* XXX just to keep gcc quiet */
124 havepos = 0;
125 break;
126
127 default:
128 errno = EINVAL;
129 return (EOF);
130 }
131
132 /*
133 * Can only optimise if:
134 * reading (and not reading-and-writing);
135 * not unbuffered; and
136 * this is a `regular' Unix file (and hence seekfn==__sseek).
137 * We must check __NBF first, because it is possible to have __NBF
138 * and __SOPT both set.
139 */
140 if (fp->_bf._base == NULL)
141 __smakebuf(fp);
142 if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
143 goto dumb;
144 if ((fp->_flags & __SOPT) == 0) {
145 if (seekfn != __sseek ||
146 fp->_file < 0 || fstat(fp->_file, &st) ||
147 (st.st_mode & S_IFMT) != S_IFREG) {
148 fp->_flags |= __SNPT;
149 goto dumb;
150 }
151 fp->_blksize = st.st_blksize;
152 fp->_flags |= __SOPT;
153 }
154
155 /*
156 * We are reading; we can try to optimise.
157 * Figure out where we are going and where we are now.
158 */
159 if (whence == SEEK_SET)
160 target = offset;
161 else {
162 if (fstat(fp->_file, &st))
163 goto dumb;
164 target = st.st_size + offset;
165 }
166
167 if (!havepos) {
168 if (fp->_flags & __SOFF)
169 curoff = fp->_offset;
170 else {
171 curoff = (*seekfn)(fp->_cookie, 0L, SEEK_CUR);
172 if (curoff == POS_ERR)
173 goto dumb;
174 }
175 curoff -= fp->_r;
176 if (HASUB(fp))
177 curoff -= fp->_ur;
178 }
179
180 /*
181 * Compute the number of bytes in the input buffer (pretending
182 * that any ungetc() input has been discarded). Adjust current
183 * offset backwards by this count so that it represents the
184 * file offset for the first byte in the current input buffer.
185 */
186 if (HASUB(fp)) {
187 n = fp->_up - fp->_bf._base;
188 curoff -= n;
189 n += fp->_ur;
190 } else {
191 n = fp->_p - fp->_bf._base;
192 curoff -= n;
193 n += fp->_r;
194 }
195
196 /*
197 * If the target offset is within the current buffer,
198 * simply adjust the pointers, clear EOF, undo ungetc(),
199 * and return. (If the buffer was modified, we have to
200 * skip this; see fgetline.c.)
201 */
202 if ((fp->_flags & __SMOD) == 0 &&
203 target >= curoff && target < curoff + n) {
204 register int o = target - curoff;
205
206 fp->_p = fp->_bf._base + o;
207 fp->_r = n - o;
208 if (HASUB(fp))
209 FREEUB(fp);
210 fp->_flags &= ~__SEOF;
211 return (0);
212 }
213
214 /*
215 * The place we want to get to is not within the current buffer,
216 * but we can still be kind to the kernel copyout mechanism.
217 * By aligning the file offset to a block boundary, we can let
218 * the kernel use the VM hardware to map pages instead of
219 * copying bytes laboriously. Using a block boundary also
220 * ensures that we only read one block, rather than two.
221 */
222 curoff = target & ~(fp->_blksize - 1);
223 if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR)
224 goto dumb;
225 fp->_r = 0;
226 fp->_p = fp->_bf._base;
227 if (HASUB(fp))
228 FREEUB(fp);
229 fp->_flags &= ~__SEOF;
230 n = target - curoff;
231 if (n) {
232 if (__srefill(fp) || fp->_r < n)
233 goto dumb;
234 fp->_p += n;
235 fp->_r -= n;
236 }
237 return (0);
238
239 /*
240 * We get here if we cannot optimise the seek ... just
241 * do it. Allow the seek function to change fp->_bf._base.
242 */
243 dumb:
244 if (__sflush(fp) ||
245 (*seekfn)(fp->_cookie, offset, whence) == POS_ERR) {
246 return (EOF);
247 }
248 /* success: clear EOF indicator and discard ungetc() data */
249 if (HASUB(fp))
250 FREEUB(fp);
251 fp->_p = fp->_bf._base;
252 fp->_r = 0;
253 /* fp->_w = 0; */ /* unnecessary (I think...) */
254 fp->_flags &= ~__SEOF;
255 return (0);
256 }
257