winsize_solaris_cgo.go 924 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // +build solaris,cgo
  2. package term
  3. import (
  4. "unsafe"
  5. "golang.org/x/sys/unix"
  6. )
  7. /*
  8. #include <unistd.h>
  9. #include <stropts.h>
  10. #include <termios.h>
  11. // Small wrapper to get rid of variadic args of ioctl()
  12. int my_ioctl(int fd, int cmd, struct winsize *ws) {
  13. return ioctl(fd, cmd, ws);
  14. }
  15. */
  16. import "C"
  17. // GetWinsize returns the window size based on the specified file descriptor.
  18. func GetWinsize(fd uintptr) (*Winsize, error) {
  19. ws := &Winsize{}
  20. ret, err := C.my_ioctl(C.int(fd), C.int(unix.TIOCGWINSZ), (*C.struct_winsize)(unsafe.Pointer(ws)))
  21. // Skip retval = 0
  22. if ret == 0 {
  23. return ws, nil
  24. }
  25. return ws, err
  26. }
  27. // SetWinsize tries to set the specified window size for the specified file descriptor.
  28. func SetWinsize(fd uintptr, ws *Winsize) error {
  29. ret, err := C.my_ioctl(C.int(fd), C.int(unix.TIOCSWINSZ), (*C.struct_winsize)(unsafe.Pointer(ws)))
  30. // Skip retval = 0
  31. if ret == 0 {
  32. return nil
  33. }
  34. return err
  35. }