Skip to content
Myr

Implement ttyname() in case /dev/tty is not available

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Close #1266 Close #447

authored by Junegunn Choi · committed by Junegunn Choi · parent f57920a · Browse files · 3 files+44 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+44 added−0 removed

  • other:+44 added−0 removed

3 files+44 added−0 removed

modified:src/tui/light.go+6 added
30 unmodified lines
3132func openTtyIn() *os.File {33 in, err := os.OpenFile(consoleDevice, syscall.O_RDONLY, 0)34 if err != nil {35 tty := ttyname()36 if len(tty) > 0 {37 if in, err := os.OpenFile(tty, syscall.O_RDONLY, 0); err == nil {38 return in39 }40 }41 fmt.Fprintln(os.Stderr, "Failed to open "+consoleDevice)42 os.Exit(2)43 }44 return in
889 unmodified lines
added:src/tui/ttyname_unix.go+31 added
1// +build !windows23package tui45import (6 "io/ioutil"7 "syscall"8)910var devPrefixes = [...]string{"/dev/pts/", "/dev/"}1112func ttyname() string {13 var stderr syscall.Stat_t14 if syscall.Fstat(2, &stderr) != nil {15 return ""16 }1718 for _, prefix := range devPrefixes {19 files, err := ioutil.ReadDir(prefix)20 if err != nil {21 continue22 }2324 for _, file := range files {25 if stat, ok := file.Sys().(*syscall.Stat_t); ok && stat.Rdev == stderr.Rdev {26 return prefix + file.Name()27 }28 }29 }30 return ""31}
added:src/tui/ttyname_windows.go+7 added
1// +build windows23package tui45func ttyname() string {6 return ""7}