2000-02-18 03:39:52 +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,
|
|
|
|
* 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
|
2002-07-05 03:33:54 +08:00
|
|
|
<<ftell>>, <<ftello>>---return position in a stream or file
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
INDEX
|
|
|
|
ftell
|
2002-07-05 03:33:54 +08:00
|
|
|
INDEX
|
|
|
|
ftello
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
ANSI_SYNOPSIS
|
|
|
|
#include <stdio.h>
|
|
|
|
long ftell(FILE *<[fp]>);
|
2002-07-05 03:33:54 +08:00
|
|
|
off_t ftello(FILE *<[fp]>);
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
TRAD_SYNOPSIS
|
|
|
|
#include <stdio.h>
|
|
|
|
long ftell(<[fp]>)
|
|
|
|
FILE *<[fp]>;
|
|
|
|
|
2002-07-05 03:33:54 +08:00
|
|
|
off_t ftello(<[fp]>)
|
|
|
|
FILE *<[fp]>;
|
|
|
|
|
2000-02-18 03:39:52 +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.
|
|
|
|
|
2002-07-05 03:33:54 +08:00
|
|
|
The result of <<ftell>>/<<ftello>> is the current position for a file
|
2000-02-18 03:39:52 +08:00
|
|
|
identified by <[fp]>. If you record this result, you can later
|
2002-07-05 03:33:54 +08:00
|
|
|
use it with <<fseek>>/<<fseeko>> to return the file to this
|
|
|
|
position. The difference between <<ftell>> and <<ftello>> is that
|
2002-07-16 23:30:32 +08:00
|
|
|
<<ftell>> returns <<long>> and <<ftello>> returns <<off_t>>.
|
2000-02-18 03:39:52 +08:00
|
|
|
|
2002-07-05 03:33:54 +08:00
|
|
|
In the current implementation, <<ftell>>/<<ftello>> simply uses a character
|
2000-02-18 03:39:52 +08:00
|
|
|
count to represent the file position; this is the same number that
|
|
|
|
would be recorded by <<fgetpos>>.
|
|
|
|
|
|
|
|
RETURNS
|
2002-07-05 03:33:54 +08:00
|
|
|
<<ftell>>/<<ftello>> return the file position, if possible. If they cannot do
|
|
|
|
this, they return <<-1L>>. Failure occurs on streams that do not support
|
2000-02-18 03:39:52 +08:00
|
|
|
positioning; the global <<errno>> indicates this condition with the
|
|
|
|
value <<ESPIPE>>.
|
|
|
|
|
|
|
|
PORTABILITY
|
|
|
|
<<ftell>> is required by the ANSI C standard, but the meaning of its
|
|
|
|
result (when successful) is not specified beyond requiring that it be
|
|
|
|
acceptable as an argument to <<fseek>>. In particular, other
|
|
|
|
conforming C implementations may return a different result from
|
|
|
|
<<ftell>> than what <<fgetpos>> records.
|
|
|
|
|
2002-07-05 03:33:54 +08:00
|
|
|
<<ftello>> is defined by the Single Unix specification.
|
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
No supporting OS subroutines are required.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
|
|
|
static char sccsid[] = "%W% (Berkeley) %G%";
|
|
|
|
#endif /* LIBC_SCCS and not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ftell: return current offset.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "local.h"
|
|
|
|
|
|
|
|
long
|
|
|
|
_DEFUN (ftell, (fp),
|
|
|
|
register FILE * fp)
|
|
|
|
{
|
2003-03-15 02:39:05 +08:00
|
|
|
_fpos_t pos;
|
2000-02-18 03:39:52 +08:00
|
|
|
|
2002-05-08 08:12:49 +08:00
|
|
|
_flockfile(fp);
|
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
/* Ensure stdio is set up. */
|
|
|
|
|
|
|
|
CHECK_INIT (fp);
|
|
|
|
|
|
|
|
if (fp->_seek == NULL)
|
|
|
|
{
|
|
|
|
fp->_data->_errno = ESPIPE;
|
2002-05-08 08:12:49 +08:00
|
|
|
_funlockfile(fp);
|
2000-02-18 03:39:52 +08:00
|
|
|
return -1L;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find offset of underlying I/O object, then
|
|
|
|
adjust for buffered bytes. */
|
|
|
|
fflush(fp); /* may adjust seek offset on append stream */
|
|
|
|
if (fp->_flags & __SOFF)
|
|
|
|
pos = fp->_offset;
|
|
|
|
else
|
|
|
|
{
|
2003-03-15 02:39:05 +08:00
|
|
|
pos = (*fp->_seek) (fp->_cookie, (_fpos_t) 0, SEEK_CUR);
|
2000-02-18 03:39:52 +08:00
|
|
|
if (pos == -1L)
|
2002-05-08 08:12:49 +08:00
|
|
|
{
|
|
|
|
_funlockfile(fp);
|
|
|
|
return pos;
|
|
|
|
}
|
2000-02-18 03:39:52 +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;
|
|
|
|
}
|
|
|
|
|
2002-05-08 08:12:49 +08:00
|
|
|
_funlockfile(fp);
|
2000-02-18 03:39:52 +08:00
|
|
|
return pos;
|
|
|
|
}
|