blob: dae9f4ee18ecb96aa32712d1375a8dd2cc8fb6d0 [file] [log] [blame]
Shenghou Ma2fc67e72015-11-01 04:18:58 -05001// 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
5package os
6
Roberto Clapisfaadda02018-08-02 15:27:14 +02007import "errors"
8
Shenghou Ma2fc67e72015-11-01 04:18:58 -05009var executablePath string // set by ../runtime/os_darwin.go
10
11var initCwd, initCwdErr = Getwd()
12
13func executable() (string, error) {
14 ep := executablePath
Roberto Clapisfaadda02018-08-02 15:27:14 +020015 if len(ep) == 0 {
16 return ep, errors.New("cannot find executable path")
17 }
Shenghou Ma2fc67e72015-11-01 04:18:58 -050018 if ep[0] != '/' {
19 if initCwdErr != nil {
20 return ep, initCwdErr
21 }
22 if len(ep) > 2 && ep[0:2] == "./" {
23 // skip "./"
24 ep = ep[2:]
25 }
26 ep = initCwd + "/" + ep
27 }
28 return ep, nil
29}