Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Theophanes authored and Daniel Theophanes committed May 5, 2012
0 parents commit d29bca0
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
8 changes: 8 additions & 0 deletions osext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package osext

// Returns the full path of the running executable
// as reported by the system. Includes the executable
// image name.
func GetExePath() (exePath string, err error) {
return getExePath()
}
8 changes: 8 additions & 0 deletions osext_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package osext

import "errors"

// _NSGetExecutablePath()
func getExePath() (exePath string, err error) {
return "", errors.New("Unimplemented")
}
7 changes: 7 additions & 0 deletions osext_freebsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package osext

// FreeBSD: sysctl CTL_KERN KERN_PROC KERN_PROC_PATHNAME -1
// BSD with procfs: readlink /proc/curproc/file
func getExePath() (exePath string, err error) {
return os.Readlink(`/proc/curproc/file`)
}
8 changes: 8 additions & 0 deletions osext_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package osext

import "os"

// readlink /proc/self/exe
func getExePath() (exePath string, err error) {
return os.Readlink(`/proc/self/exe`)
}
11 changes: 11 additions & 0 deletions osext_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package osext

import "testing"

func TestGetExePath(t *testing.T) {
p, err := GetExePath()
if err != nil {
t.Fatal(err)
}
t.Logf("IMG EXE PATH: %s", p)
}
29 changes: 29 additions & 0 deletions osext_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package osext

import (
"unsafe"
"utf16"
)

var (
kernel = syscall.MustLoadDLL("kernel32.dll")
getModuleFileNameProc = kernel.MustFindProc("GetModuleFileNameW")
)

// GetModuleFileName() with hModule = NULL
func getExePath() (exePath string, err error) {
return getModuleFileName()
}

func getModuleFileName() (string, error) {
var n uint32
b := make([]uint16, syscall.MAX_PATH)
size := uint32(len(b))

r0, _, e1 := getModuleFileNameProc.Call(0, uintptr(unsafe.Pointer(&b[0])), uintptr(size))
n = uint32(r0)
if n == 0 {
return "", e1
}
return string(utf16.Decode(b[0:n])), nil
}

0 comments on commit d29bca0

Please sign in to comment.