fseek.c revision 92889
1133359Sobrien/*-
2133359Sobrien * Copyright (c) 1990, 1993
3133359Sobrien *	The Regents of the University of California.  All rights reserved.
4133359Sobrien *
5133359Sobrien * This code is derived from software contributed to Berkeley by
6133359Sobrien * Chris Torek.
7133359Sobrien *
8133359Sobrien * Redistribution and use in source and binary forms, with or without
9133359Sobrien * modification, are permitted provided that the following conditions
10133359Sobrien * are met:
11133359Sobrien * 1. Redistributions of source code must retain the above copyright
12133359Sobrien *    notice, this list of conditions and the following disclaimer.
13133359Sobrien * 2. Redistributions in binary form must reproduce the above copyright
14133359Sobrien *    notice, this list of conditions and the following disclaimer in the
15133359Sobrien *    documentation and/or other materials provided with the distribution.
16133359Sobrien * 3. All advertising materials mentioning features or use of this software
17133359Sobrien *    must display the following acknowledgement:
18133359Sobrien *	This product includes software developed by the University of
19133359Sobrien *	California, Berkeley and its contributors.
20133359Sobrien * 4. Neither the name of the University nor the names of its contributors
21133359Sobrien *    may be used to endorse or promote products derived from this software
22133359Sobrien *    without specific prior written permission.
23133359Sobrien *
24133359Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25133359Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26133359Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27133359Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28133359Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29133359Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30133359Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31133359Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32133359Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33133359Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34133359Sobrien * SUCH DAMAGE.
35133359Sobrien */
36133359Sobrien
37133359Sobrien#if defined(LIBC_SCCS) && !defined(lint)
38133359Sobrien#if 0
39133359Sobrienstatic char sccsid[] = "@(#)fseek.c	8.3 (Berkeley) 1/2/94";
40133359Sobrien#endif
41133359Sobrienstatic const char rcsid[] =
42133359Sobrien  "$FreeBSD: head/lib/libc/stdio/fseek.c 92889 2002-03-21 18:49:23Z obrien $";
43133359Sobrien#endif /* LIBC_SCCS and not lint */
44133359Sobrien
45133359Sobrien#include "namespace.h"
46133359Sobrien#include <sys/types.h>
47133359Sobrien#include <sys/stat.h>
48133359Sobrien#include <errno.h>
49133359Sobrien#include <fcntl.h>
50133359Sobrien#include <limits.h>
51133359Sobrien#include <stdio.h>
52133359Sobrien#include <stdlib.h>
53133359Sobrien#include "un-namespace.h"
54133359Sobrien#include "local.h"
55133359Sobrien#include "libc_private.h"
56133359Sobrien
57133359Sobrien#define	POS_ERR	(-(fpos_t)1)
58133359Sobrien
59133359Sobrienint
60133359Sobrienfseek(fp, offset, whence)
61133359Sobrien	FILE *fp;
62133359Sobrien	long offset;
63133359Sobrien	int whence;
64133359Sobrien{
65133359Sobrien	int ret;
66133359Sobrien	int serrno = errno;
67133359Sobrien
68133359Sobrien	/* make sure stdio is set up */
69133359Sobrien	if (!__sdidinit)
70133359Sobrien		__sinit();
71133359Sobrien
72133359Sobrien	FLOCKFILE(fp);
73133359Sobrien	ret = _fseeko(fp, (off_t)offset, whence, 1);
74133359Sobrien	FUNLOCKFILE(fp);
75133359Sobrien	if (ret == 0)
76133359Sobrien		errno = serrno;
77133359Sobrien	return (ret);
78133359Sobrien}
79133359Sobrien
80133359Sobrienint
81133359Sobrienfseeko(fp, offset, whence)
82133359Sobrien	FILE *fp;
83133359Sobrien	off_t offset;
84133359Sobrien	int whence;
85133359Sobrien{
86133359Sobrien	int ret;
87133359Sobrien	int serrno = errno;
88133359Sobrien
89133359Sobrien	/* make sure stdio is set up */
90133359Sobrien	if (!__sdidinit)
91133359Sobrien		__sinit();
92133359Sobrien
93133359Sobrien	FLOCKFILE(fp);
94133359Sobrien	ret = _fseeko(fp, offset, whence, 0);
95133359Sobrien	FUNLOCKFILE(fp);
96133359Sobrien	if (ret == 0)
97133359Sobrien		errno = serrno;
98133359Sobrien	return (ret);
99133359Sobrien}
100133359Sobrien
101133359Sobrien/*
102133359Sobrien * Seek the given file to the given offset.
103133359Sobrien * `Whence' must be one of the three SEEK_* macros.
104133359Sobrien */
105133359Sobrienint
106133359Sobrien_fseeko(fp, offset, whence, ltest)
107133359Sobrien	FILE *fp;
108133359Sobrien	off_t offset;
109133359Sobrien	int whence;
110133359Sobrien	int ltest;
111133359Sobrien{
112	fpos_t (*seekfn) __P((void *, fpos_t, int));
113	fpos_t target, curoff, ret;
114	size_t n;
115	struct stat st;
116	int havepos;
117
118	/*
119	 * Have to be able to seek.
120	 */
121	if ((seekfn = fp->_seek) == NULL) {
122		errno = ESPIPE;		/* historic practice */
123		return (-1);
124	}
125
126	/*
127	 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
128	 * After this, whence is either SEEK_SET or SEEK_END.
129	 */
130	switch (whence) {
131
132	case SEEK_CUR:
133		/*
134		 * In order to seek relative to the current stream offset,
135		 * we have to first find the current stream offset via
136		 * ftell (see ftell for details).
137		 */
138		if (_ftello(fp, &curoff))
139			return (-1);
140		if (curoff < 0) {
141			/* Unspecified position because of ungetc() at 0 */
142			errno = ESPIPE;
143			return (-1);
144		}
145		if (offset > 0 && curoff > OFF_MAX - offset) {
146			errno = EOVERFLOW;
147			return (-1);
148		}
149		offset += curoff;
150		if (offset < 0) {
151			errno = EINVAL;
152			return (-1);
153		}
154		if (ltest && offset > LONG_MAX) {
155			errno = EOVERFLOW;
156			return (-1);
157		}
158		whence = SEEK_SET;
159		havepos = 1;
160		break;
161
162	case SEEK_SET:
163		if (offset < 0) {
164			errno = EINVAL;
165			return (-1);
166		}
167	case SEEK_END:
168		curoff = 0;		/* XXX just to keep gcc quiet */
169		havepos = 0;
170		break;
171
172	default:
173		errno = EINVAL;
174		return (-1);
175	}
176
177	/*
178	 * Can only optimise if:
179	 *	reading (and not reading-and-writing);
180	 *	not unbuffered; and
181	 *	this is a `regular' Unix file (and hence seekfn==__sseek).
182	 * We must check __NBF first, because it is possible to have __NBF
183	 * and __SOPT both set.
184	 */
185	if (fp->_bf._base == NULL)
186		__smakebuf(fp);
187	if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
188		goto dumb;
189	if ((fp->_flags & __SOPT) == 0) {
190		if (seekfn != __sseek ||
191		    fp->_file < 0 || _fstat(fp->_file, &st) ||
192		    (st.st_mode & S_IFMT) != S_IFREG) {
193			fp->_flags |= __SNPT;
194			goto dumb;
195		}
196		fp->_blksize = st.st_blksize;
197		fp->_flags |= __SOPT;
198	}
199
200	/*
201	 * We are reading; we can try to optimise.
202	 * Figure out where we are going and where we are now.
203	 */
204	if (whence == SEEK_SET)
205		target = offset;
206	else {
207		if (_fstat(fp->_file, &st))
208			goto dumb;
209		if (offset > 0 && st.st_size > OFF_MAX - offset) {
210			errno = EOVERFLOW;
211			return (-1);
212		}
213		target = st.st_size + offset;
214		if ((off_t)target < 0) {
215			errno = EINVAL;
216			return (-1);
217		}
218		if (ltest && (off_t)target > LONG_MAX) {
219			errno = EOVERFLOW;
220			return (-1);
221		}
222	}
223
224	if (!havepos && _ftello(fp, &curoff))
225		goto dumb;
226
227	/*
228	 * (If the buffer was modified, we have to
229	 * skip this; see fgetln.c.)
230	 */
231	if (fp->_flags & __SMOD)
232		goto abspos;
233
234	/*
235	 * Compute the number of bytes in the input buffer (pretending
236	 * that any ungetc() input has been discarded).  Adjust current
237	 * offset backwards by this count so that it represents the
238	 * file offset for the first byte in the current input buffer.
239	 */
240	if (HASUB(fp)) {
241		curoff += fp->_r;	/* kill off ungetc */
242		n = fp->_extra->_up - fp->_bf._base;
243		curoff -= n;
244		n += fp->_ur;
245	} else {
246		n = fp->_p - fp->_bf._base;
247		curoff -= n;
248		n += fp->_r;
249	}
250
251	/*
252	 * If the target offset is within the current buffer,
253	 * simply adjust the pointers, clear EOF, undo ungetc(),
254	 * and return.
255	 */
256	if (target >= curoff && target < curoff + n) {
257		size_t o = target - curoff;
258
259		fp->_p = fp->_bf._base + o;
260		fp->_r = n - o;
261		if (HASUB(fp))
262			FREEUB(fp);
263		fp->_flags &= ~__SEOF;
264		return (0);
265	}
266
267abspos:
268	/*
269	 * The place we want to get to is not within the current buffer,
270	 * but we can still be kind to the kernel copyout mechanism.
271	 * By aligning the file offset to a block boundary, we can let
272	 * the kernel use the VM hardware to map pages instead of
273	 * copying bytes laboriously.  Using a block boundary also
274	 * ensures that we only read one block, rather than two.
275	 */
276	curoff = target & ~(fp->_blksize - 1);
277	if (_sseek(fp, curoff, SEEK_SET) == POS_ERR)
278		goto dumb;
279	fp->_r = 0;
280	fp->_p = fp->_bf._base;
281	if (HASUB(fp))
282		FREEUB(fp);
283	n = target - curoff;
284	if (n) {
285		if (__srefill(fp) || fp->_r < n)
286			goto dumb;
287		fp->_p += n;
288		fp->_r -= n;
289	}
290	fp->_flags &= ~__SEOF;
291	return (0);
292
293	/*
294	 * We get here if we cannot optimise the seek ... just
295	 * do it.  Allow the seek function to change fp->_bf._base.
296	 */
297dumb:
298	if (__sflush(fp) ||
299	    (ret = _sseek(fp, (fpos_t)offset, whence)) == POS_ERR)
300		return (-1);
301	/* success: clear EOF indicator and discard ungetc() data */
302	if (HASUB(fp))
303		FREEUB(fp);
304	fp->_p = fp->_bf._base;
305	fp->_r = 0;
306	/* fp->_w = 0; */	/* unnecessary (I think...) */
307	fp->_flags &= ~__SEOF;
308	if (ltest && ret > LONG_MAX) {
309		fp->_flags |= __SERR;
310		errno = EOVERFLOW;
311		return (-1);
312	}
313	return (0);
314}
315