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