Joe Poirier | b155a76 | 2010-09-12 17:38:36 +1000 | [diff] [blame] | 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 | |
Dave Cheney | 7c8280c | 2014-02-25 09:47:42 -0500 | [diff] [blame] | 5 | // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris |
Russ Cox | 2715956 | 2011-09-15 16:48:57 -0400 | [diff] [blame] | 6 | |
Joe Poirier | b155a76 | 2010-09-12 17:38:36 +1000 | [diff] [blame] | 7 | package exec |
| 8 | |
| 9 | import ( |
Russ Cox | c2049d2 | 2011-11-01 22:04:37 -0400 | [diff] [blame] | 10 | "errors" |
Joe Poirier | b155a76 | 2010-09-12 17:38:36 +1000 | [diff] [blame] | 11 | "os" |
| 12 | "strings" |
| 13 | ) |
| 14 | |
Rob Pike | 4e9e925 | 2011-06-03 07:48:06 +1000 | [diff] [blame] | 15 | // ErrNotFound is the error resulting if a path search failed to find an executable file. |
Russ Cox | c2049d2 | 2011-11-01 22:04:37 -0400 | [diff] [blame] | 16 | var ErrNotFound = errors.New("executable file not found in $PATH") |
Rob Pike | 4e9e925 | 2011-06-03 07:48:06 +1000 | [diff] [blame] | 17 | |
Russ Cox | c2049d2 | 2011-11-01 22:04:37 -0400 | [diff] [blame] | 18 | func findExecutable(file string) error { |
Joe Poirier | b155a76 | 2010-09-12 17:38:36 +1000 | [diff] [blame] | 19 | d, err := os.Stat(file) |
| 20 | if err != nil { |
Rob Pike | 4e9e925 | 2011-06-03 07:48:06 +1000 | [diff] [blame] | 21 | return err |
Joe Poirier | b155a76 | 2010-09-12 17:38:36 +1000 | [diff] [blame] | 22 | } |
Russ Cox | 8dce57e | 2011-11-30 12:04:16 -0500 | [diff] [blame] | 23 | if m := d.Mode(); !m.IsDir() && m&0111 != 0 { |
Rob Pike | 4e9e925 | 2011-06-03 07:48:06 +1000 | [diff] [blame] | 24 | return nil |
| 25 | } |
Rob Pike | 56069f0 | 2012-02-17 10:04:29 +1100 | [diff] [blame] | 26 | return os.ErrPermission |
Joe Poirier | b155a76 | 2010-09-12 17:38:36 +1000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | // LookPath searches for an executable binary named file |
| 30 | // in the directories named by the PATH environment variable. |
| 31 | // If file contains a slash, it is tried directly and the PATH is not consulted. |
Rob Pike | a41a5bb | 2013-08-15 14:29:04 +1000 | [diff] [blame] | 32 | // The result may be an absolute path or a path relative to the current directory. |
Russ Cox | c2049d2 | 2011-11-01 22:04:37 -0400 | [diff] [blame] | 33 | func LookPath(file string) (string, error) { |
Joe Poirier | b155a76 | 2010-09-12 17:38:36 +1000 | [diff] [blame] | 34 | // NOTE(rsc): I wish we could use the Plan 9 behavior here |
| 35 | // (only bypass the path if file begins with / or ./ or ../) |
| 36 | // but that would not match all the Unix shells. |
| 37 | |
Brad Fitzpatrick | e198a50 | 2010-11-01 14:32:48 -0700 | [diff] [blame] | 38 | if strings.Contains(file, "/") { |
Rob Pike | 4e9e925 | 2011-06-03 07:48:06 +1000 | [diff] [blame] | 39 | err := findExecutable(file) |
| 40 | if err == nil { |
Joe Poirier | b155a76 | 2010-09-12 17:38:36 +1000 | [diff] [blame] | 41 | return file, nil |
| 42 | } |
Rob Pike | 4e9e925 | 2011-06-03 07:48:06 +1000 | [diff] [blame] | 43 | return "", &Error{file, err} |
Joe Poirier | b155a76 | 2010-09-12 17:38:36 +1000 | [diff] [blame] | 44 | } |
| 45 | pathenv := os.Getenv("PATH") |
Péter Surányi | fe7dbea | 2013-02-07 06:41:35 -0800 | [diff] [blame] | 46 | if pathenv == "" { |
| 47 | return "", &Error{file, ErrNotFound} |
| 48 | } |
Rob Pike | ebb1566 | 2011-06-28 09:43:14 +1000 | [diff] [blame] | 49 | for _, dir := range strings.Split(pathenv, ":") { |
Joe Poirier | b155a76 | 2010-09-12 17:38:36 +1000 | [diff] [blame] | 50 | if dir == "" { |
| 51 | // Unix shell semantics: path element "" means "." |
| 52 | dir = "." |
| 53 | } |
Gustavo Niemeyer | 01a0d39 | 2012-01-19 20:17:46 -0200 | [diff] [blame] | 54 | path := dir + "/" + file |
| 55 | if err := findExecutable(path); err == nil { |
| 56 | return path, nil |
Joe Poirier | b155a76 | 2010-09-12 17:38:36 +1000 | [diff] [blame] | 57 | } |
| 58 | } |
Rob Pike | 4e9e925 | 2011-06-03 07:48:06 +1000 | [diff] [blame] | 59 | return "", &Error{file, ErrNotFound} |
Joe Poirier | b155a76 | 2010-09-12 17:38:36 +1000 | [diff] [blame] | 60 | } |