github.com/apcera/util@v0.0.0-20180322191801-7a50bc84ee48/str/term_linux.go (about)

     1  // Copyright 2012 Apcera Inc. All rights reserved.
     2  
     3  // +build linux
     4  
     5  package str
     6  
     7  // BSD implements isatty() by testing if tcgetattr() errors; some experimental
     8  // Go code from Google (Ian Lance Taylor) used to do the same thing.
     9  
    10  // FreeBSD implements tcgetattr() with TIOCGETA ioctl, but on Ubuntu Linux,
    11  // tty_ioctl(4) documents this as being the TCGETS ioctl instead.  For now,
    12  // we only care about Linux.
    13  
    14  import (
    15  	"os"
    16  	"syscall"
    17  	"unsafe"
    18  )
    19  
    20  // May move to util/file? For now is here because it's the only
    21  // one used by colors.
    22  func IsTerminal(file *os.File) bool {
    23  	var tios syscall.Termios
    24  	_, _, ep := syscall.Syscall(syscall.SYS_IOCTL,
    25  		uintptr(file.Fd()), syscall.TCGETS, uintptr(unsafe.Pointer(&tios)))
    26  	if ep != 0 {
    27  		// syscall failed, it's not a terminal
    28  		return false
    29  	}
    30  	return true
    31  }