Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove CGO dependency from daemon_darwin.go #32

Closed
wants to merge 1 commit into from
Closed
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
29 changes: 2 additions & 27 deletions daemon_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,13 @@ package godaemon
// Copyright (c) 2013 VividCortex, Inc. All rights reserved.
// Please see the LICENSE file for applicable license terms.

//#include <mach-o/dyld.h>
import "C"

import (
"bytes"
"fmt"
"path/filepath"
"unsafe"
"os"
)

// GetExecutablePath returns the absolute path to the currently running
// executable. It is used internally by the godaemon package, and exported
// publicly because it's useful outside of the package too.
func GetExecutablePath() (string, error) {
PATH_MAX := 1024 // From <sys/syslimits.h>
exePath := make([]byte, PATH_MAX)
exeLen := C.uint32_t(len(exePath))

status, err := C._NSGetExecutablePath((*C.char)(unsafe.Pointer(&exePath[0])), &exeLen)

if err != nil {
return "", fmt.Errorf("_NSGetExecutablePath: %v", err)
}

// Not sure why this might happen with err being nil, but...
if status != 0 {
return "", fmt.Errorf("_NSGetExecutablePath returned %d", status)
}

// Convert from null-padded []byte to a clean string. (Can't simply cast!)
exePathStringLen := bytes.Index(exePath, []byte{0})
exePathString := string(exePath[:exePathStringLen])

return filepath.Clean(exePathString), nil
return os.Executable()
}