ioctl_unsigned.go raw

   1  // Copyright 2018 The Go Authors. All rights reserved.
   2  // Use of this source code is governed by a BSD-style
   3  // license that can be found in the LICENSE file.
   4  
   5  //go:build darwin || dragonfly || freebsd || hurd || linux || netbsd || openbsd
   6  
   7  package unix
   8  
   9  import "unsafe"
  10  
  11  // ioctl itself should not be exposed directly, but additional get/set
  12  // functions for specific types are permissible.
  13  
  14  // IoctlSetInt performs an ioctl operation which sets an integer value
  15  // on fd, using the specified request number.
  16  func IoctlSetInt(fd int, req uint, value int) error {
  17  	return ioctl(fd, req, uintptr(value))
  18  }
  19  
  20  // IoctlSetPointerInt performs an ioctl operation which sets an
  21  // integer value on fd, using the specified request number. The ioctl
  22  // argument is called with a pointer to the integer value, rather than
  23  // passing the integer value directly.
  24  func IoctlSetPointerInt(fd int, req uint, value int) error {
  25  	v := int32(value)
  26  	return ioctlPtr(fd, req, unsafe.Pointer(&v))
  27  }
  28  
  29  // IoctlSetString performs an ioctl operation which sets a string value
  30  // on fd, using the specified request number.
  31  func IoctlSetString(fd int, req uint, value string) error {
  32  	bs := append([]byte(value), 0)
  33  	return ioctlPtr(fd, req, unsafe.Pointer(&bs[0]))
  34  }
  35  
  36  // IoctlSetWinsize performs an ioctl on fd with a *Winsize argument.
  37  //
  38  // To change fd's window size, the req argument should be TIOCSWINSZ.
  39  func IoctlSetWinsize(fd int, req uint, value *Winsize) error {
  40  	// TODO: if we get the chance, remove the req parameter and
  41  	// hardcode TIOCSWINSZ.
  42  	return ioctlPtr(fd, req, unsafe.Pointer(value))
  43  }
  44  
  45  // IoctlSetTermios performs an ioctl on fd with a *Termios.
  46  //
  47  // The req value will usually be TCSETA or TIOCSETA.
  48  func IoctlSetTermios(fd int, req uint, value *Termios) error {
  49  	// TODO: if we get the chance, remove the req parameter.
  50  	return ioctlPtr(fd, req, unsafe.Pointer(value))
  51  }
  52  
  53  // IoctlGetInt performs an ioctl operation which gets an integer value
  54  // from fd, using the specified request number.
  55  //
  56  // A few ioctl requests use the return value as an output parameter;
  57  // for those, IoctlRetInt should be used instead of this function.
  58  func IoctlGetInt(fd int, req uint) (int, error) {
  59  	var value int
  60  	err := ioctlPtr(fd, req, unsafe.Pointer(&value))
  61  	return value, err
  62  }
  63  
  64  func IoctlGetWinsize(fd int, req uint) (*Winsize, error) {
  65  	var value Winsize
  66  	err := ioctlPtr(fd, req, unsafe.Pointer(&value))
  67  	return &value, err
  68  }
  69  
  70  func IoctlGetTermios(fd int, req uint) (*Termios, error) {
  71  	var value Termios
  72  	err := ioctlPtr(fd, req, unsafe.Pointer(&value))
  73  	return &value, err
  74  }
  75