Commit 2bea3cd

mo khan <mo@mokhan.ca>
2021-10-12 20:48:30
create `which` program
1 parent b73697d
Changed files (1)
which/which.go
@@ -0,0 +1,31 @@
+package main
+
+import (
+	"fmt"
+	"os"
+	"path/filepath"
+)
+
+func main() {
+	arguments := os.Args
+	if len(arguments) == 1 {
+		fmt.Println("Please provide an argument!")
+		return
+	}
+
+	pathes := filepath.SplitList(os.Getenv("PATH"))
+	for _, exe := range arguments[1:] {
+		for _, directory := range pathes {
+			fullPath := filepath.Join(directory, exe)
+			fileInfo, err := os.Stat(fullPath)
+			if err == nil {
+				mode := fileInfo.Mode()
+				if mode.IsRegular() {
+					if mode&0111 != 0 {
+						fmt.Println(fullPath)
+					}
+				}
+			}
+		}
+	}
+}