func GetLogin() (string, error) { // GetLogin is based on stdin, so find the name of the current terminal // and return nothing if it doesn't exist. According to GNU, this is what // DEC Unix, SunOS, Solaris, and HP-UX all do. name, err := ttyname.TtyName(os.Stdin.Fd()) if err != nil { return "", err } u := new(utmp.Utmp) _ = copy(u.Line[:], []byte(name[5:])) file, err := os.Open(utmp.UtmpFile) if err != nil { return "", err } line, err := u.GetUtLine(file) if err != nil { return "", err } return string(line.User[:]), nil }
func main() { flag.Usage = func() { fmt.Fprintf(os.Stderr, "%s\n", HELP) return } flag.Parse() if *version { fmt.Printf("%s\n", VERSION) return } silent := *quiet1 || *quiet2 si := os.Stdin.Fd() tty, err := ttyname.TtyName(si) if !silent { if err == ttyname.NotTty { fmt.Println("not a tty") os.Exit(1) } if tty != "" { fmt.Println(tty) return } else { fmt.Println("tty") } } if err != nil { os.Exit(1) } }
func scanEntries(n uint64, u *utmp.UtmpBuffer) { var name string if *heading { fmt.Println("HEADING") } if *cur { si := os.Stdin.Fd() stat := syscall.Stat_t{} _ = syscall.Fstat(int(si), &stat) name, err := ttyname.TtyName(stat, ttyname.dev) if err != nil { return } if bytes.Compare([]byte(name), dev) == 0 { if strings.HasPrefix(name, string(dev)) { name = name[5:] } } } for _, v := range *u { if !*cur || bytes.Compare([]byte(name), v.Line[:]) == 0 { switch true { case *users && v.IsUserProcess(): fmt.Println("print user") case *rlvl && v.Type == utmp.RunLevel: fmt.Println("print run level") case *boot && v.Type == utmp.BootTime: fmt.Println("print boot") case *clock && v.Type == utmp.NewTime: fmt.Println("print time") case *proc && v.Type == utmp.InitProcess: fmt.Println("print initspawn") case *login && v.Type == utmp.LoginProcess: fmt.Println("print login") case *dead && v.Type == utmp.DeadProcess: fmt.Println("print dead procs") } } if v.Type == utmp.BootTime { bt = v.Time.Sec } } }