stdio-impl.h 7.8 KB

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