Added the PATH searching algorithm we talked about at the last meeting with Ciaraldi. It will find the location of our program

and search the PATH for that location. If found, it will search all remaining elements for the requested program. If the location
is not found on the PATH, then it will search the entire PATH for the requested program. If found, it returns the absolute path
to the program. If not, err.
This commit is contained in:
Fredric Silberberg 2014-09-24 23:32:11 -04:00
parent f11cbeaf22
commit f21df51bd2
4 changed files with 71 additions and 7 deletions

View File

@ -95,7 +95,7 @@ func main() {
flag.Parse()
if !*jsonModePtr {
fmt.Printf("%s", jsh.Fallback("/usr/bin/free"))
fmt.Printf("%s", jsh.Fallback("free"))
} else {
runJsonMode()
}

View File

@ -2,8 +2,8 @@ package main
import (
"jsh"
"testing"
"reflect"
"testing"
)
func testUnitError(expected jsh.Unit, t *testing.T) {

View File

@ -25,7 +25,7 @@ func PsOutputToProcesses(out string) *[]jsh.Process {
func runJsonMode() {
// Run procps-ng "ps" with full output
psOut := string(*jsh.FallbackWithArgs("/usr/bin/ps", []string{"auxww"}))
psOut := string(*jsh.FallbackWithArgs("ps", []string{"auxww"}))
processesPtr := PsOutputToProcesses(psOut)
frame := jsh.JshFrame{*processesPtr, []string{}}
@ -43,7 +43,7 @@ func main() {
flag.Parse()
if !*jsonModePtr {
fmt.Printf("%s", jsh.Fallback("/usr/bin/ps"))
fmt.Printf("%s", jsh.Fallback("ps"))
} else {
runJsonMode()
}

View File

@ -1,9 +1,12 @@
package jsh
import (
"errors"
"fmt"
"math"
"os"
"os/exec"
"strings"
"unicode"
)
@ -52,7 +55,13 @@ func FallbackWithArgs(program string, args []string) *[]byte {
if args == nil {
args = os.Args[1:]
}
out, err := exec.Command(program, args...).Output()
executable, err := findExecutable(program)
if err != nil {
panic(err)
}
out, err := exec.Command(executable, args...).Output()
if err != nil {
panic(err)
}
@ -62,3 +71,58 @@ func FallbackWithArgs(program string, args []string) *[]byte {
func Fallback(program string) *[]byte {
return FallbackWithArgs(program, nil)
}
// Finds a given executable on the system path, returning the absolute path the program if found, and an error
// if it's not found
func findExecutable(programName string) (string, error) {
// First, find our binary location
binLocWithName, err := os.Readlink("/proc/self/exe")
if err != nil {
return "", err
}
// Get our binary name and remove it from the binary path, leaving just our binary location
// Also trim the extra path separator
binName := os.Args[0]
binLoc := strings.TrimSuffix(binLocWithName, binName)
binLoc = strings.TrimSuffix(binLoc, string(os.PathSeparator))
// Get the system path and split it by the system path separator
pathString := os.Getenv("PATH")
path := strings.Split(pathString, string(os.PathListSeparator))
// Loop until we find our location on the path, so we know where to start looking after for the
// next implementation of the given program name
var foundPath bool
var pathNum int
for index, pathEl := range path {
// Trim the path separator from this element, if it has it
pathEl = strings.TrimSuffix(pathEl, string(os.PathSeparator))
if pathEl == binLoc {
foundPath = true
pathNum = index
break
}
}
// If we found our path on the real path, then search the remaining path elements. Otherwise, search all elements
if foundPath {
return searchPath(programName, path[pathNum+1:len(path)-1])
} else {
return searchPath(programName, path)
}
}
// Searches the given set of paths for a the given program, returing when it finds it or returning an error if the
// program is not found
func searchPath(programName string, path []string) (string, error) {
// Loop through each path element, test for the existance of the file, and then return if we find it
for _, pathEl := range path {
fullProgram := pathEl + string(os.PathSeparator) + programName
_, err := os.Stat(fullProgram)
if err == nil {
return fullProgram, nil
}
}
return "", errors.New(fmt.Sprintf("Could not find program %s on the PATH: %s", programName, path))
}