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