1*b1e83836Smrg /* Windows support code to wrap differences between different
2*b1e83836Smrg versions of the Microsoft C libaries.
3*b1e83836Smrg Copyright (C) 2021-2022 Free Software Foundation, Inc.
4*b1e83836Smrg
5*b1e83836Smrg This file is part of GCC.
6*b1e83836Smrg
7*b1e83836Smrg GCC is free software; you can redistribute it and/or modify it under
8*b1e83836Smrg the terms of the GNU General Public License as published by the Free
9*b1e83836Smrg Software Foundation; either version 3, or (at your option) any later
10*b1e83836Smrg version.
11*b1e83836Smrg
12*b1e83836Smrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*b1e83836Smrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*b1e83836Smrg FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15*b1e83836Smrg for more details.
16*b1e83836Smrg
17*b1e83836Smrg Under Section 7 of GPL version 3, you are granted additional
18*b1e83836Smrg permissions described in the GCC Runtime Library Exception, version
19*b1e83836Smrg 3.1, as published by the Free Software Foundation.
20*b1e83836Smrg
21*b1e83836Smrg You should have received a copy of the GNU General Public License and
22*b1e83836Smrg a copy of the GCC Runtime Library Exception along with this program;
23*b1e83836Smrg see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24*b1e83836Smrg <http://www.gnu.org/licenses/>. */
25*b1e83836Smrg
26*b1e83836Smrg #ifdef __MINGW32__
27*b1e83836Smrg #include <_mingw.h>
28*b1e83836Smrg #endif
29*b1e83836Smrg #include <stdio.h>
30*b1e83836Smrg
31*b1e83836Smrg /* The D runtime library defines stdin, stdout, and stderr as extern(C) symbols
32*b1e83836Smrg in the core.stdc.stdio module, and require initializing at start-up. */
33*b1e83836Smrg __attribute__((weakref ("stdin")))
34*b1e83836Smrg static FILE *core_stdc_stdin;
35*b1e83836Smrg
36*b1e83836Smrg __attribute__((weakref ("stdout")))
37*b1e83836Smrg static FILE *core_stdc_stdout;
38*b1e83836Smrg
39*b1e83836Smrg __attribute__((weakref ("stderr")))
40*b1e83836Smrg static FILE *core_stdc_stderr;
41*b1e83836Smrg
42*b1e83836Smrg /* Set to 1 if runtime is using libucrt.dll. */
43*b1e83836Smrg unsigned char msvcUsesUCRT;
44*b1e83836Smrg
45*b1e83836Smrg void
init_msvc(void)46*b1e83836Smrg init_msvc (void)
47*b1e83836Smrg {
48*b1e83836Smrg core_stdc_stdin = stdin;
49*b1e83836Smrg core_stdc_stdout = stdout;
50*b1e83836Smrg core_stdc_stderr = stderr;
51*b1e83836Smrg
52*b1e83836Smrg #if __MSVCRT_VERSION__ >= 0xE00
53*b1e83836Smrg msvcUsesUCRT = 1;
54*b1e83836Smrg #endif
55*b1e83836Smrg }
56*b1e83836Smrg
57*b1e83836Smrg /* Phobos std.stdio module assumes these functions are present at link time,
58*b1e83836Smrg and not absent or macros. */
59*b1e83836Smrg #ifdef _fgetc_nolock
60*b1e83836Smrg #undef _fgetc_nolock
61*b1e83836Smrg
62*b1e83836Smrg int
_fgetc_nolock(FILE * fp)63*b1e83836Smrg _fgetc_nolock (FILE *fp)
64*b1e83836Smrg {
65*b1e83836Smrg fp->_cnt--;
66*b1e83836Smrg if (fp->_cnt >= 0)
67*b1e83836Smrg {
68*b1e83836Smrg const int c = *fp->_ptr;
69*b1e83836Smrg fp->_ptr++;
70*b1e83836Smrg return c & 0xff;
71*b1e83836Smrg }
72*b1e83836Smrg else
73*b1e83836Smrg return _filbuf (fp);
74*b1e83836Smrg }
75*b1e83836Smrg
76*b1e83836Smrg #endif /* _fgetc_nolock */
77*b1e83836Smrg
78*b1e83836Smrg #ifdef _fputc_nolock
79*b1e83836Smrg #undef _fputc_nolock
80*b1e83836Smrg
81*b1e83836Smrg int
_fputc_nolock(int c,FILE * fp)82*b1e83836Smrg _fputc_nolock (int c, FILE *fp)
83*b1e83836Smrg {
84*b1e83836Smrg fp->_cnt--;
85*b1e83836Smrg if (fp->_cnt >= 0)
86*b1e83836Smrg {
87*b1e83836Smrg *fp->_ptr = (char) c;
88*b1e83836Smrg fp->_ptr++;
89*b1e83836Smrg return c & 0xff;
90*b1e83836Smrg }
91*b1e83836Smrg else
92*b1e83836Smrg return _flsbuf (c, fp);
93*b1e83836Smrg }
94*b1e83836Smrg
95*b1e83836Smrg #endif /* _fputc_nolock */
96*b1e83836Smrg
97*b1e83836Smrg #ifdef rewind
98*b1e83836Smrg #undef rewind
99*b1e83836Smrg
100*b1e83836Smrg void
rewind(FILE * fp)101*b1e83836Smrg rewind (FILE *fp)
102*b1e83836Smrg {
103*b1e83836Smrg fseek (fp, 0, SEEK_SET);
104*b1e83836Smrg fp->_flag &= ~_IOERR;
105*b1e83836Smrg }
106*b1e83836Smrg
107*b1e83836Smrg #endif /* rewind */
108*b1e83836Smrg
109*b1e83836Smrg #ifdef clearerr
110*b1e83836Smrg #undef clearerr
111*b1e83836Smrg
112*b1e83836Smrg void
clearerr(FILE * fp)113*b1e83836Smrg clearerr (FILE *fp)
114*b1e83836Smrg {
115*b1e83836Smrg fp->_flag &= ~(_IOERR | _IOEOF);
116*b1e83836Smrg }
117*b1e83836Smrg
118*b1e83836Smrg #endif /* clearerr */
119*b1e83836Smrg
120*b1e83836Smrg #ifdef feof
121*b1e83836Smrg #undef feof
122*b1e83836Smrg
123*b1e83836Smrg int
feof(FILE * fp)124*b1e83836Smrg feof (FILE *fp)
125*b1e83836Smrg {
126*b1e83836Smrg return fp->_flag & _IOEOF;
127*b1e83836Smrg }
128*b1e83836Smrg
129*b1e83836Smrg #endif /* feof */
130*b1e83836Smrg
131*b1e83836Smrg #ifdef ferror
132*b1e83836Smrg #undef ferror
133*b1e83836Smrg
134*b1e83836Smrg int
ferror(FILE * fp)135*b1e83836Smrg ferror (FILE *fp)
136*b1e83836Smrg {
137*b1e83836Smrg return fp->_flag & _IOERR;
138*b1e83836Smrg }
139*b1e83836Smrg
140*b1e83836Smrg #endif /* ferror */
141*b1e83836Smrg
142*b1e83836Smrg #ifdef fileno
143*b1e83836Smrg #undef fileno
144*b1e83836Smrg
145*b1e83836Smrg int
fileno(FILE * fp)146*b1e83836Smrg fileno (FILE *fp)
147*b1e83836Smrg {
148*b1e83836Smrg return fp->_file;
149*b1e83836Smrg }
150*b1e83836Smrg
151*b1e83836Smrg #endif /* fileno */
152*b1e83836Smrg
153*b1e83836Smrg /* Phobos std.stdio module has a dependency on the UCRT library, so provide
154*b1e83836Smrg stubs that forward to the nearest equivalent. */
155*b1e83836Smrg #if __MSVCRT_VERSION__ < 0x800
156*b1e83836Smrg
157*b1e83836Smrg wint_t
_fgetwc_nolock(FILE * fp)158*b1e83836Smrg _fgetwc_nolock (FILE *fp)
159*b1e83836Smrg {
160*b1e83836Smrg return fgetwc (fp);
161*b1e83836Smrg }
162*b1e83836Smrg
163*b1e83836Smrg wint_t
_fputwc_nolock(wchar_t c,FILE * fp)164*b1e83836Smrg _fputwc_nolock (wchar_t c, FILE *fp)
165*b1e83836Smrg {
166*b1e83836Smrg return fputwc(c, fp);
167*b1e83836Smrg }
168*b1e83836Smrg
169*b1e83836Smrg #endif /* __MSVCRT_VERSION__ < 0x800*/
170