(root)/
gcc-13.2.0/
libphobos/
libdruntime/
config/
mingw/
msvc.c
       1  /* Windows support code to wrap differences between different
       2     versions of the Microsoft C libaries.
       3     Copyright (C) 2021-2023 Free Software Foundation, Inc.
       4  
       5  This file is part of GCC.
       6  
       7  GCC is free software; you can redistribute it and/or modify it under
       8  the terms of the GNU General Public License as published by the Free
       9  Software Foundation; either version 3, or (at your option) any later
      10  version.
      11  
      12  GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13  WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15  for more details.
      16  
      17  Under Section 7 of GPL version 3, you are granted additional
      18  permissions described in the GCC Runtime Library Exception, version
      19  3.1, as published by the Free Software Foundation.
      20  
      21  You should have received a copy of the GNU General Public License and
      22  a copy of the GCC Runtime Library Exception along with this program;
      23  see 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")))
      34  static FILE *core_stdc_stdin;
      35  
      36  __attribute__((weakref ("stdout")))
      37  static FILE *core_stdc_stdout;
      38  
      39  __attribute__((weakref ("stderr")))
      40  static FILE *core_stdc_stderr;
      41  
      42  /* Set to 1 if runtime is using libucrt.dll.  */
      43  unsigned char msvcUsesUCRT;
      44  
      45  void
      46  init_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  
      62  int
      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  
      81  int
      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  
     100  void
     101  rewind (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  
     112  void
     113  clearerr (FILE *fp)
     114  {
     115    fp->_flag &= ~(_IOERR | _IOEOF);
     116  }
     117  
     118  #endif /* clearerr */
     119  
     120  #ifdef feof
     121  #undef feof
     122  
     123  int
     124  feof (FILE *fp)
     125  {
     126    return fp->_flag & _IOEOF;
     127  }
     128  
     129  #endif /* feof */
     130  
     131  #ifdef ferror
     132  #undef ferror
     133  
     134  int
     135  ferror (FILE *fp)
     136  {
     137    return fp->_flag & _IOERR;
     138  }
     139  
     140  #endif /* ferror */
     141  
     142  #ifdef fileno
     143  #undef fileno
     144  
     145  int
     146  fileno (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  
     157  wint_t
     158  _fgetwc_nolock (FILE *fp)
     159  {
     160    return fgetwc (fp);
     161  }
     162  
     163  wint_t
     164  _fputwc_nolock (wchar_t c, FILE *fp)
     165  {
     166      return fputwc(c, fp);
     167  }
     168  
     169  #endif /* __MSVCRT_VERSION__ < 0x800*/