Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions src/os/executable_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,5 @@ package os
func runtime_executable_path() string

func Executable() (string, error) {
p := runtime_executable_path()
if p != "" && p[0] == '/' {
// absolute path
return p, nil
}
cwd, err := Getwd()
if err != nil {
return "", err
}
return joinPath(cwd, p), nil
return runtime_executable_path(), nil
}
65 changes: 60 additions & 5 deletions src/runtime/os_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,18 @@ func call_syscall6X(fn, a1, a2, a3, a4, a5, a6 uintptr) uintptr

//go:linkname os_runtime_executable_path os.runtime_executable_path
func os_runtime_executable_path() string {
argv := (*unsafe.Pointer)(unsafe.Pointer(main_argv))
return executablePath
}

var executablePath string

func platform_argv(argc int32, argv *unsafe.Pointer) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'm not sure I like this name. What about storeExecutablePath or something like that?

executablePath = executable_path(argc, argv)
}

func executable_path(argc int32, argv *unsafe.Pointer) string {
// skip over argv
argv = (*unsafe.Pointer)(unsafe.Add(unsafe.Pointer(argv), (uintptr(main_argc)+1)*unsafe.Sizeof(argv)))
argv = (*unsafe.Pointer)(unsafe.Add(unsafe.Pointer(argv), (uintptr(argc)+1)*unsafe.Sizeof(argv)))

// skip over envv
for (*argv) != nil {
Expand All @@ -251,11 +259,21 @@ func os_runtime_executable_path() string {
length: length,
ptr: (*byte)(cstr),
}
executablePath := *(*string)(unsafe.Pointer(&argString))
path := *(*string)(unsafe.Pointer(&argString))

// strip "executable_path=" prefix if available, it's added after OS X 10.11.
executablePath = stringsTrimPrefix(executablePath, "executable_path=")
return executablePath
path = stringsTrimPrefix(path, "executable_path=")

if path != "" && path[0] == '/' {
// absolute path
return path
}

cwd := getcwd()
if cwd != "" {
path = joinPath(cwd, path)
}
return path
}

func stringsTrimPrefix(s, prefix string) string {
Expand All @@ -264,3 +282,40 @@ func stringsTrimPrefix(s, prefix string) string {
}
return s
}

func joinPath(dir, name string) string {
if len(dir) > 0 && dir[len(dir)-1] == '/' {
return dir + name
}
return dir + "/" + name
}

// from syscall
func getcwd() string {
const pathMax = 1024
var buf [4 * pathMax]byte
Comment on lines +295 to +296
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use 1024 and then use 4 times that size in the actual buffer? (Also, it would help to give a pointer to why that is the right value, like a link to a man page or something).

s := libc_getcwd(&buf[0], uint(len(buf)))
if s == nil {
return ""
}
n := clen(buf[:])
if n < 1 {
return ""
}
return string(buf[:n])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could consider using unsafe.String here to avoid an allocation. It should be safe, since buf is never modified afterwards. (The potential downside is that executablePath will then always be 4kB in size - not sure whether that's a good tradeoff).

}

// clen returns the index of the first NULL byte in n or len(n) if n contains no NULL byte.
func clen(n []byte) int {
for i := 0; i < len(n); i++ {
if n[i] == 0 {
return i
}
}
return len(n)
}

// char *getcwd(char *buf, size_t size)
//
//export getcwd
func libc_getcwd(buf *byte, size uint) *byte
4 changes: 4 additions & 0 deletions src/runtime/os_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,7 @@ func fcntl(fd int32, cmd int32, arg int32) (ret int32, errno int32) {
errno = *libc_errno_location()
return
}

func platform_argv(argc int32, argv *unsafe.Pointer) {
// nothing
}
3 changes: 3 additions & 0 deletions src/runtime/runtime_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ func main(argc int32, argv *unsafe.Pointer) int {
// The run function has been moved to a separate (non-inlined) function so
// that the correct stack pointer is read.
stackTop = getCurrentStackPointer()

platform_argv(main_argc, main_argv)

runMain()

// For libc compatibility.
Expand Down