1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
13 #if defined(_LIBCPP_WIN32API)
14 # define WIN32_LEAN_AND_MEAN
19 # include <__system_error/system_error.h>
21 # include "filesystem/error.h"
24 _LIBCPP_BEGIN_NAMESPACE_STD
27 _LIBCPP_EXPORTED_FROM_ABI
bool __is_windows_terminal(FILE* __stream
) {
28 // Note the Standard does this in one call, but it's unclear whether
29 // an invalid handle is allowed when calling GetConsoleMode.
31 // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=msvc-170
32 // https://learn.microsoft.com/en-us/windows/console/getconsolemode
33 intptr_t __handle
= _get_osfhandle(fileno(__stream
));
38 return GetConsoleMode(reinterpret_cast<void*>(__handle
), &__mode
);
41 # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
42 _LIBCPP_EXPORTED_FROM_ABI
void
43 __write_to_windows_console([[maybe_unused
]] FILE* __stream
, [[maybe_unused
]] wstring_view __view
) {
44 // https://learn.microsoft.com/en-us/windows/console/writeconsole
45 if (WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fileno(__stream
))), // clang-format aid
50 __throw_system_error(filesystem::detail::make_windows_error(GetLastError()), "failed to write formatted output");
53 # endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
57 _LIBCPP_END_NAMESPACE_STD