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