Home | History | Annotate | Line # | Download | only in stdio
fseek.c revision 1.7
      1 /*	$NetBSD: fseek.c,v 1.7 1995/02/02 02:09:39 jtc 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 #if defined(LIBC_SCCS) && !defined(lint)
     40 #if 0
     41 static char sccsid[] = "@(#)fseek.c	8.3 (Berkeley) 1/2/94";
     42 #endif
     43 static char rcsid[] = "$NetBSD: fseek.c,v 1.7 1995/02/02 02:09:39 jtc Exp $";
     44 #endif /* LIBC_SCCS and not lint */
     45 
     46 #include <sys/types.h>
     47 #include <sys/stat.h>
     48 #include <fcntl.h>
     49 #include <stdio.h>
     50 #include <stdlib.h>
     51 #include <errno.h>
     52 #include "local.h"
     53 
     54 #define	POS_ERR	(-(fpos_t)1)
     55 
     56 /*
     57  * Seek the given file to the given offset.
     58  * `Whence' must be one of the three SEEK_* macros.
     59  */
     60 int
     61 fseek(fp, offset, whence)
     62 	register FILE *fp;
     63 	long offset;
     64 	int whence;
     65 {
     66 	register fpos_t (*seekfn) __P((void *, fpos_t, int));
     67 	fpos_t target, curoff;
     68 	size_t n;
     69 	struct stat st;
     70 	int havepos;
     71 
     72 	/* make sure stdio is set up */
     73 	if (!__sdidinit)
     74 		__sinit();
     75 
     76 	/*
     77 	 * Have to be able to seek.
     78 	 */
     79 	if ((seekfn = fp->_seek) == NULL) {
     80 		errno = ESPIPE;			/* historic practice */
     81 		return (EOF);
     82 	}
     83 
     84 	/*
     85 	 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
     86 	 * After this, whence is either SEEK_SET or SEEK_END.
     87 	 */
     88 	switch (whence) {
     89 
     90 	case SEEK_CUR:
     91 		/*
     92 		 * In order to seek relative to the current stream offset,
     93 		 * we have to first find the current stream offset a la
     94 		 * ftell (see ftell for details).
     95 		 */
     96 		if (fp->_flags & __SOFF)
     97 			curoff = fp->_offset;
     98 		else {
     99 			curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
    100 			if (curoff == -1L)
    101 				return (EOF);
    102 		}
    103 		if (fp->_flags & __SRD) {
    104 			curoff -= fp->_r;
    105 			if (HASUB(fp))
    106 				curoff -= fp->_ur;
    107 		} else if (fp->_flags & __SWR && fp->_p != NULL)
    108 			curoff += fp->_p - fp->_bf._base;
    109 
    110 		offset += curoff;
    111 		whence = SEEK_SET;
    112 		havepos = 1;
    113 		break;
    114 
    115 	case SEEK_SET:
    116 	case SEEK_END:
    117 		curoff = 0;		/* XXX just to keep gcc quiet */
    118 		havepos = 0;
    119 		break;
    120 
    121 	default:
    122 		errno = EINVAL;
    123 		return (EOF);
    124 	}
    125 
    126 	/*
    127 	 * Can only optimise if:
    128 	 *	reading (and not reading-and-writing);
    129 	 *	not unbuffered; and
    130 	 *	this is a `regular' Unix file (and hence seekfn==__sseek).
    131 	 * We must check __NBF first, because it is possible to have __NBF
    132 	 * and __SOPT both set.
    133 	 */
    134 	if (fp->_bf._base == NULL)
    135 		__smakebuf(fp);
    136 	if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
    137 		goto dumb;
    138 	if ((fp->_flags & __SOPT) == 0) {
    139 		if (seekfn != __sseek ||
    140 		    fp->_file < 0 || fstat(fp->_file, &st) ||
    141 		    (st.st_mode & S_IFMT) != S_IFREG) {
    142 			fp->_flags |= __SNPT;
    143 			goto dumb;
    144 		}
    145 		fp->_blksize = st.st_blksize;
    146 		fp->_flags |= __SOPT;
    147 	}
    148 
    149 	/*
    150 	 * We are reading; we can try to optimise.
    151 	 * Figure out where we are going and where we are now.
    152 	 */
    153 	if (whence == SEEK_SET)
    154 		target = offset;
    155 	else {
    156 		if (fstat(fp->_file, &st))
    157 			goto dumb;
    158 		target = st.st_size + offset;
    159 	}
    160 
    161 	if (!havepos) {
    162 		if (fp->_flags & __SOFF)
    163 			curoff = fp->_offset;
    164 		else {
    165 			curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
    166 			if (curoff == POS_ERR)
    167 				goto dumb;
    168 		}
    169 		curoff -= fp->_r;
    170 		if (HASUB(fp))
    171 			curoff -= fp->_ur;
    172 	}
    173 
    174 	/*
    175 	 * Compute the number of bytes in the input buffer (pretending
    176 	 * that any ungetc() input has been discarded).  Adjust current
    177 	 * offset backwards by this count so that it represents the
    178 	 * file offset for the first byte in the current input buffer.
    179 	 */
    180 	if (HASUB(fp)) {
    181 		curoff += fp->_r;	/* kill off ungetc */
    182 		n = fp->_up - fp->_bf._base;
    183 		curoff -= n;
    184 		n += fp->_ur;
    185 	} else {
    186 		n = fp->_p - fp->_bf._base;
    187 		curoff -= n;
    188 		n += fp->_r;
    189 	}
    190 
    191 	/*
    192 	 * If the target offset is within the current buffer,
    193 	 * simply adjust the pointers, clear EOF, undo ungetc(),
    194 	 * and return.  (If the buffer was modified, we have to
    195 	 * skip this; see fgetln.c.)
    196 	 */
    197 	if ((fp->_flags & __SMOD) == 0 &&
    198 	    target >= curoff && target < curoff + n) {
    199 		register int o = target - curoff;
    200 
    201 		fp->_p = fp->_bf._base + o;
    202 		fp->_r = n - o;
    203 		if (HASUB(fp))
    204 			FREEUB(fp);
    205 		fp->_flags &= ~__SEOF;
    206 		return (0);
    207 	}
    208 
    209 	/*
    210 	 * The place we want to get to is not within the current buffer,
    211 	 * but we can still be kind to the kernel copyout mechanism.
    212 	 * By aligning the file offset to a block boundary, we can let
    213 	 * the kernel use the VM hardware to map pages instead of
    214 	 * copying bytes laboriously.  Using a block boundary also
    215 	 * ensures that we only read one block, rather than two.
    216 	 */
    217 	curoff = target & ~(fp->_blksize - 1);
    218 	if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR)
    219 		goto dumb;
    220 	fp->_r = 0;
    221  	fp->_p = fp->_bf._base;
    222 	if (HASUB(fp))
    223 		FREEUB(fp);
    224 	fp->_flags &= ~__SEOF;
    225 	n = target - curoff;
    226 	if (n) {
    227 		if (__srefill(fp) || fp->_r < n)
    228 			goto dumb;
    229 		fp->_p += n;
    230 		fp->_r -= n;
    231 	}
    232 	return (0);
    233 
    234 	/*
    235 	 * We get here if we cannot optimise the seek ... just
    236 	 * do it.  Allow the seek function to change fp->_bf._base.
    237 	 */
    238 dumb:
    239 	if (__sflush(fp) ||
    240 	    (*seekfn)(fp->_cookie, (fpos_t)offset, whence) == POS_ERR) {
    241 		return (EOF);
    242 	}
    243 	/* success: clear EOF indicator and discard ungetc() data */
    244 	if (HASUB(fp))
    245 		FREEUB(fp);
    246 	fp->_p = fp->_bf._base;
    247 	fp->_r = 0;
    248 	/* fp->_w = 0; */	/* unnecessary (I think...) */
    249 	fp->_flags &= ~__SEOF;
    250 	return (0);
    251 }
    252