[libc-commits] [libc] [libc] Add TIOCGWINSZ and struct winsize support (PR #203919)
via libc-commits
libc-commits at lists.llvm.org
Mon Jun 15 08:28:32 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Jeff Bailey (kaladron)
<details>
<summary>Changes</summary>
Added support for the TIOCGWINSZ ioctl command.
* Defined struct winsize in llvm-libc-types.
* Defined TIOCGWINSZ in linux/sys-ioctl-macros.h.
* Exposed struct_winsize and TIOCGWINSZ in sys/ioctl.yaml.
* Added struct_winsize proxy header in hdr/types/struct_winsize.h.
* Added a unit test in test/src/sys/ioctl/linux/ioctl_test.cpp.
Assisted-by: Automated tooling, human reviewed.
---
Full diff: https://github.com/llvm/llvm-project/pull/203919.diff
9 Files Affected:
- (modified) libc/hdr/types/CMakeLists.txt (+8)
- (added) libc/hdr/types/struct_winsize.h (+23)
- (modified) libc/include/CMakeLists.txt (+1)
- (modified) libc/include/llvm-libc-macros/linux/sys-ioctl-macros.h (+1)
- (modified) libc/include/llvm-libc-types/CMakeLists.txt (+1)
- (added) libc/include/llvm-libc-types/struct_winsize.h (+24)
- (modified) libc/include/sys/ioctl.yaml (+4-1)
- (modified) libc/test/src/sys/ioctl/linux/CMakeLists.txt (+1)
- (modified) libc/test/src/sys/ioctl/linux/ioctl_test.cpp (+19)
``````````diff
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 4ca50242131d8..1742e423b1d00 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -167,6 +167,14 @@ add_proxy_header_library(
libc.include.llvm-libc-types.struct_stat
)
+add_proxy_header_library(
+ struct_winsize
+ HDRS
+ struct_winsize.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-types.struct_winsize
+)
+
add_proxy_header_library(
ssize_t
HDRS
diff --git a/libc/hdr/types/struct_winsize.h b/libc/hdr/types/struct_winsize.h
new file mode 100644
index 0000000000000..c64fa6d885a3f
--- /dev/null
+++ b/libc/hdr/types/struct_winsize.h
@@ -0,0 +1,23 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Proxy header for struct winsize.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_WINSIZE_H
+#define LLVM_LIBC_HDR_TYPES_STRUCT_WINSIZE_H
+
+#ifdef LIBC_FULL_BUILD
+#include "include/llvm-libc-types/struct_winsize.h"
+#else // Overlay mode
+#include <sys/ioctl.h>
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_STRUCT_WINSIZE_H
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index bcd2ed11407b5..549dbd9e4c3f8 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -663,6 +663,7 @@ add_header_macro(
DEPENDS
.llvm_libc_common_h
.llvm-libc-macros.sys_ioctl_macros
+ .llvm-libc-types.struct_winsize
)
add_header_macro(
diff --git a/libc/include/llvm-libc-macros/linux/sys-ioctl-macros.h b/libc/include/llvm-libc-macros/linux/sys-ioctl-macros.h
index 41226080084c3..5dc4a5270e084 100644
--- a/libc/include/llvm-libc-macros/linux/sys-ioctl-macros.h
+++ b/libc/include/llvm-libc-macros/linux/sys-ioctl-macros.h
@@ -16,5 +16,6 @@
// think is worth digging into right now.
#define TIOCGETD 0x5424
#define FIONREAD 0x541B
+#define TIOCGWINSZ 0x5413
#endif // LLVM_LIBC_MACROS_LINUX_SYS_IOCTL_MACROS_H
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 8d3b5e4b1cce8..46b0eb7d71183 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -176,6 +176,7 @@ add_header(__atexithandler_t HDR __atexithandler_t.h)
add_header(speed_t HDR speed_t.h)
add_header(tcflag_t HDR tcflag_t.h)
add_header(struct_termios HDR struct_termios.h DEPENDS .cc_t .speed_t .tcflag_t)
+add_header(struct_winsize HDR struct_winsize.h)
add_header(__getoptargv_t HDR __getoptargv_t.h)
add_header(wchar_t HDR wchar_t.h)
add_header(char8_t HDR char8_t.h)
diff --git a/libc/include/llvm-libc-types/struct_winsize.h b/libc/include/llvm-libc-types/struct_winsize.h
new file mode 100644
index 0000000000000..0918f293acd57
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_winsize.h
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Definition of struct winsize.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TYPES_STRUCT_WINSIZE_H
+#define LLVM_LIBC_TYPES_STRUCT_WINSIZE_H
+
+struct winsize {
+ unsigned short ws_row;
+ unsigned short ws_col;
+ unsigned short ws_xpixel;
+ unsigned short ws_ypixel;
+};
+
+#endif // LLVM_LIBC_TYPES_STRUCT_WINSIZE_H
diff --git a/libc/include/sys/ioctl.yaml b/libc/include/sys/ioctl.yaml
index 1757e47663d79..cee4fff5a8ead 100644
--- a/libc/include/sys/ioctl.yaml
+++ b/libc/include/sys/ioctl.yaml
@@ -4,7 +4,10 @@ standards:
macros:
- macro_name: FIONREAD
macro_header: sys-ioctl-macros.h
-types: []
+ - macro_name: TIOCGWINSZ
+ macro_header: sys-ioctl-macros.h
+types:
+ - type_name: struct_winsize
enums: []
objects: []
functions:
diff --git a/libc/test/src/sys/ioctl/linux/CMakeLists.txt b/libc/test/src/sys/ioctl/linux/CMakeLists.txt
index 2ccef25f4264f..69a521249dd9c 100644
--- a/libc/test/src/sys/ioctl/linux/CMakeLists.txt
+++ b/libc/test/src/sys/ioctl/linux/CMakeLists.txt
@@ -7,6 +7,7 @@ add_libc_unittest(
SRCS
ioctl_test.cpp
DEPENDS
+ libc.hdr.types.struct_winsize
libc.hdr.sys_ioctl_macros
libc.src.sys.ioctl.ioctl
libc.src.errno.errno
diff --git a/libc/test/src/sys/ioctl/linux/ioctl_test.cpp b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
index 4560bcf6e2e96..581e1f6226b10 100644
--- a/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
+++ b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
@@ -18,6 +18,7 @@
#include "hdr/sys_stat_macros.h"
#include "hdr/sys_ioctl_macros.h"
+#include "hdr/types/struct_winsize.h"
using LlvmLibcSysIoctlTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
@@ -71,3 +72,21 @@ TEST_F(LlvmLibcSysIoctlTest, InvalidCommandAndFIONREAD) {
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
}
+
+TEST_F(LlvmLibcSysIoctlTest, GetWindowSize) {
+ int fd = LIBC_NAMESPACE::open("/dev/tty", O_RDONLY);
+ if (fd < 0) {
+ libc_errno = 0;
+ return;
+ }
+ struct winsize ws = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
+ int ret = LIBC_NAMESPACE::ioctl(fd, TIOCGWINSZ, &ws);
+ if (ret < 0) {
+ ASSERT_ERRNO_EQ(ENOTTY);
+ } else {
+ ASSERT_ERRNO_SUCCESS();
+ EXPECT_NE(ws.ws_row, static_cast<unsigned short>(0xFFFF));
+ EXPECT_NE(ws.ws_col, static_cast<unsigned short>(0xFFFF));
+ }
+ ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/203919
More information about the libc-commits
mailing list