Skip to content

Commit a1a9925

Browse files
committed
[libc++] Guard fileno() and isatty() usage correctly.
See [1] and [2]. isatty() is part of unistd.h, but fileno() is in stdio.h, and is guarded by `_POSIX_C_SOURCE >= 1` (on my machine, `man 3 fileno` only requires `defined(_POSIX_C_SOURCE)`, though). I ran into this issue while trying to compile libc++ for a bare metal environment with Newlib as the libc. Newlib defines fileno() in stdio.h, and its unistd.h does not include stdio.h. [1]: https://linux.die.net/man/3/fileno [2]: https://linux.die.net/man/3/isatty
1 parent efe8573 commit a1a9925

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

libcxx/src/print.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
# define NOMINMAX
2121
# include <io.h>
2222
# include <windows.h>
23-
#elif __has_include(<unistd.h>)
23+
#elif __has_include(<unistd.h>) && __has_include(<stdio.h>) && \
24+
defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 1
25+
# include <stdio.h>
2426
# include <unistd.h>
27+
# define HAS_FILENO_AND_ISATTY
2528
#endif
2629

2730
_LIBCPP_BEGIN_NAMESPACE_STD
@@ -56,7 +59,7 @@ __write_to_windows_console([[maybe_unused]] FILE* __stream, [[maybe_unused]] wst
5659
}
5760
# endif // _LIBCPP_HAS_WIDE_CHARACTERS
5861

59-
#elif __has_include(<unistd.h>) // !_LIBCPP_WIN32API
62+
#elif defined(HAS_FILENO_AND_ISATTY) // !_LIBCPP_WIN32API
6063

6164
_LIBCPP_EXPORTED_FROM_ABI bool __is_posix_terminal(FILE* __stream) { return isatty(fileno(__stream)); }
6265
#endif

0 commit comments

Comments
 (0)