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) 3858f0484fSRodney W. Grimes static char sccsid[] = "@(#)fseek.c 8.3 (Berkeley) 1/2/94"; 3958f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 4058f0484fSRodney W. Grimes 4158f0484fSRodney W. Grimes #include <sys/types.h> 4258f0484fSRodney W. Grimes #include <sys/stat.h> 4358f0484fSRodney W. Grimes #include <fcntl.h> 4458f0484fSRodney W. Grimes #include <stdio.h> 4558f0484fSRodney W. Grimes #include <stdlib.h> 4658f0484fSRodney W. Grimes #include <errno.h> 4758f0484fSRodney W. Grimes #include "local.h" 48f70177e7SJulian Elischer #ifdef _THREAD_SAFE 49f70177e7SJulian Elischer #include <pthread.h> 50f70177e7SJulian Elischer #include "pthread_private.h" 51f70177e7SJulian Elischer #endif 5258f0484fSRodney W. Grimes 5358f0484fSRodney W. Grimes #define POS_ERR (-(fpos_t)1) 5458f0484fSRodney W. Grimes 5558f0484fSRodney W. Grimes /* 5658f0484fSRodney W. Grimes * Seek the given file to the given offset. 5758f0484fSRodney W. Grimes * `Whence' must be one of the three SEEK_* macros. 5858f0484fSRodney W. Grimes */ 5958f0484fSRodney W. Grimes int 6058f0484fSRodney W. Grimes fseek(fp, offset, whence) 6158f0484fSRodney W. Grimes register FILE *fp; 6258f0484fSRodney W. Grimes long offset; 6358f0484fSRodney W. Grimes int whence; 6458f0484fSRodney W. Grimes { 6558f0484fSRodney W. Grimes register fpos_t (*seekfn) __P((void *, fpos_t, int)); 6658f0484fSRodney W. Grimes fpos_t target, curoff; 6758f0484fSRodney W. Grimes size_t n; 6858f0484fSRodney W. Grimes struct stat st; 6958f0484fSRodney W. Grimes int havepos; 7058f0484fSRodney W. Grimes 7158f0484fSRodney W. Grimes /* make sure stdio is set up */ 7258f0484fSRodney W. Grimes if (!__sdidinit) 7358f0484fSRodney W. Grimes __sinit(); 7458f0484fSRodney W. Grimes 75f70177e7SJulian Elischer #ifdef _THREAD_SAFE 76f70177e7SJulian Elischer _thread_flockfile(fp,__FILE__,__LINE__); 77f70177e7SJulian Elischer #endif 7858f0484fSRodney W. Grimes /* 7958f0484fSRodney W. Grimes * Have to be able to seek. 8058f0484fSRodney W. Grimes */ 8158f0484fSRodney W. Grimes if ((seekfn = fp->_seek) == NULL) { 8258f0484fSRodney W. Grimes errno = ESPIPE; /* historic practice */ 8358f0484fSRodney W. Grimes return (EOF); 8458f0484fSRodney W. Grimes } 8558f0484fSRodney W. Grimes 8658f0484fSRodney W. Grimes /* 8758f0484fSRodney W. Grimes * Change any SEEK_CUR to SEEK_SET, and check `whence' argument. 8858f0484fSRodney W. Grimes * After this, whence is either SEEK_SET or SEEK_END. 8958f0484fSRodney W. Grimes */ 9058f0484fSRodney W. Grimes switch (whence) { 9158f0484fSRodney W. Grimes 9258f0484fSRodney W. Grimes case SEEK_CUR: 9358f0484fSRodney W. Grimes /* 9458f0484fSRodney W. Grimes * In order to seek relative to the current stream offset, 9558f0484fSRodney W. Grimes * we have to first find the current stream offset a la 9658f0484fSRodney W. Grimes * ftell (see ftell for details). 9758f0484fSRodney W. Grimes */ 9858f0484fSRodney W. Grimes if (fp->_flags & __SOFF) 9958f0484fSRodney W. Grimes curoff = fp->_offset; 10058f0484fSRodney W. Grimes else { 10158f0484fSRodney W. Grimes curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR); 102f70177e7SJulian Elischer if (curoff == -1L) { 103f70177e7SJulian Elischer #ifdef _THREAD_SAFE 104f70177e7SJulian Elischer _thread_funlockfile(fp); 105f70177e7SJulian Elischer #endif 10658f0484fSRodney W. Grimes return (EOF); 10758f0484fSRodney W. Grimes } 108f70177e7SJulian Elischer } 10958f0484fSRodney W. Grimes if (fp->_flags & __SRD) { 11058f0484fSRodney W. Grimes curoff -= fp->_r; 11158f0484fSRodney W. Grimes if (HASUB(fp)) 11258f0484fSRodney W. Grimes curoff -= fp->_ur; 11358f0484fSRodney W. Grimes } else if (fp->_flags & __SWR && fp->_p != NULL) 11458f0484fSRodney W. Grimes curoff += fp->_p - fp->_bf._base; 11558f0484fSRodney W. Grimes 11658f0484fSRodney W. Grimes offset += curoff; 11758f0484fSRodney W. Grimes whence = SEEK_SET; 11858f0484fSRodney W. Grimes havepos = 1; 11958f0484fSRodney W. Grimes break; 12058f0484fSRodney W. Grimes 12158f0484fSRodney W. Grimes case SEEK_SET: 12258f0484fSRodney W. Grimes case SEEK_END: 12358f0484fSRodney W. Grimes curoff = 0; /* XXX just to keep gcc quiet */ 12458f0484fSRodney W. Grimes havepos = 0; 12558f0484fSRodney W. Grimes break; 12658f0484fSRodney W. Grimes 12758f0484fSRodney W. Grimes default: 12858f0484fSRodney W. Grimes errno = EINVAL; 129f70177e7SJulian Elischer #ifdef _THREAD_SAFE 130f70177e7SJulian Elischer _thread_funlockfile(fp); 131f70177e7SJulian Elischer #endif 13258f0484fSRodney W. Grimes return (EOF); 13358f0484fSRodney W. Grimes } 13458f0484fSRodney W. Grimes 13558f0484fSRodney W. Grimes /* 13658f0484fSRodney W. Grimes * Can only optimise if: 13758f0484fSRodney W. Grimes * reading (and not reading-and-writing); 13858f0484fSRodney W. Grimes * not unbuffered; and 13958f0484fSRodney W. Grimes * this is a `regular' Unix file (and hence seekfn==__sseek). 14058f0484fSRodney W. Grimes * We must check __NBF first, because it is possible to have __NBF 14158f0484fSRodney W. Grimes * and __SOPT both set. 14258f0484fSRodney W. Grimes */ 14358f0484fSRodney W. Grimes if (fp->_bf._base == NULL) 14458f0484fSRodney W. Grimes __smakebuf(fp); 14558f0484fSRodney W. Grimes if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT)) 14658f0484fSRodney W. Grimes goto dumb; 14758f0484fSRodney W. Grimes if ((fp->_flags & __SOPT) == 0) { 14858f0484fSRodney W. Grimes if (seekfn != __sseek || 14958f0484fSRodney W. Grimes fp->_file < 0 || fstat(fp->_file, &st) || 15058f0484fSRodney W. Grimes (st.st_mode & S_IFMT) != S_IFREG) { 15158f0484fSRodney W. Grimes fp->_flags |= __SNPT; 15258f0484fSRodney W. Grimes goto dumb; 15358f0484fSRodney W. Grimes } 15458f0484fSRodney W. Grimes fp->_blksize = st.st_blksize; 15558f0484fSRodney W. Grimes fp->_flags |= __SOPT; 15658f0484fSRodney W. Grimes } 15758f0484fSRodney W. Grimes 15858f0484fSRodney W. Grimes /* 15958f0484fSRodney W. Grimes * We are reading; we can try to optimise. 16058f0484fSRodney W. Grimes * Figure out where we are going and where we are now. 16158f0484fSRodney W. Grimes */ 16258f0484fSRodney W. Grimes if (whence == SEEK_SET) 16358f0484fSRodney W. Grimes target = offset; 16458f0484fSRodney W. Grimes else { 16558f0484fSRodney W. Grimes if (fstat(fp->_file, &st)) 16658f0484fSRodney W. Grimes goto dumb; 16758f0484fSRodney W. Grimes target = st.st_size + offset; 16858f0484fSRodney W. Grimes } 16958f0484fSRodney W. Grimes 17058f0484fSRodney W. Grimes if (!havepos) { 17158f0484fSRodney W. Grimes if (fp->_flags & __SOFF) 17258f0484fSRodney W. Grimes curoff = fp->_offset; 17358f0484fSRodney W. Grimes else { 17458f0484fSRodney W. Grimes curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR); 17558f0484fSRodney W. Grimes if (curoff == POS_ERR) 17658f0484fSRodney W. Grimes goto dumb; 17758f0484fSRodney W. Grimes } 17858f0484fSRodney W. Grimes curoff -= fp->_r; 17958f0484fSRodney W. Grimes if (HASUB(fp)) 18058f0484fSRodney W. Grimes curoff -= fp->_ur; 18158f0484fSRodney W. Grimes } 18258f0484fSRodney W. Grimes 18358f0484fSRodney W. Grimes /* 18458f0484fSRodney W. Grimes * Compute the number of bytes in the input buffer (pretending 18558f0484fSRodney W. Grimes * that any ungetc() input has been discarded). Adjust current 18658f0484fSRodney W. Grimes * offset backwards by this count so that it represents the 18758f0484fSRodney W. Grimes * file offset for the first byte in the current input buffer. 18858f0484fSRodney W. Grimes */ 18958f0484fSRodney W. Grimes if (HASUB(fp)) { 19058f0484fSRodney W. Grimes curoff += fp->_r; /* kill off ungetc */ 19158f0484fSRodney W. Grimes n = fp->_up - fp->_bf._base; 19258f0484fSRodney W. Grimes curoff -= n; 19358f0484fSRodney W. Grimes n += fp->_ur; 19458f0484fSRodney W. Grimes } else { 19558f0484fSRodney W. Grimes n = fp->_p - fp->_bf._base; 19658f0484fSRodney W. Grimes curoff -= n; 19758f0484fSRodney W. Grimes n += fp->_r; 19858f0484fSRodney W. Grimes } 19958f0484fSRodney W. Grimes 20058f0484fSRodney W. Grimes /* 20158f0484fSRodney W. Grimes * If the target offset is within the current buffer, 20258f0484fSRodney W. Grimes * simply adjust the pointers, clear EOF, undo ungetc(), 20358f0484fSRodney W. Grimes * and return. (If the buffer was modified, we have to 20458f0484fSRodney W. Grimes * skip this; see fgetln.c.) 20558f0484fSRodney W. Grimes */ 20658f0484fSRodney W. Grimes if ((fp->_flags & __SMOD) == 0 && 20758f0484fSRodney W. Grimes target >= curoff && target < curoff + n) { 20858f0484fSRodney W. Grimes register int o = target - curoff; 20958f0484fSRodney W. Grimes 21058f0484fSRodney W. Grimes fp->_p = fp->_bf._base + o; 21158f0484fSRodney W. Grimes fp->_r = n - o; 21258f0484fSRodney W. Grimes if (HASUB(fp)) 21358f0484fSRodney W. Grimes FREEUB(fp); 21458f0484fSRodney W. Grimes fp->_flags &= ~__SEOF; 215f70177e7SJulian Elischer #ifdef _THREAD_SAFE 216f70177e7SJulian Elischer _thread_funlockfile(fp); 217f70177e7SJulian Elischer #endif 21858f0484fSRodney W. Grimes return (0); 21958f0484fSRodney W. Grimes } 22058f0484fSRodney W. Grimes 22158f0484fSRodney W. Grimes /* 22258f0484fSRodney W. Grimes * The place we want to get to is not within the current buffer, 22358f0484fSRodney W. Grimes * but we can still be kind to the kernel copyout mechanism. 22458f0484fSRodney W. Grimes * By aligning the file offset to a block boundary, we can let 22558f0484fSRodney W. Grimes * the kernel use the VM hardware to map pages instead of 22658f0484fSRodney W. Grimes * copying bytes laboriously. Using a block boundary also 22758f0484fSRodney W. Grimes * ensures that we only read one block, rather than two. 22858f0484fSRodney W. Grimes */ 22958f0484fSRodney W. Grimes curoff = target & ~(fp->_blksize - 1); 23058f0484fSRodney W. Grimes if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR) 23158f0484fSRodney W. Grimes goto dumb; 23258f0484fSRodney W. Grimes fp->_r = 0; 233692a99c0SNate Williams fp->_p = fp->_bf._base; 23458f0484fSRodney W. Grimes if (HASUB(fp)) 23558f0484fSRodney W. Grimes FREEUB(fp); 23658f0484fSRodney W. Grimes fp->_flags &= ~__SEOF; 23758f0484fSRodney W. Grimes n = target - curoff; 23858f0484fSRodney W. Grimes if (n) { 23958f0484fSRodney W. Grimes if (__srefill(fp) || fp->_r < n) 24058f0484fSRodney W. Grimes goto dumb; 24158f0484fSRodney W. Grimes fp->_p += n; 24258f0484fSRodney W. Grimes fp->_r -= n; 24358f0484fSRodney W. Grimes } 244f70177e7SJulian Elischer #ifdef _THREAD_SAFE 245f70177e7SJulian Elischer _thread_funlockfile(fp); 246f70177e7SJulian Elischer #endif 24758f0484fSRodney W. Grimes return (0); 24858f0484fSRodney W. Grimes 24958f0484fSRodney W. Grimes /* 25058f0484fSRodney W. Grimes * We get here if we cannot optimise the seek ... just 25158f0484fSRodney W. Grimes * do it. Allow the seek function to change fp->_bf._base. 25258f0484fSRodney W. Grimes */ 25358f0484fSRodney W. Grimes dumb: 25458f0484fSRodney W. Grimes if (__sflush(fp) || 25558f0484fSRodney W. Grimes (*seekfn)(fp->_cookie, (fpos_t)offset, whence) == POS_ERR) { 256f70177e7SJulian Elischer #ifdef _THREAD_SAFE 257f70177e7SJulian Elischer _thread_funlockfile(fp); 258f70177e7SJulian Elischer #endif 25958f0484fSRodney W. Grimes return (EOF); 26058f0484fSRodney W. Grimes } 26158f0484fSRodney W. Grimes /* success: clear EOF indicator and discard ungetc() data */ 26258f0484fSRodney W. Grimes if (HASUB(fp)) 26358f0484fSRodney W. Grimes FREEUB(fp); 26458f0484fSRodney W. Grimes fp->_p = fp->_bf._base; 26558f0484fSRodney W. Grimes fp->_r = 0; 26658f0484fSRodney W. Grimes /* fp->_w = 0; */ /* unnecessary (I think...) */ 26758f0484fSRodney W. Grimes fp->_flags &= ~__SEOF; 268f70177e7SJulian Elischer #ifdef _THREAD_SAFE 269f70177e7SJulian Elischer _thread_funlockfile(fp); 270f70177e7SJulian Elischer #endif 27158f0484fSRodney W. Grimes return (0); 27258f0484fSRodney W. Grimes } 273