xref: /minix3/lib/libc/stdio/fseeko.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: fseeko.c,v 1.13 2014/10/19 11:17:43 justin Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras  * Copyright (c) 1990, 1993
52fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * This code is derived from software contributed to Berkeley by
82fe8fb19SBen Gras  * Chris Torek.
92fe8fb19SBen Gras  *
102fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras  * are met:
132fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
192fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
202fe8fb19SBen Gras  *    without specific prior written permission.
212fe8fb19SBen Gras  *
222fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322fe8fb19SBen Gras  * SUCH DAMAGE.
332fe8fb19SBen Gras  */
342fe8fb19SBen Gras 
352fe8fb19SBen Gras #include <sys/cdefs.h>
362fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
37*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: fseeko.c,v 1.13 2014/10/19 11:17:43 justin Exp $");
382fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
392fe8fb19SBen Gras 
402fe8fb19SBen Gras #include "namespace.h"
412fe8fb19SBen Gras #include <sys/types.h>
422fe8fb19SBen Gras #include <sys/stat.h>
432fe8fb19SBen Gras 
442fe8fb19SBen Gras #include <assert.h>
452fe8fb19SBen Gras #include <errno.h>
462fe8fb19SBen Gras #include <fcntl.h>
472fe8fb19SBen Gras #include <stdio.h>
482fe8fb19SBen Gras #include <stdlib.h>
492fe8fb19SBen Gras #include "reentrant.h"
502fe8fb19SBen Gras #include "local.h"
512fe8fb19SBen Gras 
522fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(fseeko,_fseeko)532fe8fb19SBen Gras __weak_alias(fseeko, _fseeko)
542fe8fb19SBen Gras #endif
552fe8fb19SBen Gras 
56f14fb602SLionel Sambuc #define	POS_ERR	((off_t)-1)
572fe8fb19SBen Gras 
582fe8fb19SBen Gras /*
592fe8fb19SBen Gras  * Seek the given file to the given offset.
602fe8fb19SBen Gras  * `Whence' must be one of the three SEEK_* macros.
612fe8fb19SBen Gras  */
622fe8fb19SBen Gras int
632fe8fb19SBen Gras fseeko(FILE *fp, off_t offset, int whence)
642fe8fb19SBen Gras {
65f14fb602SLionel Sambuc 	off_t (*seekfn)(void *, off_t, int);
66f14fb602SLionel Sambuc 	off_t target, curoff;
672fe8fb19SBen Gras 	size_t n;
682fe8fb19SBen Gras 	struct stat st;
692fe8fb19SBen Gras 	int havepos;
702fe8fb19SBen Gras 
712fe8fb19SBen Gras 	_DIAGASSERT(fp != NULL);
722fe8fb19SBen Gras 
732fe8fb19SBen Gras 	/* make sure stdio is set up */
742fe8fb19SBen Gras 	if (!__sdidinit)
752fe8fb19SBen Gras 		__sinit();
762fe8fb19SBen Gras 
772fe8fb19SBen Gras 	FLOCKFILE(fp);
782fe8fb19SBen Gras 
792fe8fb19SBen Gras 	/*
802fe8fb19SBen Gras 	 * Have to be able to seek.
812fe8fb19SBen Gras 	 */
822fe8fb19SBen Gras 	if ((seekfn = fp->_seek) == NULL) {
832fe8fb19SBen Gras 		errno = ESPIPE;			/* historic practice */
842fe8fb19SBen Gras 		FUNLOCKFILE(fp);
85f14fb602SLionel Sambuc 		return -1;
862fe8fb19SBen Gras 	}
872fe8fb19SBen Gras 
882fe8fb19SBen Gras 	/*
892fe8fb19SBen Gras 	 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
902fe8fb19SBen Gras 	 * After this, whence is either SEEK_SET or SEEK_END.
912fe8fb19SBen Gras 	 */
922fe8fb19SBen Gras 	switch (whence) {
932fe8fb19SBen Gras 
942fe8fb19SBen Gras 	case SEEK_CUR:
952fe8fb19SBen Gras 		/*
962fe8fb19SBen Gras 		 * In order to seek relative to the current stream offset,
972fe8fb19SBen Gras 		 * we have to first find the current stream offset a la
982fe8fb19SBen Gras 		 * ftell (see ftell for details).
992fe8fb19SBen Gras 		 */
100f14fb602SLionel Sambuc 		(void)__sflush(fp); /* may adjust seek offset on append stream */
1012fe8fb19SBen Gras 		if (fp->_flags & __SOFF)
1022fe8fb19SBen Gras 			curoff = fp->_offset;
1032fe8fb19SBen Gras 		else {
104f14fb602SLionel Sambuc 			curoff = (*seekfn)(fp->_cookie, (off_t)0, SEEK_CUR);
1052fe8fb19SBen Gras 			if (curoff == POS_ERR) {
1062fe8fb19SBen Gras 				FUNLOCKFILE(fp);
107f14fb602SLionel Sambuc 				return -1;
1082fe8fb19SBen Gras 			}
1092fe8fb19SBen Gras 		}
1102fe8fb19SBen Gras 		if (fp->_flags & __SRD) {
1112fe8fb19SBen Gras 			curoff -= fp->_r;
1122fe8fb19SBen Gras 			if (HASUB(fp))
1132fe8fb19SBen Gras 				curoff -= fp->_ur;
1142fe8fb19SBen Gras 		} else if (fp->_flags & __SWR && fp->_p != NULL)
1152fe8fb19SBen Gras 			curoff += fp->_p - fp->_bf._base;
1162fe8fb19SBen Gras 
1172fe8fb19SBen Gras 		offset += curoff;
118*0a6a1f1dSLionel Sambuc 		if (offset < 0) {
119*0a6a1f1dSLionel Sambuc 			errno = EINVAL;
120*0a6a1f1dSLionel Sambuc 			FUNLOCKFILE(fp);
121*0a6a1f1dSLionel Sambuc 			return -1;
122*0a6a1f1dSLionel Sambuc 		}
1232fe8fb19SBen Gras 		whence = SEEK_SET;
1242fe8fb19SBen Gras 		havepos = 1;
1252fe8fb19SBen Gras 		break;
1262fe8fb19SBen Gras 
1272fe8fb19SBen Gras 	case SEEK_SET:
128*0a6a1f1dSLionel Sambuc 		if (offset < 0) {
129*0a6a1f1dSLionel Sambuc 			errno = EINVAL;
130*0a6a1f1dSLionel Sambuc 			FUNLOCKFILE(fp);
131*0a6a1f1dSLionel Sambuc 			return -1;
132*0a6a1f1dSLionel Sambuc 		}
1332fe8fb19SBen Gras 	case SEEK_END:
1342fe8fb19SBen Gras 		curoff = 0;		/* XXX just to keep gcc quiet */
1352fe8fb19SBen Gras 		havepos = 0;
1362fe8fb19SBen Gras 		break;
1372fe8fb19SBen Gras 
1382fe8fb19SBen Gras 	default:
1392fe8fb19SBen Gras 		errno = EINVAL;
1402fe8fb19SBen Gras 		FUNLOCKFILE(fp);
141f14fb602SLionel Sambuc 		return -1;
1422fe8fb19SBen Gras 	}
1432fe8fb19SBen Gras 
1442fe8fb19SBen Gras 	/*
1452fe8fb19SBen Gras 	 * Can only optimise if:
1462fe8fb19SBen Gras 	 *	reading (and not reading-and-writing);
1472fe8fb19SBen Gras 	 *	not unbuffered; and
1482fe8fb19SBen Gras 	 *	this is a `regular' Unix file (and hence seekfn==__sseek).
1492fe8fb19SBen Gras 	 * We must check __NBF first, because it is possible to have __NBF
1502fe8fb19SBen Gras 	 * and __SOPT both set.
1512fe8fb19SBen Gras 	 */
1522fe8fb19SBen Gras 	if (fp->_bf._base == NULL)
1532fe8fb19SBen Gras 		__smakebuf(fp);
1542fe8fb19SBen Gras 	if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
1552fe8fb19SBen Gras 		goto dumb;
1562fe8fb19SBen Gras 	if ((fp->_flags & __SOPT) == 0) {
1572fe8fb19SBen Gras 		if (seekfn != __sseek ||
1582fe8fb19SBen Gras 		    __sfileno(fp) == -1 || fstat(__sfileno(fp), &st) ||
1592fe8fb19SBen Gras 		    !S_ISREG(st.st_mode)) {
1602fe8fb19SBen Gras 			fp->_flags |= __SNPT;
1612fe8fb19SBen Gras 			goto dumb;
1622fe8fb19SBen Gras 		}
1632fe8fb19SBen Gras 		fp->_blksize = st.st_blksize;
1642fe8fb19SBen Gras 		fp->_flags |= __SOPT;
1652fe8fb19SBen Gras 	}
1662fe8fb19SBen Gras 
1672fe8fb19SBen Gras 	/*
1682fe8fb19SBen Gras 	 * We are reading; we can try to optimise.
1692fe8fb19SBen Gras 	 * Figure out where we are going and where we are now.
1702fe8fb19SBen Gras 	 */
1712fe8fb19SBen Gras 	if (whence == SEEK_SET)
1722fe8fb19SBen Gras 		target = offset;
1732fe8fb19SBen Gras 	else {
1742fe8fb19SBen Gras 		if (fstat(__sfileno(fp), &st))
1752fe8fb19SBen Gras 			goto dumb;
1762fe8fb19SBen Gras 		target = st.st_size + offset;
1772fe8fb19SBen Gras 	}
1782fe8fb19SBen Gras 
1792fe8fb19SBen Gras 	if (!havepos) {
1802fe8fb19SBen Gras 		if (fp->_flags & __SOFF)
1812fe8fb19SBen Gras 			curoff = fp->_offset;
1822fe8fb19SBen Gras 		else {
183f14fb602SLionel Sambuc 			curoff = (*seekfn)(fp->_cookie, (off_t)0, SEEK_CUR);
1842fe8fb19SBen Gras 			if (curoff == POS_ERR)
1852fe8fb19SBen Gras 				goto dumb;
1862fe8fb19SBen Gras 		}
1872fe8fb19SBen Gras 		curoff -= fp->_r;
1882fe8fb19SBen Gras 		if (HASUB(fp))
1892fe8fb19SBen Gras 			curoff -= fp->_ur;
1902fe8fb19SBen Gras 	}
1912fe8fb19SBen Gras 
1922fe8fb19SBen Gras 	/*
1932fe8fb19SBen Gras 	 * Compute the number of bytes in the input buffer (pretending
1942fe8fb19SBen Gras 	 * that any ungetc() input has been discarded).  Adjust current
1952fe8fb19SBen Gras 	 * offset backwards by this count so that it represents the
1962fe8fb19SBen Gras 	 * file offset for the first byte in the current input buffer.
1972fe8fb19SBen Gras 	 */
1982fe8fb19SBen Gras 	if (HASUB(fp)) {
1992fe8fb19SBen Gras 		curoff += fp->_r;	/* kill off ungetc */
2002fe8fb19SBen Gras 		n = fp->_up - fp->_bf._base;
2012fe8fb19SBen Gras 		curoff -= n;
2022fe8fb19SBen Gras 		n += fp->_ur;
2032fe8fb19SBen Gras 	} else {
2042fe8fb19SBen Gras 		n = fp->_p - fp->_bf._base;
2052fe8fb19SBen Gras 		curoff -= n;
2062fe8fb19SBen Gras 		n += fp->_r;
2072fe8fb19SBen Gras 	}
2082fe8fb19SBen Gras 
2092fe8fb19SBen Gras 	/*
2102fe8fb19SBen Gras 	 * If the target offset is within the current buffer,
2112fe8fb19SBen Gras 	 * simply adjust the pointers, clear EOF, undo ungetc(),
2122fe8fb19SBen Gras 	 * and return.  (If the buffer was modified, we have to
2132fe8fb19SBen Gras 	 * skip this; see fgetln.c.)
2142fe8fb19SBen Gras 	 */
2152fe8fb19SBen Gras 	if ((fp->_flags & __SMOD) == 0 &&
216f14fb602SLionel Sambuc 	    target >= curoff && target < curoff + (off_t)n) {
2172fe8fb19SBen Gras 		int o = (int)(target - curoff);
2182fe8fb19SBen Gras 
2192fe8fb19SBen Gras 		fp->_p = fp->_bf._base + o;
220f14fb602SLionel Sambuc 		_DIAGASSERT(__type_fit(int, n - o));
221f14fb602SLionel Sambuc 		fp->_r = (int)(n - o);
2222fe8fb19SBen Gras 		if (HASUB(fp))
2232fe8fb19SBen Gras 			FREEUB(fp);
2242fe8fb19SBen Gras 		fp->_flags &= ~__SEOF;
2252fe8fb19SBen Gras 		FUNLOCKFILE(fp);
226f14fb602SLionel Sambuc 		return 0;
2272fe8fb19SBen Gras 	}
2282fe8fb19SBen Gras 
2292fe8fb19SBen Gras 	/*
2302fe8fb19SBen Gras 	 * The place we want to get to is not within the current buffer,
2312fe8fb19SBen Gras 	 * but we can still be kind to the kernel copyout mechanism.
2322fe8fb19SBen Gras 	 * By aligning the file offset to a block boundary, we can let
2332fe8fb19SBen Gras 	 * the kernel use the VM hardware to map pages instead of
2342fe8fb19SBen Gras 	 * copying bytes laboriously.  Using a block boundary also
2352fe8fb19SBen Gras 	 * ensures that we only read one block, rather than two.
2362fe8fb19SBen Gras 	 */
2372fe8fb19SBen Gras 	curoff = target & ~(fp->_blksize - 1);
2382fe8fb19SBen Gras 	if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR)
2392fe8fb19SBen Gras 		goto dumb;
2402fe8fb19SBen Gras 	fp->_r = 0;
2412fe8fb19SBen Gras  	fp->_p = fp->_bf._base;
2422fe8fb19SBen Gras 	if (HASUB(fp))
2432fe8fb19SBen Gras 		FREEUB(fp);
2442fe8fb19SBen Gras 	fp->_flags &= ~__SEOF;
2452fe8fb19SBen Gras 	n = (int)(target - curoff);
2462fe8fb19SBen Gras 	if (n) {
2472fe8fb19SBen Gras 		if (__srefill(fp) || (size_t)fp->_r < n)
2482fe8fb19SBen Gras 			goto dumb;
2492fe8fb19SBen Gras 		fp->_p += n;
250f14fb602SLionel Sambuc 		_DIAGASSERT(__type_fit(int, fp->_r - n));
251f14fb602SLionel Sambuc 		fp->_r -= (int)n;
2522fe8fb19SBen Gras 	}
2532fe8fb19SBen Gras 	FUNLOCKFILE(fp);
254f14fb602SLionel Sambuc 	return 0;
2552fe8fb19SBen Gras 
2562fe8fb19SBen Gras 	/*
2572fe8fb19SBen Gras 	 * We get here if we cannot optimise the seek ... just
2582fe8fb19SBen Gras 	 * do it.  Allow the seek function to change fp->_bf._base.
2592fe8fb19SBen Gras 	 */
2602fe8fb19SBen Gras dumb:
2612fe8fb19SBen Gras 	if (__sflush(fp) ||
262f14fb602SLionel Sambuc 	    (*seekfn)(fp->_cookie, offset, whence) == POS_ERR) {
2632fe8fb19SBen Gras 		FUNLOCKFILE(fp);
264f14fb602SLionel Sambuc 		return -1;
2652fe8fb19SBen Gras 	}
2662fe8fb19SBen Gras 	/* success: clear EOF indicator and discard ungetc() data */
2672fe8fb19SBen Gras 	if (HASUB(fp))
2682fe8fb19SBen Gras 		FREEUB(fp);
2692fe8fb19SBen Gras 	fp->_p = fp->_bf._base;
2702fe8fb19SBen Gras 	fp->_r = 0;
2712fe8fb19SBen Gras 	/* fp->_w = 0; */	/* unnecessary (I think...) */
2722fe8fb19SBen Gras 	fp->_flags &= ~__SEOF;
2732fe8fb19SBen Gras 	FUNLOCKFILE(fp);
274f14fb602SLionel Sambuc 	return 0;
2752fe8fb19SBen Gras }
276