No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.go 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. )
  8. var all = flag.Bool("a", false, "list all instances of executables found (instead of just the first one of each).")
  9. var silent = flag.Bool("s", false, "No output, just return 0 if all of the executables are found, or 1 if some were not found.")
  10. func main() {
  11. flag.Parse()
  12. paths := filepath.SplitList(os.Getenv("PATH"))
  13. rc := 0
  14. for _, cmd := range flag.Args() {
  15. found := false
  16. for _, dir := range paths {
  17. path := filepath.Join(dir, cmd)
  18. if err := findExecutable(path); err == nil {
  19. found = true
  20. if !*silent {
  21. fmt.Println(path)
  22. }
  23. if !*all {
  24. break
  25. }
  26. }
  27. }
  28. if !found {
  29. if !*silent {
  30. fmt.Printf("%v not found\n", cmd)
  31. }
  32. rc++
  33. }
  34. }
  35. if rc > 0 {
  36. rc = 1
  37. }
  38. os.Exit(rc)
  39. }
  40. // findExecutable is from the stdlib: https://golang.org/src/os/exec/lp_unix.go?s=458:647#L19
  41. func findExecutable(file string) error {
  42. d, err := os.Stat(file)
  43. if err != nil {
  44. return err
  45. }
  46. if m := d.Mode(); !m.IsDir() && m&0111 != 0 {
  47. return nil
  48. }
  49. return os.ErrPermission
  50. }