os/ossrv/genericopenlibs/cstdlib/LSTDIO/FTELL.C
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LSTDIO/FTELL.C	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,137 @@
     1.4 +/* FTELL.C
     1.5 + * 
     1.6 + * Portions Copyright (c) 1990-1999 Nokia Corporation and/or its subsidiary(-ies).
     1.7 + * All rights reserved.
     1.8 + */
     1.9 +
    1.10 +/*
    1.11 + * Copyright (c) 1990 The Regents of the University of California.
    1.12 + * All rights reserved.
    1.13 + *
    1.14 + * Redistribution and use in source and binary forms are permitted
    1.15 + * provided that the above copyright notice and this paragraph are
    1.16 + * duplicated in all such forms and that any documentation,
    1.17 + * advertising materials, and other materials related to such
    1.18 + * distribution and use acknowledge that the software was developed
    1.19 + * by the University of California, Berkeley.  The name of the
    1.20 + * University may not be used to endorse or promote products derived
    1.21 + * from this software without specific prior written permission.
    1.22 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
    1.23 + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
    1.24 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 +FUNCTION
    1.29 +<<ftell>>---return position in a stream or file
    1.30 +
    1.31 +INDEX
    1.32 +	ftell
    1.33 +
    1.34 +ANSI_SYNOPSIS
    1.35 +	#include <stdio.h>
    1.36 +	long ftell(FILE *<[fp]>);
    1.37 +
    1.38 +TRAD_SYNOPSIS
    1.39 +	#include <stdio.h>
    1.40 +	long ftell(<[fp]>)
    1.41 +	FILE *<[fp]>;
    1.42 +
    1.43 +DESCRIPTION
    1.44 +Objects of type <<FILE>> can have a ``position'' that records how much
    1.45 +of the file your program has already read.  Many of the <<stdio>> functions
    1.46 +depend on this position, and many change it as a side effect.
    1.47 +
    1.48 +The result of <<ftell>> is the current position for a file
    1.49 +identified by <[fp]>.  If you record this result, you can later
    1.50 +use it with <<fseek>> to return the file to this
    1.51 +position.
    1.52 +
    1.53 +In the current implementation, <<ftell>> simply uses a character
    1.54 +count to represent the file position; this is the same number that
    1.55 +would be recorded by <<fgetpos>>.
    1.56 +
    1.57 +RETURNS
    1.58 +<<ftell>> returns the file position, if possible.  If it cannot do
    1.59 +this, it returns <<-1L>>.  Failure occurs on streams that do not support
    1.60 +positioning; the global <<errno>> indicates this condition with the
    1.61 +value <<ESPIPE>>.
    1.62 +
    1.63 +PORTABILITY
    1.64 +<<ftell>> is required by the ANSI C standard, but the meaning of its
    1.65 +result (when successful) is not specified beyond requiring that it be
    1.66 +acceptable as an argument to <<fseek>>.  In particular, other
    1.67 +conforming C implementations may return a different result from
    1.68 +<<ftell>> than what <<fgetpos>> records.
    1.69 +
    1.70 +No supporting OS subroutines are required.
    1.71 +*/
    1.72 +
    1.73 +/*
    1.74 + * ftell: return current offset.
    1.75 + */
    1.76 +
    1.77 +#include <stdio_r.h>
    1.78 +#include <errno.h>
    1.79 +
    1.80 +#include "LOCAL.H"
    1.81 +
    1.82 +/**
    1.83 +Return the current position in a stream.
    1.84 +Returns the current position pointed by the position indicator of the stream. 
    1.85 +When a file has been opened in binary mode the value obtained corresponds to 
    1.86 +the number of bytes from the beginning of the file. In files opened in 
    1.87 +text-mode this is not granted because of carriage-return translations under that mode.
    1.88 +@return On success, the current file pointer position is returned.
    1.89 +If an error occurs -1 is returned.
    1.90 +@param Pointer to an open file.
    1.91 +*/
    1.92 +EXPORT_C long
    1.93 +ftell (FILE * fp)
    1.94 +{
    1.95 +  fpos_t pos;
    1.96 +
    1.97 +  /* Ensure stdio is set up.  */
    1.98 +
    1.99 +  CHECK_INIT (fp);
   1.100 +
   1.101 +  if (fp->_seek == NULL)
   1.102 +    {
   1.103 +      __errno_r(fp->_data) = ESPIPE;
   1.104 +      return -1L;
   1.105 +    }
   1.106 +
   1.107 +  /* Find offset of underlying I/O object, then
   1.108 +     adjust for buffered bytes.  */
   1.109 +
   1.110 +  if (fp->_flags & __SOFF)
   1.111 +    pos = fp->_offset;
   1.112 +  else
   1.113 +    {
   1.114 +      pos = (*fp->_seek) (fp->_cookie, (fpos_t) 0, SEEK_CUR);
   1.115 +      if (pos == -1L)
   1.116 +	return pos;
   1.117 +    }
   1.118 +  if (fp->_flags & __SRD)
   1.119 +    {
   1.120 +      /*
   1.121 +       * Reading.  Any unread characters (including
   1.122 +       * those from ungetc) cause the position to be
   1.123 +       * smaller than that in the underlying object.
   1.124 +       */
   1.125 +      pos -= fp->_r;
   1.126 +      if (HASUB (fp))
   1.127 +	pos -= fp->_ur;
   1.128 +    }
   1.129 +  else if (fp->_flags & __SWR && fp->_p != NULL)
   1.130 +    {
   1.131 +      /*
   1.132 +       * Writing.  Any buffered characters cause the
   1.133 +       * position to be greater than that in the
   1.134 +       * underlying object.
   1.135 +       */
   1.136 +      pos += fp->_p - fp->_bf._base;
   1.137 +    }
   1.138 +
   1.139 +  return pos;
   1.140 +}