Hi, A prerequisite of implementing tcgetwinsize and tcsetwinsize as specified by POSIX 2024 is defining struct winsize on all platforms in termios.h.
This patch defines the struct on Windows where it is missing and checks if it is defined in sys/ioctl.h like glibc. POSIX 2024 states that it should be defined in termios.h so I reported a glibc bug here [1]. I haven't tested other platforms yet but hopefully it should all be handled in termios.h there or through a single #include like glibc. [1] https://sourceware.org/bugzilla/show_bug.cgi?id=32074
>From afbe284229fc249527b0e76827e67e5e1c7a7399 Mon Sep 17 00:00:00 2001 From: Collin Funk <collin.fu...@gmail.com> Date: Mon, 12 Aug 2024 20:09:01 -0700 Subject: [PATCH 1/2] termios: Define struct winsize on all platforms. * doc/posix-headers/termios.texi (termios.h): Mention struct winsize is defined in <sys/ioctl.h> on glibc. * lib/termios.in.h (struct winsize): Define or include. * m4/termios_h.m4 (gl_TERMIOS_H): Check for a definition of struct winsize. * modules/termios (Makefile.am): Replace AC_SUBST variables. --- ChangeLog | 10 ++++++++ doc/posix-headers/termios.texi | 10 ++++++-- lib/termios.in.h | 17 +++++++++++++ m4/termios_h.m4 | 44 +++++++++++++++++++++++++++++++++- modules/termios | 2 ++ 5 files changed, 80 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 63b19e0480..b6c173ab03 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2024-08-12 Collin Funk <collin.fu...@gmail.com> + + termios: Define struct winsize on all platforms. + * doc/posix-headers/termios.texi (termios.h): Mention struct winsize is + defined in <sys/ioctl.h> on glibc. + * lib/termios.in.h (struct winsize): Define or include. + * m4/termios_h.m4 (gl_TERMIOS_H): Check for a definition of struct + winsize. + * modules/termios (Makefile.am): Replace AC_SUBST variables. + 2024-08-12 Bruno Haible <br...@clisp.org> errno tests: Avoid test failure on Haiku. diff --git a/doc/posix-headers/termios.texi b/doc/posix-headers/termios.texi index cf4da4da3a..87f02db571 100644 --- a/doc/posix-headers/termios.texi +++ b/doc/posix-headers/termios.texi @@ -18,7 +18,13 @@ @node termios.h Portability problems not fixed by Gnulib: @itemize @item -The types @code{struct termios}, @code{cc_t}, @code{speed_t}, @code{tcflag_t} -are not defined on some platforms: +The types @code{struct termios}, @code{struct winsize}, @code{cc_t}, +@code{speed_t}, @code{tcflag_t} are not defined on some platforms: mingw, MSVC 14. + +@item +@code{struct winsize} is defined in @code{sys/ioctl.h} on some +platforms: +glibc + @end itemize diff --git a/lib/termios.in.h b/lib/termios.in.h index a7c1f3f90a..173cb69786 100644 --- a/lib/termios.in.h +++ b/lib/termios.in.h @@ -48,6 +48,23 @@ extern "C" { /* Get pid_t. */ #include <sys/types.h> +#if ! @TERMIOS_H_DEFINES_STRUCT_WINSIZE@ +/* On glibc. */ +# if @SYS_IOCTL_H_DEFINES_STRUCT_WINSIZE@ +# include <sys/ioctl.h> +# else +/* On Windows. */ +# if !GNULIB_defined_struct_winsize +struct winsize +{ + unsigned short ws_row; + unsigned short ws_col; +}; +# define GNULIB_defined_struct_winsize 1 +# endif +# endif +#endif + /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */ /* The definition of _GL_WARN_ON_USE is copied here. */ diff --git a/m4/termios_h.m4 b/m4/termios_h.m4 index 788008eb82..537656d8cc 100644 --- a/m4/termios_h.m4 +++ b/m4/termios_h.m4 @@ -1,5 +1,5 @@ # termios_h.m4 -# serial 7 +# serial 8 dnl Copyright (C) 2010-2024 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -16,6 +16,45 @@ AC_DEFUN_ONCE([gl_TERMIOS_H] HAVE_TERMIOS_H=0 fi + if test $ac_cv_header_termios_h = yes; then + AC_CACHE_CHECK([for struct winsize in <termios.h>], + [gl_cv_struct_winsize_in_termios_h], + [AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM( + [[#include <termios.h> + ]], + [[static struct winsize x; x.ws_row = x.ws_col;]])], + [gl_cv_struct_winsize_in_termios_h=yes], + [gl_cv_struct_winsize_in_termios_h=no]) + ]) + if test $gl_cv_struct_winsize_in_termios_h = no; then + AC_CHECK_HEADERS_ONCE([sys/ioctl.h]) + if test $ac_cv_header_sys_ioctl_h = yes; then + AC_CACHE_CHECK([for struct winsize in <sys/ioctl.h>], + [gl_cv_struct_winsize_in_sys_ioctl_h], + [AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM( + [[#include <sys/ioctl.h> + ]], + [[static struct winsize x; x.ws_row = x.ws_col;]])], + [gl_cv_struct_winsize_in_sys_ioctl_h=yes], + [gl_cv_struct_winsize_in_sys_ioctl_h=no]) + ]) + fi + fi + fi + + TERMIOS_H_DEFINES_STRUCT_WINSIZE=0 + SYS_IOCTL_H_DEFINES_STRUCT_WINSIZE=0 + if test "$gl_cv_struct_winsize_in_termios_h" != yes \ + || test "$gl_cv_struct_winsize_in_sys_ioctl_h" != yes; then + if test "$gl_cv_struct_winsize_in_termios_h" = yes; then + TERMIOS_H_DEFINES_STRUCT_WINSIZE=1 + elif test "$gl_cv_struct_winsize_in_sys_ioctl_h" = yes; then + SYS_IOCTL_H_DEFINES_STRUCT_WINSIZE=1 + fi + fi + dnl Ensure the type pid_t gets defined. AC_REQUIRE([AC_TYPE_PID_T]) @@ -24,6 +63,9 @@ AC_DEFUN_ONCE([gl_TERMIOS_H] dnl guaranteed by C89. gl_WARN_ON_USE_PREPARE([[#include <termios.h>]], [tcgetsid]) + + AC_SUBST([TERMIOS_H_DEFINES_STRUCT_WINSIZE]) + AC_SUBST([SYS_IOCTL_H_DEFINES_STRUCT_WINSIZE]) ]) # gl_TERMIOS_MODULE_INDICATOR([modulename]) diff --git a/modules/termios b/modules/termios index abc954ceb4..3af20592b9 100644 --- a/modules/termios +++ b/modules/termios @@ -33,6 +33,8 @@ termios.h: termios.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE -e 's/@''GNULIB_TCGETSID''@/$(GNULIB_TCGETSID)/g' \ -e 's|@''HAVE_DECL_TCGETSID''@|$(HAVE_DECL_TCGETSID)|g' \ -e 's|@''HAVE_TERMIOS_H''@|$(HAVE_TERMIOS_H)|g' \ + -e 's|@''TERMIOS_H_DEFINES_STRUCT_WINSIZE''@|$(TERMIOS_H_DEFINES_STRUCT_WINSIZE)|g' \ + -e 's|@''SYS_IOCTL_H_DEFINES_STRUCT_WINSIZE''@|$(SYS_IOCTL_H_DEFINES_STRUCT_WINSIZE)|g' \ -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \ -e '/definition of _GL_WARN_ON_USE/r $(WARN_ON_USE_H)' \ $(srcdir)/termios.in.h > $@-t -- 2.46.0
>From 79216cc1fa5fc1a0571efdd8d84f794326785207 Mon Sep 17 00:00:00 2001 From: Collin Funk <collin.fu...@gmail.com> Date: Mon, 12 Aug 2024 20:11:19 -0700 Subject: [PATCH 2/2] termios tests: Check for struct winsize. * tests/test-termios.c (t2): Make sure inclusion of termios.h allows use of struct winsize. --- ChangeLog | 4 ++++ tests/test-termios.c | 1 + 2 files changed, 5 insertions(+) diff --git a/ChangeLog b/ChangeLog index b6c173ab03..300af4c444 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2024-08-12 Collin Funk <collin.fu...@gmail.com> + termios tests: Check for struct winsize. + * tests/test-termios.c (t2): Make sure inclusion of termios.h allows use + of struct winsize. + termios: Define struct winsize on all platforms. * doc/posix-headers/termios.texi (termios.h): Mention struct winsize is defined in <sys/ioctl.h> on glibc. diff --git a/tests/test-termios.c b/tests/test-termios.c index 93a6b458cf..fdc01f13e5 100644 --- a/tests/test-termios.c +++ b/tests/test-termios.c @@ -22,6 +22,7 @@ /* Check that the types are all defined. */ pid_t t1; +struct winsize t2; int main (void) -- 2.46.0