[PATCH 02/13] io: replace local_isatty() with a proper function __isatty_nostatus()

H. Peter Anvin hpa@zytor.com
Tue Jun 3 00:03:01 GMT 2025


From: "H. Peter Anvin" (Intel) <hpa@zytor.com>

Replace local_isatty() inlined in libio with a proper function
__isatty_nostatus(). This allows simpler system-specific
implementations that don't need to touch errno at all.

Note: I left the prototype in include/unistd.h (the internal header
file.) It didn't much make sense to me to put it in a different header
(not-cancel.h), but perhaps someone can elucidate the need.

Add such an implementation for Linux, with a generic fallback.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
---
 include/unistd.h                          |  1 +
 io/Makefile                               |  1 +
 io/isatty_nostatus.c                      | 29 +++++++++++++++++++++++
 libio/filedoalloc.c                       | 12 +---------
 sysdeps/unix/sysv/linux/isatty_nostatus.c | 29 +++++++++++++++++++++++
 5 files changed, 61 insertions(+), 11 deletions(-)
 create mode 100644 io/isatty_nostatus.c
 create mode 100644 sysdeps/unix/sysv/linux/isatty_nostatus.c

diff --git a/include/unistd.h b/include/unistd.h
index e241603b8131..376ab5a93688 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -152,6 +152,7 @@ libc_hidden_proto (__ttyname_r)
 extern __pid_t _Fork (void);
 libc_hidden_proto (_Fork);
 extern int __isatty (int __fd) attribute_hidden;
+extern int __isatty_nostatus (int __fd) attribute_hidden;
 extern int __link (const char *__from, const char *__to);
 extern int __symlink (const char *__from, const char *__to);
 extern int __symlinkat (const char *__from, int __fd, const char *__to);
diff --git a/io/Makefile b/io/Makefile
index e06f3cb3dba1..edee38e233d8 100644
--- a/io/Makefile
+++ b/io/Makefile
@@ -92,6 +92,7 @@ routines := \
   getdirname \
   getwd \
   isatty \
+  isatty_nostatus \
   lchmod \
   lchown \
   link \
diff --git a/io/isatty_nostatus.c b/io/isatty_nostatus.c
new file mode 100644
index 000000000000..e8ee796f3ba2
--- /dev/null
+++ b/io/isatty_nostatus.c
@@ -0,0 +1,29 @@
+/* Copyright (C) 1991-2025 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <errno.h>
+#include <unistd.h>
+
+/* Return 1 if FD is a terminal, 0 if not, without changing errno  */
+int
+__isatty_nostatus (int fd)
+{
+  int save_errno = errno;
+  int res = __isatty (fd);
+  __set_errno (save_errno);
+  return res;
+}
diff --git a/libio/filedoalloc.c b/libio/filedoalloc.c
index 9ddd75b42923..f360d96a9b57 100644
--- a/libio/filedoalloc.c
+++ b/libio/filedoalloc.c
@@ -61,16 +61,6 @@
 #include <stdlib.h>
 #include <unistd.h>
 
-/* Return the result of isatty, without changing errno.  */
-static int
-local_isatty (int fd)
-{
-  int save_errno = errno;
-  int res = __isatty (fd);
-  __set_errno (save_errno);
-  return res;
-}
-
 /* Allocate a file buffer, or switch to unbuffered I/O.  Streams for
    TTY devices default to line buffered.  */
 int
@@ -90,7 +80,7 @@ _IO_file_doallocate (FILE *fp)
 #ifdef DEV_TTY_P
 	      DEV_TTY_P (&st) ||
 #endif
-	      local_isatty (fp->_fileno))
+	      __isatty_nostatus (fp->_fileno))
 	    fp->_flags |= _IO_LINE_BUF;
 	}
 #if defined _STATBUF_ST_BLKSIZE
diff --git a/sysdeps/unix/sysv/linux/isatty_nostatus.c b/sysdeps/unix/sysv/linux/isatty_nostatus.c
new file mode 100644
index 000000000000..7f110be00c0a
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/isatty_nostatus.c
@@ -0,0 +1,29 @@
+/* Copyright (C) 1991-2025 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <sysdep.h>
+#include <termios.h>
+#include <kernel_termios.h>
+#include <sys/ioctl.h>
+
+/* Return 1 if FD is a terminal, 0 if not, without changing errno  */
+int
+__isatty_nostatus (int fd)
+{
+  struct __kernel_termios k_termios;
+  return INTERNAL_SYSCALL_CALL (ioctl, fd, TCGETS, &k_termios) == 0;
+}
-- 
2.49.0



More information about the Libc-alpha mailing list