fseek.c revision 35129
190075Sobrien/*- 2132718Skan * Copyright (c) 1990, 1993 390075Sobrien * The Regents of the University of California. All rights reserved. 490075Sobrien * 590075Sobrien * This code is derived from software contributed to Berkeley by 690075Sobrien * Chris Torek. 790075Sobrien * 890075Sobrien * Redistribution and use in source and binary forms, with or without 990075Sobrien * modification, are permitted provided that the following conditions 1090075Sobrien * are met: 1190075Sobrien * 1. Redistributions of source code must retain the above copyright 1290075Sobrien * notice, this list of conditions and the following disclaimer. 1390075Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1490075Sobrien * notice, this list of conditions and the following disclaimer in the 1590075Sobrien * documentation and/or other materials provided with the distribution. 1690075Sobrien * 3. All advertising materials mentioning features or use of this software 1790075Sobrien * must display the following acknowledgement: 1890075Sobrien * This product includes software developed by the University of 1990075Sobrien * California, Berkeley and its contributors. 2090075Sobrien * 4. Neither the name of the University nor the names of its contributors 2190075Sobrien * may be used to endorse or promote products derived from this software 2290075Sobrien * without specific prior written permission. 23132718Skan * 24132718Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2590075Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2690075Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2790075Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2890075Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2990075Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3090075Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3190075Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3290075Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3390075Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3490075Sobrien * SUCH DAMAGE. 3590075Sobrien */ 3690075Sobrien 3790075Sobrien#if defined(LIBC_SCCS) && !defined(lint) 38117395Skan#if 0 39117395Skanstatic char sccsid[] = "@(#)fseek.c 8.3 (Berkeley) 1/2/94"; 40132718Skan#endif 4190075Sobrienstatic const char rcsid[] = 42132718Skan "$Id: fseek.c,v 1.6 1997/02/22 15:02:05 peter Exp $"; 43132718Skan#endif /* LIBC_SCCS and not lint */ 44132718Skan 4590075Sobrien#include <sys/types.h> 4690075Sobrien#include <sys/stat.h> 47132718Skan#include <fcntl.h> 4890075Sobrien#include <stdio.h> 4990075Sobrien#include <stdlib.h> 5090075Sobrien#include <errno.h> 5190075Sobrien#include "local.h" 5290075Sobrien#include "libc_private.h" 5390075Sobrien 5490075Sobrien#define POS_ERR (-(fpos_t)1) 5590075Sobrien 5690075Sobrien/* 5790075Sobrien * Seek the given file to the given offset. 5890075Sobrien * `Whence' must be one of the three SEEK_* macros. 59132718Skan */ 6090075Sobrienint 6196263Sobrienfseek(fp, offset, whence) 6296263Sobrien register FILE *fp; 6396263Sobrien long offset; 64132718Skan int whence; 65132718Skan{ 6690075Sobrien register fpos_t (*seekfn) __P((void *, fpos_t, int)); 6790075Sobrien fpos_t target, curoff; 6890075Sobrien size_t n; 69132718Skan struct stat st; 7090075Sobrien int havepos; 7190075Sobrien 7290075Sobrien /* make sure stdio is set up */ 73132718Skan if (!__sdidinit) 74132718Skan __sinit(); 75132718Skan 76132718Skan FLOCKFILE(fp); 7790075Sobrien /* 7896263Sobrien * Have to be able to seek. 7996263Sobrien */ 80132718Skan if ((seekfn = fp->_seek) == NULL) { 81132718Skan errno = ESPIPE; /* historic practice */ 82132718Skan FUNLOCKFILE(fp); 83132718Skan return (EOF); 84132718Skan } 85132718Skan 8696263Sobrien /* 87132718Skan * Change any SEEK_CUR to SEEK_SET, and check `whence' argument. 88104752Skan * After this, whence is either SEEK_SET or SEEK_END. 89117395Skan */ 90132718Skan switch (whence) { 91132718Skan 92132718Skan case SEEK_CUR: 93132718Skan /* 94132718Skan * In order to seek relative to the current stream offset, 95132718Skan * we have to first find the current stream offset a la 96104752Skan * ftell (see ftell for details). 9790075Sobrien */ 98132718Skan if (fp->_flags & __SOFF) 99132718Skan curoff = fp->_offset; 100132718Skan else { 101132718Skan curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR); 102132718Skan if (curoff == -1) { 103132718Skan FUNLOCKFILE(fp); 10490075Sobrien return (EOF); 10590075Sobrien } 10690075Sobrien } 10790075Sobrien if (fp->_flags & __SRD) { 10890075Sobrien curoff -= fp->_r; 10990075Sobrien if (HASUB(fp)) 11090075Sobrien curoff -= fp->_ur; 11190075Sobrien } else if (fp->_flags & __SWR && fp->_p != NULL) 11290075Sobrien curoff += fp->_p - fp->_bf._base; 113132718Skan 114132718Skan offset += curoff; 115132718Skan whence = SEEK_SET; 116132718Skan havepos = 1; 117132718Skan break; 118132718Skan 11990075Sobrien case SEEK_SET: 12090075Sobrien case SEEK_END: 121132718Skan curoff = 0; /* XXX just to keep gcc quiet */ 12290075Sobrien havepos = 0; 12390075Sobrien break; 12490075Sobrien 12590075Sobrien default: 126132718Skan errno = EINVAL; 127132718Skan FUNLOCKFILE(fp); 128132718Skan return (EOF); 129132718Skan } 130132718Skan 131132718Skan /* 13290075Sobrien * Can only optimise if: 13390075Sobrien * reading (and not reading-and-writing); 13490075Sobrien * not unbuffered; and 135117395Skan * this is a `regular' Unix file (and hence seekfn==__sseek). 136117395Skan * We must check __NBF first, because it is possible to have __NBF 137117395Skan * and __SOPT both set. 138117395Skan */ 13990075Sobrien if (fp->_bf._base == NULL) 14090075Sobrien __smakebuf(fp); 141117395Skan if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT)) 142117395Skan goto dumb; 143117395Skan if ((fp->_flags & __SOPT) == 0) { 144132718Skan if (seekfn != __sseek || 145117395Skan fp->_file < 0 || fstat(fp->_file, &st) || 146117395Skan (st.st_mode & S_IFMT) != S_IFREG) { 147117395Skan fp->_flags |= __SNPT; 148117395Skan goto dumb; 149117395Skan } 150117395Skan fp->_blksize = st.st_blksize; 151117395Skan fp->_flags |= __SOPT; 152117395Skan } 153117395Skan 15490075Sobrien /* 155132718Skan * We are reading; we can try to optimise. 156132718Skan * Figure out where we are going and where we are now. 15790075Sobrien */ 158132718Skan if (whence == SEEK_SET) 159132718Skan target = offset; 160132718Skan else { 161132718Skan if (fstat(fp->_file, &st)) 162132718Skan goto dumb; 163132718Skan target = st.st_size + offset; 16490075Sobrien } 16590075Sobrien 166132718Skan if (!havepos) { 167132718Skan if (fp->_flags & __SOFF) 16890075Sobrien curoff = fp->_offset; 16990075Sobrien else { 17090075Sobrien curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR); 17190075Sobrien if (curoff == POS_ERR) 17290075Sobrien goto dumb; 17390075Sobrien } 17490075Sobrien curoff -= fp->_r; 17590075Sobrien if (HASUB(fp)) 17690075Sobrien curoff -= fp->_ur; 17790075Sobrien } 17890075Sobrien 17990075Sobrien /* 18090075Sobrien * Compute the number of bytes in the input buffer (pretending 18190075Sobrien * that any ungetc() input has been discarded). Adjust current 18290075Sobrien * offset backwards by this count so that it represents the 18390075Sobrien * file offset for the first byte in the current input buffer. 18490075Sobrien */ 18590075Sobrien if (HASUB(fp)) { 18690075Sobrien curoff += fp->_r; /* kill off ungetc */ 187132718Skan n = fp->_up - fp->_bf._base; 18890075Sobrien curoff -= n; 18990075Sobrien n += fp->_ur; 19090075Sobrien } else { 191132718Skan n = fp->_p - fp->_bf._base; 19290075Sobrien curoff -= n; 19390075Sobrien n += fp->_r; 19490075Sobrien } 19590075Sobrien 19690075Sobrien /* 19790075Sobrien * If the target offset is within the current buffer, 19890075Sobrien * simply adjust the pointers, clear EOF, undo ungetc(), 19990075Sobrien * and return. (If the buffer was modified, we have to 20090075Sobrien * skip this; see fgetln.c.) 20190075Sobrien */ 20290075Sobrien if ((fp->_flags & __SMOD) == 0 && 20390075Sobrien target >= curoff && target < curoff + n) { 20490075Sobrien register int o = target - curoff; 20590075Sobrien 20690075Sobrien fp->_p = fp->_bf._base + o; 20790075Sobrien fp->_r = n - o; 20890075Sobrien if (HASUB(fp)) 20990075Sobrien FREEUB(fp); 21090075Sobrien fp->_flags &= ~__SEOF; 21190075Sobrien FUNLOCKFILE(fp); 21290075Sobrien return (0); 21390075Sobrien } 21490075Sobrien 21590075Sobrien /* 21690075Sobrien * The place we want to get to is not within the current buffer, 217132718Skan * but we can still be kind to the kernel copyout mechanism. 21890075Sobrien * By aligning the file offset to a block boundary, we can let 21990075Sobrien * the kernel use the VM hardware to map pages instead of 22090075Sobrien * copying bytes laboriously. Using a block boundary also 22190075Sobrien * ensures that we only read one block, rather than two. 22290075Sobrien */ 22390075Sobrien curoff = target & ~(fp->_blksize - 1); 22490075Sobrien if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR) 22590075Sobrien goto dumb; 22690075Sobrien fp->_r = 0; 22790075Sobrien fp->_p = fp->_bf._base; 22890075Sobrien if (HASUB(fp)) 229132718Skan FREEUB(fp); 23090075Sobrien fp->_flags &= ~__SEOF; 23190075Sobrien n = target - curoff; 23290075Sobrien if (n) { 23390075Sobrien if (__srefill(fp) || fp->_r < n) 23490075Sobrien goto dumb; 235132718Skan fp->_p += n; 23690075Sobrien fp->_r -= n; 237132718Skan } 238132718Skan FUNLOCKFILE(fp); 23990075Sobrien return (0); 240132718Skan 241132718Skan /* 242132718Skan * We get here if we cannot optimise the seek ... just 243132718Skan * do it. Allow the seek function to change fp->_bf._base. 244132718Skan */ 245132718Skandumb: 246132718Skan if (__sflush(fp) || 24790075Sobrien (*seekfn)(fp->_cookie, (fpos_t)offset, whence) == POS_ERR) { 24890075Sobrien FUNLOCKFILE(fp); 24990075Sobrien return (EOF); 25090075Sobrien } 25190075Sobrien /* success: clear EOF indicator and discard ungetc() data */ 25290075Sobrien if (HASUB(fp)) 25390075Sobrien FREEUB(fp); 25490075Sobrien fp->_p = fp->_bf._base; 25590075Sobrien fp->_r = 0; 25690075Sobrien /* fp->_w = 0; */ /* unnecessary (I think...) */ 25790075Sobrien fp->_flags &= ~__SEOF; 25890075Sobrien FUNLOCKFILE(fp); 25990075Sobrien return (0); 26090075Sobrien} 26190075Sobrien