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
11#include <cstdlib>
12#include <print>
13
14#include <__system_error/system_error.h>
15
16#include "filesystem/error.h"
17
18#if defined(_LIBCPP_WIN32API)
19# define WIN32_LEAN_AND_MEAN
20# define NOMINMAX
21# include <io.h>
22# include <windows.h>
23#elif __has_include(<unistd.h>)
24# include <unistd.h>
25# if defined(_NEWLIB_VERSION)
26# if defined(_POSIX_C_SOURCE) && __has_include(<stdio.h>)
27# include <stdio.h>
28# define HAS_FILENO_AND_ISATTY
29# endif
30# else
31# define HAS_FILENO_AND_ISATTY
32# endif
33#endif
34
35_LIBCPP_BEGIN_NAMESPACE_STD
36
37#if defined(_LIBCPP_WIN32API)
38
39_LIBCPP_EXPORTED_FROM_ABI bool __is_windows_terminal(FILE* __stream) {
40 // Note the Standard does this in one call, but it's unclear whether
41 // an invalid handle is allowed when calling GetConsoleMode.
42 //
43 // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=msvc-170
44 // https://learn.microsoft.com/en-us/windows/console/getconsolemode
45 intptr_t __handle = _get_osfhandle(fileno(__stream));
46 if (__handle == -1)
47 return false;
48
49 unsigned long __mode;
50 return GetConsoleMode(reinterpret_cast<void*>(__handle), &__mode);
51}
52
53# if _LIBCPP_HAS_WIDE_CHARACTERS
54_LIBCPP_EXPORTED_FROM_ABI void
55__write_to_windows_console([[maybe_unused]] FILE* __stream, [[maybe_unused]] wstring_view __view) {
56 // https://learn.microsoft.com/en-us/windows/console/writeconsole
57 if (WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fileno(__stream))), // clang-format aid
58 __view.data(),
59 __view.size(),
60 nullptr,
61 nullptr) == 0) {
62 std::__throw_system_error(filesystem::detail::get_last_error(), "failed to write formatted output");
63 }
64}
65# endif // _LIBCPP_HAS_WIDE_CHARACTERS
66
67#elif defined(HAS_FILENO_AND_ISATTY) // !_LIBCPP_WIN32API
68
69_LIBCPP_EXPORTED_FROM_ABI bool __is_posix_terminal(FILE* __stream) { return isatty(fd: fileno(__stream)); }
70#endif
71
72_LIBCPP_END_NAMESPACE_STD
73