fseek.c revision 82709
1/*-
2 * Copyright (c) 1990, 1993
3 *	The Regents of the University of California.  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
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 82709 2001-09-01 01:56:54Z 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 via
130		 * ftell (see ftell for details).
131		 */
132		if (_ftello(fp, &curoff))
133			return (-1);
134		if ((offset > 0 && curoff > OFF_MAX - offset) ||
135		    (offset < 0 && curoff < OFF_MIN - offset)) {
136			errno = EOVERFLOW;
137			return (-1);
138		}
139		offset += curoff;
140		if (offset < 0) {
141			errno = EINVAL;
142			return (-1);
143		}
144		if (ltest && offset > LONG_MAX) {
145			errno = EOVERFLOW;
146			return (-1);
147		}
148		whence = SEEK_SET;
149		havepos = 1;
150		break;
151
152	case SEEK_SET:
153		if (offset < 0) {
154			errno = EINVAL;
155			return (-1);
156		}
157	case SEEK_END:
158		curoff = 0;		/* XXX just to keep gcc quiet */
159		havepos = 0;
160		break;
161
162	default:
163		errno = EINVAL;
164		return (-1);
165	}
166
167	/*
168	 * Can only optimise if:
169	 *	reading (and not reading-and-writing);
170	 *	not unbuffered; and
171	 *	this is a `regular' Unix file (and hence seekfn==__sseek).
172	 * We must check __NBF first, because it is possible to have __NBF
173	 * and __SOPT both set.
174	 */
175	if (fp->_bf._base == NULL)
176		__smakebuf(fp);
177	if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
178		goto dumb;
179	if ((fp->_flags & __SOPT) == 0) {
180		if (seekfn != __sseek ||
181		    fp->_file < 0 || _fstat(fp->_file, &st) ||
182		    (st.st_mode & S_IFMT) != S_IFREG) {
183			fp->_flags |= __SNPT;
184			goto dumb;
185		}
186		fp->_blksize = st.st_blksize;
187		fp->_flags |= __SOPT;
188	}
189
190	/*
191	 * We are reading; we can try to optimise.
192	 * Figure out where we are going and where we are now.
193	 */
194	if (whence == SEEK_SET)
195		target = offset;
196	else {
197		if (_fstat(fp->_file, &st))
198			goto dumb;
199		if (offset > 0 && st.st_size > OFF_MAX - offset) {
200			errno = EOVERFLOW;
201			return (-1);
202		}
203		target = st.st_size + offset;
204		if ((off_t)target < 0) {
205			errno = EINVAL;
206			return (-1);
207		}
208		if (ltest && (off_t)target > LONG_MAX) {
209			errno = EOVERFLOW;
210			return (-1);
211		}
212	}
213
214	if (!havepos && _ftello(fp, &curoff))
215		goto dumb;
216
217	/*
218	 * (If the buffer was modified, we have to
219	 * skip this; see fgetln.c.)
220	 */
221	if (fp->_flags & __SMOD)
222		goto abspos;
223
224	/*
225	 * Compute the number of bytes in the input buffer (pretending
226	 * that any ungetc() input has been discarded).  Adjust current
227	 * offset backwards by this count so that it represents the
228	 * file offset for the first byte in the current input buffer.
229	 */
230	if (HASUB(fp)) {
231		if (curoff > 0 && fp->_r > OFF_MAX - curoff)
232			goto abspos;
233		curoff += fp->_r;	/* kill off ungetc */
234		n = fp->_extra->_up - fp->_bf._base;
235		if (curoff < 0 && -((off_t)n) < OFF_MIN - curoff)
236			goto abspos;
237		curoff -= n;
238		n += fp->_ur;
239	} else {
240		n = fp->_p - fp->_bf._base;
241		if (curoff < 0 && -((off_t)n) < OFF_MIN - curoff)
242			goto abspos;
243		curoff -= n;
244		n += fp->_r;
245	}
246
247	/*
248	 * If the target offset is within the current buffer,
249	 * simply adjust the pointers, clear EOF, undo ungetc(),
250	 * and return.
251	 */
252	if (target >= curoff &&
253	    (curoff <= 0 || n <= OFF_MAX - curoff) &&
254	    target < curoff + n) {
255		size_t o = target - curoff;
256
257		fp->_p = fp->_bf._base + o;
258		fp->_r = n - o;
259		if (HASUB(fp))
260			FREEUB(fp);
261		fp->_flags &= ~__SEOF;
262		return (0);
263	}
264
265abspos:
266	/*
267	 * The place we want to get to is not within the current buffer,
268	 * but we can still be kind to the kernel copyout mechanism.
269	 * By aligning the file offset to a block boundary, we can let
270	 * the kernel use the VM hardware to map pages instead of
271	 * copying bytes laboriously.  Using a block boundary also
272	 * ensures that we only read one block, rather than two.
273	 */
274	curoff = target & ~(fp->_blksize - 1);
275	if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR)
276		goto dumb;
277	fp->_r = 0;
278	fp->_p = fp->_bf._base;
279	if (HASUB(fp))
280		FREEUB(fp);
281	n = target - curoff;
282	if (n) {
283		if (__srefill(fp) || fp->_r < n)
284			goto dumb;
285		fp->_p += n;
286		fp->_r -= n;
287	}
288	fp->_flags &= ~__SEOF;
289	return (0);
290
291	/*
292	 * We get here if we cannot optimise the seek ... just
293	 * do it.  Allow the seek function to change fp->_bf._base.
294	 */
295dumb:
296	if (__sflush(fp) ||
297	    (*seekfn)(fp->_cookie, (fpos_t)offset, whence) == POS_ERR)
298		return (-1);
299	if (ltest && fp->_offset > LONG_MAX) {
300		errno = EOVERFLOW;
301		return (-1);
302	}
303	/* success: clear EOF indicator and discard ungetc() data */
304	if (HASUB(fp))
305		FREEUB(fp);
306	fp->_p = fp->_bf._base;
307	fp->_r = 0;
308	/* fp->_w = 0; */	/* unnecessary (I think...) */
309	fp->_flags &= ~__SEOF;
310	return (0);
311}
312