Home | History | Annotate | Line # | Download | only in stdio
fseek.c revision 1.18.10.1
      1  1.18.10.1   nathanw /*	$NetBSD: fseek.c,v 1.18.10.1 2002/02/06 23:18:45 nathanw Exp $	*/
      2        1.7       jtc 
      3        1.1       cgd /*-
      4        1.7       jtc  * Copyright (c) 1990, 1993
      5        1.7       jtc  *	The Regents of the University of California.  All rights reserved.
      6        1.1       cgd  *
      7        1.1       cgd  * This code is derived from software contributed to Berkeley by
      8        1.1       cgd  * Chris Torek.
      9        1.1       cgd  *
     10        1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11        1.1       cgd  * modification, are permitted provided that the following conditions
     12        1.1       cgd  * are met:
     13        1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14        1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15        1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17        1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18        1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     19        1.1       cgd  *    must display the following acknowledgement:
     20        1.1       cgd  *	This product includes software developed by the University of
     21        1.1       cgd  *	California, Berkeley and its contributors.
     22        1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     23        1.1       cgd  *    may be used to endorse or promote products derived from this software
     24        1.1       cgd  *    without specific prior written permission.
     25        1.1       cgd  *
     26        1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29        1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36        1.1       cgd  * SUCH DAMAGE.
     37        1.1       cgd  */
     38        1.1       cgd 
     39        1.9  christos #include <sys/cdefs.h>
     40        1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     41        1.7       jtc #if 0
     42        1.7       jtc static char sccsid[] = "@(#)fseek.c	8.3 (Berkeley) 1/2/94";
     43        1.9  christos #else
     44  1.18.10.1   nathanw __RCSID("$NetBSD: fseek.c,v 1.18.10.1 2002/02/06 23:18:45 nathanw Exp $");
     45        1.7       jtc #endif
     46        1.1       cgd #endif /* LIBC_SCCS and not lint */
     47        1.1       cgd 
     48        1.1       cgd #include <sys/types.h>
     49        1.1       cgd #include <sys/stat.h>
     50       1.17     lukem 
     51       1.17     lukem #include <assert.h>
     52       1.17     lukem #include <errno.h>
     53        1.1       cgd #include <fcntl.h>
     54        1.1       cgd #include <stdio.h>
     55        1.1       cgd #include <stdlib.h>
     56       1.12       jtc #include "reentrant.h"
     57  1.18.10.1   nathanw #include "local.h"
     58        1.1       cgd 
     59        1.1       cgd #define	POS_ERR	(-(fpos_t)1)
     60        1.1       cgd 
     61        1.1       cgd /*
     62        1.1       cgd  * Seek the given file to the given offset.
     63        1.1       cgd  * `Whence' must be one of the three SEEK_* macros.
     64        1.1       cgd  */
     65        1.7       jtc int
     66        1.1       cgd fseek(fp, offset, whence)
     67       1.14     perry 	FILE *fp;
     68        1.1       cgd 	long offset;
     69        1.1       cgd 	int whence;
     70        1.1       cgd {
     71       1.14     perry 	fpos_t (*seekfn) __P((void *, fpos_t, int));
     72        1.1       cgd 	fpos_t target, curoff;
     73        1.1       cgd 	size_t n;
     74        1.1       cgd 	struct stat st;
     75        1.1       cgd 	int havepos;
     76       1.17     lukem 
     77       1.17     lukem 	_DIAGASSERT(fp != NULL);
     78       1.13   mycroft 
     79       1.13   mycroft #ifdef __GNUC__
     80       1.13   mycroft 	/* This outrageous construct just to shut up a GCC warning. */
     81       1.13   mycroft 	(void) &curoff;
     82       1.13   mycroft #endif
     83        1.1       cgd 
     84        1.1       cgd 	/* make sure stdio is set up */
     85        1.1       cgd 	if (!__sdidinit)
     86        1.1       cgd 		__sinit();
     87        1.1       cgd 
     88       1.12       jtc 	FLOCKFILE(fp);
     89       1.12       jtc 
     90        1.1       cgd 	/*
     91        1.1       cgd 	 * Have to be able to seek.
     92        1.1       cgd 	 */
     93        1.1       cgd 	if ((seekfn = fp->_seek) == NULL) {
     94        1.1       cgd 		errno = ESPIPE;			/* historic practice */
     95       1.15    kleink 		FUNLOCKFILE(fp);
     96       1.11    kleink 		return (-1);
     97        1.1       cgd 	}
     98        1.1       cgd 
     99        1.1       cgd 	/*
    100        1.1       cgd 	 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
    101        1.1       cgd 	 * After this, whence is either SEEK_SET or SEEK_END.
    102        1.1       cgd 	 */
    103        1.1       cgd 	switch (whence) {
    104        1.1       cgd 
    105        1.1       cgd 	case SEEK_CUR:
    106        1.1       cgd 		/*
    107        1.1       cgd 		 * In order to seek relative to the current stream offset,
    108        1.1       cgd 		 * we have to first find the current stream offset a la
    109        1.1       cgd 		 * ftell (see ftell for details).
    110        1.1       cgd 		 */
    111        1.8       jtc 		__sflush(fp);	/* may adjust seek offset on append stream */
    112        1.1       cgd 		if (fp->_flags & __SOFF)
    113        1.1       cgd 			curoff = fp->_offset;
    114        1.1       cgd 		else {
    115        1.1       cgd 			curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
    116       1.12       jtc 			if (curoff == -1L) {
    117       1.12       jtc 				FUNLOCKFILE(fp);
    118       1.11    kleink 				return (-1);
    119       1.12       jtc 			}
    120        1.1       cgd 		}
    121        1.1       cgd 		if (fp->_flags & __SRD) {
    122        1.1       cgd 			curoff -= fp->_r;
    123        1.1       cgd 			if (HASUB(fp))
    124        1.1       cgd 				curoff -= fp->_ur;
    125        1.1       cgd 		} else if (fp->_flags & __SWR && fp->_p != NULL)
    126        1.1       cgd 			curoff += fp->_p - fp->_bf._base;
    127        1.1       cgd 
    128       1.16  christos 		offset += (long)curoff;
    129        1.1       cgd 		whence = SEEK_SET;
    130        1.1       cgd 		havepos = 1;
    131        1.1       cgd 		break;
    132        1.1       cgd 
    133        1.1       cgd 	case SEEK_SET:
    134        1.1       cgd 	case SEEK_END:
    135        1.1       cgd 		curoff = 0;		/* XXX just to keep gcc quiet */
    136        1.1       cgd 		havepos = 0;
    137        1.1       cgd 		break;
    138        1.1       cgd 
    139        1.1       cgd 	default:
    140        1.1       cgd 		errno = EINVAL;
    141       1.12       jtc 		FUNLOCKFILE(fp);
    142       1.11    kleink 		return (-1);
    143        1.1       cgd 	}
    144        1.1       cgd 
    145        1.1       cgd 	/*
    146        1.1       cgd 	 * Can only optimise if:
    147        1.1       cgd 	 *	reading (and not reading-and-writing);
    148        1.1       cgd 	 *	not unbuffered; and
    149        1.1       cgd 	 *	this is a `regular' Unix file (and hence seekfn==__sseek).
    150        1.1       cgd 	 * We must check __NBF first, because it is possible to have __NBF
    151        1.1       cgd 	 * and __SOPT both set.
    152        1.1       cgd 	 */
    153        1.1       cgd 	if (fp->_bf._base == NULL)
    154        1.1       cgd 		__smakebuf(fp);
    155        1.1       cgd 	if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
    156        1.1       cgd 		goto dumb;
    157        1.1       cgd 	if ((fp->_flags & __SOPT) == 0) {
    158        1.1       cgd 		if (seekfn != __sseek ||
    159        1.1       cgd 		    fp->_file < 0 || fstat(fp->_file, &st) ||
    160       1.10   mycroft 		    !S_ISREG(st.st_mode)) {
    161        1.1       cgd 			fp->_flags |= __SNPT;
    162        1.1       cgd 			goto dumb;
    163        1.1       cgd 		}
    164        1.1       cgd 		fp->_blksize = st.st_blksize;
    165        1.1       cgd 		fp->_flags |= __SOPT;
    166        1.1       cgd 	}
    167        1.1       cgd 
    168        1.1       cgd 	/*
    169        1.1       cgd 	 * We are reading; we can try to optimise.
    170        1.1       cgd 	 * Figure out where we are going and where we are now.
    171        1.1       cgd 	 */
    172        1.1       cgd 	if (whence == SEEK_SET)
    173        1.1       cgd 		target = offset;
    174        1.1       cgd 	else {
    175        1.1       cgd 		if (fstat(fp->_file, &st))
    176        1.1       cgd 			goto dumb;
    177        1.1       cgd 		target = st.st_size + offset;
    178        1.1       cgd 	}
    179        1.1       cgd 
    180        1.1       cgd 	if (!havepos) {
    181        1.1       cgd 		if (fp->_flags & __SOFF)
    182        1.1       cgd 			curoff = fp->_offset;
    183        1.1       cgd 		else {
    184        1.7       jtc 			curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
    185        1.1       cgd 			if (curoff == POS_ERR)
    186        1.1       cgd 				goto dumb;
    187        1.1       cgd 		}
    188        1.1       cgd 		curoff -= fp->_r;
    189        1.1       cgd 		if (HASUB(fp))
    190        1.1       cgd 			curoff -= fp->_ur;
    191        1.1       cgd 	}
    192        1.1       cgd 
    193        1.1       cgd 	/*
    194        1.1       cgd 	 * Compute the number of bytes in the input buffer (pretending
    195        1.1       cgd 	 * that any ungetc() input has been discarded).  Adjust current
    196        1.1       cgd 	 * offset backwards by this count so that it represents the
    197        1.1       cgd 	 * file offset for the first byte in the current input buffer.
    198        1.1       cgd 	 */
    199        1.1       cgd 	if (HASUB(fp)) {
    200        1.7       jtc 		curoff += fp->_r;	/* kill off ungetc */
    201        1.1       cgd 		n = fp->_up - fp->_bf._base;
    202        1.1       cgd 		curoff -= n;
    203        1.1       cgd 		n += fp->_ur;
    204        1.1       cgd 	} else {
    205        1.1       cgd 		n = fp->_p - fp->_bf._base;
    206        1.1       cgd 		curoff -= n;
    207        1.1       cgd 		n += fp->_r;
    208        1.1       cgd 	}
    209        1.1       cgd 
    210        1.1       cgd 	/*
    211        1.1       cgd 	 * If the target offset is within the current buffer,
    212        1.1       cgd 	 * simply adjust the pointers, clear EOF, undo ungetc(),
    213        1.1       cgd 	 * and return.  (If the buffer was modified, we have to
    214        1.6       cgd 	 * skip this; see fgetln.c.)
    215        1.1       cgd 	 */
    216        1.1       cgd 	if ((fp->_flags & __SMOD) == 0 &&
    217        1.1       cgd 	    target >= curoff && target < curoff + n) {
    218       1.16  christos 		int o = (int)(target - curoff);
    219        1.1       cgd 
    220        1.1       cgd 		fp->_p = fp->_bf._base + o;
    221        1.1       cgd 		fp->_r = n - o;
    222        1.1       cgd 		if (HASUB(fp))
    223        1.1       cgd 			FREEUB(fp);
    224        1.1       cgd 		fp->_flags &= ~__SEOF;
    225       1.12       jtc 		FUNLOCKFILE(fp);
    226        1.1       cgd 		return (0);
    227        1.1       cgd 	}
    228        1.1       cgd 
    229        1.1       cgd 	/*
    230        1.1       cgd 	 * The place we want to get to is not within the current buffer,
    231        1.1       cgd 	 * but we can still be kind to the kernel copyout mechanism.
    232        1.1       cgd 	 * By aligning the file offset to a block boundary, we can let
    233        1.1       cgd 	 * the kernel use the VM hardware to map pages instead of
    234        1.1       cgd 	 * copying bytes laboriously.  Using a block boundary also
    235        1.1       cgd 	 * ensures that we only read one block, rather than two.
    236        1.1       cgd 	 */
    237        1.1       cgd 	curoff = target & ~(fp->_blksize - 1);
    238        1.1       cgd 	if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR)
    239        1.1       cgd 		goto dumb;
    240        1.1       cgd 	fp->_r = 0;
    241        1.2       cgd  	fp->_p = fp->_bf._base;
    242        1.1       cgd 	if (HASUB(fp))
    243        1.1       cgd 		FREEUB(fp);
    244        1.1       cgd 	fp->_flags &= ~__SEOF;
    245       1.16  christos 	n = (int)(target - curoff);
    246        1.1       cgd 	if (n) {
    247        1.1       cgd 		if (__srefill(fp) || fp->_r < n)
    248        1.1       cgd 			goto dumb;
    249        1.1       cgd 		fp->_p += n;
    250        1.1       cgd 		fp->_r -= n;
    251        1.1       cgd 	}
    252       1.12       jtc 	FUNLOCKFILE(fp);
    253        1.1       cgd 	return (0);
    254        1.1       cgd 
    255        1.1       cgd 	/*
    256        1.1       cgd 	 * We get here if we cannot optimise the seek ... just
    257        1.1       cgd 	 * do it.  Allow the seek function to change fp->_bf._base.
    258        1.1       cgd 	 */
    259        1.1       cgd dumb:
    260        1.1       cgd 	if (__sflush(fp) ||
    261        1.7       jtc 	    (*seekfn)(fp->_cookie, (fpos_t)offset, whence) == POS_ERR) {
    262       1.12       jtc 		FUNLOCKFILE(fp);
    263       1.11    kleink 		return (-1);
    264        1.1       cgd 	}
    265        1.1       cgd 	/* success: clear EOF indicator and discard ungetc() data */
    266        1.1       cgd 	if (HASUB(fp))
    267        1.1       cgd 		FREEUB(fp);
    268        1.1       cgd 	fp->_p = fp->_bf._base;
    269        1.1       cgd 	fp->_r = 0;
    270        1.1       cgd 	/* fp->_w = 0; */	/* unnecessary (I think...) */
    271        1.1       cgd 	fp->_flags &= ~__SEOF;
    272       1.12       jtc 	FUNLOCKFILE(fp);
    273        1.1       cgd 	return (0);
    274        1.1       cgd }
    275