OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [stdio/] [ftell.c] - Blame information for rev 1773

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.