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_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
37
38#if defined(_LIBCPP_WIN32API)
39
40_LIBCPP_EXPORTED_FROM_ABI bool __is_windows_terminal(FILE* __stream) {
41 // Note the Standard does this in one call, but it's unclear whether
42 // an invalid handle is allowed when calling GetConsoleMode.
43 //
44 // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=msvc-170
45 // https://learn.microsoft.com/en-us/windows/console/getconsolemode
46 intptr_t __handle = _get_osfhandle(fileno(__stream));
47 if (__handle == -1)
48 return false;
49
50 unsigned long __mode;
51 return GetConsoleMode(reinterpret_cast<void*>(__handle), &__mode);
52}
53
54# if _LIBCPP_HAS_WIDE_CHARACTERS
55_LIBCPP_EXPORTED_FROM_ABI void
56__write_to_windows_console([[maybe_unused]] FILE* __stream, [[maybe_unused]] wstring_view __view) {
57 // https://learn.microsoft.com/en-us/windows/console/writeconsole
58 if (WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fileno(__stream))), // clang-format aid
59 __view.data(),
60 __view.size(),
61 nullptr,
62 nullptr) == 0) {
63 std::__throw_system_error(filesystem::detail::get_last_error(), "failed to write formatted output");
64 }
65}
66# endif // _LIBCPP_HAS_WIDE_CHARACTERS
67
68#elif defined(HAS_FILENO_AND_ISATTY) && _LIBCPP_AVAILABILITY_MINIMUM_HEADER_VERSION < 23 // !_LIBCPP_WIN32API
69
70_LIBCPP_DIAGNOSTIC_PUSH
71_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wmissing-prototypes")
72_LIBCPP_EXPORTED_FROM_ABI bool __is_posix_terminal(FILE* __stream) { return isatty(fd: fileno(__stream)); }
73_LIBCPP_DIAGNOSTIC_POP
74#endif
75
76_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
77_LIBCPP_END_NAMESPACE_STD
78