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