2010-09-12 17:38:36 +10:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
2011-12-18 02:29:18 +11:00
|
|
|
// +build darwin freebsd linux netbsd openbsd
|
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"
|
2010-09-12 17:38:36 +10:00
|
|
|
"os"
|
|
|
|
|
"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
|
|
|
}
|
2011-06-03 07:48:06 +10:00
|
|
|
return os.EPERM
|
2010-09-12 17:38:36 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LookPath searches for an executable binary named file
|
|
|
|
|
// in the directories named by the PATH environment variable.
|
|
|
|
|
// If file contains a slash, it is tried directly and the PATH is not consulted.
|
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
|
|
|
}
|
|
|
|
|
pathenv := os.Getenv("PATH")
|
2011-06-28 09:43:14 +10:00
|
|
|
for _, dir := range strings.Split(pathenv, ":") {
|
2010-09-12 17:38:36 +10:00
|
|
|
if dir == "" {
|
|
|
|
|
// Unix shell semantics: path element "" means "."
|
|
|
|
|
dir = "."
|
|
|
|
|
}
|
2011-06-03 07:48:06 +10:00
|
|
|
if err := findExecutable(dir + "/" + file); err == nil {
|
2010-09-12 17:38:36 +10:00
|
|
|
return dir + "/" + file, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-06-03 07:48:06 +10:00
|
|
|
return "", &Error{file, ErrNotFound}
|
2010-09-12 17:38:36 +10:00
|
|
|
}
|