xref: /freebsd/lib/libc/stdio/fseek.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
158f0484fSRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
458f0484fSRodney W. Grimes  * Copyright (c) 1990, 1993
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
858f0484fSRodney W. Grimes  * Chris Torek.
958f0484fSRodney W. Grimes  *
1058f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1158f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1258f0484fSRodney W. Grimes  * are met:
1358f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1458f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1558f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1658f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1758f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
181d8053c5SEd Maste  * 3. Neither the name of the University nor the names of its contributors
1958f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2058f0484fSRodney W. Grimes  *    without specific prior written permission.
2158f0484fSRodney W. Grimes  *
2258f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2358f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2458f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2558f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2658f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2758f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2858f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2958f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3058f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3158f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3258f0484fSRodney W. Grimes  * SUCH DAMAGE.
3358f0484fSRodney W. Grimes  */
3458f0484fSRodney W. Grimes 
35d201fe46SDaniel Eischen #include "namespace.h"
3658f0484fSRodney W. Grimes #include <sys/types.h>
3758f0484fSRodney W. Grimes #include <sys/stat.h>
3874b27728SAndrey A. Chernov #include <errno.h>
3958f0484fSRodney W. Grimes #include <fcntl.h>
4074b27728SAndrey A. Chernov #include <limits.h>
4158f0484fSRodney W. Grimes #include <stdio.h>
4258f0484fSRodney W. Grimes #include <stdlib.h>
43d201fe46SDaniel Eischen #include "un-namespace.h"
4458f0484fSRodney W. Grimes #include "local.h"
45ec216c26SJohn Birrell #include "libc_private.h"
4658f0484fSRodney W. Grimes 
4758f0484fSRodney W. Grimes #define	POS_ERR	(-(fpos_t)1)
4858f0484fSRodney W. Grimes 
497307d07dSDmitrij Tejblum int
fseek(FILE * fp,long offset,int whence)505a6307cfSEd Maste fseek(FILE *fp, long offset, int whence)
517307d07dSDmitrij Tejblum {
5271b5a432SAndrey A. Chernov 	int ret;
5335e1a550SAndrey A. Chernov 	int serrno = errno;
5471b5a432SAndrey A. Chernov 
5571b5a432SAndrey A. Chernov 	/* make sure stdio is set up */
5671b5a432SAndrey A. Chernov 	if (!__sdidinit)
5771b5a432SAndrey A. Chernov 		__sinit();
5871b5a432SAndrey A. Chernov 
59fda0a14fSKonstantin Belousov 	FLOCKFILE_CANCELSAFE(fp);
6071b5a432SAndrey A. Chernov 	ret = _fseeko(fp, (off_t)offset, whence, 1);
61fda0a14fSKonstantin Belousov 	FUNLOCKFILE_CANCELSAFE();
6235e1a550SAndrey A. Chernov 	if (ret == 0)
6335e1a550SAndrey A. Chernov 		errno = serrno;
6471b5a432SAndrey A. Chernov 	return (ret);
657307d07dSDmitrij Tejblum }
667307d07dSDmitrij Tejblum 
67d201fe46SDaniel Eischen int
fseeko(FILE * fp,off_t offset,int whence)685a6307cfSEd Maste fseeko(FILE *fp, off_t offset, int whence)
69d201fe46SDaniel Eischen {
70d201fe46SDaniel Eischen 	int ret;
7135e1a550SAndrey A. Chernov 	int serrno = errno;
72d201fe46SDaniel Eischen 
73d201fe46SDaniel Eischen 	/* make sure stdio is set up */
74d201fe46SDaniel Eischen 	if (!__sdidinit)
75d201fe46SDaniel Eischen 		__sinit();
76d201fe46SDaniel Eischen 
77fda0a14fSKonstantin Belousov 	FLOCKFILE_CANCELSAFE(fp);
7871b5a432SAndrey A. Chernov 	ret = _fseeko(fp, offset, whence, 0);
79fda0a14fSKonstantin Belousov 	FUNLOCKFILE_CANCELSAFE();
8035e1a550SAndrey A. Chernov 	if (ret == 0)
8135e1a550SAndrey A. Chernov 		errno = serrno;
82d201fe46SDaniel Eischen 	return (ret);
83d201fe46SDaniel Eischen }
84d201fe46SDaniel Eischen 
8558f0484fSRodney W. Grimes /*
8658f0484fSRodney W. Grimes  * Seek the given file to the given offset.
8758f0484fSRodney W. Grimes  * `Whence' must be one of the three SEEK_* macros.
8858f0484fSRodney W. Grimes  */
8958f0484fSRodney W. Grimes int
_fseeko(FILE * fp,off_t offset,int whence,int ltest)905a6307cfSEd Maste _fseeko(FILE *fp, off_t offset, int whence, int ltest)
9158f0484fSRodney W. Grimes {
92c05ac53bSDavid E. O'Brien 	fpos_t (*seekfn)(void *, fpos_t, int);
937686e760SAndrey A. Chernov 	fpos_t target, curoff, ret;
9458f0484fSRodney W. Grimes 	size_t n;
9558f0484fSRodney W. Grimes 	struct stat st;
9658f0484fSRodney W. Grimes 	int havepos;
9758f0484fSRodney W. Grimes 
9858f0484fSRodney W. Grimes 	/*
9958f0484fSRodney W. Grimes 	 * Have to be able to seek.
10058f0484fSRodney W. Grimes 	 */
10158f0484fSRodney W. Grimes 	if ((seekfn = fp->_seek) == NULL) {
10258f0484fSRodney W. Grimes 		errno = ESPIPE;		/* historic practice */
103e54bc118SAndrey A. Chernov 		return (-1);
10458f0484fSRodney W. Grimes 	}
10558f0484fSRodney W. Grimes 
10658f0484fSRodney W. Grimes 	/*
10758f0484fSRodney W. Grimes 	 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
10858f0484fSRodney W. Grimes 	 * After this, whence is either SEEK_SET or SEEK_END.
10958f0484fSRodney W. Grimes 	 */
11058f0484fSRodney W. Grimes 	switch (whence) {
11158f0484fSRodney W. Grimes 
11258f0484fSRodney W. Grimes 	case SEEK_CUR:
11358f0484fSRodney W. Grimes 		/*
11458f0484fSRodney W. Grimes 		 * In order to seek relative to the current stream offset,
1152ff678f5SAndrey A. Chernov 		 * we have to first find the current stream offset via
11658f0484fSRodney W. Grimes 		 * ftell (see ftell for details).
11758f0484fSRodney W. Grimes 		 */
11865efd812SAndrey A. Chernov 		if (_ftello(fp, &curoff))
119e54bc118SAndrey A. Chernov 			return (-1);
12045892fd8SAndrey A. Chernov 		if (curoff < 0) {
12145892fd8SAndrey A. Chernov 			/* Unspecified position because of ungetc() at 0 */
12245892fd8SAndrey A. Chernov 			errno = ESPIPE;
12345892fd8SAndrey A. Chernov 			return (-1);
12445892fd8SAndrey A. Chernov 		}
12545892fd8SAndrey A. Chernov 		if (offset > 0 && curoff > OFF_MAX - offset) {
126d9e3eff3SAndrey A. Chernov 			errno = EOVERFLOW;
127e54bc118SAndrey A. Chernov 			return (-1);
128d9e3eff3SAndrey A. Chernov 		}
12958f0484fSRodney W. Grimes 		offset += curoff;
130d9e3eff3SAndrey A. Chernov 		if (offset < 0) {
131d9e3eff3SAndrey A. Chernov 			errno = EINVAL;
132e54bc118SAndrey A. Chernov 			return (-1);
133d9e3eff3SAndrey A. Chernov 		}
134b98ba422SAndrey A. Chernov 		if (ltest && offset > LONG_MAX) {
135b98ba422SAndrey A. Chernov 			errno = EOVERFLOW;
136e54bc118SAndrey A. Chernov 			return (-1);
137b98ba422SAndrey A. Chernov 		}
13858f0484fSRodney W. Grimes 		whence = SEEK_SET;
13958f0484fSRodney W. Grimes 		havepos = 1;
14058f0484fSRodney W. Grimes 		break;
14158f0484fSRodney W. Grimes 
14258f0484fSRodney W. Grimes 	case SEEK_SET:
143d9e3eff3SAndrey A. Chernov 		if (offset < 0) {
144d9e3eff3SAndrey A. Chernov 			errno = EINVAL;
145e54bc118SAndrey A. Chernov 			return (-1);
146d9e3eff3SAndrey A. Chernov 		}
14758f0484fSRodney W. Grimes 	case SEEK_END:
14858f0484fSRodney W. Grimes 		curoff = 0;		/* XXX just to keep gcc quiet */
14958f0484fSRodney W. Grimes 		havepos = 0;
15058f0484fSRodney W. Grimes 		break;
15158f0484fSRodney W. Grimes 
15258f0484fSRodney W. Grimes 	default:
15358f0484fSRodney W. Grimes 		errno = EINVAL;
154e54bc118SAndrey A. Chernov 		return (-1);
15558f0484fSRodney W. Grimes 	}
15658f0484fSRodney W. Grimes 
15758f0484fSRodney W. Grimes 	/*
15858f0484fSRodney W. Grimes 	 * Can only optimise if:
15958f0484fSRodney W. Grimes 	 *	reading (and not reading-and-writing);
16058f0484fSRodney W. Grimes 	 *	not unbuffered; and
16158f0484fSRodney W. Grimes 	 *	this is a `regular' Unix file (and hence seekfn==__sseek).
16258f0484fSRodney W. Grimes 	 * We must check __NBF first, because it is possible to have __NBF
16358f0484fSRodney W. Grimes 	 * and __SOPT both set.
16458f0484fSRodney W. Grimes 	 */
16558f0484fSRodney W. Grimes 	if (fp->_bf._base == NULL)
16658f0484fSRodney W. Grimes 		__smakebuf(fp);
16758f0484fSRodney W. Grimes 	if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
16858f0484fSRodney W. Grimes 		goto dumb;
16958f0484fSRodney W. Grimes 	if ((fp->_flags & __SOPT) == 0) {
17058f0484fSRodney W. Grimes 		if (seekfn != __sseek ||
171d201fe46SDaniel Eischen 		    fp->_file < 0 || _fstat(fp->_file, &st) ||
17258f0484fSRodney W. Grimes 		    (st.st_mode & S_IFMT) != S_IFREG) {
17358f0484fSRodney W. Grimes 			fp->_flags |= __SNPT;
17458f0484fSRodney W. Grimes 			goto dumb;
17558f0484fSRodney W. Grimes 		}
17658f0484fSRodney W. Grimes 		fp->_blksize = st.st_blksize;
17758f0484fSRodney W. Grimes 		fp->_flags |= __SOPT;
17858f0484fSRodney W. Grimes 	}
17958f0484fSRodney W. Grimes 
18058f0484fSRodney W. Grimes 	/*
18158f0484fSRodney W. Grimes 	 * We are reading; we can try to optimise.
18258f0484fSRodney W. Grimes 	 * Figure out where we are going and where we are now.
18358f0484fSRodney W. Grimes 	 */
18458f0484fSRodney W. Grimes 	if (whence == SEEK_SET)
18558f0484fSRodney W. Grimes 		target = offset;
18658f0484fSRodney W. Grimes 	else {
187d201fe46SDaniel Eischen 		if (_fstat(fp->_file, &st))
18858f0484fSRodney W. Grimes 			goto dumb;
18998aa5183SAndrey A. Chernov 		if (offset > 0 && st.st_size > OFF_MAX - offset) {
190d9e3eff3SAndrey A. Chernov 			errno = EOVERFLOW;
191e54bc118SAndrey A. Chernov 			return (-1);
192d9e3eff3SAndrey A. Chernov 		}
19358f0484fSRodney W. Grimes 		target = st.st_size + offset;
194d9e3eff3SAndrey A. Chernov 		if ((off_t)target < 0) {
195d9e3eff3SAndrey A. Chernov 			errno = EINVAL;
196e54bc118SAndrey A. Chernov 			return (-1);
197d9e3eff3SAndrey A. Chernov 		}
198b98ba422SAndrey A. Chernov 		if (ltest && (off_t)target > LONG_MAX) {
199b98ba422SAndrey A. Chernov 			errno = EOVERFLOW;
200e54bc118SAndrey A. Chernov 			return (-1);
201b98ba422SAndrey A. Chernov 		}
20258f0484fSRodney W. Grimes 	}
20358f0484fSRodney W. Grimes 
20465efd812SAndrey A. Chernov 	if (!havepos && _ftello(fp, &curoff))
20558f0484fSRodney W. Grimes 		goto dumb;
20658f0484fSRodney W. Grimes 
20758f0484fSRodney W. Grimes 	/*
2083f498bf7SAndrey A. Chernov 	 * (If the buffer was modified, we have to
2093f498bf7SAndrey A. Chernov 	 * skip this; see fgetln.c.)
21065efd812SAndrey A. Chernov 	 */
21165efd812SAndrey A. Chernov 	if (fp->_flags & __SMOD)
21265efd812SAndrey A. Chernov 		goto abspos;
21365efd812SAndrey A. Chernov 
21465efd812SAndrey A. Chernov 	/*
21558f0484fSRodney W. Grimes 	 * Compute the number of bytes in the input buffer (pretending
21658f0484fSRodney W. Grimes 	 * that any ungetc() input has been discarded).  Adjust current
21758f0484fSRodney W. Grimes 	 * offset backwards by this count so that it represents the
21858f0484fSRodney W. Grimes 	 * file offset for the first byte in the current input buffer.
21958f0484fSRodney W. Grimes 	 */
22058f0484fSRodney W. Grimes 	if (HASUB(fp)) {
22158f0484fSRodney W. Grimes 		curoff += fp->_r;	/* kill off ungetc */
2221e98f887SJohn Baldwin 		n = fp->_up - fp->_bf._base;
22358f0484fSRodney W. Grimes 		curoff -= n;
22458f0484fSRodney W. Grimes 		n += fp->_ur;
22558f0484fSRodney W. Grimes 	} else {
22658f0484fSRodney W. Grimes 		n = fp->_p - fp->_bf._base;
22758f0484fSRodney W. Grimes 		curoff -= n;
22858f0484fSRodney W. Grimes 		n += fp->_r;
22958f0484fSRodney W. Grimes 	}
23058f0484fSRodney W. Grimes 
23158f0484fSRodney W. Grimes 	/*
23258f0484fSRodney W. Grimes 	 * If the target offset is within the current buffer,
23358f0484fSRodney W. Grimes 	 * simply adjust the pointers, clear EOF, undo ungetc(),
23465efd812SAndrey A. Chernov 	 * and return.
23558f0484fSRodney W. Grimes 	 */
236d911eb45SAndrey A. Chernov 	if (target >= curoff && target < curoff + n) {
237e54bc118SAndrey A. Chernov 		size_t o = target - curoff;
23858f0484fSRodney W. Grimes 
23958f0484fSRodney W. Grimes 		fp->_p = fp->_bf._base + o;
24058f0484fSRodney W. Grimes 		fp->_r = n - o;
24158f0484fSRodney W. Grimes 		if (HASUB(fp))
24258f0484fSRodney W. Grimes 			FREEUB(fp);
24358f0484fSRodney W. Grimes 		fp->_flags &= ~__SEOF;
2441e98f887SJohn Baldwin 		memset(&fp->_mbstate, 0, sizeof(mbstate_t));
24558f0484fSRodney W. Grimes 		return (0);
24658f0484fSRodney W. Grimes 	}
24758f0484fSRodney W. Grimes 
248e54bc118SAndrey A. Chernov abspos:
24958f0484fSRodney W. Grimes 	/*
25058f0484fSRodney W. Grimes 	 * The place we want to get to is not within the current buffer,
25158f0484fSRodney W. Grimes 	 * but we can still be kind to the kernel copyout mechanism.
25258f0484fSRodney W. Grimes 	 * By aligning the file offset to a block boundary, we can let
25358f0484fSRodney W. Grimes 	 * the kernel use the VM hardware to map pages instead of
25458f0484fSRodney W. Grimes 	 * copying bytes laboriously.  Using a block boundary also
25558f0484fSRodney W. Grimes 	 * ensures that we only read one block, rather than two.
25658f0484fSRodney W. Grimes 	 */
25758f0484fSRodney W. Grimes 	curoff = target & ~(fp->_blksize - 1);
258924888f9SAndrey A. Chernov 	if (_sseek(fp, curoff, SEEK_SET) == POS_ERR)
25958f0484fSRodney W. Grimes 		goto dumb;
26058f0484fSRodney W. Grimes 	fp->_r = 0;
261692a99c0SNate Williams 	fp->_p = fp->_bf._base;
26258f0484fSRodney W. Grimes 	if (HASUB(fp))
26358f0484fSRodney W. Grimes 		FREEUB(fp);
26458f0484fSRodney W. Grimes 	n = target - curoff;
26558f0484fSRodney W. Grimes 	if (n) {
26658f0484fSRodney W. Grimes 		if (__srefill(fp) || fp->_r < n)
26758f0484fSRodney W. Grimes 			goto dumb;
26858f0484fSRodney W. Grimes 		fp->_p += n;
26958f0484fSRodney W. Grimes 		fp->_r -= n;
27058f0484fSRodney W. Grimes 	}
2712ff678f5SAndrey A. Chernov 	fp->_flags &= ~__SEOF;
2721e98f887SJohn Baldwin 	memset(&fp->_mbstate, 0, sizeof(mbstate_t));
27358f0484fSRodney W. Grimes 	return (0);
27458f0484fSRodney W. Grimes 
27558f0484fSRodney W. Grimes 	/*
27658f0484fSRodney W. Grimes 	 * We get here if we cannot optimise the seek ... just
27758f0484fSRodney W. Grimes 	 * do it.  Allow the seek function to change fp->_bf._base.
27858f0484fSRodney W. Grimes 	 */
27958f0484fSRodney W. Grimes dumb:
2807686e760SAndrey A. Chernov 	if (__sflush(fp) ||
2817686e760SAndrey A. Chernov 	    (ret = _sseek(fp, (fpos_t)offset, whence)) == POS_ERR)
282e54bc118SAndrey A. Chernov 		return (-1);
283ef0fddb7SAndrey A. Chernov 	if (ltest && ret > LONG_MAX) {
284ef0fddb7SAndrey A. Chernov 		fp->_flags |= __SERR;
285ef0fddb7SAndrey A. Chernov 		errno = EOVERFLOW;
286ef0fddb7SAndrey A. Chernov 		return (-1);
287ef0fddb7SAndrey A. Chernov 	}
28858f0484fSRodney W. Grimes 	/* success: clear EOF indicator and discard ungetc() data */
28958f0484fSRodney W. Grimes 	if (HASUB(fp))
29058f0484fSRodney W. Grimes 		FREEUB(fp);
29158f0484fSRodney W. Grimes 	fp->_p = fp->_bf._base;
29258f0484fSRodney W. Grimes 	fp->_r = 0;
29358f0484fSRodney W. Grimes 	/* fp->_w = 0; */	/* unnecessary (I think...) */
29458f0484fSRodney W. Grimes 	fp->_flags &= ~__SEOF;
2951e98f887SJohn Baldwin 	memset(&fp->_mbstate, 0, sizeof(mbstate_t));
29658f0484fSRodney W. Grimes 	return (0);
29758f0484fSRodney W. Grimes }
298