msvc.c 3.2 KB

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