[libc-commits] [libc] [libc] Implement tcgetwinsize in termios (PR #228435)

via libc-commits libc-commits at lists.llvm.org
Fri Oct 2 06:19:23 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 `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.

---
Full diff: https://github.com/llvm/llvm-project/pull/228435.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/tcgetwinsize.cpp (+38) 
- (added) libc/src/termios/tcgetwinsize.h (+26) 
- (modified) libc/test/src/termios/CMakeLists.txt (+19) 
- (added) libc/test/src/termios/tcgetwinsize_test.cpp (+65) 


``````````diff
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));
+}

``````````

</details>


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


More information about the libc-commits mailing list