146087Sbostic /*- 261180Sbostic * Copyright (c) 1990, 1993 361180Sbostic * The Regents of the University of California. 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*64811Storek static char sccsid[] = "@(#)fseek.c 8.2 (Berkeley) 11/04/93"; 1346087Sbostic #endif /* LIBC_SCCS and not lint */ 1422134Smckusick 1546087Sbostic #include <sys/types.h> 1646087Sbostic #include <sys/stat.h> 1746611Sbostic #include <fcntl.h> 1846087Sbostic #include <stdio.h> 1946611Sbostic #include <stdlib.h> 2046087Sbostic #include <errno.h> 2146087Sbostic #include "local.h" 2246087Sbostic 2346087Sbostic #define POS_ERR (-(fpos_t)1) 2446087Sbostic 252009Swnj /* 2646087Sbostic * Seek the given file to the given offset. 2746087Sbostic * `Whence' must be one of the three SEEK_* macros. 282009Swnj */ 29*64811Storek int 3046087Sbostic fseek(fp, offset, whence) 3146087Sbostic register FILE *fp; 3246087Sbostic long offset; 3346087Sbostic int whence; 3446087Sbostic { 35*64811Storek register fpos_t (*seekfn) __P((void *, fpos_t, int)); 3646087Sbostic fpos_t target, curoff; 3746087Sbostic size_t n; 3846087Sbostic struct stat st; 3946087Sbostic int havepos; 402009Swnj 4146087Sbostic /* make sure stdio is set up */ 4246087Sbostic if (!__sdidinit) 4346087Sbostic __sinit(); 442009Swnj 4546087Sbostic /* 4646087Sbostic * Have to be able to seek. 4746087Sbostic */ 4846087Sbostic if ((seekfn = fp->_seek) == NULL) { 4946087Sbostic errno = ESPIPE; /* historic practice */ 5046087Sbostic return (EOF); 5146087Sbostic } 522009Swnj 5346087Sbostic /* 5446087Sbostic * Change any SEEK_CUR to SEEK_SET, and check `whence' argument. 5546087Sbostic * After this, whence is either SEEK_SET or SEEK_END. 5646087Sbostic */ 5746087Sbostic switch (whence) { 582009Swnj 5946087Sbostic case SEEK_CUR: 6046087Sbostic /* 6146087Sbostic * In order to seek relative to the current stream offset, 6246087Sbostic * we have to first find the current stream offset a la 6346087Sbostic * ftell (see ftell for details). 6446087Sbostic */ 6546087Sbostic if (fp->_flags & __SOFF) 6646087Sbostic curoff = fp->_offset; 6746087Sbostic else { 6846087Sbostic curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR); 6946087Sbostic if (curoff == -1L) 7046087Sbostic return (EOF); 713163Stoy } 7246087Sbostic if (fp->_flags & __SRD) { 7346087Sbostic curoff -= fp->_r; 7446087Sbostic if (HASUB(fp)) 7546087Sbostic curoff -= fp->_ur; 7646087Sbostic } else if (fp->_flags & __SWR && fp->_p != NULL) 7746087Sbostic curoff += fp->_p - fp->_bf._base; 7846087Sbostic 7946087Sbostic offset += curoff; 8046087Sbostic whence = SEEK_SET; 8146087Sbostic havepos = 1; 8246087Sbostic break; 8346087Sbostic 8446087Sbostic case SEEK_SET: 8546087Sbostic case SEEK_END: 8646271Storek curoff = 0; /* XXX just to keep gcc quiet */ 8746087Sbostic havepos = 0; 8846087Sbostic break; 8946087Sbostic 9046087Sbostic default: 9146087Sbostic errno = EINVAL; 9246087Sbostic return (EOF); 932009Swnj } 9446087Sbostic 9546087Sbostic /* 9646087Sbostic * Can only optimise if: 9746087Sbostic * reading (and not reading-and-writing); 9846087Sbostic * not unbuffered; and 9946087Sbostic * this is a `regular' Unix file (and hence seekfn==__sseek). 10046087Sbostic * We must check __NBF first, because it is possible to have __NBF 10146087Sbostic * and __SOPT both set. 10246087Sbostic */ 10346087Sbostic if (fp->_bf._base == NULL) 10446087Sbostic __smakebuf(fp); 10546087Sbostic if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT)) 10646087Sbostic goto dumb; 10746087Sbostic if ((fp->_flags & __SOPT) == 0) { 10846087Sbostic if (seekfn != __sseek || 10946087Sbostic fp->_file < 0 || fstat(fp->_file, &st) || 11046087Sbostic (st.st_mode & S_IFMT) != S_IFREG) { 11146087Sbostic fp->_flags |= __SNPT; 11246087Sbostic goto dumb; 1133163Stoy } 11446087Sbostic fp->_blksize = st.st_blksize; 11546087Sbostic fp->_flags |= __SOPT; 1162009Swnj } 11746087Sbostic 11846087Sbostic /* 11946087Sbostic * We are reading; we can try to optimise. 12046087Sbostic * Figure out where we are going and where we are now. 12146087Sbostic */ 12246087Sbostic if (whence == SEEK_SET) 12346087Sbostic target = offset; 12446087Sbostic else { 12546087Sbostic if (fstat(fp->_file, &st)) 12646087Sbostic goto dumb; 12746087Sbostic target = st.st_size + offset; 12846087Sbostic } 12946087Sbostic 13046087Sbostic if (!havepos) { 13146087Sbostic if (fp->_flags & __SOFF) 13246087Sbostic curoff = fp->_offset; 13346087Sbostic else { 13454197Sbostic curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR); 13546087Sbostic if (curoff == POS_ERR) 13646087Sbostic goto dumb; 13746087Sbostic } 13846087Sbostic curoff -= fp->_r; 13946087Sbostic if (HASUB(fp)) 14046087Sbostic curoff -= fp->_ur; 14146087Sbostic } 14246087Sbostic 14346087Sbostic /* 14446087Sbostic * Compute the number of bytes in the input buffer (pretending 14546087Sbostic * that any ungetc() input has been discarded). Adjust current 14646087Sbostic * offset backwards by this count so that it represents the 14746087Sbostic * file offset for the first byte in the current input buffer. 14846087Sbostic */ 14946087Sbostic if (HASUB(fp)) { 150*64811Storek curoff += fp->_r; /* kill off ungetc */ 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: 20746215Sbostic if (__sflush(fp) || 20854197Sbostic (*seekfn)(fp->_cookie, (fpos_t)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