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