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