[libcxx-commits] [libcxx] [libc++] Guard fileno() and isatty() usage correctly. (PR #166668)

Chenguang Wang via libcxx-commits libcxx-commits at lists.llvm.org
Thu Nov 6 22:48:43 PST 2025


https://github.com/wecing updated https://github.com/llvm/llvm-project/pull/166668

>From a1a99258e820f9c7cce5925c051f0c2c36b579a8 Mon Sep 17 00:00:00 2001
From: Chenguang Wang <w3cing at gmail.com>
Date: Wed, 5 Nov 2025 15:29:59 -0800
Subject: [PATCH 1/2] [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
---
 libcxx/src/print.cpp | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/libcxx/src/print.cpp b/libcxx/src/print.cpp
index 3f2baa6dcc60b..e7ff60ba58dac 100644
--- a/libcxx/src/print.cpp
+++ b/libcxx/src/print.cpp
@@ -20,8 +20,11 @@
 #  define NOMINMAX
 #  include <io.h>
 #  include <windows.h>
-#elif __has_include(<unistd.h>)
+#elif __has_include(<unistd.h>) && __has_include(<stdio.h>) && \
+      defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 1
+#  include <stdio.h>
 #  include <unistd.h>
+#  define HAS_FILENO_AND_ISATTY
 #endif
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -56,7 +59,7 @@ __write_to_windows_console([[maybe_unused]] FILE* __stream, [[maybe_unused]] wst
 }
 #  endif // _LIBCPP_HAS_WIDE_CHARACTERS
 
-#elif __has_include(<unistd.h>) // !_LIBCPP_WIN32API
+#elif defined(HAS_FILENO_AND_ISATTY) // !_LIBCPP_WIN32API
 
 _LIBCPP_EXPORTED_FROM_ABI bool __is_posix_terminal(FILE* __stream) { return isatty(fileno(__stream)); }
 #endif

>From b6a99b18ca70b58122737e611382ce3cee1669af Mon Sep 17 00:00:00 2001
From: Chenguang Wang <w3cing at gmail.com>
Date: Thu, 6 Nov 2025 22:48:01 -0800
Subject: [PATCH 2/2] [libcxx] special-handle Newlib in print.cpp.

---
 libcxx/src/print.cpp | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/libcxx/src/print.cpp b/libcxx/src/print.cpp
index e7ff60ba58dac..ae98e3a239e28 100644
--- a/libcxx/src/print.cpp
+++ b/libcxx/src/print.cpp
@@ -20,11 +20,16 @@
 #  define NOMINMAX
 #  include <io.h>
 #  include <windows.h>
-#elif __has_include(<unistd.h>) && __has_include(<stdio.h>) && \
-      defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 1
-#  include <stdio.h>
+#elif __has_include(<unistd.h>)
 #  include <unistd.h>
-#  define HAS_FILENO_AND_ISATTY
+#  if defined(_NEWLIB_VERSION)
+#    if defined(__POSIX_VISIBLE) && __POSIX_VISIBLE && __has_include(<stdio.h>)
+#      include <stdio.h>
+#      define HAS_FILENO_AND_ISATTY
+#    endif
+#  else
+#    define HAS_FILENO_AND_ISATTY
+#  endif
 #endif
 
 _LIBCPP_BEGIN_NAMESPACE_STD



More information about the libcxx-commits mailing list