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_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
22
23#if defined(_LIBCPP_WIN32API)
24
25// Confirm that `HANDLE` is `void*` as implemented in `basic_filebuf`
26static_assert(std::same_as<HANDLE, void*>);
27
28_LIBCPP_EXPORTED_FROM_ABI void* __filebuf_windows_native_handle(FILE* __file) noexcept {
29 // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=msvc-170
30 intptr_t __handle = _get_osfhandle(fileno(__file));
31 if (__handle == -1)
32 return nullptr;
33 return reinterpret_cast<void*>(__handle);
34}
35
36#endif
37
38_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
39_LIBCPP_END_NAMESPACE_STD
40