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