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 //===----------------------------------------------------------------------===//
14 #include <__system_error/system_error.h>
16 #include "filesystem/error.h"
18 #if defined(_LIBCPP_WIN32API)
19 # define WIN32_LEAN_AND_MEAN
23 #elif __has_include(<unistd.h>)
27 _LIBCPP_BEGIN_NAMESPACE_STD
29 #if defined(_LIBCPP_WIN32API)
31 _LIBCPP_EXPORTED_FROM_ABI
bool __is_windows_terminal(FILE* __stream
) {
32 // Note the Standard does this in one call, but it's unclear whether
33 // an invalid handle is allowed when calling GetConsoleMode.
35 // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=msvc-170
36 // https://learn.microsoft.com/en-us/windows/console/getconsolemode
37 intptr_t __handle
= _get_osfhandle(fileno(__stream
));
42 return GetConsoleMode(reinterpret_cast<void*>(__handle
), &__mode
);
45 # if _LIBCPP_HAS_WIDE_CHARACTERS
46 _LIBCPP_EXPORTED_FROM_ABI
void
47 __write_to_windows_console([[maybe_unused
]] FILE* __stream
, [[maybe_unused
]] wstring_view __view
) {
48 // https://learn.microsoft.com/en-us/windows/console/writeconsole
49 if (WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fileno(__stream
))), // clang-format aid
54 __throw_system_error(filesystem::detail::make_windows_error(GetLastError()), "failed to write formatted output");
57 # endif // _LIBCPP_HAS_WIDE_CHARACTERS
59 #elif __has_include(<unistd.h>) // !_LIBCPP_WIN32API
61 _LIBCPP_EXPORTED_FROM_ABI
bool __is_posix_terminal(FILE* __stream
) { return isatty(fileno(__stream
)); }
64 _LIBCPP_END_NAMESPACE_STD