...

Source file src/os/exec/lp_unix.go

Documentation: os/exec

		 1  // Copyright 2010 The Go Authors. All rights reserved.
		 2  // Use of this source code is governed by a BSD-style
		 3  // license that can be found in the LICENSE file.
		 4  
		 5  //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
		 6  // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
		 7  
		 8  package exec
		 9  
		10  import (
		11  	"errors"
		12  	"io/fs"
		13  	"os"
		14  	"path/filepath"
		15  	"strings"
		16  )
		17  
		18  // ErrNotFound is the error resulting if a path search failed to find an executable file.
		19  var ErrNotFound = errors.New("executable file not found in $PATH")
		20  
		21  func findExecutable(file string) error {
		22  	d, err := os.Stat(file)
		23  	if err != nil {
		24  		return err
		25  	}
		26  	if m := d.Mode(); !m.IsDir() && m&0111 != 0 {
		27  		return nil
		28  	}
		29  	return fs.ErrPermission
		30  }
		31  
		32  // LookPath searches for an executable named file in the
		33  // directories named by the PATH environment variable.
		34  // If file contains a slash, it is tried directly and the PATH is not consulted.
		35  // The result may be an absolute path or a path relative to the current directory.
		36  func LookPath(file string) (string, error) {
		37  	// NOTE(rsc): I wish we could use the Plan 9 behavior here
		38  	// (only bypass the path if file begins with / or ./ or ../)
		39  	// but that would not match all the Unix shells.
		40  
		41  	if strings.Contains(file, "/") {
		42  		err := findExecutable(file)
		43  		if err == nil {
		44  			return file, nil
		45  		}
		46  		return "", &Error{file, err}
		47  	}
		48  	path := os.Getenv("PATH")
		49  	for _, dir := range filepath.SplitList(path) {
		50  		if dir == "" {
		51  			// Unix shell semantics: path element "" means "."
		52  			dir = "."
		53  		}
		54  		path := filepath.Join(dir, file)
		55  		if err := findExecutable(path); err == nil {
		56  			return path, nil
		57  		}
		58  	}
		59  	return "", &Error{file, ErrNotFound}
		60  }
		61  

View as plain text