[libc-commits] [libc] [libc] Implement tcgetwinsize in termios (PR #228435)
Aman Maurya via libc-commits
libc-commits at lists.llvm.org
Tue Oct 6 19:14:45 PDT 2026
https://github.com/amanmaurya92 updated https://github.com/llvm/llvm-project/pull/228435
>From ba959d6b1ebbba1c3f8fcbaf9512db18074a5570 Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Fri, 2 Oct 2026 17:59:33 +0530
Subject: [PATCH 1/2] [libc] Implement tcgetwinsize in termios
Implement the standard POSIX.1-2024 function tcgetwinsize in <termios.h>.
Fixes #228379
---
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/riscv/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/include/CMakeLists.txt | 1 +
libc/include/termios.yaml | 9 +++
libc/src/termios/CMakeLists.txt | 8 +++
libc/src/termios/linux/CMakeLists.txt | 15 +++++
libc/src/termios/linux/tcgetwinsize.cpp | 38 ++++++++++++
libc/src/termios/tcgetwinsize.h | 26 +++++++++
libc/test/src/termios/CMakeLists.txt | 19 ++++++
libc/test/src/termios/tcgetwinsize_test.cpp | 65 +++++++++++++++++++++
11 files changed, 184 insertions(+)
create mode 100644 libc/src/termios/linux/tcgetwinsize.cpp
create mode 100644 libc/src/termios/tcgetwinsize.h
create mode 100644 libc/test/src/termios/tcgetwinsize_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index c7ded2fade05d9..cc6b7e3a9dd0d6 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -435,6 +435,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.termios.tcflush
libc.src.termios.tcgetattr
libc.src.termios.tcgetsid
+ libc.src.termios.tcgetwinsize
libc.src.termios.tcsendbreak
libc.src.termios.tcsetattr
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 296985eeff937c..2c39ff8b78bfa2 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -454,6 +454,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.termios.tcflush
libc.src.termios.tcgetattr
libc.src.termios.tcgetsid
+ libc.src.termios.tcgetwinsize
libc.src.termios.tcsendbreak
libc.src.termios.tcsetattr
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 7928a35a6284a5..aeec2061da810a 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -474,6 +474,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.termios.tcflush
libc.src.termios.tcgetattr
libc.src.termios.tcgetsid
+ libc.src.termios.tcgetwinsize
libc.src.termios.tcsendbreak
libc.src.termios.tcsetattr
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 2fe1f92afc1d19..59deca11a9cdd2 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -1197,6 +1197,7 @@ add_header_macro(
.llvm-libc-types.pid_t
.llvm-libc-types.speed_t
.llvm-libc-types.struct_termios
+ .llvm-libc-types.struct_winsize
.llvm-libc-types.tcflag_t
)
diff --git a/libc/include/termios.yaml b/libc/include/termios.yaml
index 21ee25d700d9ae..a581610bf3ee12 100644
--- a/libc/include/termios.yaml
+++ b/libc/include/termios.yaml
@@ -10,6 +10,7 @@ types:
- type_name: speed_t
- type_name: pid_t
- type_name: cc_t
+ - type_name: struct_winsize
enums: []
objects: []
functions:
@@ -87,3 +88,11 @@ functions:
- type: int
- type: int
- type: struct termios *
+ - name: tcgetwinsize
+ standards:
+ - posix
+ return_type: int
+ arguments:
+ - type: int
+ - type: struct winsize *
+
diff --git a/libc/src/termios/CMakeLists.txt b/libc/src/termios/CMakeLists.txt
index 0fbd632b78fbda..5c2761b0f3cb27 100644
--- a/libc/src/termios/CMakeLists.txt
+++ b/libc/src/termios/CMakeLists.txt
@@ -78,3 +78,11 @@ add_entrypoint_object(
DEPENDS
.${LIBC_TARGET_OS}.tcsetattr
)
+
+add_entrypoint_object(
+ tcgetwinsize
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.tcgetwinsize
+)
+
diff --git a/libc/src/termios/linux/CMakeLists.txt b/libc/src/termios/linux/CMakeLists.txt
index 86565cf1294c7d..94f1f0e65a1ab0 100644
--- a/libc/src/termios/linux/CMakeLists.txt
+++ b/libc/src/termios/linux/CMakeLists.txt
@@ -162,3 +162,18 @@ add_entrypoint_object(
libc.src.errno.errno
libc.src.__support.macros.null_check
)
+
+add_entrypoint_object(
+ tcgetwinsize
+ SRCS
+ tcgetwinsize.cpp
+ HDRS
+ ../tcgetwinsize.h
+ DEPENDS
+ libc.hdr.types.struct_winsize
+ libc.src.__support.OSUtil.linux.syscall_wrappers.ioctl
+ libc.src.errno.errno
+ libc.src.__support.macros.config
+ libc.src.__support.macros.null_check
+)
+
diff --git a/libc/src/termios/linux/tcgetwinsize.cpp b/libc/src/termios/linux/tcgetwinsize.cpp
new file mode 100644
index 00000000000000..21910a8993780e
--- /dev/null
+++ b/libc/src/termios/linux/tcgetwinsize.cpp
@@ -0,0 +1,38 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of tcgetwinsize.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/termios/tcgetwinsize.h"
+
+#include "hdr/types/struct_winsize.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/ioctl.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
+
+#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, tcgetwinsize, (int fd, struct winsize *ws)) {
+ LIBC_CRASH_ON_NULLPTR(ws);
+
+ auto ret = linux_syscalls::ioctl(fd, TIOCGWINSZ, ws);
+ if (!ret.has_value()) {
+ libc_errno = ret.error();
+ return -1;
+ }
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/termios/tcgetwinsize.h b/libc/src/termios/tcgetwinsize.h
new file mode 100644
index 00000000000000..57b52dbdd9f5bc
--- /dev/null
+++ b/libc/src/termios/tcgetwinsize.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Header file for tcgetwinsize function.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_TERMIOS_TCGETWINSIZE_H
+#define LLVM_LIBC_SRC_TERMIOS_TCGETWINSIZE_H
+
+#include "hdr/types/struct_winsize.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int tcgetwinsize(int fd, struct winsize *ws);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_TERMIOS_TCGETWINSIZE_H
diff --git a/libc/test/src/termios/CMakeLists.txt b/libc/test/src/termios/CMakeLists.txt
index a802b3e7aba2aa..0c930bfe29592e 100644
--- a/libc/test/src/termios/CMakeLists.txt
+++ b/libc/test/src/termios/CMakeLists.txt
@@ -23,3 +23,22 @@ add_libc_test(
libc.test.UnitTest.ErrnoCheckingTest
libc.test.UnitTest.ErrnoSetterMatcher
)
+
+add_libc_test(
+ tcgetwinsize_test
+ SUITE
+ libc_termios_unittests
+ SRCS
+ tcgetwinsize_test.cpp
+ DEPENDS
+ libc.hdr.fcntl_macros
+ libc.hdr.types.struct_winsize
+ libc.src.errno.errno
+ libc.src.fcntl.open
+ libc.src.termios.tcgetwinsize
+ libc.src.unistd.close
+ libc.src.unistd.pipe
+ libc.test.UnitTest.ErrnoCheckingTest
+ libc.test.UnitTest.ErrnoSetterMatcher
+)
+
diff --git a/libc/test/src/termios/tcgetwinsize_test.cpp b/libc/test/src/termios/tcgetwinsize_test.cpp
new file mode 100644
index 00000000000000..756e1f2bc93bd1
--- /dev/null
+++ b/libc/test/src/termios/tcgetwinsize_test.cpp
@@ -0,0 +1,65 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unit tests for tcgetwinsize.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/fcntl_macros.h"
+#include "hdr/types/struct_winsize.h"
+#include "src/__support/libc_errno.h"
+#include "src/fcntl/open.h"
+#include "src/termios/tcgetwinsize.h"
+#include "src/unistd/close.h"
+#include "src/unistd/pipe.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcTcGetWinSizeTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+
+TEST_F(LlvmLibcTcGetWinSizeTest, InvalidFileDescriptor) {
+ struct winsize ws;
+ ASSERT_THAT(LIBC_NAMESPACE::tcgetwinsize(-1, &ws), Fails(EBADF));
+}
+
+TEST_F(LlvmLibcTcGetWinSizeTest, NonTerminalFileDescriptor) {
+ int pipefd[2];
+ ASSERT_THAT(LIBC_NAMESPACE::pipe(pipefd), Succeeds(0));
+
+ struct winsize ws;
+ ASSERT_THAT(LIBC_NAMESPACE::tcgetwinsize(pipefd[0], &ws), Fails(ENOTTY));
+ ASSERT_THAT(LIBC_NAMESPACE::tcgetwinsize(pipefd[1], &ws), Fails(ENOTTY));
+
+ ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[0]), Succeeds(0));
+ ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[1]), Succeeds(0));
+}
+
+TEST_F(LlvmLibcTcGetWinSizeTest, TerminalSmokeTest) {
+ int fd = LIBC_NAMESPACE::open("/dev/tty", O_RDONLY);
+ if (fd < 0) {
+ // When /dev/tty is not available, gracefully skip the test.
+ libc_errno = 0;
+ return;
+ }
+ ASSERT_ERRNO_SUCCESS();
+
+ constexpr unsigned short SENTINEL_VAL = 0xFFFF;
+ struct winsize ws = {SENTINEL_VAL, SENTINEL_VAL, SENTINEL_VAL, SENTINEL_VAL};
+ int ret = LIBC_NAMESPACE::tcgetwinsize(fd, &ws);
+ if (ret < 0) {
+ ASSERT_ERRNO_EQ(ENOTTY);
+ } else {
+ ASSERT_ERRNO_SUCCESS();
+ EXPECT_NE(ws.ws_row, SENTINEL_VAL);
+ EXPECT_NE(ws.ws_col, SENTINEL_VAL);
+ }
+ ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+}
>From 173d0e52248eb494d125e3a22dce7628684ee4ca Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Wed, 7 Oct 2026 07:44:07 +0530
Subject: [PATCH 2/2] [libc] Address review feedback for tcgetwinsize
- Use proxy header hdr/sys_ioctl_macros.h for ioctl macros
- Use cpp::scope_exit for test cleanup
- Remove LIBC_CRASH_ON_NULLPTR and let kernel return EFAULT
- Add NullPointer test and isolate tests to /dev/ptmx
---
libc/src/termios/linux/CMakeLists.txt | 2 +-
libc/src/termios/linux/tcgetwinsize.cpp | 6 +--
libc/test/src/termios/CMakeLists.txt | 1 +
libc/test/src/termios/tcgetwinsize_test.cpp | 42 +++++++++++++++++----
4 files changed, 37 insertions(+), 14 deletions(-)
diff --git a/libc/src/termios/linux/CMakeLists.txt b/libc/src/termios/linux/CMakeLists.txt
index 94f1f0e65a1ab0..81c44f23104d24 100644
--- a/libc/src/termios/linux/CMakeLists.txt
+++ b/libc/src/termios/linux/CMakeLists.txt
@@ -170,10 +170,10 @@ add_entrypoint_object(
HDRS
../tcgetwinsize.h
DEPENDS
+ libc.hdr.sys_ioctl_macros
libc.hdr.types.struct_winsize
libc.src.__support.OSUtil.linux.syscall_wrappers.ioctl
libc.src.errno.errno
libc.src.__support.macros.config
- libc.src.__support.macros.null_check
)
diff --git a/libc/src/termios/linux/tcgetwinsize.cpp b/libc/src/termios/linux/tcgetwinsize.cpp
index 21910a8993780e..88e26e04c21961 100644
--- a/libc/src/termios/linux/tcgetwinsize.cpp
+++ b/libc/src/termios/linux/tcgetwinsize.cpp
@@ -13,20 +13,16 @@
#include "src/termios/tcgetwinsize.h"
+#include "hdr/sys_ioctl_macros.h"
#include "hdr/types/struct_winsize.h"
#include "src/__support/OSUtil/linux/syscall_wrappers/ioctl.h"
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
-#include "src/__support/macros/null_check.h"
-
-#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(int, tcgetwinsize, (int fd, struct winsize *ws)) {
- LIBC_CRASH_ON_NULLPTR(ws);
-
auto ret = linux_syscalls::ioctl(fd, TIOCGWINSZ, ws);
if (!ret.has_value()) {
libc_errno = ret.error();
diff --git a/libc/test/src/termios/CMakeLists.txt b/libc/test/src/termios/CMakeLists.txt
index 0c930bfe29592e..1c0b4ee4f94bcf 100644
--- a/libc/test/src/termios/CMakeLists.txt
+++ b/libc/test/src/termios/CMakeLists.txt
@@ -33,6 +33,7 @@ add_libc_test(
DEPENDS
libc.hdr.fcntl_macros
libc.hdr.types.struct_winsize
+ libc.src.__support.CPP.scope
libc.src.errno.errno
libc.src.fcntl.open
libc.src.termios.tcgetwinsize
diff --git a/libc/test/src/termios/tcgetwinsize_test.cpp b/libc/test/src/termios/tcgetwinsize_test.cpp
index 756e1f2bc93bd1..23a673ccb4e58d 100644
--- a/libc/test/src/termios/tcgetwinsize_test.cpp
+++ b/libc/test/src/termios/tcgetwinsize_test.cpp
@@ -13,6 +13,7 @@
#include "hdr/fcntl_macros.h"
#include "hdr/types/struct_winsize.h"
+#include "src/__support/CPP/scope.h"
#include "src/__support/libc_errno.h"
#include "src/fcntl/open.h"
#include "src/termios/tcgetwinsize.h"
@@ -33,33 +34,58 @@ TEST_F(LlvmLibcTcGetWinSizeTest, InvalidFileDescriptor) {
TEST_F(LlvmLibcTcGetWinSizeTest, NonTerminalFileDescriptor) {
int pipefd[2];
ASSERT_THAT(LIBC_NAMESPACE::pipe(pipefd), Succeeds(0));
+ LIBC_NAMESPACE::cpp::scope_exit close_pipe([&] {
+ ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[0]), Succeeds(0));
+ ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[1]), Succeeds(0));
+ });
struct winsize ws;
ASSERT_THAT(LIBC_NAMESPACE::tcgetwinsize(pipefd[0], &ws), Fails(ENOTTY));
ASSERT_THAT(LIBC_NAMESPACE::tcgetwinsize(pipefd[1], &ws), Fails(ENOTTY));
-
- ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[0]), Succeeds(0));
- ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[1]), Succeeds(0));
}
TEST_F(LlvmLibcTcGetWinSizeTest, TerminalSmokeTest) {
- int fd = LIBC_NAMESPACE::open("/dev/tty", O_RDONLY);
+ // Use a pseudo-terminal master rather than /dev/tty so tests do not depend
+ // on an interactive terminal.
+ int fd = LIBC_NAMESPACE::open("/dev/ptmx", O_RDWR);
if (fd < 0) {
- // When /dev/tty is not available, gracefully skip the test.
+ // When /dev/ptmx is not available, gracefully skip the test.
libc_errno = 0;
return;
}
ASSERT_ERRNO_SUCCESS();
+ LIBC_NAMESPACE::cpp::scope_exit close_fd(
+ [&] { ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); });
constexpr unsigned short SENTINEL_VAL = 0xFFFF;
struct winsize ws = {SENTINEL_VAL, SENTINEL_VAL, SENTINEL_VAL, SENTINEL_VAL};
int ret = LIBC_NAMESPACE::tcgetwinsize(fd, &ws);
- if (ret < 0) {
+ if (ret < 0)
ASSERT_ERRNO_EQ(ENOTTY);
- } else {
+ else {
ASSERT_ERRNO_SUCCESS();
EXPECT_NE(ws.ws_row, SENTINEL_VAL);
EXPECT_NE(ws.ws_col, SENTINEL_VAL);
}
- ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+}
+
+TEST_F(LlvmLibcTcGetWinSizeTest, NullPointer) {
+ int fd = LIBC_NAMESPACE::open("/dev/ptmx", O_RDWR);
+ if (fd < 0) {
+ // When /dev/ptmx is not available, gracefully skip the test.
+ libc_errno = 0;
+ return;
+ }
+ ASSERT_ERRNO_SUCCESS();
+ LIBC_NAMESPACE::cpp::scope_exit close_fd(
+ [&] { ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); });
+
+ struct winsize ws;
+ int ret = LIBC_NAMESPACE::tcgetwinsize(fd, &ws);
+ if (ret < 0) {
+ ASSERT_ERRNO_EQ(ENOTTY);
+ return;
+ }
+
+ ASSERT_THAT(LIBC_NAMESPACE::tcgetwinsize(fd, nullptr), Fails(EFAULT));
}
More information about the libc-commits
mailing list