This commit is contained in:
Stephen McQuay 2018-01-20 17:15:09 -08:00
commit ea1e2cf894
Signed by: sm
GPG Key ID: 4E4B72F479BA3CE5
3 changed files with 86 additions and 0 deletions

27
LICENSE Normal file
View File

@ -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.

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# wch is just which
I wanted to try my hand at another unix utility; this is it.

56
main.go Normal file
View File

@ -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
}