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