xref: /freebsd/lib/libc/stdio/fseek.c (revision ce51cf0392a0b2cc80e5ddcdb01394a303093443)
158f0484fSRodney W. Grimes /*-
258f0484fSRodney W. Grimes  * Copyright (c) 1990, 1993
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
658f0484fSRodney W. Grimes  * Chris Torek.
758f0484fSRodney W. Grimes  *
858f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
958f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1058f0484fSRodney W. Grimes  * are met:
1158f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1258f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1358f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1458f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1558f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1658f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1758f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1858f0484fSRodney W. Grimes  *	This product includes software developed by the University of
1958f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
2058f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
2158f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2258f0484fSRodney W. Grimes  *    without specific prior written permission.
2358f0484fSRodney W. Grimes  *
2458f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2558f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2658f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2758f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2858f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2958f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3058f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3158f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3258f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3358f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3458f0484fSRodney W. Grimes  * SUCH DAMAGE.
3558f0484fSRodney W. Grimes  */
3658f0484fSRodney W. Grimes 
3758f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
38ce51cf03SJames Raynard #if 0
3958f0484fSRodney W. Grimes static char sccsid[] = "@(#)fseek.c	8.3 (Berkeley) 1/2/94";
40ce51cf03SJames Raynard #endif
41ce51cf03SJames Raynard static const char rcsid[] =
42ce51cf03SJames Raynard 		"$Id$";
4358f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
4458f0484fSRodney W. Grimes 
4558f0484fSRodney W. Grimes #include <sys/types.h>
4658f0484fSRodney W. Grimes #include <sys/stat.h>
4758f0484fSRodney W. Grimes #include <fcntl.h>
4858f0484fSRodney W. Grimes #include <stdio.h>
4958f0484fSRodney W. Grimes #include <stdlib.h>
5058f0484fSRodney W. Grimes #include <errno.h>
5158f0484fSRodney W. Grimes #include "local.h"
52f70177e7SJulian Elischer #ifdef _THREAD_SAFE
53f70177e7SJulian Elischer #include <pthread.h>
54f70177e7SJulian Elischer #include "pthread_private.h"
55f70177e7SJulian Elischer #endif
5658f0484fSRodney W. Grimes 
5758f0484fSRodney W. Grimes #define	POS_ERR	(-(fpos_t)1)
5858f0484fSRodney W. Grimes 
5958f0484fSRodney W. Grimes /*
6058f0484fSRodney W. Grimes  * Seek the given file to the given offset.
6158f0484fSRodney W. Grimes  * `Whence' must be one of the three SEEK_* macros.
6258f0484fSRodney W. Grimes  */
6358f0484fSRodney W. Grimes int
6458f0484fSRodney W. Grimes fseek(fp, offset, whence)
6558f0484fSRodney W. Grimes 	register FILE *fp;
6658f0484fSRodney W. Grimes 	long offset;
6758f0484fSRodney W. Grimes 	int whence;
6858f0484fSRodney W. Grimes {
6958f0484fSRodney W. Grimes 	register fpos_t (*seekfn) __P((void *, fpos_t, int));
7058f0484fSRodney W. Grimes 	fpos_t target, curoff;
7158f0484fSRodney W. Grimes 	size_t n;
7258f0484fSRodney W. Grimes 	struct stat st;
7358f0484fSRodney W. Grimes 	int havepos;
7458f0484fSRodney W. Grimes 
7558f0484fSRodney W. Grimes 	/* make sure stdio is set up */
7658f0484fSRodney W. Grimes 	if (!__sdidinit)
7758f0484fSRodney W. Grimes 		__sinit();
7858f0484fSRodney W. Grimes 
79f70177e7SJulian Elischer #ifdef _THREAD_SAFE
80f70177e7SJulian Elischer 	_thread_flockfile(fp,__FILE__,__LINE__);
81f70177e7SJulian Elischer #endif
8258f0484fSRodney W. Grimes 	/*
8358f0484fSRodney W. Grimes 	 * Have to be able to seek.
8458f0484fSRodney W. Grimes 	 */
8558f0484fSRodney W. Grimes 	if ((seekfn = fp->_seek) == NULL) {
8658f0484fSRodney W. Grimes 		errno = ESPIPE;			/* historic practice */
8758f0484fSRodney W. Grimes 		return (EOF);
8858f0484fSRodney W. Grimes 	}
8958f0484fSRodney W. Grimes 
9058f0484fSRodney W. Grimes 	/*
9158f0484fSRodney W. Grimes 	 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
9258f0484fSRodney W. Grimes 	 * After this, whence is either SEEK_SET or SEEK_END.
9358f0484fSRodney W. Grimes 	 */
9458f0484fSRodney W. Grimes 	switch (whence) {
9558f0484fSRodney W. Grimes 
9658f0484fSRodney W. Grimes 	case SEEK_CUR:
9758f0484fSRodney W. Grimes 		/*
9858f0484fSRodney W. Grimes 		 * In order to seek relative to the current stream offset,
9958f0484fSRodney W. Grimes 		 * we have to first find the current stream offset a la
10058f0484fSRodney W. Grimes 		 * ftell (see ftell for details).
10158f0484fSRodney W. Grimes 		 */
10258f0484fSRodney W. Grimes 		if (fp->_flags & __SOFF)
10358f0484fSRodney W. Grimes 			curoff = fp->_offset;
10458f0484fSRodney W. Grimes 		else {
10558f0484fSRodney W. Grimes 			curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
106ce51cf03SJames Raynard 			if (curoff == -1) {
107f70177e7SJulian Elischer #ifdef _THREAD_SAFE
108f70177e7SJulian Elischer 				_thread_funlockfile(fp);
109f70177e7SJulian Elischer #endif
11058f0484fSRodney W. Grimes 				return (EOF);
11158f0484fSRodney W. Grimes 			}
112f70177e7SJulian Elischer 		}
11358f0484fSRodney W. Grimes 		if (fp->_flags & __SRD) {
11458f0484fSRodney W. Grimes 			curoff -= fp->_r;
11558f0484fSRodney W. Grimes 			if (HASUB(fp))
11658f0484fSRodney W. Grimes 				curoff -= fp->_ur;
11758f0484fSRodney W. Grimes 		} else if (fp->_flags & __SWR && fp->_p != NULL)
11858f0484fSRodney W. Grimes 			curoff += fp->_p - fp->_bf._base;
11958f0484fSRodney W. Grimes 
12058f0484fSRodney W. Grimes 		offset += curoff;
12158f0484fSRodney W. Grimes 		whence = SEEK_SET;
12258f0484fSRodney W. Grimes 		havepos = 1;
12358f0484fSRodney W. Grimes 		break;
12458f0484fSRodney W. Grimes 
12558f0484fSRodney W. Grimes 	case SEEK_SET:
12658f0484fSRodney W. Grimes 	case SEEK_END:
12758f0484fSRodney W. Grimes 		curoff = 0;		/* XXX just to keep gcc quiet */
12858f0484fSRodney W. Grimes 		havepos = 0;
12958f0484fSRodney W. Grimes 		break;
13058f0484fSRodney W. Grimes 
13158f0484fSRodney W. Grimes 	default:
13258f0484fSRodney W. Grimes 		errno = EINVAL;
133f70177e7SJulian Elischer #ifdef _THREAD_SAFE
134f70177e7SJulian Elischer 		_thread_funlockfile(fp);
135f70177e7SJulian Elischer #endif
13658f0484fSRodney W. Grimes 		return (EOF);
13758f0484fSRodney W. Grimes 	}
13858f0484fSRodney W. Grimes 
13958f0484fSRodney W. Grimes 	/*
14058f0484fSRodney W. Grimes 	 * Can only optimise if:
14158f0484fSRodney W. Grimes 	 *	reading (and not reading-and-writing);
14258f0484fSRodney W. Grimes 	 *	not unbuffered; and
14358f0484fSRodney W. Grimes 	 *	this is a `regular' Unix file (and hence seekfn==__sseek).
14458f0484fSRodney W. Grimes 	 * We must check __NBF first, because it is possible to have __NBF
14558f0484fSRodney W. Grimes 	 * and __SOPT both set.
14658f0484fSRodney W. Grimes 	 */
14758f0484fSRodney W. Grimes 	if (fp->_bf._base == NULL)
14858f0484fSRodney W. Grimes 		__smakebuf(fp);
14958f0484fSRodney W. Grimes 	if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
15058f0484fSRodney W. Grimes 		goto dumb;
15158f0484fSRodney W. Grimes 	if ((fp->_flags & __SOPT) == 0) {
15258f0484fSRodney W. Grimes 		if (seekfn != __sseek ||
15358f0484fSRodney W. Grimes 		    fp->_file < 0 || fstat(fp->_file, &st) ||
15458f0484fSRodney W. Grimes 		    (st.st_mode & S_IFMT) != S_IFREG) {
15558f0484fSRodney W. Grimes 			fp->_flags |= __SNPT;
15658f0484fSRodney W. Grimes 			goto dumb;
15758f0484fSRodney W. Grimes 		}
15858f0484fSRodney W. Grimes 		fp->_blksize = st.st_blksize;
15958f0484fSRodney W. Grimes 		fp->_flags |= __SOPT;
16058f0484fSRodney W. Grimes 	}
16158f0484fSRodney W. Grimes 
16258f0484fSRodney W. Grimes 	/*
16358f0484fSRodney W. Grimes 	 * We are reading; we can try to optimise.
16458f0484fSRodney W. Grimes 	 * Figure out where we are going and where we are now.
16558f0484fSRodney W. Grimes 	 */
16658f0484fSRodney W. Grimes 	if (whence == SEEK_SET)
16758f0484fSRodney W. Grimes 		target = offset;
16858f0484fSRodney W. Grimes 	else {
16958f0484fSRodney W. Grimes 		if (fstat(fp->_file, &st))
17058f0484fSRodney W. Grimes 			goto dumb;
17158f0484fSRodney W. Grimes 		target = st.st_size + offset;
17258f0484fSRodney W. Grimes 	}
17358f0484fSRodney W. Grimes 
17458f0484fSRodney W. Grimes 	if (!havepos) {
17558f0484fSRodney W. Grimes 		if (fp->_flags & __SOFF)
17658f0484fSRodney W. Grimes 			curoff = fp->_offset;
17758f0484fSRodney W. Grimes 		else {
17858f0484fSRodney W. Grimes 			curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
17958f0484fSRodney W. Grimes 			if (curoff == POS_ERR)
18058f0484fSRodney W. Grimes 				goto dumb;
18158f0484fSRodney W. Grimes 		}
18258f0484fSRodney W. Grimes 		curoff -= fp->_r;
18358f0484fSRodney W. Grimes 		if (HASUB(fp))
18458f0484fSRodney W. Grimes 			curoff -= fp->_ur;
18558f0484fSRodney W. Grimes 	}
18658f0484fSRodney W. Grimes 
18758f0484fSRodney W. Grimes 	/*
18858f0484fSRodney W. Grimes 	 * Compute the number of bytes in the input buffer (pretending
18958f0484fSRodney W. Grimes 	 * that any ungetc() input has been discarded).  Adjust current
19058f0484fSRodney W. Grimes 	 * offset backwards by this count so that it represents the
19158f0484fSRodney W. Grimes 	 * file offset for the first byte in the current input buffer.
19258f0484fSRodney W. Grimes 	 */
19358f0484fSRodney W. Grimes 	if (HASUB(fp)) {
19458f0484fSRodney W. Grimes 		curoff += fp->_r;	/* kill off ungetc */
19558f0484fSRodney W. Grimes 		n = fp->_up - fp->_bf._base;
19658f0484fSRodney W. Grimes 		curoff -= n;
19758f0484fSRodney W. Grimes 		n += fp->_ur;
19858f0484fSRodney W. Grimes 	} else {
19958f0484fSRodney W. Grimes 		n = fp->_p - fp->_bf._base;
20058f0484fSRodney W. Grimes 		curoff -= n;
20158f0484fSRodney W. Grimes 		n += fp->_r;
20258f0484fSRodney W. Grimes 	}
20358f0484fSRodney W. Grimes 
20458f0484fSRodney W. Grimes 	/*
20558f0484fSRodney W. Grimes 	 * If the target offset is within the current buffer,
20658f0484fSRodney W. Grimes 	 * simply adjust the pointers, clear EOF, undo ungetc(),
20758f0484fSRodney W. Grimes 	 * and return.  (If the buffer was modified, we have to
20858f0484fSRodney W. Grimes 	 * skip this; see fgetln.c.)
20958f0484fSRodney W. Grimes 	 */
21058f0484fSRodney W. Grimes 	if ((fp->_flags & __SMOD) == 0 &&
21158f0484fSRodney W. Grimes 	    target >= curoff && target < curoff + n) {
21258f0484fSRodney W. Grimes 		register int o = target - curoff;
21358f0484fSRodney W. Grimes 
21458f0484fSRodney W. Grimes 		fp->_p = fp->_bf._base + o;
21558f0484fSRodney W. Grimes 		fp->_r = n - o;
21658f0484fSRodney W. Grimes 		if (HASUB(fp))
21758f0484fSRodney W. Grimes 			FREEUB(fp);
21858f0484fSRodney W. Grimes 		fp->_flags &= ~__SEOF;
219f70177e7SJulian Elischer #ifdef _THREAD_SAFE
220f70177e7SJulian Elischer 		_thread_funlockfile(fp);
221f70177e7SJulian Elischer #endif
22258f0484fSRodney W. Grimes 		return (0);
22358f0484fSRodney W. Grimes 	}
22458f0484fSRodney W. Grimes 
22558f0484fSRodney W. Grimes 	/*
22658f0484fSRodney W. Grimes 	 * The place we want to get to is not within the current buffer,
22758f0484fSRodney W. Grimes 	 * but we can still be kind to the kernel copyout mechanism.
22858f0484fSRodney W. Grimes 	 * By aligning the file offset to a block boundary, we can let
22958f0484fSRodney W. Grimes 	 * the kernel use the VM hardware to map pages instead of
23058f0484fSRodney W. Grimes 	 * copying bytes laboriously.  Using a block boundary also
23158f0484fSRodney W. Grimes 	 * ensures that we only read one block, rather than two.
23258f0484fSRodney W. Grimes 	 */
23358f0484fSRodney W. Grimes 	curoff = target & ~(fp->_blksize - 1);
23458f0484fSRodney W. Grimes 	if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR)
23558f0484fSRodney W. Grimes 		goto dumb;
23658f0484fSRodney W. Grimes 	fp->_r = 0;
237692a99c0SNate Williams 	fp->_p = fp->_bf._base;
23858f0484fSRodney W. Grimes 	if (HASUB(fp))
23958f0484fSRodney W. Grimes 		FREEUB(fp);
24058f0484fSRodney W. Grimes 	fp->_flags &= ~__SEOF;
24158f0484fSRodney W. Grimes 	n = target - curoff;
24258f0484fSRodney W. Grimes 	if (n) {
24358f0484fSRodney W. Grimes 		if (__srefill(fp) || fp->_r < n)
24458f0484fSRodney W. Grimes 			goto dumb;
24558f0484fSRodney W. Grimes 		fp->_p += n;
24658f0484fSRodney W. Grimes 		fp->_r -= n;
24758f0484fSRodney W. Grimes 	}
248f70177e7SJulian Elischer #ifdef _THREAD_SAFE
249f70177e7SJulian Elischer 	_thread_funlockfile(fp);
250f70177e7SJulian Elischer #endif
25158f0484fSRodney W. Grimes 	return (0);
25258f0484fSRodney W. Grimes 
25358f0484fSRodney W. Grimes 	/*
25458f0484fSRodney W. Grimes 	 * We get here if we cannot optimise the seek ... just
25558f0484fSRodney W. Grimes 	 * do it.  Allow the seek function to change fp->_bf._base.
25658f0484fSRodney W. Grimes 	 */
25758f0484fSRodney W. Grimes dumb:
25858f0484fSRodney W. Grimes 	if (__sflush(fp) ||
25958f0484fSRodney W. Grimes 	    (*seekfn)(fp->_cookie, (fpos_t)offset, whence) == POS_ERR) {
260f70177e7SJulian Elischer #ifdef _THREAD_SAFE
261f70177e7SJulian Elischer 		_thread_funlockfile(fp);
262f70177e7SJulian Elischer #endif
26358f0484fSRodney W. Grimes 		return (EOF);
26458f0484fSRodney W. Grimes 	}
26558f0484fSRodney W. Grimes 	/* success: clear EOF indicator and discard ungetc() data */
26658f0484fSRodney W. Grimes 	if (HASUB(fp))
26758f0484fSRodney W. Grimes 		FREEUB(fp);
26858f0484fSRodney W. Grimes 	fp->_p = fp->_bf._base;
26958f0484fSRodney W. Grimes 	fp->_r = 0;
27058f0484fSRodney W. Grimes 	/* fp->_w = 0; */	/* unnecessary (I think...) */
27158f0484fSRodney W. Grimes 	fp->_flags &= ~__SEOF;
272f70177e7SJulian Elischer #ifdef _THREAD_SAFE
273f70177e7SJulian Elischer 	_thread_funlockfile(fp);
274f70177e7SJulian Elischer #endif
27558f0484fSRodney W. Grimes 	return (0);
27658f0484fSRodney W. Grimes }
277