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