OSDN Git Service

Update Go library to r60.
[pf3gnuchains/gcc-fork.git] / libgo / go / exec / lp_windows.go
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
5 package exec
6
7 import (
8         "os"
9         "strings"
10 )
11
12 // ErrNotFound is the error resulting if a path search failed to find an executable file.
13 var ErrNotFound = os.NewError("executable file not found in %PATH%")
14
15 func chkStat(file string) os.Error {
16         d, err := os.Stat(file)
17         if err != nil {
18                 return err
19         }
20         if d.IsRegular() {
21                 return nil
22         }
23         return os.EPERM
24 }
25
26 func findExecutable(file string, exts []string) (string, os.Error) {
27         if len(exts) == 0 {
28                 return file, chkStat(file)
29         }
30         f := strings.ToLower(file)
31         for _, e := range exts {
32                 if strings.HasSuffix(f, e) {
33                         return file, chkStat(file)
34                 }
35         }
36         for _, e := range exts {
37                 if f := file + e; chkStat(f) == nil {
38                         return f, nil
39                 }
40         }
41         return ``, os.ENOENT
42 }
43
44 func LookPath(file string) (f string, err os.Error) {
45         x := os.Getenv(`PATHEXT`)
46         if x == `` {
47                 x = `.COM;.EXE;.BAT;.CMD`
48         }
49         exts := []string{}
50         for _, e := range strings.Split(strings.ToLower(x), `;`) {
51                 if e == "" {
52                         continue
53                 }
54                 if e[0] != '.' {
55                         e = "." + e
56                 }
57                 exts = append(exts, e)
58         }
59         if strings.IndexAny(file, `:\/`) != -1 {
60                 if f, err = findExecutable(file, exts); err == nil {
61                         return
62                 }
63                 return ``, &Error{file, err}
64         }
65         if pathenv := os.Getenv(`PATH`); pathenv == `` {
66                 if f, err = findExecutable(`.\`+file, exts); err == nil {
67                         return
68                 }
69         } else {
70                 for _, dir := range strings.Split(pathenv, `;`) {
71                         if f, err = findExecutable(dir+`\`+file, exts); err == nil {
72                                 return
73                         }
74                 }
75         }
76         return ``, &Error{file, ErrNotFound}
77 }