2016-03-01 22:57:46 +00:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
2010-09-12 17:38:36 +10:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
2022-03-03 18:23:35 -08:00
|
|
|
//go:build unix
|
build: add build comments to core packages
The go/build package already recognizes
system-specific file names like
mycode_darwin.go
mycode_darwin_386.go
mycode_386.s
However, it is also common to write files that
apply to multiple architectures, so a recent CL added
to go/build the ability to process comments
listing a set of conditions for building. For example:
// +build darwin freebsd openbsd/386
says that this file should be compiled only on
OS X, FreeBSD, or 32-bit x86 OpenBSD systems.
These conventions are not yet documented
(hence this long CL description).
This CL adds build comments to the multi-system
files in the core library, a step toward making it
possible to use go/build to build them.
With this change go/build can handle crypto/rand,
exec, net, path/filepath, os/user, and time.
os and syscall need additional adjustments.
R=golang-dev, r, gri, r, gustavo
CC=golang-dev
https://golang.org/cl/5011046
2011-09-15 16:48:57 -04:00
|
|
|
|
2010-09-12 17:38:36 +10:00
|
|
|
package exec
|
|
|
|
|
|
|
|
|
|
import (
|
2011-11-01 22:04:37 -04:00
|
|
|
"errors"
|
2022-07-27 14:36:05 -04:00
|
|
|
"internal/godebug"
|
2020-07-07 13:49:21 -04:00
|
|
|
"io/fs"
|
2010-09-12 17:38:36 +10:00
|
|
|
"os"
|
2015-12-26 16:50:01 +09:00
|
|
|
"path/filepath"
|
2010-09-12 17:38:36 +10:00
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2011-06-03 07:48:06 +10:00
|
|
|
// ErrNotFound is the error resulting if a path search failed to find an executable file.
|
2011-11-01 22:04:37 -04:00
|
|
|
var ErrNotFound = errors.New("executable file not found in $PATH")
|
2011-06-03 07:48:06 +10:00
|
|
|
|
2011-11-01 22:04:37 -04:00
|
|
|
func findExecutable(file string) error {
|
2010-09-12 17:38:36 +10:00
|
|
|
d, err := os.Stat(file)
|
|
|
|
|
if err != nil {
|
2011-06-03 07:48:06 +10:00
|
|
|
return err
|
|
|
|
|
}
|
2011-11-30 12:04:16 -05:00
|
|
|
if m := d.Mode(); !m.IsDir() && m&0111 != 0 {
|
2011-06-03 07:48:06 +10:00
|
|
|
return nil
|
2010-09-12 17:38:36 +10:00
|
|
|
}
|
2020-07-07 13:49:21 -04:00
|
|
|
return fs.ErrPermission
|
2010-09-12 17:38:36 +10:00
|
|
|
}
|
|
|
|
|
|
2018-01-26 22:29:55 -04:00
|
|
|
// LookPath searches for an executable named file in the
|
|
|
|
|
// directories named by the PATH environment variable.
|
2010-09-12 17:38:36 +10:00
|
|
|
// If file contains a slash, it is tried directly and the PATH is not consulted.
|
2022-04-29 20:05:26 -04:00
|
|
|
// Otherwise, on success, the result is an absolute path.
|
|
|
|
|
//
|
|
|
|
|
// In older versions of Go, LookPath could return a path relative to the current directory.
|
|
|
|
|
// As of Go 1.19, LookPath will instead return that path along with an error satisfying
|
|
|
|
|
// errors.Is(err, ErrDot). See the package documentation for more details.
|
2011-11-01 22:04:37 -04:00
|
|
|
func LookPath(file string) (string, error) {
|
2010-09-12 17:38:36 +10:00
|
|
|
// NOTE(rsc): I wish we could use the Plan 9 behavior here
|
|
|
|
|
// (only bypass the path if file begins with / or ./ or ../)
|
|
|
|
|
// but that would not match all the Unix shells.
|
|
|
|
|
|
2010-11-01 14:32:48 -07:00
|
|
|
if strings.Contains(file, "/") {
|
2011-06-03 07:48:06 +10:00
|
|
|
err := findExecutable(file)
|
|
|
|
|
if err == nil {
|
2010-09-12 17:38:36 +10:00
|
|
|
return file, nil
|
|
|
|
|
}
|
2011-06-03 07:48:06 +10:00
|
|
|
return "", &Error{file, err}
|
2010-09-12 17:38:36 +10:00
|
|
|
}
|
2015-12-26 16:50:01 +09:00
|
|
|
path := os.Getenv("PATH")
|
|
|
|
|
for _, dir := range filepath.SplitList(path) {
|
2010-09-12 17:38:36 +10:00
|
|
|
if dir == "" {
|
|
|
|
|
// Unix shell semantics: path element "" means "."
|
|
|
|
|
dir = "."
|
|
|
|
|
}
|
2015-12-26 16:50:01 +09:00
|
|
|
path := filepath.Join(dir, file)
|
2012-01-19 20:17:46 -02:00
|
|
|
if err := findExecutable(path); err == nil {
|
2022-07-27 14:36:05 -04:00
|
|
|
if !filepath.IsAbs(path) && godebug.Get("execerrdot") != "0" {
|
2022-04-29 20:05:26 -04:00
|
|
|
return path, &Error{file, ErrDot}
|
|
|
|
|
}
|
2012-01-19 20:17:46 -02:00
|
|
|
return path, nil
|
2010-09-12 17:38:36 +10:00
|
|
|
}
|
|
|
|
|
}
|
2011-06-03 07:48:06 +10:00
|
|
|
return "", &Error{file, ErrNotFound}
|
2010-09-12 17:38:36 +10:00
|
|
|
}
|