Source file src/os/exec/lp_unix.go

     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 unix
     6  
     7  package exec
     8  
     9  import (
    10  	"errors"
    11  	"internal/godebug"
    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  // Otherwise, on success, the result is an absolute path.
    36  //
    37  // In older versions of Go, LookPath could return a path relative to the current directory.
    38  // As of Go 1.19, LookPath will instead return that path along with an error satisfying
    39  // errors.Is(err, ErrDot). See the package documentation for more details.
    40  func LookPath(file string) (string, error) {
    41  	// NOTE(rsc): I wish we could use the Plan 9 behavior here
    42  	// (only bypass the path if file begins with / or ./ or ../)
    43  	// but that would not match all the Unix shells.
    44  
    45  	if strings.Contains(file, "/") {
    46  		err := findExecutable(file)
    47  		if err == nil {
    48  			return file, nil
    49  		}
    50  		return "", &Error{file, err}
    51  	}
    52  	path := os.Getenv("PATH")
    53  	for _, dir := range filepath.SplitList(path) {
    54  		if dir == "" {
    55  			// Unix shell semantics: path element "" means "."
    56  			dir = "."
    57  		}
    58  		path := filepath.Join(dir, file)
    59  		if err := findExecutable(path); err == nil {
    60  			if !filepath.IsAbs(path) && godebug.Get("execerrdot") != "0" {
    61  				return path, &Error{file, ErrDot}
    62  			}
    63  			return path, nil
    64  		}
    65  	}
    66  	return "", &Error{file, ErrNotFound}
    67  }
    68  

View as plain text