fseek.c revision 82591
1208926Smjacob/*-
2208926Smjacob * Copyright (c) 1990, 1993
3208926Smjacob *	The Regents of the University of California.  All rights reserved.
4208926Smjacob *
5208926Smjacob * This code is derived from software contributed to Berkeley by
6208926Smjacob * Chris Torek.
7208926Smjacob *
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
37#if defined(LIBC_SCCS) && !defined(lint)
38#if 0
39static char sccsid[] = "@(#)fseek.c	8.3 (Berkeley) 1/2/94";
40#endif
41static const char rcsid[] =
42  "$FreeBSD: head/lib/libc/stdio/fseek.c 82591 2001-08-30 20:49:47Z ache $";
43#endif /* LIBC_SCCS and not lint */
44
45#include "namespace.h"
46#include <sys/types.h>
47#include <sys/stat.h>
48#include <errno.h>
49#include <fcntl.h>
50#include <limits.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include "un-namespace.h"
54#include "local.h"
55#include "libc_private.h"
56
57#define	POS_ERR	(-(fpos_t)1)
58
59int
60fseek(fp, offset, whence)
61	register FILE *fp;
62	long offset;
63	int whence;
64{
65	int ret;
66
67	/* make sure stdio is set up */
68	if (!__sdidinit)
69		__sinit();
70
71	FLOCKFILE(fp);
72	ret = _fseeko(fp, (off_t)offset, whence, 1);
73	FUNLOCKFILE(fp);
74	return (ret);
75}
76
77int
78fseeko(fp, offset, whence)
79	FILE *fp;
80	off_t offset;
81	int whence;
82{
83	int ret;
84
85	/* make sure stdio is set up */
86	if (!__sdidinit)
87		__sinit();
88
89	FLOCKFILE(fp);
90	ret = _fseeko(fp, offset, whence, 0);
91	FUNLOCKFILE(fp);
92	return (ret);
93}
94
95/*
96 * Seek the given file to the given offset.
97 * `Whence' must be one of the three SEEK_* macros.
98 */
99int
100_fseeko(fp, offset, whence, ltest)
101	FILE *fp;
102	off_t offset;
103	int whence;
104	int ltest;
105{
106	register fpos_t (*seekfn) __P((void *, fpos_t, int));
107	fpos_t target, curoff;
108	size_t n;
109	struct stat st;
110	int havepos;
111
112	/*
113	 * Have to be able to seek.
114	 */
115	if ((seekfn = fp->_seek) == NULL) {
116		errno = ESPIPE;		/* historic practice */
117		return (-1);
118	}
119
120	/*
121	 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
122	 * After this, whence is either SEEK_SET or SEEK_END.
123	 */
124	switch (whence) {
125
126	case SEEK_CUR:
127		/*
128		 * In order to seek relative to the current stream offset,
129		 * we have to first find the current stream offset a la
130		 * ftell (see ftell for details).
131		 */
132		if (fp->_flags & __SOFF)
133			curoff = fp->_offset;
134		else {
135			curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
136			if (curoff == -1)
137				return (-1);
138		}
139		if (fp->_flags & __SRD) {
140			curoff -= fp->_r;
141			if (curoff < 0) {
142				if (HASUB(fp)) {
143					fp->_p -= curoff;
144					fp->_r += curoff;
145					curoff = 0;
146				} else {
147					errno = EBADF;
148					return (-1);
149				}
150			}
151			if (HASUB(fp)) {
152				curoff -= fp->_ur;
153				if (curoff < 0) {
154					if (-curoff <= fp->_r) {
155						fp->_p -= curoff;
156						fp->_r += curoff;
157						curoff = 0;
158					} else {
159						errno = EBADF;
160						return (-1);
161					}
162				}
163			}
164		} else if ((fp->_flags & __SWR) && fp->_p != NULL) {
165			n = fp->_p - fp->_bf._base;
166			if (curoff > OFF_MAX - n) {
167				errno = EOVERFLOW;
168				return (-1);
169			}
170			curoff += n;
171		}
172		if (offset > 0 && curoff > OFF_MAX - offset) {
173			errno = EOVERFLOW;
174			return (-1);
175		}
176		offset += curoff;
177		if (offset < 0) {
178			errno = EINVAL;
179			return (-1);
180		}
181		if (ltest && offset > LONG_MAX) {
182			errno = EOVERFLOW;
183			return (-1);
184		}
185		whence = SEEK_SET;
186		havepos = 1;
187		break;
188
189	case SEEK_SET:
190		if (offset < 0) {
191			errno = EINVAL;
192			return (-1);
193		}
194	case SEEK_END:
195		curoff = 0;		/* XXX just to keep gcc quiet */
196		havepos = 0;
197		break;
198
199	default:
200		errno = EINVAL;
201		return (-1);
202	}
203
204	/*
205	 * Can only optimise if:
206	 *	reading (and not reading-and-writing);
207	 *	not unbuffered; and
208	 *	this is a `regular' Unix file (and hence seekfn==__sseek).
209	 * We must check __NBF first, because it is possible to have __NBF
210	 * and __SOPT both set.
211	 */
212	if (fp->_bf._base == NULL)
213		__smakebuf(fp);
214	if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
215		goto dumb;
216	if ((fp->_flags & __SOPT) == 0) {
217		if (seekfn != __sseek ||
218		    fp->_file < 0 || _fstat(fp->_file, &st) ||
219		    (st.st_mode & S_IFMT) != S_IFREG) {
220			fp->_flags |= __SNPT;
221			goto dumb;
222		}
223		fp->_blksize = st.st_blksize;
224		fp->_flags |= __SOPT;
225	}
226
227	/*
228	 * We are reading; we can try to optimise.
229	 * Figure out where we are going and where we are now.
230	 */
231	if (whence == SEEK_SET)
232		target = offset;
233	else {
234		if (_fstat(fp->_file, &st))
235			goto dumb;
236		if (offset > 0 && st.st_size > OFF_MAX - offset) {
237			errno = EOVERFLOW;
238			return (-1);
239		}
240		target = st.st_size + offset;
241		if ((off_t)target < 0) {
242			errno = EINVAL;
243			return (-1);
244		}
245		if (ltest && (off_t)target > LONG_MAX) {
246			errno = EOVERFLOW;
247			return (-1);
248		}
249	}
250
251	if (!havepos) {
252		if (fp->_flags & __SOFF)
253			curoff = fp->_offset;
254		else {
255			curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
256			if (curoff == POS_ERR)
257				goto dumb;
258		}
259		curoff -= fp->_r;
260		if (curoff < 0) {
261			if (HASUB(fp)) {
262				fp->_p -= curoff;
263				fp->_r += curoff;
264				curoff = 0;
265			} else
266				goto dumb;
267		}
268		if (HASUB(fp)) {
269			curoff -= fp->_ur;
270			if (curoff < 0) {
271				if (-curoff <= fp->_r) {
272					fp->_p -= curoff;
273					fp->_r += curoff;
274					curoff = 0;
275				} else
276					goto dumb;
277			}
278		}
279	}
280
281	/*
282	 * Compute the number of bytes in the input buffer (pretending
283	 * that any ungetc() input has been discarded).  Adjust current
284	 * offset backwards by this count so that it represents the
285	 * file offset for the first byte in the current input buffer.
286	 */
287	if (HASUB(fp)) {
288		if (curoff > OFF_MAX - fp->_r)
289			goto abspos;
290		curoff += fp->_r;	/* kill off ungetc */
291		n = fp->_extra->_up - fp->_bf._base;
292		curoff -= n;
293		n += fp->_ur;
294	} else {
295		n = fp->_p - fp->_bf._base;
296		curoff -= n;
297		n += fp->_r;
298	}
299	/* curoff can be negative at this point. */
300
301	/*
302	 * If the target offset is within the current buffer,
303	 * simply adjust the pointers, clear EOF, undo ungetc(),
304	 * and return.  (If the buffer was modified, we have to
305	 * skip this; see fgetln.c.)
306	 */
307	if ((fp->_flags & __SMOD) == 0 &&
308	    target >= curoff &&
309	    (curoff <= 0 || curoff <= OFF_MAX - n) &&
310	    target < curoff + n) {
311		size_t o = target - curoff;
312
313		fp->_p = fp->_bf._base + o;
314		fp->_r = n - o;
315		if (HASUB(fp))
316			FREEUB(fp);
317		fp->_flags &= ~__SEOF;
318		return (0);
319	}
320
321abspos:
322	/*
323	 * The place we want to get to is not within the current buffer,
324	 * but we can still be kind to the kernel copyout mechanism.
325	 * By aligning the file offset to a block boundary, we can let
326	 * the kernel use the VM hardware to map pages instead of
327	 * copying bytes laboriously.  Using a block boundary also
328	 * ensures that we only read one block, rather than two.
329	 */
330	curoff = target & ~(fp->_blksize - 1);
331	if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR)
332		goto dumb;
333	fp->_r = 0;
334	fp->_p = fp->_bf._base;
335	if (HASUB(fp))
336		FREEUB(fp);
337	fp->_flags &= ~__SEOF;
338	n = target - curoff;
339	if (n) {
340		if (__srefill(fp) || fp->_r < n)
341			goto dumb;
342		fp->_p += n;
343		fp->_r -= n;
344	}
345	return (0);
346
347	/*
348	 * We get here if we cannot optimise the seek ... just
349	 * do it.  Allow the seek function to change fp->_bf._base.
350	 */
351dumb:
352	if (__sflush(fp) ||
353	    (*seekfn)(fp->_cookie, (fpos_t)offset, whence) == POS_ERR)
354		return (-1);
355	if (ltest && fp->_offset > LONG_MAX) {
356		errno = EOVERFLOW;
357		return (-1);
358	}
359	/* success: clear EOF indicator and discard ungetc() data */
360	if (HASUB(fp))
361		FREEUB(fp);
362	fp->_p = fp->_bf._base;
363	fp->_r = 0;
364	/* fp->_w = 0; */	/* unnecessary (I think...) */
365	fp->_flags &= ~__SEOF;
366	return (0);
367}
368