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