From 84bb81d6ff8e441dfd45825742d557ae61065b46 Mon Sep 17 00:00:00 2001
From: Soumendra Ganguly <soumendraganguly@gmail.com>
Date: Sun, 27 Aug 2023 17:56:34 -0500
Subject: [PATCH] Add functions tcgetwinsize() and tcsetwinsize() which are set
 to appear in the upcoming POSIX Base Specifications, Issue 8.

---
 bits/ioctl-types.h                            | 13 ++-----
 bits/winsize.h                                | 36 +++++++++++++++++
 sysdeps/unix/bsd/tcgetwinsize.c               | 25 ++++++++++++
 sysdeps/unix/bsd/tcsetwinsize.c               | 25 ++++++++++++
 sysdeps/unix/sysv/linux/bits/ioctl-types.h    | 11 ++----
 .../unix/sysv/linux/mips/bits/ioctl-types.h   | 10 ++---
 .../sysv/linux/powerpc/bits/ioctl-types.h     | 11 ++----
 sysdeps/unix/sysv/linux/tcgetwinsize.c        |  1 +
 sysdeps/unix/sysv/linux/tcsetwinsize.c        |  1 +
 termios/Makefile                              |  3 +-
 termios/Versions                              |  4 ++
 termios/tcgetwinsize.c                        | 39 +++++++++++++++++++
 termios/tcsetwinsize.c                        | 39 +++++++++++++++++++
 termios/termios.h                             | 11 ++++++
 14 files changed, 195 insertions(+), 34 deletions(-)
 create mode 100644 bits/winsize.h
 create mode 100644 sysdeps/unix/bsd/tcgetwinsize.c
 create mode 100644 sysdeps/unix/bsd/tcsetwinsize.c
 create mode 100644 sysdeps/unix/sysv/linux/tcgetwinsize.c
 create mode 100644 sysdeps/unix/sysv/linux/tcsetwinsize.c
 create mode 100644 termios/tcgetwinsize.c
 create mode 100644 termios/tcsetwinsize.c

diff --git a/bits/ioctl-types.h b/bits/ioctl-types.h
index 961e5f8d3f..73308e819e 100644
--- a/bits/ioctl-types.h
+++ b/bits/ioctl-types.h
@@ -66,16 +66,9 @@ struct sgttyb
   _IOT (_IOTS (char), 4, _IOTS (short int), 1, 0, 0)
 
 #if defined TIOCGWINSZ || defined TIOCSWINSZ
-/* Type of ARG for TIOCGWINSZ and TIOCSWINSZ requests.  */
-struct winsize
-{
-  unsigned short int ws_row;	/* Rows, in characters.  */
-  unsigned short int ws_col;	/* Columns, in characters.  */
-
-  /* These are not actually used.  */
-  unsigned short int ws_xpixel;	/* Horizontal pixels.  */
-  unsigned short int ws_ypixel;	/* Vertical pixels.  */
-};
+/* Get the definition of 'struct winsize', which is the type of ARG for
+   TIOCGWINSZ and TIOCSWINSZ requests. */
+#include <bits/winsize.h>
 
 # define	_IOT_winsize	/* Hurd ioctl type field.  */ \
   _IOT (_IOTS (unsigned short int), 4, 0, 0, 0, 0)
diff --git a/bits/winsize.h b/bits/winsize.h
new file mode 100644
index 0000000000..3a95700f82
--- /dev/null
+++ b/bits/winsize.h
@@ -0,0 +1,36 @@
+/* Copyright (C) 2023 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#if !defined(_SYS_IOCTL_H) && !defined(_TERMIOS_H)
+# error "Never use <bits/winsize.h> directly; include <sys/ioctl.h> or <termios.h> instead."
+#endif
+
+#ifndef _BITS_WINSIZE_H
+#define _BITS_WINSIZE_H
+
+/* Type of ARG for TIOCGWINSZ and TIOCSWINSZ requests.  */
+struct winsize
+{
+  unsigned short int ws_row;	/* Rows, in characters.  */
+  unsigned short int ws_col;	/* Columns, in characters.  */
+
+  /* These are not actually used.  */
+  unsigned short int ws_xpixel;	/* Horizontal pixels.  */
+  unsigned short int ws_ypixel;	/* Vertical pixels.  */
+};
+
+#endif /* _BITS_WINSIZE_H */
diff --git a/sysdeps/unix/bsd/tcgetwinsize.c b/sysdeps/unix/bsd/tcgetwinsize.c
new file mode 100644
index 0000000000..eeff1c7b9c
--- /dev/null
+++ b/sysdeps/unix/bsd/tcgetwinsize.c
@@ -0,0 +1,25 @@
+/* Copyright (C) 2023 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <sys/ioctl.h>
+
+/* Get the winsize of FD in *WINSIZE_P.  */
+int
+tcgetwinsize (int fd, struct winsize *winsize_p)
+{
+  return __ioctl (fd, TIOCGWINSZ, winsize_p);
+}
diff --git a/sysdeps/unix/bsd/tcsetwinsize.c b/sysdeps/unix/bsd/tcsetwinsize.c
new file mode 100644
index 0000000000..e788a6d69a
--- /dev/null
+++ b/sysdeps/unix/bsd/tcsetwinsize.c
@@ -0,0 +1,25 @@
+/* Copyright (C) 2023 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <sys/ioctl.h>
+
+/* Set the winsize of FD to *WINSIZE_P.  */
+int
+tcsetwinsize (int fd, const struct winsize *winsize_p)
+{
+  return __ioctl (fd, TIOCSWINSZ, winsize_p);
+}
diff --git a/sysdeps/unix/sysv/linux/bits/ioctl-types.h b/sysdeps/unix/sysv/linux/bits/ioctl-types.h
index 0984adbfdf..7626fde5be 100644
--- a/sysdeps/unix/sysv/linux/bits/ioctl-types.h
+++ b/sysdeps/unix/sysv/linux/bits/ioctl-types.h
@@ -23,14 +23,9 @@
 /* Get definition of constants for use with `ioctl'.  */
 #include <asm/ioctls.h>
 
-
-struct winsize
-  {
-    unsigned short int ws_row;
-    unsigned short int ws_col;
-    unsigned short int ws_xpixel;
-    unsigned short int ws_ypixel;
-  };
+/* Get the definition of 'struct winsize', which is the type of ARG for
+   TIOCGWINSZ and TIOCSWINSZ requests. */
+#include <bits/winsize.h>
 
 #define NCC 8
 struct termio
diff --git a/sysdeps/unix/sysv/linux/mips/bits/ioctl-types.h b/sysdeps/unix/sysv/linux/mips/bits/ioctl-types.h
index 4b718537a6..2f65c2ba7f 100644
--- a/sysdeps/unix/sysv/linux/mips/bits/ioctl-types.h
+++ b/sysdeps/unix/sysv/linux/mips/bits/ioctl-types.h
@@ -23,13 +23,9 @@
 /* Get definition of constants for use with `ioctl'.  */
 #include <asm/ioctls.h>
 
-struct winsize
-  {
-    unsigned short int ws_row;
-    unsigned short int ws_col;
-    unsigned short int ws_xpixel;
-    unsigned short int ws_ypixel;
-  };
+/* Get the definition of 'struct winsize', which is the type of ARG for
+   TIOCGWINSZ and TIOCSWINSZ requests. */
+#include <bits/winsize.h>
 
 #define NCC	8
 struct termio
diff --git a/sysdeps/unix/sysv/linux/powerpc/bits/ioctl-types.h b/sysdeps/unix/sysv/linux/powerpc/bits/ioctl-types.h
index a14b9511be..7e795c3dd6 100644
--- a/sysdeps/unix/sysv/linux/powerpc/bits/ioctl-types.h
+++ b/sysdeps/unix/sysv/linux/powerpc/bits/ioctl-types.h
@@ -23,14 +23,9 @@
 /* Get definition of constants for use with `ioctl'.  */
 #include <asm/ioctls.h>
 
-
-struct winsize
-  {
-    unsigned short int ws_row;
-    unsigned short int ws_col;
-    unsigned short int ws_xpixel;
-    unsigned short int ws_ypixel;
-  };
+/* Get the definition of 'struct winsize', which is the type of ARG for
+   TIOCGWINSZ and TIOCSWINSZ requests. */
+#include <bits/winsize.h>
 
 #define NCC 10
 struct termio
diff --git a/sysdeps/unix/sysv/linux/tcgetwinsize.c b/sysdeps/unix/sysv/linux/tcgetwinsize.c
new file mode 100644
index 0000000000..460c5a5f2d
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/tcgetwinsize.c
@@ -0,0 +1 @@
+#include <sysdeps/unix/bsd/tcgetwinsize.c>
diff --git a/sysdeps/unix/sysv/linux/tcsetwinsize.c b/sysdeps/unix/sysv/linux/tcsetwinsize.c
new file mode 100644
index 0000000000..562bc40439
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/tcsetwinsize.c
@@ -0,0 +1 @@
+#include <sysdeps/unix/bsd/tcsetwinsize.c>
diff --git a/termios/Makefile b/termios/Makefile
index cd45fc7626..673ef9dfaa 100644
--- a/termios/Makefile
+++ b/termios/Makefile
@@ -26,7 +26,8 @@ headers	:= termios.h bits/termios.h sys/ttydefaults.h sys/termios.h \
 	   sys/ttychars.h
 
 routines	:= speed cfsetspeed tcsetattr tcgetattr tcgetpgrp tcsetpgrp \
-		   tcdrain tcflow tcflush tcsendbrk cfmakeraw tcgetsid
+		   tcdrain tcflow tcflush tcsendbrk cfmakeraw tcgetsid \
+		   tcgetwinsize tcsetwinsize
 
 include ../Rules
 
diff --git a/termios/Versions b/termios/Versions
index 711ed0334b..159d4a149e 100644
--- a/termios/Versions
+++ b/termios/Versions
@@ -11,4 +11,8 @@ libc {
     # t*
     tcgetsid;
   }
+  GLIBC_2.38 {
+    # t*
+    tcgetwinsize; tcsetwinsize;
+  }
 }
diff --git a/termios/tcgetwinsize.c b/termios/tcgetwinsize.c
new file mode 100644
index 0000000000..3a09659593
--- /dev/null
+++ b/termios/tcgetwinsize.c
@@ -0,0 +1,39 @@
+/* Copyright (C) 2023 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <errno.h>
+#include <termios.h>
+
+/* Get the winsize of FD in *WINSIZE_P.  */
+int
+tcgetwinsize (int fd, struct winsize *winsize_p)
+{
+  if (fd < 0)
+    {
+      __set_errno (EBADF);
+      return -1;
+    }
+  if (winsize_p == NULL)
+    {
+      __set_errno (EINVAL);
+      return -1;
+    }
+
+  __set_errno (ENOSYS);
+  return -1;
+}
+stub_warning (tcgetwinsize)
diff --git a/termios/tcsetwinsize.c b/termios/tcsetwinsize.c
new file mode 100644
index 0000000000..a2f30ffe07
--- /dev/null
+++ b/termios/tcsetwinsize.c
@@ -0,0 +1,39 @@
+/* Copyright (C) 2023 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <errno.h>
+#include <termios.h>
+
+/* Set the winsize of FD to *WINSIZE_P.  */
+int
+tcsetwinsize (int fd, const struct winsize *winsize_p)
+{
+  if (fd < 0)
+    {
+      __set_errno (EBADF);
+      return -1;
+    }
+  if (winsize_p == NULL)
+    {
+      __set_errno (EINVAL);
+      return -1;
+    }
+
+  __set_errno (ENOSYS);
+  return -1;
+}
+stub_warning (tcsetwinsize)
diff --git a/termios/termios.h b/termios/termios.h
index c974960580..1e972b292f 100644
--- a/termios/termios.h
+++ b/termios/termios.h
@@ -99,6 +99,17 @@ extern int tcflow (int __fd, int __action) __THROW;
 extern __pid_t tcgetsid (int __fd) __THROW;
 #endif
 
+#ifdef __USE_GNU
+/* Get the definition of 'struct winsize', which is the type of ARG for
+   TIOCGWINSZ and TIOCSWINSZ requests. */
+#include <bits/winsize.h>
+
+/* Get the winsize of FD in *WINSIZE_P.  */
+extern int tcgetwinsize (int __fd, struct winsize *__winsize_p) __THROW;
+
+/* Set the winsize of FD to *WINSIZE_P.  */
+extern int tcsetwinsize (int __fd, const struct winsize *__winsize_p) __THROW;
+#endif
 
 #ifdef __USE_MISC
 # include <sys/ttydefaults.h>
-- 
2.30.2

