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) 38ce51cf03SJames Raynard #if 0 3958f0484fSRodney W. Grimes static char sccsid[] = "@(#)fseek.c 8.3 (Berkeley) 1/2/94"; 40ce51cf03SJames Raynard #endif 41ce51cf03SJames Raynard static const char rcsid[] = 427f3dea24SPeter Wemm "$FreeBSD$"; 4358f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 4458f0484fSRodney W. Grimes 45d201fe46SDaniel Eischen #include "namespace.h" 4658f0484fSRodney W. Grimes #include <sys/types.h> 4758f0484fSRodney W. Grimes #include <sys/stat.h> 4874b27728SAndrey A. Chernov #include <errno.h> 4958f0484fSRodney W. Grimes #include <fcntl.h> 5074b27728SAndrey A. Chernov #include <limits.h> 5158f0484fSRodney W. Grimes #include <stdio.h> 5258f0484fSRodney W. Grimes #include <stdlib.h> 53d201fe46SDaniel Eischen #include "un-namespace.h" 5458f0484fSRodney W. Grimes #include "local.h" 55ec216c26SJohn Birrell #include "libc_private.h" 5658f0484fSRodney W. Grimes 5758f0484fSRodney W. Grimes #define POS_ERR (-(fpos_t)1) 5858f0484fSRodney W. Grimes 597307d07dSDmitrij Tejblum int 607307d07dSDmitrij Tejblum fseek(fp, offset, whence) 617307d07dSDmitrij Tejblum register FILE *fp; 627307d07dSDmitrij Tejblum long offset; 637307d07dSDmitrij Tejblum int whence; 647307d07dSDmitrij Tejblum { 6571b5a432SAndrey A. Chernov int ret; 6635e1a550SAndrey A. Chernov int serrno = errno; 6771b5a432SAndrey A. Chernov 6871b5a432SAndrey A. Chernov /* make sure stdio is set up */ 6971b5a432SAndrey A. Chernov if (!__sdidinit) 7071b5a432SAndrey A. Chernov __sinit(); 7171b5a432SAndrey A. Chernov 7271b5a432SAndrey A. Chernov FLOCKFILE(fp); 7371b5a432SAndrey A. Chernov ret = _fseeko(fp, (off_t)offset, whence, 1); 7471b5a432SAndrey A. Chernov FUNLOCKFILE(fp); 7535e1a550SAndrey A. Chernov if (ret == 0) 7635e1a550SAndrey A. Chernov errno = serrno; 7771b5a432SAndrey A. Chernov return (ret); 787307d07dSDmitrij Tejblum } 797307d07dSDmitrij Tejblum 80d201fe46SDaniel Eischen int 81d201fe46SDaniel Eischen fseeko(fp, offset, whence) 82d201fe46SDaniel Eischen FILE *fp; 83d201fe46SDaniel Eischen off_t offset; 84d201fe46SDaniel Eischen int whence; 85d201fe46SDaniel Eischen { 86d201fe46SDaniel Eischen int ret; 8735e1a550SAndrey A. Chernov int serrno = errno; 88d201fe46SDaniel Eischen 89d201fe46SDaniel Eischen /* make sure stdio is set up */ 90d201fe46SDaniel Eischen if (!__sdidinit) 91d201fe46SDaniel Eischen __sinit(); 92d201fe46SDaniel Eischen 93d201fe46SDaniel Eischen FLOCKFILE(fp); 9471b5a432SAndrey A. Chernov ret = _fseeko(fp, offset, whence, 0); 95d201fe46SDaniel Eischen FUNLOCKFILE(fp); 9635e1a550SAndrey A. Chernov if (ret == 0) 9735e1a550SAndrey A. Chernov errno = serrno; 98d201fe46SDaniel Eischen return (ret); 99d201fe46SDaniel Eischen } 100d201fe46SDaniel Eischen 10158f0484fSRodney W. Grimes /* 10258f0484fSRodney W. Grimes * Seek the given file to the given offset. 10358f0484fSRodney W. Grimes * `Whence' must be one of the three SEEK_* macros. 10458f0484fSRodney W. Grimes */ 10558f0484fSRodney W. Grimes int 10671b5a432SAndrey A. Chernov _fseeko(fp, offset, whence, ltest) 107d201fe46SDaniel Eischen FILE *fp; 1087307d07dSDmitrij Tejblum off_t offset; 10958f0484fSRodney W. Grimes int whence; 11071b5a432SAndrey A. Chernov int ltest; 11158f0484fSRodney W. Grimes { 11258f0484fSRodney W. Grimes register fpos_t (*seekfn) __P((void *, fpos_t, int)); 1132ff678f5SAndrey A. Chernov fpos_t target, curoff; 11458f0484fSRodney W. Grimes size_t n; 11558f0484fSRodney W. Grimes struct stat st; 11658f0484fSRodney W. Grimes int havepos; 11758f0484fSRodney W. Grimes 11858f0484fSRodney W. Grimes /* 11958f0484fSRodney W. Grimes * Have to be able to seek. 12058f0484fSRodney W. Grimes */ 12158f0484fSRodney W. Grimes if ((seekfn = fp->_seek) == NULL) { 12258f0484fSRodney W. Grimes errno = ESPIPE; /* historic practice */ 123e54bc118SAndrey A. Chernov return (-1); 12458f0484fSRodney W. Grimes } 12558f0484fSRodney W. Grimes 12658f0484fSRodney W. Grimes /* 12758f0484fSRodney W. Grimes * Change any SEEK_CUR to SEEK_SET, and check `whence' argument. 12858f0484fSRodney W. Grimes * After this, whence is either SEEK_SET or SEEK_END. 12958f0484fSRodney W. Grimes */ 13058f0484fSRodney W. Grimes switch (whence) { 13158f0484fSRodney W. Grimes 13258f0484fSRodney W. Grimes case SEEK_CUR: 13358f0484fSRodney W. Grimes /* 13458f0484fSRodney W. Grimes * In order to seek relative to the current stream offset, 1352ff678f5SAndrey A. Chernov * we have to first find the current stream offset via 13658f0484fSRodney W. Grimes * ftell (see ftell for details). 13758f0484fSRodney W. Grimes */ 13865efd812SAndrey A. Chernov if (_ftello(fp, &curoff)) 139e54bc118SAndrey A. Chernov return (-1); 14045892fd8SAndrey A. Chernov if (curoff < 0) { 14145892fd8SAndrey A. Chernov /* Unspecified position because of ungetc() at 0 */ 14245892fd8SAndrey A. Chernov errno = ESPIPE; 14345892fd8SAndrey A. Chernov return (-1); 14445892fd8SAndrey A. Chernov } 14545892fd8SAndrey A. Chernov if (offset > 0 && curoff > OFF_MAX - offset) { 146d9e3eff3SAndrey A. Chernov errno = EOVERFLOW; 147e54bc118SAndrey A. Chernov return (-1); 148d9e3eff3SAndrey A. Chernov } 14958f0484fSRodney W. Grimes offset += curoff; 150d9e3eff3SAndrey A. Chernov if (offset < 0) { 151d9e3eff3SAndrey A. Chernov errno = EINVAL; 152e54bc118SAndrey A. Chernov return (-1); 153d9e3eff3SAndrey A. Chernov } 154b98ba422SAndrey A. Chernov if (ltest && offset > LONG_MAX) { 155b98ba422SAndrey A. Chernov errno = EOVERFLOW; 156e54bc118SAndrey A. Chernov return (-1); 157b98ba422SAndrey A. Chernov } 15858f0484fSRodney W. Grimes whence = SEEK_SET; 15958f0484fSRodney W. Grimes havepos = 1; 16058f0484fSRodney W. Grimes break; 16158f0484fSRodney W. Grimes 16258f0484fSRodney W. Grimes case SEEK_SET: 163d9e3eff3SAndrey A. Chernov if (offset < 0) { 164d9e3eff3SAndrey A. Chernov errno = EINVAL; 165e54bc118SAndrey A. Chernov return (-1); 166d9e3eff3SAndrey A. Chernov } 16758f0484fSRodney W. Grimes case SEEK_END: 16858f0484fSRodney W. Grimes curoff = 0; /* XXX just to keep gcc quiet */ 16958f0484fSRodney W. Grimes havepos = 0; 17058f0484fSRodney W. Grimes break; 17158f0484fSRodney W. Grimes 17258f0484fSRodney W. Grimes default: 17358f0484fSRodney W. Grimes errno = EINVAL; 174e54bc118SAndrey A. Chernov return (-1); 17558f0484fSRodney W. Grimes } 17658f0484fSRodney W. Grimes 17758f0484fSRodney W. Grimes /* 17858f0484fSRodney W. Grimes * Can only optimise if: 17958f0484fSRodney W. Grimes * reading (and not reading-and-writing); 18058f0484fSRodney W. Grimes * not unbuffered; and 18158f0484fSRodney W. Grimes * this is a `regular' Unix file (and hence seekfn==__sseek). 18258f0484fSRodney W. Grimes * We must check __NBF first, because it is possible to have __NBF 18358f0484fSRodney W. Grimes * and __SOPT both set. 18458f0484fSRodney W. Grimes */ 18558f0484fSRodney W. Grimes if (fp->_bf._base == NULL) 18658f0484fSRodney W. Grimes __smakebuf(fp); 18758f0484fSRodney W. Grimes if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT)) 18858f0484fSRodney W. Grimes goto dumb; 18958f0484fSRodney W. Grimes if ((fp->_flags & __SOPT) == 0) { 19058f0484fSRodney W. Grimes if (seekfn != __sseek || 191d201fe46SDaniel Eischen fp->_file < 0 || _fstat(fp->_file, &st) || 19258f0484fSRodney W. Grimes (st.st_mode & S_IFMT) != S_IFREG) { 19358f0484fSRodney W. Grimes fp->_flags |= __SNPT; 19458f0484fSRodney W. Grimes goto dumb; 19558f0484fSRodney W. Grimes } 19658f0484fSRodney W. Grimes fp->_blksize = st.st_blksize; 19758f0484fSRodney W. Grimes fp->_flags |= __SOPT; 19858f0484fSRodney W. Grimes } 19958f0484fSRodney W. Grimes 20058f0484fSRodney W. Grimes /* 20158f0484fSRodney W. Grimes * We are reading; we can try to optimise. 20258f0484fSRodney W. Grimes * Figure out where we are going and where we are now. 20358f0484fSRodney W. Grimes */ 20458f0484fSRodney W. Grimes if (whence == SEEK_SET) 20558f0484fSRodney W. Grimes target = offset; 20658f0484fSRodney W. Grimes else { 207d201fe46SDaniel Eischen if (_fstat(fp->_file, &st)) 20858f0484fSRodney W. Grimes goto dumb; 20998aa5183SAndrey A. Chernov if (offset > 0 && st.st_size > OFF_MAX - offset) { 210d9e3eff3SAndrey A. Chernov errno = EOVERFLOW; 211e54bc118SAndrey A. Chernov return (-1); 212d9e3eff3SAndrey A. Chernov } 21358f0484fSRodney W. Grimes target = st.st_size + offset; 214d9e3eff3SAndrey A. Chernov if ((off_t)target < 0) { 215d9e3eff3SAndrey A. Chernov errno = EINVAL; 216e54bc118SAndrey A. Chernov return (-1); 217d9e3eff3SAndrey A. Chernov } 218b98ba422SAndrey A. Chernov if (ltest && (off_t)target > LONG_MAX) { 219b98ba422SAndrey A. Chernov errno = EOVERFLOW; 220e54bc118SAndrey A. Chernov return (-1); 221b98ba422SAndrey A. Chernov } 22258f0484fSRodney W. Grimes } 22358f0484fSRodney W. Grimes 22465efd812SAndrey A. Chernov if (!havepos && _ftello(fp, &curoff)) 22558f0484fSRodney W. Grimes goto dumb; 22658f0484fSRodney W. Grimes 22758f0484fSRodney W. Grimes /* 22865efd812SAndrey A. Chernov * (If the buffer was modified, we have to 22965efd812SAndrey A. Chernov * skip this; see fgetln.c.) 23065efd812SAndrey A. Chernov */ 23165efd812SAndrey A. Chernov if (fp->_flags & __SMOD) 23265efd812SAndrey A. Chernov goto abspos; 23365efd812SAndrey A. Chernov 23465efd812SAndrey A. Chernov /* 23558f0484fSRodney W. Grimes * Compute the number of bytes in the input buffer (pretending 23658f0484fSRodney W. Grimes * that any ungetc() input has been discarded). Adjust current 23758f0484fSRodney W. Grimes * offset backwards by this count so that it represents the 23858f0484fSRodney W. Grimes * file offset for the first byte in the current input buffer. 23958f0484fSRodney W. Grimes */ 24058f0484fSRodney W. Grimes if (HASUB(fp)) { 24158f0484fSRodney W. Grimes curoff += fp->_r; /* kill off ungetc */ 24291e1be28SWarner Losh n = fp->_extra->_up - fp->_bf._base; 24358f0484fSRodney W. Grimes curoff -= n; 24458f0484fSRodney W. Grimes n += fp->_ur; 24558f0484fSRodney W. Grimes } else { 24658f0484fSRodney W. Grimes n = fp->_p - fp->_bf._base; 24758f0484fSRodney W. Grimes curoff -= n; 24858f0484fSRodney W. Grimes n += fp->_r; 24958f0484fSRodney W. Grimes } 25058f0484fSRodney W. Grimes 25158f0484fSRodney W. Grimes /* 25258f0484fSRodney W. Grimes * If the target offset is within the current buffer, 25358f0484fSRodney W. Grimes * simply adjust the pointers, clear EOF, undo ungetc(), 25465efd812SAndrey A. Chernov * and return. 25558f0484fSRodney W. Grimes */ 256d911eb45SAndrey A. Chernov if (target >= curoff && target < curoff + n) { 257e54bc118SAndrey A. Chernov size_t o = target - curoff; 25858f0484fSRodney W. Grimes 25958f0484fSRodney W. Grimes fp->_p = fp->_bf._base + o; 26058f0484fSRodney W. Grimes fp->_r = n - o; 26158f0484fSRodney W. Grimes if (HASUB(fp)) 26258f0484fSRodney W. Grimes FREEUB(fp); 26358f0484fSRodney W. Grimes fp->_flags &= ~__SEOF; 26458f0484fSRodney W. Grimes return (0); 26558f0484fSRodney W. Grimes } 26658f0484fSRodney W. Grimes 267e54bc118SAndrey A. Chernov abspos: 26858f0484fSRodney W. Grimes /* 26958f0484fSRodney W. Grimes * The place we want to get to is not within the current buffer, 27058f0484fSRodney W. Grimes * but we can still be kind to the kernel copyout mechanism. 27158f0484fSRodney W. Grimes * By aligning the file offset to a block boundary, we can let 27258f0484fSRodney W. Grimes * the kernel use the VM hardware to map pages instead of 27358f0484fSRodney W. Grimes * copying bytes laboriously. Using a block boundary also 27458f0484fSRodney W. Grimes * ensures that we only read one block, rather than two. 27558f0484fSRodney W. Grimes */ 27658f0484fSRodney W. Grimes curoff = target & ~(fp->_blksize - 1); 277924888f9SAndrey A. Chernov if (_sseek(fp, curoff, SEEK_SET) == POS_ERR) 27858f0484fSRodney W. Grimes goto dumb; 27958f0484fSRodney W. Grimes fp->_r = 0; 280692a99c0SNate Williams fp->_p = fp->_bf._base; 28158f0484fSRodney W. Grimes if (HASUB(fp)) 28258f0484fSRodney W. Grimes FREEUB(fp); 28358f0484fSRodney W. Grimes n = target - curoff; 28458f0484fSRodney W. Grimes if (n) { 28558f0484fSRodney W. Grimes if (__srefill(fp) || fp->_r < n) 28658f0484fSRodney W. Grimes goto dumb; 28758f0484fSRodney W. Grimes fp->_p += n; 28858f0484fSRodney W. Grimes fp->_r -= n; 28958f0484fSRodney W. Grimes } 2902ff678f5SAndrey A. Chernov fp->_flags &= ~__SEOF; 29158f0484fSRodney W. Grimes return (0); 29258f0484fSRodney W. Grimes 29358f0484fSRodney W. Grimes /* 29458f0484fSRodney W. Grimes * We get here if we cannot optimise the seek ... just 29558f0484fSRodney W. Grimes * do it. Allow the seek function to change fp->_bf._base. 29658f0484fSRodney W. Grimes */ 29758f0484fSRodney W. Grimes dumb: 298924888f9SAndrey A. Chernov if (__sflush(fp) || _sseek(fp, (fpos_t)offset, whence) == POS_ERR) 299e54bc118SAndrey A. Chernov return (-1); 300e54bc118SAndrey A. Chernov if (ltest && fp->_offset > LONG_MAX) { 3016946977cSAndrey A. Chernov fp->_flags |= __SERR; 30271b5a432SAndrey A. Chernov errno = EOVERFLOW; 303e54bc118SAndrey A. Chernov return (-1); 30471b5a432SAndrey A. Chernov } 30558f0484fSRodney W. Grimes /* success: clear EOF indicator and discard ungetc() data */ 30658f0484fSRodney W. Grimes if (HASUB(fp)) 30758f0484fSRodney W. Grimes FREEUB(fp); 30858f0484fSRodney W. Grimes fp->_p = fp->_bf._base; 30958f0484fSRodney W. Grimes fp->_r = 0; 31058f0484fSRodney W. Grimes /* fp->_w = 0; */ /* unnecessary (I think...) */ 31158f0484fSRodney W. Grimes fp->_flags &= ~__SEOF; 31258f0484fSRodney W. Grimes return (0); 31358f0484fSRodney W. Grimes } 314924888f9SAndrey A. Chernov 315924888f9SAndrey A. Chernov 316924888f9SAndrey A. Chernov fpos_t 317924888f9SAndrey A. Chernov _sseek(fp, offset, whence) 318924888f9SAndrey A. Chernov FILE *fp; 319924888f9SAndrey A. Chernov fpos_t offset; 320924888f9SAndrey A. Chernov int whence; 321924888f9SAndrey A. Chernov { 322924888f9SAndrey A. Chernov fpos_t ret; 323924888f9SAndrey A. Chernov int serrno, errret; 324924888f9SAndrey A. Chernov 325924888f9SAndrey A. Chernov if (fp->_seek == NULL) { 326924888f9SAndrey A. Chernov errno = ESPIPE; 327924888f9SAndrey A. Chernov return (-1); 328924888f9SAndrey A. Chernov } 329924888f9SAndrey A. Chernov serrno = errno; 330924888f9SAndrey A. Chernov errno = 0; 331924888f9SAndrey A. Chernov ret = (*fp->_seek)(fp->_cookie, offset, whence); 332924888f9SAndrey A. Chernov errret = errno; 333924888f9SAndrey A. Chernov if (errno == 0) 334924888f9SAndrey A. Chernov errno = serrno; 335924888f9SAndrey A. Chernov /* 336924888f9SAndrey A. Chernov * Disallow negative seeks per POSIX. 337924888f9SAndrey A. Chernov * It is needed here to help upper level caller 338924888f9SAndrey A. Chernov * in the cases it can't detect. 339924888f9SAndrey A. Chernov */ 340924888f9SAndrey A. Chernov if (ret < 0) { 341924888f9SAndrey A. Chernov if (errret == 0) { 342924888f9SAndrey A. Chernov fp->_flags |= __SERR; 343924888f9SAndrey A. Chernov errno = EINVAL; 344924888f9SAndrey A. Chernov } 345924888f9SAndrey A. Chernov fp->_flags &= ~__SOFF; 346924888f9SAndrey A. Chernov ret = -1; 347924888f9SAndrey A. Chernov } else { 348924888f9SAndrey A. Chernov fp->_flags |= __SOFF; 349924888f9SAndrey A. Chernov fp->_offset = ret; 350924888f9SAndrey A. Chernov } 351924888f9SAndrey A. Chernov return (ret); 352924888f9SAndrey A. Chernov } 353