sl@0: /* FTELL.C sl@0: * sl@0: * Portions Copyright (c) 1990-1999 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: */ sl@0: sl@0: /* sl@0: * Copyright (c) 1990 The Regents of the University of California. sl@0: * All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms are permitted sl@0: * provided that the above copyright notice and this paragraph are sl@0: * duplicated in all such forms and that any documentation, sl@0: * advertising materials, and other materials related to such sl@0: * distribution and use acknowledge that the software was developed sl@0: * by the University of California, Berkeley. The name of the sl@0: * University may not be used to endorse or promote products derived sl@0: * from this software without specific prior written permission. sl@0: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR sl@0: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED sl@0: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. sl@0: */ sl@0: sl@0: /* sl@0: FUNCTION sl@0: <>---return position in a stream or file sl@0: sl@0: INDEX sl@0: ftell sl@0: sl@0: ANSI_SYNOPSIS sl@0: #include sl@0: long ftell(FILE *<[fp]>); sl@0: sl@0: TRAD_SYNOPSIS sl@0: #include sl@0: long ftell(<[fp]>) sl@0: FILE *<[fp]>; sl@0: sl@0: DESCRIPTION sl@0: Objects of type <> can have a ``position'' that records how much sl@0: of the file your program has already read. Many of the <> functions sl@0: depend on this position, and many change it as a side effect. sl@0: sl@0: The result of <> is the current position for a file sl@0: identified by <[fp]>. If you record this result, you can later sl@0: use it with <> to return the file to this sl@0: position. sl@0: sl@0: In the current implementation, <> simply uses a character sl@0: count to represent the file position; this is the same number that sl@0: would be recorded by <>. sl@0: sl@0: RETURNS sl@0: <> returns the file position, if possible. If it cannot do sl@0: this, it returns <<-1L>>. Failure occurs on streams that do not support sl@0: positioning; the global <> indicates this condition with the sl@0: value <>. sl@0: sl@0: PORTABILITY sl@0: <> is required by the ANSI C standard, but the meaning of its sl@0: result (when successful) is not specified beyond requiring that it be sl@0: acceptable as an argument to <>. In particular, other sl@0: conforming C implementations may return a different result from sl@0: <> than what <> records. sl@0: sl@0: No supporting OS subroutines are required. sl@0: */ sl@0: sl@0: /* sl@0: * ftell: return current offset. sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: sl@0: #include "LOCAL.H" sl@0: sl@0: /** sl@0: Return the current position in a stream. sl@0: Returns the current position pointed by the position indicator of the stream. sl@0: When a file has been opened in binary mode the value obtained corresponds to sl@0: the number of bytes from the beginning of the file. In files opened in sl@0: text-mode this is not granted because of carriage-return translations under that mode. sl@0: @return On success, the current file pointer position is returned. sl@0: If an error occurs -1 is returned. sl@0: @param Pointer to an open file. sl@0: */ sl@0: EXPORT_C long sl@0: ftell (FILE * fp) sl@0: { sl@0: fpos_t pos; sl@0: sl@0: /* Ensure stdio is set up. */ sl@0: sl@0: CHECK_INIT (fp); sl@0: sl@0: if (fp->_seek == NULL) sl@0: { sl@0: __errno_r(fp->_data) = ESPIPE; sl@0: return -1L; sl@0: } sl@0: sl@0: /* Find offset of underlying I/O object, then sl@0: adjust for buffered bytes. */ sl@0: sl@0: if (fp->_flags & __SOFF) sl@0: pos = fp->_offset; sl@0: else sl@0: { sl@0: pos = (*fp->_seek) (fp->_cookie, (fpos_t) 0, SEEK_CUR); sl@0: if (pos == -1L) sl@0: return pos; sl@0: } sl@0: if (fp->_flags & __SRD) sl@0: { sl@0: /* sl@0: * Reading. Any unread characters (including sl@0: * those from ungetc) cause the position to be sl@0: * smaller than that in the underlying object. sl@0: */ sl@0: pos -= fp->_r; sl@0: if (HASUB (fp)) sl@0: pos -= fp->_ur; sl@0: } sl@0: else if (fp->_flags & __SWR && fp->_p != NULL) sl@0: { sl@0: /* sl@0: * Writing. Any buffered characters cause the sl@0: * position to be greater than that in the sl@0: * underlying object. sl@0: */ sl@0: pos += fp->_p - fp->_bf._base; sl@0: } sl@0: sl@0: return pos; sl@0: }