Example #1
0
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)
	}
}
Example #2
0
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
		}
	}
}