• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5package os
6
7import (
8	"errors"
9	_ "unsafe" // for linkname
10)
11
12//go:linkname executablePath
13var executablePath string // set by ../runtime/os_darwin.go
14
15var initCwd, initCwdErr = Getwd()
16
17func executable() (string, error) {
18	ep := executablePath
19	if len(ep) == 0 {
20		return ep, errors.New("cannot find executable path")
21	}
22	if ep[0] != '/' {
23		if initCwdErr != nil {
24			return ep, initCwdErr
25		}
26		if len(ep) > 2 && ep[0:2] == "./" {
27			// skip "./"
28			ep = ep[2:]
29		}
30		ep = initCwd + "/" + ep
31	}
32	return ep, nil
33}
34