source: trunk/libs/newlib/src/newlib/libc/stdio/ftello.c @ 567

Last change on this file since 567 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 4.4 KB
Line 
1/*
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/*
19FUNCTION
20<<ftell>>, <<ftello>>---return position in a stream or file
21
22INDEX
23        ftell
24INDEX
25        ftello
26INDEX
27        _ftell_r
28INDEX
29        _ftello_r
30
31SYNOPSIS
32        #include <stdio.h>
33        long ftell(FILE *<[fp]>);
34        off_t ftello(FILE *<[fp]>);
35        long _ftell_r(struct _reent *<[ptr]>, FILE *<[fp]>);
36        off_t _ftello_r(struct _reent *<[ptr]>, FILE *<[fp]>);
37
38DESCRIPTION
39Objects of type <<FILE>> can have a ``position'' that records how much
40of the file your program has already read.  Many of the <<stdio>> functions
41depend on this position, and many change it as a side effect.
42
43The result of <<ftell>>/<<ftello>> is the current position for a file
44identified by <[fp]>.  If you record this result, you can later
45use it with <<fseek>>/<<fseeko>> to return the file to this
46position.  The difference between <<ftell>> and <<ftello>> is that
47<<ftell>> returns <<long>> and <<ftello>> returns <<off_t>>.
48
49In the current implementation, <<ftell>>/<<ftello>> simply uses a character
50count to represent the file position; this is the same number that
51would be recorded by <<fgetpos>>.
52
53RETURNS
54<<ftell>>/<<ftello>> return the file position, if possible.  If they cannot do
55this, they return <<-1L>>.  Failure occurs on streams that do not support
56positioning; the global <<errno>> indicates this condition with the
57value <<ESPIPE>>.
58
59PORTABILITY
60<<ftell>> is required by the ANSI C standard, but the meaning of its
61result (when successful) is not specified beyond requiring that it be
62acceptable as an argument to <<fseek>>.  In particular, other
63conforming C implementations may return a different result from
64<<ftell>> than what <<fgetpos>> records.
65
66<<ftello>> is defined by the Single Unix specification.
67
68No supporting OS subroutines are required.
69*/
70
71#if defined(LIBC_SCCS) && !defined(lint)
72static char sccsid[] = "%W% (Berkeley) %G%";
73#endif /* LIBC_SCCS and not lint */
74
75/*
76 * ftello: return current offset.
77 */
78
79#include <_ansi.h>
80#include <reent.h>
81#include <stdio.h>
82#include <errno.h>
83#include "local.h"
84
85_off_t
86_ftello_r (struct _reent * ptr,
87       register FILE * fp)
88{
89  _fpos_t pos;
90
91  /* Ensure stdio is set up.  */
92
93  CHECK_INIT (ptr, fp);
94
95  _newlib_flockfile_start (fp);
96
97  if (fp->_seek == NULL)
98    {
99      ptr->_errno = ESPIPE;
100      _newlib_flockfile_exit (fp);
101      return (_off_t) -1;
102    }
103
104  /* Find offset of underlying I/O object, then adjust for buffered bytes. */
105  if (!(fp->_flags & __SRD) && (fp->_flags & __SWR) &&
106      fp->_p != NULL && fp->_p - fp->_bf._base > 0 &&
107      (fp->_flags & __SAPP))
108    {
109      pos = fp->_seek (ptr, fp->_cookie, (_fpos_t) 0, SEEK_END);
110      if (pos == (_fpos_t) -1)
111        {
112          _newlib_flockfile_exit (fp);
113          return (_off_t) -1;
114        }
115    }
116  else if (fp->_flags & __SOFF)
117    pos = fp->_offset;
118  else
119    {
120      pos = fp->_seek (ptr, fp->_cookie, (_fpos_t) 0, SEEK_CUR);
121      if (pos == (_fpos_t) -1)
122        {
123          _newlib_flockfile_exit (fp);
124          return (_off_t) -1;
125        }
126    }
127  if (fp->_flags & __SRD)
128    {
129      /*
130       * Reading.  Any unread characters (including
131       * those from ungetc) cause the position to be
132       * smaller than that in the underlying object.
133       */
134      pos -= fp->_r;
135      if (HASUB (fp))
136        pos -= fp->_ur;
137    }
138  else if ((fp->_flags & __SWR) && fp->_p != NULL)
139    {
140      /*
141       * Writing.  Any buffered characters cause the
142       * position to be greater than that in the
143       * underlying object.
144       */
145      pos += fp->_p - fp->_bf._base;
146    }
147
148  _newlib_flockfile_end (fp);
149  return (_off_t) pos;
150}
151
152#ifndef _REENT_ONLY
153
154_off_t
155ftello (register FILE * fp)
156{
157  return _ftello_r (_REENT, fp);
158}
159
160#endif /* !_REENT_ONLY */
Note: See TracBrowser for help on using the repository browser.