[libc-commits] [libc] [libc] Implement tcsetwinsize in termios (PR #228498)

via libc-commits libc-commits at lists.llvm.org
Fri Oct 2 09:00:56 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Aman Maurya (amanmaurya92)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/228498.diff


11 Files Affected:

- (modified) libc/config/linux/aarch64/entrypoints.txt (+1) 
- (modified) libc/config/linux/riscv/entrypoints.txt (+1) 
- (modified) libc/config/linux/x86_64/entrypoints.txt (+1) 
- (modified) libc/include/CMakeLists.txt (+1) 
- (modified) libc/include/termios.yaml (+9) 
- (modified) libc/src/termios/CMakeLists.txt (+8) 
- (modified) libc/src/termios/linux/CMakeLists.txt (+15) 
- (added) libc/src/termios/linux/tcsetwinsize.cpp (+38) 
- (added) libc/src/termios/tcsetwinsize.h (+26) 
- (modified) libc/test/src/termios/CMakeLists.txt (+19) 
- (added) libc/test/src/termios/tcsetwinsize_test.cpp (+71) 


``````````diff
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index c7ded2fade05d9..f43e2b906fdfc0 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -437,6 +437,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.termios.tcgetsid
     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 296985eeff937c..517da3cdab83a2 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -456,6 +456,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.termios.tcgetsid
     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 7928a35a6284a5..319a90216d93e4 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -476,6 +476,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.termios.tcgetsid
     libc.src.termios.tcsendbreak
     libc.src.termios.tcsetattr
+    libc.src.termios.tcsetwinsize
 
     # unistd.h entrypoints
     libc.src.unistd.alarm
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..ef89c430253d5a 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: 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 0fbd632b78fbda..dd9516690e9862 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(
+  tcsetwinsize
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.tcsetwinsize
+)
+
diff --git a/libc/src/termios/linux/CMakeLists.txt b/libc/src/termios/linux/CMakeLists.txt
index 86565cf1294c7d..1e18156c32784d 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(
+  tcsetwinsize
+  SRCS
+    tcsetwinsize.cpp
+  HDRS
+    ../tcsetwinsize.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/tcsetwinsize.cpp b/libc/src/termios/linux/tcsetwinsize.cpp
new file mode 100644
index 00000000000000..dfe28209e75276
--- /dev/null
+++ b/libc/src/termios/linux/tcsetwinsize.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 tcsetwinsize.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/termios/tcsetwinsize.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, tcsetwinsize, (int fd, const struct winsize *ws)) {
+  LIBC_CRASH_ON_NULLPTR(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 a802b3e7aba2aa..2228af98780c33 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(
+  tcsetwinsize_test
+  SUITE
+    libc_termios_unittests
+  SRCS
+    tcsetwinsize_test.cpp
+  DEPENDS
+    libc.hdr.fcntl_macros
+    libc.hdr.types.struct_winsize
+    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..4457a65dce89b4
--- /dev/null
+++ b/libc/test/src/termios/tcsetwinsize_test.cpp
@@ -0,0 +1,71 @@
+//===----------------------------------------------------------------------===//
+//
+// 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/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));
+
+  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));
+
+  ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[0]), Succeeds(0));
+  ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[1]), Succeeds(0));
+}
+
+TEST_F(LlvmLibcTcSetWinSizeTest, TerminalSmokeTest) {
+  int fd = LIBC_NAMESPACE::open("/dev/ptmx", O_RDWR);
+  if (fd < 0)
+    fd = LIBC_NAMESPACE::open("/dev/tty", O_RDWR);
+
+  if (fd < 0) {
+    // When no terminal is available, gracefully skip the test.
+    libc_errno = 0;
+    return;
+  }
+  ASSERT_ERRNO_SUCCESS();
+
+  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();
+  }
+  ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/228498


More information about the libc-commits mailing list