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*46215Sbostic static char sccsid[] = "@(#)fseek.c 5.5 (Berkeley) 02/01/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__ 3446087Sbostic register fpos_t (*seekfn)(char *, 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: 8846087Sbostic havepos = 0; 8946087Sbostic break; 9046087Sbostic 9146087Sbostic default: 9246087Sbostic errno = EINVAL; 9346087Sbostic return (EOF); 942009Swnj } 9546087Sbostic 9646087Sbostic /* 9746087Sbostic * Can only optimise if: 9846087Sbostic * reading (and not reading-and-writing); 9946087Sbostic * not unbuffered; and 10046087Sbostic * this is a `regular' Unix file (and hence seekfn==__sseek). 10146087Sbostic * We must check __NBF first, because it is possible to have __NBF 10246087Sbostic * and __SOPT both set. 10346087Sbostic */ 10446087Sbostic if (fp->_bf._base == NULL) 10546087Sbostic __smakebuf(fp); 10646087Sbostic if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT)) 10746087Sbostic goto dumb; 10846087Sbostic if ((fp->_flags & __SOPT) == 0) { 10946087Sbostic if (seekfn != __sseek || 11046087Sbostic fp->_file < 0 || fstat(fp->_file, &st) || 11146087Sbostic (st.st_mode & S_IFMT) != S_IFREG) { 11246087Sbostic fp->_flags |= __SNPT; 11346087Sbostic goto dumb; 1143163Stoy } 11546087Sbostic fp->_blksize = st.st_blksize; 11646087Sbostic fp->_flags |= __SOPT; 1172009Swnj } 11846087Sbostic 11946087Sbostic /* 12046087Sbostic * We are reading; we can try to optimise. 12146087Sbostic * Figure out where we are going and where we are now. 12246087Sbostic */ 12346087Sbostic if (whence == SEEK_SET) 12446087Sbostic target = offset; 12546087Sbostic else { 12646087Sbostic if (fstat(fp->_file, &st)) 12746087Sbostic goto dumb; 12846087Sbostic target = st.st_size + offset; 12946087Sbostic } 13046087Sbostic 13146087Sbostic if (!havepos) { 13246087Sbostic if (fp->_flags & __SOFF) 13346087Sbostic curoff = fp->_offset; 13446087Sbostic else { 13546087Sbostic curoff = (*seekfn)(fp->_cookie, 0L, SEEK_CUR); 13646087Sbostic if (curoff == POS_ERR) 13746087Sbostic goto dumb; 13846087Sbostic } 13946087Sbostic curoff -= fp->_r; 14046087Sbostic if (HASUB(fp)) 14146087Sbostic curoff -= fp->_ur; 14246087Sbostic } 14346087Sbostic 14446087Sbostic /* 14546087Sbostic * Compute the number of bytes in the input buffer (pretending 14646087Sbostic * that any ungetc() input has been discarded). Adjust current 14746087Sbostic * offset backwards by this count so that it represents the 14846087Sbostic * file offset for the first byte in the current input buffer. 14946087Sbostic */ 15046087Sbostic if (HASUB(fp)) { 15146087Sbostic n = fp->_up - fp->_bf._base; 15246087Sbostic curoff -= n; 15346087Sbostic n += fp->_ur; 15446087Sbostic } else { 15546087Sbostic n = fp->_p - fp->_bf._base; 15646087Sbostic curoff -= n; 15746087Sbostic n += fp->_r; 15846087Sbostic } 15946087Sbostic 16046087Sbostic /* 16146087Sbostic * If the target offset is within the current buffer, 16246087Sbostic * simply adjust the pointers, clear EOF, undo ungetc(), 16346087Sbostic * and return. (If the buffer was modified, we have to 16446087Sbostic * skip this; see fgetline.c.) 16546087Sbostic */ 16646087Sbostic if ((fp->_flags & __SMOD) == 0 && 16746087Sbostic target >= curoff && target < curoff + n) { 16846087Sbostic register int o = target - curoff; 16946087Sbostic 17046087Sbostic fp->_p = fp->_bf._base + o; 17146087Sbostic fp->_r = n - o; 17246087Sbostic if (HASUB(fp)) 17346087Sbostic FREEUB(fp); 17446087Sbostic fp->_flags &= ~__SEOF; 17546087Sbostic return (0); 17646087Sbostic } 17746087Sbostic 17846087Sbostic /* 17946087Sbostic * The place we want to get to is not within the current buffer, 18046087Sbostic * but we can still be kind to the kernel copyout mechanism. 18146087Sbostic * By aligning the file offset to a block boundary, we can let 18246087Sbostic * the kernel use the VM hardware to map pages instead of 18346087Sbostic * copying bytes laboriously. Using a block boundary also 18446087Sbostic * ensures that we only read one block, rather than two. 18546087Sbostic */ 18646087Sbostic curoff = target & ~(fp->_blksize - 1); 18746087Sbostic if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR) 18846087Sbostic goto dumb; 18946087Sbostic fp->_r = 0; 19046087Sbostic if (HASUB(fp)) 19146087Sbostic FREEUB(fp); 19246087Sbostic fp->_flags &= ~__SEOF; 19346087Sbostic n = target - curoff; 19446087Sbostic if (n) { 19546087Sbostic if (__srefill(fp) || fp->_r < n) 19646087Sbostic goto dumb; 19746087Sbostic fp->_p += n; 19846087Sbostic fp->_r -= n; 19946087Sbostic } 20046087Sbostic return (0); 20146087Sbostic 20246087Sbostic /* 20346087Sbostic * We get here if we cannot optimise the seek ... just 20446087Sbostic * do it. Allow the seek function to change fp->_bf._base. 20546087Sbostic */ 20646087Sbostic dumb: 207*46215Sbostic if (__sflush(fp) || 20846087Sbostic (*seekfn)(fp->_cookie, offset, whence) == POS_ERR) { 20946087Sbostic return (EOF); 21046087Sbostic } 21146087Sbostic /* success: clear EOF indicator and discard ungetc() data */ 21246087Sbostic if (HASUB(fp)) 21346087Sbostic FREEUB(fp); 21446087Sbostic fp->_p = fp->_bf._base; 21546087Sbostic fp->_r = 0; 21646087Sbostic /* fp->_w = 0; */ /* unnecessary (I think...) */ 21746087Sbostic fp->_flags &= ~__SEOF; 21846087Sbostic return (0); 2192009Swnj } 220