From ea1e2cf89475d6e9544b65c0edb863253c82e189 Mon Sep 17 00:00:00 2001 From: stephen mcquay Date: Sat, 20 Jan 2018 17:15:09 -0800 Subject: [PATCH] init --- LICENSE | 27 +++++++++++++++++++++++++++ README.md | 3 +++ main.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 main.go diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..56c859c --- /dev/null +++ b/LICENSE @@ -0,0 +1,27 @@ +Copyright (c) 2018, Stephen McQuay + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of authpixious nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..135217d --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# wch is just which + +I wanted to try my hand at another unix utility; this is it. diff --git a/main.go b/main.go new file mode 100644 index 0000000..de3f86a --- /dev/null +++ b/main.go @@ -0,0 +1,56 @@ +package main + +import ( + "flag" + "fmt" + "os" + "path/filepath" +) + +var all = flag.Bool("a", false, "list all instances of executables found (instead of just the first one of each).") +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.") + +func main() { + flag.Parse() + + paths := filepath.SplitList(os.Getenv("PATH")) + + rc := 0 + for _, cmd := range flag.Args() { + found := false + for _, dir := range paths { + path := filepath.Join(dir, cmd) + if err := findExecutable(path); err == nil { + found = true + if !*silent { + fmt.Println(path) + } + if !*all { + break + } + } + } + if !found { + if !*silent { + fmt.Printf("%v not found\n", cmd) + } + rc++ + } + } + if rc > 0 { + rc = 1 + } + os.Exit(rc) +} + +// findExecutable is from the stdlib: https://golang.org/src/os/exec/lp_unix.go?s=458:647#L19 +func findExecutable(file string) error { + d, err := os.Stat(file) + if err != nil { + return err + } + if m := d.Mode(); !m.IsDir() && m&0111 != 0 { + return nil + } + return os.ErrPermission +}