[libc-commits] [libc] e347521 - [libc] Implement tcsetwinsize in termios (#228498)
via libc-commits
libc-commits at lists.llvm.org
Fri Oct 9 05:19:20 PDT 2026
Author: Aman Maurya
Date: 2026-10-09T17:49:15+05:30
New Revision: e347521eecbe26f113a6096df66490975b314075
URL: https://github.com/llvm/llvm-project/commit/e347521eecbe26f113a6096df66490975b314075
DIFF: https://github.com/llvm/llvm-project/commit/e347521eecbe26f113a6096df66490975b314075.diff
LOG: [libc] Implement tcsetwinsize in termios (#228498)
Implement the standard POSIX.1-2024 function `tcsetwinsize` in
`<termios.h>`.
Fixes #228380
Part of #228378
Implementation was assisted by Antigravity by analysing other functions
in header and reviewed by Aman Maurya.
Added:
libc/src/termios/linux/tcsetwinsize.cpp
libc/src/termios/tcsetwinsize.h
libc/test/src/termios/tcsetwinsize_test.cpp
Modified:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/riscv/entrypoints.txt
libc/config/linux/x86_64/entrypoints.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 c97a1b7c57593d..7892fe53cf20b1 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -446,6 +446,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.termios.tcgetwinsize
libc.src.termios.tcsendbreak
libc.src.termios.tcsetattr
+ libc.src.termios.tcsetwinsize
# unistd.h entrypoints
libc.src.unistd.alarm
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 09439ce67e2e0f..6a3dfccd6c7257 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -466,6 +466,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.termios.tcgetwinsize
libc.src.termios.tcsendbreak
libc.src.termios.tcsetattr
+ libc.src.termios.tcsetwinsize
# unistd.h entrypoints
libc.src.unistd.alarm
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 6b2baa54401a12..48daea6449c638 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -486,6 +486,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.termios.tcgetwinsize
libc.src.termios.tcsendbreak
libc.src.termios.tcsetattr
+ libc.src.termios.tcsetwinsize
# unistd.h entrypoints
libc.src.unistd.alarm
diff --git a/libc/include/termios.yaml b/libc/include/termios.yaml
index a581610bf3ee12..16173beb742b9a 100644
--- a/libc/include/termios.yaml
+++ b/libc/include/termios.yaml
@@ -95,4 +95,10 @@ functions:
arguments:
- type: int
- type: struct winsize *
-
+ - name: tcsetwinsize
+ standards:
+ - posix
+ return_type: int
+ arguments:
+ - type: int
+ - type: const struct winsize *
diff --git a/libc/src/termios/CMakeLists.txt b/libc/src/termios/CMakeLists.txt
index 5c2761b0f3cb27..1fd764c4a7ecce 100644
--- a/libc/src/termios/CMakeLists.txt
+++ b/libc/src/termios/CMakeLists.txt
@@ -86,3 +86,9 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.tcgetwinsize
)
+add_entrypoint_object(
+ tcsetwinsize
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.tcsetwinsize
+)
diff --git a/libc/src/termios/linux/CMakeLists.txt b/libc/src/termios/linux/CMakeLists.txt
index 81c44f23104d24..e0c6db6c96adb8 100644
--- a/libc/src/termios/linux/CMakeLists.txt
+++ b/libc/src/termios/linux/CMakeLists.txt
@@ -177,3 +177,16 @@ add_entrypoint_object(
libc.src.__support.macros.config
)
+add_entrypoint_object(
+ tcsetwinsize
+ SRCS
+ tcsetwinsize.cpp
+ HDRS
+ ../tcsetwinsize.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/tcsetwinsize.cpp b/libc/src/termios/linux/tcsetwinsize.cpp
new file mode 100644
index 00000000000000..0fe5af12464ca4
--- /dev/null
+++ b/libc/src/termios/linux/tcsetwinsize.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 tcsetwinsize.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/termios/tcsetwinsize.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, tcsetwinsize, (int fd, const struct winsize *ws)) {
+ auto ret = linux_syscalls::ioctl(fd, TIOCSWINSZ, ws);
+ if (!ret.has_value()) {
+ libc_errno = ret.error();
+ return -1;
+ }
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/termios/tcsetwinsize.h b/libc/src/termios/tcsetwinsize.h
new file mode 100644
index 00000000000000..48d5d300602877
--- /dev/null
+++ b/libc/src/termios/tcsetwinsize.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 tcsetwinsize function.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_TERMIOS_TCSETWINSIZE_H
+#define LLVM_LIBC_SRC_TERMIOS_TCSETWINSIZE_H
+
+#include "hdr/types/struct_winsize.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int tcsetwinsize(int fd, const struct winsize *ws);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_TERMIOS_TCSETWINSIZE_H
diff --git a/libc/test/src/termios/CMakeLists.txt b/libc/test/src/termios/CMakeLists.txt
index 1c0b4ee4f94bcf..19486532bc2f9c 100644
--- a/libc/test/src/termios/CMakeLists.txt
+++ b/libc/test/src/termios/CMakeLists.txt
@@ -43,3 +43,21 @@ add_libc_test(
libc.test.UnitTest.ErrnoSetterMatcher
)
+add_libc_test(
+ tcsetwinsize_test
+ SUITE
+ libc_termios_unittests
+ SRCS
+ tcsetwinsize_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.tcsetwinsize
+ libc.src.unistd.close
+ libc.src.unistd.pipe
+ libc.test.UnitTest.ErrnoCheckingTest
+ libc.test.UnitTest.ErrnoSetterMatcher
+)
diff --git a/libc/test/src/termios/tcsetwinsize_test.cpp b/libc/test/src/termios/tcsetwinsize_test.cpp
new file mode 100644
index 00000000000000..cdcdfbca6f19fe
--- /dev/null
+++ b/libc/test/src/termios/tcsetwinsize_test.cpp
@@ -0,0 +1,95 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 tcsetwinsize.
+///
+//===----------------------------------------------------------------------===//
+
+#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/tcsetwinsize.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 LlvmLibcTcSetWinSizeTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+
+TEST_F(LlvmLibcTcSetWinSizeTest, InvalidFileDescriptor) {
+ constexpr unsigned short TEST_ROWS = 24;
+ constexpr unsigned short TEST_COLS = 80;
+ struct winsize ws = {TEST_ROWS, TEST_COLS, 0, 0};
+ ASSERT_THAT(LIBC_NAMESPACE::tcsetwinsize(-1, &ws), Fails(EBADF));
+}
+
+TEST_F(LlvmLibcTcSetWinSizeTest, 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));
+ });
+
+ constexpr unsigned short TEST_ROWS = 24;
+ constexpr unsigned short TEST_COLS = 80;
+ struct winsize ws = {TEST_ROWS, TEST_COLS, 0, 0};
+ ASSERT_THAT(LIBC_NAMESPACE::tcsetwinsize(pipefd[0], &ws), Fails(ENOTTY));
+ ASSERT_THAT(LIBC_NAMESPACE::tcsetwinsize(pipefd[1], &ws), Fails(ENOTTY));
+}
+
+TEST_F(LlvmLibcTcSetWinSizeTest, TerminalSmokeTest) {
+ // Use a pseudo-terminal master rather than /dev/tty so running tests in an
+ // interactive terminal does not resize the user's window.
+ 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 TEST_ROWS = 30;
+ constexpr unsigned short TEST_COLS = 100;
+ struct winsize ws = {TEST_ROWS, TEST_COLS, 0, 0};
+ int ret = LIBC_NAMESPACE::tcsetwinsize(fd, &ws);
+ if (ret < 0)
+ ASSERT_ERRNO_EQ(ENOTTY);
+ else
+ ASSERT_ERRNO_SUCCESS();
+}
+
+TEST_F(LlvmLibcTcSetWinSizeTest, 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)); });
+
+ constexpr unsigned short TEST_ROWS = 24;
+ constexpr unsigned short TEST_COLS = 80;
+ struct winsize ws = {TEST_ROWS, TEST_COLS, 0, 0};
+ int ret = LIBC_NAMESPACE::tcsetwinsize(fd, &ws);
+ if (ret < 0) {
+ ASSERT_ERRNO_EQ(ENOTTY);
+ return;
+ }
+
+ ASSERT_THAT(LIBC_NAMESPACE::tcsetwinsize(fd, nullptr), Fails(EFAULT));
+}
More information about the libc-commits
mailing list