Debian Codegraph @

lib/ftello.c

raw
1 /* An ftello() function that works around platform bugs.
2 Copyright (C) 2007, 2009-2026 Free Software Foundation, Inc.
3
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
8
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 /* Specification. */
20 #include <stdio.h>
21
22 #include <errno.h>
23 #include <stdckdint.h>
24
25 /* Get lseek. */
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 (defined _WIN32 && !defined __CYGWIN__) \
38 /* We need to test _FILE_OFFSET_BITS for mingw-w64 */ \
39 /* and _GL_WINDOWS_64_BIT_OFF_T for MSVC. */ \
40 && (_FILE_OFFSET_BITS == 64 || _GL_WINDOWS_64_BIT_OFF_T)
41 # undef ftello
42 # if HAVE__FTELLI64 /* msvc, mingw64 */
43 # define ftello _ftelli64
44 # else /* mingw */
45 # define ftello ftello64
46 # endif
47 #endif
48 {
49 #if FTELLO_BROKEN_AFTER_UNGETC /* macOS >= 10.15 */
50 /* The system's ftello() is completely broken, because it calls __sflush,
51 which makes side effects on the stream. */
52
53 /* Handle non-seekable files first. */
54 if (fp->_file < 0 || fp->_seek == NULL)
55 {
56 errno = ESPIPE;
57 return -1;
58 }
59
60 /* Determine the current offset, ignoring buffered and pushed-back bytes. */
61 off_t pos;
62
63 if (fp->_flags & __SOFF)
64 pos = fp->_offset;
65 else
66 {
67 pos = fp->_seek (fp->_cookie, 0, SEEK_CUR);
68 if (pos < 0)
69 return -1;
70 if (fp->_flags & __SOPT)
71 {
72 fp->_offset = pos;
73 fp->_flags |= __SOFF;
74 }
75 }
76
77 if (fp->_flags & __SRD)
78 {
79 /* Now consider buffered and pushed-back bytes from ungetc. */
80 if (fp->_ub._base != NULL)
81 /* Considering the buffered bytes, we are at position
82 pos - fp->_ur.
83 Considering also the pushed-back bytes, we are at position
84 pos - fp->_ur - fp->_r. */
85 pos = pos - fp->_ur - fp->_r;
86 else
87 /* Considering the buffered bytes, we are at position
88 pos - fp->_r. */
89 pos = pos - fp->_r;
90 if (pos < 0)
91 {
92 errno = EIO;
93 return -1;
94 }
95 }
96 else if ((fp->_flags & __SWR) && fp->_p != NULL)
97 {
98 /* Consider the buffered bytes. */
99 off_t buffered = fp->_p - fp->_bf._base;
100
101 /* Compute pos + buffered, with overflow check. */
102 off_t sum;
103 if (ckd_add (&sum, pos, buffered))
104 {
105 errno = EOVERFLOW;
106 return -1;
107 }
108 pos = sum;
109 }
110
111 return pos;
112
113 #else
114
115 # if LSEEK_PIPE_BROKEN
116 /* mingw gives bogus answers rather than failure on non-seekable files. */
117 if (lseek (fileno (fp), 0, SEEK_CUR) == -1)
118 return -1;
119 # endif
120
121 # if FTELLO_BROKEN_AFTER_SWITCHING_FROM_READ_TO_WRITE /* Solaris */
122 /* The Solaris stdio leaves the _IOREAD flag set after reading from a file
123 reaches EOF and the program then starts writing to the file. ftello
124 gets confused by this. */
125 if (fp_->_flag & _IOWRT)
126 {
127 /* Call ftello nevertheless, for the side effects that it does on fp. */
128 ftello (fp);
129
130 /* Compute the file position ourselves. */
131 off_t pos = lseek (fileno (fp), (off_t) 0, SEEK_CUR);
132 if (pos >= 0)
133 {
134 if ((fp_->_flag & _IONBF) == 0 && fp_->_base != NULL)
135 pos += fp_->_ptr - fp_->_base;
136 }
137 return pos;
138 }
139 # endif
140
141 # if defined __SL64 && defined __SCLE /* Cygwin */
142 if ((fp->_flags & __SL64) == 0)
143 {
144 /* Cygwin 1.5.0 through 1.5.24 failed to open stdin in 64-bit
145 mode; but has an ftello that requires 64-bit mode. */
146 FILE *tmp = fopen ("/dev/null", "r");
147 if (!tmp)
148 return -1;
149 fp->_flags |= __SL64;
150 fp->_seek64 = tmp->_seek64;
151 fclose (tmp);
152 }
153 # endif
154
155 return ftello (fp);
156
157 #endif
158 }