/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1990, 19934* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Chris Torek.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include "namespace.h"35#include <sys/types.h>36#include <sys/stat.h>37#include <errno.h>38#include <fcntl.h>39#include <limits.h>40#include <stdio.h>41#include <stdlib.h>42#include "un-namespace.h"43#include "local.h"44#include "libc_private.h"4546#define POS_ERR (-(fpos_t)1)4748int49fseek(FILE *fp, long offset, int whence)50{51int ret;52int serrno = errno;5354/* make sure stdio is set up */55if (!__sdidinit)56__sinit();5758FLOCKFILE_CANCELSAFE(fp);59ret = _fseeko(fp, (off_t)offset, whence, 1);60FUNLOCKFILE_CANCELSAFE();61if (ret == 0)62errno = serrno;63return (ret);64}6566int67fseeko(FILE *fp, off_t offset, int whence)68{69int ret;70int serrno = errno;7172/* make sure stdio is set up */73if (!__sdidinit)74__sinit();7576FLOCKFILE_CANCELSAFE(fp);77ret = _fseeko(fp, offset, whence, 0);78FUNLOCKFILE_CANCELSAFE();79if (ret == 0)80errno = serrno;81return (ret);82}8384/*85* Seek the given file to the given offset.86* `Whence' must be one of the three SEEK_* macros.87*/88int89_fseeko(FILE *fp, off_t offset, int whence, int ltest)90{91fpos_t (*seekfn)(void *, fpos_t, int);92fpos_t target, curoff, ret;93size_t n;94struct stat st;95int havepos;9697/*98* Have to be able to seek.99*/100if ((seekfn = fp->_seek) == NULL) {101errno = ESPIPE; /* historic practice */102return (-1);103}104105/*106* Change any SEEK_CUR to SEEK_SET, and check `whence' argument.107* After this, whence is either SEEK_SET or SEEK_END.108*/109switch (whence) {110111case SEEK_CUR:112/*113* In order to seek relative to the current stream offset,114* we have to first find the current stream offset via115* ftell (see ftell for details).116*/117if (_ftello(fp, &curoff))118return (-1);119if (curoff < 0) {120/* Unspecified position because of ungetc() at 0 */121errno = ESPIPE;122return (-1);123}124if (offset > 0 && curoff > OFF_MAX - offset) {125errno = EOVERFLOW;126return (-1);127}128offset += curoff;129if (offset < 0) {130errno = EINVAL;131return (-1);132}133if (ltest && offset > LONG_MAX) {134errno = EOVERFLOW;135return (-1);136}137whence = SEEK_SET;138havepos = 1;139break;140141case SEEK_SET:142if (offset < 0) {143errno = EINVAL;144return (-1);145}146case SEEK_END:147curoff = 0; /* XXX just to keep gcc quiet */148havepos = 0;149break;150151default:152errno = EINVAL;153return (-1);154}155156/*157* Can only optimise if:158* reading (and not reading-and-writing);159* not unbuffered; and160* this is a `regular' Unix file (and hence seekfn==__sseek).161* We must check __NBF first, because it is possible to have __NBF162* and __SOPT both set.163*/164if (fp->_bf._base == NULL)165__smakebuf(fp);166if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))167goto dumb;168if ((fp->_flags & __SOPT) == 0) {169if (seekfn != __sseek ||170fp->_file < 0 || _fstat(fp->_file, &st) ||171(st.st_mode & S_IFMT) != S_IFREG) {172fp->_flags |= __SNPT;173goto dumb;174}175fp->_blksize = st.st_blksize;176fp->_flags |= __SOPT;177}178179/*180* We are reading; we can try to optimise.181* Figure out where we are going and where we are now.182*/183if (whence == SEEK_SET)184target = offset;185else {186if (_fstat(fp->_file, &st))187goto dumb;188if (offset > 0 && st.st_size > OFF_MAX - offset) {189errno = EOVERFLOW;190return (-1);191}192target = st.st_size + offset;193if ((off_t)target < 0) {194errno = EINVAL;195return (-1);196}197if (ltest && (off_t)target > LONG_MAX) {198errno = EOVERFLOW;199return (-1);200}201}202203if (!havepos && _ftello(fp, &curoff))204goto dumb;205206/*207* (If the buffer was modified, we have to208* skip this; see fgetln.c.)209*/210if (fp->_flags & __SMOD)211goto abspos;212213/*214* Compute the number of bytes in the input buffer (pretending215* that any ungetc() input has been discarded). Adjust current216* offset backwards by this count so that it represents the217* file offset for the first byte in the current input buffer.218*/219if (HASUB(fp)) {220curoff += fp->_r; /* kill off ungetc */221n = fp->_up - fp->_bf._base;222curoff -= n;223n += fp->_ur;224} else {225n = fp->_p - fp->_bf._base;226curoff -= n;227n += fp->_r;228}229230/*231* If the target offset is within the current buffer,232* simply adjust the pointers, clear EOF, undo ungetc(),233* and return.234*/235if (target >= curoff && target < curoff + n) {236size_t o = target - curoff;237238fp->_p = fp->_bf._base + o;239fp->_r = n - o;240if (HASUB(fp))241FREEUB(fp);242fp->_flags &= ~__SEOF;243memset(&fp->_mbstate, 0, sizeof(mbstate_t));244return (0);245}246247abspos:248/*249* The place we want to get to is not within the current buffer,250* but we can still be kind to the kernel copyout mechanism.251* By aligning the file offset to a block boundary, we can let252* the kernel use the VM hardware to map pages instead of253* copying bytes laboriously. Using a block boundary also254* ensures that we only read one block, rather than two.255*/256curoff = target & ~(fp->_blksize - 1);257if (_sseek(fp, curoff, SEEK_SET) == POS_ERR)258goto dumb;259fp->_r = 0;260fp->_p = fp->_bf._base;261if (HASUB(fp))262FREEUB(fp);263n = target - curoff;264if (n) {265if (__srefill(fp) || fp->_r < n)266goto dumb;267fp->_p += n;268fp->_r -= n;269}270fp->_flags &= ~__SEOF;271memset(&fp->_mbstate, 0, sizeof(mbstate_t));272return (0);273274/*275* We get here if we cannot optimise the seek ... just276* do it. Allow the seek function to change fp->_bf._base.277*/278dumb:279if (__sflush(fp) ||280(ret = _sseek(fp, (fpos_t)offset, whence)) == POS_ERR)281return (-1);282if (ltest && ret > LONG_MAX) {283fp->_flags |= __SERR;284errno = EOVERFLOW;285return (-1);286}287/* success: clear EOF indicator and discard ungetc() data */288if (HASUB(fp))289FREEUB(fp);290fp->_p = fp->_bf._base;291fp->_r = 0;292/* fp->_w = 0; */ /* unnecessary (I think...) */293fp->_flags &= ~__SEOF;294memset(&fp->_mbstate, 0, sizeof(mbstate_t));295return (0);296}297298299