Debian Codegraph @

lib/stdio-impl.h

raw
1 /* Implementation details of FILE streams.
2 Copyright (C) 2007-2008, 2010-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 /* Many stdio implementations have the same logic and therefore can share
18 the same implementation of stdio extension API, except that some fields
19 have different naming conventions, or their access requires some casts. */
20
21 /* Glibc 2.28 made _IO_UNBUFFERED and _IO_IN_BACKUP private. For now, work
22 around this problem by defining them ourselves. FIXME: Do not rely on glibc
23 internals. */
24 #if defined _IO_EOF_SEEN
25 # if !defined _IO_UNBUFFERED
26 # define _IO_UNBUFFERED 0x2
27 # endif
28 # if !defined _IO_IN_BACKUP
29 # define _IO_IN_BACKUP 0x100
30 # endif
31 #endif
32
33 /* Haiku stdio implementation. */
34 #if defined __HAIKU__
35 # include <stdint.h>
36 /* This FILE structure was made into an incomplete type in 2025.
37 See <https://cgit.haiku-os.org/haiku/tree/src/system/libroot/posix/glibc/libio/libio.h>. */
38 # define fp_ ((struct { int _flags; \
39 char *_IO_read_ptr; \
40 char *_IO_read_end; \
41 char *_IO_read_base; \
42 char *_IO_write_base; \
43 char *_IO_write_ptr; \
44 char *_IO_write_end; \
45 char *_IO_buf_base; \
46 char *_IO_buf_end; \
47 char *_IO_save_base; \
48 char *_IO_backup_base; \
49 char *_IO_save_end; \
50 void *_markers; \
51 void *_chain; \
52 int _fileno; \
53 int _flags2; \
54 off_t _old_offset; \
55 unsigned short _cur_column; \
56 signed char _vtable_offset; \
57 char _shortbuf[1]; \
58 void *_lock; \
59 int64_t _offset; \
60 /* More fields, not relevant here. */ \
61 } *) fp)
62 # if !defined _IO_UNBUFFERED
63 # define _IO_UNBUFFERED 0x2
64 # endif
65 # if !defined _IO_EOF_SEEN
66 # define _IO_EOF_SEEN 0x10
67 # endif
68 # if !defined _IO_IN_BACKUP
69 # define _IO_IN_BACKUP 0x100
70 # endif
71 # if !defined _IO_LINE_BUF
72 # define _IO_LINE_BUF 0x200
73 # endif
74 #endif
75
76 /* BSD stdio derived implementations. */
77
78 #if defined __NetBSD__ /* NetBSD */
79 /* Get __NetBSD_Version__. */
80 # include <sys/param.h>
81 #endif
82
83 #include <errno.h> /* For detecting Plan9. */
84
85 #if defined __sferror || defined __OpenBSD__ || defined __DragonFly__ || defined __ANDROID__
86 /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
87
88 # if defined __DragonFly__ /* DragonFly */
89 /* See <https://gitweb.dragonflybsd.org/dragonfly.git/blob_plain/HEAD:/lib/libc/stdio/priv_stdio.h>. */
90 # define fp_ ((struct { struct __FILE_public pub; \
91 struct { unsigned char *_base; int _size; } _bf; \
92 void *cookie; \
93 void *_close; \
94 void *_read; \
95 void *_seek; \
96 void *_write; \
97 struct { unsigned char *_base; int _size; } _ub; \
98 int _ur; \
99 unsigned char _ubuf[3]; \
100 unsigned char _nbuf[1]; \
101 struct { unsigned char *_base; int _size; } _lb; \
102 int _blksize; \
103 fpos_t _offset; \
104 /* More fields, not relevant here. */ \
105 } *) fp)
106 /* See <https://gitweb.dragonflybsd.org/dragonfly.git/blob_plain/HEAD:/include/stdio.h>. */
107 # define _p pub._p
108 # define _flags pub._flags
109 # define _r pub._r
110 # define _w pub._w
111 # elif defined __OpenBSD__ /* OpenBSD */
112 # if defined __sferror /* OpenBSD <= 7.7 */
113 # define _gl_flags_file_t short
114 # else /* OpenBSD >= 7.8 */
115 # define _gl_flags_file_t int
116 # endif
117 /* Up to this commit from 2025-07-16
118 <https://github.com/openbsd/src/commit/b7f6c2eb760a2da367dd51d539ef06f5f3553790>
119 the innards of FILE were public. After this commit, the innards of FILE
120 are hidden. In this commit
121 <https://github.com/openbsd/src/commit/9063a2f1ec94013fb0e2c7ec851495108e788a6e>
122 they were reshuffled. */
123 # if defined __sferror /* OpenBSD <= 7.7 */
124 # define fp_ ((struct { unsigned char *_p; \
125 int _r; \
126 int _w; \
127 _gl_flags_file_t _flags; \
128 _gl_flags_file_t _file; \
129 struct { unsigned char *_base; size_t _size; } _bf; \
130 int _lbfsize; \
131 void *_cookie; \
132 void *_close; \
133 void *_read; \
134 void *_seek; \
135 void *_write; \
136 struct { unsigned char *_base; size_t _size; } _ext; \
137 unsigned char *_up; \
138 int _ur; \
139 unsigned char _ubuf[3]; \
140 unsigned char _nbuf[1]; \
141 struct { unsigned char *_base; size_t _size; } _lb; \
142 int _blksize; \
143 fpos_t _offset; \
144 /* More fields, not relevant here. */ \
145 } *) fp)
146 # else /* OpenBSD >= 7.8 */
147 # define fp_ ((struct { _gl_flags_file_t _flags; \
148 _gl_flags_file_t _file; \
149 unsigned char *_p; \
150 int _r; \
151 int _w; \
152 struct { unsigned char *_base; size_t _size; } _bf; \
153 int _lbfsize; \
154 /* More fields, not relevant here. */ \
155 } *) fp)
156 # endif
157 # elif defined __ANDROID__ /* Android */
158 # if defined __LP64__
159 # define _gl_flags_file_t int
160 # else
161 # define _gl_flags_file_t short
162 # endif
163 # if defined __LP64__
164 # define _gl_file_offset_t int64_t
165 # else
166 /* see https://android.googlesource.com/platform/bionic/+/master/docs/32-bit-abi.md */
167 # define _gl_file_offset_t __kernel_off_t
168 # endif
169 /* Up to this commit from 2015-10-12
170 <https://android.googlesource.com/platform/bionic.git/+/f0141dfab10a4b332769d52fa76631a64741297a>
171 the innards of FILE were public,
172 see <https://android.googlesource.com/platform/bionic.git/+/e78392637d5086384a5631ddfdfa8d7ec8326ee3/libc/stdio/fileext.h>
173 and <https://android.googlesource.com/platform/bionic.git/+/e78392637d5086384a5631ddfdfa8d7ec8326ee3/libc/stdio/local.h>.
174 After this commit, the innards of FILE are hidden. */
175 # define fp_ ((struct { unsigned char *_p; \
176 int _r; \
177 int _w; \
178 _gl_flags_file_t _flags; \
179 _gl_flags_file_t _file; \
180 struct { unsigned char *_base; size_t _size; } _bf; \
181 int _lbfsize; \
182 void *_cookie; \
183 void *_close; \
184 void *_read; \
185 void *_seek; \
186 void *_write; \
187 struct { unsigned char *_base; size_t _size; } _ext; \
188 unsigned char *_up; \
189 int _ur; \
190 unsigned char _ubuf[3]; \
191 unsigned char _nbuf[1]; \
192 struct { unsigned char *_base; size_t _size; } _lb; \
193 int _blksize; \
194 _gl_file_offset_t _offset; \
195 /* More fields, not relevant here. */ \
196 } *) fp)
197 # else
198 # define fp_ fp
199 # endif
200
201 # if (defined __NetBSD__ && __NetBSD_Version__ >= 105270000) || defined __minix /* NetBSD >= 1.5ZA, Minix 3 */
202 /* See <https://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup>
203 and <https://github.com/Stichting-MINIX-Research-Foundation/minix/blob/master/lib/libc/stdio/fileext.h> */
204 struct __sfileext
205 {
206 struct __sbuf _ub; /* ungetc buffer */
207 /* More fields, not relevant here. */
208 };
209 # define fp_ub ((struct __sfileext *) fp->_ext._base)->_ub
210 # elif defined __ANDROID__ || defined __OpenBSD__ /* Android, OpenBSD */
211 struct __sfileext
212 {
213 struct { unsigned char *_base; size_t _size; } _ub; /* ungetc buffer */
214 /* More fields, not relevant here. */
215 };
216 # define fp_ub ((struct __sfileext *) fp_->_ext._base)->_ub
217 # else /* FreeBSD, NetBSD <= 1.5Z, DragonFly, Mac OS X, Cygwin */
218 # define fp_ub fp_->_ub
219 # endif
220
221 # define HASUB(fp) (fp_ub._base != NULL)
222
223 # if defined __ANDROID__ || defined __OpenBSD__ /* Android, OpenBSD */
224 /* Needed after this Android commit from 2016-01-25
225 <https://android.googlesource.com/platform/bionic.git/+/e70e0e9267d069bf56a5078c99307e08a7280de7>
226 And after this OpenBSD commit from 2025-07-16
227 <https://github.com/openbsd/src/commit/b7f6c2eb760a2da367dd51d539ef06f5f3553790>. */
228 # ifndef __SEOF
229 # define __SLBF 1
230 # define __SNBF 2
231 # define __SRD 4
232 # define __SWR 8
233 # define __SRW 0x10
234 # define __SEOF 0x20
235 # define __SERR 0x40
236 # endif
237 # ifndef __SOFF
238 # define __SOFF 0x1000
239 # endif
240 # endif
241
242 #endif
243
244
245 /* SystemV derived implementations. */
246
247 #ifdef __TANDEM /* NonStop Kernel */
248 # ifndef _IOERR
249 /* These values were determined by the program 'stdioext-flags' at
250 <https://lists.gnu.org/r/bug-gnulib/2010-12/msg00165.html>. */
251 # define _IOERR 0x40
252 # define _IOREAD 0x80
253 # define _IOWRT 0x4
254 # define _IORW 0x100
255 # endif
256 #endif
257
258 #if defined _IOERR
259
260 # if defined __sun && defined _LP64 /* Solaris/{SPARC,AMD64} 64-bit */
261 # define fp_ ((struct { unsigned char *_ptr; \
262 unsigned char *_base; \
263 unsigned char *_end; \
264 long _cnt; \
265 int _file; \
266 unsigned int _flag; \
267 } *) fp)
268 # elif defined __VMS /* OpenVMS */
269 # define fp_ ((struct _iobuf *) fp)
270 # else
271 # define fp_ fp
272 # endif
273
274 # if defined _SCO_DS || (defined __SCO_VERSION__ || defined __sysv5__) /* OpenServer 5, OpenServer 6, UnixWare 7 */
275 # define _cnt __cnt
276 # define _ptr __ptr
277 # define _base __base
278 # define _flag __flag
279 # endif
280
281 #elif defined _WIN32 && ! defined __CYGWIN__ /* newer Windows with MSVC */
282
283 /* <stdio.h> does not define the innards of FILE any more. */
284 # define WINDOWS_OPAQUE_FILE
285
286 struct _gl_real_FILE
287 {
288 /* Note: Compared to older Windows and to mingw, it has the fields
289 _base and _cnt swapped. */
290 unsigned char *_ptr;
291 unsigned char *_base;
292 int _cnt;
293 int _flag;
294 int _file;
295 int _charbuf;
296 int _bufsiz;
297 };
298 # define fp_ ((struct _gl_real_FILE *) fp)
299
300 /* These values were determined by a program similar to the one at
301 <https://lists.gnu.org/r/bug-gnulib/2010-12/msg00165.html>. */
302 # define _IOREAD 0x1
303 # define _IOWRT 0x2
304 # define _IORW 0x4
305 # define _IOEOF 0x8
306 # define _IOERR 0x10
307
308 #endif