Source file src/os/exec/lp_plan9.go

     1  // Copyright 2011 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  package exec
     6  
     7  import (
     8  	"errors"
     9  	"io/fs"
    10  	"os"
    11  	"path/filepath"
    12  	"strings"
    13  )
    14  
    15  // ErrNotFound is the error resulting if a path search failed to find an executable file.
    16  var ErrNotFound = errors.New("executable file not found in $path")
    17  
    18  func findExecutable(file string) error {
    19  	d, err := os.Stat(file)
    20  	if err != nil {
    21  		return err
    22  	}
    23  	if m := d.Mode(); !m.IsDir() && m&0111 != 0 {
    24  		return nil
    25  	}
    26  	return fs.ErrPermission
    27  }
    28  
    29  // LookPath searches for an executable named file in the
    30  // directories named by the path environment variable.
    31  // If file begins with "/", "#", "./", or "../", it is tried
    32  // directly and the path is not consulted.
    33  // On success, the result is an absolute path.
    34  //
    35  // In older versions of Go, LookPath could return a path relative to the current directory.
    36  // As of Go 1.19, LookPath will instead return that path along with an error satisfying
    37  // [errors.Is](err, [ErrDot]). See the package documentation for more details.
    38  func LookPath(file string) (string, error) {
    39  	if err := validateLookPath(file); err != nil {
    40  		return "", &Error{file, err}
    41  	}
    42  
    43  	// skip the path lookup for these prefixes
    44  	skip := []string{"/", "#", "./", "../"}
    45  
    46  	for _, p := range skip {
    47  		if strings.HasPrefix(file, p) {
    48  			err := findExecutable(file)
    49  			if err == nil {
    50  				return file, nil
    51  			}
    52  			return "", &Error{file, err}
    53  		}
    54  	}
    55  
    56  	path := os.Getenv("path")
    57  	for _, dir := range filepath.SplitList(path) {
    58  		path := filepath.Join(dir, file)
    59  		if err := findExecutable(path); err == nil {
    60  			if !filepath.IsAbs(path) {
    61  				if execerrdot.Value() != "0" {
    62  					return path, &Error{file, ErrDot}
    63  				}
    64  				execerrdot.IncNonDefault()
    65  			}
    66  			return path, nil
    67  		}
    68  	}
    69  	return "", &Error{file, ErrNotFound}
    70  }
    71  
    72  // lookExtensions is a no-op on non-Windows platforms, since
    73  // they do not restrict executables to specific extensions.
    74  func lookExtensions(path, dir string) (string, error) {
    75  	return path, nil
    76  }
    77  

View as plain text