2002-07-18 07:25:44 +08:00
|
|
|
/*
|
2007-05-02 07:03:36 +08:00
|
|
|
* Copyright (c) 1990 The Regents of the University of California.
|
2002-07-18 07:25:44 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that the above copyright notice and this paragraph are
|
|
|
|
* duplicated in all such forms and that any documentation,
|
|
|
|
* advertising materials, and other materials related to such
|
|
|
|
* distribution and use acknowledge that the software was developed
|
|
|
|
* by the University of California, Berkeley. The name of the
|
|
|
|
* University may not be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
FUNCTION
|
|
|
|
<<fseeko64>>---set file position for large file
|
|
|
|
|
|
|
|
INDEX
|
|
|
|
fseeko64
|
2003-08-23 02:52:25 +08:00
|
|
|
INDEX
|
|
|
|
_fseeko64_r
|
2002-07-18 07:25:44 +08:00
|
|
|
|
2017-11-30 16:06:47 +08:00
|
|
|
SYNOPSIS
|
2002-07-18 07:25:44 +08:00
|
|
|
#include <stdio.h>
|
2015-11-03 05:34:49 +08:00
|
|
|
int fseeko64(FILE *<[fp]>, _off64_t <[offset]>, int <[whence]>);
|
2007-05-02 07:03:36 +08:00
|
|
|
int _fseeko64_r (struct _reent *<[ptr]>, FILE *<[fp]>,
|
2015-11-03 05:34:49 +08:00
|
|
|
_off64_t <[offset]>, int <[whence]>);
|
2003-08-23 02:52:25 +08:00
|
|
|
|
2002-07-18 07:25:44 +08:00
|
|
|
DESCRIPTION
|
|
|
|
Objects of type <<FILE>> can have a ``position'' that records how much
|
|
|
|
of the file your program has already read. Many of the <<stdio>> functions
|
|
|
|
depend on this position, and many change it as a side effect.
|
|
|
|
|
|
|
|
You can use <<fseeko64>> to set the position for the file identified by
|
2007-05-02 07:03:36 +08:00
|
|
|
<[fp]> that was opened via <<fopen64>>. The value of <[offset]> determines
|
|
|
|
the new position, in one of three ways selected by the value of <[whence]>
|
2002-07-18 07:25:44 +08:00
|
|
|
(defined as macros in `<<stdio.h>>'):
|
|
|
|
|
|
|
|
<<SEEK_SET>>---<[offset]> is the absolute file position (an offset
|
|
|
|
from the beginning of the file) desired. <[offset]> must be positive.
|
|
|
|
|
|
|
|
<<SEEK_CUR>>---<[offset]> is relative to the current file position.
|
|
|
|
<[offset]> can meaningfully be either positive or negative.
|
|
|
|
|
|
|
|
<<SEEK_END>>---<[offset]> is relative to the current end of file.
|
|
|
|
<[offset]> can meaningfully be either positive (to increase the size
|
|
|
|
of the file) or negative.
|
|
|
|
|
|
|
|
See <<ftello64>> to determine the current file position.
|
|
|
|
|
|
|
|
RETURNS
|
|
|
|
<<fseeko64>> returns <<0>> when successful. On failure, the
|
|
|
|
result is <<EOF>>. The reason for failure is indicated in <<errno>>:
|
|
|
|
either <<ESPIPE>> (the stream identified by <[fp]> doesn't support
|
2007-05-02 07:03:36 +08:00
|
|
|
repositioning or wasn't opened via <<fopen64>>) or <<EINVAL>>
|
2002-07-18 07:25:44 +08:00
|
|
|
(invalid file position).
|
|
|
|
|
|
|
|
PORTABILITY
|
|
|
|
<<fseeko64>> is a glibc extension.
|
|
|
|
|
|
|
|
Supporting OS subroutines required: <<close>>, <<fstat64>>, <<isatty>>,
|
|
|
|
<<lseek64>>, <<read>>, <<sbrk>>, <<write>>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
2002-08-27 02:56:09 +08:00
|
|
|
#include <sys/types.h>
|
2002-07-18 07:25:44 +08:00
|
|
|
#include <sys/stat.h>
|
2007-05-02 07:03:36 +08:00
|
|
|
#include "local.h"
|
2002-07-18 07:25:44 +08:00
|
|
|
|
|
|
|
#define POS_ERR (-(_fpos64_t)1)
|
|
|
|
|
2002-08-27 02:56:09 +08:00
|
|
|
#ifdef __LARGE64_FILES
|
|
|
|
|
2002-07-18 07:25:44 +08:00
|
|
|
/*
|
|
|
|
* Seek the given file to the given offset.
|
|
|
|
* `Whence' must be one of the three SEEK_* macros.
|
|
|
|
*/
|
|
|
|
|
2002-08-27 02:56:09 +08:00
|
|
|
_off64_t
|
2017-12-04 11:43:30 +08:00
|
|
|
_fseeko64_r (struct _reent *ptr,
|
2017-12-04 09:31:41 +08:00
|
|
|
register FILE *fp,
|
|
|
|
_off64_t offset,
|
2003-08-23 02:52:25 +08:00
|
|
|
int whence)
|
2002-07-18 07:25:44 +08:00
|
|
|
{
|
2017-12-04 12:52:13 +08:00
|
|
|
_fpos64_t (*seekfn) (struct _reent *, void *, _fpos64_t, int);
|
2002-07-18 07:25:44 +08:00
|
|
|
_fpos64_t target, curoff;
|
|
|
|
size_t n;
|
2003-08-23 02:52:25 +08:00
|
|
|
|
2003-08-27 02:09:43 +08:00
|
|
|
struct stat64 st;
|
2002-07-18 07:25:44 +08:00
|
|
|
int havepos;
|
|
|
|
|
2007-07-14 04:37:53 +08:00
|
|
|
/* Only do 64-bit seek on large file. */
|
|
|
|
if (!(fp->_flags & __SL64))
|
|
|
|
{
|
|
|
|
if ((_off_t) offset != offset)
|
|
|
|
{
|
|
|
|
ptr->_errno = EOVERFLOW;
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
return (_off64_t) _fseeko_r (ptr, fp, offset, whence);
|
|
|
|
}
|
|
|
|
|
2002-07-18 07:25:44 +08:00
|
|
|
/* Make sure stdio is set up. */
|
|
|
|
|
2006-09-27 05:22:19 +08:00
|
|
|
CHECK_INIT (ptr, fp);
|
2005-02-08 09:33:19 +08:00
|
|
|
|
2012-05-30 16:58:42 +08:00
|
|
|
_newlib_flockfile_start (fp);
|
2003-08-23 02:52:25 +08:00
|
|
|
|
|
|
|
curoff = fp->_offset;
|
2002-07-18 07:25:44 +08:00
|
|
|
|
|
|
|
/* If we've been doing some writing, and we're in append mode
|
|
|
|
then we don't really know where the filepos is. */
|
|
|
|
|
|
|
|
if (fp->_flags & __SAPP && fp->_flags & __SWR)
|
|
|
|
{
|
|
|
|
/* So flush the buffer and seek to the end. */
|
2007-07-14 04:37:53 +08:00
|
|
|
_fflush_r (ptr, fp);
|
2002-07-18 07:25:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Have to be able to seek. */
|
|
|
|
|
2007-07-14 04:37:53 +08:00
|
|
|
if ((seekfn = fp->_seek64) == NULL)
|
2002-07-18 07:25:44 +08:00
|
|
|
{
|
|
|
|
ptr->_errno = ESPIPE; /* ??? */
|
2012-05-30 16:58:42 +08:00
|
|
|
_newlib_flockfile_exit(fp);
|
2002-07-18 07:25:44 +08:00
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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:
|
2020-01-30 01:47:33 +08:00
|
|
|
/*
|
|
|
|
* 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).
|
|
|
|
*/
|
|
|
|
_fflush_r (ptr, fp); /* may adjust seek offset on append stream */
|
|
|
|
if (fp->_flags & __SOFF)
|
|
|
|
curoff = fp->_offset;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
curoff = seekfn (ptr, fp->_cookie, (_fpos64_t) 0, SEEK_CUR);
|
|
|
|
if (curoff == -1L)
|
|
|
|
{
|
|
|
|
_newlib_flockfile_exit(fp);
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
2002-07-18 07:25:44 +08:00
|
|
|
|
|
|
|
offset += curoff;
|
|
|
|
whence = SEEK_SET;
|
|
|
|
havepos = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_SET:
|
|
|
|
case SEEK_END:
|
|
|
|
havepos = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ptr->_errno = EINVAL;
|
2012-05-30 16:58:42 +08:00
|
|
|
_newlib_flockfile_exit(fp);
|
2002-07-18 07:25:44 +08:00
|
|
|
return (EOF);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
2007-03-17 04:05:10 +08:00
|
|
|
__smakebuf_r (ptr, fp);
|
2013-04-11 02:47:46 +08:00
|
|
|
|
|
|
|
#if _FSEEK_OPTIMIZATION
|
2002-07-18 07:25:44 +08:00
|
|
|
if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
|
|
|
|
goto dumb;
|
|
|
|
if ((fp->_flags & __SOPT) == 0)
|
|
|
|
{
|
|
|
|
if (seekfn != __sseek64
|
|
|
|
|| fp->_file < 0
|
2003-08-27 02:09:43 +08:00
|
|
|
|| _fstat64_r (ptr, fp->_file, &st)
|
2002-07-18 07:25:44 +08:00
|
|
|
|| (st.st_mode & S_IFMT) != S_IFREG)
|
|
|
|
{
|
|
|
|
fp->_flags |= __SNPT;
|
|
|
|
goto dumb;
|
|
|
|
}
|
|
|
|
#ifdef HAVE_BLKSIZE
|
|
|
|
fp->_blksize = st.st_blksize;
|
|
|
|
#else
|
|
|
|
fp->_blksize = 1024;
|
|
|
|
#endif
|
|
|
|
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)
|
|
|
|
target = offset;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_fstat64_r (ptr, fp->_file, &st))
|
|
|
|
goto dumb;
|
|
|
|
target = st.st_size + offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!havepos)
|
|
|
|
{
|
|
|
|
if (fp->_flags & __SOFF)
|
|
|
|
curoff = fp->_offset;
|
|
|
|
else
|
|
|
|
{
|
Implement funopen, fopencookie.
* libc/include/sys/reent.h (struct __sFILE, struct __sFILE64):
Switch to reentrant callbacks.
* libc/include/stdio.h (funopen): Fix declaration.
(fopencookie): Declare.
* libc/stdio/local.h (__sread, __swrite, __sseek, __sclose)
(__sseek64, __swrite64): Fix prototypes.
[__SCLE]: Pull in setmode declaration.
* libc/stdio/stdio.c (__sread, __swrite, __sseek, __sclose): Fix
reentrancy.
* libc/stdio64/stdio64.c (__sseek64_r, __swrite64_r): Delete.
(__sseek64, __swrite64): Fix reentrancy.
* libc/stdio/fseek.c (_fseek_r): Account for overflow, and fix
reentrancy.
* libc/stdio/ftell.c (_ftell_r): Likewise.
* libc/stdio/flags.c (__sflags): Don't lose __SAPP on "a+".
* libc/stdio/fclose.c (_fclose_r): Fix reentrancy.
* libc/stdio/freopen.c (_freopen_r): Likewise.
* libc/stdio/fvwrite.c (__sfvwrite_r): Likewise.
* libc/stdio/refill.c (__srefill_r): Likewise.
* libc/stdio/siscanf.c (eofread): Likewise.
* libc/stdio/sscanf.c (eofread): Likewise.
* libc/stdio/vsiscanf.c (eofread1): Likewise.
* libc/stdio/vsscanf.c (eofread1): Likewise.
* libc/stdio64/freopen64.c (_freopen64_r): Likewise.
* libc/stdio64/fseeko64.c (_fseeko64_r): Likewise.
* libc/stdio64/ftello64.c (_ftello64_r): Likewise.
* libc/stdio/fflush.c (fflush): Improve reentrancy, although more
could be fixed.
* libc/stdio/fopencookie.c (_fopencookie_r, fopencookie): New file.
* libc/stdio/funopen.c (_funopen_r, funopen): New file.
* libc/stdio/Makefile.am (ELIX_4_SOURCES, CHEWOUT_FILES): Build
new files.
* libc/stdio/Makefile.in: Regenerate.
2007-06-05 02:10:17 +08:00
|
|
|
curoff = seekfn (ptr, fp->_cookie, (_fpos64_t)0, SEEK_CUR);
|
2002-07-18 07:25:44 +08:00
|
|
|
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(),
|
2008-11-25 01:15:43 +08:00
|
|
|
* and return.
|
2002-07-18 07:25:44 +08:00
|
|
|
*/
|
|
|
|
|
2008-11-25 01:15:43 +08:00
|
|
|
if (target >= curoff && target < curoff + n)
|
2002-07-18 07:25:44 +08:00
|
|
|
{
|
|
|
|
register int o = target - curoff;
|
|
|
|
|
|
|
|
fp->_p = fp->_bf._base + o;
|
|
|
|
fp->_r = n - o;
|
|
|
|
if (HASUB (fp))
|
2007-03-17 04:05:10 +08:00
|
|
|
FREEUB (ptr, fp);
|
2002-07-18 07:25:44 +08:00
|
|
|
fp->_flags &= ~__SEOF;
|
2012-05-30 16:58:42 +08:00
|
|
|
_newlib_flockfile_exit(fp);
|
2002-07-18 07:25:44 +08:00
|
|
|
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 & ~((_fpos64_t)(fp->_blksize - 1));
|
Implement funopen, fopencookie.
* libc/include/sys/reent.h (struct __sFILE, struct __sFILE64):
Switch to reentrant callbacks.
* libc/include/stdio.h (funopen): Fix declaration.
(fopencookie): Declare.
* libc/stdio/local.h (__sread, __swrite, __sseek, __sclose)
(__sseek64, __swrite64): Fix prototypes.
[__SCLE]: Pull in setmode declaration.
* libc/stdio/stdio.c (__sread, __swrite, __sseek, __sclose): Fix
reentrancy.
* libc/stdio64/stdio64.c (__sseek64_r, __swrite64_r): Delete.
(__sseek64, __swrite64): Fix reentrancy.
* libc/stdio/fseek.c (_fseek_r): Account for overflow, and fix
reentrancy.
* libc/stdio/ftell.c (_ftell_r): Likewise.
* libc/stdio/flags.c (__sflags): Don't lose __SAPP on "a+".
* libc/stdio/fclose.c (_fclose_r): Fix reentrancy.
* libc/stdio/freopen.c (_freopen_r): Likewise.
* libc/stdio/fvwrite.c (__sfvwrite_r): Likewise.
* libc/stdio/refill.c (__srefill_r): Likewise.
* libc/stdio/siscanf.c (eofread): Likewise.
* libc/stdio/sscanf.c (eofread): Likewise.
* libc/stdio/vsiscanf.c (eofread1): Likewise.
* libc/stdio/vsscanf.c (eofread1): Likewise.
* libc/stdio64/freopen64.c (_freopen64_r): Likewise.
* libc/stdio64/fseeko64.c (_fseeko64_r): Likewise.
* libc/stdio64/ftello64.c (_ftello64_r): Likewise.
* libc/stdio/fflush.c (fflush): Improve reentrancy, although more
could be fixed.
* libc/stdio/fopencookie.c (_fopencookie_r, fopencookie): New file.
* libc/stdio/funopen.c (_funopen_r, funopen): New file.
* libc/stdio/Makefile.am (ELIX_4_SOURCES, CHEWOUT_FILES): Build
new files.
* libc/stdio/Makefile.in: Regenerate.
2007-06-05 02:10:17 +08:00
|
|
|
if (seekfn (ptr, fp->_cookie, curoff, SEEK_SET) == POS_ERR)
|
2002-07-18 07:25:44 +08:00
|
|
|
goto dumb;
|
|
|
|
fp->_r = 0;
|
|
|
|
fp->_p = fp->_bf._base;
|
|
|
|
if (HASUB (fp))
|
2007-03-17 04:05:10 +08:00
|
|
|
FREEUB (ptr, fp);
|
2002-07-18 07:25:44 +08:00
|
|
|
fp->_flags &= ~__SEOF;
|
|
|
|
n = target - curoff;
|
|
|
|
if (n)
|
|
|
|
{
|
2006-06-15 04:49:11 +08:00
|
|
|
if (__srefill_r (ptr, fp) || fp->_r < n)
|
2002-07-18 07:25:44 +08:00
|
|
|
goto dumb;
|
|
|
|
fp->_p += n;
|
|
|
|
fp->_r -= n;
|
|
|
|
}
|
2012-05-30 16:58:42 +08:00
|
|
|
_newlib_flockfile_end(fp);
|
2002-07-18 07:25:44 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We get here if we cannot optimise the seek ... just
|
|
|
|
* do it. Allow the seek function to change fp->_bf._base.
|
|
|
|
*/
|
2013-04-11 02:47:46 +08:00
|
|
|
#endif
|
2002-07-18 07:25:44 +08:00
|
|
|
|
|
|
|
dumb:
|
2007-07-14 04:37:53 +08:00
|
|
|
if (_fflush_r (ptr, fp)
|
|
|
|
|| seekfn (ptr, fp->_cookie, offset, whence) == POS_ERR)
|
2002-07-18 07:25:44 +08:00
|
|
|
{
|
|
|
|
_funlockfile(fp);
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
/* success: clear EOF indicator and discard ungetc() data */
|
|
|
|
if (HASUB (fp))
|
2007-03-17 04:05:10 +08:00
|
|
|
FREEUB (ptr, fp);
|
2002-07-18 07:25:44 +08:00
|
|
|
fp->_p = fp->_bf._base;
|
|
|
|
fp->_r = 0;
|
|
|
|
/* fp->_w = 0; *//* unnecessary (I think...) */
|
|
|
|
fp->_flags &= ~__SEOF;
|
|
|
|
_funlockfile(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-08-23 02:52:25 +08:00
|
|
|
#ifndef _REENT_ONLY
|
|
|
|
|
|
|
|
_off64_t
|
2017-12-04 11:43:30 +08:00
|
|
|
fseeko64 (register FILE *fp,
|
2017-12-04 09:31:41 +08:00
|
|
|
_off64_t offset,
|
2003-08-23 02:52:25 +08:00
|
|
|
int whence)
|
|
|
|
{
|
|
|
|
return _fseeko64_r (_REENT, fp, offset, whence);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !_REENT_ONLY */
|
|
|
|
|
2002-07-18 07:25:44 +08:00
|
|
|
#endif /* __LARGE64_FILES */
|