1 | /* Implementation details of FILE streams. |
2 | Copyright (C) 2007-2008, 2010-2019 Free Software Foundation, Inc. |
3 | |
4 | This program is free software: you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; either version 3 of the License, or |
7 | (at your option) any later version. |
8 | |
9 | This program 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 General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU 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_IN_BACKUP private. For now, work around this |
22 | problem by defining it ourselves. FIXME: Do not rely on glibc |
23 | internals. */ |
24 | #if !defined _IO_IN_BACKUP && defined _IO_EOF_SEEN |
25 | # define _IO_IN_BACKUP 0x100 |
26 | #endif |
27 | |
28 | /* BSD stdio derived implementations. */ |
29 | |
30 | #if defined __NetBSD__ /* NetBSD */ |
31 | /* Get __NetBSD_Version__. */ |
32 | # include <sys/param.h> |
33 | #endif |
34 | |
35 | #include <errno.h> /* For detecting Plan9. */ |
36 | |
37 | #if defined __sferror || defined __DragonFly__ || defined __ANDROID__ |
38 | /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */ |
39 | |
40 | # if defined __DragonFly__ /* DragonFly */ |
41 | /* See <https://gitweb.dragonflybsd.org/dragonfly.git/blob_plain/HEAD:/lib/libc/stdio/priv_stdio.h>. */ |
42 | # define fp_ ((struct { struct __FILE_public pub; \ |
43 | struct { unsigned char *_base; int _size; } _bf; \ |
44 | void *cookie; \ |
45 | void *_close; \ |
46 | void *_read; \ |
47 | void *_seek; \ |
48 | void *_write; \ |
49 | struct { unsigned char *_base; int _size; } _ub; \ |
50 | int _ur; \ |
51 | unsigned char _ubuf[3]; \ |
52 | unsigned char _nbuf[1]; \ |
53 | struct { unsigned char *_base; int _size; } _lb; \ |
54 | int _blksize; \ |
55 | fpos_t _offset; \ |
56 | /* More fields, not relevant here. */ \ |
57 | } *) fp) |
58 | /* See <https://gitweb.dragonflybsd.org/dragonfly.git/blob_plain/HEAD:/include/stdio.h>. */ |
59 | # define _p pub._p |
60 | # define _flags pub._flags |
61 | # define _r pub._r |
62 | # define _w pub._w |
63 | # elif defined __ANDROID__ /* Android */ |
64 | # ifdef __LP64__ |
65 | # define _gl_flags_file_t int |
66 | # else |
67 | # define _gl_flags_file_t short |
68 | # endif |
69 | /* Up to this commit from 2015-10-12 |
70 | <https://android.googlesource.com/platform/bionic.git/+/f0141dfab10a4b332769d52fa76631a64741297a> |
71 | the innards of FILE were public, and fp_ub could be defined like for OpenBSD, |
72 | see <https://android.googlesource.com/platform/bionic.git/+/e78392637d5086384a5631ddfdfa8d7ec8326ee3/libc/stdio/fileext.h> |
73 | and <https://android.googlesource.com/platform/bionic.git/+/e78392637d5086384a5631ddfdfa8d7ec8326ee3/libc/stdio/local.h>. |
74 | After this commit, the innards of FILE are hidden. */ |
75 | # define fp_ ((struct { unsigned char *_p; \ |
76 | int _r; \ |
77 | int _w; \ |
78 | _gl_flags_file_t _flags; \ |
79 | _gl_flags_file_t _file; \ |
80 | struct { unsigned char *_base; size_t _size; } _bf; \ |
81 | int _lbfsize; \ |
82 | void *_cookie; \ |
83 | void *_close; \ |
84 | void *_read; \ |
85 | void *_seek; \ |
86 | void *_write; \ |
87 | struct { unsigned char *_base; size_t _size; } _ext; \ |
88 | unsigned char *_up; \ |
89 | int _ur; \ |
90 | unsigned char _ubuf[3]; \ |
91 | unsigned char _nbuf[1]; \ |
92 | struct { unsigned char *_base; size_t _size; } _lb; \ |
93 | int _blksize; \ |
94 | fpos_t _offset; \ |
95 | /* More fields, not relevant here. */ \ |
96 | } *) fp) |
97 | # else |
98 | # define fp_ fp |
99 | # endif |
100 | |
101 | # if (defined __NetBSD__ && __NetBSD_Version__ >= 105270000) || defined __OpenBSD__ || defined __minix /* NetBSD >= 1.5ZA, OpenBSD, Minix 3 */ |
102 | /* See <http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup> |
103 | and <https://cvsweb.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup> |
104 | and <https://github.com/Stichting-MINIX-Research-Foundation/minix/blob/master/lib/libc/stdio/fileext.h> */ |
105 | struct __sfileext |
106 | { |
107 | struct __sbuf _ub; /* ungetc buffer */ |
108 | /* More fields, not relevant here. */ |
109 | }; |
110 | # define fp_ub ((struct __sfileext *) fp->_ext._base)->_ub |
111 | # elif defined __ANDROID__ /* Android */ |
112 | struct __sfileext |
113 | { |
114 | struct { unsigned char *_base; size_t _size; } _ub; /* ungetc buffer */ |
115 | /* More fields, not relevant here. */ |
116 | }; |
117 | # define fp_ub ((struct __sfileext *) fp_->_ext._base)->_ub |
118 | # else /* FreeBSD, NetBSD <= 1.5Z, DragonFly, Mac OS X, Cygwin */ |
119 | # define fp_ub fp_->_ub |
120 | # endif |
121 | |
122 | # define HASUB(fp) (fp_ub._base != NULL) |
123 | |
124 | # if defined __ANDROID__ /* Android */ |
125 | /* Needed after this commit from 2016-01-25 |
126 | <https://android.googlesource.com/platform/bionic.git/+/e70e0e9267d069bf56a5078c99307e08a7280de7> */ |
127 | # ifndef __SEOF |
128 | # define __SLBF 1 |
129 | # define __SNBF 2 |
130 | # define __SRD 4 |
131 | # define __SWR 8 |
132 | # define __SRW 0x10 |
133 | # define __SEOF 0x20 |
134 | # define __SERR 0x40 |
135 | # endif |
136 | # ifndef __SOFF |
137 | # define __SOFF 0x1000 |
138 | # endif |
139 | # endif |
140 | |
141 | #endif |
142 | |
143 | |
144 | /* SystemV derived implementations. */ |
145 | |
146 | #ifdef __TANDEM /* NonStop Kernel */ |
147 | # ifndef _IOERR |
148 | /* These values were determined by the program 'stdioext-flags' at |
149 | <https://lists.gnu.org/r/bug-gnulib/2010-12/msg00165.html>. */ |
150 | # define _IOERR 0x40 |
151 | # define _IOREAD 0x80 |
152 | # define _IOWRT 0x4 |
153 | # define _IORW 0x100 |
154 | # endif |
155 | #endif |
156 | |
157 | #if defined _IOERR |
158 | |
159 | # if defined __sun && defined _LP64 /* Solaris/{SPARC,AMD64} 64-bit */ |
160 | # define fp_ ((struct { unsigned char *_ptr; \ |
161 | unsigned char *_base; \ |
162 | unsigned char *_end; \ |
163 | long _cnt; \ |
164 | int _file; \ |
165 | unsigned int _flag; \ |
166 | } *) fp) |
167 | # elif defined __VMS /* OpenVMS */ |
168 | # define fp_ ((struct _iobuf *) fp) |
169 | # else |
170 | # define fp_ fp |
171 | # endif |
172 | |
173 | # if defined _SCO_DS /* OpenServer */ |
174 | # define _cnt __cnt |
175 | # define _ptr __ptr |
176 | # define _base __base |
177 | # define _flag __flag |
178 | # endif |
179 | |
180 | #elif defined _WIN32 && ! defined __CYGWIN__ /* newer Windows with MSVC */ |
181 | |
182 | /* <stdio.h> does not define the innards of FILE any more. */ |
183 | # define WINDOWS_OPAQUE_FILE |
184 | |
185 | struct _gl_real_FILE |
186 | { |
187 | /* Note: Compared to older Windows and to mingw, it has the fields |
188 | _base and _cnt swapped. */ |
189 | unsigned char *_ptr; |
190 | unsigned char *_base; |
191 | int _cnt; |
192 | int _flag; |
193 | int _file; |
194 | int _charbuf; |
195 | int _bufsiz; |
196 | }; |
197 | # define fp_ ((struct _gl_real_FILE *) fp) |
198 | |
199 | /* These values were determined by a program similar to the one at |
200 | <https://lists.gnu.org/r/bug-gnulib/2010-12/msg00165.html>. */ |
201 | # define _IOREAD 0x1 |
202 | # define _IOWRT 0x2 |
203 | # define _IORW 0x4 |
204 | # define _IOEOF 0x8 |
205 | # define _IOERR 0x10 |
206 | |
207 | #endif |
208 | |