xref: /csrg-svn/lib/libc/stdio/fseek.c (revision 46611)
146087Sbostic /*-
246087Sbostic  * Copyright (c) 1990 The Regents of the University of California.
346087Sbostic  * All rights reserved.
446087Sbostic  *
546087Sbostic  * This code is derived from software contributed to Berkeley by
646087Sbostic  * Chris Torek.
746087Sbostic  *
846087Sbostic  * %sccs.include.redist.c%
946087Sbostic  */
1046087Sbostic 
1126653Sdonn #if defined(LIBC_SCCS) && !defined(lint)
12*46611Sbostic static char sccsid[] = "@(#)fseek.c	5.7 (Berkeley) 02/24/91";
1346087Sbostic #endif /* LIBC_SCCS and not lint */
1422134Smckusick 
1546087Sbostic #include <sys/types.h>
1646087Sbostic #include <sys/stat.h>
17*46611Sbostic #include <fcntl.h>
1846087Sbostic #include <stdio.h>
19*46611Sbostic #include <stdlib.h>
2046087Sbostic #include <errno.h>
2146087Sbostic #include "local.h"
2246087Sbostic 
2346087Sbostic #define	POS_ERR	(-(fpos_t)1)
2446087Sbostic 
252009Swnj /*
2646087Sbostic  * Seek the given file to the given offset.
2746087Sbostic  * `Whence' must be one of the three SEEK_* macros.
282009Swnj  */
2946087Sbostic fseek(fp, offset, whence)
3046087Sbostic 	register FILE *fp;
3146087Sbostic 	long offset;
3246087Sbostic 	int whence;
3346087Sbostic {
3446087Sbostic #if __STDC__
3546271Storek 	register fpos_t (*seekfn)(void *, fpos_t, int);
3646087Sbostic #else
3746087Sbostic 	register fpos_t (*seekfn)();
3846087Sbostic #endif
3946087Sbostic 	fpos_t target, curoff;
4046087Sbostic 	size_t n;
4146087Sbostic 	struct stat st;
4246087Sbostic 	int havepos;
432009Swnj 
4446087Sbostic 	/* make sure stdio is set up */
4546087Sbostic 	if (!__sdidinit)
4646087Sbostic 		__sinit();
472009Swnj 
4846087Sbostic 	/*
4946087Sbostic 	 * Have to be able to seek.
5046087Sbostic 	 */
5146087Sbostic 	if ((seekfn = fp->_seek) == NULL) {
5246087Sbostic 		errno = ESPIPE;			/* historic practice */
5346087Sbostic 		return (EOF);
5446087Sbostic 	}
552009Swnj 
5646087Sbostic 	/*
5746087Sbostic 	 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
5846087Sbostic 	 * After this, whence is either SEEK_SET or SEEK_END.
5946087Sbostic 	 */
6046087Sbostic 	switch (whence) {
612009Swnj 
6246087Sbostic 	case SEEK_CUR:
6346087Sbostic 		/*
6446087Sbostic 		 * In order to seek relative to the current stream offset,
6546087Sbostic 		 * we have to first find the current stream offset a la
6646087Sbostic 		 * ftell (see ftell for details).
6746087Sbostic 		 */
6846087Sbostic 		if (fp->_flags & __SOFF)
6946087Sbostic 			curoff = fp->_offset;
7046087Sbostic 		else {
7146087Sbostic 			curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
7246087Sbostic 			if (curoff == -1L)
7346087Sbostic 				return (EOF);
743163Stoy 		}
7546087Sbostic 		if (fp->_flags & __SRD) {
7646087Sbostic 			curoff -= fp->_r;
7746087Sbostic 			if (HASUB(fp))
7846087Sbostic 				curoff -= fp->_ur;
7946087Sbostic 		} else if (fp->_flags & __SWR && fp->_p != NULL)
8046087Sbostic 			curoff += fp->_p - fp->_bf._base;
8146087Sbostic 
8246087Sbostic 		offset += curoff;
8346087Sbostic 		whence = SEEK_SET;
8446087Sbostic 		havepos = 1;
8546087Sbostic 		break;
8646087Sbostic 
8746087Sbostic 	case SEEK_SET:
8846087Sbostic 	case SEEK_END:
8946271Storek 		curoff = 0;		/* XXX just to keep gcc quiet */
9046087Sbostic 		havepos = 0;
9146087Sbostic 		break;
9246087Sbostic 
9346087Sbostic 	default:
9446087Sbostic 		errno = EINVAL;
9546087Sbostic 		return (EOF);
962009Swnj 	}
9746087Sbostic 
9846087Sbostic 	/*
9946087Sbostic 	 * Can only optimise if:
10046087Sbostic 	 *	reading (and not reading-and-writing);
10146087Sbostic 	 *	not unbuffered; and
10246087Sbostic 	 *	this is a `regular' Unix file (and hence seekfn==__sseek).
10346087Sbostic 	 * We must check __NBF first, because it is possible to have __NBF
10446087Sbostic 	 * and __SOPT both set.
10546087Sbostic 	 */
10646087Sbostic 	if (fp->_bf._base == NULL)
10746087Sbostic 		__smakebuf(fp);
10846087Sbostic 	if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
10946087Sbostic 		goto dumb;
11046087Sbostic 	if ((fp->_flags & __SOPT) == 0) {
11146087Sbostic 		if (seekfn != __sseek ||
11246087Sbostic 		    fp->_file < 0 || fstat(fp->_file, &st) ||
11346087Sbostic 		    (st.st_mode & S_IFMT) != S_IFREG) {
11446087Sbostic 			fp->_flags |= __SNPT;
11546087Sbostic 			goto dumb;
1163163Stoy 		}
11746087Sbostic 		fp->_blksize = st.st_blksize;
11846087Sbostic 		fp->_flags |= __SOPT;
1192009Swnj 	}
12046087Sbostic 
12146087Sbostic 	/*
12246087Sbostic 	 * We are reading; we can try to optimise.
12346087Sbostic 	 * Figure out where we are going and where we are now.
12446087Sbostic 	 */
12546087Sbostic 	if (whence == SEEK_SET)
12646087Sbostic 		target = offset;
12746087Sbostic 	else {
12846087Sbostic 		if (fstat(fp->_file, &st))
12946087Sbostic 			goto dumb;
13046087Sbostic 		target = st.st_size + offset;
13146087Sbostic 	}
13246087Sbostic 
13346087Sbostic 	if (!havepos) {
13446087Sbostic 		if (fp->_flags & __SOFF)
13546087Sbostic 			curoff = fp->_offset;
13646087Sbostic 		else {
13746087Sbostic 			curoff = (*seekfn)(fp->_cookie, 0L, SEEK_CUR);
13846087Sbostic 			if (curoff == POS_ERR)
13946087Sbostic 				goto dumb;
14046087Sbostic 		}
14146087Sbostic 		curoff -= fp->_r;
14246087Sbostic 		if (HASUB(fp))
14346087Sbostic 			curoff -= fp->_ur;
14446087Sbostic 	}
14546087Sbostic 
14646087Sbostic 	/*
14746087Sbostic 	 * Compute the number of bytes in the input buffer (pretending
14846087Sbostic 	 * that any ungetc() input has been discarded).  Adjust current
14946087Sbostic 	 * offset backwards by this count so that it represents the
15046087Sbostic 	 * file offset for the first byte in the current input buffer.
15146087Sbostic 	 */
15246087Sbostic 	if (HASUB(fp)) {
15346087Sbostic 		n = fp->_up - fp->_bf._base;
15446087Sbostic 		curoff -= n;
15546087Sbostic 		n += fp->_ur;
15646087Sbostic 	} else {
15746087Sbostic 		n = fp->_p - fp->_bf._base;
15846087Sbostic 		curoff -= n;
15946087Sbostic 		n += fp->_r;
16046087Sbostic 	}
16146087Sbostic 
16246087Sbostic 	/*
16346087Sbostic 	 * If the target offset is within the current buffer,
16446087Sbostic 	 * simply adjust the pointers, clear EOF, undo ungetc(),
16546087Sbostic 	 * and return.  (If the buffer was modified, we have to
16646087Sbostic 	 * skip this; see fgetline.c.)
16746087Sbostic 	 */
16846087Sbostic 	if ((fp->_flags & __SMOD) == 0 &&
16946087Sbostic 	    target >= curoff && target < curoff + n) {
17046087Sbostic 		register int o = target - curoff;
17146087Sbostic 
17246087Sbostic 		fp->_p = fp->_bf._base + o;
17346087Sbostic 		fp->_r = n - o;
17446087Sbostic 		if (HASUB(fp))
17546087Sbostic 			FREEUB(fp);
17646087Sbostic 		fp->_flags &= ~__SEOF;
17746087Sbostic 		return (0);
17846087Sbostic 	}
17946087Sbostic 
18046087Sbostic 	/*
18146087Sbostic 	 * The place we want to get to is not within the current buffer,
18246087Sbostic 	 * but we can still be kind to the kernel copyout mechanism.
18346087Sbostic 	 * By aligning the file offset to a block boundary, we can let
18446087Sbostic 	 * the kernel use the VM hardware to map pages instead of
18546087Sbostic 	 * copying bytes laboriously.  Using a block boundary also
18646087Sbostic 	 * ensures that we only read one block, rather than two.
18746087Sbostic 	 */
18846087Sbostic 	curoff = target & ~(fp->_blksize - 1);
18946087Sbostic 	if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR)
19046087Sbostic 		goto dumb;
19146087Sbostic 	fp->_r = 0;
19246087Sbostic 	if (HASUB(fp))
19346087Sbostic 		FREEUB(fp);
19446087Sbostic 	fp->_flags &= ~__SEOF;
19546087Sbostic 	n = target - curoff;
19646087Sbostic 	if (n) {
19746087Sbostic 		if (__srefill(fp) || fp->_r < n)
19846087Sbostic 			goto dumb;
19946087Sbostic 		fp->_p += n;
20046087Sbostic 		fp->_r -= n;
20146087Sbostic 	}
20246087Sbostic 	return (0);
20346087Sbostic 
20446087Sbostic 	/*
20546087Sbostic 	 * We get here if we cannot optimise the seek ... just
20646087Sbostic 	 * do it.  Allow the seek function to change fp->_bf._base.
20746087Sbostic 	 */
20846087Sbostic dumb:
20946215Sbostic 	if (__sflush(fp) ||
21046087Sbostic 	    (*seekfn)(fp->_cookie, offset, whence) == POS_ERR) {
21146087Sbostic 		return (EOF);
21246087Sbostic 	}
21346087Sbostic 	/* success: clear EOF indicator and discard ungetc() data */
21446087Sbostic 	if (HASUB(fp))
21546087Sbostic 		FREEUB(fp);
21646087Sbostic 	fp->_p = fp->_bf._base;
21746087Sbostic 	fp->_r = 0;
21846087Sbostic 	/* fp->_w = 0; */	/* unnecessary (I think...) */
21946087Sbostic 	fp->_flags &= ~__SEOF;
22046087Sbostic 	return (0);
2212009Swnj }
222