2002-07-18 07:25:44 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1990 The Regents of the University of California.
|
|
|
|
* 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,
|
2020-01-29 02:54:53 +08:00
|
|
|
* and/or other materials related to such
|
2002-07-18 07:25:44 +08:00
|
|
|
* 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
|
|
|
|
<<ftello64>>---return position in a stream or file
|
|
|
|
|
|
|
|
INDEX
|
|
|
|
ftello64
|
2003-08-23 02:52:25 +08:00
|
|
|
INDEX
|
|
|
|
_ftello64_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>
|
|
|
|
_off64_t ftello64(FILE *<[fp]>);
|
2003-08-23 02:52:25 +08:00
|
|
|
_off64_t _ftello64_r(struct _reent *<[ptr]>, FILE *<[fp]>);
|
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.
|
|
|
|
|
|
|
|
The result of <<ftello64>> is the current position for a large file
|
|
|
|
identified by <[fp]>. If you record this result, you can later
|
|
|
|
use it with <<fseeko64>> to return the file to this
|
|
|
|
position. The difference between <<ftello>> and <<ftello64>> is that
|
|
|
|
<<ftello>> returns <<off_t>> and <<ftello64>> is designed to work
|
|
|
|
for large files (>2GB) and returns <<_off64_t>>.
|
|
|
|
|
|
|
|
In the current implementation, <<ftello64>> simply uses a character
|
|
|
|
count to represent the file position; this is the same number that
|
|
|
|
would be recorded by <<fgetpos64>>.
|
|
|
|
|
|
|
|
The function exists only if the __LARGE64_FILES flag is defined.
|
|
|
|
An error occurs if the <[fp]> was not opened via <<fopen64>>.
|
|
|
|
|
|
|
|
RETURNS
|
|
|
|
<<ftello64>> returns the file position, if possible. If it cannot do
|
|
|
|
this, it returns <<-1>>. Failure occurs on streams that do not support
|
2007-05-02 07:03:36 +08:00
|
|
|
positioning or not opened via <<fopen64>>; the global <<errno>> indicates
|
2002-07-18 07:25:44 +08:00
|
|
|
this condition with the value <<ESPIPE>>.
|
|
|
|
|
|
|
|
PORTABILITY
|
|
|
|
<<ftello64>> is a glibc extension.
|
|
|
|
|
|
|
|
No supporting OS subroutines are required.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
|
|
|
static char sccsid[] = "%W% (Berkeley) %G%";
|
|
|
|
#endif /* LIBC_SCCS and not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ftello64: return current offset.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2007-05-02 07:03:36 +08:00
|
|
|
#include "local.h"
|
2002-07-18 07:25:44 +08:00
|
|
|
|
2002-08-27 02:56:09 +08:00
|
|
|
#ifdef __LARGE64_FILES
|
|
|
|
|
2002-07-18 07:25:44 +08:00
|
|
|
_off64_t
|
2017-12-04 11:43:30 +08:00
|
|
|
_ftello64_r (struct _reent *ptr,
|
2002-07-18 07:25:44 +08:00
|
|
|
register FILE * fp)
|
|
|
|
{
|
|
|
|
_fpos64_t pos;
|
|
|
|
|
2007-07-14 04:37:53 +08:00
|
|
|
/* Only do 64-bit tell on large file. */
|
|
|
|
if (!(fp->_flags & __SL64))
|
|
|
|
return (_off64_t) _ftello_r (ptr, fp);
|
|
|
|
|
2002-07-18 07:25:44 +08:00
|
|
|
/* Ensure 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);
|
2002-07-18 07:25:44 +08:00
|
|
|
|
|
|
|
if (fp->_seek64 == NULL)
|
|
|
|
{
|
2003-08-23 02:52:25 +08:00
|
|
|
ptr->_errno = ESPIPE;
|
2012-05-30 16:58:42 +08:00
|
|
|
_newlib_flockfile_exit(fp);
|
2017-12-19 03:15:27 +08:00
|
|
|
return (_off64_t) -1;
|
2002-07-18 07:25:44 +08:00
|
|
|
}
|
|
|
|
|
2017-12-19 03:17:51 +08:00
|
|
|
/* Find offset of underlying I/O object, then adjust for buffered bytes. */
|
|
|
|
if (!(fp->_flags & __SRD) && (fp->_flags & __SWR) &&
|
|
|
|
fp->_p != NULL && fp->_p - fp->_bf._base > 0 &&
|
|
|
|
(fp->_flags & __SAPP))
|
|
|
|
{
|
|
|
|
pos = fp->_seek64 (ptr, fp->_cookie, (_fpos64_t) 0, SEEK_END);
|
|
|
|
if (pos == (_fpos64_t) -1)
|
|
|
|
{
|
|
|
|
_newlib_flockfile_exit (fp);
|
|
|
|
return (_off64_t) -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (fp->_flags & __SOFF)
|
2002-07-18 07:25:44 +08:00
|
|
|
pos = 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
|
|
|
pos = fp->_seek64 (ptr, fp->_cookie, (_fpos64_t) 0, SEEK_CUR);
|
2017-12-19 03:15:27 +08:00
|
|
|
if (pos == (_fpos64_t) -1)
|
2002-07-18 07:25:44 +08:00
|
|
|
{
|
2012-05-30 16:58:42 +08:00
|
|
|
_newlib_flockfile_exit(fp);
|
2017-12-19 03:15:27 +08:00
|
|
|
return (_off64_t) pos;
|
2002-07-18 07:25:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fp->_flags & __SRD)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Reading. Any unread characters (including
|
|
|
|
* those from ungetc) cause the position to be
|
|
|
|
* smaller than that in the underlying object.
|
|
|
|
*/
|
|
|
|
pos -= fp->_r;
|
|
|
|
if (HASUB (fp))
|
|
|
|
pos -= fp->_ur;
|
|
|
|
}
|
|
|
|
else if (fp->_flags & __SWR && fp->_p != NULL)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Writing. Any buffered characters cause the
|
|
|
|
* position to be greater than that in the
|
|
|
|
* underlying object.
|
|
|
|
*/
|
|
|
|
pos += fp->_p - fp->_bf._base;
|
|
|
|
}
|
|
|
|
|
2012-05-30 16:58:42 +08:00
|
|
|
_newlib_flockfile_end(fp);
|
2017-12-19 03:15:27 +08:00
|
|
|
return (_off64_t) pos;
|
2002-07-18 07:25:44 +08:00
|
|
|
}
|
|
|
|
|
2003-08-23 02:52:25 +08:00
|
|
|
#ifndef _REENT_ONLY
|
|
|
|
|
|
|
|
_off64_t
|
2017-12-04 11:43:30 +08:00
|
|
|
ftello64 (register FILE * fp)
|
2003-08-23 02:52:25 +08:00
|
|
|
{
|
|
|
|
return _ftello64_r (_REENT, fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !_REENT_ONLY */
|
|
|
|
|
2002-07-18 07:25:44 +08:00
|
|
|
#endif /* __LARGE64_FILES */
|