Skip to content

Instantly share code, notes, and snippets.

@fsouza
Created May 21, 2012 23:15
Show Gist options
  • Save fsouza/2765319 to your computer and use it in GitHub Desktop.
Save fsouza/2765319 to your computer and use it in GitHub Desktop.
package main
//#include <termios.h>
import "C"
import (
"fmt"
"os"
"syscall"
)
func tcsetattr(fd uintptr, when int, termios *syscall.Termios) {
var cterm C.struct_termios
var cc_t [C.NCCS]C.cc_t
for i, c := range termios.Cc {
cc_t[i] = C.cc_t(c)
}
cterm.c_iflag = C.tcflag_t(termios.Iflag)
cterm.c_oflag = C.tcflag_t(termios.Oflag)
cterm.c_cflag = C.tcflag_t(termios.Cflag)
cterm.c_lflag = C.tcflag_t(termios.Lflag)
cterm.c_cc = cc_t
cterm.c_ispeed = C.speed_t(termios.Ispeed)
cterm.c_ospeed = C.speed_t(termios.Ospeed)
C.tcsetattr(C.int(fd), C.int(when), &cterm)
}
func tcgetattr(fd uintptr, termios *syscall.Termios) {
var cterm C.struct_termios
C.tcgetattr(C.int(fd), &cterm)
var cc [C.NCCS]uint8
for i, c := range cterm.c_cc {
cc[i] = uint8(c)
}
*termios = syscall.Termios{
Iflag: uint64(cterm.c_iflag),
Oflag: uint64(cterm.c_oflag),
Cflag: uint64(cterm.c_cflag),
Lflag: uint64(cterm.c_lflag),
Cc: cc,
Ispeed: uint64(cterm.c_ispeed),
Ospeed: uint64(cterm.c_ospeed),
}
}
func main() {
fd := os.Stdin.Fd()
var termios syscall.Termios
tcgetattr(fd, &termios)
termios.Lflag &^= syscall.ECHO
tcsetattr(fd, 0, &termios)
fmt.Print("Password: ")
var buf [16]byte
var pass []byte
for {
n, _ := syscall.Read(int(fd), buf[:])
if n == 0 {
if len(pass) == 0 {
panic("eof")
}
break
}
if buf[n-1] == '\n' {
n--
}
pass = append(pass, buf[:n]...)
if n < len(buf) {
break
}
}
fmt.Println(string(pass))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment