unix: add ioctl functions to get/set terminal window size on Linux

Add IoctlGetWinsize and IoctlSetWinsize to retreive and manipulate
Winsize structures on Linux, akin to the already existing implementation
on Solaris.

Also remove the named result parameter for IoctlSetInt and
IoctlSetTermios, as they add no additional use.

Change-Id: Id349d1d6a21d5c9a05943f4dcc3a275613ccf7b8
Reviewed-on: https://go-review.googlesource.com/49231
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/unix/linux/types.go b/unix/linux/types.go
index 9b69e27..56eb4c1 100644
--- a/unix/linux/types.go
+++ b/unix/linux/types.go
@@ -28,6 +28,7 @@
 #include <stdio.h>
 #include <sys/epoll.h>
 #include <sys/inotify.h>
+#include <sys/ioctl.h>
 #include <sys/mman.h>
 #include <sys/mount.h>
 #include <sys/param.h>
@@ -543,3 +544,5 @@
 // Terminal handling
 
 type Termios C.termios_t
+
+type Winsize C.struct_winsize
diff --git a/unix/syscall_linux.go b/unix/syscall_linux.go
index a6ac8e9..056f601 100644
--- a/unix/syscall_linux.go
+++ b/unix/syscall_linux.go
@@ -57,11 +57,15 @@
 
 // IoctlSetInt performs an ioctl operation which sets an integer value
 // on fd, using the specified request number.
-func IoctlSetInt(fd int, req uint, value int) (err error) {
+func IoctlSetInt(fd int, req uint, value int) error {
 	return ioctl(fd, req, uintptr(value))
 }
 
-func IoctlSetTermios(fd int, req uint, value *Termios) (err error) {
+func IoctlSetWinsize(fd int, req uint, value *Winsize) error {
+	return ioctl(fd, req, uintptr(unsafe.Pointer(value)))
+}
+
+func IoctlSetTermios(fd int, req uint, value *Termios) error {
 	return ioctl(fd, req, uintptr(unsafe.Pointer(value)))
 }
 
@@ -73,6 +77,12 @@
 	return value, err
 }
 
+func IoctlGetWinsize(fd int, req uint) (*Winsize, error) {
+	var value Winsize
+	err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
+	return &value, err
+}
+
 func IoctlGetTermios(fd int, req uint) (*Termios, error) {
 	var value Termios
 	err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
diff --git a/unix/ztypes_linux_386.go b/unix/ztypes_linux_386.go
index 8111206..fe539a0 100644
--- a/unix/ztypes_linux_386.go
+++ b/unix/ztypes_linux_386.go
@@ -676,3 +676,10 @@
 	Ispeed uint32
 	Ospeed uint32
 }
+
+type Winsize struct {
+	Row    uint16
+	Col    uint16
+	Xpixel uint16
+	Ypixel uint16
+}
diff --git a/unix/ztypes_linux_amd64.go b/unix/ztypes_linux_amd64.go
index 075d9c5..e99cd79 100644
--- a/unix/ztypes_linux_amd64.go
+++ b/unix/ztypes_linux_amd64.go
@@ -694,3 +694,10 @@
 	Ispeed uint32
 	Ospeed uint32
 }
+
+type Winsize struct {
+	Row    uint16
+	Col    uint16
+	Xpixel uint16
+	Ypixel uint16
+}
diff --git a/unix/ztypes_linux_arm.go b/unix/ztypes_linux_arm.go
index a66c160..0857aa6 100644
--- a/unix/ztypes_linux_arm.go
+++ b/unix/ztypes_linux_arm.go
@@ -665,3 +665,10 @@
 	Ispeed uint32
 	Ospeed uint32
 }
+
+type Winsize struct {
+	Row    uint16
+	Col    uint16
+	Xpixel uint16
+	Ypixel uint16
+}
diff --git a/unix/ztypes_linux_arm64.go b/unix/ztypes_linux_arm64.go
index b3b506a..fb1c90a 100644
--- a/unix/ztypes_linux_arm64.go
+++ b/unix/ztypes_linux_arm64.go
@@ -673,3 +673,10 @@
 	Ispeed uint32
 	Ospeed uint32
 }
+
+type Winsize struct {
+	Row    uint16
+	Col    uint16
+	Xpixel uint16
+	Ypixel uint16
+}
diff --git a/unix/ztypes_linux_mips.go b/unix/ztypes_linux_mips.go
index 5c654f5..d15c9f4 100644
--- a/unix/ztypes_linux_mips.go
+++ b/unix/ztypes_linux_mips.go
@@ -670,3 +670,10 @@
 	Ispeed uint32
 	Ospeed uint32
 }
+
+type Winsize struct {
+	Row    uint16
+	Col    uint16
+	Xpixel uint16
+	Ypixel uint16
+}
diff --git a/unix/ztypes_linux_mips64.go b/unix/ztypes_linux_mips64.go
index 3f11fb6..a39d362 100644
--- a/unix/ztypes_linux_mips64.go
+++ b/unix/ztypes_linux_mips64.go
@@ -675,3 +675,10 @@
 	Ispeed uint32
 	Ospeed uint32
 }
+
+type Winsize struct {
+	Row    uint16
+	Col    uint16
+	Xpixel uint16
+	Ypixel uint16
+}
diff --git a/unix/ztypes_linux_mips64le.go b/unix/ztypes_linux_mips64le.go
index 1a4ad57..a05f082 100644
--- a/unix/ztypes_linux_mips64le.go
+++ b/unix/ztypes_linux_mips64le.go
@@ -675,3 +675,10 @@
 	Ispeed uint32
 	Ospeed uint32
 }
+
+type Winsize struct {
+	Row    uint16
+	Col    uint16
+	Xpixel uint16
+	Ypixel uint16
+}
diff --git a/unix/ztypes_linux_mipsle.go b/unix/ztypes_linux_mipsle.go
index b3f0f30..2ecdddc 100644
--- a/unix/ztypes_linux_mipsle.go
+++ b/unix/ztypes_linux_mipsle.go
@@ -670,3 +670,10 @@
 	Ispeed uint32
 	Ospeed uint32
 }
+
+type Winsize struct {
+	Row    uint16
+	Col    uint16
+	Xpixel uint16
+	Ypixel uint16
+}
diff --git a/unix/ztypes_linux_ppc64.go b/unix/ztypes_linux_ppc64.go
index aeee27e..33b8e55 100644
--- a/unix/ztypes_linux_ppc64.go
+++ b/unix/ztypes_linux_ppc64.go
@@ -683,3 +683,10 @@
 	Ispeed uint32
 	Ospeed uint32
 }
+
+type Winsize struct {
+	Row    uint16
+	Col    uint16
+	Xpixel uint16
+	Ypixel uint16
+}
diff --git a/unix/ztypes_linux_ppc64le.go b/unix/ztypes_linux_ppc64le.go
index b8cb2c3..987d481 100644
--- a/unix/ztypes_linux_ppc64le.go
+++ b/unix/ztypes_linux_ppc64le.go
@@ -683,3 +683,10 @@
 	Ispeed uint32
 	Ospeed uint32
 }
+
+type Winsize struct {
+	Row    uint16
+	Col    uint16
+	Xpixel uint16
+	Ypixel uint16
+}
diff --git a/unix/ztypes_linux_s390x.go b/unix/ztypes_linux_s390x.go
index 58883f9..cf539ca 100644
--- a/unix/ztypes_linux_s390x.go
+++ b/unix/ztypes_linux_s390x.go
@@ -700,3 +700,10 @@
 	Ispeed uint32
 	Ospeed uint32
 }
+
+type Winsize struct {
+	Row    uint16
+	Col    uint16
+	Xpixel uint16
+	Ypixel uint16
+}