Re-implement in terms of fseek0()
This commit is contained in:
parent
917b8e232e
commit
7f90294800
@ -1,11 +1,11 @@
|
|||||||
/* $NetBSD: fseek.c,v 1.20 2003/08/07 16:43:25 agc Exp $ */
|
/* $NetBSD: fseek.c,v 1.21 2005/03/04 16:25:20 dsl Exp $ */
|
||||||
|
|
||||||
/*-
|
/*-
|
||||||
* Copyright (c) 1990, 1993
|
* Copyright (c) 2005 The NetBSD Foundation, Inc.
|
||||||
* The Regents of the University of California. All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* This code is derived from software contributed to Berkeley by
|
* This code is derived from software contributed to The NetBSD Foundation
|
||||||
* Chris Torek.
|
* by David Laight.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@ -15,30 +15,26 @@
|
|||||||
* 2. Redistributions in binary form must reproduce the above copyright
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
* documentation and/or other materials provided with the distribution.
|
* documentation and/or other materials provided with the distribution.
|
||||||
* 3. Neither the name of the University nor the names of its contributors
|
* 3. Neither the name of The NetBSD Foundation nor the names of its
|
||||||
* may be used to endorse or promote products derived from this software
|
* contributors may be used to endorse or promote products derived
|
||||||
* without specific prior written permission.
|
* from this software without specific prior written permission.
|
||||||
*
|
*
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
||||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
||||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
* SUCH DAMAGE.
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
#if defined(LIBC_SCCS) && !defined(lint)
|
#if defined(LIBC_SCCS) && !defined(lint)
|
||||||
#if 0
|
__RCSID("$NetBSD: fseek.c,v 1.21 2005/03/04 16:25:20 dsl Exp $");
|
||||||
static char sccsid[] = "@(#)fseek.c 8.3 (Berkeley) 1/2/94";
|
|
||||||
#else
|
|
||||||
__RCSID("$NetBSD: fseek.c,v 1.20 2003/08/07 16:43:25 agc Exp $");
|
|
||||||
#endif
|
|
||||||
#endif /* LIBC_SCCS and not lint */
|
#endif /* LIBC_SCCS and not lint */
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@ -52,219 +48,18 @@ __RCSID("$NetBSD: fseek.c,v 1.20 2003/08/07 16:43:25 agc Exp $");
|
|||||||
#include "reentrant.h"
|
#include "reentrant.h"
|
||||||
#include "local.h"
|
#include "local.h"
|
||||||
|
|
||||||
#define POS_ERR (-(fpos_t)1)
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Seek the given file to the given offset.
|
* Seek the given file to the given offset.
|
||||||
* `Whence' must be one of the three SEEK_* macros.
|
* Zero extend the offset if SEEK_SET to allow access to 4GB files
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
fseek(fp, offset, whence)
|
fseek(FILE *fp, long l_offset, int whence)
|
||||||
FILE *fp;
|
|
||||||
long offset;
|
|
||||||
int whence;
|
|
||||||
{
|
{
|
||||||
fpos_t (*seekfn) __P((void *, fpos_t, int));
|
off_t offset;
|
||||||
fpos_t target, curoff;
|
|
||||||
size_t n;
|
|
||||||
struct stat st;
|
|
||||||
int havepos;
|
|
||||||
|
|
||||||
_DIAGASSERT(fp != NULL);
|
|
||||||
|
|
||||||
#ifdef __GNUC__
|
|
||||||
/* This outrageous construct just to shut up a GCC warning. */
|
|
||||||
(void) &curoff;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* make sure stdio is set up */
|
|
||||||
if (!__sdidinit)
|
|
||||||
__sinit();
|
|
||||||
|
|
||||||
FLOCKFILE(fp);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Have to be able to seek.
|
|
||||||
*/
|
|
||||||
if ((seekfn = fp->_seek) == NULL) {
|
|
||||||
errno = ESPIPE; /* historic practice */
|
|
||||||
FUNLOCKFILE(fp);
|
|
||||||
return (-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
|
|
||||||
* After this, whence is either SEEK_SET or SEEK_END.
|
|
||||||
*/
|
|
||||||
switch (whence) {
|
|
||||||
|
|
||||||
case SEEK_CUR:
|
|
||||||
/*
|
|
||||||
* In order to seek relative to the current stream offset,
|
|
||||||
* we have to first find the current stream offset a la
|
|
||||||
* ftell (see ftell for details).
|
|
||||||
*/
|
|
||||||
__sflush(fp); /* may adjust seek offset on append stream */
|
|
||||||
if (fp->_flags & __SOFF)
|
|
||||||
curoff = fp->_offset;
|
|
||||||
else {
|
|
||||||
curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
|
|
||||||
if (curoff == -1L) {
|
|
||||||
FUNLOCKFILE(fp);
|
|
||||||
return (-1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (fp->_flags & __SRD) {
|
|
||||||
curoff -= fp->_r;
|
|
||||||
if (HASUB(fp))
|
|
||||||
curoff -= fp->_ur;
|
|
||||||
} else if (fp->_flags & __SWR && fp->_p != NULL)
|
|
||||||
curoff += fp->_p - fp->_bf._base;
|
|
||||||
|
|
||||||
offset += (long)curoff;
|
|
||||||
whence = SEEK_SET;
|
|
||||||
havepos = 1;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SEEK_SET:
|
|
||||||
case SEEK_END:
|
|
||||||
curoff = 0; /* XXX just to keep gcc quiet */
|
|
||||||
havepos = 0;
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
errno = EINVAL;
|
|
||||||
FUNLOCKFILE(fp);
|
|
||||||
return (-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Can only optimise if:
|
|
||||||
* reading (and not reading-and-writing);
|
|
||||||
* not unbuffered; and
|
|
||||||
* this is a `regular' Unix file (and hence seekfn==__sseek).
|
|
||||||
* We must check __NBF first, because it is possible to have __NBF
|
|
||||||
* and __SOPT both set.
|
|
||||||
*/
|
|
||||||
if (fp->_bf._base == NULL)
|
|
||||||
__smakebuf(fp);
|
|
||||||
if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
|
|
||||||
goto dumb;
|
|
||||||
if ((fp->_flags & __SOPT) == 0) {
|
|
||||||
if (seekfn != __sseek ||
|
|
||||||
fp->_file < 0 || fstat(fp->_file, &st) ||
|
|
||||||
!S_ISREG(st.st_mode)) {
|
|
||||||
fp->_flags |= __SNPT;
|
|
||||||
goto dumb;
|
|
||||||
}
|
|
||||||
fp->_blksize = st.st_blksize;
|
|
||||||
fp->_flags |= __SOPT;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* We are reading; we can try to optimise.
|
|
||||||
* Figure out where we are going and where we are now.
|
|
||||||
*/
|
|
||||||
if (whence == SEEK_SET)
|
if (whence == SEEK_SET)
|
||||||
target = offset;
|
offset = (unsigned long)l_offset;
|
||||||
else {
|
else
|
||||||
if (fstat(fp->_file, &st))
|
offset = l_offset;
|
||||||
goto dumb;
|
return fseeko(fp, offset, whence);
|
||||||
target = st.st_size + offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!havepos) {
|
|
||||||
if (fp->_flags & __SOFF)
|
|
||||||
curoff = fp->_offset;
|
|
||||||
else {
|
|
||||||
curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
|
|
||||||
if (curoff == POS_ERR)
|
|
||||||
goto dumb;
|
|
||||||
}
|
|
||||||
curoff -= fp->_r;
|
|
||||||
if (HASUB(fp))
|
|
||||||
curoff -= fp->_ur;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Compute the number of bytes in the input buffer (pretending
|
|
||||||
* that any ungetc() input has been discarded). Adjust current
|
|
||||||
* offset backwards by this count so that it represents the
|
|
||||||
* file offset for the first byte in the current input buffer.
|
|
||||||
*/
|
|
||||||
if (HASUB(fp)) {
|
|
||||||
curoff += fp->_r; /* kill off ungetc */
|
|
||||||
n = fp->_up - fp->_bf._base;
|
|
||||||
curoff -= n;
|
|
||||||
n += fp->_ur;
|
|
||||||
} else {
|
|
||||||
n = fp->_p - fp->_bf._base;
|
|
||||||
curoff -= n;
|
|
||||||
n += fp->_r;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* If the target offset is within the current buffer,
|
|
||||||
* simply adjust the pointers, clear EOF, undo ungetc(),
|
|
||||||
* and return. (If the buffer was modified, we have to
|
|
||||||
* skip this; see fgetln.c.)
|
|
||||||
*/
|
|
||||||
if ((fp->_flags & __SMOD) == 0 &&
|
|
||||||
target >= curoff && target < curoff + n) {
|
|
||||||
int o = (int)(target - curoff);
|
|
||||||
|
|
||||||
fp->_p = fp->_bf._base + o;
|
|
||||||
fp->_r = n - o;
|
|
||||||
if (HASUB(fp))
|
|
||||||
FREEUB(fp);
|
|
||||||
fp->_flags &= ~__SEOF;
|
|
||||||
FUNLOCKFILE(fp);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* The place we want to get to is not within the current buffer,
|
|
||||||
* but we can still be kind to the kernel copyout mechanism.
|
|
||||||
* By aligning the file offset to a block boundary, we can let
|
|
||||||
* the kernel use the VM hardware to map pages instead of
|
|
||||||
* copying bytes laboriously. Using a block boundary also
|
|
||||||
* ensures that we only read one block, rather than two.
|
|
||||||
*/
|
|
||||||
curoff = target & ~(fp->_blksize - 1);
|
|
||||||
if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR)
|
|
||||||
goto dumb;
|
|
||||||
fp->_r = 0;
|
|
||||||
fp->_p = fp->_bf._base;
|
|
||||||
if (HASUB(fp))
|
|
||||||
FREEUB(fp);
|
|
||||||
fp->_flags &= ~__SEOF;
|
|
||||||
n = (int)(target - curoff);
|
|
||||||
if (n) {
|
|
||||||
if (__srefill(fp) || fp->_r < n)
|
|
||||||
goto dumb;
|
|
||||||
fp->_p += n;
|
|
||||||
fp->_r -= n;
|
|
||||||
}
|
|
||||||
FUNLOCKFILE(fp);
|
|
||||||
return (0);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* We get here if we cannot optimise the seek ... just
|
|
||||||
* do it. Allow the seek function to change fp->_bf._base.
|
|
||||||
*/
|
|
||||||
dumb:
|
|
||||||
if (__sflush(fp) ||
|
|
||||||
(*seekfn)(fp->_cookie, (fpos_t)offset, whence) == POS_ERR) {
|
|
||||||
FUNLOCKFILE(fp);
|
|
||||||
return (-1);
|
|
||||||
}
|
|
||||||
/* success: clear EOF indicator and discard ungetc() data */
|
|
||||||
if (HASUB(fp))
|
|
||||||
FREEUB(fp);
|
|
||||||
fp->_p = fp->_bf._base;
|
|
||||||
fp->_r = 0;
|
|
||||||
/* fp->_w = 0; */ /* unnecessary (I think...) */
|
|
||||||
fp->_flags &= ~__SEOF;
|
|
||||||
FUNLOCKFILE(fp);
|
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user