xref: /freebsd/contrib/llvm-project/libcxx/src/fstream.cpp (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <__config>
10 #include <cstdio>
11 #include <fstream>
12 
13 #if defined(_LIBCPP_WIN32API)
14 #  define WIN32_LEAN_AND_MEAN
15 #  define NOMINMAX
16 #  include <io.h>
17 #  include <windows.h>
18 #endif
19 
20 _LIBCPP_BEGIN_NAMESPACE_STD
21 
22 #if defined(_LIBCPP_WIN32API)
23 
24 // Confirm that `HANDLE` is `void*` as implemented in `basic_filebuf`
25 static_assert(std::same_as<HANDLE, void*>);
26 
27 _LIBCPP_EXPORTED_FROM_ABI void* __filebuf_windows_native_handle(FILE* __file) noexcept {
28   // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=msvc-170
29   intptr_t __handle = _get_osfhandle(fileno(__file));
30   if (__handle == -1)
31     return nullptr;
32   return reinterpret_cast<void*>(__handle);
33 }
34 
35 #endif
36 
37 _LIBCPP_END_NAMESPACE_STD
38