Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_isatty vs GetConsoleMode? #3003

Closed
dimztimz opened this issue Jul 27, 2022 · 1 comment
Closed

_isatty vs GetConsoleMode? #3003

dimztimz opened this issue Jul 27, 2022 · 1 comment
Labels

Comments

@dimztimz
Copy link
Contributor

In the paper there is this note:

Changes since R3:
Replaced _isatty(_fileno(stream)) with GetConsoleMode(_get_osfhandle(_fileno(stream)), ...) in a note in [format.functions] because the former may return 1 for streams not referring to a terminal.

What is the difference? Which one is more correct? Currently _isatty() is still used:

fmt/include/fmt/format-inl.h

Lines 1482 to 1498 in 81f1cc7

FMT_FUNC bool write_console(std::FILE* f, string_view text) {
auto fd = _fileno(f);
if (_isatty(fd)) {
detail::utf8_to_utf16 u16(string_view(text.data(), text.size()));
auto written = detail::dword();
if (detail::WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fd)),
u16.c_str(), static_cast<uint32_t>(u16.size()),
&written, nullptr)) {
return true;
}
}
// We return false if the file descriptor was not TTY, or it was but
// SetConsoleW failed which can happen if the output has been redirected to
// NUL. In both cases when we return false, we should attempt to do regular
// write via fwrite or std::ostream::write.
return false;
}

Related issue #2080. Is the fact that we return false when WriteConsoleW() fails good enough solution and there is no need for GetConsoleMode?

@vitaut
Copy link
Contributor

vitaut commented Jul 28, 2022

For the purposes of the paper GetConsoleMode is the right thing because _isatty may return true when there is no console in some cases (I don't remember exactly which though). Implementation-wise it shouldn't matter because in the worst case there is extra transcoding in those cases which is then discarded but that should be uncommon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants