<termios.h>: add two new POSIX functions.

musl already added tcgetwinsize() and tcsetwinsize(), but I didn't
notice.

Trivial single-line inlines added to a header that's already written
that way.

Test: treehugger
Change-Id: Iac95ea6a89f3872025c512f7e61987b81d0aafa7
diff --git a/tests/headers/posix/termios_h.c b/tests/headers/posix/termios_h.c
index 1255c16..0a67eaa 100644
--- a/tests/headers/posix/termios_h.c
+++ b/tests/headers/posix/termios_h.c
@@ -42,6 +42,12 @@
   STRUCT_MEMBER(struct termios, tcflag_t, c_lflag);
   STRUCT_MEMBER_ARRAY(struct termios, cc_t/*[]*/, c_cc);
 
+#if !defined(__GLIBC__)  // Our glibc is too old.
+  TYPE(struct winsize);
+  STRUCT_MEMBER(struct winsize, unsigned short, ws_row);
+  STRUCT_MEMBER(struct winsize, unsigned short, ws_col);
+#endif
+
   MACRO(NCCS);
 
   MACRO(VEOF);
@@ -162,6 +168,12 @@
   FUNCTION(tcflush, int (*f)(int, int));
   FUNCTION(tcgetattr, int (*f)(int, struct termios*));
   FUNCTION(tcgetsid, pid_t (*f)(int));
+#if !defined(__GLIBC__)  // Our glibc is too old.
+  FUNCTION(tcgetwinsize, int (*f)(int, struct winsize*));
+#endif
   FUNCTION(tcsendbreak, int (*f)(int, int));
   FUNCTION(tcsetattr, int (*f)(int, int, const struct termios*));
+#if !defined(__GLIBC__)  // Our glibc is too old.
+  FUNCTION(tcsetwinsize, int (*f)(int, const struct winsize*));
+#endif
 }
diff --git a/tests/termios_test.cpp b/tests/termios_test.cpp
index 6290771..a8d5890 100644
--- a/tests/termios_test.cpp
+++ b/tests/termios_test.cpp
@@ -29,6 +29,8 @@
 #include <termios.h>
 
 #include <errno.h>
+#include <fcntl.h>
+#include <pty.h>
 
 #include <gtest/gtest.h>
 
@@ -96,3 +98,49 @@
   EXPECT_EQ(1, t.c_cc[VMIN]);
   EXPECT_EQ(0, t.c_cc[VTIME]);
 }
+
+TEST(termios, tcgetwinsize_tcsetwinsize_invalid) {
+#if !defined(__GLIBC__)
+  winsize ws = {};
+
+  errno = 0;
+  ASSERT_EQ(-1, tcgetwinsize(-1, &ws));
+  ASSERT_EQ(EBADF, errno);
+
+  errno = 0;
+  ASSERT_EQ(-1, tcsetwinsize(-1, &ws));
+  ASSERT_EQ(EBADF, errno);
+#else
+  GTEST_SKIP() << "glibc too old";
+#endif
+}
+
+TEST(termios, tcgetwinsize_tcsetwinsize) {
+#if !defined(__GLIBC__)
+  int pty, tty;
+  winsize ws = {123, 456, 9999, 9999};
+  ASSERT_EQ(0, openpty(&pty, &tty, nullptr, nullptr, &ws));
+
+  winsize actual = {};
+  ASSERT_EQ(0, tcgetwinsize(tty, &actual));
+  EXPECT_EQ(ws.ws_xpixel, actual.ws_xpixel);
+  EXPECT_EQ(ws.ws_ypixel, actual.ws_ypixel);
+  EXPECT_EQ(ws.ws_row, actual.ws_row);
+  EXPECT_EQ(ws.ws_col, actual.ws_col);
+
+  ws = {1, 2, 3, 4};
+  ASSERT_EQ(0, tcsetwinsize(tty, &ws));
+
+  actual = {};
+  ASSERT_EQ(0, tcgetwinsize(tty, &actual));
+  EXPECT_EQ(ws.ws_xpixel, actual.ws_xpixel);
+  EXPECT_EQ(ws.ws_ypixel, actual.ws_ypixel);
+  EXPECT_EQ(ws.ws_row, actual.ws_row);
+  EXPECT_EQ(ws.ws_col, actual.ws_col);
+
+  close(pty);
+  close(tty);
+#else
+  GTEST_SKIP() << "glibc too old";
+#endif
+}