| 1 |
// Copyright 2016 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 os |
| 6 |
|
| 7 |
import ( |
| 8 |
"syscall" |
| 9 |
_ "unsafe" // for linkname |
| 10 |
) |
| 11 |
|
| 12 |
//go:linkname executablePath |
| 13 |
var executablePath string // set by sysauxv in ../runtime/os3_solaris.go |
| 14 |
|
| 15 |
var initCwd, initCwdErr = Getwd() |
| 16 |
|
| 17 |
func executable() (string, error) { |
| 18 |
path := executablePath |
| 19 |
if len(path) == 0 { |
| 20 |
path, err := syscall.Getexecname() |
| 21 |
if err != nil { |
| 22 |
return path, err |
| 23 |
} |
| 24 |
} |
| 25 |
if len(path) > 0 && path[0] != '/' { |
| 26 |
if initCwdErr != nil { |
| 27 |
return path, initCwdErr |
| 28 |
} |
| 29 |
if len(path) > 2 && path[0:2] == "./" { |
| 30 |
// skip "./" |
| 31 |
path = path[2:] |
| 32 |
} |
| 33 |
return initCwd + "/" + path, nil |
| 34 |
} |
| 35 |
return path, nil |
| 36 |
} |