fseek.c revision 82591
155682Smarkm/*- 278527Sassar * Copyright (c) 1990, 1993 355682Smarkm * The Regents of the University of California. All rights reserved. 455682Smarkm * 555682Smarkm * This code is derived from software contributed to Berkeley by 655682Smarkm * Chris Torek. 755682Smarkm * 855682Smarkm * Redistribution and use in source and binary forms, with or without 955682Smarkm * modification, are permitted provided that the following conditions 1055682Smarkm * are met: 1155682Smarkm * 1. Redistributions of source code must retain the above copyright 1255682Smarkm * notice, this list of conditions and the following disclaimer. 1355682Smarkm * 2. Redistributions in binary form must reproduce the above copyright 1455682Smarkm * notice, this list of conditions and the following disclaimer in the 1555682Smarkm * documentation and/or other materials provided with the distribution. 1655682Smarkm * 3. All advertising materials mentioning features or use of this software 1755682Smarkm * must display the following acknowledgement: 1855682Smarkm * This product includes software developed by the University of 1955682Smarkm * California, Berkeley and its contributors. 2055682Smarkm * 4. Neither the name of the University nor the names of its contributors 2155682Smarkm * may be used to endorse or promote products derived from this software 2255682Smarkm * without specific prior written permission. 2355682Smarkm * 2455682Smarkm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2555682Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2655682Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2755682Smarkm * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2855682Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2955682Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3055682Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3155682Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3255682Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3355682Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3455682Smarkm * SUCH DAMAGE. 3555682Smarkm */ 36103423Snectar 3755682Smarkm#if defined(LIBC_SCCS) && !defined(lint) 3855682Smarkm#if 0 3955682Smarkmstatic char sccsid[] = "@(#)fseek.c 8.3 (Berkeley) 1/2/94"; 4055682Smarkm#endif 4155682Smarkmstatic const char rcsid[] = 4255682Smarkm "$FreeBSD: head/lib/libc/stdio/fseek.c 82591 2001-08-30 20:49:47Z ache $"; 4355682Smarkm#endif /* LIBC_SCCS and not lint */ 4455682Smarkm 4555682Smarkm#include "namespace.h" 4655682Smarkm#include <sys/types.h> 4755682Smarkm#include <sys/stat.h> 4855682Smarkm#include <errno.h> 4955682Smarkm#include <fcntl.h> 5055682Smarkm#include <limits.h> 5155682Smarkm#include <stdio.h> 5255682Smarkm#include <stdlib.h> 5355682Smarkm#include "un-namespace.h" 5455682Smarkm#include "local.h" 5555682Smarkm#include "libc_private.h" 5655682Smarkm 5755682Smarkm#define POS_ERR (-(fpos_t)1) 5855682Smarkm 5955682Smarkmint 6055682Smarkmfseek(fp, offset, whence) 6155682Smarkm register FILE *fp; 6255682Smarkm long offset; 6355682Smarkm int whence; 6455682Smarkm{ 6555682Smarkm int ret; 6655682Smarkm 6755682Smarkm /* make sure stdio is set up */ 6855682Smarkm if (!__sdidinit) 6955682Smarkm __sinit(); 7055682Smarkm 7155682Smarkm FLOCKFILE(fp); 7255682Smarkm ret = _fseeko(fp, (off_t)offset, whence, 1); 7355682Smarkm FUNLOCKFILE(fp); 7455682Smarkm return (ret); 7555682Smarkm} 7655682Smarkm 7755682Smarkmint 7855682Smarkmfseeko(fp, offset, whence) 7955682Smarkm FILE *fp; 8055682Smarkm off_t offset; 8155682Smarkm int whence; 8255682Smarkm{ 8355682Smarkm int ret; 8455682Smarkm 8555682Smarkm /* make sure stdio is set up */ 8655682Smarkm if (!__sdidinit) 8755682Smarkm __sinit(); 8855682Smarkm 89103423Snectar FLOCKFILE(fp); 9055682Smarkm ret = _fseeko(fp, offset, whence, 0); 9155682Smarkm FUNLOCKFILE(fp); 9255682Smarkm return (ret); 9355682Smarkm} 9478527Sassar 9578527Sassar/* 9678527Sassar * Seek the given file to the given offset. 9778527Sassar * `Whence' must be one of the three SEEK_* macros. 9878527Sassar */ 9955682Smarkmint 10055682Smarkm_fseeko(fp, offset, whence, ltest) 10155682Smarkm FILE *fp; 10255682Smarkm off_t offset; 10378527Sassar int whence; 10478527Sassar int ltest; 10578527Sassar{ 10678527Sassar register fpos_t (*seekfn) __P((void *, fpos_t, int)); 10778527Sassar fpos_t target, curoff; 10855682Smarkm size_t n; 10955682Smarkm struct stat st; 11078527Sassar int havepos; 11178527Sassar 11278527Sassar /* 11378527Sassar * Have to be able to seek. 11478527Sassar */ 11578527Sassar if ((seekfn = fp->_seek) == NULL) { 11655682Smarkm errno = ESPIPE; /* historic practice */ 11778527Sassar return (-1); 11855682Smarkm } 11978527Sassar 12078527Sassar /* 12155682Smarkm * Change any SEEK_CUR to SEEK_SET, and check `whence' argument. 12278527Sassar * After this, whence is either SEEK_SET or SEEK_END. 12355682Smarkm */ 12455682Smarkm switch (whence) { 12555682Smarkm 12655682Smarkm case SEEK_CUR: 12755682Smarkm /* 12855682Smarkm * In order to seek relative to the current stream offset, 129103423Snectar * we have to first find the current stream offset a la 13055682Smarkm * ftell (see ftell for details). 13155682Smarkm */ 13255682Smarkm if (fp->_flags & __SOFF) 13355682Smarkm curoff = fp->_offset; 134103423Snectar else { 135103423Snectar curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR); 136103423Snectar if (curoff == -1) 13755682Smarkm return (-1); 138103423Snectar } 13955682Smarkm if (fp->_flags & __SRD) { 14055682Smarkm curoff -= fp->_r; 14155682Smarkm if (curoff < 0) { 14255682Smarkm if (HASUB(fp)) { 14355682Smarkm fp->_p -= curoff; 14455682Smarkm fp->_r += curoff; 14555682Smarkm curoff = 0; 14655682Smarkm } else { 14755682Smarkm errno = EBADF; 14855682Smarkm return (-1); 14955682Smarkm } 150103423Snectar } 151103423Snectar if (HASUB(fp)) { 152103423Snectar curoff -= fp->_ur; 15355682Smarkm if (curoff < 0) { 154103423Snectar if (-curoff <= fp->_r) { 15555682Smarkm fp->_p -= curoff; 15655682Smarkm fp->_r += curoff; 15755682Smarkm curoff = 0; 158103423Snectar } else { 159103423Snectar errno = EBADF; 16055682Smarkm return (-1); 16155682Smarkm } 16255682Smarkm } 16355682Smarkm } 16455682Smarkm } else if ((fp->_flags & __SWR) && fp->_p != NULL) { 16555682Smarkm n = fp->_p - fp->_bf._base; 16655682Smarkm if (curoff > OFF_MAX - n) { 16755682Smarkm errno = EOVERFLOW; 16855682Smarkm return (-1); 16955682Smarkm } 17055682Smarkm curoff += n; 17155682Smarkm } 17255682Smarkm if (offset > 0 && curoff > OFF_MAX - offset) { 17355682Smarkm errno = EOVERFLOW; 17455682Smarkm return (-1); 17555682Smarkm } 17655682Smarkm offset += curoff; 17755682Smarkm if (offset < 0) { 17855682Smarkm errno = EINVAL; 17955682Smarkm return (-1); 18055682Smarkm } 18155682Smarkm if (ltest && offset > LONG_MAX) { 18255682Smarkm errno = EOVERFLOW; 18355682Smarkm return (-1); 18455682Smarkm } 18555682Smarkm whence = SEEK_SET; 18655682Smarkm havepos = 1; 18755682Smarkm break; 18855682Smarkm 18955682Smarkm case SEEK_SET: 19055682Smarkm if (offset < 0) { 19155682Smarkm errno = EINVAL; 19255682Smarkm return (-1); 19355682Smarkm } 19455682Smarkm case SEEK_END: 19578527Sassar curoff = 0; /* XXX just to keep gcc quiet */ 19655682Smarkm havepos = 0; 19755682Smarkm break; 19855682Smarkm 19978527Sassar default: 20055682Smarkm errno = EINVAL; 20155682Smarkm return (-1); 20255682Smarkm } 20355682Smarkm 20478527Sassar /* 20555682Smarkm * Can only optimise if: 20655682Smarkm * reading (and not reading-and-writing); 20778527Sassar * not unbuffered; and 20878527Sassar * this is a `regular' Unix file (and hence seekfn==__sseek). 20978527Sassar * We must check __NBF first, because it is possible to have __NBF 21078527Sassar * and __SOPT both set. 21155682Smarkm */ 21255682Smarkm if (fp->_bf._base == NULL) 21355682Smarkm __smakebuf(fp); 21455682Smarkm if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT)) 21555682Smarkm goto dumb; 21655682Smarkm if ((fp->_flags & __SOPT) == 0) { 21755682Smarkm if (seekfn != __sseek || 21855682Smarkm fp->_file < 0 || _fstat(fp->_file, &st) || 21955682Smarkm (st.st_mode & S_IFMT) != S_IFREG) { 22055682Smarkm fp->_flags |= __SNPT; 22155682Smarkm goto dumb; 22255682Smarkm } 22355682Smarkm fp->_blksize = st.st_blksize; 22455682Smarkm fp->_flags |= __SOPT; 22555682Smarkm } 22655682Smarkm 22755682Smarkm /* 22855682Smarkm * We are reading; we can try to optimise. 22955682Smarkm * Figure out where we are going and where we are now. 23055682Smarkm */ 23155682Smarkm if (whence == SEEK_SET) 23255682Smarkm target = offset; 23355682Smarkm else { 234 if (_fstat(fp->_file, &st)) 235 goto dumb; 236 if (offset > 0 && st.st_size > OFF_MAX - offset) { 237 errno = EOVERFLOW; 238 return (-1); 239 } 240 target = st.st_size + offset; 241 if ((off_t)target < 0) { 242 errno = EINVAL; 243 return (-1); 244 } 245 if (ltest && (off_t)target > LONG_MAX) { 246 errno = EOVERFLOW; 247 return (-1); 248 } 249 } 250 251 if (!havepos) { 252 if (fp->_flags & __SOFF) 253 curoff = fp->_offset; 254 else { 255 curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR); 256 if (curoff == POS_ERR) 257 goto dumb; 258 } 259 curoff -= fp->_r; 260 if (curoff < 0) { 261 if (HASUB(fp)) { 262 fp->_p -= curoff; 263 fp->_r += curoff; 264 curoff = 0; 265 } else 266 goto dumb; 267 } 268 if (HASUB(fp)) { 269 curoff -= fp->_ur; 270 if (curoff < 0) { 271 if (-curoff <= fp->_r) { 272 fp->_p -= curoff; 273 fp->_r += curoff; 274 curoff = 0; 275 } else 276 goto dumb; 277 } 278 } 279 } 280 281 /* 282 * Compute the number of bytes in the input buffer (pretending 283 * that any ungetc() input has been discarded). Adjust current 284 * offset backwards by this count so that it represents the 285 * file offset for the first byte in the current input buffer. 286 */ 287 if (HASUB(fp)) { 288 if (curoff > OFF_MAX - fp->_r) 289 goto abspos; 290 curoff += fp->_r; /* kill off ungetc */ 291 n = fp->_extra->_up - fp->_bf._base; 292 curoff -= n; 293 n += fp->_ur; 294 } else { 295 n = fp->_p - fp->_bf._base; 296 curoff -= n; 297 n += fp->_r; 298 } 299 /* curoff can be negative at this point. */ 300 301 /* 302 * If the target offset is within the current buffer, 303 * simply adjust the pointers, clear EOF, undo ungetc(), 304 * and return. (If the buffer was modified, we have to 305 * skip this; see fgetln.c.) 306 */ 307 if ((fp->_flags & __SMOD) == 0 && 308 target >= curoff && 309 (curoff <= 0 || curoff <= OFF_MAX - n) && 310 target < curoff + n) { 311 size_t o = target - curoff; 312 313 fp->_p = fp->_bf._base + o; 314 fp->_r = n - o; 315 if (HASUB(fp)) 316 FREEUB(fp); 317 fp->_flags &= ~__SEOF; 318 return (0); 319 } 320 321abspos: 322 /* 323 * The place we want to get to is not within the current buffer, 324 * but we can still be kind to the kernel copyout mechanism. 325 * By aligning the file offset to a block boundary, we can let 326 * the kernel use the VM hardware to map pages instead of 327 * copying bytes laboriously. Using a block boundary also 328 * ensures that we only read one block, rather than two. 329 */ 330 curoff = target & ~(fp->_blksize - 1); 331 if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR) 332 goto dumb; 333 fp->_r = 0; 334 fp->_p = fp->_bf._base; 335 if (HASUB(fp)) 336 FREEUB(fp); 337 fp->_flags &= ~__SEOF; 338 n = target - curoff; 339 if (n) { 340 if (__srefill(fp) || fp->_r < n) 341 goto dumb; 342 fp->_p += n; 343 fp->_r -= n; 344 } 345 return (0); 346 347 /* 348 * We get here if we cannot optimise the seek ... just 349 * do it. Allow the seek function to change fp->_bf._base. 350 */ 351dumb: 352 if (__sflush(fp) || 353 (*seekfn)(fp->_cookie, (fpos_t)offset, whence) == POS_ERR) 354 return (-1); 355 if (ltest && fp->_offset > LONG_MAX) { 356 errno = EOVERFLOW; 357 return (-1); 358 } 359 /* success: clear EOF indicator and discard ungetc() data */ 360 if (HASUB(fp)) 361 FREEUB(fp); 362 fp->_p = fp->_bf._base; 363 fp->_r = 0; 364 /* fp->_w = 0; */ /* unnecessary (I think...) */ 365 fp->_flags &= ~__SEOF; 366 return (0); 367} 368