[libc-commits] [libc] 3f098e0 - [libc] Implement tcgetwinsize in termios (#228435)
via libc-commits
libc-commits at lists.llvm.org
Fri Oct 9 03:58:18 PDT 2026
Author: Aman Maurya
Date: 2026-10-09T16:28:12+05:30
New Revision: 3f098e03b0846280515f386fd4da8c85e0257bf5
URL: https://github.com/llvm/llvm-project/commit/3f098e03b0846280515f386fd4da8c85e0257bf5
DIFF: https://github.com/llvm/llvm-project/commit/3f098e03b0846280515f386fd4da8c85e0257bf5.diff
LOG: [libc] Implement tcgetwinsize in termios (#228435)
Implement the standard POSIX.1-2024 function `tcgetwinsize` in
`<termios.h>`.
Fixes #228379
Part of #228378
Implementation was assisted by Antigravity by analysing other functions
in header and reviewed by Aman Maurya.
Added:
libc/src/termios/linux/tcgetwinsize.cpp
libc/src/termios/tcgetwinsize.h
libc/test/src/termios/tcgetwinsize_test.cpp
Modified:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/riscv/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/include/CMakeLists.txt
libc/include/termios.yaml
libc/src/termios/CMakeLists.txt
libc/src/termios/linux/CMakeLists.txt
libc/test/src/termios/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 5bfad95231133e..c97a1b7c57593d 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -443,6 +443,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 e5c886d53779dc..09439ce67e2e0f 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -463,6 +463,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 aec72d78fb5354..6b2baa54401a12 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -483,6 +483,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 7b8dee2b4aa77e..906e0a738fce96 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -1208,6 +1208,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..81c44f23104d24 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.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
+)
+
diff --git a/libc/src/termios/linux/tcgetwinsize.cpp b/libc/src/termios/linux/tcgetwinsize.cpp
new file mode 100644
index 00000000000000..88e26e04c21961
--- /dev/null
+++ b/libc/src/termios/linux/tcgetwinsize.cpp
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// 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/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"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, tcgetwinsize, (int fd, struct winsize *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..1c0b4ee4f94bcf 100644
--- a/libc/test/src/termios/CMakeLists.txt
+++ b/libc/test/src/termios/CMakeLists.txt
@@ -23,3 +23,23 @@ 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.__support.CPP.scope
+ 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..23a673ccb4e58d
--- /dev/null
+++ b/libc/test/src/termios/tcgetwinsize_test.cpp
@@ -0,0 +1,91 @@
+//===----------------------------------------------------------------------===//
+//
+// 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/CPP/scope.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));
+ 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));
+}
+
+TEST_F(LlvmLibcTcGetWinSizeTest, TerminalSmokeTest) {
+ // 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/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)
+ ASSERT_ERRNO_EQ(ENOTTY);
+ else {
+ ASSERT_ERRNO_SUCCESS();
+ EXPECT_NE(ws.ws_row, SENTINEL_VAL);
+ EXPECT_NE(ws.ws_col, SENTINEL_VAL);
+ }
+}
+
+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