[ARM] Fix typo in comment in arm_expand_prologue
[official-gcc.git] / libgo / go / os / executable_path.go
blob117320d7949452b5eb11bf32d418db0f57f4ff99
1 // Copyright 2017 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.
5 // +build aix
7 package os
9 // We query the working directory at init, to use it later to search for the
10 // executable file
11 // errWd will be checked later, if we need to use initWd
12 var initWd, errWd = Getwd()
14 func executable() (string, error) {
15 var err error
16 var exePath string
17 if len(Args) == 0 || Args[0] == "" {
18 return "", ErrNotExist
20 // Args[0] is an absolute path : this is the executable
21 if IsPathSeparator(Args[0][0]) {
22 exePath = Args[0]
23 } else {
24 for i := 1; i < len(Args[0]); i++ {
25 // Args[0] is a relative path : append current directory
26 if IsPathSeparator(Args[0][i]) {
27 if errWd != nil {
28 return "", errWd
30 exePath = initWd + string(PathSeparator) + Args[0]
31 break
35 if exePath != "" {
36 err = isExecutable(exePath)
37 if err == nil {
38 return exePath, nil
40 // File does not exist or is not executable,
41 // this is an unexpected situation !
42 return "", err
44 // Search for executable in $PATH
45 for _, dir := range splitPathList(Getenv("PATH")) {
46 if len(dir) == 0 {
47 continue
49 if !IsPathSeparator(dir[0]) {
50 if errWd != nil {
51 return "", errWd
53 dir = initWd + string(PathSeparator) + dir
55 exePath = dir + string(PathSeparator) + Args[0]
56 err = isExecutable(exePath)
57 if err == nil {
58 return exePath, nil
60 if err == ErrPermission {
61 return "", err
64 return "", ErrNotExist
67 // isExecutable returns an error if a given file is not an executable.
68 func isExecutable(path string) error {
69 stat, err := Stat(path)
70 if err != nil {
71 return err
73 mode := stat.Mode()
74 if !mode.IsRegular() {
75 return ErrPermission
77 if (mode & 0111) != 0 {
78 return nil
80 return ErrPermission
83 // splitPathList splits a path list.
84 // This is based on genSplit from strings/strings.go
85 func splitPathList(pathList string) []string {
86 n := 1
87 for i := 0; i < len(pathList); i++ {
88 if pathList[i] == PathListSeparator {
89 n++
92 start := 0
93 a := make([]string, n)
94 na := 0
95 for i := 0; i+1 <= len(pathList) && na+1 < n; i++ {
96 if pathList[i] == PathListSeparator {
97 a[na] = pathList[start:i]
98 na++
99 start = i + 1
102 a[na] = pathList[start:]
103 return a[:na+1]