Debian Codegraph @

lib/stdio-impl.h

raw
1 /* Implementation details of FILE streams.
2 Copyright (C) 2007-2008, 2010-2025 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 __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 __ANDROID__ /* Android */
112 # ifdef __LP64__
113 # define _gl_flags_file_t int
114 # else
115 # define _gl_flags_file_t short
116 # endif
117 # ifdef __LP64__
118 # define _gl_file_offset_t int64_t
119 # else
120 /* see https://android.googlesource.com/platform/bionic/+/master/docs/32-bit-abi.md */
121 # define _gl_file_offset_t __kernel_off_t
122 # endif
123 /* Up to this commit from 2015-10-12
124 <https://android.googlesource.com/platform/bionic.git/+/f0141dfab10a4b332769d52fa76631a64741297a>
125 the innards of FILE were public, and fp_ub could be defined like for OpenBSD,
126 see <https://android.googlesource.com/platform/bionic.git/+/e78392637d5086384a5631ddfdfa8d7ec8326ee3/libc/stdio/fileext.h>
127 and <https://android.googlesource.com/platform/bionic.git/+/e78392637d5086384a5631ddfdfa8d7ec8326ee3/libc/stdio/local.h>.
128 After this commit, the innards of FILE are hidden. */
129 # define fp_ ((struct { unsigned char *_p; \
130 int _r; \
131 int _w; \
132 _gl_flags_file_t _flags; \
133 _gl_flags_file_t _file; \
134 struct { unsigned char *_base; size_t _size; } _bf; \
135 int _lbfsize; \
136 void *_cookie; \
137 void *_close; \
138 void *_read; \
139 void *_seek; \
140 void *_write; \
141 struct { unsigned char *_base; size_t _size; } _ext; \
142 unsigned char *_up; \
143 int _ur; \
144 unsigned char _ubuf[3]; \
145 unsigned char _nbuf[1]; \
146 struct { unsigned char *_base; size_t _size; } _lb; \
147 int _blksize; \
148 _gl_file_offset_t _offset; \
149 /* More fields, not relevant here. */ \
150 } *) fp)
151 # else
152 # define fp_ fp
153 # endif
154
155 # if (defined __NetBSD__ && __NetBSD_Version__ >= 105270000) || defined __OpenBSD__ || defined __minix /* NetBSD >= 1.5ZA, OpenBSD, Minix 3 */
156 /* See <https://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup>
157 and <https://cvsweb.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup>
158 and <https://github.com/Stichting-MINIX-Research-Foundation/minix/blob/master/lib/libc/stdio/fileext.h> */
159 struct __sfileext
160 {
161 struct __sbuf _ub; /* ungetc buffer */
162 /* More fields, not relevant here. */
163 };
164 # define fp_ub ((struct __sfileext *) fp->_ext._base)->_ub
165 # elif defined __ANDROID__ /* Android */
166 struct __sfileext
167 {
168 struct { unsigned char *_base; size_t _size; } _ub; /* ungetc buffer */
169 /* More fields, not relevant here. */
170 };
171 # define fp_ub ((struct __sfileext *) fp_->_ext._base)->_ub
172 # else /* FreeBSD, NetBSD <= 1.5Z, DragonFly, Mac OS X, Cygwin */
173 # define fp_ub fp_->_ub
174 # endif
175
176 # define HASUB(fp) (fp_ub._base != NULL)
177
178 # if defined __ANDROID__ /* Android */
179 /* Needed after this commit from 2016-01-25
180 <https://android.googlesource.com/platform/bionic.git/+/e70e0e9267d069bf56a5078c99307e08a7280de7> */
181 # ifndef __SEOF
182 # define __SLBF 1
183 # define __SNBF 2
184 # define __SRD 4
185 # define __SWR 8
186 # define __SRW 0x10
187 # define __SEOF 0x20
188 # define __SERR 0x40
189 # endif
190 # ifndef __SOFF
191 # define __SOFF 0x1000
192 # endif
193 # endif
194
195 #endif
196
197
198 /* SystemV derived implementations. */
199
200 #ifdef __TANDEM /* NonStop Kernel */
201 # ifndef _IOERR
202 /* These values were determined by the program 'stdioext-flags' at
203 <https://lists.gnu.org/r/bug-gnulib/2010-12/msg00165.html>. */
204 # define _IOERR 0x40
205 # define _IOREAD 0x80
206 # define _IOWRT 0x4
207 # define _IORW 0x100
208 # endif
209 #endif
210
211 #if defined _IOERR
212
213 # if defined __sun && defined _LP64 /* Solaris/{SPARC,AMD64} 64-bit */
214 # define fp_ ((struct { unsigned char *_ptr; \
215 unsigned char *_base; \
216 unsigned char *_end; \
217 long _cnt; \
218 int _file; \
219 unsigned int _flag; \
220 } *) fp)
221 # elif defined __VMS /* OpenVMS */
222 # define fp_ ((struct _iobuf *) fp)
223 # else
224 # define fp_ fp
225 # endif
226
227 # if defined _SCO_DS || (defined __SCO_VERSION__ || defined __sysv5__) /* OpenServer 5, OpenServer 6, UnixWare 7 */
228 # define _cnt __cnt
229 # define _ptr __ptr
230 # define _base __base
231 # define _flag __flag
232 # endif
233
234 #elif defined _WIN32 && ! defined __CYGWIN__ /* newer Windows with MSVC */
235
236 /* <stdio.h> does not define the innards of FILE any more. */
237 # define WINDOWS_OPAQUE_FILE
238
239 struct _gl_real_FILE
240 {
241 /* Note: Compared to older Windows and to mingw, it has the fields
242 _base and _cnt swapped. */
243 unsigned char *_ptr;
244 unsigned char *_base;
245 int _cnt;
246 int _flag;
247 int _file;
248 int _charbuf;
249 int _bufsiz;
250 };
251 # define fp_ ((struct _gl_real_FILE *) fp)
252
253 /* These values were determined by a program similar to the one at
254 <https://lists.gnu.org/r/bug-gnulib/2010-12/msg00165.html>. */
255 # define _IOREAD 0x1
256 # define _IOWRT 0x2
257 # define _IORW 0x4
258 # define _IOEOF 0x8
259 # define _IOERR 0x10
260
261 #endif