os/ossrv/genericopenlibs/cstdlib/LSTDIO/FTELL.C
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* FTELL.C
sl@0
     2
 * 
sl@0
     3
 * Portions Copyright (c) 1990-1999 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     4
 * All rights reserved.
sl@0
     5
 */
sl@0
     6
sl@0
     7
/*
sl@0
     8
 * Copyright (c) 1990 The Regents of the University of California.
sl@0
     9
 * All rights reserved.
sl@0
    10
 *
sl@0
    11
 * Redistribution and use in source and binary forms are permitted
sl@0
    12
 * provided that the above copyright notice and this paragraph are
sl@0
    13
 * duplicated in all such forms and that any documentation,
sl@0
    14
 * advertising materials, and other materials related to such
sl@0
    15
 * distribution and use acknowledge that the software was developed
sl@0
    16
 * by the University of California, Berkeley.  The name of the
sl@0
    17
 * University may not be used to endorse or promote products derived
sl@0
    18
 * from this software without specific prior written permission.
sl@0
    19
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
sl@0
    20
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
sl@0
    21
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
sl@0
    22
 */
sl@0
    23
sl@0
    24
/*
sl@0
    25
FUNCTION
sl@0
    26
<<ftell>>---return position in a stream or file
sl@0
    27
sl@0
    28
INDEX
sl@0
    29
	ftell
sl@0
    30
sl@0
    31
ANSI_SYNOPSIS
sl@0
    32
	#include <stdio.h>
sl@0
    33
	long ftell(FILE *<[fp]>);
sl@0
    34
sl@0
    35
TRAD_SYNOPSIS
sl@0
    36
	#include <stdio.h>
sl@0
    37
	long ftell(<[fp]>)
sl@0
    38
	FILE *<[fp]>;
sl@0
    39
sl@0
    40
DESCRIPTION
sl@0
    41
Objects of type <<FILE>> can have a ``position'' that records how much
sl@0
    42
of the file your program has already read.  Many of the <<stdio>> functions
sl@0
    43
depend on this position, and many change it as a side effect.
sl@0
    44
sl@0
    45
The result of <<ftell>> is the current position for a file
sl@0
    46
identified by <[fp]>.  If you record this result, you can later
sl@0
    47
use it with <<fseek>> to return the file to this
sl@0
    48
position.
sl@0
    49
sl@0
    50
In the current implementation, <<ftell>> simply uses a character
sl@0
    51
count to represent the file position; this is the same number that
sl@0
    52
would be recorded by <<fgetpos>>.
sl@0
    53
sl@0
    54
RETURNS
sl@0
    55
<<ftell>> returns the file position, if possible.  If it cannot do
sl@0
    56
this, it returns <<-1L>>.  Failure occurs on streams that do not support
sl@0
    57
positioning; the global <<errno>> indicates this condition with the
sl@0
    58
value <<ESPIPE>>.
sl@0
    59
sl@0
    60
PORTABILITY
sl@0
    61
<<ftell>> is required by the ANSI C standard, but the meaning of its
sl@0
    62
result (when successful) is not specified beyond requiring that it be
sl@0
    63
acceptable as an argument to <<fseek>>.  In particular, other
sl@0
    64
conforming C implementations may return a different result from
sl@0
    65
<<ftell>> than what <<fgetpos>> records.
sl@0
    66
sl@0
    67
No supporting OS subroutines are required.
sl@0
    68
*/
sl@0
    69
sl@0
    70
/*
sl@0
    71
 * ftell: return current offset.
sl@0
    72
 */
sl@0
    73
sl@0
    74
#include <stdio_r.h>
sl@0
    75
#include <errno.h>
sl@0
    76
sl@0
    77
#include "LOCAL.H"
sl@0
    78
sl@0
    79
/**
sl@0
    80
Return the current position in a stream.
sl@0
    81
Returns the current position pointed by the position indicator of the stream. 
sl@0
    82
When a file has been opened in binary mode the value obtained corresponds to 
sl@0
    83
the number of bytes from the beginning of the file. In files opened in 
sl@0
    84
text-mode this is not granted because of carriage-return translations under that mode.
sl@0
    85
@return On success, the current file pointer position is returned.
sl@0
    86
If an error occurs -1 is returned.
sl@0
    87
@param Pointer to an open file.
sl@0
    88
*/
sl@0
    89
EXPORT_C long
sl@0
    90
ftell (FILE * fp)
sl@0
    91
{
sl@0
    92
  fpos_t pos;
sl@0
    93
sl@0
    94
  /* Ensure stdio is set up.  */
sl@0
    95
sl@0
    96
  CHECK_INIT (fp);
sl@0
    97
sl@0
    98
  if (fp->_seek == NULL)
sl@0
    99
    {
sl@0
   100
      __errno_r(fp->_data) = ESPIPE;
sl@0
   101
      return -1L;
sl@0
   102
    }
sl@0
   103
sl@0
   104
  /* Find offset of underlying I/O object, then
sl@0
   105
     adjust for buffered bytes.  */
sl@0
   106
sl@0
   107
  if (fp->_flags & __SOFF)
sl@0
   108
    pos = fp->_offset;
sl@0
   109
  else
sl@0
   110
    {
sl@0
   111
      pos = (*fp->_seek) (fp->_cookie, (fpos_t) 0, SEEK_CUR);
sl@0
   112
      if (pos == -1L)
sl@0
   113
	return pos;
sl@0
   114
    }
sl@0
   115
  if (fp->_flags & __SRD)
sl@0
   116
    {
sl@0
   117
      /*
sl@0
   118
       * Reading.  Any unread characters (including
sl@0
   119
       * those from ungetc) cause the position to be
sl@0
   120
       * smaller than that in the underlying object.
sl@0
   121
       */
sl@0
   122
      pos -= fp->_r;
sl@0
   123
      if (HASUB (fp))
sl@0
   124
	pos -= fp->_ur;
sl@0
   125
    }
sl@0
   126
  else if (fp->_flags & __SWR && fp->_p != NULL)
sl@0
   127
    {
sl@0
   128
      /*
sl@0
   129
       * Writing.  Any buffered characters cause the
sl@0
   130
       * position to be greater than that in the
sl@0
   131
       * underlying object.
sl@0
   132
       */
sl@0
   133
      pos += fp->_p - fp->_bf._base;
sl@0
   134
    }
sl@0
   135
sl@0
   136
  return pos;
sl@0
   137
}