OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / libgo / go / 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         "os"
10         "strings"
11 )
12
13 // ErrNotFound is the error resulting if a path search failed to find an executable file.
14 var ErrNotFound = errors.New("executable file not found in $path")
15
16 func findExecutable(file string) error {
17         d, err := os.Stat(file)
18         if err != nil {
19                 return err
20         }
21         if d.IsRegular() && d.Permission()&0111 != 0 {
22                 return nil
23         }
24         return os.EPERM
25 }
26
27 // LookPath searches for an executable binary named file
28 // in the directories named by the path environment variable.
29 // If file begins with "/", "#", "./", or "../", it is tried
30 // directly and the path is not consulted.
31 func LookPath(file string) (string, error) {
32         // skip the path lookup for these prefixes
33         skip := []string{"/", "#", "./", "../"}
34
35         for _, p := range skip {
36                 if strings.HasPrefix(file, p) {
37                         err := findExecutable(file)
38                         if err == nil {
39                                 return file, nil
40                         }
41                         return "", &Error{file, err}
42                 }
43         }
44
45         path := os.Getenv("path")
46         for _, dir := range strings.Split(path, "\000") {
47                 if err := findExecutable(dir + "/" + file); err == nil {
48                         return dir + "/" + file, nil
49                 }
50         }
51         return "", &Error{file, ErrNotFound}
52 }