1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #include <config.h>
18
19
20 #include <stdio.h>
21
22 #include <errno.h>
23 #include "intprops.h"
24
25
26 #include <unistd.h>
27
28 #include "stdio-impl.h"
29
30 off_t
31 ftello (FILE *fp)
32 #undef ftello
33 #if !HAVE_FTELLO
34 # undef ftell
35 # define ftello ftell
36 #endif
37 #if _GL_WINDOWS_64_BIT_OFF_T
38 # undef ftello
39 # if HAVE__FTELLI64
40 # define ftello _ftelli64
41 # else
42 # define ftello ftello64
43 # endif
44 #endif
45 {
46 #if FTELLO_BROKEN_AFTER_UNGETC
47
48
49
50
51 if (fp->_file < 0 || fp->_seek == NULL)
52 {
53 errno = ESPIPE;
54 return -1;
55 }
56
57
58 off_t pos;
59
60 if (fp->_flags & __SOFF)
61 pos = fp->_offset;
62 else
63 {
64 pos = fp->_seek (fp->_cookie, 0, SEEK_CUR);
65 if (pos < 0)
66 return -1;
67 if (fp->_flags & __SOPT)
68 {
69 fp->_offset = pos;
70 fp->_flags |= __SOFF;
71 }
72 }
73
74 if (fp->_flags & __SRD)
75 {
76
77 if (fp->_ub._base != NULL)
78
79
80
81
82 pos = pos - fp->_ur - fp->_r;
83 else
84
85
86 pos = pos - fp->_r;
87 if (pos < 0)
88 {
89 errno = EIO;
90 return -1;
91 }
92 }
93 else if ((fp->_flags & __SWR) && fp->_p != NULL)
94 {
95
96 off_t buffered = fp->_p - fp->_bf._base;
97
98
99 off_t sum;
100 if (! INT_ADD_OK (pos, buffered, &sum))
101 {
102 errno = EOVERFLOW;
103 return -1;
104 }
105 pos = sum;
106 }
107
108 return pos;
109
110 #else
111
112 # if LSEEK_PIPE_BROKEN
113
114 if (lseek (fileno (fp), 0, SEEK_CUR) == -1)
115 return -1;
116 # endif
117
118 # if FTELLO_BROKEN_AFTER_SWITCHING_FROM_READ_TO_WRITE
119
120
121
122 if (fp_->_flag & _IOWRT)
123 {
124 off_t pos;
125
126
127 ftello (fp);
128
129
130 pos = lseek (fileno (fp), (off_t) 0, SEEK_CUR);
131 if (pos >= 0)
132 {
133 if ((fp_->_flag & _IONBF) == 0 && fp_->_base != NULL)
134 pos += fp_->_ptr - fp_->_base;
135 }
136 return pos;
137 }
138 # endif
139
140 # if defined __SL64 && defined __SCLE
141 if ((fp->_flags & __SL64) == 0)
142 {
143
144
145 FILE *tmp = fopen ("/dev/null", "r");
146 if (!tmp)
147 return -1;
148 fp->_flags |= __SL64;
149 fp->_seek64 = tmp->_seek64;
150 fclose (tmp);
151 }
152 # endif
153
154 return ftello (fp);
155
156 #endif
157 }